ContentViewClient.java revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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(donnd): Remove this and instead expose a ContextualSearchClient, crbug.com/403001.
135     * @param eventType The selection event type, see {@link SelectionEventType}.
136     */
137    public void onSelectionEvent(int eventType) {
138    }
139
140    /**
141     * Called when a new content intent is requested to be started.
142     */
143    public void onStartContentIntent(Context context, String intentUrl) {
144        Intent intent;
145        // Perform generic parsing of the URI to turn it into an Intent.
146        try {
147            intent = Intent.parseUri(intentUrl, Intent.URI_INTENT_SCHEME);
148        } catch (URISyntaxException ex) {
149            Log.w(TAG, "Bad URI " + intentUrl + ": " + ex.getMessage());
150            return;
151        }
152
153        try {
154            context.startActivity(intent);
155        } catch (ActivityNotFoundException ex) {
156            Log.w(TAG, "No application can handle " + intentUrl);
157        }
158    }
159
160    public ContentVideoViewClient getContentVideoViewClient() {
161        return null;
162    }
163
164    /**
165     * Called when BrowserMediaPlayerManager wants to load a media resource.
166     * @param url the URL of media resource to load.
167     * @return true to prevent the resource from being loaded.
168     */
169    public boolean shouldBlockMediaRequest(String url) {
170        return false;
171    }
172
173    /**
174     * Check whether a key should be propagated to the embedder or not.
175     * We need to send almost every key to Blink. However:
176     * 1. We don't want to block the device on the renderer for
177     * some keys like menu, home, call.
178     * 2. There are no WebKit equivalents for some of these keys
179     * (see app/keyboard_codes_win.h)
180     * Note that these are not the same set as KeyEvent.isSystemKey:
181     * for instance, AKEYCODE_MEDIA_* will be dispatched to webkit*.
182     */
183    public static boolean shouldPropagateKey(int keyCode) {
184        if (keyCode == KeyEvent.KEYCODE_MENU ||
185            keyCode == KeyEvent.KEYCODE_HOME ||
186            keyCode == KeyEvent.KEYCODE_BACK ||
187            keyCode == KeyEvent.KEYCODE_CALL ||
188            keyCode == KeyEvent.KEYCODE_ENDCALL ||
189            keyCode == KeyEvent.KEYCODE_POWER ||
190            keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
191            keyCode == KeyEvent.KEYCODE_CAMERA ||
192            keyCode == KeyEvent.KEYCODE_FOCUS ||
193            keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
194            keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
195            keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
196            return false;
197        }
198        return true;
199    }
200}
201