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.common.compat;
18 
19 import android.net.Uri;
20 import android.support.annotation.Nullable;
21 import android.telecom.PhoneAccountHandle;
22 import android.telephony.TelephonyManager;
23 
24 import com.android.contacts.common.ContactsUtils;
25 
26 public class TelephonyManagerCompat {
27     public static final String TELEPHONY_MANAGER_CLASS = "android.telephony.TelephonyManager";
28 
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      */
isVoiceCapable(@ullable TelephonyManager telephonyManager)44     public static boolean isVoiceCapable(@Nullable TelephonyManager telephonyManager) {
45         if (telephonyManager == null) {
46             return false;
47         }
48         if (CompatUtils.isLollipopMr1Compatible()
49                 || CompatUtils.isMethodAvailable(TELEPHONY_MANAGER_CLASS, "isVoiceCapable")) {
50             // 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      */
getPhoneCount(@ullable TelephonyManager telephonyManager)67     public static int getPhoneCount(@Nullable TelephonyManager telephonyManager) {
68         if (telephonyManager == null) {
69             return 1;
70         }
71         if (CompatUtils.isMarshmallowCompatible() || CompatUtils
72                 .isMethodAvailable(TELEPHONY_MANAGER_CLASS, "getPhoneCount")) {
73             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      */
getDeviceId(@ullable TelephonyManager telephonyManager, int slotId)88     public static String getDeviceId(@Nullable TelephonyManager telephonyManager, int slotId) {
89         if (telephonyManager == null) {
90             return null;
91         }
92         if (CompatUtils.isMarshmallowCompatible()
93                 || CompatUtils.isMethodAvailable(TELEPHONY_MANAGER_CLASS,
94                         "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 
isTtyModeSupported(@ullable TelephonyManager telephonyManager)107     public static boolean isTtyModeSupported(@Nullable TelephonyManager telephonyManager) {
108         if (telephonyManager == null) {
109             return false;
110         }
111         if (CompatUtils.isMarshmallowCompatible()
112                 || CompatUtils.isMethodAvailable(TELEPHONY_MANAGER_CLASS, "isTtyModeSupported")) {
113             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      */
isHearingAidCompatibilitySupported( @ullable TelephonyManager telephonyManager)125     public static boolean isHearingAidCompatibilitySupported(
126             @Nullable TelephonyManager telephonyManager) {
127         if (telephonyManager == null) {
128             return false;
129         }
130         if (CompatUtils.isMarshmallowCompatible()
131                 || CompatUtils.isMethodAvailable(TELEPHONY_MANAGER_CLASS,
132                         "isHearingAidCompatibilitySupported")) {
133             return telephonyManager.isHearingAidCompatibilitySupported();
134         }
135         return false;
136     }
137 
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
getVoicemailRingtoneUri(TelephonyManager telephonyManager, PhoneAccountHandle accountHandle)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      */
isVoicemailVibrationEnabled(TelephonyManager telephonyManager, PhoneAccountHandle accountHandle)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     }
173 }
174