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.provider.Settings;
24 
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 import androidx.preference.SwitchPreference;
29 
30 import com.android.settings.core.PreferenceControllerMixin;
31 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
32 
33 public class KeepActivitiesPreferenceController extends DeveloperOptionsPreferenceController
34         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
35 
36     private static final String IMMEDIATELY_DESTROY_ACTIVITIES_KEY =
37             "immediately_destroy_activities";
38 
39     @VisibleForTesting
40     static final int SETTING_VALUE_OFF = 0;
41 
42     private IActivityManager mActivityManager;
43 
KeepActivitiesPreferenceController(Context context)44     public KeepActivitiesPreferenceController(Context context) {
45         super(context);
46     }
47 
48     @Override
getPreferenceKey()49     public String getPreferenceKey() {
50         return IMMEDIATELY_DESTROY_ACTIVITIES_KEY;
51     }
52 
53     @Override
displayPreference(PreferenceScreen screen)54     public void displayPreference(PreferenceScreen screen) {
55         super.displayPreference(screen);
56 
57         mActivityManager = getActivityManager();
58     }
59 
60     @Override
onPreferenceChange(Preference preference, Object newValue)61     public boolean onPreferenceChange(Preference preference, Object newValue) {
62         final boolean isEnabled = (Boolean) newValue;
63         writeImmediatelyDestroyActivitiesOptions(isEnabled);
64         return true;
65     }
66 
67     @Override
updateState(Preference preference)68     public void updateState(Preference preference) {
69         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
70                 Settings.Global.ALWAYS_FINISH_ACTIVITIES, SETTING_VALUE_OFF);
71         ((SwitchPreference) mPreference).setChecked(mode != SETTING_VALUE_OFF);
72     }
73 
74     @Override
onDeveloperOptionsSwitchDisabled()75     protected void onDeveloperOptionsSwitchDisabled() {
76         super.onDeveloperOptionsSwitchDisabled();
77         writeImmediatelyDestroyActivitiesOptions(false);
78         ((SwitchPreference) mPreference).setChecked(false);
79     }
80 
writeImmediatelyDestroyActivitiesOptions(boolean isEnabled)81     private void writeImmediatelyDestroyActivitiesOptions(boolean isEnabled) {
82         try {
83             mActivityManager.setAlwaysFinish(isEnabled);
84         } catch (RemoteException ex) {
85             // intentional no-op
86         }
87     }
88 
89     @VisibleForTesting
getActivityManager()90     IActivityManager getActivityManager() {
91         return ActivityManager.getService();
92     }
93 }
94