| jeffreyhuang | a8ae717 | 2017-10-12 12:28:32 -0700 | [diff] [blame] | 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; |
| Fan Zhang | de11704 | 2018-09-04 13:52:15 -0700 | [diff] [blame^] | 23 | |
| Aurimas Liutikas | 03fcde3 | 2018-04-17 11:22:43 -0700 | [diff] [blame] | 24 | import androidx.annotation.VisibleForTesting; |
| 25 | import androidx.preference.ListPreference; |
| 26 | import androidx.preference.Preference; |
| jeffreyhuang | a8ae717 | 2017-10-12 12:28:32 -0700 | [diff] [blame] | 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 BackgroundProcessLimitPreferenceController extends |
| 33 | DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener, |
| 34 | PreferenceControllerMixin { |
| 35 | |
| 36 | private static final String APP_PROCESS_LIMIT_KEY = "app_process_limit"; |
| 37 | |
| 38 | private final String[] mListValues; |
| 39 | private final String[] mListSummaries; |
| jeffreyhuang | a8ae717 | 2017-10-12 12:28:32 -0700 | [diff] [blame] | 40 | |
| 41 | public BackgroundProcessLimitPreferenceController(Context context) { |
| 42 | super(context); |
| 43 | |
| 44 | mListValues = context.getResources().getStringArray(R.array.app_process_limit_values); |
| 45 | mListSummaries = context.getResources().getStringArray(R.array.app_process_limit_entries); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public String getPreferenceKey() { |
| 50 | return APP_PROCESS_LIMIT_KEY; |
| 51 | } |
| 52 | |
| 53 | @Override |
| jeffreyhuang | a8ae717 | 2017-10-12 12:28:32 -0700 | [diff] [blame] | 54 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 55 | writeAppProcessLimitOptions(newValue); |
| 56 | updateAppProcessLimitOptions(); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void updateState(Preference preference) { |
| 62 | updateAppProcessLimitOptions(); |
| 63 | } |
| 64 | |
| 65 | @Override |
| jeffreyhuang | a8ae717 | 2017-10-12 12:28:32 -0700 | [diff] [blame] | 66 | protected void onDeveloperOptionsSwitchDisabled() { |
| Doris Ling | 4fbf04c | 2018-03-01 10:33:14 -0800 | [diff] [blame] | 67 | super.onDeveloperOptionsSwitchDisabled(); |
| jeffreyhuang | a8ae717 | 2017-10-12 12:28:32 -0700 | [diff] [blame] | 68 | writeAppProcessLimitOptions(null); |
| jeffreyhuang | a8ae717 | 2017-10-12 12:28:32 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | private void updateAppProcessLimitOptions() { |
| 72 | try { |
| 73 | final int limit = getActivityManagerService().getProcessLimit(); |
| 74 | int index = 0; // default |
| 75 | for (int i = 0; i < mListValues.length; i++) { |
| 76 | int val = Integer.parseInt(mListValues[i]); |
| 77 | if (val >= limit) { |
| 78 | index = i; |
| 79 | break; |
| 80 | } |
| 81 | } |
| Doris Ling | 4fbf04c | 2018-03-01 10:33:14 -0800 | [diff] [blame] | 82 | final ListPreference listPreference = (ListPreference) mPreference; |
| 83 | listPreference.setValue(mListValues[index]); |
| 84 | listPreference.setSummary(mListSummaries[index]); |
| jeffreyhuang | a8ae717 | 2017-10-12 12:28:32 -0700 | [diff] [blame] | 85 | } catch (RemoteException e) { |
| 86 | // intentional no-op |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private void writeAppProcessLimitOptions(Object newValue) { |
| 91 | try { |
| 92 | final int limit = newValue != null ? Integer.parseInt(newValue.toString()) : -1; |
| 93 | getActivityManagerService().setProcessLimit(limit); |
| 94 | updateAppProcessLimitOptions(); |
| 95 | } catch (RemoteException e) { |
| 96 | // intentional no-op |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @VisibleForTesting |
| 101 | IActivityManager getActivityManagerService() { |
| 102 | return ActivityManager.getService(); |
| 103 | } |
| 104 | } |