1 /*
2  * Copyright (C) 2018 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.permissioncontroller.role.model;
18 
19 import android.content.Context;
20 import android.content.pm.ApplicationInfo;
21 import android.os.UserHandle;
22 import android.telecom.TelecomManager;
23 import android.telephony.TelephonyManager;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.preference.Preference;
28 
29 import com.android.permissioncontroller.R;
30 
31 import java.util.Objects;
32 
33 /**
34  * Class for behavior of the dialer role.
35  *
36  * @see com.android.settings.applications.DefaultAppSettings
37  * @see com.android.settings.applications.defaultapps.DefaultPhonePreferenceController
38  * @see com.android.settings.applications.defaultapps.DefaultPhonePicker
39  */
40 public class DialerRoleBehavior implements RoleBehavior {
41 
42     @Override
isAvailableAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)43     public boolean isAvailableAsUser(@NonNull Role role, @NonNull UserHandle user,
44             @NonNull Context context) {
45         TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class);
46         return telephonyManager.isVoiceCapable();
47     }
48 
49     @Override
prepareApplicationPreferenceAsUser(@onNull Role role, @NonNull Preference preference, @NonNull ApplicationInfo applicationInfo, @NonNull UserHandle user, @NonNull Context context)50     public void prepareApplicationPreferenceAsUser(@NonNull Role role,
51             @NonNull Preference preference, @NonNull ApplicationInfo applicationInfo,
52             @NonNull UserHandle user, @NonNull Context context) {
53         TelecomManager telecomManager = context.getSystemService(TelecomManager.class);
54         String systemPackageName = telecomManager.getSystemDialerPackage();
55         if (Objects.equals(applicationInfo.packageName, systemPackageName)) {
56             preference.setSummary(R.string.default_app_system_default);
57         } else {
58             preference.setSummary(null);
59         }
60     }
61 
62     @Nullable
63     @Override
getConfirmationMessage(@onNull Role role, @NonNull String packageName, @NonNull Context context)64     public CharSequence getConfirmationMessage(@NonNull Role role, @NonNull String packageName,
65             @NonNull Context context) {
66         return EncryptionUnawareConfirmationMixin.getConfirmationMessage(role, packageName,
67                 context);
68     }
69 
70     @Override
isVisibleAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)71     public boolean isVisibleAsUser(@NonNull Role role, @NonNull UserHandle user,
72             @NonNull Context context) {
73         return context.getResources().getBoolean(R.bool.config_showDialerRole);
74     }
75 }
76