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.pm.PackageManager; 22 import android.icu.text.ListFormatter; 23 import android.text.TextUtils; 24 25 import androidx.core.text.BidiFormatter; 26 27 import com.android.settings.core.BasePreferenceController; 28 import com.android.settingslib.applications.AppUtils; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 33 public class DefaultAppsPreferenceController extends BasePreferenceController { 34 35 private final PackageManager mPackageManager; 36 private final RoleManager mRoleManager; 37 DefaultAppsPreferenceController(Context context, String preferenceKey)38 public DefaultAppsPreferenceController(Context context, String preferenceKey) { 39 super(context, preferenceKey); 40 41 mPackageManager = context.getPackageManager(); 42 mRoleManager = context.getSystemService(RoleManager.class); 43 } 44 45 @Override getAvailabilityStatus()46 public int getAvailabilityStatus() { 47 return AVAILABLE; 48 } 49 50 @Override getSummary()51 public CharSequence getSummary() { 52 final List<CharSequence> defaultAppLabels = new ArrayList<>(); 53 final CharSequence defaultBrowserLabel = getDefaultAppLabel(RoleManager.ROLE_BROWSER); 54 if(!TextUtils.isEmpty(defaultBrowserLabel)) { 55 defaultAppLabels.add(defaultBrowserLabel); 56 } 57 final CharSequence defaultPhoneLabel = getDefaultAppLabel(RoleManager.ROLE_DIALER); 58 if(!TextUtils.isEmpty(defaultPhoneLabel)) { 59 defaultAppLabels.add(defaultPhoneLabel); 60 } 61 final CharSequence defaultSmsLabel = getDefaultAppLabel(RoleManager.ROLE_SMS); 62 if(!TextUtils.isEmpty(defaultSmsLabel)) { 63 defaultAppLabels.add(defaultSmsLabel); 64 } 65 if (defaultAppLabels.isEmpty()) { 66 return null; 67 } 68 return ListFormatter.getInstance().format(defaultAppLabels); 69 } 70 getDefaultAppLabel(String roleName)71 private CharSequence getDefaultAppLabel(String roleName) { 72 final List<String> packageNames = mRoleManager.getRoleHolders(roleName); 73 if (packageNames.isEmpty()) { 74 return null; 75 } 76 final String packageName = packageNames.get(0); 77 return BidiFormatter.getInstance().unicodeWrap(AppUtils.getApplicationLabel(mPackageManager, 78 packageName)); 79 } 80 } 81