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.applications; 18 19 import android.app.role.RoleManager; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.icu.text.ListFormatter; 24 import android.provider.Settings; 25 import android.text.TextUtils; 26 27 import androidx.annotation.NonNull; 28 import androidx.core.text.BidiFormatter; 29 import androidx.preference.Preference; 30 import androidx.preference.PreferenceScreen; 31 32 import com.android.settings.core.BasePreferenceController; 33 import com.android.settingslib.applications.AppUtils; 34 35 import java.util.ArrayList; 36 import java.util.List; 37 38 public class DefaultAppsPreferenceController extends BasePreferenceController { 39 40 private final PackageManager mPackageManager; 41 private final RoleManager mRoleManager; 42 DefaultAppsPreferenceController(Context context, String preferenceKey)43 public DefaultAppsPreferenceController(Context context, String preferenceKey) { 44 super(context, preferenceKey); 45 46 mPackageManager = context.getPackageManager(); 47 mRoleManager = context.getSystemService(RoleManager.class); 48 } 49 50 @Override getAvailabilityStatus()51 public int getAvailabilityStatus() { 52 return AVAILABLE; 53 } 54 55 @Override displayPreference(@onNull PreferenceScreen screen)56 public void displayPreference(@NonNull PreferenceScreen screen) { 57 super.displayPreference(screen); 58 59 Preference pref = screen.findPreference(getPreferenceKey()); 60 if (pref != null) { 61 pref.setIntent(new Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS) 62 .setPackage(mPackageManager.getPermissionControllerPackageName())); 63 } 64 } 65 66 @Override getSummary()67 public CharSequence getSummary() { 68 final List<CharSequence> defaultAppLabels = new ArrayList<>(); 69 final CharSequence defaultBrowserLabel = getDefaultAppLabel(RoleManager.ROLE_BROWSER); 70 if(!TextUtils.isEmpty(defaultBrowserLabel)) { 71 defaultAppLabels.add(defaultBrowserLabel); 72 } 73 final CharSequence defaultPhoneLabel = getDefaultAppLabel(RoleManager.ROLE_DIALER); 74 if(!TextUtils.isEmpty(defaultPhoneLabel)) { 75 defaultAppLabels.add(defaultPhoneLabel); 76 } 77 final CharSequence defaultSmsLabel = getDefaultAppLabel(RoleManager.ROLE_SMS); 78 if(!TextUtils.isEmpty(defaultSmsLabel)) { 79 defaultAppLabels.add(defaultSmsLabel); 80 } 81 if (defaultAppLabels.isEmpty()) { 82 return null; 83 } 84 return ListFormatter.getInstance().format(defaultAppLabels); 85 } 86 getDefaultAppLabel(String roleName)87 private CharSequence getDefaultAppLabel(String roleName) { 88 final List<String> packageNames = mRoleManager.getRoleHolders(roleName); 89 if (packageNames.isEmpty()) { 90 return null; 91 } 92 final String packageName = packageNames.get(0); 93 return BidiFormatter.getInstance().unicodeWrap(AppUtils.getApplicationLabel(mPackageManager, 94 packageName)); 95 } 96 } 97