diff options
author | Ta-wei Yen <twyen@google.com> | 2016-03-02 19:23:55 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2016-03-02 19:23:55 +0000 |
commit | 93163d05dc4d0992b2b7addb715f05624d53ad7e (patch) | |
tree | fe65650ab1765471efdcbf68fafefa5920daaccd | |
parent | 32085106969a45718b1b432377bbbbf9a92937f2 (diff) | |
parent | d3c14bd6ae0f1505433d9eaae0be569ce6d492d5 (diff) |
Merge "Check content before requesting a voicemail audio" into nyc-dev
am: d3c14bd6ae
* commit 'd3c14bd6ae0f1505433d9eaae0be569ce6d492d5':
Check content before requesting a voicemail audio
-rw-r--r-- | src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java | 80 |
1 files changed, 51 insertions, 29 deletions
diff --git a/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java b/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java index 2bb4de932..e58cb3405 100644 --- a/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java +++ b/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java @@ -16,13 +16,14 @@ package com.android.dialer.voicemail; +import com.google.common.annotations.VisibleForTesting; + import android.app.Activity; -import android.content.Context; import android.content.ContentResolver; +import android.content.Context; import android.content.Intent; import android.database.ContentObserver; import android.database.Cursor; -import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; import android.os.AsyncTask; @@ -30,24 +31,19 @@ import android.os.Bundle; import android.os.Handler; import android.os.PowerManager; import android.provider.VoicemailContract; +import android.support.annotation.Nullable; import android.util.Log; -import android.view.View; import android.view.WindowManager.LayoutParams; -import android.widget.SeekBar; -import com.android.dialer.R; +import com.android.common.io.MoreCloseables; import com.android.dialer.calllog.CallLogAsyncTaskUtil; import com.android.dialer.util.AsyncTaskExecutor; import com.android.dialer.util.AsyncTaskExecutors; -import com.android.common.io.MoreCloseables; -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Preconditions; import java.io.IOException; import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.RejectedExecutionException; -import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -71,7 +67,7 @@ import javax.annotation.concurrent.ThreadSafe; public class VoicemailPlaybackPresenter implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener { - private static final String TAG = VoicemailPlaybackPresenter.class.getSimpleName(); + private static final String TAG = "VmPlaybackPresenter"; /** Contract describing the behaviour we need from the ui we are controlling. */ public interface PlaybackView { @@ -101,6 +97,11 @@ public class VoicemailPlaybackPresenter implements MediaPlayer.OnPreparedListene CHECK_CONTENT_AFTER_CHANGE, } + private interface OnContentCheckedListener { + + void onContentChecked(boolean hasContent); + } + private static final String[] HAS_CONTENT_PROJECTION = new String[] { VoicemailContract.Voicemails.HAS_CONTENT, VoicemailContract.Voicemails.DURATION @@ -263,8 +264,24 @@ public class VoicemailPlaybackPresenter implements MediaPlayer.OnPreparedListene // Update the view to the current speakerphone state. mView.onSpeakerphoneOn(mIsSpeakerphoneOn); } - - checkForContent(); + /* + * Check to see if the content field in the DB is set. If set, we proceed to + * prepareContent() method. We get the duration of the voicemail from the query and set + * it if the content is not available. + */ + checkForContent(new OnContentCheckedListener() { + @Override + public void onContentChecked(boolean hasContent) { + if (hasContent) { + prepareContent(); + } else { + if (mView != null) { + mView.resetSeekBar(); + mView.setClipPosition(0, mDuration.get()); + } + } + } + }); if (startPlayingImmediately) { // Since setPlaybackView can get called during the view binding process, we don't @@ -368,15 +385,8 @@ public class VoicemailPlaybackPresenter implements MediaPlayer.OnPreparedListene /** * Checks to see if we have content available for this voicemail. - * <p> - * This method will be called once, after the fragment has been created, before we know if the - * 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. We get the duration of - * the voicemail from the query and set it if the content is not available. */ - private void checkForContent() { + private void checkForContent(final OnContentCheckedListener callback) { mAsyncTaskExecutor.submit(Tasks.CHECK_FOR_CONTENT, new AsyncTask<Void, Void, Boolean>() { @Override public Boolean doInBackground(Void... params) { @@ -385,12 +395,7 @@ public class VoicemailPlaybackPresenter implements MediaPlayer.OnPreparedListene @Override public void onPostExecute(Boolean hasContent) { - if (hasContent) { - prepareContent(); - } else if (mView != null) { - mView.resetSeekBar(); - mView.setClipPosition(0, mDuration.get()); - } + callback.onContentChecked(hasContent); } }); } @@ -648,8 +653,25 @@ public class VoicemailPlaybackPresenter implements MediaPlayer.OnPreparedListene } if (!mIsPrepared) { - // If we haven't downloaded the voicemail yet, attempt to download it. - mIsPlaying = requestContent(); + /* + * Check content before requesting content to avoid duplicated requests. It is possible + * that the UI doesn't know content has arrived if the fetch took too long causing a + * timeout, but succeeded. + */ + checkForContent(new OnContentCheckedListener() { + @Override + public void onContentChecked(boolean hasContent) { + if (!hasContent) { + // No local content, download from server. Queue playing if the request was + // issued, + mIsPlaying = requestContent(); + } else { + // Queue playing once the media play loaded the content. + mIsPlaying = true; + prepareContent(); + } + } + }); return; } |