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.core.PreferenceControllerMixin;
33 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
34 
35 
36 public class CachedAppsFreezerPreferenceController extends DeveloperOptionsPreferenceController
37         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
38 
39     @VisibleForTesting
40     private static final String CACHED_APPS_FREEZER_KEY = "cached_apps_freezer";
41 
42     private final String[] mListValues;
43     private final String[] mListSummaries;
44 
CachedAppsFreezerPreferenceController(Context context)45     public CachedAppsFreezerPreferenceController(Context context) {
46         super(context);
47 
48         mListValues = context.getResources()
49                 .getStringArray(com.android.settingslib.R.array.cached_apps_freezer_values);
50         mListSummaries = context.getResources().getStringArray(
51                 com.android.settingslib.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(
80                             com.android.settingslib.R.string.cached_apps_freezer_reboot_dialog_text)
81                     .setPositiveButton(android.R.string.ok, getRebootDialogOkListener(newValue))
82                     .setNegativeButton(android.R.string.cancel, getRebootDialogCancelListener())
83                     .create();
84             dialog.show();
85         }
86 
87         return true;
88     }
89 
getRebootDialogOkListener(Object newValue)90     private DialogInterface.OnClickListener getRebootDialogOkListener(Object newValue) {
91         return (dialog, which) -> {
92             Settings.Global.putString(mContext.getContentResolver(),
93                     Settings.Global.CACHED_APPS_FREEZER_ENABLED,
94                     newValue.toString());
95 
96             updateState(mPreference);
97 
98             PowerManager pm = mContext.getSystemService(PowerManager.class);
99             pm.reboot(null);
100         };
101     }
102 
getRebootDialogCancelListener()103     private DialogInterface.OnClickListener getRebootDialogCancelListener() {
104         return (dialog, which) -> {
105             updateState(mPreference);
106         };
107     }
108 
109     @Override
110     public void updateState(Preference preference) {
111         final ListPreference listPreference = (ListPreference) preference;
112         final String currentValue = Settings.Global.getString(mContext.getContentResolver(),
113                 Settings.Global.CACHED_APPS_FREEZER_ENABLED);
114 
115         int index = 0; // Defaults to device default
116         for (int i = 0; i < mListValues.length; i++) {
117             if (TextUtils.equals(currentValue, mListValues[i])) {
118                 index = i;
119                 break;
120             }
121         }
122 
123         listPreference.setValue(mListValues[index]);
124         listPreference.setSummary(mListSummaries[index]);
125     }
126 
127     @Override
128     public void onDeveloperOptionsDisabled() {
129         super.onDeveloperOptionsDisabled();
130 
131         Settings.Global.putString(mContext.getContentResolver(),
132                 Settings.Global.CACHED_APPS_FREEZER_ENABLED,
133                 mListValues[0].toString());
134     }
135 }
136