ContentViewClient.java revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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.ActivityNotFoundException;
8import android.content.Context;
9import android.content.Intent;
10import android.util.Log;
11import android.view.ActionMode;
12import android.view.KeyEvent;
13
14import org.chromium.content.browser.SelectActionModeCallback.ActionHandler;
15
16import java.net.URISyntaxException;
17
18/**
19 *  Main callback class used by ContentView.
20 *
21 *  This contains the superset of callbacks required to implement the browser UI and the callbacks
22 *  required to implement the WebView API.
23 *  The memory and reference ownership of this class is unusual - see the .cc file and ContentView
24 *  for more details.
25 *
26 *  TODO(mkosiba): Rid this guy of default implementations. This class is used by both WebView and
27 *  the browser and we don't want a the browser-specific default implementation to accidentally leak
28 *  over to WebView.
29 */
30public class ContentViewClient {
31    // Tag used for logging.
32    private static final String TAG = "ContentViewClient";
33
34    public void onUpdateTitle(String title) {
35    }
36
37    /**
38     * Called whenever the background color of the page changes as notified by WebKit.
39     * @param color The new ARGB color of the page background.
40     */
41    public void onBackgroundColorChanged(int color) {
42    }
43
44    /**
45     * Notifies the client that the position of the top controls has changed.
46     * @param topControlsOffsetYPix The Y offset of the top controls in physical pixels.
47     * @param contentOffsetYPix The Y offset of the content in physical pixels.
48     * @param overdrawBottomHeightPix The overdraw height.
49     */
50    public void onOffsetsForFullscreenChanged(
51            float topControlsOffsetYPix, float contentOffsetYPix, float overdrawBottomHeightPix) {
52    }
53
54    public boolean shouldOverrideKeyEvent(KeyEvent event) {
55        int keyCode = event.getKeyCode();
56
57        if (!shouldPropagateKey(keyCode)) return true;
58
59        // We also have to intercept some shortcuts before we send them to the ContentView.
60        if (event.isCtrlPressed() && (
61                keyCode == KeyEvent.KEYCODE_TAB ||
62                keyCode == KeyEvent.KEYCODE_W ||
63                keyCode == KeyEvent.KEYCODE_F4)) {
64            return true;
65        }
66
67        return false;
68    }
69
70    /**
71     * Called when an ImeEvent is sent to the page. Can be used to know when some text is entered
72     * in a page.
73     */
74    public void onImeEvent() {
75    }
76
77    /**
78     * Notified when a change to the IME was requested.
79     *
80     * @param requestShow Whether the IME was requested to be shown (may already be showing
81     *                    though).
82     */
83    public void onImeStateChangeRequested(boolean requestShow) {
84    }
85
86    /**
87     * Returns an ActionMode.Callback for in-page selection.
88     */
89    public ActionMode.Callback getSelectActionModeCallback(
90            Context context, ActionHandler actionHandler, boolean incognito) {
91        return new SelectActionModeCallback(context, actionHandler, incognito);
92    }
93
94    /**
95     * Called when the contextual ActionBar is shown.
96     */
97    public void onContextualActionBarShown() {
98    }
99
100    /**
101     * Called when the contextual ActionBar is hidden.
102     */
103    public void onContextualActionBarHidden() {
104    }
105
106    /**
107     * Perform a search on {@code searchQuery}.  This method is only called if
108     * {@link #doesPerformWebSearch()} returns {@code true}.
109     * @param searchQuery The string to search for.
110     */
111    public void performWebSearch(String searchQuery) {
112    }
113
114    /**
115     * If this returns {@code true} contextual web search attempts will be forwarded to
116     * {@link #performWebSearch(String)}.
117     * @return {@code true} iff this {@link ContentViewClient} wants to consume web search queries
118     *         and override the default intent behavior.
119     */
120    public boolean doesPerformWebSearch() {
121        return false;
122    }
123
124    /**
125     * Notification that the selection has changed.
126     * TODO(donnd): Remove this and instead expose a ContextualSearchClient, crbug.com/403001.
127     * @param selection The newly established selection.
128     */
129    public void onSelectionChanged(String selection) {
130    }
131
132    /**
133     * Notification that a selection or insertion-related event has occurred.
134     * TODO(pedrosimonetti): remove this method once downstream code has been updated to use
135     * the other signature.
136     * @param eventType The selection event type, see {@link SelectionEventType}.
137     */
138    public void onSelectionEvent(int eventType) {
139    }
140
141    /**
142     * Notification that a selection or insertion-related event has occurred.
143     * TODO(donnd): Remove this and instead expose a ContextualSearchClient, crbug.com/403001.
144     * @param eventType The selection event type, see {@link SelectionEventType}.
145     * @param posXPix The x coordinate of the selection start handle.
146     * @param posYPix The y coordinate of the selection start handle.
147     */
148    public void onSelectionEvent(int eventType, float posXPix, float posYPix) {
149        onSelectionEvent(eventType);
150    }
151
152    /**
153     * Called when a new content intent is requested to be started.
154     */
155    public void onStartContentIntent(Context context, String intentUrl) {
156        Intent intent;
157        // Perform generic parsing of the URI to turn it into an Intent.
158        try {
159            intent = Intent.parseUri(intentUrl, Intent.URI_INTENT_SCHEME);
160        } catch (URISyntaxException ex) {
161            Log.w(TAG, "Bad URI " + intentUrl + ": " + ex.getMessage());
162            return;
163        }
164
165        try {
166            context.startActivity(intent);
167        } catch (ActivityNotFoundException ex) {
168            Log.w(TAG, "No application can handle " + intentUrl);
169        }
170    }
171
172    public ContentVideoViewClient getContentVideoViewClient() {
173        return null;
174    }
175
176    /**
177     * Called when BrowserMediaPlayerManager wants to load a media resource.
178     * @param url the URL of media resource to load.
179     * @return true to prevent the resource from being loaded.
180     */
181    public boolean shouldBlockMediaRequest(String url) {
182        return false;
183    }
184
185    /**
186     * Check whether a key should be propagated to the embedder or not.
187     * We need to send almost every key to Blink. However:
188     * 1. We don't want to block the device on the renderer for
189     * some keys like menu, home, call.
190     * 2. There are no WebKit equivalents for some of these keys
191     * (see app/keyboard_codes_win.h)
192     * Note that these are not the same set as KeyEvent.isSystemKey:
193     * for instance, AKEYCODE_MEDIA_* will be dispatched to webkit*.
194     */
195    public static boolean shouldPropagateKey(int keyCode) {
196        if (keyCode == KeyEvent.KEYCODE_MENU ||
197            keyCode == KeyEvent.KEYCODE_HOME ||
198            keyCode == KeyEvent.KEYCODE_BACK ||
199            keyCode == KeyEvent.KEYCODE_CALL ||
200            keyCode == KeyEvent.KEYCODE_ENDCALL ||
201            keyCode == KeyEvent.KEYCODE_POWER ||
202            keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
203            keyCode == KeyEvent.KEYCODE_CAMERA ||
204            keyCode == KeyEvent.KEYCODE_FOCUS ||
205            keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
206            keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
207            keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
208            return false;
209        }
210        return true;
211    }
212}
213