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