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.graphics.RectF;
11import android.util.Log;
12import android.view.ActionMode;
13import android.view.KeyEvent;
14
15import org.chromium.content.browser.SelectActionModeCallback.ActionHandler;
16
17import java.net.URISyntaxException;
18
19/**
20 *  Main callback class used by ContentView.
21 *
22 *  This contains the superset of callbacks required to implement the browser UI and the callbacks
23 *  required to implement the WebView API.
24 *  The memory and reference ownership of this class is unusual - see the .cc file and ContentView
25 *  for more details.
26 *
27 *  TODO(mkosiba): Rid this guy of default implementations. This class is used by both WebView and
28 *  the browser and we don't want a the browser-specific default implementation to accidentally leak
29 *  over to WebView.
30 */
31public class ContentViewClient {
32    // Tag used for logging.
33    private static final String TAG = "ContentViewClient";
34
35    public void onUpdateTitle(String title) {
36    }
37
38    /**
39     * Called whenever the background color of the page changes as notified by WebKit.
40     * @param color The new ARGB color of the page background.
41     */
42    public void onBackgroundColorChanged(int color) {
43    }
44
45    /**
46     * Notifies the client that the position of the top controls has changed.
47     * @param topControlsOffsetYPix The Y offset of the top controls in physical pixels.
48     * @param contentOffsetYPix The Y offset of the content in physical pixels.
49     * @param overdrawBottomHeightPix The overdraw height.
50     */
51    public void onOffsetsForFullscreenChanged(
52            float topControlsOffsetYPix, float contentOffsetYPix, float overdrawBottomHeightPix) {
53    }
54
55    /**
56     * Notifies the client that the renderer backing the ContentView has crashed.
57     * @param crashedWhileOomProtected True iff the renderer died while being bound with a high
58     * priority binding, which indicates that it was probably an actual crash (as opposed to the
59     * renderer being killed by the OS out-of-memory killer).
60     */
61    public void onRendererCrash(boolean processWasOomProtected) {
62    }
63
64    public boolean shouldOverrideKeyEvent(KeyEvent event) {
65        int keyCode = event.getKeyCode();
66        // We need to send almost every key to WebKit. However:
67        // 1. We don't want to block the device on the renderer for
68        // some keys like menu, home, call.
69        // 2. There are no WebKit equivalents for some of these keys
70        // (see app/keyboard_codes_win.h)
71        // Note that these are not the same set as KeyEvent.isSystemKey:
72        // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
73        if (keyCode == KeyEvent.KEYCODE_MENU ||
74            keyCode == KeyEvent.KEYCODE_HOME ||
75            keyCode == KeyEvent.KEYCODE_BACK ||
76            keyCode == KeyEvent.KEYCODE_CALL ||
77            keyCode == KeyEvent.KEYCODE_ENDCALL ||
78            keyCode == KeyEvent.KEYCODE_POWER ||
79            keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
80            keyCode == KeyEvent.KEYCODE_CAMERA ||
81            keyCode == KeyEvent.KEYCODE_FOCUS ||
82            keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
83            keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
84            keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
85            return true;
86        }
87
88        // We also have to intercept some shortcuts before we send them to the ContentView.
89        if (event.isCtrlPressed() && (
90                keyCode == KeyEvent.KEYCODE_TAB ||
91                keyCode == KeyEvent.KEYCODE_W ||
92                keyCode == KeyEvent.KEYCODE_F4)) {
93            return true;
94        }
95
96        return false;
97    }
98
99    // Called when an ImeEvent is sent to the page. Can be used to know when some text is entered
100    // in a page.
101    public void onImeEvent() {
102    }
103
104    /**
105     * Notified when a change to the IME was requested.
106     *
107     * @param requestShow Whether the IME was requested to be shown (may already be showing
108     *                    though).
109     */
110    public void onImeStateChangeRequested(boolean requestShow) {
111    }
112
113    // TODO (dtrainor): Should expose getScrollX/Y from ContentView or make
114    // computeHorizontalScrollOffset()/computeVerticalScrollOffset() public.
115    /**
116     * Gives the UI the chance to override each scroll event.
117     * @param dx The amount scrolled in the X direction (in physical pixels).
118     * @param dy The amount scrolled in the Y direction (in physical pixels).
119     * @param scrollX The current X scroll offset (in physical pixels).
120     * @param scrollY The current Y scroll offset (in physical pixels).
121     * @return Whether or not the UI consumed and handled this event.
122     */
123    public boolean shouldOverrideScroll(float dx, float dy, float scrollX, float scrollY) {
124        return false;
125    }
126
127    /**
128     * Returns an ActionMode.Callback for in-page selection.
129     */
130    public ActionMode.Callback getSelectActionModeCallback(
131            Context context, ActionHandler actionHandler, boolean incognito) {
132        return new SelectActionModeCallback(context, actionHandler, incognito);
133    }
134
135    /**
136     * Called when the contextual ActionBar is shown.
137     */
138    public void onContextualActionBarShown() {
139    }
140
141    /**
142     * Called when the contextual ActionBar is hidden.
143     */
144    public void onContextualActionBarHidden() {
145    }
146
147    /**
148     * Called when a new content intent is requested to be started.
149     */
150    public void onStartContentIntent(Context context, String intentUrl) {
151        Intent intent;
152        // Perform generic parsing of the URI to turn it into an Intent.
153        try {
154            intent = Intent.parseUri(intentUrl, Intent.URI_INTENT_SCHEME);
155        } catch (URISyntaxException ex) {
156            Log.w(TAG, "Bad URI " + intentUrl + ": " + ex.getMessage());
157            return;
158        }
159
160        try {
161            context.startActivity(intent);
162        } catch (ActivityNotFoundException ex) {
163            Log.w(TAG, "No application can handle " + intentUrl);
164        }
165    }
166
167    public void onExternalVideoSurfaceRequested(int playerId) {
168    }
169
170    public void onGeometryChanged(int playerId, RectF rect) {
171    }
172
173    public ContentVideoViewClient getContentVideoViewClient() {
174        return null;
175    }
176}
177