diff options
author | Andrew Lee <anwlee@google.com> | 2015-06-08 16:54:03 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-06-08 16:54:08 +0000 |
commit | ff67bc1104e8886c4e26a61b86e7afbac6ddb02c (patch) | |
tree | 704c3c2918246e806ff66cbeef1a36ba42a2c896 | |
parent | 2f5aabc39148868cdfcf8c3d4dd876d09f0d89b9 (diff) | |
parent | 46e7fbc485dd15a6c627497f121f60798d62cf80 (diff) |
Merge "Handle FetchResultHandler race condition." into mnc-dev
-rw-r--r-- | src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java b/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java index 1a1547692..f06840139 100644 --- a/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java +++ b/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java @@ -141,6 +141,7 @@ public class VoicemailPlaybackPresenter * This variable is thread-contained, accessed only on the ui thread. */ private FetchResultHandler mFetchResultHandler; + private Handler mHandler = new Handler(); private PowerManager.WakeLock mProximityWakeLock; private AudioManager mAudioManager; @@ -298,13 +299,12 @@ public class VoicemailPlaybackPresenter * was not available. */ private void requestContent() { - Preconditions.checkState(mFetchResultHandler == null, "mFetchResultHandler should be null"); + if (mFetchResultHandler != null) { + mFetchResultHandler.destroy(); + } - Handler handler = new Handler(); - mFetchResultHandler = new FetchResultHandler(handler); - mContext.getContentResolver().registerContentObserver( - mVoicemailUri, false, mFetchResultHandler); - handler.postDelayed(mFetchResultHandler.getTimeoutRunnable(), FETCH_CONTENT_TIMEOUT_MS); + mFetchResultHandler = new FetchResultHandler(new Handler()); + mFetchResultHandler.registerContentObserver(mVoicemailUri); // Send voicemail fetch request. Intent intent = new Intent(VoicemailContract.ACTION_FETCH_VOICEMAIL, mVoicemailUri); @@ -313,30 +313,37 @@ public class VoicemailPlaybackPresenter @ThreadSafe private class FetchResultHandler extends ContentObserver implements Runnable { - private AtomicBoolean mResultStillPending = new AtomicBoolean(true); - private final Handler mHandler; + private AtomicBoolean mIsWaitingForResult = new AtomicBoolean(true); + private final Handler mFetchResultHandler; public FetchResultHandler(Handler handler) { super(handler); - mHandler = handler; + mFetchResultHandler = handler; } - public Runnable getTimeoutRunnable() { - return this; + public void registerContentObserver(Uri voicemailUri) { + if (mIsWaitingForResult.get()) { + mContext.getContentResolver().registerContentObserver( + voicemailUri, false, this); + mFetchResultHandler.postDelayed(this, FETCH_CONTENT_TIMEOUT_MS); + } } + /** + * Stop waiting for content and notify UI if {@link FETCH_CONTENT_TIMEOUT_MS} has elapsed. + */ @Override public void run() { - if (mResultStillPending.getAndSet(false)) { - mContext.getContentResolver().unregisterContentObserver(FetchResultHandler.this); + if (mIsWaitingForResult.getAndSet(false)) { + mContext.getContentResolver().unregisterContentObserver(this); mView.setFetchContentTimeout(); } } public void destroy() { - if (mResultStillPending.getAndSet(false)) { - mContext.getContentResolver().unregisterContentObserver(FetchResultHandler.this); - mHandler.removeCallbacks(this); + if (mIsWaitingForResult.getAndSet(false)) { + mContext.getContentResolver().unregisterContentObserver(this); + mFetchResultHandler.removeCallbacks(this); } } @@ -352,7 +359,7 @@ public class VoicemailPlaybackPresenter @Override public void onPostExecute(Boolean hasContent) { if (hasContent) { - if (mResultStillPending.getAndSet(false)) { + if (mIsWaitingForResult.getAndSet(false)) { mContext.getContentResolver().unregisterContentObserver( FetchResultHandler.this); prepareToPlayContent(); |