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