blob: 07523b197dac7131a403cc2aa6ae45cf20b45433 [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
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.compat;
Nancy Chena949f532015-12-22 14:28:09 -080018
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
24public class TelephonyManagerCompat {
Nancy Chen78384182015-12-22 17:21:13 -080025 public static final String TELEPHONY_MANAGER_CLASS = "android.telephony.TelephonyManager";
Nancy Chena949f532015-12-22 14:28:09 -080026
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 */
42 public static boolean isVoiceCapable(@Nullable TelephonyManager telephonyManager) {
43 if (telephonyManager == null) {
44 return false;
45 }
Nancy Chen78384182015-12-22 17:21:13 -080046 if (CompatUtils.isLollipopMr1Compatible()
47 || CompatUtils.isMethodAvailable(TELEPHONY_MANAGER_CLASS, "isVoiceCapable")) {
Nancy Chena949f532015-12-22 14:28:09 -080048 // 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 */
65 public static int getPhoneCount(@Nullable TelephonyManager telephonyManager) {
66 if (telephonyManager == null) {
67 return 1;
68 }
69 if (CompatUtils.isMarshmallowCompatible() || CompatUtils
Nancy Chen78384182015-12-22 17:21:13 -080070 .isMethodAvailable(TELEPHONY_MANAGER_CLASS, "getPhoneCount")) {
Nancy Chena949f532015-12-22 14:28:09 -080071 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 */
86 public static String getDeviceId(@Nullable TelephonyManager telephonyManager, int slotId) {
87 if (telephonyManager == null) {
88 return null;
89 }
90 if (CompatUtils.isMarshmallowCompatible()
Nancy Chen78384182015-12-22 17:21:13 -080091 || CompatUtils.isMethodAvailable(TELEPHONY_MANAGER_CLASS,
Nancy Chena949f532015-12-22 14:28:09 -080092 "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
105 public static boolean isTtyModeSupported(@Nullable TelephonyManager telephonyManager) {
106 if (telephonyManager == null) {
107 return false;
108 }
109 if (CompatUtils.isMarshmallowCompatible()
Nancy Chen78384182015-12-22 17:21:13 -0800110 || CompatUtils.isMethodAvailable(TELEPHONY_MANAGER_CLASS, "isTtyModeSupported")) {
Nancy Chena949f532015-12-22 14:28:09 -0800111 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 */
123 public static boolean isHearingAidCompatibilitySupported(
124 @Nullable TelephonyManager telephonyManager) {
125 if (telephonyManager == null) {
126 return false;
127 }
128 if (CompatUtils.isMarshmallowCompatible()
Nancy Chen78384182015-12-22 17:21:13 -0800129 || CompatUtils.isMethodAvailable(TELEPHONY_MANAGER_CLASS,
130 "isHearingAidCompatibilitySupported")) {
Nancy Chena949f532015-12-22 14:28:09 -0800131 return telephonyManager.isHearingAidCompatibilitySupported();
132 }
133 return false;
134 }
Nancy Chen49344d82016-01-20 15:28:30 -0800135
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
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 */
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 }
Nancy Chena949f532015-12-22 14:28:09 -0800171}