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.contacts.compat; 18 19 import android.net.Uri; 20 import androidx.annotation.Nullable; 21 import android.telecom.PhoneAccountHandle; 22 import android.telephony.TelephonyManager; 23 24 public class TelephonyManagerCompat { 25 public static final String TELEPHONY_MANAGER_CLASS = "android.telephony.TelephonyManager"; 26 27 /** 28 * @param telephonyManager The telephony manager instance to use for method calls. 29 * @return true if the current device is "voice capable". 30 * <p> 31 * "Voice capable" means that this device supports circuit-switched 32 * (i.e. voice) phone calls over the telephony network, and is allowed 33 * to display the in-call UI while a cellular voice call is active. 34 * This will be false on "data only" devices which can't make voice 35 * calls and don't support any in-call UI. 36 * <p> 37 * Note: the meaning of this flag is subtly different from the 38 * PackageManager.FEATURE_TELEPHONY system feature, which is available 39 * on any device with a telephony radio, even if the device is 40 * data-only. 41 */ isVoiceCapable(@ullable TelephonyManager telephonyManager)42 public static boolean isVoiceCapable(@Nullable TelephonyManager telephonyManager) { 43 if (telephonyManager == null) { 44 return false; 45 } 46 if (CompatUtils.isLollipopMr1Compatible() 47 || CompatUtils.isMethodAvailable(TELEPHONY_MANAGER_CLASS, "isVoiceCapable")) { 48 // isVoiceCapable was unhidden in L-MR1 49 return telephonyManager.isVoiceCapable(); 50 } 51 final int phoneType = telephonyManager.getPhoneType(); 52 return phoneType == TelephonyManager.PHONE_TYPE_CDMA || 53 phoneType == TelephonyManager.PHONE_TYPE_GSM; 54 } 55 56 /** 57 * Returns the number of phones available. 58 * Returns 1 for Single standby mode (Single SIM functionality) 59 * Returns 2 for Dual standby mode.(Dual SIM functionality) 60 * 61 * Returns 1 if the method or telephonyManager is not available. 62 * 63 * @param telephonyManager The telephony manager instance to use for method calls. 64 */ getPhoneCount(@ullable TelephonyManager telephonyManager)65 public static int getPhoneCount(@Nullable TelephonyManager telephonyManager) { 66 if (telephonyManager == null) { 67 return 1; 68 } 69 if (CompatUtils.isMarshmallowCompatible() || CompatUtils 70 .isMethodAvailable(TELEPHONY_MANAGER_CLASS, "getPhoneCount")) { 71 return telephonyManager.getPhoneCount(); 72 } 73 return 1; 74 } 75 76 /** 77 * Returns the unique device ID of a subscription, for example, the IMEI for 78 * GSM and the MEID for CDMA phones. Return null if device ID is not available. 79 * 80 * <p>Requires Permission: 81 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} 82 * 83 * @param telephonyManager The telephony manager instance to use for method calls. 84 * @param slotId of which deviceID is returned 85 */ getDeviceId(@ullable TelephonyManager telephonyManager, int slotId)86 public static String getDeviceId(@Nullable TelephonyManager telephonyManager, int slotId) { 87 if (telephonyManager == null) { 88 return null; 89 } 90 if (CompatUtils.isMarshmallowCompatible() 91 || CompatUtils.isMethodAvailable(TELEPHONY_MANAGER_CLASS, 92 "getDeviceId", Integer.class)) { 93 return telephonyManager.getDeviceId(slotId); 94 } 95 return null; 96 } 97 98 /** 99 * Whether the phone supports TTY mode. 100 * 101 * @param telephonyManager The telephony manager instance to use for method calls. 102 * @return {@code true} if the device supports TTY mode, and {@code false} otherwise. 103 */ 104 isTtyModeSupported(@ullable TelephonyManager telephonyManager)105 public static boolean isTtyModeSupported(@Nullable TelephonyManager telephonyManager) { 106 if (telephonyManager == null) { 107 return false; 108 } 109 if (CompatUtils.isMarshmallowCompatible() 110 || CompatUtils.isMethodAvailable(TELEPHONY_MANAGER_CLASS, "isTtyModeSupported")) { 111 return telephonyManager.isTtyModeSupported(); 112 } 113 return false; 114 } 115 116 /** 117 * Whether the phone supports hearing aid compatibility. 118 * 119 * @param telephonyManager The telephony manager instance to use for method calls. 120 * @return {@code true} if the device supports hearing aid compatibility, and {@code false} 121 * otherwise. 122 */ isHearingAidCompatibilitySupported( @ullable TelephonyManager telephonyManager)123 public static boolean isHearingAidCompatibilitySupported( 124 @Nullable TelephonyManager telephonyManager) { 125 if (telephonyManager == null) { 126 return false; 127 } 128 if (CompatUtils.isMarshmallowCompatible() 129 || CompatUtils.isMethodAvailable(TELEPHONY_MANAGER_CLASS, 130 "isHearingAidCompatibilitySupported")) { 131 return telephonyManager.isHearingAidCompatibilitySupported(); 132 } 133 return false; 134 } 135 136 /** 137 * Returns the URI for the per-account voicemail ringtone set in Phone settings. 138 * 139 * @param telephonyManager The telephony manager instance to use for method calls. 140 * @param accountHandle The handle for the {@link android.telecom.PhoneAccount} for which to 141 * retrieve the voicemail ringtone. 142 * @return The URI for the ringtone to play when receiving a voicemail from a specific 143 * PhoneAccount. 144 */ 145 @Nullable getVoicemailRingtoneUri(TelephonyManager telephonyManager, PhoneAccountHandle accountHandle)146 public static Uri getVoicemailRingtoneUri(TelephonyManager telephonyManager, 147 PhoneAccountHandle accountHandle) { 148 if (!CompatUtils.isNCompatible()) { 149 return null; 150 } 151 return TelephonyManagerSdkCompat 152 .getVoicemailRingtoneUri(telephonyManager, accountHandle); 153 } 154 155 /** 156 * Returns whether vibration is set for voicemail notification in Phone settings. 157 * 158 * @param telephonyManager The telephony manager instance to use for method calls. 159 * @param accountHandle The handle for the {@link android.telecom.PhoneAccount} for which to 160 * retrieve the voicemail vibration setting. 161 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise. 162 */ isVoicemailVibrationEnabled(TelephonyManager telephonyManager, PhoneAccountHandle accountHandle)163 public static boolean isVoicemailVibrationEnabled(TelephonyManager telephonyManager, 164 PhoneAccountHandle accountHandle) { 165 if (!CompatUtils.isNCompatible()) { 166 return true; 167 } 168 return TelephonyManagerSdkCompat 169 .isVoicemailVibrationEnabled(telephonyManager, accountHandle); 170 } 171 } 172