Merge "Revert "Improve speakerphone setting."" into ub-contactsdialer-a-dev
diff --git a/res/layout/voicemail_playback_layout.xml b/res/layout/voicemail_playback_layout.xml
index 54493f1..555d201 100644
--- a/res/layout/voicemail_playback_layout.xml
+++ b/res/layout/voicemail_playback_layout.xml
@@ -71,7 +71,7 @@
 
                 <ImageButton android:id="@+id/playback_speakerphone"
                     style="@style/VoicemailPlaybackLayoutButtonStyle"
-                    android:src="@drawable/ic_volume_down_24dp"
+                    android:src="@drawable/ic_speakerphone_on"
                     android:tint="@color/voicemail_icon_tint"
                     android:contentDescription="@string/description_playback_speakerphone" />
 
diff --git a/src/com/android/dialer/voicemail/VoicemailPlaybackLayout.java b/src/com/android/dialer/voicemail/VoicemailPlaybackLayout.java
index 133da36..69c075f 100644
--- a/src/com/android/dialer/voicemail/VoicemailPlaybackLayout.java
+++ b/src/com/android/dialer/voicemail/VoicemailPlaybackLayout.java
@@ -159,7 +159,7 @@
         @Override
         public void onClick(View v) {
             if (mPresenter != null) {
-                mPresenter.toggleSpeakerphone();
+                onSpeakerphoneOn(!mPresenter.isSpeakerphoneOn());
             }
         }
     };
@@ -286,6 +286,10 @@
 
         mStartStopButton.setImageResource(R.drawable.ic_pause);
 
+        if (mPresenter != null) {
+            onSpeakerphoneOn(mPresenter.isSpeakerphoneOn());
+        }
+
         if (mPositionUpdater != null) {
             mPositionUpdater.stopUpdating();
             mPositionUpdater = null;
@@ -317,6 +321,10 @@
     }
 
     public void onSpeakerphoneOn(boolean on) {
+        if (mPresenter != null) {
+            mPresenter.setSpeakerphoneOn(on);
+        }
+
         if (on) {
             mPlaybackSpeakerphone.setImageResource(R.drawable.ic_volume_up_24dp);
             // Speaker is now on, tapping button will turn it off.
@@ -365,6 +373,7 @@
     @Override
     public void disableUiElements() {
         mStartStopButton.setEnabled(false);
+        mPlaybackSpeakerphone.setEnabled(false);
         mPlaybackSeek.setProgress(0);
         mPlaybackSeek.setEnabled(false);
 
@@ -375,6 +384,7 @@
     @Override
     public void enableUiElements() {
         mStartStopButton.setEnabled(true);
+        mPlaybackSpeakerphone.setEnabled(true);
         mPlaybackSeek.setEnabled(true);
 
         mPositionText.setVisibility(View.VISIBLE);
diff --git a/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java b/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java
index 1a9c239..3f5a489 100644
--- a/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java
+++ b/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java
@@ -60,7 +60,7 @@
  * {@link CallLogFragment} and {@link CallLogAdapter}.
  * <p>
  * This controls a single {@link com.android.dialer.voicemail.VoicemailPlaybackLayout}. A single
- * instance can be reused for different such layouts, using {@link #setPlaybackView}. This
+ * instance can be reused for different such layouts, using {@link #setVoicemailPlaybackView}. This
  * is to facilitate reuse across different voicemail call log entries.
  * <p>
  * This class is not thread safe. The thread policy for this class is thread-confinement, all calls
@@ -120,8 +120,6 @@
     // If present in the saved instance bundle, indicates where to set the playback slider.
     private static final String CLIP_POSITION_KEY =
             VoicemailPlaybackPresenter.class.getName() + ".CLIP_POSITION_KEY";
-    private static final String IS_SPEAKERPHONE_ON_KEY =
-            VoicemailPlaybackPresenter.class.getName() + ".IS_SPEAKER_PHONE_ON";
 
     /**
      * The most recently cached duration. We cache this since we don't want to keep requesting it
@@ -143,7 +141,6 @@
     // MediaPlayer crashes on some method calls if not prepared but does not have a method which
     // exposes its prepared state. Store this locally, so we can check and prevent crashes.
     private boolean mIsPrepared;
-    private boolean mIsSpeakerphoneOn;
 
     private boolean mShouldResumePlaybackAfterSeeking;
     private int mInitialOrientation;
@@ -214,7 +211,6 @@
             mIsPrepared = savedInstanceState.getBoolean(IS_PREPARED_KEY);
             mPosition = savedInstanceState.getInt(CLIP_POSITION_KEY, 0);
             mIsPlaying = savedInstanceState.getBoolean(IS_PLAYING_STATE_KEY, false);
-            mIsSpeakerphoneOn = savedInstanceState.getBoolean(IS_SPEAKERPHONE_ON_KEY, false);
         }
 
         if (mMediaPlayer == null) {
@@ -232,7 +228,6 @@
             outState.putBoolean(IS_PREPARED_KEY, mIsPrepared);
             outState.putInt(CLIP_POSITION_KEY, mView.getDesiredClipPosition());
             outState.putBoolean(IS_PLAYING_STATE_KEY, mIsPlaying);
-            outState.putBoolean(IS_SPEAKERPHONE_ON_KEY, mIsSpeakerphoneOn);
         }
     }
 
@@ -244,21 +239,16 @@
         mView = view;
         mView.setPresenter(this, voicemailUri);
 
-        // Handles cases where the same entry is binded again when scrolling in list, or where
-        // the MediaPlayer was retained after an orientation change.
         if (mMediaPlayer != null && mIsPrepared && voicemailUri.equals(mVoicemailUri)) {
+            // Handles case where MediaPlayer was retained after an orientation change.
             onPrepared(mMediaPlayer);
+            mView.onSpeakerphoneOn(isSpeakerphoneOn());
         } else {
             if (!voicemailUri.equals(mVoicemailUri)) {
-                mVoicemailUri = voicemailUri;
                 mPosition = 0;
-                // Default to earpiece.
-                setSpeakerphoneOn(false);
-            } else {
-                // Update the view to the current speakerphone state.
-                mView.onSpeakerphoneOn(mIsSpeakerphoneOn);
             }
 
+            mVoicemailUri = voicemailUri;
             mDuration.set(0);
 
             if (startPlayingImmediately) {
@@ -268,6 +258,9 @@
                 mIsPlaying = startPlayingImmediately;
                 checkForContent();
             }
+
+            // Default to earpiece.
+            mView.onSpeakerphoneOn(false);
         }
     }
 
@@ -602,6 +595,7 @@
             // If we haven't downloaded the voicemail yet, attempt to download it.
             checkForContent();
             mIsPlaying = true;
+
             return;
         }
 
@@ -622,7 +616,6 @@
                     throw new RejectedExecutionException("Could not capture audio focus.");
                 }
 
-                setSpeakerphoneOn(mIsSpeakerphoneOn);
                 // Can throw RejectedExecutionException.
                 mMediaPlayer.start();
             } catch (RejectedExecutionException e) {
@@ -632,6 +625,11 @@
 
         Log.d(TAG, "Resumed playback at " + mPosition + ".");
         mView.onPlaybackStarted(mDuration.get(), getScheduledExecutorServiceInstance());
+        if (isSpeakerphoneOn()) {
+            mActivity.getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
+        } else {
+            enableProximitySensor();
+        }
     }
 
     /**
@@ -683,7 +681,7 @@
     }
 
     private void enableProximitySensor() {
-        if (mProximityWakeLock == null || mIsSpeakerphoneOn || !mIsPrepared
+        if (mProximityWakeLock == null || isSpeakerphoneOn() || !mIsPrepared
                 || mMediaPlayer == null || !mMediaPlayer.isPlaying()) {
             return;
         }
@@ -709,34 +707,26 @@
         }
     }
 
-    public void toggleSpeakerphone() {
-        setSpeakerphoneOn(!mIsSpeakerphoneOn);
-    }
+    public void setSpeakerphoneOn(boolean on) {
+        mAudioManager.setSpeakerphoneOn(on);
 
-    private void setSpeakerphoneOn(boolean on) {
-        mView.onSpeakerphoneOn(on);
-        if (mIsSpeakerphoneOn == on) {
-            return;
-        }
-
-        mIsSpeakerphoneOn = on;
-        mAudioManager.setSpeakerphoneOn(mIsSpeakerphoneOn);
-
-        if (mIsPlaying) {
-            if (on) {
-                disableProximitySensor(false /* waitForFarState */);
-                if (mIsPrepared && mMediaPlayer != null && mMediaPlayer.isPlaying()) {
-                    mActivity.getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
-                }
-            } else {
-                enableProximitySensor();
-                if (mActivity != null) {
-                    mActivity.getWindow().clearFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
-                }
+        if (on) {
+            disableProximitySensor(false /* waitForFarState */);
+            if (mIsPrepared && mMediaPlayer != null && mMediaPlayer.isPlaying()) {
+                mActivity.getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
+            }
+        } else {
+            enableProximitySensor();
+            if (mActivity != null) {
+                mActivity.getWindow().clearFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
             }
         }
     }
 
+    public boolean isSpeakerphoneOn() {
+        return mAudioManager.isSpeakerphoneOn();
+    }
+
     public void setOnVoicemailDeletedListener(OnVoicemailDeletedListener listener) {
         mOnVoicemailDeletedListener = listener;
     }