Commit 0f1af763 authored by Sandeep Vijayasekar's avatar Sandeep Vijayasekar Committed by Commit Bot

[Chromecast] Allow enabling touch after window creation

Besides setting this flag at creation time, it can be
useful to enable later.

Bug: internal b/69007670
Test: none
Change-Id: I95d1eaa557e91c9c7e4ed29787928adbdb388ee2
Reviewed-on: https://chromium-review.googlesource.com/961543
Commit-Queue: Sandeep Vijayasekar <sandv@chromium.org>
Reviewed-by: default avatarLuke Halliwell <halliwell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#543884}
parent aba7e429
......@@ -61,6 +61,13 @@ public class CastContentWindowAndroid implements CastWebContentsComponent.OnComp
mContext, webContents, appId, visisbilityPriority));
}
@SuppressWarnings("unused")
@CalledByNative
private void enableTouchInput(boolean enabled) {
if (DEBUG) Log.d(TAG, "enableTouchInput");
mComponent.enableTouchInput(mInstanceId, enabled);
}
@SuppressWarnings("unused")
@CalledByNative
private void onNativeDestroyed() {
......
......@@ -36,4 +36,11 @@ public class CastIntents {
*/
public static final String ACTION_ON_WEB_CONTENT_STOPPED =
"com.google.assistant.ON_WEB_CONTENT_STOPPED";
/**
* Action type of intent from CastWebContentsComponent to notify CastWebContentsActivity that
* touch should be enabled.
*/
public static final String ACTION_ENABLE_TOUCH_INPUT =
"com.google.android.apps.castshell.intent.action.ENABLE_TOUCH_INPUT";
}
......@@ -95,9 +95,8 @@ public class CastWebContentsComponent {
@Override
public void start(StartParams params) {
if (!sendIntent(CastWebContentsIntentUtils.requestStartCastFragment(
params.webContents, params.appId, params.visibilityPriority,
mEnableTouchInput, mInstanceId))) {
if (!sendIntent(CastWebContentsIntentUtils.requestStartCastFragment(params.webContents,
params.appId, params.visibilityPriority, mEnableTouchInput, mInstanceId))) {
// No intent receiver to handle SHOW_WEB_CONTENT in fragment
startCastActivity(params.context, params.webContents, mEnableTouchInput);
}
......@@ -196,7 +195,7 @@ public class CastWebContentsComponent {
filter.addAction(CastWebContentsIntentUtils.ACTION_KEY_EVENT);
filter.addAction(CastWebContentsIntentUtils.ACTION_ON_VISIBILITY_CHANGE);
filter.addAction(CastWebContentsIntentUtils.ACTION_ON_GESTURE);
return new LocalBroadcastReceiverScope(filter, this::onReceiveIntent);
return new LocalBroadcastReceiverScope(filter, this ::onReceiveIntent);
});
}
......@@ -283,6 +282,11 @@ public class CastWebContentsComponent {
sendIntentSync(CastWebContentsIntentUtils.requestMoveOut(mInstanceId));
}
public void enableTouchInput(String instanceId, boolean enabled) {
if (DEBUG) Log.d(TAG, "enableTouchInput");
sendIntentSync(CastWebContentsIntentUtils.enableTouchInput(mInstanceId, enabled));
}
public static void onComponentClosed(String instanceId) {
if (DEBUG) Log.d(TAG, "onComponentClosed");
sendIntentSync(CastWebContentsIntentUtils.onActivityStopped(instanceId));
......
......@@ -168,8 +168,7 @@ public class CastWebContentsIntentUtils {
}
// CastWebContentsComponent.Receiver -> Host acitivity of CastWebContentsFragment
public static Intent gestureConsumed(
String instanceId, int gestureType, boolean consumed) {
public static Intent gestureConsumed(String instanceId, int gestureType, boolean consumed) {
Intent intent = new Intent(ACTION_GESTURE_CONSUMED);
intent.putExtra(INTENT_EXTRA_URI, getInstanceUri(instanceId).toString());
intent.putExtra(INTENT_EXTRA_GESTURE_TYPE, gestureType);
......@@ -317,6 +316,15 @@ public class CastWebContentsIntentUtils {
return isTouchable(in.getExtras());
}
// CastWebContentsComponent -> CastWebContentsSurfaceHelper and host activity of
// CastWebContentsFragment
public static Intent enableTouchInput(String instanceId, boolean enabled) {
Intent intent = new Intent(CastIntents.ACTION_ENABLE_TOUCH_INPUT);
intent.putExtra(INTENT_EXTRA_URI, getInstanceUri(instanceId).toString());
intent.putExtra(INTENT_EXTRA_TOUCH_INPUT_ENABLED, enabled);
return intent;
}
// CastWebContentsSurfaceHelper -> CastWebContentsActivity or host activity
// of CastWebContentsFragment
public static Intent onWebContentStopped(Uri uri) {
......
......@@ -49,6 +49,7 @@ class CastWebContentsSurfaceHelper {
private final Controller<WebContents> mHasWebContentsState = new Controller<>();
private final Controller<Unit> mResumedState = new Controller<>();
private final Controller<Uri> mHasUriState = new Controller<>();
private final Controller<Uri> mTouchEnabledState = new Controller<>();
private final Activity mHostActivity;
private final boolean mShowInFragment;
......@@ -106,6 +107,23 @@ class CastWebContentsSurfaceHelper {
maybeFinishLater();
});
});
// Receive broadcasts indicating that touch input should be enabled.
// TODO(yyzhong) Handle this intent in an external activity hosting a cast fragment as
// well.
mTouchEnabledState.watch((Uri uri) -> {
IntentFilter filter = new IntentFilter();
filter.addAction(CastIntents.ACTION_ENABLE_TOUCH_INPUT);
return new LocalBroadcastReceiverScope(filter, (Intent intent) -> {
String intentUri = CastWebContentsIntentUtils.getUriString(intent);
Log.d(TAG, "Intent action=" + intent.getAction() + "; URI=" + intentUri);
if (!uri.toString().equals(intentUri)) {
Log.d(TAG, "Current URI=" + mUri + "; intent URI=" + intentUri);
return;
}
mTouchInputEnabled = CastWebContentsIntentUtils.isTouchable(intent);
});
});
}
void onNewWebContents(
......@@ -129,6 +147,7 @@ class CastWebContentsSurfaceHelper {
mHasUriState.set(mUri);
mHasWebContentsState.set(webContents);
mTouchEnabledState.set(mUri);
showWebContents(webContents);
}
......@@ -262,6 +281,7 @@ class CastWebContentsSurfaceHelper {
detachWebContentsIfAny();
mHasWebContentsState.reset();
mHasUriState.reset();
mTouchEnabledState.reset();
}
String getInstanceId() {
......
......@@ -69,7 +69,14 @@ void CastContentWindowAndroid::CreateWindowForWebContents(
web_contents->GetJavaWebContents();
Java_CastContentWindowAndroid_createWindowForWebContents(
env, java_window_, java_web_contents, static_cast<int>(visibility_priority));
env, java_window_, java_web_contents,
static_cast<int>(visibility_priority));
}
void CastContentWindowAndroid::EnableTouchInput(bool enabled) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_CastContentWindowAndroid_enableTouchInput(
env, java_window_, static_cast<jboolean>(enabled));
}
void CastContentWindowAndroid::OnActivityStopped(
......@@ -91,8 +98,8 @@ void CastContentWindowAndroid::OnKeyDown(
void CastContentWindowAndroid::RequestVisibility(
VisibilityPriority visibility_priority) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_CastContentWindowAndroid_requestVisibilityPriority(env, java_window_,
static_cast<int>(visibility_priority));
Java_CastContentWindowAndroid_requestVisibilityPriority(
env, java_window_, static_cast<int>(visibility_priority));
}
void CastContentWindowAndroid::RequestMoveOut() {
......
......@@ -31,6 +31,8 @@ class CastContentWindowAndroid : public CastContentWindow {
bool is_visible,
VisibilityPriority visibility_priority) override;
void EnableTouchInput(bool enabled) override;
void RequestVisibility(VisibilityPriority visibility_priority) override;
void RequestMoveOut() override;
......
......@@ -136,6 +136,25 @@ public class CastWebContentsComponentTest {
verify(mActivity).unbindService(any(ServiceConnection.class));
}
@Test
public void testEnableTouchInputSendsEnableTouchToActivity() {
Assume.assumeTrue(BuildConfig.DISPLAY_WEB_CONTENTS_IN_SERVICE);
BroadcastReceiver receiver = Mockito.mock(BroadcastReceiver.class);
IntentFilter intentFilter = new IntentFilter(CastIntents.ACTION_ENABLE_TOUCH_INPUT);
LocalBroadcastManager.getInstance(ContextUtils.getApplicationContext())
.registerReceiver(receiver, intentFilter);
CastWebContentsComponent component =
new CastWebContentsComponent(INSTANCE_ID, null, null, null, false, false);
component.enableTouchInput(INSTANCE_ID, true);
LocalBroadcastManager.getInstance(ContextUtils.getApplicationContext())
.unregisterReceiver(receiver);
verify(receiver).onReceive(any(Context.class), any(Intent.class));
}
@Test
public void testOnComponentClosedCallsCallback() {
CastWebContentsComponent.OnComponentClosedHandler callback =
......
......@@ -134,8 +134,7 @@ public class CastWebContentsIntentUtilsTest {
int type = CastWebContentsIntentUtils.getGestureType(in);
Assert.assertEquals(1, type);
Assert.assertTrue(CastWebContentsIntentUtils.isGestureConsumed(in));
Assert.assertEquals(CastWebContentsIntentUtils.ACTION_GESTURE_CONSUMED,
in.getAction());
Assert.assertEquals(CastWebContentsIntentUtils.ACTION_GESTURE_CONSUMED, in.getAction());
in = CastWebContentsIntentUtils.gestureConsumed(INSTANCE_ID, 2, false);
Assert.assertNull(in.getData());
......@@ -197,6 +196,26 @@ public class CastWebContentsIntentUtilsTest {
Assert.assertEquals(EXPECTED_URI, uri);
}
@Test
public void testEnableTouchInputTrue() {
Intent in = CastWebContentsIntentUtils.enableTouchInput(INSTANCE_ID, true);
String uri = CastWebContentsIntentUtils.getUriString(in);
Assert.assertNotNull(uri);
Assert.assertEquals(EXPECTED_URI, uri);
Assert.assertEquals(CastIntents.ACTION_ENABLE_TOUCH_INPUT, in.getAction());
Assert.assertTrue(CastWebContentsIntentUtils.isTouchable(in));
}
@Test
public void testEnableTouchInputFalse() {
Intent in = CastWebContentsIntentUtils.enableTouchInput(INSTANCE_ID, false);
String uri = CastWebContentsIntentUtils.getUriString(in);
Assert.assertNotNull(uri);
Assert.assertEquals(EXPECTED_URI, uri);
Assert.assertEquals(CastIntents.ACTION_ENABLE_TOUCH_INPUT, in.getAction());
Assert.assertFalse(CastWebContentsIntentUtils.isTouchable(in));
}
@Test
public void testOnWebContentStopped() {
Intent in = CastWebContentsIntentUtils.onWebContentStopped(Uri.parse(EXPECTED_URI));
......
......@@ -89,6 +89,9 @@ class CastContentWindow {
bool is_visible,
VisibilityPriority visibility_priority) = 0;
// Enables touch input to be routed to the window's WebContents.
virtual void EnableTouchInput(bool enabled) = 0;
// Cast activity or application calls it to request for a visibility priority
// change.
virtual void RequestVisibility(VisibilityPriority visibility_priority) = 0;
......
......@@ -76,6 +76,10 @@ void CastContentWindowAura::CreateWindowForWebContents(
}
}
void CastContentWindowAura::EnableTouchInput(bool enabled) {
// TODO(halliwell): implement this
}
void CastContentWindowAura::RequestVisibility(
VisibilityPriority visibility_priority){};
......
......@@ -30,6 +30,8 @@ class CastContentWindowAura : public CastContentWindow {
void RequestVisibility(VisibilityPriority visibility_priority) override;
void RequestMoveOut() override;
void EnableTouchInput(bool enabled) override;
private:
friend class CastContentWindow;
......
......@@ -62,8 +62,9 @@ void CastWebViewExtension::ClosePage(const base::TimeDelta& shutdown_delay) {}
void CastWebViewExtension::CreateWindow(CastWindowManager* window_manager,
bool is_visible) {
window_->CreateWindowForWebContents(web_contents(), window_manager,
is_visible, chromecast::shell::VisibilityPriority::DEFAULT);
window_->CreateWindowForWebContents(
web_contents(), window_manager, is_visible,
chromecast::shell::VisibilityPriority::DEFAULT);
web_contents()->Focus();
}
......
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