1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.development; 18 19 import android.content.Context; 20 import android.content.pm.IShortcutService; 21 import android.os.RemoteException; 22 import android.os.ServiceManager; 23 import android.text.TextUtils; 24 import android.util.Log; 25 import android.widget.Toast; 26 27 import androidx.preference.Preference; 28 29 import com.android.settings.R; 30 import com.android.settings.core.PreferenceControllerMixin; 31 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 32 33 public class ShortcutManagerThrottlingPreferenceController extends 34 DeveloperOptionsPreferenceController implements PreferenceControllerMixin { 35 36 private static final String TAG = "ShortcutMgrPrefCtrl"; 37 38 private static final String SHORTCUT_MANAGER_RESET_KEY = "reset_shortcut_manager_throttling"; 39 40 private final IShortcutService mShortcutService; 41 ShortcutManagerThrottlingPreferenceController(Context context)42 public ShortcutManagerThrottlingPreferenceController(Context context) { 43 super(context); 44 45 mShortcutService = getShortCutService(); 46 } 47 48 @Override getPreferenceKey()49 public String getPreferenceKey() { 50 return SHORTCUT_MANAGER_RESET_KEY; 51 } 52 53 @Override handlePreferenceTreeClick(Preference preference)54 public boolean handlePreferenceTreeClick(Preference preference) { 55 if (!TextUtils.equals(SHORTCUT_MANAGER_RESET_KEY, preference.getKey())) { 56 return false; 57 } 58 resetShortcutManagerThrottling(); 59 return true; 60 } 61 resetShortcutManagerThrottling()62 private void resetShortcutManagerThrottling() { 63 if (mShortcutService == null) { 64 return; 65 } 66 try { 67 mShortcutService.resetThrottling(); 68 Toast.makeText(mContext, R.string.reset_shortcut_manager_throttling_complete, 69 Toast.LENGTH_SHORT).show(); 70 } catch (RemoteException e) { 71 Log.e(TAG, "Failed to reset rate limiting", e); 72 } 73 } 74 getShortCutService()75 private IShortcutService getShortCutService() { 76 try { 77 return IShortcutService.Stub.asInterface( 78 ServiceManager.getService(Context.SHORTCUT_SERVICE)); 79 } catch (VerifyError e) { 80 // Used for tests since Robolectric cannot initialize this class. 81 return null; 82 } 83 } 84 } 85