am dd24a8b2: am f5dc3a52: Merge "Use a BroadcastReceiver for Dialpad behavior upon call hangup." into lmp-mr1-dev

* commit 'dd24a8b23cb25b195c5682fd7d8c975bbce1417c':
  Use a BroadcastReceiver for Dialpad behavior upon call hangup.
diff --git a/src/com/android/dialer/dialpad/DialpadFragment.java b/src/com/android/dialer/dialpad/DialpadFragment.java
index 0f5a333..ce6475e 100644
--- a/src/com/android/dialer/dialpad/DialpadFragment.java
+++ b/src/com/android/dialer/dialpad/DialpadFragment.java
@@ -21,11 +21,13 @@
 import android.app.Dialog;
 import android.app.DialogFragment;
 import android.app.Fragment;
+import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.graphics.Bitmap;
@@ -218,17 +220,21 @@
 
     private String mCurrentCountryIso;
 
-    private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
+    private CallStateReceiver mCallStateReceiver;
+
+    private class CallStateReceiver extends BroadcastReceiver {
         /**
-         * Listen for phone state changes so that we can take down the
+         * Receive call state changes so that we can take down the
          * "dialpad chooser" if the phone becomes idle while the
          * chooser UI is visible.
          */
         @Override
-        public void onCallStateChanged(int state, String incomingNumber) {
-            // Log.i(TAG, "PhoneStateListener.onCallStateChanged: "
-            //       + state + ", '" + incomingNumber + "'");
-            if ((state == TelephonyManager.CALL_STATE_IDLE) && isDialpadChooserVisible()) {
+        public void onReceive(Context context, Intent intent) {
+            // Log.i(TAG, "CallStateReceiver.onReceive");
+            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
+            if ((TextUtils.equals(state, TelephonyManager.EXTRA_STATE_IDLE) ||
+                    TextUtils.equals(state, TelephonyManager.EXTRA_STATE_OFFHOOK))
+                    && isDialpadChooserVisible()) {
                 // Log.i(TAG, "Call ended with dialpad chooser visible!  Taking it down...");
                 // Note there's a race condition in the UI here: the
                 // dialpad chooser could conceivably disappear (on its
@@ -239,7 +245,7 @@
                 showDialpadChooser(false);
             }
         }
-    };
+    }
 
     private boolean mWasEmptyBeforeTextChange;
 
@@ -328,6 +334,13 @@
         }
 
         mDialpadSlideInDuration = getResources().getInteger(R.integer.dialpad_slide_in_duration);
+
+        if (mCallStateReceiver == null) {
+            IntentFilter callStateIntentFilter = new IntentFilter(
+                    TelephonyManager.ACTION_PHONE_STATE_CHANGED);
+            mCallStateReceiver = new CallStateReceiver();
+            ((Context) getActivity()).registerReceiver(mCallStateReceiver, callStateIntentFilter);
+        }
     }
 
     @Override
@@ -623,13 +636,6 @@
 
         stopWatch.lap("fdin");
 
-        // 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.
-        getTelephonyManager().listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
-
-        stopWatch.lap("tm");
-
         if (!isPhoneInUse()) {
             // A sanity-check: the "dialpad chooser" UI should not be visible if the phone is idle.
             showDialpadChooser(false);
@@ -661,9 +667,6 @@
     public void onPause() {
         super.onPause();
 
-        // Stop listening for phone state changes.
-        getTelephonyManager().listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
-
         // Make sure we don't leave this activity with a tone still playing.
         stopTone();
         mPressedDialpadKeys.clear();
@@ -698,6 +701,12 @@
         outState.putBoolean(PREF_DIGITS_FILLED_BY_INTENT, mDigitsFilledByIntent);
     }
 
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        ((Context) getActivity()).unregisterReceiver(mCallStateReceiver);
+    }
+
     private void keyPressed(int keyCode) {
         if (getView().getTranslationY() != 0) {
             return;