Chromium Code Reviews| Index: chromecast/shell/android/apk/src/org/chromium/chromecast/shell/CastWindowAndroid.java |
| diff --git a/chromecast/shell/android/apk/src/org/chromium/chromecast/shell/CastWindowAndroid.java b/chromecast/shell/android/apk/src/org/chromium/chromecast/shell/CastWindowAndroid.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a9605b3b7c2234e7b16d434614cc0b7c36437d8b |
| --- /dev/null |
| +++ b/chromecast/shell/android/apk/src/org/chromium/chromecast/shell/CastWindowAndroid.java |
| @@ -0,0 +1,151 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chromecast.shell; |
| + |
| +import android.content.Context; |
| +import android.content.Intent; |
| +import android.net.Uri; |
| +import android.support.v4.content.LocalBroadcastManager; |
| +import android.text.TextUtils; |
| +import android.util.AttributeSet; |
| +import android.view.ViewGroup; |
| +import android.widget.FrameLayout; |
| +import android.widget.LinearLayout; |
| + |
| +import org.chromium.base.CalledByNative; |
| +import org.chromium.base.JNINamespace; |
| +import org.chromium.content.browser.ContentView; |
| +import org.chromium.content.browser.ContentViewCore; |
| +import org.chromium.content.browser.ContentViewRenderView; |
| +import org.chromium.content.browser.LoadUrlParams; |
| +import org.chromium.content.browser.WebContentsObserverAndroid; |
| +import org.chromium.ui.base.WindowAndroid; |
| + |
| +/** |
| + * Container for the various UI components that make up a shell window. |
| + */ |
| +@JNINamespace("chromecast::shell") |
| +public class CastWindowAndroid extends LinearLayout { |
| + public static final String TAG = "CastWindowAndroid"; |
| + |
| + public static final String ACTION_PAGE_LOADED = "castPageLoaded"; |
| + public static final String ACTION_ENABLE_DEV_TOOLS = "castEnableDevTools"; |
| + public static final String ACTION_DISABLE_DEV_TOOLS = "castDisableDevTools"; |
| + |
| + private ContentViewCore mContentViewCore; |
| + private ContentViewRenderView mContentViewRenderView; |
| + private WebContentsObserverAndroid mWebContentsObserver; |
| + private WindowAndroid mWindow; |
| + |
| + /** |
| + * Constructor for inflating via XML. |
| + */ |
| + public CastWindowAndroid(Context context, AttributeSet attrs) { |
| + super(context, attrs); |
| + } |
| + |
| + /** |
| + * Set the SurfaceView being renderered to as soon as it is available. |
| + */ |
| + public void setContentViewRenderView(ContentViewRenderView contentViewRenderView) { |
| + FrameLayout contentViewHolder = (FrameLayout) findViewById(R.id.contentview_holder); |
| + if (contentViewRenderView == null) { |
| + if (mContentViewRenderView != null) { |
| + contentViewHolder.removeView(mContentViewRenderView); |
| + } |
| + } else { |
| + contentViewHolder.addView(contentViewRenderView, |
| + new FrameLayout.LayoutParams( |
| + FrameLayout.LayoutParams.MATCH_PARENT, |
| + FrameLayout.LayoutParams.MATCH_PARENT)); |
| + } |
| + mContentViewRenderView = contentViewRenderView; |
| + } |
| + |
| + /** |
| + * @param window The owning window for this shell. |
| + */ |
| + public void setWindow(WindowAndroid window) { |
| + mWindow = window; |
| + } |
| + |
| + /** |
| + * Loads an URL. This will perform minimal amounts of sanitizing of the URL to attempt to |
| + * make it valid. |
| + * |
| + * @param url The URL to be loaded by the shell. |
| + */ |
| + public void loadUrl(String url) { |
| + if (url == null) return; |
| + |
| + if (TextUtils.equals(url, mContentViewCore.getUrl())) { |
| + mContentViewCore.reload(true); |
| + } else { |
| + mContentViewCore.loadUrl(new LoadUrlParams(sanitizeUrl(url))); |
| + } |
| + |
| + // TODO(aurimas): Remove this when crbug.com/174541 is fixed. |
| + mContentViewCore.getContainerView().clearFocus(); |
| + mContentViewCore.getContainerView().requestFocus(); |
| + } |
| + |
| + /** |
| + * Given an URL, this performs minimal sanitizing to ensure it will be valid. |
| + * @param url The url to be sanitized. |
| + * @return The sanitized URL. |
| + */ |
| + public static String sanitizeUrl(String url) { |
|
Yaron
2014/08/20 18:11:02
This looks to be copied from ContentShell. I think
gunsch
2014/08/22 22:44:30
Done.
|
| + if (url == null) return url; |
| + if (url.startsWith("www.") || url.indexOf(":") == -1) url = "http://" + url; |
| + return url; |
| + } |
| + |
| + /** |
| + * Initializes the ContentView based on the native tab contents pointer passed in. |
| + * @param nativeWebContents The pointer to the native tab contents object. |
| + */ |
| + @SuppressWarnings("unused") |
| + @CalledByNative |
| + private void initFromNativeTabContents(long nativeWebContents) { |
| + Context context = getContext(); |
| + mContentViewCore = new ContentViewCore(context); |
| + ContentView view = ContentView.newInstance(context, mContentViewCore); |
| + mContentViewCore.initialize(view, view, nativeWebContents, mWindow); |
| + |
| + if (getParent() != null) mContentViewCore.onShow(); |
| + ((FrameLayout) findViewById(R.id.contentview_holder)).addView(view, |
| + new FrameLayout.LayoutParams( |
| + FrameLayout.LayoutParams.MATCH_PARENT, |
| + FrameLayout.LayoutParams.MATCH_PARENT)); |
| + view.requestFocus(); |
| + mContentViewRenderView.setCurrentContentViewCore(mContentViewCore); |
| + |
| + mWebContentsObserver = new WebContentsObserverAndroid(mContentViewCore.getWebContents()) { |
| + @Override |
| + public void didStopLoading(String url) { |
| + Uri intentUri = Uri.parse(mContentViewCore |
| + .getOriginalUrlForActiveNavigationEntry()); |
| + Log.v(TAG, "Broadcast ACTION_PAGE_LOADED: scheme=" + intentUri.getScheme() |
| + + ", host=" + intentUri.getHost()); |
| + LocalBroadcastManager.getInstance(getContext()).sendBroadcast( |
| + new Intent(ACTION_PAGE_LOADED, intentUri)); |
| + } |
| + }; |
| + } |
| + |
| + /** |
| + * @return The {@link ViewGroup} currently shown by this Shell. |
| + */ |
| + public ViewGroup getContentView() { |
| + return mContentViewCore.getContainerView(); |
| + } |
| + |
| + /** |
| + * @return The {@link ContentViewCore} currently managing the view shown by this Shell. |
| + */ |
| + public ContentViewCore getContentViewCore() { |
| + return mContentViewCore; |
| + } |
| +} |