Skip to content

Commit

Permalink
Add session ID to cast content window's start param
Browse files Browse the repository at this point in the history
As there are going to be multiple home screen tiles for media and remote control app.
We need to pass the session ID to android side so it could be used to retrieve target
device of a remote control app from remote control manager.

Bug: b/118458714

Test: cast_shell_junit_tests

Change-Id: I4f5372db96946c91cec3302d50fd837d26c91949
Reviewed-on: https://chromium-review.googlesource.com/c/1303236
Commit-Queue: Zhiheng(Vincent) Li <vincentli@google.com>
Reviewed-by: Luke Halliwell <halliwell@chromium.org>
Reviewed-by: Simeon Anfinrud <sanfin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#604441}
  • Loading branch information
Zhiheng(Vincent) Li authored and Commit Bot committed Oct 31, 2018
1 parent ae0e850 commit 3549935
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
public class CastContentWindowAndroid implements CastWebContentsComponent.OnComponentClosedHandler,
CastWebContentsComponent.OnKeyDownHandler,
CastWebContentsComponent.SurfaceEventHandler {
private static final String TAG = "cr_CastContentWindowAndroid";
private static final String TAG = "cr_CastContentWindow";
private static final boolean DEBUG = true;

// Note: CastContentWindowAndroid may outlive the native object. The native
// ref should be checked that it is not zero before it is used.
private long mNativeCastContentWindowAndroid;
private Context mContext;
private String mInstanceId;
private CastWebContentsComponent mComponent;

private static int sInstanceId = 1;
Expand All @@ -38,20 +37,22 @@ public class CastContentWindowAndroid implements CastWebContentsComponent.OnComp
@CalledByNative
private static CastContentWindowAndroid create(long nativeCastContentWindowAndroid,
boolean isHeadless, boolean enableTouchInput, boolean isRemoteControlMode,
boolean turnOnScreen) {
boolean turnOnScreen, String sessionId) {
return new CastContentWindowAndroid(nativeCastContentWindowAndroid,
ContextUtils.getApplicationContext(), isHeadless, enableTouchInput,
isRemoteControlMode, turnOnScreen);
isRemoteControlMode, turnOnScreen, sessionId);
}

private CastContentWindowAndroid(long nativeCastContentWindowAndroid, final Context context,
boolean isHeadless, boolean enableTouchInput, boolean isRemoteControlMode,
boolean turnOnScreen) {
boolean turnOnScreen, String sessionId) {
mNativeCastContentWindowAndroid = nativeCastContentWindowAndroid;
mContext = context;
mInstanceId = Integer.toString(sInstanceId++);
Log.i(TAG,
"Creating new CastContentWindowAndroid(No. " + sInstanceId++
+ ") Seesion ID: " + sessionId);
// TODO call nativeGetId() to set ID to CastWebContentsComponent.
mComponent = new CastWebContentsComponent(mInstanceId, this, this, this, isHeadless,
mComponent = new CastWebContentsComponent(sessionId, this, this, this, isHeadless,
enableTouchInput, isRemoteControlMode, turnOnScreen);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public boolean dispatchKeyEvent(KeyEvent event) {
|| keyCode == KeyEvent.KEYCODE_MEDIA_STOP
|| keyCode == KeyEvent.KEYCODE_MEDIA_NEXT
|| keyCode == KeyEvent.KEYCODE_MEDIA_PREVIOUS) {
CastWebContentsComponent.onKeyDown(mSurfaceHelper.getInstanceId(), keyCode);
CastWebContentsComponent.onKeyDown(mSurfaceHelper.getSessionId(), keyCode);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ static class StartParams {
public final String appId;
public final int visibilityPriority;

public StartParams(Context context, WebContents webContents, String id, int priority) {
public StartParams(Context context, WebContents webContents, String appId, int priority) {
this.context = context;
this.webContents = webContents;
appId = id;
this.appId = appId;
visibilityPriority = priority;
}
}
Expand Down Expand Up @@ -95,7 +95,7 @@ private class FragmentDelegate implements Delegate {
@Override
public void start(StartParams params) {
if (!sendIntent(CastWebContentsIntentUtils.requestStartCastFragment(params.webContents,
params.appId, params.visibilityPriority, mEnableTouchInput, mInstanceId,
params.appId, params.visibilityPriority, mEnableTouchInput, mSessionId,
mIsRemoteControlMode, mTurnOnScreen))) {
// No intent receiver to handle SHOW_WEB_CONTENT in fragment
startCastActivity(params.context, params.webContents, mEnableTouchInput,
Expand All @@ -112,13 +112,13 @@ public void stop(Context context) {
private void startCastActivity(Context context, WebContents webContents, boolean enableTouch,
boolean isRemoteControlMode, boolean turnOnScreen) {
Intent intent = CastWebContentsIntentUtils.requestStartCastActivity(
context, webContents, enableTouch, isRemoteControlMode, turnOnScreen, mInstanceId);
context, webContents, enableTouch, isRemoteControlMode, turnOnScreen, mSessionId);
if (DEBUG) Log.d(TAG, "start activity by intent: " + intent);
context.startActivity(intent);
}

private void sendStopWebContentEvent() {
Intent intent = CastWebContentsIntentUtils.requestStopWebContents(mInstanceId);
Intent intent = CastWebContentsIntentUtils.requestStopWebContents(mSessionId);
if (DEBUG) Log.d(TAG, "stop: send STOP_WEB_CONTENT intent: " + intent);
sendIntentSync(intent);
}
Expand All @@ -142,7 +142,7 @@ public void onServiceDisconnected(ComponentName name) {
public void start(StartParams params) {
if (DEBUG) Log.d(TAG, "start");
Intent intent = CastWebContentsIntentUtils.requestStartCastService(
params.context, params.webContents, mInstanceId);
params.context, params.webContents, mSessionId);
params.context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

Expand All @@ -158,7 +158,7 @@ public void stop(Context context) {

private final OnComponentClosedHandler mComponentClosedHandler;
private final OnKeyDownHandler mKeyDownHandler;
private final String mInstanceId;
private final String mSessionId;
private final SurfaceEventHandler mSurfaceEventHandler;
private final Controller<WebContents> mHasWebContentsState = new Controller<>();
private Delegate mDelegate;
Expand All @@ -167,21 +167,21 @@ public void stop(Context context) {
private final boolean mIsRemoteControlMode;
private final boolean mTurnOnScreen;

public CastWebContentsComponent(String instanceId,
public CastWebContentsComponent(String sessionId,
OnComponentClosedHandler onComponentClosedHandler, OnKeyDownHandler onKeyDownHandler,
SurfaceEventHandler surfaceEventHandler, boolean isHeadless, boolean enableTouchInput,
boolean isRemoteControlMode, boolean turnOnScreen) {
if (DEBUG) {
Log.d(TAG,
"New CastWebContentsComponent. Instance ID: " + instanceId + "; isHeadless: "
"New CastWebContentsComponent. Instance ID: " + sessionId + "; isHeadless: "
+ isHeadless + "; enableTouchInput:" + enableTouchInput
+ "; isRemoteControlMode:" + isRemoteControlMode);
}

mComponentClosedHandler = onComponentClosedHandler;
mEnableTouchInput = enableTouchInput;
mKeyDownHandler = onKeyDownHandler;
mInstanceId = instanceId;
mSessionId = sessionId;
mSurfaceEventHandler = surfaceEventHandler;
mIsRemoteControlMode = isRemoteControlMode;
mTurnOnScreen = turnOnScreen;
Expand All @@ -199,7 +199,7 @@ public CastWebContentsComponent(String instanceId,

mHasWebContentsState.subscribe(x -> {
final IntentFilter filter = new IntentFilter();
Uri instanceUri = CastWebContentsIntentUtils.getInstanceUri(instanceId);
Uri instanceUri = CastWebContentsIntentUtils.getInstanceUri(sessionId);
filter.addDataScheme(instanceUri.getScheme());
filter.addDataAuthority(instanceUri.getAuthority(), null);
filter.addDataPath(instanceUri.getPath(), PatternMatcher.PATTERN_LITERAL);
Expand All @@ -213,17 +213,17 @@ public CastWebContentsComponent(String instanceId,

private void onReceiveIntent(Intent intent) {
if (CastWebContentsIntentUtils.isIntentOfActivityStopped(intent)) {
if (DEBUG) Log.d(TAG, "onReceive ACTION_ACTIVITY_STOPPED instance=" + mInstanceId);
if (DEBUG) Log.d(TAG, "onReceive ACTION_ACTIVITY_STOPPED instance=" + mSessionId);
if (mComponentClosedHandler != null) mComponentClosedHandler.onComponentClosed();
} else if (CastWebContentsIntentUtils.isIntentOfKeyEvent(intent)) {
if (DEBUG) Log.d(TAG, "onReceive ACTION_KEY_EVENT instance=" + mInstanceId);
if (DEBUG) Log.d(TAG, "onReceive ACTION_KEY_EVENT instance=" + mSessionId);
int keyCode = CastWebContentsIntentUtils.getKeyCode(intent);
if (mKeyDownHandler != null) mKeyDownHandler.onKeyDown(keyCode);
} else if (CastWebContentsIntentUtils.isIntentOfVisibilityChange(intent)) {
int visibilityType = CastWebContentsIntentUtils.getVisibilityType(intent);
if (DEBUG) {
Log.d(TAG,
"onReceive ACTION_ON_VISIBILITY_CHANGE instance=" + mInstanceId
"onReceive ACTION_ON_VISIBILITY_CHANGE instance=" + mSessionId
+ "; visibilityType=" + visibilityType);
}
if (mSurfaceEventHandler != null) {
Expand All @@ -233,22 +233,22 @@ private void onReceiveIntent(Intent intent) {
int gestureType = CastWebContentsIntentUtils.getGestureType(intent);
if (DEBUG) {
Log.d(TAG,
"onReceive ACTION_ON_GESTURE_CHANGE instance=" + mInstanceId
"onReceive ACTION_ON_GESTURE_CHANGE instance=" + mSessionId
+ "; gesture=" + gestureType);
}
if (mSurfaceEventHandler != null) {
if (mSurfaceEventHandler.consumeGesture(gestureType)) {
if (DEBUG) Log.d(TAG, "send gesture consumed instance=" + mInstanceId);
if (DEBUG) Log.d(TAG, "send gesture consumed instance=" + mSessionId);
sendIntentSync(CastWebContentsIntentUtils.gestureConsumed(
mInstanceId, gestureType, true));
mSessionId, gestureType, true));
} else {
if (DEBUG) Log.d(TAG, "send gesture NOT consumed instance=" + mInstanceId);
if (DEBUG) Log.d(TAG, "send gesture NOT consumed instance=" + mSessionId);
sendIntentSync(CastWebContentsIntentUtils.gestureConsumed(
mInstanceId, gestureType, false));
mSessionId, gestureType, false));
}
} else {
sendIntentSync(CastWebContentsIntentUtils.gestureConsumed(
mInstanceId, gestureType, false));
sendIntentSync(
CastWebContentsIntentUtils.gestureConsumed(mSessionId, gestureType, false));
}
}
}
Expand All @@ -267,7 +267,7 @@ public void start(StartParams params) {
if (DEBUG) {
Log.d(TAG,
"Starting WebContents with delegate: " + mDelegate.getClass().getSimpleName()
+ "; Instance ID: " + mInstanceId + "; App ID: " + params.appId
+ "; Instance ID: " + mSessionId + "; App ID: " + params.appId
+ "; Visibility Priority: " + params.visibilityPriority);
}
mHasWebContentsState.set(params.webContents);
Expand All @@ -279,7 +279,7 @@ public void stop(Context context) {
if (DEBUG) {
Log.d(TAG,
"stop with delegate: " + mDelegate.getClass().getSimpleName()
+ "; Instance ID: " + mInstanceId);
+ "; Instance ID: " + mSessionId);
}
if (mStarted) {
mHasWebContentsState.reset();
Expand All @@ -290,41 +290,43 @@ public void stop(Context context) {
}

public void requestVisibilityPriority(int visibilityPriority) {
if (DEBUG) Log.d(TAG, "requestVisibilityPriority: " + mInstanceId + "; Visibility:"
+ visibilityPriority);
if (DEBUG)
Log.d(TAG,
"requestVisibilityPriority: " + mSessionId
+ "; Visibility:" + visibilityPriority);
sendIntentSync(CastWebContentsIntentUtils.requestVisibilityPriority(
mInstanceId, visibilityPriority));
mSessionId, visibilityPriority));
}

public void requestMoveOut() {
if (DEBUG) Log.d(TAG, "requestMoveOut: " + mInstanceId);
sendIntentSync(CastWebContentsIntentUtils.requestMoveOut(mInstanceId));
if (DEBUG) Log.d(TAG, "requestMoveOut: " + mSessionId);
sendIntentSync(CastWebContentsIntentUtils.requestMoveOut(mSessionId));
}

public void enableTouchInput(boolean enabled) {
if (DEBUG) Log.d(TAG, "enableTouchInput enabled:" + enabled);
mEnableTouchInput = enabled;
sendIntentSync(CastWebContentsIntentUtils.enableTouchInput(mInstanceId, enabled));
sendIntentSync(CastWebContentsIntentUtils.enableTouchInput(mSessionId, enabled));
}

public static void onComponentClosed(String instanceId) {
public static void onComponentClosed(String sessionId) {
if (DEBUG) Log.d(TAG, "onComponentClosed");
sendIntentSync(CastWebContentsIntentUtils.onActivityStopped(instanceId));
sendIntentSync(CastWebContentsIntentUtils.onActivityStopped(sessionId));
}

public static void onKeyDown(String instanceId, int keyCode) {
public static void onKeyDown(String sessionId, int keyCode) {
if (DEBUG) Log.d(TAG, "onKeyDown");
sendIntentSync(CastWebContentsIntentUtils.onKeyDown(instanceId, keyCode));
sendIntentSync(CastWebContentsIntentUtils.onKeyDown(sessionId, keyCode));
}

public static void onVisibilityChange(String instanceId, int visibilityType) {
public static void onVisibilityChange(String sessionId, int visibilityType) {
if (DEBUG) Log.d(TAG, "onVisibilityChange");
sendIntentSync(CastWebContentsIntentUtils.onVisibilityChange(instanceId, visibilityType));
sendIntentSync(CastWebContentsIntentUtils.onVisibilityChange(sessionId, visibilityType));
}

public static void onGesture(String instanceId, int gestureType) {
if (DEBUG) Log.d(TAG, "onGesture: " + instanceId + "; gestureType: "+ gestureType);
sendIntentSync(CastWebContentsIntentUtils.onGesture(instanceId, gestureType));
public static void onGesture(String sessionId, int gestureType) {
if (DEBUG) Log.d(TAG, "onGesture: " + sessionId + "; gestureType: " + gestureType);
sendIntentSync(CastWebContentsIntentUtils.onGesture(sessionId, gestureType));
}

private static boolean sendIntent(Intent in) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public class CastWebContentsIntentUtils {
/** Key of extra value of the intent to start a web content, value is app ID of cast app */
static final String INTENT_EXTRA_APP_ID = "content_app_id";

/** Key of extra value of the intent to start a web content, value is session ID of cast app */
static final String INTENT_EXTRA_SESSION_ID = "content_session_id";

/** Key of extra value of the intent to start a web content, value is object of WebContents */
static final String INTENT_EXTRA_WEB_CONTENTS =
"com.google.android.apps.castshell.intent.extra.WEB_CONTENTS";
Expand Down Expand Up @@ -306,6 +309,7 @@ public static Intent requestStartCastFragment(WebContents webContents, String ap
intent.setAction(CastIntents.ACTION_SHOW_WEB_CONTENT);
intent.putExtra(INTENT_EXTRA_URI, getInstanceUri(instanceId).toString());
intent.putExtra(INTENT_EXTRA_APP_ID, appId);
intent.putExtra(INTENT_EXTRA_SESSION_ID, instanceId);
intent.putExtra(INTENT_EXTRA_VISIBILITY_PRIORITY, visibilityPriority);
intent.putExtra(INTENT_EXTRA_TOUCH_INPUT_ENABLED, enableTouch);
intent.putExtra(INTENT_EXTRA_TURN_ON_SCREEN, turnOnScreen);
Expand Down Expand Up @@ -340,6 +344,16 @@ public static String getAppId(Intent in) {
return getAppId(in.getExtras());
}

// Used by ACTION_SHOW_WEB_CONTENT
public static String getSessionId(Bundle bundle) {
return bundle.getString(INTENT_EXTRA_SESSION_ID);
}

// Used by ACTION_SHOW_WEB_CONTENT
public static String getSessionId(Intent in) {
return getSessionId(in.getExtras());
}

// Used by ACTION_VIEW, ACTION_SHOW_WEB_CONTENT
public static WebContents getWebContents(Bundle bundle) {
return (WebContents) bundle.getParcelable(INTENT_EXTRA_WEB_CONTENTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CastWebContentsSurfaceHelper {
// Activated when we have WebContents to display.
private final Controller<StartParams> mStartParamsState = new Controller<>();

private String mInstanceId;
private String mSessionId;
private MediaSessionGetter mMediaSessionGetter;

// TODO(vincentli) interrupt touch event from Fragment's root view when it's false.
Expand Down Expand Up @@ -175,7 +175,7 @@ public static StartParams fromBundle(Bundle bundle) {
// http://developer.android.com/training/managing-audio/volume-playback.html
hostActivity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Notify CastWebContentsComponent when closed.
return () -> CastWebContentsComponent.onComponentClosed(mInstanceId);
return () -> CastWebContentsComponent.onComponentClosed(mSessionId);
});

// When onDestroy() is called after onNewStartParams(), log and reset StartParams states.
Expand All @@ -189,7 +189,7 @@ public static StartParams fromBundle(Bundle bundle) {
// Cache relevant fields from StartParams in instance variables.
mStartParamsState.subscribe(Observers.onEnter(params -> {
mTouchInputEnabled = params.touchInputEnabled;
mInstanceId = params.uri.getPath();
mSessionId = params.uri.getPath();
}));

mCreatedState.set(Unit.unit());
Expand All @@ -202,9 +202,9 @@ void onNewStartParams(final StartParams params) {

// Closes this activity if a new WebContents is not being displayed.
private void maybeFinishLater(Handler handler, Runnable callback) {
final String currentInstanceId = mInstanceId;
final String currentSessionId = mSessionId;
handler.postDelayed(() -> {
if (currentInstanceId != null && currentInstanceId.equals(mInstanceId)) {
if (currentSessionId != null && currentSessionId.equals(mSessionId)) {
callback.run();
}
}, TEARDOWN_GRACE_PERIOD_TIMEOUT_MILLIS);
Expand All @@ -215,8 +215,8 @@ void onDestroy() {
mCreatedState.reset();
}

String getInstanceId() {
return mInstanceId;
String getSessionId() {
return mSessionId;
}

boolean isTouchInputEnabled() {
Expand Down
Loading

0 comments on commit 3549935

Please sign in to comment.