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