ContentViewClient.java revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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 // We need to send almost every key to WebKit. However: 57 // 1. We don't want to block the device on the renderer for 58 // some keys like menu, home, call. 59 // 2. There are no WebKit equivalents for some of these keys 60 // (see app/keyboard_codes_win.h) 61 // Note that these are not the same set as KeyEvent.isSystemKey: 62 // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit. 63 if (keyCode == KeyEvent.KEYCODE_MENU || 64 keyCode == KeyEvent.KEYCODE_HOME || 65 keyCode == KeyEvent.KEYCODE_BACK || 66 keyCode == KeyEvent.KEYCODE_CALL || 67 keyCode == KeyEvent.KEYCODE_ENDCALL || 68 keyCode == KeyEvent.KEYCODE_POWER || 69 keyCode == KeyEvent.KEYCODE_HEADSETHOOK || 70 keyCode == KeyEvent.KEYCODE_CAMERA || 71 keyCode == KeyEvent.KEYCODE_FOCUS || 72 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || 73 keyCode == KeyEvent.KEYCODE_VOLUME_MUTE || 74 keyCode == KeyEvent.KEYCODE_VOLUME_UP) { 75 return true; 76 } 77 78 // We also have to intercept some shortcuts before we send them to the ContentView. 79 if (event.isCtrlPressed() && ( 80 keyCode == KeyEvent.KEYCODE_TAB || 81 keyCode == KeyEvent.KEYCODE_W || 82 keyCode == KeyEvent.KEYCODE_F4)) { 83 return true; 84 } 85 86 return false; 87 } 88 89 /** 90 * Called when an ImeEvent is sent to the page. Can be used to know when some text is entered 91 * in a page. 92 */ 93 public void onImeEvent() { 94 } 95 96 /** 97 * Notified when a change to the IME was requested. 98 * 99 * @param requestShow Whether the IME was requested to be shown (may already be showing 100 * though). 101 */ 102 public void onImeStateChangeRequested(boolean requestShow) { 103 } 104 105 /** 106 * Returns an ActionMode.Callback for in-page selection. 107 */ 108 public ActionMode.Callback getSelectActionModeCallback( 109 Context context, ActionHandler actionHandler, boolean incognito) { 110 return new SelectActionModeCallback(context, actionHandler, incognito); 111 } 112 113 /** 114 * Called when the contextual ActionBar is shown. 115 */ 116 public void onContextualActionBarShown() { 117 } 118 119 /** 120 * Called when the contextual ActionBar is hidden. 121 */ 122 public void onContextualActionBarHidden() { 123 } 124 125 /** 126 * Perform a search on {@code searchQuery}. This method is only called if 127 * {@link #doesPerformWebSearch()} returns {@code true}. 128 * @param searchQuery The string to search for. 129 */ 130 public void performWebSearch(String searchQuery) { 131 } 132 133 /** 134 * If this returns {@code true} contextual web search attempts will be forwarded to 135 * {@link #performWebSearch(String)}. 136 * @return {@code true} iff this {@link ContentViewClient} wants to consume web search queries 137 * and override the default intent behavior. 138 */ 139 public boolean doesPerformWebSearch() { 140 return false; 141 } 142 143 /** 144 * Called when a new content intent is requested to be started. 145 */ 146 public void onStartContentIntent(Context context, String intentUrl) { 147 Intent intent; 148 // Perform generic parsing of the URI to turn it into an Intent. 149 try { 150 intent = Intent.parseUri(intentUrl, Intent.URI_INTENT_SCHEME); 151 } catch (URISyntaxException ex) { 152 Log.w(TAG, "Bad URI " + intentUrl + ": " + ex.getMessage()); 153 return; 154 } 155 156 try { 157 context.startActivity(intent); 158 } catch (ActivityNotFoundException ex) { 159 Log.w(TAG, "No application can handle " + intentUrl); 160 } 161 } 162 163 public ContentVideoViewClient getContentVideoViewClient() { 164 return null; 165 } 166 167 /** 168 * Called when BrowserMediaPlayerManager wants to load a media resource. 169 * @param url the URL of media resource to load. 170 * @return true to prevent the resource from being loaded. 171 */ 172 public boolean shouldBlockMediaRequest(String url) { 173 return false; 174 } 175 176} 177