OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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.Context; |
| 8 import android.content.Intent; |
| 9 import android.net.Uri; |
| 10 import android.support.v4.content.LocalBroadcastManager; |
| 11 import android.text.TextUtils; |
| 12 import android.util.AttributeSet; |
| 13 import android.util.Log; |
| 14 import android.view.ViewGroup; |
| 15 import android.widget.FrameLayout; |
| 16 import android.widget.LinearLayout; |
| 17 |
| 18 import org.chromium.base.CalledByNative; |
| 19 import org.chromium.base.JNINamespace; |
| 20 import org.chromium.content.browser.ContentView; |
| 21 import org.chromium.content.browser.ContentViewCore; |
| 22 import org.chromium.content.browser.ContentViewRenderView; |
| 23 import org.chromium.content.browser.WebContentsObserverAndroid; |
| 24 import org.chromium.content_public.browser.LoadUrlParams; |
| 25 import org.chromium.ui.base.WindowAndroid; |
| 26 |
| 27 /** |
| 28 * Container for the various UI components that make up a shell window. |
| 29 */ |
| 30 @JNINamespace("chromecast::shell") |
| 31 public class CastWindowAndroid extends LinearLayout { |
| 32 public static final String TAG = "CastWindowAndroid"; |
| 33 |
| 34 public static final String ACTION_PAGE_LOADED = "castPageLoaded"; |
| 35 public static final String ACTION_ENABLE_DEV_TOOLS = "castEnableDevTools"; |
| 36 public static final String ACTION_DISABLE_DEV_TOOLS = "castDisableDevTools"; |
| 37 |
| 38 private ContentViewCore mContentViewCore; |
| 39 private ContentViewRenderView mContentViewRenderView; |
| 40 private WebContentsObserverAndroid mWebContentsObserver; |
| 41 private WindowAndroid mWindow; |
| 42 |
| 43 /** |
| 44 * Constructor for inflating via XML. |
| 45 */ |
| 46 public CastWindowAndroid(Context context, AttributeSet attrs) { |
| 47 super(context, attrs); |
| 48 } |
| 49 |
| 50 /** |
| 51 * Set the SurfaceView being renderered to as soon as it is available. |
| 52 */ |
| 53 public void setContentViewRenderView(ContentViewRenderView contentViewRender
View) { |
| 54 FrameLayout contentViewHolder = (FrameLayout) findViewById(R.id.contentv
iew_holder); |
| 55 if (contentViewRenderView == null) { |
| 56 if (mContentViewRenderView != null) { |
| 57 contentViewHolder.removeView(mContentViewRenderView); |
| 58 } |
| 59 } else { |
| 60 contentViewHolder.addView(contentViewRenderView, |
| 61 new FrameLayout.LayoutParams( |
| 62 FrameLayout.LayoutParams.MATCH_PARENT, |
| 63 FrameLayout.LayoutParams.MATCH_PARENT)); |
| 64 } |
| 65 mContentViewRenderView = contentViewRenderView; |
| 66 } |
| 67 |
| 68 /** |
| 69 * @param window The owning window for this shell. |
| 70 */ |
| 71 public void setWindow(WindowAndroid window) { |
| 72 mWindow = window; |
| 73 } |
| 74 |
| 75 /** |
| 76 * Loads an URL. This will perform minimal amounts of sanitizing of the URL
to attempt to |
| 77 * make it valid. |
| 78 * |
| 79 * @param url The URL to be loaded by the shell. |
| 80 */ |
| 81 public void loadUrl(String url) { |
| 82 if (url == null) return; |
| 83 |
| 84 if (TextUtils.equals(url, mContentViewCore.getUrl())) { |
| 85 mContentViewCore.reload(true); |
| 86 } else { |
| 87 mContentViewCore.loadUrl(new LoadUrlParams(normalizeUrl(url))); |
| 88 } |
| 89 |
| 90 // TODO(aurimas): Remove this when crbug.com/174541 is fixed. |
| 91 mContentViewCore.getContainerView().clearFocus(); |
| 92 mContentViewCore.getContainerView().requestFocus(); |
| 93 } |
| 94 |
| 95 /** |
| 96 * Given a URI String, performs minimal normalization to attempt to build a
usable URL from it. |
| 97 * @param uriString The passed-in path to be normalized. |
| 98 * @return The normalized URL, as a string. |
| 99 */ |
| 100 private static String normalizeUrl(String uriString) { |
| 101 if (uriString == null) return uriString; |
| 102 Uri uri = Uri.parse(uriString); |
| 103 if (uri.getScheme() == null) { |
| 104 uri = Uri.parse("http://" + uriString); |
| 105 } |
| 106 return uri.toString(); |
| 107 } |
| 108 |
| 109 /** |
| 110 * Initializes the ContentView based on the native tab contents pointer pass
ed in. |
| 111 * @param nativeWebContents The pointer to the native tab contents object. |
| 112 */ |
| 113 @SuppressWarnings("unused") |
| 114 @CalledByNative |
| 115 private void initFromNativeWebContents(long nativeWebContents) { |
| 116 Context context = getContext(); |
| 117 mContentViewCore = new ContentViewCore(context); |
| 118 ContentView view = ContentView.newInstance(context, mContentViewCore); |
| 119 mContentViewCore.initialize(view, view, nativeWebContents, mWindow); |
| 120 |
| 121 if (getParent() != null) mContentViewCore.onShow(); |
| 122 ((FrameLayout) findViewById(R.id.contentview_holder)).addView(view, |
| 123 new FrameLayout.LayoutParams( |
| 124 FrameLayout.LayoutParams.MATCH_PARENT, |
| 125 FrameLayout.LayoutParams.MATCH_PARENT)); |
| 126 view.requestFocus(); |
| 127 mContentViewRenderView.setCurrentContentViewCore(mContentViewCore); |
| 128 |
| 129 mWebContentsObserver = new WebContentsObserverAndroid(mContentViewCore.g
etWebContents()) { |
| 130 @Override |
| 131 public void didStopLoading(String url) { |
| 132 Uri intentUri = Uri.parse(mContentViewCore |
| 133 .getOriginalUrlForActiveNavigationEntry()); |
| 134 Log.v(TAG, "Broadcast ACTION_PAGE_LOADED: scheme=" + intentUri.g
etScheme() |
| 135 + ", host=" + intentUri.getHost()); |
| 136 LocalBroadcastManager.getInstance(getContext()).sendBroadcast( |
| 137 new Intent(ACTION_PAGE_LOADED, intentUri)); |
| 138 } |
| 139 }; |
| 140 } |
| 141 |
| 142 /** |
| 143 * @return The {@link ViewGroup} currently shown by this Shell. |
| 144 */ |
| 145 public ViewGroup getContentView() { |
| 146 return mContentViewCore.getContainerView(); |
| 147 } |
| 148 |
| 149 /** |
| 150 * @return The {@link ContentViewCore} currently managing the view shown by
this Shell. |
| 151 */ |
| 152 public ContentViewCore getContentViewCore() { |
| 153 return mContentViewCore; |
| 154 } |
| 155 } |
OLD | NEW |