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