1 package com.android.settings.development; 2 3 import android.content.Context; 4 import android.content.pm.PackageManager; 5 import android.os.UserManager; 6 7 import androidx.annotation.VisibleForTesting; 8 import androidx.preference.Preference; 9 import androidx.preference.PreferenceScreen; 10 import androidx.preference.SwitchPreference; 11 12 import com.android.settings.core.PreferenceControllerMixin; 13 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 14 15 public class LocalTerminalPreferenceController extends DeveloperOptionsPreferenceController 16 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { 17 18 private static final String ENABLE_TERMINAL_KEY = "enable_terminal"; 19 20 @VisibleForTesting 21 static final String TERMINAL_APP_PACKAGE = "com.android.terminal"; 22 23 private PackageManager mPackageManager; 24 private UserManager mUserManager; 25 LocalTerminalPreferenceController(Context context)26 public LocalTerminalPreferenceController(Context context) { 27 super(context); 28 29 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 30 } 31 32 @Override isAvailable()33 public boolean isAvailable() { 34 return isPackageInstalled(TERMINAL_APP_PACKAGE); 35 } 36 37 @Override getPreferenceKey()38 public String getPreferenceKey() { 39 return ENABLE_TERMINAL_KEY; 40 } 41 42 @Override displayPreference(PreferenceScreen screen)43 public void displayPreference(PreferenceScreen screen) { 44 super.displayPreference(screen); 45 46 mPackageManager = getPackageManager(); 47 48 if (isAvailable() && !isEnabled()) { 49 mPreference.setEnabled(false); 50 } 51 } 52 53 @Override onPreferenceChange(Preference preference, Object newValue)54 public boolean onPreferenceChange(Preference preference, Object newValue) { 55 final boolean terminalEnabled = (Boolean) newValue; 56 mPackageManager.setApplicationEnabledSetting(TERMINAL_APP_PACKAGE, 57 terminalEnabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED 58 : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0 /* flags */); 59 return true; 60 } 61 62 @Override updateState(Preference preference)63 public void updateState(Preference preference) { 64 final boolean isTerminalEnabled = mPackageManager.getApplicationEnabledSetting( 65 TERMINAL_APP_PACKAGE) == PackageManager.COMPONENT_ENABLED_STATE_ENABLED; 66 ((SwitchPreference) mPreference).setChecked(isTerminalEnabled); 67 } 68 69 @Override onDeveloperOptionsSwitchEnabled()70 protected void onDeveloperOptionsSwitchEnabled() { 71 if (isEnabled()) { 72 mPreference.setEnabled(true); 73 } 74 } 75 76 @Override onDeveloperOptionsSwitchDisabled()77 protected void onDeveloperOptionsSwitchDisabled() { 78 super.onDeveloperOptionsSwitchDisabled(); 79 mPackageManager.setApplicationEnabledSetting(TERMINAL_APP_PACKAGE, 80 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0 /* flags */); 81 ((SwitchPreference) mPreference).setChecked(false); 82 } 83 84 @VisibleForTesting getPackageManager()85 PackageManager getPackageManager() { 86 return mContext.getPackageManager(); 87 } 88 isPackageInstalled(String packageName)89 private boolean isPackageInstalled(String packageName) { 90 try { 91 return mContext.getPackageManager().getPackageInfo(packageName, 0 /* flags */) != null; 92 } catch (PackageManager.NameNotFoundException e) { 93 return false; 94 } 95 } 96 isEnabled()97 private boolean isEnabled() { 98 return mUserManager.isAdminUser(); 99 } 100 } 101