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 static com.android.settings.development.DevelopmentOptionsActivityRequestCodes.REQUEST_CODE_DEBUG_APP; 20 21 import android.app.Activity; 22 import android.app.ActivityManager; 23 import android.app.IActivityManager; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.os.RemoteException; 27 import android.provider.Settings; 28 import android.text.TextUtils; 29 30 import androidx.annotation.VisibleForTesting; 31 import androidx.preference.Preference; 32 import androidx.preference.SwitchPreference; 33 34 import com.android.settings.core.PreferenceControllerMixin; 35 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 36 37 public class WaitForDebuggerPreferenceController extends DeveloperOptionsPreferenceController 38 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin, 39 OnActivityResultListener { 40 41 private static final String WAIT_FOR_DEBUGGER_KEY = "wait_for_debugger"; 42 43 @VisibleForTesting 44 static final int SETTING_VALUE_ON = 1; 45 @VisibleForTesting 46 static final int SETTING_VALUE_OFF = 0; 47 WaitForDebuggerPreferenceController(Context context)48 public WaitForDebuggerPreferenceController(Context context) { 49 super(context); 50 } 51 52 @Override getPreferenceKey()53 public String getPreferenceKey() { 54 return WAIT_FOR_DEBUGGER_KEY; 55 } 56 57 @Override onPreferenceChange(Preference preference, Object newValue)58 public boolean onPreferenceChange(Preference preference, Object newValue) { 59 final boolean debuggerEnabled = (Boolean) newValue; 60 final String debugApp = Settings.Global.getString( 61 mContext.getContentResolver(), Settings.Global.DEBUG_APP); 62 writeDebuggerAppOptions(debugApp, debuggerEnabled, true /* persistent */); 63 return true; 64 } 65 66 @Override updateState(Preference preference)67 public void updateState(Preference preference) { 68 updateState(mPreference, Settings.Global.getString( 69 mContext.getContentResolver(), Settings.Global.DEBUG_APP)); 70 } 71 72 @Override onActivityResult(int requestCode, int resultCode, Intent data)73 public boolean onActivityResult(int requestCode, int resultCode, Intent data) { 74 if (requestCode != REQUEST_CODE_DEBUG_APP || resultCode != Activity.RESULT_OK) { 75 return false; 76 } 77 updateState(mPreference, data.getAction()); 78 return true; 79 } 80 updateState(Preference preference, String debugApp)81 private void updateState(Preference preference, String debugApp) { 82 final SwitchPreference switchPreference = (SwitchPreference) preference; 83 final boolean debuggerEnabled = Settings.Global.getInt(mContext.getContentResolver(), 84 Settings.Global.WAIT_FOR_DEBUGGER, SETTING_VALUE_OFF) != SETTING_VALUE_OFF; 85 writeDebuggerAppOptions(debugApp, debuggerEnabled, true /* persistent */); 86 switchPreference.setChecked(debuggerEnabled); 87 switchPreference.setEnabled(!TextUtils.isEmpty(debugApp)); 88 } 89 90 @Override onDeveloperOptionsSwitchDisabled()91 protected void onDeveloperOptionsSwitchDisabled() { 92 super.onDeveloperOptionsSwitchDisabled(); 93 writeDebuggerAppOptions(null /* package name */, 94 false /* waitForDebugger */, false /* persistent */); 95 ((SwitchPreference) mPreference).setChecked(false); 96 } 97 98 @VisibleForTesting getActivityManagerService()99 IActivityManager getActivityManagerService() { 100 return ActivityManager.getService(); 101 } 102 writeDebuggerAppOptions(String packageName, boolean waitForDebugger, boolean persistent)103 private void writeDebuggerAppOptions(String packageName, boolean waitForDebugger, 104 boolean persistent) { 105 try { 106 getActivityManagerService().setDebugApp(packageName, waitForDebugger, persistent); 107 } catch (RemoteException ex) { 108 /* intentional no-op */ 109 } 110 } 111 } 112