Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / public / android / java / src / org / chromium / content / browser / ContentView.java
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.
4
5 package org.chromium.content.browser;
6
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;
20
21 import org.chromium.base.TraceEvent;
22 import org.chromium.ui.base.WindowAndroid;
23
24 /**
25  * The containing view for {@link ContentViewCore} that exists in the Android UI hierarchy and
26  * exposes the various {@link View} functionality to it.
27  *
28  * TODO(joth): Remove any methods overrides from this class that were added for WebView
29  *             compatibility.
30  */
31 public class ContentView extends FrameLayout
32         implements ContentViewCore.InternalAccessDelegate {
33
34     private final ContentViewCore mContentViewCore;
35
36     private final int[] mLocationInWindow = new int[2];
37
38     /**
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.
45      */
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);
50         } else {
51             return new JellyBeanContentView(context, nativeWebContents, windowAndroid);
52         }
53     }
54
55     protected ContentView(Context context, long nativeWebContents, WindowAndroid windowAndroid) {
56         super(context, null, android.R.attr.webViewStyle);
57
58         if (getScrollBarStyle() == View.SCROLLBARS_INSIDE_OVERLAY) {
59             setHorizontalScrollBarEnabled(false);
60             setVerticalScrollBarEnabled(false);
61         }
62
63         setFocusable(true);
64         setFocusableInTouchMode(true);
65
66         mContentViewCore = new ContentViewCore(context);
67         mContentViewCore.initialize(this, this, nativeWebContents, windowAndroid);
68     }
69
70     /**
71      * @return The core component of the ContentView that handles JNI communication.  Should only be
72      *         used for passing to native.
73      */
74     public ContentViewCore getContentViewCore() {
75         return mContentViewCore;
76     }
77
78     // FrameLayout overrides.
79
80     // Needed by ContentViewCore.InternalAccessDelegate
81     @Override
82     public boolean drawChild(Canvas canvas, View child, long drawingTime) {
83         return super.drawChild(canvas, child, drawingTime);
84     }
85
86     // Needed by ContentViewCore.InternalAccessDelegate
87     @Override
88     public void onScrollChanged(int l, int t, int oldl, int oldt) {
89         super.onScrollChanged(l, t, oldl, oldt);
90     }
91
92     @Override
93     protected void onSizeChanged(int w, int h, int ow, int oh) {
94         TraceEvent.begin();
95         super.onSizeChanged(w, h, ow, oh);
96         mContentViewCore.onSizeChanged(w, h, ow, oh);
97         TraceEvent.end();
98     }
99
100     @Override
101     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
102         super.onLayout(changed, left, top, right, bottom);
103         if (changed) {
104             getLocationInWindow(mLocationInWindow);
105             mContentViewCore.onLocationInWindowChanged(mLocationInWindow[0], mLocationInWindow[1]);
106         }
107     }
108
109     @Override
110     public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
111         return mContentViewCore.onCreateInputConnection(outAttrs);
112     }
113
114     @Override
115     public boolean onCheckIsTextEditor() {
116         return mContentViewCore.onCheckIsTextEditor();
117     }
118
119     @Override
120     protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
121         TraceEvent.begin();
122         super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
123         mContentViewCore.onFocusChanged(gainFocus);
124         TraceEvent.end();
125     }
126
127     @Override
128     public void onWindowFocusChanged(boolean hasWindowFocus) {
129         super.onWindowFocusChanged(hasWindowFocus);
130         mContentViewCore.onWindowFocusChanged(hasWindowFocus);
131     }
132
133     @Override
134     public boolean onKeyUp(int keyCode, KeyEvent event) {
135         return mContentViewCore.onKeyUp(keyCode, event);
136     }
137
138     @Override
139     public boolean dispatchKeyEventPreIme(KeyEvent event) {
140         return mContentViewCore.dispatchKeyEventPreIme(event);
141     }
142
143     @Override
144     public boolean dispatchKeyEvent(KeyEvent event) {
145         if (isFocused()) {
146             return mContentViewCore.dispatchKeyEvent(event);
147         } else {
148             return super.dispatchKeyEvent(event);
149         }
150     }
151
152     @Override
153     public boolean onTouchEvent(MotionEvent event) {
154         return mContentViewCore.onTouchEvent(event);
155     }
156
157     /**
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.
161      */
162     @Override
163     public boolean onHoverEvent(MotionEvent event) {
164         boolean consumed = mContentViewCore.onHoverEvent(event);
165         if (!mContentViewCore.isTouchExplorationEnabled()) super.onHoverEvent(event);
166         return consumed;
167     }
168
169     @Override
170     public boolean onGenericMotionEvent(MotionEvent event) {
171         return mContentViewCore.onGenericMotionEvent(event);
172     }
173
174     @Override
175     public boolean performLongClick() {
176         return false;
177     }
178
179     @Override
180     protected void onConfigurationChanged(Configuration newConfig) {
181         mContentViewCore.onConfigurationChanged(newConfig);
182     }
183
184     /**
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.
189      */
190     @Override
191     public void scrollBy(int x, int y) {
192         mContentViewCore.scrollBy(x, y);
193     }
194
195     @Override
196     public void scrollTo(int x, int y) {
197         mContentViewCore.scrollTo(x, y);
198     }
199
200     @Override
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();
205     }
206
207     @Override
208     protected int computeHorizontalScrollOffset() {
209         return mContentViewCore.computeHorizontalScrollOffset();
210     }
211
212     @Override
213     protected int computeHorizontalScrollRange() {
214         return mContentViewCore.computeHorizontalScrollRange();
215     }
216
217     @Override
218     protected int computeVerticalScrollExtent() {
219         return mContentViewCore.computeVerticalScrollExtent();
220     }
221
222     @Override
223     protected int computeVerticalScrollOffset() {
224         return mContentViewCore.computeVerticalScrollOffset();
225     }
226
227     @Override
228     protected int computeVerticalScrollRange() {
229         return mContentViewCore.computeVerticalScrollRange();
230     }
231
232     // End FrameLayout overrides.
233
234     @Override
235     public boolean awakenScrollBars(int startDelay, boolean invalidate) {
236         return mContentViewCore.awakenScrollBars(startDelay, invalidate);
237     }
238
239     @Override
240     public boolean awakenScrollBars() {
241         return super.awakenScrollBars();
242     }
243
244     @Override
245     public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
246         super.onInitializeAccessibilityNodeInfo(info);
247         mContentViewCore.onInitializeAccessibilityNodeInfo(info);
248     }
249
250     /**
251      * Fills in scrolling values for AccessibilityEvents.
252      * @param event Event being fired.
253      */
254     @Override
255     public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
256         super.onInitializeAccessibilityEvent(event);
257         mContentViewCore.onInitializeAccessibilityEvent(event);
258     }
259
260     @Override
261     protected void onAttachedToWindow() {
262         super.onAttachedToWindow();
263         mContentViewCore.onAttachedToWindow();
264     }
265
266     @Override
267     protected void onDetachedFromWindow() {
268         super.onDetachedFromWindow();
269         mContentViewCore.onDetachedFromWindow();
270     }
271
272     @Override
273     protected void onVisibilityChanged(View changedView, int visibility) {
274         super.onVisibilityChanged(changedView, visibility);
275         mContentViewCore.onVisibilityChanged(changedView, visibility);
276     }
277
278     /**
279      * Return content scroll y.
280      *
281      * @return The vertical scroll position in pixels.
282      */
283     public int getContentScrollY() {
284         return mContentViewCore.computeVerticalScrollOffset();
285     }
286
287     /**
288      * Return content height.
289      *
290      * @return The height of the content in pixels.
291      */
292     public int getContentHeight() {
293         return mContentViewCore.computeVerticalScrollRange();
294     }
295
296     ///////////////////////////////////////////////////////////////////////////////////////////////
297     //              Start Implementation of ContentViewCore.InternalAccessDelegate               //
298     ///////////////////////////////////////////////////////////////////////////////////////////////
299
300     @Override
301     public boolean super_onKeyUp(int keyCode, KeyEvent event) {
302         return super.onKeyUp(keyCode, event);
303     }
304
305     @Override
306     public boolean super_dispatchKeyEventPreIme(KeyEvent event) {
307         return super.dispatchKeyEventPreIme(event);
308     }
309
310     @Override
311     public boolean super_dispatchKeyEvent(KeyEvent event) {
312         return super.dispatchKeyEvent(event);
313     }
314
315     @Override
316     public boolean super_onGenericMotionEvent(MotionEvent event) {
317         return super.onGenericMotionEvent(event);
318     }
319
320     @Override
321     public void super_onConfigurationChanged(Configuration newConfig) {
322         super.onConfigurationChanged(newConfig);
323     }
324
325     @Override
326     public boolean super_awakenScrollBars(int startDelay, boolean invalidate) {
327         return super.awakenScrollBars(startDelay, invalidate);
328     }
329
330     ///////////////////////////////////////////////////////////////////////////////////////////////
331     //                End Implementation of ContentViewCore.InternalAccessDelegate               //
332     ///////////////////////////////////////////////////////////////////////////////////////////////
333 }