1 /* 2 * Copyright (C) 2015 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.content.Context; 20 import android.os.UserHandle; 21 import android.os.UserManager; 22 import android.telecom.DefaultDialerManager; 23 import android.telecom.TelecomManager; 24 import android.telephony.TelephonyManager; 25 import android.text.TextUtils; 26 import android.util.AttributeSet; 27 import com.android.settings.AppListPreference; 28 import com.android.settings.SelfAvailablePreference; 29 30 import java.util.List; 31 import java.util.Objects; 32 33 public class DefaultPhonePreference extends AppListPreference implements SelfAvailablePreference { 34 private final Context mContext; 35 36 public DefaultPhonePreference(Context context, AttributeSet attrs) { 37 super(context, attrs); 38 39 mContext = context.getApplicationContext(); 40 loadDialerApps(); 41 } 42 43 @Override 44 protected boolean persistString(String value) { 45 if (!TextUtils.isEmpty(value) && !Objects.equals(value, getDefaultPackage())) { 46 DefaultDialerManager.setDefaultDialerApplication(getContext(), value, mUserId); 47 } 48 setSummary(getEntry()); 49 return true; 50 } 51 52 private void loadDialerApps() { 53 List<String> dialerPackages = 54 DefaultDialerManager.getInstalledDialerApplications(getContext(), mUserId); 55 56 final String[] dialers = new String[dialerPackages.size()]; 57 for (int i = 0; i < dialerPackages.size(); i++) { 58 dialers[i] = dialerPackages.get(i); 59 } 60 setPackageNames(dialers, getDefaultPackage(), getSystemPackage()); 61 } 62 63 private String getDefaultPackage() { 64 return DefaultDialerManager.getDefaultDialerApplication(getContext(), mUserId); 65 } 66 67 private String getSystemPackage() { 68 TelecomManager tm = TelecomManager.from(getContext()); 69 return tm.getSystemDialerPackage(); 70 } 71 72 @Override 73 public boolean isAvailable(Context context) { 74 final TelephonyManager tm = 75 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 76 if (!tm.isVoiceCapable()) { 77 return false; 78 } 79 80 final UserManager um = 81 (UserManager) context.getSystemService(Context.USER_SERVICE); 82 return !um.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS); 83 } 84 85 public static boolean hasPhonePreference(String pkg, Context context) { 86 List<String> dialerPackages = 87 DefaultDialerManager.getInstalledDialerApplications(context, UserHandle.myUserId()); 88 return dialerPackages.contains(pkg); 89 } 90 91 public static boolean isPhoneDefault(String pkg, Context context) { 92 String def = DefaultDialerManager.getDefaultDialerApplication(context, 93 UserHandle.myUserId()); 94 return def != null && def.equals(pkg); 95 } 96 } 97