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.content.Context; 23 import android.content.Intent; 24 import android.content.pm.ApplicationInfo; 25 import android.content.pm.PackageManager; 26 import android.provider.Settings; 27 28 import androidx.annotation.VisibleForTesting; 29 import androidx.preference.Preference; 30 31 import com.android.settings.R; 32 import com.android.settings.core.PreferenceControllerMixin; 33 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 34 35 public class SelectDebugAppPreferenceController extends DeveloperOptionsPreferenceController 36 implements PreferenceControllerMixin, OnActivityResultListener { 37 38 private static final String DEBUG_APP_KEY = "debug_app"; 39 40 private final DevelopmentSettingsDashboardFragment mFragment; 41 private final PackageManager mPackageManager; 42 SelectDebugAppPreferenceController(Context context, DevelopmentSettingsDashboardFragment fragment)43 public SelectDebugAppPreferenceController(Context context, 44 DevelopmentSettingsDashboardFragment fragment) { 45 super(context); 46 mFragment = fragment; 47 mPackageManager = mContext.getPackageManager(); 48 } 49 50 @Override getPreferenceKey()51 public String getPreferenceKey() { 52 return DEBUG_APP_KEY; 53 } 54 55 @Override handlePreferenceTreeClick(Preference preference)56 public boolean handlePreferenceTreeClick(Preference preference) { 57 if (DEBUG_APP_KEY.equals(preference.getKey())) { 58 final Intent intent = getActivityStartIntent(); 59 intent.putExtra(AppPicker.EXTRA_DEBUGGABLE, true /* value */); 60 mFragment.startActivityForResult(intent, REQUEST_CODE_DEBUG_APP); 61 return true; 62 } 63 return false; 64 } 65 66 @Override updateState(Preference preference)67 public void updateState(Preference preference) { 68 updatePreferenceSummary(); 69 } 70 71 @Override onActivityResult(int requestCode, int resultCode, Intent data)72 public boolean onActivityResult(int requestCode, int resultCode, Intent data) { 73 if (requestCode != REQUEST_CODE_DEBUG_APP || resultCode != Activity.RESULT_OK) { 74 return false; 75 } 76 Settings.Global.putString(mContext.getContentResolver(), Settings.Global.DEBUG_APP, 77 data.getAction()); 78 updatePreferenceSummary(); 79 return true; 80 } 81 82 @Override onDeveloperOptionsSwitchDisabled()83 protected void onDeveloperOptionsSwitchDisabled() { 84 super.onDeveloperOptionsSwitchDisabled(); 85 mPreference.setSummary(mContext.getResources().getString(R.string.debug_app_not_set)); 86 } 87 88 @VisibleForTesting getActivityStartIntent()89 Intent getActivityStartIntent() { 90 return new Intent(mContext, AppPicker.class); 91 } 92 updatePreferenceSummary()93 private void updatePreferenceSummary() { 94 final String debugApp = Settings.Global.getString( 95 mContext.getContentResolver(), Settings.Global.DEBUG_APP); 96 if (debugApp != null && debugApp.length() > 0) { 97 mPreference.setSummary(mContext.getResources().getString(R.string.debug_app_set, 98 getAppLabel(debugApp))); 99 } else { 100 mPreference.setSummary(mContext.getResources().getString(R.string.debug_app_not_set)); 101 } 102 } 103 getAppLabel(String debugApp)104 private String getAppLabel(String debugApp) { 105 try { 106 final ApplicationInfo ai = mPackageManager.getApplicationInfo(debugApp, 107 PackageManager.GET_DISABLED_COMPONENTS); 108 final CharSequence lab = mPackageManager.getApplicationLabel(ai); 109 return lab != null ? lab.toString() : debugApp; 110 } catch (PackageManager.NameNotFoundException e) { 111 return debugApp; 112 } 113 } 114 } 115