1 // Copyright 2012 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.
5 package org.chromium.content.browser;
7 import android.content.Context;
8 import android.content.res.Configuration;
9 import android.graphics.Canvas;
10 import android.graphics.Rect;
11 import android.os.Build;
12 import android.view.KeyEvent;
13 import android.view.MotionEvent;
14 import android.view.View;
15 import android.view.accessibility.AccessibilityEvent;
16 import android.view.accessibility.AccessibilityNodeInfo;
17 import android.view.inputmethod.EditorInfo;
18 import android.view.inputmethod.InputConnection;
19 import android.widget.FrameLayout;
21 import org.chromium.base.TraceEvent;
22 import org.chromium.ui.base.WindowAndroid;
25 * The containing view for {@link ContentViewCore} that exists in the Android UI hierarchy and
26 * exposes the various {@link View} functionality to it.
28 * TODO(joth): Remove any methods overrides from this class that were added for WebView
31 public class ContentView extends FrameLayout
32 implements ContentViewCore.InternalAccessDelegate {
34 private final ContentViewCore mContentViewCore;
36 private final int[] mLocationInWindow = new int[2];
39 * Creates an instance of a ContentView.
40 * @param context The Context the view is running in, through which it can
41 * access the current theme, resources, etc.
42 * @param nativeWebContents A pointer to the native web contents.
43 * @param windowAndroid An instance of the WindowAndroid.
44 * @return A ContentView instance.
46 public static ContentView newInstance(
47 Context context, long nativeWebContents, WindowAndroid windowAndroid) {
48 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
49 return new ContentView(context, nativeWebContents, windowAndroid);
51 return new JellyBeanContentView(context, nativeWebContents, windowAndroid);
55 protected ContentView(Context context, long nativeWebContents, WindowAndroid windowAndroid) {
56 super(context, null, android.R.attr.webViewStyle);
58 if (getScrollBarStyle() == View.SCROLLBARS_INSIDE_OVERLAY) {
59 setHorizontalScrollBarEnabled(false);
60 setVerticalScrollBarEnabled(false);
64 setFocusableInTouchMode(true);
66 mContentViewCore = new ContentViewCore(context);
67 mContentViewCore.initialize(this, this, nativeWebContents, windowAndroid);
71 * @return The core component of the ContentView that handles JNI communication. Should only be
72 * used for passing to native.
74 public ContentViewCore getContentViewCore() {
75 return mContentViewCore;
78 // FrameLayout overrides.
80 // Needed by ContentViewCore.InternalAccessDelegate
82 public boolean drawChild(Canvas canvas, View child, long drawingTime) {
83 return super.drawChild(canvas, child, drawingTime);
86 // Needed by ContentViewCore.InternalAccessDelegate
88 public void onScrollChanged(int l, int t, int oldl, int oldt) {
89 super.onScrollChanged(l, t, oldl, oldt);
93 protected void onSizeChanged(int w, int h, int ow, int oh) {
95 super.onSizeChanged(w, h, ow, oh);
96 mContentViewCore.onSizeChanged(w, h, ow, oh);
101 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
102 super.onLayout(changed, left, top, right, bottom);
104 getLocationInWindow(mLocationInWindow);
105 mContentViewCore.onLocationInWindowChanged(mLocationInWindow[0], mLocationInWindow[1]);
110 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
111 return mContentViewCore.onCreateInputConnection(outAttrs);
115 public boolean onCheckIsTextEditor() {
116 return mContentViewCore.onCheckIsTextEditor();
120 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
122 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
123 mContentViewCore.onFocusChanged(gainFocus);
128 public void onWindowFocusChanged(boolean hasWindowFocus) {
129 super.onWindowFocusChanged(hasWindowFocus);
130 mContentViewCore.onWindowFocusChanged(hasWindowFocus);
134 public boolean onKeyUp(int keyCode, KeyEvent event) {
135 return mContentViewCore.onKeyUp(keyCode, event);
139 public boolean dispatchKeyEventPreIme(KeyEvent event) {
140 return mContentViewCore.dispatchKeyEventPreIme(event);
144 public boolean dispatchKeyEvent(KeyEvent event) {
146 return mContentViewCore.dispatchKeyEvent(event);
148 return super.dispatchKeyEvent(event);
153 public boolean onTouchEvent(MotionEvent event) {
154 return mContentViewCore.onTouchEvent(event);
158 * Mouse move events are sent on hover enter, hover move and hover exit.
159 * They are sent on hover exit because sometimes it acts as both a hover
160 * move and hover exit.
163 public boolean onHoverEvent(MotionEvent event) {
164 boolean consumed = mContentViewCore.onHoverEvent(event);
165 if (!mContentViewCore.isTouchExplorationEnabled()) super.onHoverEvent(event);
170 public boolean onGenericMotionEvent(MotionEvent event) {
171 return mContentViewCore.onGenericMotionEvent(event);
175 public boolean performLongClick() {
180 protected void onConfigurationChanged(Configuration newConfig) {
181 mContentViewCore.onConfigurationChanged(newConfig);
185 * Currently the ContentView scrolling happens in the native side. In
186 * the Java view system, it is always pinned at (0, 0). scrollBy() and scrollTo()
187 * are overridden, so that View's mScrollX and mScrollY will be unchanged at
188 * (0, 0). This is critical for drawing ContentView correctly.
191 public void scrollBy(int x, int y) {
192 mContentViewCore.scrollBy(x, y);
196 public void scrollTo(int x, int y) {
197 mContentViewCore.scrollTo(x, y);
201 protected int computeHorizontalScrollExtent() {
202 // TODO(dtrainor): Need to expose scroll events properly to public. Either make getScroll*
203 // work or expose computeHorizontalScrollOffset()/computeVerticalScrollOffset as public.
204 return mContentViewCore.computeHorizontalScrollExtent();
208 protected int computeHorizontalScrollOffset() {
209 return mContentViewCore.computeHorizontalScrollOffset();
213 protected int computeHorizontalScrollRange() {
214 return mContentViewCore.computeHorizontalScrollRange();
218 protected int computeVerticalScrollExtent() {
219 return mContentViewCore.computeVerticalScrollExtent();
223 protected int computeVerticalScrollOffset() {
224 return mContentViewCore.computeVerticalScrollOffset();
228 protected int computeVerticalScrollRange() {
229 return mContentViewCore.computeVerticalScrollRange();
232 // End FrameLayout overrides.
235 public boolean awakenScrollBars(int startDelay, boolean invalidate) {
236 return mContentViewCore.awakenScrollBars(startDelay, invalidate);
240 public boolean awakenScrollBars() {
241 return super.awakenScrollBars();
245 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
246 super.onInitializeAccessibilityNodeInfo(info);
247 mContentViewCore.onInitializeAccessibilityNodeInfo(info);
251 * Fills in scrolling values for AccessibilityEvents.
252 * @param event Event being fired.
255 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
256 super.onInitializeAccessibilityEvent(event);
257 mContentViewCore.onInitializeAccessibilityEvent(event);
261 protected void onAttachedToWindow() {
262 super.onAttachedToWindow();
263 mContentViewCore.onAttachedToWindow();
267 protected void onDetachedFromWindow() {
268 super.onDetachedFromWindow();
269 mContentViewCore.onDetachedFromWindow();
273 protected void onVisibilityChanged(View changedView, int visibility) {
274 super.onVisibilityChanged(changedView, visibility);
275 mContentViewCore.onVisibilityChanged(changedView, visibility);
279 * Return content scroll y.
281 * @return The vertical scroll position in pixels.
283 public int getContentScrollY() {
284 return mContentViewCore.computeVerticalScrollOffset();
288 * Return content height.
290 * @return The height of the content in pixels.
292 public int getContentHeight() {
293 return mContentViewCore.computeVerticalScrollRange();
296 ///////////////////////////////////////////////////////////////////////////////////////////////
297 // Start Implementation of ContentViewCore.InternalAccessDelegate //
298 ///////////////////////////////////////////////////////////////////////////////////////////////
301 public boolean super_onKeyUp(int keyCode, KeyEvent event) {
302 return super.onKeyUp(keyCode, event);
306 public boolean super_dispatchKeyEventPreIme(KeyEvent event) {
307 return super.dispatchKeyEventPreIme(event);
311 public boolean super_dispatchKeyEvent(KeyEvent event) {
312 return super.dispatchKeyEvent(event);
316 public boolean super_onGenericMotionEvent(MotionEvent event) {
317 return super.onGenericMotionEvent(event);
321 public void super_onConfigurationChanged(Configuration newConfig) {
322 super.onConfigurationChanged(newConfig);
326 public boolean super_awakenScrollBars(int startDelay, boolean invalidate) {
327 return super.awakenScrollBars(startDelay, invalidate);
330 ///////////////////////////////////////////////////////////////////////////////////////////////
331 // End Implementation of ContentViewCore.InternalAccessDelegate //
332 ///////////////////////////////////////////////////////////////////////////////////////////////