Home | History | Annotate | Download | only in development
      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.support.v7.preference.Preference;
     24 import android.text.TextUtils;
     25 import android.util.Log;
     26 import android.widget.Toast;
     27 
     28 import com.android.settings.R;
     29 import com.android.settings.core.PreferenceControllerMixin;
     30 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
     31 
     32 public class ShortcutManagerThrottlingPreferenceController extends
     33         DeveloperOptionsPreferenceController implements PreferenceControllerMixin {
     34 
     35     private static final String TAG = "ShortcutMgrPrefCtrl";
     36 
     37     private static final String SHORTCUT_MANAGER_RESET_KEY = "reset_shortcut_manager_throttling";
     38 
     39     private final IShortcutService mShortcutService;
     40 
     41     public ShortcutManagerThrottlingPreferenceController(Context context) {
     42         super(context);
     43 
     44         mShortcutService = getShortCutService();
     45     }
     46 
     47     @Override
     48     public String getPreferenceKey() {
     49         return SHORTCUT_MANAGER_RESET_KEY;
     50     }
     51 
     52     @Override
     53     public boolean handlePreferenceTreeClick(Preference preference) {
     54         if (!TextUtils.equals(SHORTCUT_MANAGER_RESET_KEY, preference.getKey())) {
     55             return false;
     56         }
     57         resetShortcutManagerThrottling();
     58         return true;
     59     }
     60 
     61     private void resetShortcutManagerThrottling() {
     62         if (mShortcutService == null) {
     63             return;
     64         }
     65         try {
     66             mShortcutService.resetThrottling();
     67             Toast.makeText(mContext, R.string.reset_shortcut_manager_throttling_complete,
     68                     Toast.LENGTH_SHORT).show();
     69         } catch (RemoteException e) {
     70             Log.e(TAG, "Failed to reset rate limiting", e);
     71         }
     72     }
     73 
     74     private IShortcutService getShortCutService() {
     75         try {
     76             return IShortcutService.Stub.asInterface(
     77                     ServiceManager.getService(Context.SHORTCUT_SERVICE));
     78         } catch (VerifyError e) {
     79             // Used for tests since Robolectric cannot initialize this class.
     80             return null;
     81         }
     82     }
     83 }
     84