ContentViewClient.java revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.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    public void onTabCrash() {
38    }
39
40    public boolean shouldOverrideKeyEvent(KeyEvent event) {
41        int keyCode = event.getKeyCode();
42        // We need to send almost every key to WebKit. However:
43        // 1. We don't want to block the device on the renderer for
44        // some keys like menu, home, call.
45        // 2. There are no WebKit equivalents for some of these keys
46        // (see app/keyboard_codes_win.h)
47        // Note that these are not the same set as KeyEvent.isSystemKey:
48        // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
49        if (keyCode == KeyEvent.KEYCODE_MENU ||
50            keyCode == KeyEvent.KEYCODE_HOME ||
51            keyCode == KeyEvent.KEYCODE_BACK ||
52            keyCode == KeyEvent.KEYCODE_CALL ||
53            keyCode == KeyEvent.KEYCODE_ENDCALL ||
54            keyCode == KeyEvent.KEYCODE_POWER ||
55            keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
56            keyCode == KeyEvent.KEYCODE_CAMERA ||
57            keyCode == KeyEvent.KEYCODE_FOCUS ||
58            keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
59            keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
60            keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
61            return true;
62        }
63
64        // We also have to intercept some shortcuts before we send them to the ContentView.
65        if (event.isCtrlPressed() && (
66                keyCode == KeyEvent.KEYCODE_TAB ||
67                keyCode == KeyEvent.KEYCODE_W ||
68                keyCode == KeyEvent.KEYCODE_F4)) {
69            return true;
70        }
71
72        return false;
73    }
74
75    // Called when an ImeEvent is sent to the page. Can be used to know when some text is entered
76    // in a page.
77    public void onImeEvent() {
78    }
79
80    /**
81     * A callback invoked after the JavaScript code passed to evaluateJavaScript
82     * has finished execution.
83     * Used in automation tests.
84     * @hide
85     */
86    public void onEvaluateJavaScriptResult(int id, String jsonResult) {
87    }
88
89    // TODO (dtrainor): Should expose getScrollX/Y from ContentView or make
90    // computeHorizontalScrollOffset()/computeVerticalScrollOffset() public.
91    /**
92     * Gives the UI the chance to override each scroll event.
93     * @param dx The amount scrolled in the X direction.
94     * @param dy The amount scrolled in the Y direction.
95     * @param scrollX The current X scroll offset.
96     * @param scrollY The current Y scroll offset.
97     * @return Whether or not the UI consumed and handled this event.
98     */
99    public boolean shouldOverrideScroll(float dx, float dy, float scrollX, float scrollY) {
100        return false;
101    }
102
103    /**
104     * Returns an ActionMode.Callback for in-page selection.
105     */
106    public ActionMode.Callback getSelectActionModeCallback(
107            Context context, ActionHandler actionHandler, boolean incognito) {
108        return new SelectActionModeCallback(context, actionHandler, incognito);
109    }
110
111    /**
112     * Called when the contextual ActionBar is shown.
113     */
114    public void onContextualActionBarShown() {
115    }
116
117    /**
118     * Called when the contextual ActionBar is hidden.
119     */
120    public void onContextualActionBarHidden() {
121    }
122
123    /**
124     * Called when a new content intent is requested to be started.
125     */
126    public void onStartContentIntent(Context context, String intentUrl) {
127        Intent intent;
128        // Perform generic parsing of the URI to turn it into an Intent.
129        try {
130            intent = Intent.parseUri(intentUrl, Intent.URI_INTENT_SCHEME);
131        } catch (URISyntaxException ex) {
132            Log.w(TAG, "Bad URI " + intentUrl + ": " + ex.getMessage());
133            return;
134        }
135
136        try {
137            context.startActivity(intent);
138        } catch (ActivityNotFoundException ex) {
139            Log.w(TAG, "No application can handle " + intentUrl);
140        }
141    }
142}
143