Merge "Resume voicemail playback at previous position on resume." into ub-contactsdialer-a-dev
diff --git a/res/drawable/ic_voicemail_seek_handle_disabled.xml b/res/drawable/ic_voicemail_seek_handle_disabled.xml
new file mode 100644
index 0000000..1262808
--- /dev/null
+++ b/res/drawable/ic_voicemail_seek_handle_disabled.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ 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
+  -->
+<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
+        android:src="@drawable/ic_handle"
+        android:autoMirrored="true"
+        android:tint="@color/voicemail_icon_disabled_tint" >
+</bitmap>
\ No newline at end of file
diff --git a/src/com/android/dialer/voicemail/VoicemailPlaybackLayout.java b/src/com/android/dialer/voicemail/VoicemailPlaybackLayout.java
index 440d921..f86fc55 100644
--- a/src/com/android/dialer/voicemail/VoicemailPlaybackLayout.java
+++ b/src/com/android/dialer/voicemail/VoicemailPlaybackLayout.java
@@ -19,6 +19,7 @@
 import android.app.Activity;
 import android.app.Fragment;
 import android.content.Context;
+import android.graphics.drawable.Drawable;
 import android.media.MediaPlayer;
 import android.net.Uri;
 import android.os.Bundle;
@@ -238,6 +239,8 @@
     private TextView mTotalDurationText;
 
     private PositionUpdater mPositionUpdater;
+    private Drawable mVoicemailSeekHandleEnabled;
+    private Drawable mVoicemailSeekHandleDisabled;
 
     public VoicemailPlaybackLayout(Context context) {
         this(context, null);
@@ -277,6 +280,11 @@
 
         mPositionText.setText(formatAsMinutesAndSeconds(0));
         mTotalDurationText.setText(formatAsMinutesAndSeconds(0));
+
+        mVoicemailSeekHandleEnabled = getResources().getDrawable(
+                R.drawable.ic_voicemail_seek_handle, mContext.getTheme());
+        mVoicemailSeekHandleDisabled = getResources().getDrawable(
+                R.drawable.ic_voicemail_seek_handle_disabled, mContext.getTheme());
     }
 
     @Override
@@ -365,17 +373,22 @@
     @Override
     public void disableUiElements() {
         mStartStopButton.setEnabled(false);
-        mPlaybackSeek.setProgress(0);
         mPlaybackSeek.setEnabled(false);
+        mPlaybackSeek.setThumb(mVoicemailSeekHandleDisabled);
     }
 
     @Override
     public void enableUiElements() {
         mStartStopButton.setEnabled(true);
         mPlaybackSeek.setEnabled(true);
+        mPlaybackSeek.setThumb(mVoicemailSeekHandleEnabled);
+    }
 
-        mPositionText.setVisibility(View.VISIBLE);
-        mTotalDurationText.setVisibility(View.VISIBLE);
+    @Override
+    public void resetSeekBar() {
+        mPlaybackSeek.setProgress(0);
+        mPlaybackSeek.setEnabled(false);
+        mPlaybackSeek.setThumb(mVoicemailSeekHandleDisabled);
     }
 
     @Override
diff --git a/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java b/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java
index 208096d..7a2bffc 100644
--- a/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java
+++ b/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java
@@ -85,6 +85,7 @@
         void setFetchContentTimeout();
         void setIsFetchingContent();
         void setPresenter(VoicemailPlaybackPresenter presenter, Uri voicemailUri);
+        void resetSeekBar();
     }
 
     public interface OnVoicemailDeletedListener {
@@ -272,16 +273,20 @@
      * Reset the presenter for playback back to its original state.
      */
     public void resetAll() {
-        reset();
+        pausePresenter(true);
 
         mView = null;
         mVoicemailUri = null;
     }
 
     /**
-     * Reset the presenter such that it is as if the voicemail has not been played.
+     * When navigating away from voicemail playback, we need to release the media player,
+     * pause the UI and save the position.
+     *
+     * @param reset {@code true} if we want to reset the position of the playback, {@code false} if
+     * we want to retain the current position (in case we return to the voicemail).
      */
-    public void reset() {
+    public void pausePresenter(boolean reset) {
         if (mMediaPlayer != null) {
             mMediaPlayer.release();
             mMediaPlayer = null;
@@ -291,12 +296,19 @@
 
         mIsPrepared = false;
         mIsPlaying = false;
-        mPosition = 0;
-        mDuration.set(0);
+
+        if (reset) {
+            // We want to reset the position whether or not the view is valid.
+            mPosition = 0;
+        }
 
         if (mView != null) {
             mView.onPlaybackStopped();
-            mView.setClipPosition(0, mDuration.get());
+            if (reset) {
+                mView.setClipPosition(0, mDuration.get());
+            } else {
+                mPosition = mView.getDesiredClipPosition();
+            }
         }
     }
 
@@ -312,7 +324,7 @@
         }
 
         // Release the media player, otherwise there may be failures.
-        reset();
+        pausePresenter(false);
 
         if (mActivity != null) {
             mActivity.getWindow().clearFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
@@ -345,8 +357,8 @@
      * voicemail we've been asked to play has any content available.
      * <p>
      * Notify the user that we are fetching the content, then check to see if the content field in
-     * the DB is set. If set, we proceed to {@link #prepareContent()} method. If not set, make
-     * a request to fetch the content asynchronously via {@link #requestContent()}.
+     * the DB is set. If set, we proceed to {@link #prepareContent()} method. We get the duration of
+     * the voicemail from the query and set it if the content is not available.
      */
     private void checkForContent() {
         mAsyncTaskExecutor.submit(Tasks.CHECK_FOR_CONTENT, new AsyncTask<Void, Void, Boolean>() {
@@ -360,6 +372,7 @@
                 if (hasContent) {
                     prepareContent();
                 } else {
+                    mView.resetSeekBar();
                     mView.setClipPosition(0, mDuration.get());
                 }
             }
@@ -523,7 +536,6 @@
         mIsPrepared = true;
 
         mDuration.set(mMediaPlayer.getDuration());
-        mPosition = mMediaPlayer.getCurrentPosition();
 
         Log.d(TAG, "onPrepared: mPosition=" + mPosition);
         mView.setClipPosition(mPosition, mDuration.get());
@@ -618,6 +630,7 @@
         if (!mMediaPlayer.isPlaying()) {
             // Clamp the start position between 0 and the duration.
             mPosition = Math.max(0, Math.min(mPosition, mDuration.get()));
+
             mMediaPlayer.seekTo(mPosition);
 
             try {