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