ContentViewClient.java revision d3868032626d59662ff73b372b5d584c1d144c53
1// Copyright (c) 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.ActivityNotFoundException;
8import android.content.Context;
9import android.content.Intent;
10import android.graphics.RectF;
11import android.util.Log;
12import android.view.ActionMode;
13import android.view.KeyEvent;
14
15import org.chromium.content.browser.SelectActionModeCallback.ActionHandler;
16
17import java.net.URISyntaxException;
18
19/**
20 *  Main callback class used by ContentView.
21 *
22 *  This contains the superset of callbacks required to implement the browser UI and the callbacks
23 *  required to implement the WebView API.
24 *  The memory and reference ownership of this class is unusual - see the .cc file and ContentView
25 *  for more details.
26 *
27 *  TODO(mkosiba): Rid this guy of default implementations. This class is used by both WebView and
28 *  the browser and we don't want a the browser-specific default implementation to accidentally leak
29 *  over to WebView.
30 */
31public class ContentViewClient {
32    // Tag used for logging.
33    private static final String TAG = "ContentViewClient";
34
35    public void onUpdateTitle(String title) {
36    }
37
38    /**
39     * Called whenever the background color of the page changes as notified by WebKit.
40     * @param color The new ARGB color of the page background.
41     */
42    public void onBackgroundColorChanged(int color) {
43    }
44
45    /**
46      * Lets client listen on the scaling changes on delayed, throttled
47      * and best-effort basis. Used for WebView.onScaleChanged.
48      */
49    public void onScaleChanged(float oldScale, float newScale) {
50    }
51
52    /**
53     * Notifies the client that the position of the top controls has changed.
54     * @param topControlsOffsetYPix The Y offset of the top controls in physical pixels.
55     * @param contentOffsetYPix The Y offset of the content in physical pixels.
56     * @param overdrawBottomHeightPix The overdraw height.
57     */
58    public void onOffsetsForFullscreenChanged(
59            float topControlsOffsetYPix, float contentOffsetYPix, float overdrawBottomHeightPix) {
60    }
61
62    /**
63     * Notifies the client that the renderer backing the ContentView has crashed. This is a legacy
64     * method to be dropped when all clients switch to onRendererCrash().
65     */
66    @Deprecated
67    public void onTabCrash() {
68    }
69
70    /**
71     * Notifies the client that the renderer backing the ContentView has crashed.
72     * @param crashedWhileOomProtected True iff the renderer died while being bound with a high
73     * priority binding, which indicates that it was probably an actual crash (as opposed to the
74     * renderer being killed by the OS out-of-memory killer).
75     */
76    public void onRendererCrash(boolean processWasOomProtected) {
77    }
78
79    public boolean shouldOverrideKeyEvent(KeyEvent event) {
80        int keyCode = event.getKeyCode();
81        // We need to send almost every key to WebKit. However:
82        // 1. We don't want to block the device on the renderer for
83        // some keys like menu, home, call.
84        // 2. There are no WebKit equivalents for some of these keys
85        // (see app/keyboard_codes_win.h)
86        // Note that these are not the same set as KeyEvent.isSystemKey:
87        // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
88        if (keyCode == KeyEvent.KEYCODE_MENU ||
89            keyCode == KeyEvent.KEYCODE_HOME ||
90            keyCode == KeyEvent.KEYCODE_BACK ||
91            keyCode == KeyEvent.KEYCODE_CALL ||
92            keyCode == KeyEvent.KEYCODE_ENDCALL ||
93            keyCode == KeyEvent.KEYCODE_POWER ||
94            keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
95            keyCode == KeyEvent.KEYCODE_CAMERA ||
96            keyCode == KeyEvent.KEYCODE_FOCUS ||
97            keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
98            keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
99            keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
100            return true;
101        }
102
103        // We also have to intercept some shortcuts before we send them to the ContentView.
104        if (event.isCtrlPressed() && (
105                keyCode == KeyEvent.KEYCODE_TAB ||
106                keyCode == KeyEvent.KEYCODE_W ||
107                keyCode == KeyEvent.KEYCODE_F4)) {
108            return true;
109        }
110
111        return false;
112    }
113
114    // Called when an ImeEvent is sent to the page. Can be used to know when some text is entered
115    // in a page.
116    public void onImeEvent() {
117    }
118
119    /**
120     * Notified when a change to the IME was requested.
121     *
122     * @param requestShow Whether the IME was requested to be shown (may already be showing
123     *                    though).
124     */
125    public void onImeStateChangeRequested(boolean requestShow) {
126    }
127
128    // TODO (dtrainor): Should expose getScrollX/Y from ContentView or make
129    // computeHorizontalScrollOffset()/computeVerticalScrollOffset() public.
130    /**
131     * Gives the UI the chance to override each scroll event.
132     * @param dx The amount scrolled in the X direction (in physical pixels).
133     * @param dy The amount scrolled in the Y direction (in physical pixels).
134     * @param scrollX The current X scroll offset (in physical pixels).
135     * @param scrollY The current Y scroll offset (in physical pixels).
136     * @return Whether or not the UI consumed and handled this event.
137     */
138    public boolean shouldOverrideScroll(float dx, float dy, float scrollX, float scrollY) {
139        return false;
140    }
141
142    /**
143     * Returns an ActionMode.Callback for in-page selection.
144     */
145    public ActionMode.Callback getSelectActionModeCallback(
146            Context context, ActionHandler actionHandler, boolean incognito) {
147        return new SelectActionModeCallback(context, actionHandler, incognito);
148    }
149
150    /**
151     * Called when the contextual ActionBar is shown.
152     */
153    public void onContextualActionBarShown() {
154    }
155
156    /**
157     * Called when the contextual ActionBar is hidden.
158     */
159    public void onContextualActionBarHidden() {
160    }
161
162    /**
163     * Called when a new content intent is requested to be started.
164     */
165    public void onStartContentIntent(Context context, String intentUrl) {
166        Intent intent;
167        // Perform generic parsing of the URI to turn it into an Intent.
168        try {
169            intent = Intent.parseUri(intentUrl, Intent.URI_INTENT_SCHEME);
170        } catch (URISyntaxException ex) {
171            Log.w(TAG, "Bad URI " + intentUrl + ": " + ex.getMessage());
172            return;
173        }
174
175        try {
176            context.startActivity(intent);
177        } catch (ActivityNotFoundException ex) {
178            Log.w(TAG, "No application can handle " + intentUrl);
179        }
180    }
181
182    public void onExternalVideoSurfaceRequested(int playerId) {
183    }
184
185    public void onGeometryChanged(int playerId, RectF rect) {
186    }
187
188    public ContentVideoViewClient getContentVideoViewClient() {
189        return null;
190    }
191}
192