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