ContentViewClient.java revision f2477e01787aa58f445919b809d89e252beef54f
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     * Notifies the client that the position of the top controls has changed.
47     * @param topControlsOffsetYPix The Y offset of the top controls in physical pixels.
48     * @param contentOffsetYPix The Y offset of the content in physical pixels.
49     * @param overdrawBottomHeightPix The overdraw height.
50     */
51    public void onOffsetsForFullscreenChanged(
52            float topControlsOffsetYPix, float contentOffsetYPix, float overdrawBottomHeightPix) {
53    }
54
55    public boolean shouldOverrideKeyEvent(KeyEvent event) {
56        int keyCode = event.getKeyCode();
57        // We need to send almost every key to WebKit. However:
58        // 1. We don't want to block the device on the renderer for
59        // some keys like menu, home, call.
60        // 2. There are no WebKit equivalents for some of these keys
61        // (see app/keyboard_codes_win.h)
62        // Note that these are not the same set as KeyEvent.isSystemKey:
63        // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
64        if (keyCode == KeyEvent.KEYCODE_MENU ||
65            keyCode == KeyEvent.KEYCODE_HOME ||
66            keyCode == KeyEvent.KEYCODE_BACK ||
67            keyCode == KeyEvent.KEYCODE_CALL ||
68            keyCode == KeyEvent.KEYCODE_ENDCALL ||
69            keyCode == KeyEvent.KEYCODE_POWER ||
70            keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
71            keyCode == KeyEvent.KEYCODE_CAMERA ||
72            keyCode == KeyEvent.KEYCODE_FOCUS ||
73            keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
74            keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
75            keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
76            return true;
77        }
78
79        // We also have to intercept some shortcuts before we send them to the ContentView.
80        if (event.isCtrlPressed() && (
81                keyCode == KeyEvent.KEYCODE_TAB ||
82                keyCode == KeyEvent.KEYCODE_W ||
83                keyCode == KeyEvent.KEYCODE_F4)) {
84            return true;
85        }
86
87        return false;
88    }
89
90    /**
91     * Called when an ImeEvent is sent to the page. Can be used to know when some text is entered
92     * in a page.
93     */
94    public void onImeEvent() {
95    }
96
97    /**
98     * Notified when a change to the IME was requested.
99     *
100     * @param requestShow Whether the IME was requested to be shown (may already be showing
101     *                    though).
102     */
103    public void onImeStateChangeRequested(boolean requestShow) {
104    }
105
106    // TODO(dtrainor): Should expose getScrollX/Y from ContentView or make
107    // computeHorizontalScrollOffset()/computeVerticalScrollOffset() public.
108    /**
109     * Gives the UI the chance to override each scroll event.
110     * @param dx The amount scrolled in the X direction (in physical pixels).
111     * @param dy The amount scrolled in the Y direction (in physical pixels).
112     * @param scrollX The current X scroll offset (in physical pixels).
113     * @param scrollY The current Y scroll offset (in physical pixels).
114     * @return Whether or not the UI consumed and handled this event.
115     */
116    public boolean shouldOverrideScroll(float dx, float dy, float scrollX, float scrollY) {
117        return false;
118    }
119
120    /**
121     * Returns an ActionMode.Callback for in-page selection.
122     */
123    public ActionMode.Callback getSelectActionModeCallback(
124            Context context, ActionHandler actionHandler, boolean incognito) {
125        return new SelectActionModeCallback(context, actionHandler, incognito);
126    }
127
128    /**
129     * Called when the contextual ActionBar is shown.
130     */
131    public void onContextualActionBarShown() {
132    }
133
134    /**
135     * Called when the contextual ActionBar is hidden.
136     */
137    public void onContextualActionBarHidden() {
138    }
139
140    /**
141     * Perform a search on {@code searchQuery}.  This method is only called if
142     * {@link #doesPerformWebSearch()} returns {@code true}.
143     * @param searchQuery The string to search for.
144     */
145    public void performWebSearch(String searchQuery) {
146    }
147
148    /**
149     * If this returns {@code true} contextual web search attempts will be forwarded to
150     * {@link #performWebSearch(String)}.
151     * @return {@code true} iff this {@link ContentViewClient} wants to consume web search queries
152     *         and override the default intent behavior.
153     */
154    public boolean doesPerformWebSearch() {
155        return false;
156    }
157
158    /**
159     * Called when a new content intent is requested to be started.
160     */
161    public void onStartContentIntent(Context context, String intentUrl) {
162        Intent intent;
163        // Perform generic parsing of the URI to turn it into an Intent.
164        try {
165            intent = Intent.parseUri(intentUrl, Intent.URI_INTENT_SCHEME);
166        } catch (URISyntaxException ex) {
167            Log.w(TAG, "Bad URI " + intentUrl + ": " + ex.getMessage());
168            return;
169        }
170
171        try {
172            context.startActivity(intent);
173        } catch (ActivityNotFoundException ex) {
174            Log.w(TAG, "No application can handle " + intentUrl);
175        }
176    }
177
178    public void onExternalVideoSurfaceRequested(int playerId) {
179    }
180
181    public void onGeometryChanged(int playerId, RectF rect) {
182    }
183
184    public ContentVideoViewClient getContentVideoViewClient() {
185        return null;
186    }
187
188    /**
189     * Called when BrowserMediaPlayerManager wants to load a media resource.
190     * @param url the URL of media resource to load.
191     * @return true to prevent the resource from being loaded.
192     */
193    public boolean shouldBlockMediaRequest(String url) {
194        return false;
195    }
196}
197