Use TelephonyManager instead of ITelephony

Switch to using public TelephonyManager calls (needed for unbundling).

Bug: 6948882
Change-Id: I301590efa5c45dfc3d31a16181694941a2bee092
diff --git a/src/com/android/dialer/DialtactsActivity.java b/src/com/android/dialer/DialtactsActivity.java
index 22e6ba0..2a2d11b 100644
--- a/src/com/android/dialer/DialtactsActivity.java
+++ b/src/com/android/dialer/DialtactsActivity.java
@@ -331,7 +331,7 @@
             }
 
             // During the call, we don't remember the tab position.
-            if (!DialpadFragment.phoneIsInUse()) {
+            if (mDialpadFragment == null || !mDialpadFragment.phoneIsInUse()) {
                 // Remember this tab index. This function is also called, if the tab is set
                 // automatically in which case the setter (setCurrentTab) has to set this to its old
                 // value afterwards
@@ -803,7 +803,8 @@
         final int savedTabIndex = mLastManuallySelectedFragment;
 
         final int tabIndex;
-        if (DialpadFragment.phoneIsInUse() || isDialIntent(intent)) {
+        if ((mDialpadFragment != null && mDialpadFragment.phoneIsInUse())
+                || isDialIntent(intent)) {
             tabIndex = TAB_INDEX_DIALER;
         } else if (recentCallsRequest) {
             tabIndex = TAB_INDEX_CALL_LOG;
@@ -1122,7 +1123,8 @@
         final Tab tab = actionBar.getSelectedTab();
 
         // User can search during the call, but we don't want to remember the status.
-        if (tab != null && !DialpadFragment.phoneIsInUse()) {
+        if (tab != null && (mDialpadFragment == null ||
+                        !mDialpadFragment.phoneIsInUse())) {
             mLastManuallySelectedFragment = tab.getPosition();
         }
 
diff --git a/src/com/android/dialer/dialpad/DialpadFragment.java b/src/com/android/dialer/dialpad/DialpadFragment.java
index aa963b7..fdcd140 100644
--- a/src/com/android/dialer/dialpad/DialpadFragment.java
+++ b/src/com/android/dialer/dialpad/DialpadFragment.java
@@ -252,6 +252,10 @@
         return intent;
     }
 
+    private TelephonyManager getTelephonyManager() {
+        return (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
+    }
+
     @Override
     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
         mWasEmptyBeforeTextChange = TextUtils.isEmpty(s);
@@ -603,9 +607,7 @@
         // While we're in the foreground, listen for phone state changes,
         // purely so that we can take down the "dialpad chooser" if the
         // phone becomes idle while the chooser UI is visible.
-        TelephonyManager telephonyManager =
-                (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
-        telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
+        getTelephonyManager().listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
 
         stopWatch.lap("tm");
 
@@ -648,9 +650,7 @@
         super.onPause();
 
         // Stop listening for phone state changes.
-        TelephonyManager telephonyManager =
-                (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
-        telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
+        getTelephonyManager().listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
 
         // Make sure we don't leave this activity with a tone still playing.
         stopTone();
@@ -1458,45 +1458,22 @@
      * @return true if the phone is "in use", meaning that at least one line
      *              is active (ie. off hook or ringing or dialing).
      */
-    public static boolean phoneIsInUse() {
-        boolean phoneInUse = false;
-        try {
-            ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
-            if (phone != null) phoneInUse = !phone.isIdle();
-        } catch (RemoteException e) {
-            Log.w(TAG, "phone.isIdle() failed", e);
-        }
-        return phoneInUse;
+    public boolean phoneIsInUse() {
+        return getTelephonyManager().getCallState() != TelephonyManager.CALL_STATE_IDLE;
     }
 
     /**
      * @return true if the phone is a CDMA phone type
      */
     private boolean phoneIsCdma() {
-        boolean isCdma = false;
-        try {
-            ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
-            if (phone != null) {
-                isCdma = (phone.getActivePhoneType() == TelephonyManager.PHONE_TYPE_CDMA);
-            }
-        } catch (RemoteException e) {
-            Log.w(TAG, "phone.getActivePhoneType() failed", e);
-        }
-        return isCdma;
+        return getTelephonyManager().getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA;
     }
 
     /**
      * @return true if the phone state is OFFHOOK
      */
     private boolean phoneIsOffhook() {
-        boolean phoneOffhook = false;
-        try {
-            ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
-            if (phone != null) phoneOffhook = phone.isOffhook();
-        } catch (RemoteException e) {
-            Log.w(TAG, "phone.isOffhook() failed", e);
-        }
-        return phoneOffhook;
+        return getTelephonyManager().getCallState() == TelephonyManager.CALL_STATE_OFFHOOK;
     }
 
     /**
@@ -1591,7 +1568,7 @@
      */
     private boolean isVoicemailAvailable() {
         try {
-            return (TelephonyManager.getDefault().getVoiceMailNumber() != null);
+            return getTelephonyManager().getVoiceMailNumber() != null;
         } catch (SecurityException se) {
             // Possibly no READ_PHONE_STATE privilege.
             Log.w(TAG, "SecurityException is thrown. Maybe privilege isn't sufficient.");