OLD | NEW |
(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.Context; |
| 9 import android.content.Intent; |
| 10 import android.content.IntentFilter; |
| 11 import android.net.Uri; |
| 12 import android.os.PatternMatcher; |
| 13 import android.support.v4.content.LocalBroadcastManager; |
| 14 |
| 15 import org.chromium.base.ContextUtils; |
| 16 import org.chromium.base.Log; |
| 17 import org.chromium.base.annotations.CalledByNative; |
| 18 import org.chromium.base.annotations.JNINamespace; |
| 19 import org.chromium.content_public.browser.WebContents; |
| 20 |
| 21 /** |
| 22 * The Java component of CastContentWindowAndroid. This class is responsible for |
| 23 * starting, stopping and monitoring CastWebContentsActivity. |
| 24 * |
| 25 * See chromecast/browser/cast_content_window_android.* for the native half. |
| 26 */ |
| 27 @JNINamespace("chromecast::shell") |
| 28 public class CastContentWindowAndroid { |
| 29 private static final String TAG = "cr_CastContentWindowAndroid"; |
| 30 private static final boolean DEBUG = true; |
| 31 |
| 32 // Note: CastContentWindowAndroid may outlive the native object. The native |
| 33 // ref should be checked that it is not zero before it is used. |
| 34 private long mNativeCastContentWindowAndroid; |
| 35 private Context mContext; |
| 36 private IntentFilter mActivityClosedIntentFilter; |
| 37 private BroadcastReceiver mActivityClosedBroadcastReceiver; |
| 38 private String mInstanceId; |
| 39 |
| 40 private static int sInstanceId = 1; |
| 41 |
| 42 @SuppressWarnings("unused") |
| 43 @CalledByNative |
| 44 private static CastContentWindowAndroid create(long nativeCastContentWindowA
ndroid) { |
| 45 return new CastContentWindowAndroid( |
| 46 nativeCastContentWindowAndroid, ContextUtils.getApplicationConte
xt()); |
| 47 } |
| 48 |
| 49 private CastContentWindowAndroid(long nativeCastContentWindowAndroid, Contex
t context) { |
| 50 mNativeCastContentWindowAndroid = nativeCastContentWindowAndroid; |
| 51 mContext = context; |
| 52 mInstanceId = Integer.toString(sInstanceId++); |
| 53 } |
| 54 |
| 55 private Uri getInstanceUri() { |
| 56 Uri instanceUri = new Uri.Builder() |
| 57 .scheme(CastWebContentsActivity.ACTION_DATA_SCHEME) |
| 58 .authority(CastWebContentsActivity.ACTION_DATA_AUTHORITY) |
| 59 .path(mInstanceId) |
| 60 .build(); |
| 61 return instanceUri; |
| 62 } |
| 63 |
| 64 @SuppressWarnings("unused") |
| 65 @CalledByNative |
| 66 private void showWebContents(WebContents webContents) { |
| 67 if (DEBUG) Log.d(TAG, "showWebContents"); |
| 68 |
| 69 Intent intent = new Intent( |
| 70 Intent.ACTION_VIEW, getInstanceUri(), mContext, CastWebContentsA
ctivity.class); |
| 71 |
| 72 mActivityClosedBroadcastReceiver = new BroadcastReceiver() { |
| 73 @Override |
| 74 public void onReceive(Context context, Intent intent) { |
| 75 if (intent.getAction() == CastWebContentsActivity.ACTION_ACTIVIT
Y_STOPPED) { |
| 76 onActivityStopped(); |
| 77 } else if (intent.getAction() == CastWebContentsActivity.ACTION_
KEY_EVENT) { |
| 78 int keyCode = |
| 79 intent.getIntExtra(CastWebContentsActivity.ACTION_EX
TRA_KEY_CODE, 0); |
| 80 onKeyDown(keyCode); |
| 81 } |
| 82 } |
| 83 }; |
| 84 mActivityClosedIntentFilter = new IntentFilter(); |
| 85 mActivityClosedIntentFilter.addDataScheme(intent.getData().getScheme()); |
| 86 mActivityClosedIntentFilter.addDataAuthority(intent.getData().getAuthori
ty(), null); |
| 87 mActivityClosedIntentFilter.addDataPath( |
| 88 intent.getData().getPath(), PatternMatcher.PATTERN_LITERAL); |
| 89 mActivityClosedIntentFilter.addAction(CastWebContentsActivity.ACTION_ACT
IVITY_STOPPED); |
| 90 mActivityClosedIntentFilter.addAction(CastWebContentsActivity.ACTION_KEY
_EVENT); |
| 91 LocalBroadcastManager.getInstance(mContext).registerReceiver( |
| 92 mActivityClosedBroadcastReceiver, mActivityClosedIntentFilter); |
| 93 |
| 94 intent.putExtra(CastWebContentsActivity.ACTION_EXTRA_WEB_CONTENTS, webCo
ntents); |
| 95 // FLAG_ACTIVITY_SINGLE_TOP will try to reuse existing activity. |
| 96 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MUL
TIPLE_TASK |
| 97 | Intent.FLAG_ACTIVITY_SINGLE_TOP); |
| 98 mContext.startActivity(intent); |
| 99 } |
| 100 |
| 101 @SuppressWarnings("unused") |
| 102 @CalledByNative |
| 103 private void onNativeDestroyed() { |
| 104 assert mNativeCastContentWindowAndroid != 0; |
| 105 mNativeCastContentWindowAndroid = 0; |
| 106 |
| 107 // Note: there is a potential race condition when this function is calle
d after |
| 108 // showWebContents. If the stop intent is received after the start inten
t but before |
| 109 // onCreate, the activity won't shutdown. |
| 110 // TODO(derekjchow): Add a unittest to check this behaviour. Also consid
er using |
| 111 // Instrumentation.startActivitySync to guarentee onCreate is run. |
| 112 |
| 113 if (DEBUG) Log.d(TAG, "onNativeDestroyed"); |
| 114 Intent intent = new Intent(CastWebContentsActivity.ACTION_STOP_ACTIVITY,
getInstanceUri()); |
| 115 LocalBroadcastManager.getInstance(mContext).sendBroadcastSync(intent); |
| 116 } |
| 117 |
| 118 private void onActivityStopped() { |
| 119 if (DEBUG) Log.d(TAG, "onActivityStopped"); |
| 120 if (mNativeCastContentWindowAndroid != 0) { |
| 121 nativeOnActivityStopped(mNativeCastContentWindowAndroid); |
| 122 } |
| 123 } |
| 124 |
| 125 private void onKeyDown(int keyCode) { |
| 126 if (DEBUG) Log.d(TAG, "onKeyDown"); |
| 127 if (mNativeCastContentWindowAndroid != 0) { |
| 128 nativeOnKeyDown(mNativeCastContentWindowAndroid, keyCode); |
| 129 } |
| 130 } |
| 131 |
| 132 private native void nativeOnActivityStopped(long nativeCastContentWindowAndr
oid); |
| 133 private native void nativeOnKeyDown(long nativeCastContentWindowAndroid, int
keyCode); |
| 134 } |
OLD | NEW |