1 /* 2 * Copyright (C) 2019 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 android.content.Context; 20 import android.content.pm.ApplicationInfo; 21 import android.content.pm.PackageManager; 22 import android.os.UserManager; 23 import android.text.TextUtils; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.Preference; 27 28 import com.android.settings.R; 29 import com.android.settings.bugreporthandler.BugReportHandlerUtil; 30 import com.android.settings.core.PreferenceControllerMixin; 31 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 32 33 /** 34 * PreferenceController for BugReportHandler 35 */ 36 public class BugReportHandlerPreferenceController extends DeveloperOptionsPreferenceController 37 implements PreferenceControllerMixin { 38 39 private static final String KEY_BUG_REPORT_HANDLER = "bug_report_handler"; 40 41 private final UserManager mUserManager; 42 private final BugReportHandlerUtil mBugReportHandlerUtil; 43 BugReportHandlerPreferenceController(Context context)44 public BugReportHandlerPreferenceController(Context context) { 45 super(context); 46 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 47 mBugReportHandlerUtil = new BugReportHandlerUtil(); 48 } 49 50 @Override isAvailable()51 public boolean isAvailable() { 52 return !mUserManager.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES) 53 && mBugReportHandlerUtil.isBugReportHandlerEnabled(mContext); 54 } 55 56 @Override getPreferenceKey()57 public String getPreferenceKey() { 58 return KEY_BUG_REPORT_HANDLER; 59 } 60 61 @Override updateState(Preference preference)62 public void updateState(Preference preference) { 63 final CharSequence currentBugReportHandlerAppLabel = getCurrentBugReportHandlerAppLabel(); 64 if (!TextUtils.isEmpty(currentBugReportHandlerAppLabel)) { 65 mPreference.setSummary(currentBugReportHandlerAppLabel); 66 } else { 67 mPreference.setSummary(R.string.app_list_preference_none); 68 } 69 } 70 71 @VisibleForTesting getCurrentBugReportHandlerAppLabel()72 CharSequence getCurrentBugReportHandlerAppLabel() { 73 final String handlerApp = mBugReportHandlerUtil.getCurrentBugReportHandlerAppAndUser( 74 mContext).first; 75 if (BugReportHandlerUtil.SHELL_APP_PACKAGE.equals(handlerApp)) { 76 return mContext.getString(com.android.internal.R.string.android_system_label); 77 } 78 ApplicationInfo applicationInfo; 79 try { 80 applicationInfo = mContext.getPackageManager().getApplicationInfo(handlerApp, 81 PackageManager.MATCH_ANY_USER); 82 } catch (PackageManager.NameNotFoundException e) { 83 return null; 84 } 85 return applicationInfo.loadLabel(mContext.getPackageManager()); 86 } 87 } 88