ContentViewClient.java revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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    /**
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 that the content size has changed.
46     * @param widthCss The width of the content in CSS pixels.
47     * @param heightCss The height of the content in CSS pixels.
48     */
49    public void onContentSizeChanged(float widthCss, float heightCss) {
50    }
51
52    /**
53      * Lets client listen on the scaling changes on delayed, throttled
54      * and best-effort basis. Used for WebView.onScaleChanged.
55      */
56    public void onScaleChanged(float oldScale, float newScale) {
57    }
58
59    /**
60     * Notifies the client that the position of the top controls has changed.
61     * @param topControlsOffsetYPix The Y offset of the top controls in physical pixels.
62     * @param contentOffsetYPix The Y offset of the content in physical pixels.
63     */
64    public void onOffsetsForFullscreenChanged(
65            float topControlsOffsetYPix, float contentOffsetYPix) {
66    }
67
68    public void onTabCrash() {
69    }
70
71    public boolean shouldOverrideKeyEvent(KeyEvent event) {
72        int keyCode = event.getKeyCode();
73        // We need to send almost every key to WebKit. However:
74        // 1. We don't want to block the device on the renderer for
75        // some keys like menu, home, call.
76        // 2. There are no WebKit equivalents for some of these keys
77        // (see app/keyboard_codes_win.h)
78        // Note that these are not the same set as KeyEvent.isSystemKey:
79        // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
80        if (keyCode == KeyEvent.KEYCODE_MENU ||
81            keyCode == KeyEvent.KEYCODE_HOME ||
82            keyCode == KeyEvent.KEYCODE_BACK ||
83            keyCode == KeyEvent.KEYCODE_CALL ||
84            keyCode == KeyEvent.KEYCODE_ENDCALL ||
85            keyCode == KeyEvent.KEYCODE_POWER ||
86            keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
87            keyCode == KeyEvent.KEYCODE_CAMERA ||
88            keyCode == KeyEvent.KEYCODE_FOCUS ||
89            keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
90            keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
91            keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
92            return true;
93        }
94
95        // We also have to intercept some shortcuts before we send them to the ContentView.
96        if (event.isCtrlPressed() && (
97                keyCode == KeyEvent.KEYCODE_TAB ||
98                keyCode == KeyEvent.KEYCODE_W ||
99                keyCode == KeyEvent.KEYCODE_F4)) {
100            return true;
101        }
102
103        return false;
104    }
105
106    // Called when an ImeEvent is sent to the page. Can be used to know when some text is entered
107    // in a page.
108    public void onImeEvent() {
109    }
110
111    /**
112     * Notified when a change to the IME was requested.
113     *
114     * @param requestShow Whether the IME was requested to be shown (may already be showing
115     *                    though).
116     */
117    public void onImeStateChangeRequested(boolean requestShow) {
118    }
119
120    // TODO (dtrainor): Should expose getScrollX/Y from ContentView or make
121    // computeHorizontalScrollOffset()/computeVerticalScrollOffset() public.
122    /**
123     * Gives the UI the chance to override each scroll event.
124     * @param dx The amount scrolled in the X direction (in physical pixels).
125     * @param dy The amount scrolled in the Y direction (in physical pixels).
126     * @param scrollX The current X scroll offset (in physical pixels).
127     * @param scrollY The current Y scroll offset (in physical pixels).
128     * @return Whether or not the UI consumed and handled this event.
129     */
130    public boolean shouldOverrideScroll(float dx, float dy, float scrollX, float scrollY) {
131        return false;
132    }
133
134    /**
135     * Returns an ActionMode.Callback for in-page selection.
136     */
137    public ActionMode.Callback getSelectActionModeCallback(
138            Context context, ActionHandler actionHandler, boolean incognito) {
139        return new SelectActionModeCallback(context, actionHandler, incognito);
140    }
141
142    /**
143     * Called when the contextual ActionBar is shown.
144     */
145    public void onContextualActionBarShown() {
146    }
147
148    /**
149     * Called when the contextual ActionBar is hidden.
150     */
151    public void onContextualActionBarHidden() {
152    }
153
154    /**
155     * Called when a new content intent is requested to be started.
156     */
157    public void onStartContentIntent(Context context, String intentUrl) {
158        Intent intent;
159        // Perform generic parsing of the URI to turn it into an Intent.
160        try {
161            intent = Intent.parseUri(intentUrl, Intent.URI_INTENT_SCHEME);
162        } catch (URISyntaxException ex) {
163            Log.w(TAG, "Bad URI " + intentUrl + ": " + ex.getMessage());
164            return;
165        }
166
167        try {
168            context.startActivity(intent);
169        } catch (ActivityNotFoundException ex) {
170            Log.w(TAG, "No application can handle " + intentUrl);
171        }
172    }
173
174    public void onExternalVideoSurfaceRequested(int playerId) {
175    }
176}
177