ContentView.java revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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
5package org.chromium.content.browser;
6
7import android.content.Context;
8import android.content.res.Configuration;
9import android.graphics.Canvas;
10import android.graphics.Rect;
11import android.os.Build;
12import android.view.KeyEvent;
13import android.view.MotionEvent;
14import android.view.View;
15import android.view.accessibility.AccessibilityEvent;
16import android.view.accessibility.AccessibilityNodeInfo;
17import android.view.inputmethod.EditorInfo;
18import android.view.inputmethod.InputConnection;
19import android.widget.FrameLayout;
20
21import org.chromium.base.TraceEvent;
22
23/**
24 * The containing view for {@link ContentViewCore} that exists in the Android UI hierarchy and
25 * exposes the various {@link View} functionality to it.
26 */
27public class ContentView extends FrameLayout
28        implements ContentViewCore.InternalAccessDelegate {
29
30    protected final ContentViewCore mContentViewCore;
31
32    private final int[] mLocationInWindow = new int[2];
33
34    /**
35     * Creates an instance of a ContentView.
36     * @param context The Context the view is running in, through which it can
37     *                access the current theme, resources, etc.
38     * @param cvc A pointer to the content view core managing this content view.
39     * @return A ContentView instance.
40     */
41    public static ContentView newInstance(Context context, ContentViewCore cvc) {
42        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
43            return new ContentView(context, cvc);
44        } else {
45            return new JellyBeanContentView(context, cvc);
46        }
47    }
48
49    protected ContentView(Context context, ContentViewCore cvc) {
50        super(context, null, android.R.attr.webViewStyle);
51
52        if (getScrollBarStyle() == View.SCROLLBARS_INSIDE_OVERLAY) {
53            setHorizontalScrollBarEnabled(false);
54            setVerticalScrollBarEnabled(false);
55        }
56
57        setFocusable(true);
58        setFocusableInTouchMode(true);
59
60        mContentViewCore = cvc;
61    }
62
63    // Needed by ContentViewCore.InternalAccessDelegate
64    @Override
65    public boolean drawChild(Canvas canvas, View child, long drawingTime) {
66        return super.drawChild(canvas, child, drawingTime);
67    }
68
69    // Needed by ContentViewCore.InternalAccessDelegate
70    @Override
71    public void onScrollChanged(int l, int t, int oldl, int oldt) {
72        super.onScrollChanged(l, t, oldl, oldt);
73    }
74
75    @Override
76    protected void onSizeChanged(int w, int h, int ow, int oh) {
77        TraceEvent.begin();
78        super.onSizeChanged(w, h, ow, oh);
79        mContentViewCore.onSizeChanged(w, h, ow, oh);
80        TraceEvent.end();
81    }
82
83    @Override
84    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
85        super.onLayout(changed, left, top, right, bottom);
86        if (changed) {
87            getLocationInWindow(mLocationInWindow);
88            mContentViewCore.onLocationInWindowChanged(mLocationInWindow[0], mLocationInWindow[1]);
89        }
90    }
91
92    @Override
93    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
94        return mContentViewCore.onCreateInputConnection(outAttrs);
95    }
96
97    @Override
98    public boolean onCheckIsTextEditor() {
99        return mContentViewCore.onCheckIsTextEditor();
100    }
101
102    @Override
103    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
104        TraceEvent.begin();
105        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
106        mContentViewCore.onFocusChanged(gainFocus);
107        TraceEvent.end();
108    }
109
110    @Override
111    public void onWindowFocusChanged(boolean hasWindowFocus) {
112        super.onWindowFocusChanged(hasWindowFocus);
113        mContentViewCore.onWindowFocusChanged(hasWindowFocus);
114    }
115
116    @Override
117    public boolean onKeyUp(int keyCode, KeyEvent event) {
118        return mContentViewCore.onKeyUp(keyCode, event);
119    }
120
121    @Override
122    public boolean dispatchKeyEventPreIme(KeyEvent event) {
123        return mContentViewCore.dispatchKeyEventPreIme(event);
124    }
125
126    @Override
127    public boolean dispatchKeyEvent(KeyEvent event) {
128        if (isFocused()) {
129            return mContentViewCore.dispatchKeyEvent(event);
130        } else {
131            return super.dispatchKeyEvent(event);
132        }
133    }
134
135    @Override
136    public boolean onTouchEvent(MotionEvent event) {
137        return mContentViewCore.onTouchEvent(event);
138    }
139
140    /**
141     * Mouse move events are sent on hover enter, hover move and hover exit.
142     * They are sent on hover exit because sometimes it acts as both a hover
143     * move and hover exit.
144     */
145    @Override
146    public boolean onHoverEvent(MotionEvent event) {
147        boolean consumed = mContentViewCore.onHoverEvent(event);
148        if (!mContentViewCore.isTouchExplorationEnabled()) super.onHoverEvent(event);
149        return consumed;
150    }
151
152    @Override
153    public boolean onGenericMotionEvent(MotionEvent event) {
154        return mContentViewCore.onGenericMotionEvent(event);
155    }
156
157    @Override
158    public boolean performLongClick() {
159        return false;
160    }
161
162    @Override
163    protected void onConfigurationChanged(Configuration newConfig) {
164        mContentViewCore.onConfigurationChanged(newConfig);
165    }
166
167    /**
168     * Currently the ContentView scrolling happens in the native side. In
169     * the Java view system, it is always pinned at (0, 0). scrollBy() and scrollTo()
170     * are overridden, so that View's mScrollX and mScrollY will be unchanged at
171     * (0, 0). This is critical for drawing ContentView correctly.
172     */
173    @Override
174    public void scrollBy(int x, int y) {
175        mContentViewCore.scrollBy(x, y);
176    }
177
178    @Override
179    public void scrollTo(int x, int y) {
180        mContentViewCore.scrollTo(x, y);
181    }
182
183    @Override
184    protected int computeHorizontalScrollExtent() {
185        // TODO(dtrainor): Need to expose scroll events properly to public. Either make getScroll*
186        // work or expose computeHorizontalScrollOffset()/computeVerticalScrollOffset as public.
187        return mContentViewCore.computeHorizontalScrollExtent();
188    }
189
190    @Override
191    protected int computeHorizontalScrollOffset() {
192        return mContentViewCore.computeHorizontalScrollOffset();
193    }
194
195    @Override
196    protected int computeHorizontalScrollRange() {
197        return mContentViewCore.computeHorizontalScrollRange();
198    }
199
200    @Override
201    protected int computeVerticalScrollExtent() {
202        return mContentViewCore.computeVerticalScrollExtent();
203    }
204
205    @Override
206    protected int computeVerticalScrollOffset() {
207        return mContentViewCore.computeVerticalScrollOffset();
208    }
209
210    @Override
211    protected int computeVerticalScrollRange() {
212        return mContentViewCore.computeVerticalScrollRange();
213    }
214
215    // End FrameLayout overrides.
216
217    @Override
218    public boolean awakenScrollBars(int startDelay, boolean invalidate) {
219        return mContentViewCore.awakenScrollBars(startDelay, invalidate);
220    }
221
222    @Override
223    public boolean awakenScrollBars() {
224        return super.awakenScrollBars();
225    }
226
227    @Override
228    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
229        super.onInitializeAccessibilityNodeInfo(info);
230        mContentViewCore.onInitializeAccessibilityNodeInfo(info);
231    }
232
233    /**
234     * Fills in scrolling values for AccessibilityEvents.
235     * @param event Event being fired.
236     */
237    @Override
238    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
239        super.onInitializeAccessibilityEvent(event);
240        mContentViewCore.onInitializeAccessibilityEvent(event);
241    }
242
243    @Override
244    protected void onAttachedToWindow() {
245        super.onAttachedToWindow();
246        mContentViewCore.onAttachedToWindow();
247    }
248
249    @Override
250    protected void onDetachedFromWindow() {
251        super.onDetachedFromWindow();
252        mContentViewCore.onDetachedFromWindow();
253    }
254
255    @Override
256    protected void onVisibilityChanged(View changedView, int visibility) {
257        super.onVisibilityChanged(changedView, visibility);
258        mContentViewCore.onVisibilityChanged(changedView, visibility);
259    }
260
261    ///////////////////////////////////////////////////////////////////////////////////////////////
262    //              Start Implementation of ContentViewCore.InternalAccessDelegate               //
263    ///////////////////////////////////////////////////////////////////////////////////////////////
264
265    @Override
266    public boolean super_onKeyUp(int keyCode, KeyEvent event) {
267        return super.onKeyUp(keyCode, event);
268    }
269
270    @Override
271    public boolean super_dispatchKeyEventPreIme(KeyEvent event) {
272        return super.dispatchKeyEventPreIme(event);
273    }
274
275    @Override
276    public boolean super_dispatchKeyEvent(KeyEvent event) {
277        return super.dispatchKeyEvent(event);
278    }
279
280    @Override
281    public boolean super_onGenericMotionEvent(MotionEvent event) {
282        return super.onGenericMotionEvent(event);
283    }
284
285    @Override
286    public void super_onConfigurationChanged(Configuration newConfig) {
287        super.onConfigurationChanged(newConfig);
288    }
289
290    @Override
291    public boolean super_awakenScrollBars(int startDelay, boolean invalidate) {
292        return super.awakenScrollBars(startDelay, invalidate);
293    }
294
295    ///////////////////////////////////////////////////////////////////////////////////////////////
296    //                End Implementation of ContentViewCore.InternalAccessDelegate               //
297    ///////////////////////////////////////////////////////////////////////////////////////////////
298}
299