Commit 1c5ef852 authored by Thoren Paulson's avatar Thoren Paulson Committed by Commit Bot

[Chromecast] Use Activity.setTurnScreenOn on 27+.

The old way of setting a flag on Window is deprecated, and doesn't work
correctly on some newer devices.

Bug: internal b/122813747
Test: unittests, P and pre-O manual tests
Change-Id: I593c287b755ba2dd68221820621af2e955b1fd65
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1548555Reviewed-by: default avatarSimeon Anfinrud <sanfin@chromium.org>
Commit-Queue: Thoren Paulson <thoren@chromium.org>
Cr-Commit-Position: refs/heads/master@{#646608}
parent 202cc67f
...@@ -10,6 +10,7 @@ import android.content.IntentFilter; ...@@ -10,6 +10,7 @@ import android.content.IntentFilter;
import android.graphics.Color; import android.graphics.Color;
import android.media.AudioManager; import android.media.AudioManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.view.KeyEvent; import android.view.KeyEvent;
...@@ -112,8 +113,7 @@ public class CastWebContentsActivity extends Activity { ...@@ -112,8 +113,7 @@ public class CastWebContentsActivity extends Activity {
// Set flags to both exit sleep mode when this activity starts and // Set flags to both exit sleep mode when this activity starts and
// avoid entering sleep mode while playing media. If an app that shouldn't turn // avoid entering sleep mode while playing media. If an app that shouldn't turn
// on the screen is launching, we don't add TURN_SCREEN_ON. // on the screen is launching, we don't add TURN_SCREEN_ON.
if (CastWebContentsIntentUtils.shouldTurnOnScreen(intent)) if (CastWebContentsIntentUtils.shouldTurnOnScreen(intent)) turnScreenOn();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}))); })));
...@@ -290,6 +290,14 @@ public class CastWebContentsActivity extends Activity { ...@@ -290,6 +290,14 @@ public class CastWebContentsActivity extends Activity {
return false; return false;
} }
private void turnScreenOn() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setTurnScreenOn(true);
} else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
}
@RemovableInRelease @RemovableInRelease
public void finishForTesting() { public void finishForTesting() {
mIsFinishingState.set("Finish for testing"); mIsFinishingState.set("Finish for testing");
......
...@@ -12,8 +12,10 @@ import static org.mockito.Mockito.never; ...@@ -12,8 +12,10 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset; import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.media.AudioManager; import android.media.AudioManager;
import android.os.Build;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.WindowManager; import android.view.WindowManager;
...@@ -28,6 +30,9 @@ import org.robolectric.RuntimeEnvironment; ...@@ -28,6 +30,9 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows; import org.robolectric.Shadows;
import org.robolectric.android.controller.ActivityController; import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowActivity; import org.robolectric.shadows.ShadowActivity;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
...@@ -41,6 +46,20 @@ import org.chromium.testing.local.LocalRobolectricTestRunner; ...@@ -41,6 +46,20 @@ import org.chromium.testing.local.LocalRobolectricTestRunner;
@RunWith(LocalRobolectricTestRunner.class) @RunWith(LocalRobolectricTestRunner.class)
@Config(manifest = Config.NONE) @Config(manifest = Config.NONE)
public class CastWebContentsActivityTest { public class CastWebContentsActivityTest {
@Implements(Activity.class)
public static class ExtendedShadowActivity extends ShadowActivity {
private boolean mTurnScreenOn;
public boolean getTurnScreenOn() {
return mTurnScreenOn;
}
@Implementation
public void setTurnScreenOn(boolean turnScreenOn) {
mTurnScreenOn = turnScreenOn;
}
}
private ActivityController<CastWebContentsActivity> mActivityLifecycle; private ActivityController<CastWebContentsActivity> mActivityLifecycle;
private CastWebContentsActivity mActivity; private CastWebContentsActivity mActivity;
private ShadowActivity mShadowActivity; private ShadowActivity mShadowActivity;
...@@ -151,13 +170,27 @@ public class CastWebContentsActivityTest { ...@@ -151,13 +170,27 @@ public class CastWebContentsActivityTest {
} }
@Test @Test
@Config(shadows = {ExtendedShadowActivity.class})
public void testTurnsScreenOnIfTurnOnScreen() { public void testTurnsScreenOnIfTurnOnScreen() {
mActivityLifecycle = Robolectric.buildActivity(CastWebContentsActivity.class, mActivityLifecycle = Robolectric.buildActivity(CastWebContentsActivity.class,
CastWebContentsIntentUtils.requestStartCastActivity( CastWebContentsIntentUtils.requestStartCastActivity(
RuntimeEnvironment.application, mWebContents, true, false, true, "0")); RuntimeEnvironment.application, mWebContents, true, false, true, "0"));
mActivity = mActivityLifecycle.get(); mActivity = mActivityLifecycle.get();
mActivity.testingModeForTesting(); mActivity.testingModeForTesting();
mShadowActivity = Shadows.shadowOf(mActivity); ExtendedShadowActivity shadowActivity = (ExtendedShadowActivity) Shadow.extract(mActivity);
mActivityLifecycle.create();
Assert.assertTrue(shadowActivity.getTurnScreenOn());
}
@Test
@Config(sdk = {Build.VERSION_CODES.O})
public void testTurnsScreenOnIfTurnOnScreen_AndroidO() {
mActivityLifecycle = Robolectric.buildActivity(CastWebContentsActivity.class,
CastWebContentsIntentUtils.requestStartCastActivity(
RuntimeEnvironment.application, mWebContents, true, false, true, "0"));
mActivity = mActivityLifecycle.get();
mActivity.testingModeForTesting();
mActivityLifecycle.create(); mActivityLifecycle.create();
Assert.assertTrue(Shadows.shadowOf(mActivity.getWindow()) Assert.assertTrue(Shadows.shadowOf(mActivity.getWindow())
...@@ -165,17 +198,31 @@ public class CastWebContentsActivityTest { ...@@ -165,17 +198,31 @@ public class CastWebContentsActivityTest {
} }
@Test @Test
@Config(shadows = {ExtendedShadowActivity.class})
public void testDoesNotTurnScreenOnIfNotTurnOnScreen() { public void testDoesNotTurnScreenOnIfNotTurnOnScreen() {
mActivityLifecycle = Robolectric.buildActivity(CastWebContentsActivity.class, mActivityLifecycle = Robolectric.buildActivity(CastWebContentsActivity.class,
CastWebContentsIntentUtils.requestStartCastActivity( CastWebContentsIntentUtils.requestStartCastActivity(
RuntimeEnvironment.application, mWebContents, true, false, false, "0")); RuntimeEnvironment.application, mWebContents, true, false, false, "0"));
mActivity = mActivityLifecycle.get(); mActivity = mActivityLifecycle.get();
mActivity.testingModeForTesting(); mActivity.testingModeForTesting();
mShadowActivity = Shadows.shadowOf(mActivity); ExtendedShadowActivity shadowActivity = (ExtendedShadowActivity) Shadow.extract(mActivity);
mActivityLifecycle.create();
Assert.assertFalse(shadowActivity.getTurnScreenOn());
}
@Test
@Config(sdk = {Build.VERSION_CODES.O})
public void testDoesNotTurnScreenOnIfNotTurnOnScreen_AndroidO() {
mActivityLifecycle = Robolectric.buildActivity(CastWebContentsActivity.class,
CastWebContentsIntentUtils.requestStartCastActivity(
RuntimeEnvironment.application, mWebContents, true, false, true, "0"));
mActivity = mActivityLifecycle.get();
mActivity.testingModeForTesting();
mActivityLifecycle.create(); mActivityLifecycle.create();
Assert.assertFalse(Shadows.shadowOf(mActivity.getWindow()) Assert.assertTrue(Shadows.shadowOf(mActivity.getWindow())
.getFlag(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)); .getFlag(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON));
} }
@Test @Test
......
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