1 /*
2  * Copyright (C) 2017 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.defaultapps;
18 
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.os.UserHandle;
22 import android.os.UserManager;
23 import android.telecom.DefaultDialerManager;
24 import android.telephony.TelephonyManager;
25 
26 import java.util.List;
27 
28 public class DefaultPhonePreferenceController extends DefaultAppPreferenceController {
29 
DefaultPhonePreferenceController(Context context)30     public DefaultPhonePreferenceController(Context context) {
31         super(context);
32     }
33 
34     @Override
isAvailable()35     public boolean isAvailable() {
36         final TelephonyManager tm =
37                 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
38         if (!tm.isVoiceCapable()) {
39             return false;
40         }
41         final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
42         final boolean hasUserRestriction =
43                 um.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS);
44 
45         if (hasUserRestriction) {
46             return false;
47         }
48         final List<String> candidates = getCandidates();
49         return candidates != null && !candidates.isEmpty();
50     }
51 
52     @Override
getPreferenceKey()53     public String getPreferenceKey() {
54         return "default_phone_app";
55     }
56 
57     @Override
getDefaultAppInfo()58     protected DefaultAppInfo getDefaultAppInfo() {
59         try {
60             return new DefaultAppInfo(mPackageManager,
61                     mPackageManager.getPackageManager().getApplicationInfo(
62                             DefaultDialerManager.getDefaultDialerApplication(mContext, mUserId),
63                             0));
64         } catch (PackageManager.NameNotFoundException e) {
65             return null;
66         }
67     }
68 
getCandidates()69     private List<String> getCandidates() {
70         return DefaultDialerManager.getInstalledDialerApplications(mContext, mUserId);
71     }
72 
hasPhonePreference(String pkg, Context context)73     public static boolean hasPhonePreference(String pkg, Context context) {
74         List<String> dialerPackages =
75                 DefaultDialerManager.getInstalledDialerApplications(context, UserHandle.myUserId());
76         return dialerPackages.contains(pkg);
77     }
78 
isPhoneDefault(String pkg, Context context)79     public static boolean isPhoneDefault(String pkg, Context context) {
80         String def = DefaultDialerManager.getDefaultDialerApplication(context,
81                 UserHandle.myUserId());
82         return def != null && def.equals(pkg);
83     }
84 }
85