OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.media.router.cast; | 5 package org.chromium.chrome.browser.media.router.cast; |
6 | 6 |
7 import android.annotation.SuppressLint; | 7 import android.annotation.SuppressLint; |
8 import android.content.Context; | |
9 import android.os.Bundle; | 8 import android.os.Bundle; |
10 | 9 |
11 import com.google.android.gms.cast.ApplicationMetadata; | 10 import com.google.android.gms.cast.ApplicationMetadata; |
12 import com.google.android.gms.cast.Cast; | 11 import com.google.android.gms.cast.Cast; |
13 import com.google.android.gms.cast.CastStatusCodes; | 12 import com.google.android.gms.cast.CastStatusCodes; |
14 import com.google.android.gms.cast.LaunchOptions; | 13 import com.google.android.gms.cast.LaunchOptions; |
15 import com.google.android.gms.common.ConnectionResult; | 14 import com.google.android.gms.common.ConnectionResult; |
16 import com.google.android.gms.common.api.GoogleApiClient; | 15 import com.google.android.gms.common.api.GoogleApiClient; |
17 import com.google.android.gms.common.api.PendingResult; | 16 import com.google.android.gms.common.api.PendingResult; |
18 import com.google.android.gms.common.api.ResultCallback; | 17 import com.google.android.gms.common.api.ResultCallback; |
19 import com.google.android.gms.common.api.Status; | 18 import com.google.android.gms.common.api.Status; |
20 | 19 |
| 20 import org.chromium.base.ContextUtils; |
21 import org.chromium.base.Log; | 21 import org.chromium.base.Log; |
22 import org.chromium.chrome.browser.media.router.ChromeMediaRouter; | 22 import org.chromium.chrome.browser.media.router.ChromeMediaRouter; |
23 import org.chromium.chrome.browser.media.router.MediaRoute; | 23 import org.chromium.chrome.browser.media.router.MediaRoute; |
24 | 24 |
25 /** | 25 /** |
26 * Establishes a {@link MediaRoute} by starting a Cast application represented b
y the given | 26 * Establishes a {@link MediaRoute} by starting a Cast application represented b
y the given |
27 * presentation URL. Reports success or failure to {@link ChromeMediaRouter}. | 27 * presentation URL. Reports success or failure to {@link ChromeMediaRouter}. |
28 * Since there're numerous asynchronous calls involved in getting the applicatio
n to launch | 28 * Since there're numerous asynchronous calls involved in getting the applicatio
n to launch |
29 * the class is implemented as a state machine. | 29 * the class is implemented as a state machine. |
30 */ | 30 */ |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 public boolean isIncognito() { | 157 public boolean isIncognito() { |
158 return mIsIncognito; | 158 return mIsIncognito; |
159 } | 159 } |
160 | 160 |
161 public int getNativeRequestId() { | 161 public int getNativeRequestId() { |
162 return mRequestId; | 162 return mRequestId; |
163 } | 163 } |
164 | 164 |
165 /** | 165 /** |
166 * Starts the process of launching the application on the Cast device. | 166 * Starts the process of launching the application on the Cast device. |
167 * @param applicationContext application context | |
168 * implementation provided by the caller. | |
169 */ | 167 */ |
170 public void start(Context applicationContext) { | 168 public void start() { |
171 assert applicationContext != null; | |
172 | |
173 if (mState != STATE_IDLE) throwInvalidState(); | 169 if (mState != STATE_IDLE) throwInvalidState(); |
174 | 170 |
175 mApiClient = createApiClient(mCastListener, applicationContext); | 171 mApiClient = createApiClient(mCastListener); |
176 mApiClient.connect(); | 172 mApiClient.connect(); |
177 mState = STATE_CONNECTING_TO_API; | 173 mState = STATE_CONNECTING_TO_API; |
178 } | 174 } |
179 | 175 |
180 @Override | 176 @Override |
181 public void onConnected(Bundle connectionHint) { | 177 public void onConnected(Bundle connectionHint) { |
182 if (mState != STATE_CONNECTING_TO_API && mState != STATE_API_CONNECTION_
SUSPENDED) { | 178 if (mState != STATE_CONNECTING_TO_API && mState != STATE_API_CONNECTION_
SUSPENDED) { |
183 throwInvalidState(); | 179 throwInvalidState(); |
184 } | 180 } |
185 | 181 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 // See https://crbug.com/522478 | 222 // See https://crbug.com/522478 |
227 @Override | 223 @Override |
228 public void onConnectionFailed(ConnectionResult result) { | 224 public void onConnectionFailed(ConnectionResult result) { |
229 if (mState != STATE_CONNECTING_TO_API) throwInvalidState(); | 225 if (mState != STATE_CONNECTING_TO_API) throwInvalidState(); |
230 | 226 |
231 Log.e(TAG, "GoogleApiClient connection failed: %d, %b", | 227 Log.e(TAG, "GoogleApiClient connection failed: %d, %b", |
232 result.getErrorCode(), result.hasResolution()); | 228 result.getErrorCode(), result.hasResolution()); |
233 reportError(); | 229 reportError(); |
234 } | 230 } |
235 | 231 |
236 private GoogleApiClient createApiClient(Cast.Listener listener, Context cont
ext) { | 232 private GoogleApiClient createApiClient(Cast.Listener listener) { |
237 Cast.CastOptions.Builder apiOptionsBuilder = | 233 Cast.CastOptions.Builder apiOptionsBuilder = |
238 new Cast.CastOptions.Builder(mSink.getDevice(), listener) | 234 new Cast.CastOptions.Builder(mSink.getDevice(), listener) |
239 // TODO(avayvod): hide this behind the flag or remove | 235 // TODO(avayvod): hide this behind the flag or remove |
240 .setVerboseLoggingEnabled(true); | 236 .setVerboseLoggingEnabled(true); |
241 | 237 |
242 return new GoogleApiClient.Builder(context) | 238 return new GoogleApiClient.Builder(ContextUtils.getApplicationContext()) |
243 .addApi(Cast.API, apiOptionsBuilder.build()) | 239 .addApi(Cast.API, apiOptionsBuilder.build()) |
244 .addConnectionCallbacks(this) | 240 .addConnectionCallbacks(this) |
245 .addOnConnectionFailedListener(this) | 241 .addOnConnectionFailedListener(this) |
246 .build(); | 242 .build(); |
247 } | 243 } |
248 | 244 |
249 private PendingResult<Cast.ApplicationConnectionResult> launchApplication( | 245 private PendingResult<Cast.ApplicationConnectionResult> launchApplication( |
250 GoogleApiClient apiClient, | 246 GoogleApiClient apiClient, |
251 String appId, | 247 String appId, |
252 boolean relaunchIfRunning) { | 248 boolean relaunchIfRunning) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 | 286 |
291 terminate(); | 287 terminate(); |
292 } | 288 } |
293 | 289 |
294 private void terminate() { | 290 private void terminate() { |
295 mApiClient.unregisterConnectionCallbacks(this); | 291 mApiClient.unregisterConnectionCallbacks(this); |
296 mApiClient.unregisterConnectionFailedListener(this); | 292 mApiClient.unregisterConnectionFailedListener(this); |
297 mState = STATE_TERMINATED; | 293 mState = STATE_TERMINATED; |
298 } | 294 } |
299 } | 295 } |
OLD | NEW |