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.app.ActivityManager; 20 import android.app.IActivityManager; 21 import android.content.Context; 22 import android.os.RemoteException; 23 import android.support.annotation.VisibleForTesting; 24 import android.support.v7.preference.ListPreference; 25 import android.support.v7.preference.Preference; 26 27 import com.android.settings.R; 28 import com.android.settings.core.PreferenceControllerMixin; 29 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 30 31 public class BackgroundProcessLimitPreferenceController extends 32 DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener, 33 PreferenceControllerMixin { 34 35 private static final String APP_PROCESS_LIMIT_KEY = "app_process_limit"; 36 37 private final String[] mListValues; 38 private final String[] mListSummaries; 39 40 public BackgroundProcessLimitPreferenceController(Context context) { 41 super(context); 42 43 mListValues = context.getResources().getStringArray(R.array.app_process_limit_values); 44 mListSummaries = context.getResources().getStringArray(R.array.app_process_limit_entries); 45 } 46 47 @Override 48 public String getPreferenceKey() { 49 return APP_PROCESS_LIMIT_KEY; 50 } 51 52 @Override 53 public boolean onPreferenceChange(Preference preference, Object newValue) { 54 writeAppProcessLimitOptions(newValue); 55 updateAppProcessLimitOptions(); 56 return true; 57 } 58 59 @Override 60 public void updateState(Preference preference) { 61 updateAppProcessLimitOptions(); 62 } 63 64 @Override 65 protected void onDeveloperOptionsSwitchDisabled() { 66 super.onDeveloperOptionsSwitchDisabled(); 67 writeAppProcessLimitOptions(null); 68 } 69 70 private void updateAppProcessLimitOptions() { 71 try { 72 final int limit = getActivityManagerService().getProcessLimit(); 73 int index = 0; // default 74 for (int i = 0; i < mListValues.length; i++) { 75 int val = Integer.parseInt(mListValues[i]); 76 if (val >= limit) { 77 index = i; 78 break; 79 } 80 } 81 final ListPreference listPreference = (ListPreference) mPreference; 82 listPreference.setValue(mListValues[index]); 83 listPreference.setSummary(mListSummaries[index]); 84 } catch (RemoteException e) { 85 // intentional no-op 86 } 87 } 88 89 private void writeAppProcessLimitOptions(Object newValue) { 90 try { 91 final int limit = newValue != null ? Integer.parseInt(newValue.toString()) : -1; 92 getActivityManagerService().setProcessLimit(limit); 93 updateAppProcessLimitOptions(); 94 } catch (RemoteException e) { 95 // intentional no-op 96 } 97 } 98 99 @VisibleForTesting 100 IActivityManager getActivityManagerService() { 101 return ActivityManager.getService(); 102 } 103 } 104