1 /* 2 * Copyright (C) 2020 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 package com.android.settings.development; 17 18 import android.app.ActivityManager; 19 import android.content.Context; 20 import android.content.DialogInterface; 21 import android.os.PowerManager; 22 import android.os.RemoteException; 23 import android.provider.Settings; 24 import android.text.TextUtils; 25 import android.util.Log; 26 27 import androidx.annotation.VisibleForTesting; 28 import androidx.appcompat.app.AlertDialog; 29 import androidx.preference.ListPreference; 30 import androidx.preference.Preference; 31 32 import com.android.settings.R; 33 import com.android.settings.core.PreferenceControllerMixin; 34 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 35 36 37 public class CachedAppsFreezerPreferenceController extends DeveloperOptionsPreferenceController 38 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { 39 40 @VisibleForTesting 41 private static final String CACHED_APPS_FREEZER_KEY = "cached_apps_freezer"; 42 43 private final String[] mListValues; 44 private final String[] mListSummaries; 45 CachedAppsFreezerPreferenceController(Context context)46 public CachedAppsFreezerPreferenceController(Context context) { 47 super(context); 48 49 mListValues = context.getResources().getStringArray(R.array.cached_apps_freezer_values); 50 mListSummaries = context.getResources().getStringArray( 51 R.array.cached_apps_freezer_entries); 52 } 53 54 @Override isAvailable()55 public boolean isAvailable() { 56 boolean available = false; 57 58 try { 59 available = ActivityManager.getService().isAppFreezerSupported(); 60 } catch (RemoteException e) { 61 Log.w(TAG, "Unable to obtain freezer support status from ActivityManager"); 62 } 63 64 return available; 65 } 66 67 @Override getPreferenceKey()68 public String getPreferenceKey() { 69 return CACHED_APPS_FREEZER_KEY; 70 } 71 72 @Override onPreferenceChange(Preference preference, Object newValue)73 public boolean onPreferenceChange(Preference preference, Object newValue) { 74 final String currentValue = Settings.Global.getString(mContext.getContentResolver(), 75 Settings.Global.CACHED_APPS_FREEZER_ENABLED); 76 77 if (!newValue.equals(currentValue)) { 78 final AlertDialog dialog = new AlertDialog.Builder(mContext) 79 .setMessage(R.string.cached_apps_freezer_reboot_dialog_text) 80 .setPositiveButton(android.R.string.ok, getRebootDialogOkListener(newValue)) 81 .setNegativeButton(android.R.string.cancel, getRebootDialogCancelListener()) 82 .create(); 83 dialog.show(); 84 } 85 86 return true; 87 } 88 getRebootDialogOkListener(Object newValue)89 private DialogInterface.OnClickListener getRebootDialogOkListener(Object newValue) { 90 return (dialog, which) -> { 91 Settings.Global.putString(mContext.getContentResolver(), 92 Settings.Global.CACHED_APPS_FREEZER_ENABLED, 93 newValue.toString()); 94 95 updateState(mPreference); 96 97 PowerManager pm = mContext.getSystemService(PowerManager.class); 98 pm.reboot(null); 99 }; 100 } 101 getRebootDialogCancelListener()102 private DialogInterface.OnClickListener getRebootDialogCancelListener() { 103 return (dialog, which) -> { 104 updateState(mPreference); 105 }; 106 } 107 108 @Override 109 public void updateState(Preference preference) { 110 final ListPreference listPreference = (ListPreference) preference; 111 final String currentValue = Settings.Global.getString(mContext.getContentResolver(), 112 Settings.Global.CACHED_APPS_FREEZER_ENABLED); 113 114 int index = 0; // Defaults to device default 115 for (int i = 0; i < mListValues.length; i++) { 116 if (TextUtils.equals(currentValue, mListValues[i])) { 117 index = i; 118 break; 119 } 120 } 121 122 listPreference.setValue(mListValues[index]); 123 listPreference.setSummary(mListSummaries[index]); 124 } 125 126 @Override 127 public void onDeveloperOptionsDisabled() { 128 super.onDeveloperOptionsDisabled(); 129 130 Settings.Global.putString(mContext.getContentResolver(), 131 Settings.Global.CACHED_APPS_FREEZER_ENABLED, 132 mListValues[0].toString()); 133 } 134 } 135