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