ContentView.java revision 3551c9c881056c480085172ff9840cab31610854
19f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project// Copyright (c) 2012 The Chromium Authors. All rights reserved.
29f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project// Use of this source code is governed by a BSD-style license that can be
39f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project// found in the LICENSE file.
49f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
59f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectpackage org.chromium.content.browser;
69f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
79f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.app.Activity;
89f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.content.Context;
99f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.content.res.Configuration;
109f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.graphics.Bitmap;
119f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.graphics.Canvas;
129f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.graphics.Rect;
139f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.os.Build;
149f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.util.AttributeSet;
159f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.view.KeyEvent;
169f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.view.MotionEvent;
179f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.view.View;
189f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.view.accessibility.AccessibilityEvent;
199f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.view.accessibility.AccessibilityNodeInfo;
209f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.view.inputmethod.EditorInfo;
219f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.view.inputmethod.InputConnection;
229f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport android.widget.FrameLayout;
239f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
249f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport com.google.common.annotations.VisibleForTesting;
259f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
269f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport org.chromium.content.common.TraceEvent;
279f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectimport org.chromium.ui.WindowAndroid;
289f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
299f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project/**
309f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * The containing view for {@link ContentViewCore} that exists in the Android UI hierarchy and
319f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * exposes the various {@link View} functionality to it.
329f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project *
339f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * TODO(joth): Remove any methods overrides from this class that were added for WebView
349f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project *             compatibility.
359f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project */
369f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectpublic class ContentView extends FrameLayout
379f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        implements ContentViewCore.InternalAccessDelegate, PageInfo {
389f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
399f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    private final ContentViewCore mContentViewCore;
409f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
419f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    private float mCurrentTouchOffsetX;
429f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    private float mCurrentTouchOffsetY;
439f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
449f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    /**
459f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * Creates an instance of a ContentView.
469f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @param context The Context the view is running in, through which it can
479f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     *                access the current theme, resources, etc.
489f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @param nativeWebContents A pointer to the native web contents.
499f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @param windowAndroid An instance of the WindowAndroid.
509f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @return A ContentView instance.
519f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     */
529f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    public static ContentView newInstance(Context context, int nativeWebContents,
539f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project            WindowAndroid windowAndroid) {
549f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        return newInstance(context, nativeWebContents, windowAndroid, null,
559f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project                android.R.attr.webViewStyle);
569f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    }
579f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
589f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    /**
599f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * Creates an instance of a ContentView.
609f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @param context The Context the view is running in, through which it can
619f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     *                access the current theme, resources, etc.
629f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @param nativeWebContents A pointer to the native web contents.
639f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @param windowAndroid An instance of the WindowAndroid.
649f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @param attrs The attributes of the XML tag that is inflating the view.
659f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @return A ContentView instance.
669f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     */
679f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    public static ContentView newInstance(Context context, int nativeWebContents,
689f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project            WindowAndroid windowAndroid, AttributeSet attrs) {
699f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        // TODO(klobag): use the WebViewStyle as the default style for now. It enables scrollbar.
709f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        // When ContentView is moved to framework, we can define its own style in the res.
719f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        return newInstance(context, nativeWebContents, windowAndroid, attrs,
729f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project                android.R.attr.webViewStyle);
739f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    }
749f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
759f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    /**
769f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * Creates an instance of a ContentView.
779f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @param context The Context the view is running in, through which it can
789f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     *                access the current theme, resources, etc.
799f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @param nativeWebContents A pointer to the native web contents.
809f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @param windowAndroid An instance of the WindowAndroid.
819f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @param attrs The attributes of the XML tag that is inflating the view.
829f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @param defStyle The default style to apply to this view.
839f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @return A ContentView instance.
849f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     */
859f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    public static ContentView newInstance(Context context, int nativeWebContents,
869f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project            WindowAndroid windowAndroid, AttributeSet attrs, int defStyle) {
879f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
889f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project            return new ContentView(context, nativeWebContents, windowAndroid, attrs, defStyle);
899f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        } else {
909f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project            return new JellyBeanContentView(context, nativeWebContents, windowAndroid, attrs,
919f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project                    defStyle);
929f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        }
939f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    }
949f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
959f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    protected ContentView(Context context, int nativeWebContents, WindowAndroid windowAndroid,
969f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project            AttributeSet attrs, int defStyle) {
979f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        super(context, attrs, defStyle);
989f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
999f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        if (getScrollBarStyle() == View.SCROLLBARS_INSIDE_OVERLAY) {
1009f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project            setHorizontalScrollBarEnabled(false);
1019f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project            setVerticalScrollBarEnabled(false);
1029f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        }
1039f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1049f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        setFocusable(true);
1059f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        setFocusableInTouchMode(true);
1069f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1079f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        mContentViewCore = new ContentViewCore(context);
1089f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        mContentViewCore.initialize(this, this, nativeWebContents, windowAndroid,
1099f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project                Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ?
1109f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project                ContentViewCore.INPUT_EVENTS_DELIVERED_AT_VSYNC :
1119f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project                ContentViewCore.INPUT_EVENTS_DELIVERED_IMMEDIATELY);
1129f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    }
1139f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1149f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    // PageInfo implementation.
1159f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1169f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    @Override
1179f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    public String getUrl() {
1189f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        return mContentViewCore.getUrl();
1199f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    }
1209f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1219f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    @Override
1229f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    public String getTitle() {
1239f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        return mContentViewCore.getTitle();
1249f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    }
1259f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1269f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    @Override
1279f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    public boolean isReadyForSnapshot() {
1289f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        return !isCrashed() && isReady();
1299f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    }
1309f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1319f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    @Override
1329f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    public Bitmap getBitmap() {
1339f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        return getBitmap(getWidth(), getHeight());
1349f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    }
1359f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1369f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    @Override
1379f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    public Bitmap getBitmap(int width, int height) {
1389f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        return mContentViewCore.getBitmap(width, height);
1399f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    }
1409f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1419f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    @Override
1429f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    public int getBackgroundColor() {
1439f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        return mContentViewCore.getBackgroundColor();
1449f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    }
1459f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1469f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    @Override
1479f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    public View getView() {
1489f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        return this;
1499f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    }
1509f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1519f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    /**
1529f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @return The core component of the ContentView that handles JNI communication.  Should only be
1539f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     *         used for passing to native.
1549f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     */
1559f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    public ContentViewCore getContentViewCore() {
1569f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        return mContentViewCore;
1579f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    }
1589f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1599f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    /**
1609f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * @return The cache of scales and positions used to convert coordinates from/to CSS.
1619f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     */
1629f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    public RenderCoordinates getRenderCoordinates() {
1639f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        return mContentViewCore.getRenderCoordinates();
1649f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    }
1659f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1669f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    /**
1679f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * Returns true if the given Activity has hardware acceleration enabled
1689f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * in its manifest, or in its foreground window.
1699f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     *
1709f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * TODO(husky): Remove when ContentViewCore.initialize() is refactored (see TODO there)
1719f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * TODO(dtrainor) This is still used by other classes.  Make sure to pull some version of this
1729f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * out before removing it.
1739f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     */
1749f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    public static boolean hasHardwareAcceleration(Activity activity) {
1759f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        return ContentViewCore.hasHardwareAcceleration(activity);
1769f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    }
1779f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1789f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    /**
1799f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * Destroy the internal state of the WebView. This method may only be called
1809f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * after the WebView has been removed from the view system. No other methods
1819f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     * may be called on this WebView after this method has been called.
1829f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project     */
1839f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    public void destroy() {
1849f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project        mContentViewCore.destroy();
185    }
186
187    /**
188     * Returns true initially, false after destroy() has been called.
189     * It is illegal to call any other public method after destroy().
190     */
191    public boolean isAlive() {
192        return mContentViewCore.isAlive();
193    }
194
195    /**
196     * For internal use. Throws IllegalStateException if mNativeContentView is 0.
197     * Use this to ensure we get a useful Java stack trace, rather than a native
198     * crash dump, from use-after-destroy bugs in Java code.
199     */
200    void checkIsAlive() throws IllegalStateException {
201        mContentViewCore.checkIsAlive();
202    }
203
204    public void setContentViewClient(ContentViewClient client) {
205        mContentViewCore.setContentViewClient(client);
206    }
207
208    @VisibleForTesting
209    public ContentViewClient getContentViewClient() {
210        return mContentViewCore.getContentViewClient();
211    }
212
213    /**
214     * Load url without fixing up the url string. Consumers of ContentView are responsible for
215     * ensuring the URL passed in is properly formatted (i.e. the scheme has been added if left
216     * off during user input).
217     *
218     * @param params Parameters for this load.
219     */
220    public void loadUrl(LoadUrlParams params) {
221        mContentViewCore.loadUrl(params);
222    }
223
224    /**
225     * Stops loading the current web contents.
226     */
227    public void stopLoading() {
228        mContentViewCore.stopLoading();
229    }
230
231    /**
232     * @return Whether the current WebContents has a previous navigation entry.
233     */
234    public boolean canGoBack() {
235        return mContentViewCore.canGoBack();
236    }
237
238    /**
239     * @return Whether the current WebContents has a navigation entry after the current one.
240     */
241    public boolean canGoForward() {
242        return mContentViewCore.canGoForward();
243    }
244
245    /**
246     * @param offset The offset into the navigation history.
247     * @return Whether we can move in history by given offset
248     */
249    public boolean canGoToOffset(int offset) {
250        return mContentViewCore.canGoToOffset(offset);
251    }
252
253    /**
254     * Navigates to the specified offset from the "current entry". Does nothing if the offset is out
255     * of bounds.
256     * @param offset The offset into the navigation history.
257     */
258    public void goToOffset(int offset) {
259        mContentViewCore.goToOffset(offset);
260    }
261
262    /**
263     * Goes to the navigation entry before the current one.
264     */
265    public void goBack() {
266        mContentViewCore.goBack();
267    }
268
269    /**
270     * Goes to the navigation entry following the current one.
271     */
272    public void goForward() {
273        mContentViewCore.goForward();
274    }
275
276    /**
277     * Reload the current page.
278     */
279    public void reload() {
280        mContentViewCore.reload();
281    }
282
283    /**
284     * Clears the WebView's page history in both the backwards and forwards
285     * directions.
286     */
287    public void clearHistory() {
288        mContentViewCore.clearHistory();
289    }
290
291    /**
292     * Start profiling the update speed. You must call {@link #stopFpsProfiling}
293     * to stop profiling.
294     */
295    @VisibleForTesting
296    public void startFpsProfiling() {
297        // TODO(nileshagrawal): Implement this.
298    }
299
300    /**
301     * Stop profiling the update speed.
302     */
303    @VisibleForTesting
304    public float stopFpsProfiling() {
305        // TODO(nileshagrawal): Implement this.
306        return 0.0f;
307    }
308
309    /**
310     * Fling the ContentView from the current position.
311     * @param x Fling touch starting position
312     * @param y Fling touch starting position
313     * @param velocityX Initial velocity of the fling (X) measured in pixels per second.
314     * @param velocityY Initial velocity of the fling (Y) measured in pixels per second.
315     */
316    @VisibleForTesting
317    public void fling(long timeMs, int x, int y, int velocityX, int velocityY) {
318        mContentViewCore.getContentViewGestureHandler().fling(timeMs, x, y, velocityX, velocityY);
319    }
320
321    /**
322     * Start pinch zoom. You must call {@link #pinchEnd} to stop.
323     */
324    @VisibleForTesting
325    public void pinchBegin(long timeMs, int x, int y) {
326        mContentViewCore.getContentViewGestureHandler().pinchBegin(timeMs, x, y);
327    }
328
329    /**
330     * Stop pinch zoom.
331     */
332    @VisibleForTesting
333    public void pinchEnd(long timeMs) {
334        mContentViewCore.getContentViewGestureHandler().pinchEnd(timeMs);
335    }
336
337    void setIgnoreSingleTap(boolean value) {
338        mContentViewCore.getContentViewGestureHandler().setIgnoreSingleTap(value);
339    }
340
341    /** @see ContentViewGestureHandler#setIgnoreRemainingTouchEvents */
342    public void setIgnoreRemainingTouchEvents() {
343        mContentViewCore.getContentViewGestureHandler().setIgnoreRemainingTouchEvents();
344    }
345
346    /**
347     * Modify the ContentView magnification level. The effect of calling this
348     * method is exactly as after "pinch zoom".
349     *
350     * @param timeMs The event time in milliseconds.
351     * @param delta The ratio of the new magnification level over the current
352     *            magnification level.
353     * @param anchorX The magnification anchor (X) in the current view
354     *            coordinate.
355     * @param anchorY The magnification anchor (Y) in the current view
356     *            coordinate.
357     */
358    @VisibleForTesting
359    public void pinchBy(long timeMs, int anchorX, int anchorY, float delta) {
360        mContentViewCore.getContentViewGestureHandler().pinchBy(timeMs, anchorX, anchorY, delta);
361    }
362
363    /**
364     * Injects the passed JavaScript code in the current page and evaluates it.
365     *
366     * @throws IllegalStateException If the ContentView has been destroyed.
367     */
368    public void evaluateJavaScript(String script) throws IllegalStateException {
369        mContentViewCore.evaluateJavaScript(script, null);
370    }
371
372    /**
373     * This method should be called when the containing activity is paused.
374     **/
375    public void onActivityPause() {
376        mContentViewCore.onActivityPause();
377    }
378
379    /**
380     * This method should be called when the containing activity is resumed.
381     **/
382    public void onActivityResume() {
383        mContentViewCore.onActivityResume();
384    }
385
386    /**
387     * To be called when the ContentView is shown.
388     **/
389    public void onShow() {
390        mContentViewCore.onShow();
391    }
392
393    /**
394     * To be called when the ContentView is hidden.
395     **/
396    public void onHide() {
397        mContentViewCore.onHide();
398    }
399
400    /**
401     * Return the ContentSettings object used to retrieve the settings for this
402     * ContentView.
403     * @return A ContentSettings object that can be used to retrieve this ContentView's
404     *         settings.
405     */
406    public ContentSettings getContentSettings() {
407        return mContentViewCore.getContentSettings();
408    }
409
410    /**
411     * Hides the select action bar.
412     */
413    public void hideSelectActionBar() {
414        mContentViewCore.hideSelectActionBar();
415    }
416
417    // FrameLayout overrides.
418
419    // Needed by ContentViewCore.InternalAccessDelegate
420    @Override
421    public boolean drawChild(Canvas canvas, View child, long drawingTime) {
422        return super.drawChild(canvas, child, drawingTime);
423    }
424
425    @Override
426    protected void onSizeChanged(int w, int h, int ow, int oh) {
427        TraceEvent.begin();
428        super.onSizeChanged(w, h, ow, oh);
429        mContentViewCore.onSizeChanged(w, h, ow, oh);
430        TraceEvent.end();
431    }
432
433    @Override
434    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
435        return mContentViewCore.onCreateInputConnection(outAttrs);
436    }
437
438    @Override
439    public boolean onCheckIsTextEditor() {
440        return mContentViewCore.onCheckIsTextEditor();
441    }
442
443    @Override
444    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
445        TraceEvent.begin();
446        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
447        mContentViewCore.onFocusChanged(gainFocus);
448        TraceEvent.end();
449    }
450
451    @Override
452    public boolean onKeyUp(int keyCode, KeyEvent event) {
453        return mContentViewCore.onKeyUp(keyCode, event);
454    }
455
456    @Override
457    public boolean dispatchKeyEventPreIme(KeyEvent event) {
458        return mContentViewCore.dispatchKeyEventPreIme(event);
459    }
460
461    @Override
462    public boolean dispatchKeyEvent(KeyEvent event) {
463        if (isFocused()) {
464            return mContentViewCore.dispatchKeyEvent(event);
465        } else {
466            return super.dispatchKeyEvent(event);
467        }
468    }
469
470    @Override
471    public boolean onTouchEvent(MotionEvent event) {
472        MotionEvent offset = createOffsetMotionEvent(event);
473        boolean consumed = mContentViewCore.onTouchEvent(offset);
474        offset.recycle();
475        return consumed;
476    }
477
478    /**
479     * Mouse move events are sent on hover enter, hover move and hover exit.
480     * They are sent on hover exit because sometimes it acts as both a hover
481     * move and hover exit.
482     */
483    @Override
484    public boolean onHoverEvent(MotionEvent event) {
485        MotionEvent offset = createOffsetMotionEvent(event);
486        boolean consumed = mContentViewCore.onHoverEvent(offset);
487        offset.recycle();
488        return consumed;
489    }
490
491    @Override
492    public boolean onGenericMotionEvent(MotionEvent event) {
493        return mContentViewCore.onGenericMotionEvent(event);
494    }
495
496    @Override
497    public boolean performLongClick() {
498        return false;
499    }
500
501    /**
502     * Sets the current amount to offset incoming touch events by.  This is used to handle content
503     * moving and not lining up properly with the android input system.
504     * @param dx The X offset in pixels to shift touch events.
505     * @param dy The Y offset in pixels to shift touch events.
506     */
507    public void setCurrentMotionEventOffsets(float dx, float dy) {
508        mCurrentTouchOffsetX = dx;
509        mCurrentTouchOffsetY = dy;
510    }
511
512    private MotionEvent createOffsetMotionEvent(MotionEvent src) {
513        MotionEvent dst = MotionEvent.obtain(src);
514        dst.offsetLocation(mCurrentTouchOffsetX, mCurrentTouchOffsetY);
515        return dst;
516    }
517
518    @Override
519    protected void onConfigurationChanged(Configuration newConfig) {
520        mContentViewCore.onConfigurationChanged(newConfig);
521    }
522
523    /**
524     * Currently the ContentView scrolling happens in the native side. In
525     * the Java view system, it is always pinned at (0, 0). scrollBy() and scrollTo()
526     * are overridden, so that View's mScrollX and mScrollY will be unchanged at
527     * (0, 0). This is critical for drawing ContentView correctly.
528     */
529    @Override
530    public void scrollBy(int x, int y) {
531        mContentViewCore.scrollBy(x, y);
532    }
533
534    @Override
535    public void scrollTo(int x, int y) {
536        mContentViewCore.scrollTo(x, y);
537    }
538
539    @Override
540    protected int computeHorizontalScrollExtent() {
541        // TODO (dtrainor): Need to expose scroll events properly to public. Either make getScroll*
542        // work or expose computeHorizontalScrollOffset()/computeVerticalScrollOffset as public.
543        return mContentViewCore.computeHorizontalScrollExtent();
544    }
545
546    @Override
547    protected int computeHorizontalScrollOffset() {
548        return mContentViewCore.computeHorizontalScrollOffset();
549    }
550
551    @Override
552    protected int computeHorizontalScrollRange() {
553        return mContentViewCore.computeHorizontalScrollRange();
554    }
555
556    @Override
557    protected int computeVerticalScrollExtent() {
558        return mContentViewCore.computeVerticalScrollExtent();
559    }
560
561    @Override
562    protected int computeVerticalScrollOffset() {
563        return mContentViewCore.computeVerticalScrollOffset();
564    }
565
566    @Override
567    protected int computeVerticalScrollRange() {
568        return mContentViewCore.computeVerticalScrollRange();
569    }
570
571    // End FrameLayout overrides.
572
573    @Override
574    public boolean awakenScrollBars(int startDelay, boolean invalidate) {
575        return mContentViewCore.awakenScrollBars(startDelay, invalidate);
576    }
577
578    @Override
579    public boolean awakenScrollBars() {
580        return super.awakenScrollBars();
581    }
582
583    public int getSingleTapX()  {
584        return mContentViewCore.getContentViewGestureHandler().getSingleTapX();
585    }
586
587    public int getSingleTapY()  {
588        return mContentViewCore.getContentViewGestureHandler().getSingleTapY();
589    }
590
591    @Override
592    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
593        super.onInitializeAccessibilityNodeInfo(info);
594        mContentViewCore.onInitializeAccessibilityNodeInfo(info);
595    }
596
597    /**
598     * Fills in scrolling values for AccessibilityEvents.
599     * @param event Event being fired.
600     */
601    @Override
602    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
603        super.onInitializeAccessibilityEvent(event);
604        mContentViewCore.onInitializeAccessibilityEvent(event);
605    }
606
607    @Override
608    protected void onAttachedToWindow() {
609        super.onAttachedToWindow();
610        mContentViewCore.onAttachedToWindow();
611    }
612
613    @Override
614    protected void onDetachedFromWindow() {
615        super.onDetachedFromWindow();
616        mContentViewCore.onDetachedFromWindow();
617    }
618
619    @Override
620    protected void onVisibilityChanged(View changedView, int visibility) {
621        super.onVisibilityChanged(changedView, visibility);
622        mContentViewCore.onVisibilityChanged(changedView, visibility);
623    }
624
625    /**
626     * Register the delegate to be used when content can not be handled by
627     * the rendering engine, and should be downloaded instead. This will replace
628     * the current delegate.
629     * @param delegate An implementation of ContentViewDownloadDelegate.
630     */
631    public void setDownloadDelegate(ContentViewDownloadDelegate delegate) {
632        mContentViewCore.setDownloadDelegate(delegate);
633    }
634
635    // Called by DownloadController.
636    ContentViewDownloadDelegate getDownloadDelegate() {
637        return mContentViewCore.getDownloadDelegate();
638    }
639
640    public boolean getUseDesktopUserAgent() {
641        return mContentViewCore.getUseDesktopUserAgent();
642    }
643
644    /**
645     * Set whether or not we're using a desktop user agent for the currently loaded page.
646     * @param override If true, use a desktop user agent.  Use a mobile one otherwise.
647     * @param reloadOnChange Reload the page if the UA has changed.
648     */
649    public void setUseDesktopUserAgent(boolean override, boolean reloadOnChange) {
650        mContentViewCore.setUseDesktopUserAgent(override, reloadOnChange);
651    }
652
653    /**
654     * @return Whether the native ContentView has crashed.
655     */
656    public boolean isCrashed() {
657        return mContentViewCore.isCrashed();
658    }
659
660    /**
661     * @return Whether a reload happens when this ContentView is activated.
662     */
663    public boolean needsReload() {
664        return mContentViewCore.needsReload();
665    }
666
667    /**
668     * Checks whether the WebView can be zoomed in.
669     *
670     * @return True if the WebView can be zoomed in.
671     */
672    // This method uses the term 'zoom' for legacy reasons, but relates
673    // to what chrome calls the 'page scale factor'.
674    public boolean canZoomIn() {
675        return mContentViewCore.canZoomIn();
676    }
677
678    /**
679     * Checks whether the WebView can be zoomed out.
680     *
681     * @return True if the WebView can be zoomed out.
682     */
683    // This method uses the term 'zoom' for legacy reasons, but relates
684    // to what chrome calls the 'page scale factor'.
685    public boolean canZoomOut() {
686        return mContentViewCore.canZoomOut();
687    }
688
689    /**
690     * Zooms in the WebView by 25% (or less if that would result in zooming in
691     * more than possible).
692     *
693     * @return True if there was a zoom change, false otherwise.
694     */
695    // This method uses the term 'zoom' for legacy reasons, but relates
696    // to what chrome calls the 'page scale factor'.
697    public boolean zoomIn() {
698        return mContentViewCore.zoomIn();
699    }
700
701    /**
702     * Zooms out the WebView by 20% (or less if that would result in zooming out
703     * more than possible).
704     *
705     * @return True if there was a zoom change, false otherwise.
706     */
707    // This method uses the term 'zoom' for legacy reasons, but relates
708    // to what chrome calls the 'page scale factor'.
709    public boolean zoomOut() {
710        return mContentViewCore.zoomOut();
711    }
712
713    /**
714     * Resets the zoom factor of the WebView.
715     *
716     * @return True if there was a zoom change, false otherwise.
717     */
718    // This method uses the term 'zoom' for legacy reasons, but relates
719    // to what chrome calls the 'page scale factor'.
720    public boolean zoomReset() {
721        return mContentViewCore.zoomReset();
722    }
723
724    /**
725     * Return the current scale of the WebView
726     * @return The current scale.
727     */
728    public float getScale() {
729        return mContentViewCore.getScale();
730    }
731
732    /**
733     * If the view is ready to draw contents to the screen. In hardware mode,
734     * the initialization of the surface texture may not occur until after the
735     * view has been added to the layout. This method will return {@code true}
736     * once the texture is actually ready.
737     */
738    public boolean isReady() {
739        return mContentViewCore.isReady();
740    }
741
742    /**
743     * Returns whether or not accessibility injection is being used.
744     */
745    public boolean isInjectingAccessibilityScript() {
746        return mContentViewCore.isInjectingAccessibilityScript();
747    }
748
749    /**
750     * Enable or disable accessibility features.
751     */
752    public void setAccessibilityState(boolean state) {
753        mContentViewCore.setAccessibilityState(state);
754    }
755
756    /**
757     * Stop any TTS notifications that are currently going on.
758     */
759    public void stopCurrentAccessibilityNotifications() {
760        mContentViewCore.stopCurrentAccessibilityNotifications();
761    }
762
763    /**
764     * Inform WebKit that Fullscreen mode has been exited by the user.
765     */
766    public void exitFullscreen() {
767        mContentViewCore.exitFullscreen();
768    }
769
770    /**
771     * Return content scroll y.
772     *
773     * @return The vertical scroll position in pixels.
774     */
775    public int getContentScrollY() {
776        return mContentViewCore.computeVerticalScrollOffset();
777    }
778
779    /**
780     * Return content height.
781     *
782     * @return The height of the content in pixels.
783     */
784    public int getContentHeight() {
785        return mContentViewCore.computeVerticalScrollRange();
786    }
787
788    ///////////////////////////////////////////////////////////////////////////////////////////////
789    //              Start Implementation of ContentViewCore.InternalAccessDelegate               //
790    ///////////////////////////////////////////////////////////////////////////////////////////////
791
792    @Override
793    public boolean super_onKeyUp(int keyCode, KeyEvent event) {
794        return super.onKeyUp(keyCode, event);
795    }
796
797    @Override
798    public boolean super_dispatchKeyEventPreIme(KeyEvent event) {
799        return super.dispatchKeyEventPreIme(event);
800    }
801
802    @Override
803    public boolean super_dispatchKeyEvent(KeyEvent event) {
804        return super.dispatchKeyEvent(event);
805    }
806
807    @Override
808    public boolean super_onGenericMotionEvent(MotionEvent event) {
809        return super.onGenericMotionEvent(event);
810    }
811
812    @Override
813    public void super_onConfigurationChanged(Configuration newConfig) {
814        super.onConfigurationChanged(newConfig);
815    }
816
817    @Override
818    public boolean super_awakenScrollBars(int startDelay, boolean invalidate) {
819        return super.awakenScrollBars(startDelay, invalidate);
820    }
821
822    ///////////////////////////////////////////////////////////////////////////////////////////////
823    //                End Implementation of ContentViewCore.InternalAccessDelegate               //
824    ///////////////////////////////////////////////////////////////////////////////////////////////
825}
826