Commit 2d8ddd8a authored by Thoren Paulson's avatar Thoren Paulson Committed by Commit Bot

[Chromecast] Finish when stopped by user leaving.

We used to tear down CastWebContentsActivity when we stop because the
user left. This is needed because sometimes we can leave the activity
without tearing down, leaving the receiver running without the user
seeing it.

Bug: internal b/112775688
Test: cast_shell_junit_tests
Change-Id: If234a52facb9ea310b8899db0a9bda1a67439cd5
Reviewed-on: https://chromium-review.googlesource.com/1184044
Commit-Queue: Thoren Paulson <thoren@chromium.org>
Reviewed-by: default avatarSimeon Anfinrud <sanfin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584937}
parent db1594c2
......@@ -45,6 +45,10 @@ public class CastWebContentsActivity extends Activity {
private final Controller<Unit> mCreatedState = new Controller<>();
// Tracks whether this Activity is between onResume() and onPause().
private final Controller<Unit> mResumedState = new Controller<>();
// Tracks whether this Activity is between onStart() and onStop().
private final Controller<Unit> mStartedState = new Controller<>();
// Tracks whether the user has left according to onUserLeaveHint().
private final Controller<Unit> mUserLeftState = new Controller<>();
// Tracks the most recent Intent for the Activity.
private final Controller<Intent> mGotIntentState = new Controller<>();
// Set this to cause the Activity to finish.
......@@ -133,6 +137,11 @@ public class CastWebContentsActivity extends Activity {
intent.setFlags(flags);
startActivity(intent);
}));
Observable<?> stoppingBecauseUserLeftState =
Observable.not(mStartedState).and(mUserLeftState);
stoppingBecauseUserLeftState.watch(Observers.onEnter(
x -> { mIsFinishingState.set("User left and activity stopped."); }));
}
@Override
......@@ -156,6 +165,7 @@ public class CastWebContentsActivity extends Activity {
@Override
protected void onStart() {
if (DEBUG) Log.d(TAG, "onStart");
mStartedState.set(Unit.unit());
super.onStart();
}
......@@ -176,6 +186,7 @@ public class CastWebContentsActivity extends Activity {
@Override
protected void onStop() {
if (DEBUG) Log.d(TAG, "onStop");
mStartedState.reset();
super.onStop();
}
......@@ -189,6 +200,12 @@ public class CastWebContentsActivity extends Activity {
super.onDestroy();
}
@Override
protected void onUserLeaveHint() {
mUserLeftState.set(Unit.unit());
super.onUserLeaveHint();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (DEBUG) Log.d(TAG, "onWindowFocusChanged(%b)", hasFocus);
......
......@@ -14,6 +14,7 @@ import static org.mockito.Mockito.verify;
import android.content.Intent;
import android.media.AudioManager;
import android.view.KeyEvent;
import android.view.WindowManager;
import org.junit.Assert;
......@@ -175,4 +176,25 @@ public class CastWebContentsActivityTest {
Assert.assertTrue(Shadows.shadowOf(mShadowActivity.getWindow())
.getFlag(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON));
}
@Test
public void testStopDoesNotCauseFinish() {
mActivityLifecycle.create().start().resume();
mActivityLifecycle.pause().stop();
Assert.assertFalse(mShadowActivity.isFinishing());
}
@Test
public void testUserLeaveAndStopCausesFinish() {
mActivityLifecycle.create().start().resume();
mActivityLifecycle.pause().userLeaving().stop();
Assert.assertTrue(mShadowActivity.isFinishing());
}
@Test
public void testBackButtonFinishes() {
mActivityLifecycle.create().start().resume();
mActivity.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
Assert.assertTrue(mShadowActivity.isFinishing());
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment