Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: chromecast/browser/android/apk/src/org/chromium/chromecast/shell/CastWebContentsComponent.java

Issue 2874943002: [Chromecast] Add service for "displaying" cast web contents. (Closed)
Patch Set: Add service for "displaying" cast web contents. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.chromecast.shell;
6
7 import android.content.BroadcastReceiver;
8 import android.content.ComponentName;
9 import android.content.Context;
10 import android.content.Intent;
11 import android.content.IntentFilter;
12 import android.content.ServiceConnection;
13 import android.net.Uri;
14 import android.os.IBinder;
15 import android.os.PatternMatcher;
16 import android.support.v4.content.LocalBroadcastManager;
17
18 import org.chromium.base.Log;
19 import org.chromium.content_public.browser.WebContents;
20
21 /**
22 * A layer of indirection between CastContentWindowAndroid and CastWebContents(A ctivity|Service).
23 * <p>
24 * On builds with CAST_ANDROID_USE_SERVICE set to false, it will use CastWebCont entsActivity,
25 * otherwise, it will use CastWebContentsService.
26 */
27 public class CastWebContentsComponent {
28 /**
29 * Callback interface for when the associated component is closed or the
30 * WebContents is detached.
31 */
32 public interface OnComponentClosedHandler { void onComponentClosed(); }
33
34 /**
35 * Callback interface for passing along keyDown events. This only applies
36 * to CastWebContentsActivity, really.
37 */
38 public interface OnKeyDownHandler { void onKeyDown(int keyCode); }
39
40 private interface Delegate {
41 void start(Context context, WebContents webContents);
42 void stop(Context context);
43 }
44
45 private class ActivityDelegate implements Delegate {
46 private static final String TAG = "cr_CastWebComponent_AD";
47
48 @Override
49 public void start(Context context, WebContents webContents) {
50 if (DEBUG) Log.d(TAG, "start");
51
52 Intent intent = new Intent(Intent.ACTION_VIEW, getInstanceUri(mInsta nceId), context,
53 CastWebContentsActivity.class);
54 intent.putExtra(ACTION_EXTRA_WEB_CONTENTS, webContents);
55 // FLAG_ACTIVITY_SINGLE_TOP will try to reuse existing activity.
56 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY _MULTIPLE_TASK
57 | Intent.FLAG_ACTIVITY_SINGLE_TOP);
58 context.startActivity(intent);
59 }
60
61 @Override
62 public void stop(Context context) {
63 if (DEBUG) Log.d(TAG, "stop");
64
65 Intent intent = new Intent(
66 CastWebContentsActivity.ACTION_STOP_ACTIVITY, getInstanceUri (mInstanceId));
67 LocalBroadcastManager.getInstance(context).sendBroadcastSync(intent) ;
68 }
69 }
70
71 private class ServiceDelegate implements Delegate {
72 private static final String TAG = "cr_CastWebComponent_SD";
73
74 private ServiceConnection mConnection = new ServiceConnection() {
75 @Override
76 public void onServiceConnected(ComponentName name, IBinder service) {}
77
78 @Override
79 public void onServiceDisconnected(ComponentName name) {
80 if (DEBUG) Log.d(TAG, "onServiceDisconnected");
81
82 if (mComponentClosedHandler != null) mComponentClosedHandler.onC omponentClosed();
83 }
84 };
85
86 @Override
87 public void start(Context context, WebContents webContents) {
88 if (DEBUG) Log.d(TAG, "start");
89
90 Intent intent = new Intent(Intent.ACTION_VIEW, getInstanceUri(mInsta nceId), context,
91 CastWebContentsService.class);
92 intent.putExtra(ACTION_EXTRA_WEB_CONTENTS, webContents);
93 context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
94 }
95
96 @Override
97 public void stop(Context context) {
98 if (DEBUG) Log.d(TAG, "stop");
99
100 context.unbindService(mConnection);
101 }
102 }
103
104 private class Receiver extends BroadcastReceiver {
105 @Override
106 public void onReceive(Context context, Intent intent) {
107 if (intent.getAction().equals(ACTION_ACTIVITY_STOPPED)) {
108 if (DEBUG) Log.d(TAG, "onReceive ACTION_ACTIVITY_STOPPED");
109
110 if (mComponentClosedHandler != null) mComponentClosedHandler.onC omponentClosed();
111 } else if (intent.getAction().equals(ACTION_KEY_EVENT)) {
112 if (DEBUG) Log.d(TAG, "onReceive ACTION_KEY_EVENT");
113
114 int keyCode = intent.getIntExtra(ACTION_EXTRA_KEY_CODE, 0);
115 if (mKeyDownHandler != null) mKeyDownHandler.onKeyDown(keyCode);
116 }
117 }
118 }
119
120 static final String ACTION_DATA_SCHEME = "cast";
121 static final String ACTION_DATA_AUTHORITY = "webcontents";
122 static final String ACTION_EXTRA_WEB_CONTENTS =
123 "com.google.android.apps.castshell.intent.extra.WEB_CONTENTS";
124
125 private static final String TAG = "cr_CastWebComponent";
126 private static final boolean DEBUG = false;
127
128 private static final String ACTION_EXTRA_KEY_CODE =
129 "com.google.android.apps.castshell.intent.extra.KEY_CODE";
130 private static final String ACTION_KEY_EVENT =
131 "com.google.android.apps.castshell.intent.action.KEY_EVENT";
132 private static final String ACTION_ACTIVITY_STOPPED =
133 "com.google.android.apps.castshell.intent.action.ACTIVITY_STOPPED";
134
135 private Delegate mDelegate;
136 private OnComponentClosedHandler mComponentClosedHandler;
137 private OnKeyDownHandler mKeyDownHandler;
138 private Receiver mReceiver;
139 private String mInstanceId;
140
141 public CastWebContentsComponent(String instanceId) {
142 mInstanceId = instanceId;
143 if (BuildConfig.DISPLAY_WEB_CONTENTS_IN_SERVICE) {
144 mDelegate = new ServiceDelegate();
145 } else {
146 mDelegate = new ActivityDelegate();
147 }
148 }
149
150 public void setOnComponentClosedHandler(OnComponentClosedHandler onComponent ClosedHandler) {
Simeon 2017/05/17 00:31:56 If we're only supporting one OnComponentClosedHand
thoren 2017/05/17 18:20:37 Done.
151 mComponentClosedHandler = onComponentClosedHandler;
152 }
153
154 public void setOnKeyDownHandler(OnKeyDownHandler onKeyDownHandler) {
155 mKeyDownHandler = onKeyDownHandler;
156 }
157
158 public void start(Context context, WebContents webContents) {
159 if (DEBUG) Log.d(TAG, "start");
160
161 Uri instanceUri = getInstanceUri(mInstanceId);
162 mReceiver = new Receiver();
163 IntentFilter filter = new IntentFilter();
164 filter.addDataScheme(instanceUri.getScheme());
165 filter.addDataAuthority(instanceUri.getAuthority(), null);
166 filter.addDataPath(instanceUri.getPath(), PatternMatcher.PATTERN_LITERAL );
167 filter.addAction(ACTION_ACTIVITY_STOPPED);
168 filter.addAction(ACTION_KEY_EVENT);
169 LocalBroadcastManager.getInstance(context).registerReceiver(mReceiver, f ilter);
170
171 mDelegate.start(context, webContents);
172 }
173
174 public void stop(Context context) {
175 if (DEBUG) Log.d(TAG, "stop");
176
177 LocalBroadcastManager.getInstance(context).unregisterReceiver(mReceiver) ;
178 mDelegate.stop(context);
179 }
180
181 public static void onComponentClosed(Context context, String instanceId) {
182 if (DEBUG) Log.d(TAG, "onComponentClosed");
183
184 Intent intent = new Intent(ACTION_ACTIVITY_STOPPED, getInstanceUri(insta nceId));
185 LocalBroadcastManager.getInstance(context).sendBroadcastSync(intent);
186 }
187
188 public static void onKeyDown(Context context, String instanceId, int keyCode ) {
Simeon 2017/05/17 00:31:56 I'd be interested in putting this logic in a diffe
thoren 2017/05/17 18:20:36 Acknowledged.
189 if (DEBUG) Log.d(TAG, "onKeyDown");
190
191 Intent intent = new Intent(ACTION_KEY_EVENT, getInstanceUri(instanceId)) ;
192 intent.putExtra(ACTION_EXTRA_KEY_CODE, keyCode);
193 LocalBroadcastManager.getInstance(context).sendBroadcastSync(intent);
194 }
195
196 private static Uri getInstanceUri(String instanceId) {
197 Uri instanceUri = new Uri.Builder()
198 .scheme(ACTION_DATA_SCHEME)
199 .authority(ACTION_DATA_AUTHORITY)
200 .path(instanceId)
201 .build();
202 return instanceUri;
203 }
204 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698