Backport isVoiceCapable() using getPhoneType()

isVoiceCapable() was unhidden in API 22. We couldn't copy the code to
backport it because the app doesn't have access to
com.android.internal.R.bool.config_voice_capable. So we use getPhoneType()
as a workaround since it also calls isVoiceCapable().

Bug: 25629359

Change-Id: Ia117823ae871c7132014d10c5475eba5dbfda009
diff --git a/src/com/android/contacts/util/PhoneCapabilityTester.java b/src/com/android/contacts/util/PhoneCapabilityTester.java
index a510b76..d8609a0 100644
--- a/src/com/android/contacts/util/PhoneCapabilityTester.java
+++ b/src/com/android/contacts/util/PhoneCapabilityTester.java
@@ -25,7 +25,6 @@
 import android.net.sip.SipManager;
 import android.provider.MediaStore;
 import android.provider.Telephony;
-import android.telephony.TelephonyManager;
 
 import com.android.contacts.common.ContactsUtils;
 
@@ -61,9 +60,7 @@
     }
 
     private static void initialize(Context context) {
-        final TelephonyManager telephonyManager
-                = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
-        sIsPhone = telephonyManager.isVoiceCapable();
+        sIsPhone = TelephonyManagerCompat.isVoiceCapable(context);
         sIsSipPhone = sIsPhone && SipManager.isVoipSupported(context);
         sIsInitialized = true;
     }
diff --git a/src/com/android/contacts/util/TelephonyManagerCompat.java b/src/com/android/contacts/util/TelephonyManagerCompat.java
new file mode 100644
index 0000000..1559a89
--- /dev/null
+++ b/src/com/android/contacts/util/TelephonyManagerCompat.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.util;
+
+import android.content.Context;
+import android.os.Build;
+import android.telephony.TelephonyManager;
+
+import com.android.contacts.common.compat.SdkVersionOverride;
+
+public class TelephonyManagerCompat {
+    public static boolean isVoiceCapable(Context context) {
+        final TelephonyManager telephonyManager
+                = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
+        if (telephonyManager == null) {
+            return false;
+        }
+        if (SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.M)
+                >= Build.VERSION_CODES.LOLLIPOP_MR1) {
+            // isVoiceCapable was unhidden in L-MR1
+            return telephonyManager.isVoiceCapable();
+        }
+        final int phoneType = telephonyManager.getPhoneType();
+        return phoneType == TelephonyManager.PHONE_TYPE_CDMA ||
+                phoneType == TelephonyManager.PHONE_TYPE_GSM;
+    }
+}