ContentViewClient.java revision effb81e5f8246d0db0270817048dc992db66e9fb
18cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar// Copyright 2012 The Chromium Authors. All rights reserved.
28cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar// Use of this source code is governed by a BSD-style license that can be
38cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar// found in the LICENSE file.
4b1c07156bab839c0502789f09654ec5da8d33c39Blaine Garst
5b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarpackage org.chromium.content.browser;
6b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
7b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarimport android.content.ActivityNotFoundException;
8b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarimport android.content.Context;
9b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarimport android.content.Intent;
10b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarimport android.util.Log;
11b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarimport android.view.ActionMode;
12b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarimport android.view.KeyEvent;
13b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
14b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarimport org.chromium.content.browser.SelectActionModeCallback.ActionHandler;
15b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
16b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarimport java.net.URISyntaxException;
17b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
18b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar/**
19b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar *  Main callback class used by ContentView.
20b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar *
21b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar *  This contains the superset of callbacks required to implement the browser UI and the callbacks
22b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar *  required to implement the WebView API.
238cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar *  The memory and reference ownership of this class is unusual - see the .cc file and ContentView
248cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar *  for more details.
258cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar *
268cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar *  TODO(mkosiba): Rid this guy of default implementations. This class is used by both WebView and
278cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar *  the browser and we don't want a the browser-specific default implementation to accidentally leak
288cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar *  over to WebView.
298cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar */
308cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbarpublic class ContentViewClient {
318cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    // Tag used for logging.
328cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    private static final String TAG = "ContentViewClient";
338cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
348cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    public void onUpdateTitle(String title) {
358cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    }
36ffd69e7b181a486cbba10deb386558cf3ff5e24aCharles Davis
378cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    /**
38ffd69e7b181a486cbba10deb386558cf3ff5e24aCharles Davis     * Called whenever the background color of the page changes as notified by WebKit.
39ffd69e7b181a486cbba10deb386558cf3ff5e24aCharles Davis     * @param color The new ARGB color of the page background.
40ffd69e7b181a486cbba10deb386558cf3ff5e24aCharles Davis     */
41ffd69e7b181a486cbba10deb386558cf3ff5e24aCharles Davis    public void onBackgroundColorChanged(int color) {
42ffd69e7b181a486cbba10deb386558cf3ff5e24aCharles Davis    }
43ffd69e7b181a486cbba10deb386558cf3ff5e24aCharles Davis
448cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    /**
45b4c3b6f8a2d3481bac6b0e9b4240fa0c99412d10Shantonu Sen     * Notifies the client that the position of the top controls has changed.
468cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * @param topControlsOffsetYPix The Y offset of the top controls in physical pixels.
478cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * @param contentOffsetYPix The Y offset of the content in physical pixels.
488cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * @param overdrawBottomHeightPix The overdraw height.
498cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     */
508cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    public void onOffsetsForFullscreenChanged(
518cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            float topControlsOffsetYPix, float contentOffsetYPix, float overdrawBottomHeightPix) {
528cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    }
538cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
5409870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan    public boolean shouldOverrideKeyEvent(KeyEvent event) {
558cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        int keyCode = event.getKeyCode();
568cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        // We need to send almost every key to WebKit. However:
5709870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan        // 1. We don't want to block the device on the renderer for
588cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        // some keys like menu, home, call.
598cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        // 2. There are no WebKit equivalents for some of these keys
6009870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan        // (see app/keyboard_codes_win.h)
6109870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan        // Note that these are not the same set as KeyEvent.isSystemKey:
628cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
638cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        if (keyCode == KeyEvent.KEYCODE_MENU ||
648cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            keyCode == KeyEvent.KEYCODE_HOME ||
658cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            keyCode == KeyEvent.KEYCODE_BACK ||
668cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            keyCode == KeyEvent.KEYCODE_CALL ||
678cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            keyCode == KeyEvent.KEYCODE_ENDCALL ||
688cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            keyCode == KeyEvent.KEYCODE_POWER ||
6909870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan            keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
708cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            keyCode == KeyEvent.KEYCODE_CAMERA ||
718cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            keyCode == KeyEvent.KEYCODE_FOCUS ||
728cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
738cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
748cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
758cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            return true;
7609870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan        }
778cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
788cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        // We also have to intercept some shortcuts before we send them to the ContentView.
798cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        if (event.isCtrlPressed() && (
808cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar                keyCode == KeyEvent.KEYCODE_TAB ||
818cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar                keyCode == KeyEvent.KEYCODE_W ||
828cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar                keyCode == KeyEvent.KEYCODE_F4)) {
830898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan            return true;
848cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        }
858cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
868cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        return false;
870898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan    }
888cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
898cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    /**
9009870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan     * Called when an ImeEvent is sent to the page. Can be used to know when some text is entered
918cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * in a page.
928cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     */
938cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    public void onImeEvent() {
948cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    }
958cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
968cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    /**
978cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * Notified when a change to the IME was requested.
988cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     *
9909870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan     * @param requestShow Whether the IME was requested to be shown (may already be showing
1008cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     *                    though).
1018cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     */
10209870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan    public void onImeStateChangeRequested(boolean requestShow) {
1030898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan    }
1040898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan
1050898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan    /**
1060898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan     * Returns an ActionMode.Callback for in-page selection.
10709870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan     */
1088cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    public ActionMode.Callback getSelectActionModeCallback(
1098cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            Context context, ActionHandler actionHandler, boolean incognito) {
1100898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan        return new SelectActionModeCallback(context, actionHandler, incognito);
1118cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    }
1120898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan
1138cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    /**
1148cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * Called when the contextual ActionBar is shown.
1150898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan     */
1168cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    public void onContextualActionBarShown() {
1178cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    }
1188cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
1198cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    /**
1200898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan     * Called when the contextual ActionBar is hidden.
1218cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     */
12209870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan    public void onContextualActionBarHidden() {
1238cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    }
1248cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
1258cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    /**
1268cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * Perform a search on {@code searchQuery}.  This method is only called if
1270898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan     * {@link #doesPerformWebSearch()} returns {@code true}.
1280898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan     * @param searchQuery The string to search for.
1298cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     */
1308cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    public void performWebSearch(String searchQuery) {
1318cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    }
1328cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
1338cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    /**
1348cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * If this returns {@code true} contextual web search attempts will be forwarded to
1358cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * {@link #performWebSearch(String)}.
1368cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * @return {@code true} iff this {@link ContentViewClient} wants to consume web search queries
1370898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan     *         and override the default intent behavior.
1388cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     */
1398cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    public boolean doesPerformWebSearch() {
1408cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        return false;
1418cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    }
1428cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
1438cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    /**
1440898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan     * Notification that the selection has changed.
1458cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * @param selection The newly established selection.
1468cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     */
1478cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    public void onSelectionChanged(String selection) {
1488cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    }
1498cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
1508cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    /**
1518cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * Called when a new content intent is requested to be started.
1528cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     */
1530898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan    public void onStartContentIntent(Context context, String intentUrl) {
1548cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        Intent intent;
1558cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        // Perform generic parsing of the URI to turn it into an Intent.
1560898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan        try {
1578cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            intent = Intent.parseUri(intentUrl, Intent.URI_INTENT_SCHEME);
1588cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        } catch (URISyntaxException ex) {
1598cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            Log.w(TAG, "Bad URI " + intentUrl + ": " + ex.getMessage());
1600898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan            return;
1618cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        }
1620898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan
1638cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        try {
1648cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar            context.startActivity(intent);
1650898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan        } catch (ActivityNotFoundException ex) {
16609870645031d5a05c7c3b9a66a53348a0d82ae52Edward O'Callaghan            Log.w(TAG, "No application can handle " + intentUrl);
1678cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        }
1680898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan    }
1690898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan
1700898ee9197a0049e479a12f4c5b3c65e3deaf03bEdward O'Callaghan    public ContentVideoViewClient getContentVideoViewClient() {
1718cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar        return null;
1728cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar    }
1738cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar
174b4c3b6f8a2d3481bac6b0e9b4240fa0c99412d10Shantonu Sen    /**
1758cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * Called when BrowserMediaPlayerManager wants to load a media resource.
1768cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * @param url the URL of media resource to load.
1778cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     * @return true to prevent the resource from being loaded.
1788cbe163cba77c772621f89ddb33793ac170b1faDaniel Dunbar     */
179b4c3b6f8a2d3481bac6b0e9b4240fa0c99412d10Shantonu Sen    public boolean shouldBlockMediaRequest(String url) {
180        return false;
181    }
182
183}
184