Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
luxiaol | b40014b | 2013-07-19 10:01:43 +0800 | [diff] [blame] | 20 | import android.app.AlertDialog; |
John Reck | 68234a9 | 2012-04-19 15:27:12 -0700 | [diff] [blame] | 21 | import android.app.Dialog; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 22 | import android.app.DownloadManager; |
John Reck | 68234a9 | 2012-04-19 15:27:12 -0700 | [diff] [blame] | 23 | import android.app.ProgressDialog; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 24 | import android.content.ClipboardManager; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 25 | import android.content.ContentResolver; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 26 | import android.content.ContentUris; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 27 | import android.content.ContentValues; |
| 28 | import android.content.Context; |
John Reck | 68234a9 | 2012-04-19 15:27:12 -0700 | [diff] [blame] | 29 | import android.content.DialogInterface; |
| 30 | import android.content.DialogInterface.OnCancelListener; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 31 | import android.content.Intent; |
| 32 | import android.content.pm.PackageManager; |
| 33 | import android.content.pm.ResolveInfo; |
| 34 | import android.content.res.Configuration; |
John Reck | 30b065e | 2011-07-19 10:58:05 -0700 | [diff] [blame] | 35 | import android.content.res.TypedArray; |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 36 | import android.database.ContentObserver; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 37 | import android.database.Cursor; |
| 38 | import android.database.sqlite.SQLiteDatabase; |
Mattias Nilsson | 561d195 | 2011-10-04 10:18:50 +0200 | [diff] [blame] | 39 | import android.database.sqlite.SQLiteException; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 40 | import android.graphics.Bitmap; |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 41 | import android.graphics.BitmapFactory; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 42 | import android.graphics.Canvas; |
kaiyiz | a016da1 | 2013-08-26 17:50:22 +0800 | [diff] [blame] | 43 | import android.net.ConnectivityManager; |
| 44 | import android.net.NetworkInfo; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 45 | import android.net.Uri; |
| 46 | import android.net.http.SslError; |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 47 | import android.net.WebAddress; |
kaiyiz | a016da1 | 2013-08-26 17:50:22 +0800 | [diff] [blame] | 48 | import android.net.wifi.WifiManager; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 49 | import android.os.AsyncTask; |
| 50 | import android.os.Bundle; |
George Mount | 387d45d | 2011-10-07 15:57:53 -0700 | [diff] [blame] | 51 | import android.os.Environment; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 52 | import android.os.Handler; |
| 53 | import android.os.Message; |
| 54 | import android.os.PowerManager; |
| 55 | import android.os.PowerManager.WakeLock; |
kaiyiz | a8b6dbb | 2013-07-29 18:11:22 +0800 | [diff] [blame] | 56 | import android.os.SystemProperties; |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 57 | import android.preference.PreferenceActivity; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 58 | import android.provider.Browser; |
| 59 | import android.provider.BrowserContract; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 60 | import android.provider.BrowserContract.Images; |
| 61 | import android.provider.ContactsContract; |
| 62 | import android.provider.ContactsContract.Intents.Insert; |
kaiyiz | a016da1 | 2013-08-26 17:50:22 +0800 | [diff] [blame] | 63 | import android.provider.Settings; |
Michael Kolb | 0b12912 | 2012-06-04 16:31:58 -0700 | [diff] [blame] | 64 | import android.speech.RecognizerIntent; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 65 | import android.text.TextUtils; |
| 66 | import android.util.Log; |
John Reck | a00cbbd | 2010-12-16 12:38:19 -0800 | [diff] [blame] | 67 | import android.util.Patterns; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 68 | import android.view.ActionMode; |
| 69 | import android.view.ContextMenu; |
| 70 | import android.view.ContextMenu.ContextMenuInfo; |
| 71 | import android.view.Gravity; |
| 72 | import android.view.KeyEvent; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 73 | import android.view.Menu; |
| 74 | import android.view.MenuInflater; |
| 75 | import android.view.MenuItem; |
| 76 | import android.view.MenuItem.OnMenuItemClickListener; |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 77 | import android.view.MotionEvent; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 78 | import android.view.View; |
| 79 | import android.webkit.CookieManager; |
| 80 | import android.webkit.CookieSyncManager; |
| 81 | import android.webkit.HttpAuthHandler; |
George Mount | 387d45d | 2011-10-07 15:57:53 -0700 | [diff] [blame] | 82 | import android.webkit.MimeTypeMap; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 83 | import android.webkit.SslErrorHandler; |
| 84 | import android.webkit.ValueCallback; |
| 85 | import android.webkit.WebChromeClient; |
| 86 | import android.webkit.WebIconDatabase; |
| 87 | import android.webkit.WebSettings; |
| 88 | import android.webkit.WebView; |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 89 | import android.webkit.WebViewClassic; |
Leon Scroggins | ac99384 | 2011-02-02 12:54:07 -0500 | [diff] [blame] | 90 | import android.widget.Toast; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 91 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 92 | import com.android.browser.IntentHandler.UrlData; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 93 | import com.android.browser.UI.ComboViews; |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 94 | import com.android.browser.provider.BrowserProvider2.Thumbnails; |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 95 | import com.android.browser.provider.SnapshotProvider.Snapshots; |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 96 | import com.android.browser.mynavigation.AddMyNavigationPage; |
| 97 | import com.android.browser.mynavigation.MyNavigationUtil; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 98 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 99 | import java.io.ByteArrayOutputStream; |
George Mount | 387d45d | 2011-10-07 15:57:53 -0700 | [diff] [blame] | 100 | import java.io.File; |
| 101 | import java.io.FileOutputStream; |
| 102 | import java.io.IOException; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 103 | import java.net.URLEncoder; |
George Mount | 387d45d | 2011-10-07 15:57:53 -0700 | [diff] [blame] | 104 | import java.text.DateFormat; |
| 105 | import java.text.SimpleDateFormat; |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 106 | import java.util.ArrayList; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 107 | import java.util.Calendar; |
George Mount | 387d45d | 2011-10-07 15:57:53 -0700 | [diff] [blame] | 108 | import java.util.Date; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 109 | import java.util.HashMap; |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 110 | import java.util.List; |
Johan Redestig | aa67618 | 2012-10-03 13:33:01 +0200 | [diff] [blame] | 111 | import java.util.Locale; |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 112 | import java.util.Map; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Controller for browser |
| 116 | */ |
| 117 | public class Controller |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 118 | implements WebViewController, UiController, ActivityController { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 119 | |
| 120 | private static final String LOGTAG = "Controller"; |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 121 | private static final String SEND_APP_ID_EXTRA = |
| 122 | "android.speech.extras.SEND_APPLICATION_ID_EXTRA"; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 123 | private static final String INCOGNITO_URI = "browser:incognito"; |
kaiyiz | a016da1 | 2013-08-26 17:50:22 +0800 | [diff] [blame] | 124 | private static final String PROP_NETSWITCH = "persist.env.browser.netswitch"; |
| 125 | private static final String INTENT_WIFI_SELECTION_DATA_CONNECTION = |
| 126 | "android.net.wifi.cmcc.WIFI_SELECTION_DATA_CONNECTION"; |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 127 | private static final String OFFLINE_PAGE = |
| 128 | "content://com.android.browser.mynavigation/websites"; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 129 | |
| 130 | // public message ids |
| 131 | public final static int LOAD_URL = 1001; |
| 132 | public final static int STOP_LOAD = 1002; |
| 133 | |
| 134 | // Message Ids |
| 135 | private static final int FOCUS_NODE_HREF = 102; |
| 136 | private static final int RELEASE_WAKELOCK = 107; |
| 137 | |
| 138 | static final int UPDATE_BOOKMARK_THUMBNAIL = 108; |
| 139 | |
| 140 | private static final int OPEN_BOOKMARKS = 201; |
kaiyiz | 591110b | 2013-08-06 17:11:06 +0800 | [diff] [blame] | 141 | private static final int OPEN_MENU = 202; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 142 | |
| 143 | private static final int EMPTY_MENU = -1; |
| 144 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 145 | // activity requestCode |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 146 | final static int COMBO_VIEW = 1; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 147 | final static int PREFERENCES_PAGE = 3; |
| 148 | final static int FILE_SELECTED = 4; |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 149 | final static int AUTOFILL_SETUP = 5; |
Michael Kolb | 0b12912 | 2012-06-04 16:31:58 -0700 | [diff] [blame] | 150 | final static int VOICE_RESULT = 6; |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 151 | final static int MY_NAVIGATION = 7; |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 152 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 153 | private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes |
| 154 | |
| 155 | // As the ids are dynamically created, we can't guarantee that they will |
| 156 | // be in sequence, so this static array maps ids to a window number. |
| 157 | final static private int[] WINDOW_SHORTCUT_ID_ARRAY = |
| 158 | { R.id.window_one_menu_id, R.id.window_two_menu_id, |
| 159 | R.id.window_three_menu_id, R.id.window_four_menu_id, |
| 160 | R.id.window_five_menu_id, R.id.window_six_menu_id, |
| 161 | R.id.window_seven_menu_id, R.id.window_eight_menu_id }; |
| 162 | |
| 163 | // "source" parameter for Google search through search key |
| 164 | final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key"; |
| 165 | // "source" parameter for Google search through simplily type |
| 166 | final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type"; |
| 167 | |
George Mount | 387d45d | 2011-10-07 15:57:53 -0700 | [diff] [blame] | 168 | // "no-crash-recovery" parameter in intent to suppress crash recovery |
Guang Zhu | 9e78f51 | 2011-05-04 11:45:11 -0700 | [diff] [blame] | 169 | final static String NO_CRASH_RECOVERY = "no-crash-recovery"; |
| 170 | |
John Reck | d7dd9b2 | 2011-08-30 09:18:29 -0700 | [diff] [blame] | 171 | // A bitmap that is re-used in createScreenshot as scratch space |
| 172 | private static Bitmap sThumbnailBitmap; |
| 173 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 174 | private Activity mActivity; |
| 175 | private UI mUi; |
| 176 | private TabControl mTabControl; |
| 177 | private BrowserSettings mSettings; |
| 178 | private WebViewFactory mFactory; |
| 179 | |
| 180 | private WakeLock mWakeLock; |
| 181 | |
| 182 | private UrlHandler mUrlHandler; |
| 183 | private UploadHandler mUploadHandler; |
| 184 | private IntentHandler mIntentHandler; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 185 | private PageDialogsHandler mPageDialogsHandler; |
| 186 | private NetworkStateHandler mNetworkHandler; |
| 187 | |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 188 | private Message mAutoFillSetupMessage; |
| 189 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 190 | private boolean mShouldShowErrorConsole; |
kaiyiz | a016da1 | 2013-08-26 17:50:22 +0800 | [diff] [blame] | 191 | private boolean mNetworkShouldNotify = true; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 192 | |
| 193 | private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins; |
| 194 | |
| 195 | // FIXME, temp address onPrepareMenu performance problem. |
| 196 | // When we move everything out of view, we should rewrite this. |
| 197 | private int mCurrentMenuState = 0; |
| 198 | private int mMenuState = R.id.MAIN_MENU; |
| 199 | private int mOldMenuState = EMPTY_MENU; |
| 200 | private Menu mCachedMenu; |
| 201 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 202 | private boolean mMenuIsDown; |
| 203 | |
| 204 | // For select and find, we keep track of the ActionMode so that |
| 205 | // finish() can be called as desired. |
| 206 | private ActionMode mActionMode; |
| 207 | |
| 208 | /** |
| 209 | * Only meaningful when mOptionsMenuOpen is true. This variable keeps track |
| 210 | * of whether the configuration has changed. The first onMenuOpened call |
| 211 | * after a configuration change is simply a reopening of the same menu |
| 212 | * (i.e. mIconView did not change). |
| 213 | */ |
| 214 | private boolean mConfigChanged; |
| 215 | |
| 216 | /** |
| 217 | * Keeps track of whether the options menu is open. This is important in |
| 218 | * determining whether to show or hide the title bar overlay |
| 219 | */ |
| 220 | private boolean mOptionsMenuOpen; |
| 221 | |
| 222 | /** |
| 223 | * Whether or not the options menu is in its bigger, popup menu form. When |
| 224 | * true, we want the title bar overlay to be gone. When false, we do not. |
| 225 | * Only meaningful if mOptionsMenuOpen is true. |
| 226 | */ |
| 227 | private boolean mExtendedMenuOpen; |
| 228 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 229 | private boolean mActivityPaused = true; |
| 230 | private boolean mLoadStopped; |
| 231 | |
| 232 | private Handler mHandler; |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 233 | // Checks to see when the bookmarks database has changed, and updates the |
| 234 | // Tabs' notion of whether they represent bookmarked sites. |
| 235 | private ContentObserver mBookmarksObserver; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 236 | private CrashRecoveryHandler mCrashRecoveryHandler; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 237 | |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 238 | private boolean mBlockEvents; |
| 239 | |
Michael Kolb | 0b12912 | 2012-06-04 16:31:58 -0700 | [diff] [blame] | 240 | private String mVoiceResult; |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 241 | private boolean mUpdateMyNavThumbnail; |
| 242 | private String mUpdateMyNavThumbnailUrl; |
Michael Kolb | 0b12912 | 2012-06-04 16:31:58 -0700 | [diff] [blame] | 243 | |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 244 | public Controller(Activity browser) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 245 | mActivity = browser; |
| 246 | mSettings = BrowserSettings.getInstance(); |
| 247 | mTabControl = new TabControl(this); |
| 248 | mSettings.setController(this); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 249 | mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this); |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 250 | mCrashRecoveryHandler.preloadCrashState(); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 251 | mFactory = new BrowserWebViewFactory(browser); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 252 | |
| 253 | mUrlHandler = new UrlHandler(this); |
| 254 | mIntentHandler = new IntentHandler(mActivity, this); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 255 | mPageDialogsHandler = new PageDialogsHandler(mActivity, this); |
| 256 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 257 | startHandler(); |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 258 | mBookmarksObserver = new ContentObserver(mHandler) { |
| 259 | @Override |
| 260 | public void onChange(boolean selfChange) { |
| 261 | int size = mTabControl.getTabCount(); |
| 262 | for (int i = 0; i < size; i++) { |
| 263 | mTabControl.getTab(i).updateBookmarkedStatus(); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | }; |
| 268 | browser.getContentResolver().registerContentObserver( |
| 269 | BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 270 | |
| 271 | mNetworkHandler = new NetworkStateHandler(mActivity, this); |
| 272 | // Start watching the default geolocation permissions |
| 273 | mSystemAllowGeolocationOrigins = |
| 274 | new SystemAllowGeolocationOrigins(mActivity.getApplicationContext()); |
| 275 | mSystemAllowGeolocationOrigins.start(); |
| 276 | |
John Reck | af262e7 | 2011-07-25 13:55:44 -0700 | [diff] [blame] | 277 | openIconDatabase(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 278 | } |
| 279 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 280 | @Override |
| 281 | public void start(final Intent intent) { |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 282 | if (BrowserWebView.isClassic()) WebViewClassic.setShouldMonitorWebCoreThread(); |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 283 | // mCrashRecoverHandler has any previously saved state. |
| 284 | mCrashRecoveryHandler.startRecovery(intent); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 285 | } |
| 286 | |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 287 | void doStart(final Bundle icicle, final Intent intent) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 288 | // Unless the last browser usage was within 24 hours, destroy any |
| 289 | // remaining incognito tabs. |
| 290 | |
| 291 | Calendar lastActiveDate = icicle != null ? |
| 292 | (Calendar) icicle.getSerializable("lastActiveDate") : null; |
| 293 | Calendar today = Calendar.getInstance(); |
| 294 | Calendar yesterday = Calendar.getInstance(); |
| 295 | yesterday.add(Calendar.DATE, -1); |
| 296 | |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 297 | final boolean restoreIncognitoTabs = !(lastActiveDate == null |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 298 | || lastActiveDate.before(yesterday) |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 299 | || lastActiveDate.after(today)); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 300 | |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 301 | // Find out if we will restore any state and remember the tab. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 302 | final long currentTabId = |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 303 | mTabControl.canRestoreState(icicle, restoreIncognitoTabs); |
Kristian Monsen | 2cd9701 | 2010-12-07 11:11:40 +0000 | [diff] [blame] | 304 | |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 305 | if (currentTabId == -1) { |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 306 | // Not able to restore so we go ahead and clear session cookies. We |
| 307 | // must do this before trying to login the user as we don't want to |
| 308 | // clear any session cookies set during login. |
| 309 | CookieManager.getInstance().removeSessionCookie(); |
| 310 | } |
| 311 | |
Patrick Scott | d43e75a | 2011-03-14 14:47:23 -0400 | [diff] [blame] | 312 | GoogleAccountLogin.startLoginIfNeeded(mActivity, |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 313 | new Runnable() { |
| 314 | @Override public void run() { |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 315 | onPreloginFinished(icicle, intent, currentTabId, |
| 316 | restoreIncognitoTabs); |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 317 | } |
| 318 | }); |
| 319 | } |
| 320 | |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 321 | private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId, |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 322 | boolean restoreIncognitoTabs) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 323 | if (currentTabId == -1) { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 324 | BackgroundHandler.execute(new PruneThumbnails(mActivity, null)); |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 325 | if (intent == null) { |
| 326 | // This won't happen under common scenarios. The icicle is |
| 327 | // not null, but there aren't any tabs to restore. |
| 328 | openTabToHomePage(); |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 329 | } else { |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 330 | final Bundle extra = intent.getExtras(); |
| 331 | // Create an initial tab. |
| 332 | // If the intent is ACTION_VIEW and data is not null, the Browser is |
| 333 | // invoked to view the content by another application. In this case, |
| 334 | // the tab will be close when exit. |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 335 | UrlData urlData = null; |
| 336 | if (intent.getData() != null |
| 337 | && Intent.ACTION_VIEW.equals(intent.getAction()) |
| 338 | && intent.getData().toString().startsWith("content://")) { |
| 339 | urlData = new UrlData(intent.getData().toString()); |
| 340 | } else { |
| 341 | urlData = IntentHandler.getUrlDataFromIntent(intent); |
| 342 | } |
| 343 | |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 344 | Tab t = null; |
| 345 | if (urlData.isEmpty()) { |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 346 | if (SystemProperties.get("persist.env.c.browser.resource", "default").equals( |
| 347 | "cmcc")) { |
| 348 | t = openTab(OFFLINE_PAGE, false, true, true); |
| 349 | } else { |
| 350 | t = openTabToHomePage(); |
| 351 | } |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 352 | } else { |
| 353 | t = openTab(urlData); |
| 354 | } |
| 355 | if (t != null) { |
| 356 | t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID)); |
| 357 | } |
| 358 | WebView webView = t.getWebView(); |
| 359 | if (extra != null) { |
| 360 | int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0); |
| 361 | if (scale > 0 && scale <= 1000) { |
| 362 | webView.setInitialScale(scale); |
| 363 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 366 | mUi.updateTabs(mTabControl.getTabs()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 367 | } else { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 368 | mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs, |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 369 | mUi.needsRestoreAllTabs()); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 370 | List<Tab> tabs = mTabControl.getTabs(); |
| 371 | ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size()); |
| 372 | for (Tab t : tabs) { |
| 373 | restoredTabs.add(t.getId()); |
| 374 | } |
| 375 | BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs)); |
John Reck | 52be478 | 2011-08-26 15:37:29 -0700 | [diff] [blame] | 376 | if (tabs.size() == 0) { |
| 377 | openTabToHomePage(); |
| 378 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 379 | mUi.updateTabs(tabs); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 380 | // TabControl.restoreState() will create a new tab even if |
| 381 | // restoring the state fails. |
| 382 | setActiveTab(mTabControl.getCurrentTab()); |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 383 | // Intent is non-null when framework thinks the browser should be |
| 384 | // launching with a new intent (icicle is null). |
| 385 | if (intent != null) { |
John Reck | 9dd4a41 | 2011-10-05 14:55:30 -0700 | [diff] [blame] | 386 | mIntentHandler.onNewIntent(intent); |
| 387 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 388 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 389 | // Read JavaScript flags if it exists. |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 390 | String jsFlags = getSettings().getJsEngineFlags(); |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 391 | if (jsFlags.trim().length() != 0 && BrowserWebView.isClassic()) { |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 392 | WebViewClassic.fromWebView(getCurrentWebView()).setJsFlags(jsFlags); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 393 | } |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 394 | if (intent != null |
| 395 | && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) { |
Michael Kolb | 315d502 | 2011-10-13 12:47:11 -0700 | [diff] [blame] | 396 | bookmarksOrHistoryPicker(ComboViews.Bookmarks); |
John Reck | 439c9a5 | 2010-12-14 10:04:39 -0800 | [diff] [blame] | 397 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 398 | } |
| 399 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 400 | private static class PruneThumbnails implements Runnable { |
| 401 | private Context mContext; |
| 402 | private List<Long> mIds; |
| 403 | |
| 404 | PruneThumbnails(Context context, List<Long> preserveIds) { |
| 405 | mContext = context.getApplicationContext(); |
| 406 | mIds = preserveIds; |
| 407 | } |
| 408 | |
| 409 | @Override |
| 410 | public void run() { |
| 411 | ContentResolver cr = mContext.getContentResolver(); |
| 412 | if (mIds == null || mIds.size() == 0) { |
| 413 | cr.delete(Thumbnails.CONTENT_URI, null, null); |
| 414 | } else { |
| 415 | int length = mIds.size(); |
| 416 | StringBuilder where = new StringBuilder(); |
| 417 | where.append(Thumbnails._ID); |
| 418 | where.append(" not in ("); |
| 419 | for (int i = 0; i < length; i++) { |
| 420 | where.append(mIds.get(i)); |
| 421 | if (i < (length - 1)) { |
| 422 | where.append(","); |
| 423 | } |
| 424 | } |
| 425 | where.append(")"); |
| 426 | cr.delete(Thumbnails.CONTENT_URI, where.toString(), null); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | } |
| 431 | |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 432 | @Override |
| 433 | public WebViewFactory getWebViewFactory() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 434 | return mFactory; |
| 435 | } |
| 436 | |
| 437 | @Override |
Michael Kolb | a713ec8 | 2010-11-29 17:27:06 -0800 | [diff] [blame] | 438 | public void onSetWebView(Tab tab, WebView view) { |
| 439 | mUi.onSetWebView(tab, view); |
| 440 | } |
| 441 | |
| 442 | @Override |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 443 | public void createSubWindow(Tab tab) { |
| 444 | endActionMode(); |
| 445 | WebView mainView = tab.getWebView(); |
| 446 | WebView subView = mFactory.createWebView((mainView == null) |
| 447 | ? false |
| 448 | : mainView.isPrivateBrowsingEnabled()); |
| 449 | mUi.createSubWindow(tab, subView); |
| 450 | } |
| 451 | |
| 452 | @Override |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 453 | public Context getContext() { |
| 454 | return mActivity; |
| 455 | } |
| 456 | |
| 457 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 458 | public Activity getActivity() { |
| 459 | return mActivity; |
| 460 | } |
| 461 | |
| 462 | void setUi(UI ui) { |
| 463 | mUi = ui; |
| 464 | } |
| 465 | |
Michael Kolb | af63dba | 2012-05-16 12:58:05 -0700 | [diff] [blame] | 466 | @Override |
| 467 | public BrowserSettings getSettings() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 468 | return mSettings; |
| 469 | } |
| 470 | |
| 471 | IntentHandler getIntentHandler() { |
| 472 | return mIntentHandler; |
| 473 | } |
| 474 | |
| 475 | @Override |
| 476 | public UI getUi() { |
| 477 | return mUi; |
| 478 | } |
| 479 | |
| 480 | int getMaxTabs() { |
| 481 | return mActivity.getResources().getInteger(R.integer.max_tabs); |
| 482 | } |
| 483 | |
| 484 | @Override |
| 485 | public TabControl getTabControl() { |
| 486 | return mTabControl; |
| 487 | } |
| 488 | |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 489 | @Override |
| 490 | public List<Tab> getTabs() { |
| 491 | return mTabControl.getTabs(); |
| 492 | } |
| 493 | |
John Reck | af262e7 | 2011-07-25 13:55:44 -0700 | [diff] [blame] | 494 | // Open the icon database. |
| 495 | private void openIconDatabase() { |
| 496 | // We have to call getInstance on the UI thread |
| 497 | final WebIconDatabase instance = WebIconDatabase.getInstance(); |
| 498 | BackgroundHandler.execute(new Runnable() { |
Ben Murdoch | 9446b93 | 2010-11-25 16:20:14 +0000 | [diff] [blame] | 499 | |
John Reck | af262e7 | 2011-07-25 13:55:44 -0700 | [diff] [blame] | 500 | @Override |
| 501 | public void run() { |
| 502 | instance.open(mActivity.getDir("icons", 0).getPath()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 503 | } |
John Reck | af262e7 | 2011-07-25 13:55:44 -0700 | [diff] [blame] | 504 | }); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | private void startHandler() { |
| 508 | mHandler = new Handler() { |
| 509 | |
| 510 | @Override |
| 511 | public void handleMessage(Message msg) { |
| 512 | switch (msg.what) { |
| 513 | case OPEN_BOOKMARKS: |
Michael Kolb | 315d502 | 2011-10-13 12:47:11 -0700 | [diff] [blame] | 514 | bookmarksOrHistoryPicker(ComboViews.Bookmarks); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 515 | break; |
| 516 | case FOCUS_NODE_HREF: |
| 517 | { |
| 518 | String url = (String) msg.getData().get("url"); |
| 519 | String title = (String) msg.getData().get("title"); |
Cary Clark | 043c2d6 | 2010-12-15 11:19:39 -0500 | [diff] [blame] | 520 | String src = (String) msg.getData().get("src"); |
| 521 | if (url == "") url = src; // use image if no anchor |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 522 | if (TextUtils.isEmpty(url)) { |
| 523 | break; |
| 524 | } |
| 525 | HashMap focusNodeMap = (HashMap) msg.obj; |
| 526 | WebView view = (WebView) focusNodeMap.get("webview"); |
| 527 | // Only apply the action if the top window did not change. |
| 528 | if (getCurrentTopWebView() != view) { |
| 529 | break; |
| 530 | } |
| 531 | switch (msg.arg1) { |
| 532 | case R.id.open_context_menu_id: |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 533 | loadUrlFromContext(url); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 534 | break; |
Cary Clark | 043c2d6 | 2010-12-15 11:19:39 -0500 | [diff] [blame] | 535 | case R.id.view_image_context_menu_id: |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 536 | loadUrlFromContext(src); |
Cary Clark | 043c2d6 | 2010-12-15 11:19:39 -0500 | [diff] [blame] | 537 | break; |
Leon Scroggins | 026f254 | 2010-11-22 13:26:12 -0500 | [diff] [blame] | 538 | case R.id.open_newtab_context_menu_id: |
| 539 | final Tab parent = mTabControl.getCurrentTab(); |
John Reck | 5949c66 | 2011-05-27 09:52:29 -0700 | [diff] [blame] | 540 | openTab(url, parent, |
| 541 | !mSettings.openInBackground(), true); |
Leon Scroggins | 026f254 | 2010-11-22 13:26:12 -0500 | [diff] [blame] | 542 | break; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 543 | case R.id.copy_link_context_menu_id: |
| 544 | copy(url); |
| 545 | break; |
| 546 | case R.id.save_link_context_menu_id: |
| 547 | case R.id.download_context_menu_id: |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 548 | DownloadHandler.onDownloadStartNoStream( |
Andreas Sandblad | 8e4ce66 | 2012-06-11 11:02:49 +0200 | [diff] [blame] | 549 | mActivity, url, view.getSettings().getUserAgentString(), |
luxiaol | 62677b0 | 2013-07-22 07:54:49 +0800 | [diff] [blame] | 550 | null, null, null, view.isPrivateBrowsingEnabled(), 0); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 551 | break; |
| 552 | } |
| 553 | break; |
| 554 | } |
| 555 | |
| 556 | case LOAD_URL: |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 557 | loadUrlFromContext((String) msg.obj); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 558 | break; |
| 559 | |
| 560 | case STOP_LOAD: |
| 561 | stopLoading(); |
| 562 | break; |
| 563 | |
| 564 | case RELEASE_WAKELOCK: |
John Reck | f57c029 | 2011-07-21 18:15:39 -0700 | [diff] [blame] | 565 | if (mWakeLock != null && mWakeLock.isHeld()) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 566 | mWakeLock.release(); |
| 567 | // if we reach here, Browser should be still in the |
| 568 | // background loading after WAKELOCK_TIMEOUT (5-min). |
| 569 | // To avoid burning the battery, stop loading. |
| 570 | mTabControl.stopAllLoading(); |
| 571 | } |
| 572 | break; |
| 573 | |
| 574 | case UPDATE_BOOKMARK_THUMBNAIL: |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 575 | Tab tab = (Tab) msg.obj; |
| 576 | if (tab != null) { |
| 577 | updateScreenshot(tab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 578 | } |
| 579 | break; |
kaiyiz | 591110b | 2013-08-06 17:11:06 +0800 | [diff] [blame] | 580 | |
| 581 | case OPEN_MENU: |
| 582 | if (!mOptionsMenuOpen && mActivity != null ) { |
| 583 | mActivity.openOptionsMenu(); |
| 584 | } |
| 585 | break; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | }; |
| 589 | |
| 590 | } |
| 591 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 592 | @Override |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 593 | public Tab getCurrentTab() { |
| 594 | return mTabControl.getCurrentTab(); |
| 595 | } |
| 596 | |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 597 | @Override |
| 598 | public void shareCurrentPage() { |
| 599 | shareCurrentPage(mTabControl.getCurrentTab()); |
| 600 | } |
| 601 | |
| 602 | private void shareCurrentPage(Tab tab) { |
| 603 | if (tab != null) { |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 604 | sharePage(mActivity, tab.getTitle(), |
| 605 | tab.getUrl(), tab.getFavicon(), |
| 606 | createScreenshot(tab.getWebView(), |
| 607 | getDesiredThumbnailWidth(mActivity), |
| 608 | getDesiredThumbnailHeight(mActivity))); |
| 609 | } |
| 610 | } |
| 611 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 612 | /** |
| 613 | * Share a page, providing the title, url, favicon, and a screenshot. Uses |
| 614 | * an {@link Intent} to launch the Activity chooser. |
| 615 | * @param c Context used to launch a new Activity. |
| 616 | * @param title Title of the page. Stored in the Intent with |
| 617 | * {@link Intent#EXTRA_SUBJECT} |
| 618 | * @param url URL of the page. Stored in the Intent with |
| 619 | * {@link Intent#EXTRA_TEXT} |
| 620 | * @param favicon Bitmap of the favicon for the page. Stored in the Intent |
| 621 | * with {@link Browser#EXTRA_SHARE_FAVICON} |
| 622 | * @param screenshot Bitmap of a screenshot of the page. Stored in the |
| 623 | * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT} |
| 624 | */ |
| 625 | static final void sharePage(Context c, String title, String url, |
| 626 | Bitmap favicon, Bitmap screenshot) { |
| 627 | Intent send = new Intent(Intent.ACTION_SEND); |
| 628 | send.setType("text/plain"); |
| 629 | send.putExtra(Intent.EXTRA_TEXT, url); |
| 630 | send.putExtra(Intent.EXTRA_SUBJECT, title); |
| 631 | send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon); |
| 632 | send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot); |
| 633 | try { |
| 634 | c.startActivity(Intent.createChooser(send, c.getString( |
| 635 | R.string.choosertitle_sharevia))); |
| 636 | } catch(android.content.ActivityNotFoundException ex) { |
| 637 | // if no app handles it, do nothing |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | private void copy(CharSequence text) { |
| 642 | ClipboardManager cm = (ClipboardManager) mActivity |
| 643 | .getSystemService(Context.CLIPBOARD_SERVICE); |
| 644 | cm.setText(text); |
| 645 | } |
| 646 | |
| 647 | // lifecycle |
| 648 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 649 | @Override |
| 650 | public void onConfgurationChanged(Configuration config) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 651 | mConfigChanged = true; |
Michael Kolb | 18f252f | 2012-03-06 13:36:20 -0800 | [diff] [blame] | 652 | // update the menu in case of a locale change |
| 653 | mActivity.invalidateOptionsMenu(); |
kaiyiz | 591110b | 2013-08-06 17:11:06 +0800 | [diff] [blame] | 654 | if (mOptionsMenuOpen) { |
| 655 | mActivity.closeOptionsMenu(); |
| 656 | mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100); |
| 657 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 658 | if (mPageDialogsHandler != null) { |
| 659 | mPageDialogsHandler.onConfigurationChanged(config); |
| 660 | } |
| 661 | mUi.onConfigurationChanged(config); |
| 662 | } |
| 663 | |
| 664 | @Override |
| 665 | public void handleNewIntent(Intent intent) { |
Michael Kolb | 59e232c | 2011-08-18 17:19:53 -0700 | [diff] [blame] | 666 | if (!mUi.isWebShowing()) { |
| 667 | mUi.showWeb(false); |
| 668 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 669 | mIntentHandler.onNewIntent(intent); |
| 670 | } |
| 671 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 672 | @Override |
| 673 | public void onPause() { |
Michael Kolb | 11fe02d | 2011-02-02 09:52:16 -0800 | [diff] [blame] | 674 | if (mUi.isCustomViewShowing()) { |
| 675 | hideCustomView(); |
| 676 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 677 | if (mActivityPaused) { |
| 678 | Log.e(LOGTAG, "BrowserActivity is already paused."); |
| 679 | return; |
| 680 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 681 | mActivityPaused = true; |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 682 | Tab tab = mTabControl.getCurrentTab(); |
| 683 | if (tab != null) { |
| 684 | tab.pause(); |
| 685 | if (!pauseWebViewTimers(tab)) { |
John Reck | f57c029 | 2011-07-21 18:15:39 -0700 | [diff] [blame] | 686 | if (mWakeLock == null) { |
| 687 | PowerManager pm = (PowerManager) mActivity |
| 688 | .getSystemService(Context.POWER_SERVICE); |
| 689 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser"); |
| 690 | } |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 691 | mWakeLock.acquire(); |
| 692 | mHandler.sendMessageDelayed(mHandler |
| 693 | .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT); |
| 694 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 695 | } |
| 696 | mUi.onPause(); |
| 697 | mNetworkHandler.onPause(); |
| 698 | |
| 699 | WebView.disablePlatformNotifications(); |
Ben Murdoch | 015e1e3 | 2011-09-01 23:45:31 +0100 | [diff] [blame] | 700 | NfcHandler.unregister(mActivity); |
John Reck | d7dd9b2 | 2011-08-30 09:18:29 -0700 | [diff] [blame] | 701 | if (sThumbnailBitmap != null) { |
| 702 | sThumbnailBitmap.recycle(); |
| 703 | sThumbnailBitmap = null; |
| 704 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 705 | } |
| 706 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 707 | @Override |
| 708 | public void onSaveInstanceState(Bundle outState) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 709 | // Save all the tabs |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 710 | Bundle saveState = createSaveState(); |
| 711 | |
| 712 | // crash recovery manages all save & restore state |
| 713 | mCrashRecoveryHandler.writeState(saveState); |
| 714 | mSettings.setLastRunPaused(true); |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * Save the current state to outState. Does not write the state to |
| 719 | * disk. |
| 720 | * @return Bundle containing the current state of all tabs. |
| 721 | */ |
| 722 | /* package */ Bundle createSaveState() { |
| 723 | Bundle saveState = new Bundle(); |
| 724 | mTabControl.saveState(saveState); |
| 725 | if (!saveState.isEmpty()) { |
John Reck | 24f1826 | 2011-06-17 14:47:20 -0700 | [diff] [blame] | 726 | // Save time so that we know how old incognito tabs (if any) are. |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 727 | saveState.putSerializable("lastActiveDate", Calendar.getInstance()); |
John Reck | 24f1826 | 2011-06-17 14:47:20 -0700 | [diff] [blame] | 728 | } |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 729 | return saveState; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 730 | } |
| 731 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 732 | @Override |
| 733 | public void onResume() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 734 | if (!mActivityPaused) { |
| 735 | Log.e(LOGTAG, "BrowserActivity is already resumed."); |
| 736 | return; |
| 737 | } |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 738 | mSettings.setLastRunPaused(false); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 739 | mActivityPaused = false; |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 740 | Tab current = mTabControl.getCurrentTab(); |
| 741 | if (current != null) { |
| 742 | current.resume(); |
| 743 | resumeWebViewTimers(current); |
| 744 | } |
John Reck | f57c029 | 2011-07-21 18:15:39 -0700 | [diff] [blame] | 745 | releaseWakeLock(); |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 746 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 747 | mUi.onResume(); |
| 748 | mNetworkHandler.onResume(); |
| 749 | WebView.enablePlatformNotifications(); |
Ben Murdoch | 015e1e3 | 2011-09-01 23:45:31 +0100 | [diff] [blame] | 750 | NfcHandler.register(mActivity, this); |
Michael Kolb | 0b12912 | 2012-06-04 16:31:58 -0700 | [diff] [blame] | 751 | if (mVoiceResult != null) { |
| 752 | mUi.onVoiceResult(mVoiceResult); |
| 753 | mVoiceResult = null; |
| 754 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 755 | } |
| 756 | |
John Reck | f57c029 | 2011-07-21 18:15:39 -0700 | [diff] [blame] | 757 | private void releaseWakeLock() { |
| 758 | if (mWakeLock != null && mWakeLock.isHeld()) { |
| 759 | mHandler.removeMessages(RELEASE_WAKELOCK); |
| 760 | mWakeLock.release(); |
| 761 | } |
| 762 | } |
| 763 | |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 764 | /** |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 765 | * resume all WebView timers using the WebView instance of the given tab |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 766 | * @param tab guaranteed non-null |
| 767 | */ |
| 768 | private void resumeWebViewTimers(Tab tab) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 769 | boolean inLoad = tab.inPageLoad(); |
| 770 | if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) { |
| 771 | CookieSyncManager.getInstance().startSync(); |
| 772 | WebView w = tab.getWebView(); |
Mathew Inwood | e1dbb95 | 2011-07-08 17:27:38 +0100 | [diff] [blame] | 773 | WebViewTimersControl.getInstance().onBrowserActivityResume(w); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 777 | /** |
| 778 | * Pause all WebView timers using the WebView of the given tab |
| 779 | * @param tab |
| 780 | * @return true if the timers are paused or tab is null |
| 781 | */ |
| 782 | private boolean pauseWebViewTimers(Tab tab) { |
| 783 | if (tab == null) { |
| 784 | return true; |
| 785 | } else if (!tab.inPageLoad()) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 786 | CookieSyncManager.getInstance().stopSync(); |
Mathew Inwood | e1dbb95 | 2011-07-08 17:27:38 +0100 | [diff] [blame] | 787 | WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 788 | return true; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 789 | } |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 790 | return false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 791 | } |
| 792 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 793 | @Override |
| 794 | public void onDestroy() { |
John Reck | 38b4bf5 | 2011-02-22 14:39:34 -0800 | [diff] [blame] | 795 | if (mUploadHandler != null && !mUploadHandler.handled()) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 796 | mUploadHandler.onResult(Activity.RESULT_CANCELED, null); |
| 797 | mUploadHandler = null; |
| 798 | } |
| 799 | if (mTabControl == null) return; |
| 800 | mUi.onDestroy(); |
| 801 | // Remove the current tab and sub window |
| 802 | Tab t = mTabControl.getCurrentTab(); |
| 803 | if (t != null) { |
| 804 | dismissSubWindow(t); |
| 805 | removeTab(t); |
| 806 | } |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 807 | mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 808 | // Destroy all the tabs |
| 809 | mTabControl.destroy(); |
| 810 | WebIconDatabase.getInstance().close(); |
| 811 | // Stop watching the default geolocation permissions |
| 812 | mSystemAllowGeolocationOrigins.stop(); |
| 813 | mSystemAllowGeolocationOrigins = null; |
| 814 | } |
| 815 | |
| 816 | protected boolean isActivityPaused() { |
| 817 | return mActivityPaused; |
| 818 | } |
| 819 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 820 | @Override |
| 821 | public void onLowMemory() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 822 | mTabControl.freeMemory(); |
| 823 | } |
| 824 | |
| 825 | @Override |
| 826 | public boolean shouldShowErrorConsole() { |
| 827 | return mShouldShowErrorConsole; |
| 828 | } |
| 829 | |
| 830 | protected void setShouldShowErrorConsole(boolean show) { |
| 831 | if (show == mShouldShowErrorConsole) { |
| 832 | // Nothing to do. |
| 833 | return; |
| 834 | } |
| 835 | mShouldShowErrorConsole = show; |
| 836 | Tab t = mTabControl.getCurrentTab(); |
| 837 | if (t == null) { |
| 838 | // There is no current tab so we cannot toggle the error console |
| 839 | return; |
| 840 | } |
| 841 | mUi.setShouldShowErrorConsole(t, show); |
| 842 | } |
| 843 | |
| 844 | @Override |
| 845 | public void stopLoading() { |
| 846 | mLoadStopped = true; |
| 847 | Tab tab = mTabControl.getCurrentTab(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 848 | WebView w = getCurrentTopWebView(); |
Michael Kolb | 778a488 | 2012-04-12 15:27:28 -0700 | [diff] [blame] | 849 | if (w != null) { |
| 850 | w.stopLoading(); |
| 851 | mUi.onPageStopped(tab); |
| 852 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | boolean didUserStopLoading() { |
| 856 | return mLoadStopped; |
| 857 | } |
| 858 | |
kaiyiz | a016da1 | 2013-08-26 17:50:22 +0800 | [diff] [blame] | 859 | private void handleNetworkNotify(WebView view) { |
| 860 | ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService( |
| 861 | Context.CONNECTIVITY_SERVICE); |
| 862 | WifiManager wifiMgr = (WifiManager) this.getContext() |
| 863 | .getSystemService(Context.WIFI_SERVICE); |
| 864 | int networkSwitchTypeOK = this.getContext().getResources() |
| 865 | .getInteger(R.integer.netswitch_type_remind); |
| 866 | |
| 867 | if (wifiMgr.isWifiEnabled()) { |
| 868 | NetworkInfo mNetworkInfo = conMgr.getActiveNetworkInfo(); |
| 869 | if (mNetworkInfo == null |
| 870 | || (mNetworkInfo != null && (mNetworkInfo.getType() != |
| 871 | ConnectivityManager.TYPE_WIFI))) { |
| 872 | int isReminder = Settings.System.getInt( |
| 873 | mActivity.getContentResolver(), |
| 874 | this.getContext().getResources() |
| 875 | .getString(R.string.network_switch_remind_type), |
| 876 | networkSwitchTypeOK); |
| 877 | |
| 878 | if (isReminder == networkSwitchTypeOK) { |
| 879 | Intent intent = new Intent( |
| 880 | INTENT_WIFI_SELECTION_DATA_CONNECTION); |
| 881 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 882 | this.getContext().startActivity(intent); |
| 883 | } |
| 884 | mNetworkShouldNotify = false; |
| 885 | } |
| 886 | } else { |
| 887 | if (!mNetworkHandler.isNetworkUp()) { |
| 888 | view.setNetworkAvailable(false); |
| 889 | Log.v(LOGTAG, "handleNetworkNotify() Wlan is not enabled."); |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 894 | // WebViewController |
| 895 | |
| 896 | @Override |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 897 | public void onPageStarted(Tab tab, WebView view, Bitmap favicon) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 898 | |
| 899 | // We've started to load a new page. If there was a pending message |
| 900 | // to save a screenshot then we will now take the new page and save |
| 901 | // an incorrect screenshot. Therefore, remove any pending thumbnail |
| 902 | // messages from the queue. |
| 903 | mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL, |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 904 | tab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 905 | |
| 906 | // reset sync timer to avoid sync starts during loading a page |
| 907 | CookieSyncManager.getInstance().resetSync(); |
| 908 | |
kaiyiz | a016da1 | 2013-08-26 17:50:22 +0800 | [diff] [blame] | 909 | if (SystemProperties.getBoolean(PROP_NETSWITCH, false)) { |
| 910 | if (!mNetworkHandler.isNetworkUp()) { |
| 911 | Log.d(LOGTAG, "onPageStarted() network unavailable"); |
| 912 | if (mNetworkShouldNotify) { |
| 913 | handleNetworkNotify(view); |
| 914 | } else { |
| 915 | view.setNetworkAvailable(false); |
| 916 | } |
| 917 | mNetworkShouldNotify = false; |
| 918 | } else { |
| 919 | Log.d(LOGTAG, "onPageStarted() network available"); |
| 920 | if (mNetworkShouldNotify) { |
| 921 | handleNetworkNotify(view); |
| 922 | } |
| 923 | mNetworkShouldNotify = false; |
| 924 | } |
| 925 | } else { |
| 926 | if (!mNetworkHandler.isNetworkUp()) { |
| 927 | view.setNetworkAvailable(false); |
| 928 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | // when BrowserActivity just starts, onPageStarted may be called before |
| 932 | // onResume as it is triggered from onCreate. Call resumeWebViewTimers |
| 933 | // to start the timer. As we won't switch tabs while an activity is in |
| 934 | // pause state, we can ensure calling resume and pause in pair. |
| 935 | if (mActivityPaused) { |
Michael Kolb | 7097693 | 2010-11-30 11:34:01 -0800 | [diff] [blame] | 936 | resumeWebViewTimers(tab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 937 | } |
| 938 | mLoadStopped = false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 939 | endActionMode(); |
| 940 | |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 941 | mUi.onTabDataChanged(tab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 942 | |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 943 | String url = tab.getUrl(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 944 | // update the bookmark database for favicon |
| 945 | maybeUpdateFavicon(tab, null, url, favicon); |
| 946 | |
| 947 | Performance.tracePageStart(url); |
| 948 | |
| 949 | // Performance probe |
| 950 | if (false) { |
| 951 | Performance.onPageStarted(); |
| 952 | } |
| 953 | |
| 954 | } |
| 955 | |
| 956 | @Override |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 957 | public void onPageFinished(Tab tab) { |
Michael Kolb | 7286427 | 2012-05-03 15:42:15 -0700 | [diff] [blame] | 958 | mCrashRecoveryHandler.backupState(); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 959 | mUi.onTabDataChanged(tab); |
Martijn Coenen | b2f9355 | 2011-06-14 10:48:35 +0200 | [diff] [blame] | 960 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 961 | // Performance probe |
| 962 | if (false) { |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 963 | Performance.onPageFinished(tab.getUrl()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | Performance.tracePageFinished(); |
| 967 | } |
| 968 | |
| 969 | @Override |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 970 | public void onProgressChanged(Tab tab) { |
| 971 | int newProgress = tab.getLoadProgress(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 972 | |
| 973 | if (newProgress == 100) { |
| 974 | CookieSyncManager.getInstance().sync(); |
| 975 | // onProgressChanged() may continue to be called after the main |
| 976 | // frame has finished loading, as any remaining sub frames continue |
| 977 | // to load. We'll only get called once though with newProgress as |
| 978 | // 100 when everything is loaded. (onPageFinished is called once |
| 979 | // when the main frame completes loading regardless of the state of |
| 980 | // any sub frames so calls to onProgressChanges may continue after |
| 981 | // onPageFinished has executed) |
Michael Kolb | b1fb70c | 2011-11-21 12:38:14 -0800 | [diff] [blame] | 982 | if (tab.inPageLoad()) { |
| 983 | updateInLoadMenuItems(mCachedMenu, tab); |
Mattias Falk | 9cc2d03 | 2012-05-25 09:40:31 +0200 | [diff] [blame] | 984 | } else if (mActivityPaused && pauseWebViewTimers(tab)) { |
| 985 | // pause the WebView timer and release the wake lock if it is |
| 986 | // finished while BrowserActivity is in pause state. |
| 987 | releaseWakeLock(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 988 | } |
John Reck | d986237 | 2012-02-21 15:04:50 -0800 | [diff] [blame] | 989 | if (!tab.isPrivateBrowsingEnabled() |
| 990 | && !TextUtils.isEmpty(tab.getUrl()) |
| 991 | && !tab.isSnapshot()) { |
| 992 | // Only update the bookmark screenshot if the user did not |
| 993 | // cancel the load early and there is not already |
| 994 | // a pending update for the tab. |
Michael Kolb | 7286427 | 2012-05-03 15:42:15 -0700 | [diff] [blame] | 995 | if (tab.shouldUpdateThumbnail() && |
| 996 | (tab.inForeground() && !didUserStopLoading() |
| 997 | || !tab.inForeground())) { |
John Reck | d986237 | 2012-02-21 15:04:50 -0800 | [diff] [blame] | 998 | if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) { |
| 999 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 1000 | UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab), |
| 1001 | 500); |
| 1002 | } |
| 1003 | } |
| 1004 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1005 | } else { |
Michael Kolb | b1fb70c | 2011-11-21 12:38:14 -0800 | [diff] [blame] | 1006 | if (!tab.inPageLoad()) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1007 | // onPageFinished may have already been called but a subframe is |
Michael Kolb | b1fb70c | 2011-11-21 12:38:14 -0800 | [diff] [blame] | 1008 | // still loading |
| 1009 | // updating the progress and |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1010 | // update the menu items. |
Michael Kolb | b1fb70c | 2011-11-21 12:38:14 -0800 | [diff] [blame] | 1011 | updateInLoadMenuItems(mCachedMenu, tab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1012 | } |
| 1013 | } |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1014 | mUi.onProgressChanged(tab); |
| 1015 | } |
| 1016 | |
| 1017 | @Override |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 1018 | public void onUpdatedSecurityState(Tab tab) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1019 | mUi.onTabDataChanged(tab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | @Override |
| 1023 | public void onReceivedTitle(Tab tab, final String title) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1024 | mUi.onTabDataChanged(tab); |
John Reck | 49a603c | 2011-03-03 09:33:05 -0800 | [diff] [blame] | 1025 | final String pageUrl = tab.getOriginalUrl(); |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 1026 | if (TextUtils.isEmpty(pageUrl) || pageUrl.length() |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1027 | >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) { |
| 1028 | return; |
| 1029 | } |
| 1030 | // Update the title in the history database if not in private browsing mode |
| 1031 | if (!tab.isPrivateBrowsingEnabled()) { |
John Reck | f57c029 | 2011-07-21 18:15:39 -0700 | [diff] [blame] | 1032 | DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | @Override |
| 1037 | public void onFavicon(Tab tab, WebView view, Bitmap icon) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1038 | mUi.onTabDataChanged(tab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1039 | maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon); |
| 1040 | } |
| 1041 | |
| 1042 | @Override |
Michael Kolb | 18eb377 | 2010-12-10 14:29:51 -0800 | [diff] [blame] | 1043 | public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) { |
| 1044 | return mUrlHandler.shouldOverrideUrlLoading(tab, view, url); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | @Override |
| 1048 | public boolean shouldOverrideKeyEvent(KeyEvent event) { |
| 1049 | if (mMenuIsDown) { |
| 1050 | // only check shortcut key when MENU is held |
| 1051 | return mActivity.getWindow().isShortcutKey(event.getKeyCode(), |
| 1052 | event); |
| 1053 | } else { |
| 1054 | return false; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | @Override |
John Reck | 997b1b7 | 2012-04-19 18:08:25 -0700 | [diff] [blame] | 1059 | public boolean onUnhandledKeyEvent(KeyEvent event) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1060 | if (!isActivityPaused()) { |
| 1061 | if (event.getAction() == KeyEvent.ACTION_DOWN) { |
John Reck | 997b1b7 | 2012-04-19 18:08:25 -0700 | [diff] [blame] | 1062 | return mActivity.onKeyDown(event.getKeyCode(), event); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1063 | } else { |
John Reck | 997b1b7 | 2012-04-19 18:08:25 -0700 | [diff] [blame] | 1064 | return mActivity.onKeyUp(event.getKeyCode(), event); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1065 | } |
| 1066 | } |
John Reck | 997b1b7 | 2012-04-19 18:08:25 -0700 | [diff] [blame] | 1067 | return false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | @Override |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 1071 | public void doUpdateVisitedHistory(Tab tab, boolean isReload) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1072 | // Don't save anything in private browsing mode |
| 1073 | if (tab.isPrivateBrowsingEnabled()) return; |
John Reck | 49a603c | 2011-03-03 09:33:05 -0800 | [diff] [blame] | 1074 | String url = tab.getOriginalUrl(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1075 | |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 1076 | if (TextUtils.isEmpty(url) |
| 1077 | || url.regionMatches(true, 0, "about:", 0, 6)) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1078 | return; |
| 1079 | } |
John Reck | f57c029 | 2011-07-21 18:15:39 -0700 | [diff] [blame] | 1080 | DataController.getInstance(mActivity).updateVisitedHistory(url); |
John Reck | 6c2e2f3 | 2011-08-22 13:41:23 -0700 | [diff] [blame] | 1081 | mCrashRecoveryHandler.backupState(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | @Override |
| 1085 | public void getVisitedHistory(final ValueCallback<String[]> callback) { |
| 1086 | AsyncTask<Void, Void, String[]> task = |
| 1087 | new AsyncTask<Void, Void, String[]>() { |
| 1088 | @Override |
| 1089 | public String[] doInBackground(Void... unused) { |
| 1090 | return Browser.getVisitedHistory(mActivity.getContentResolver()); |
| 1091 | } |
| 1092 | @Override |
| 1093 | public void onPostExecute(String[] result) { |
| 1094 | callback.onReceiveValue(result); |
| 1095 | } |
| 1096 | }; |
| 1097 | task.execute(); |
| 1098 | } |
| 1099 | |
| 1100 | @Override |
| 1101 | public void onReceivedHttpAuthRequest(Tab tab, WebView view, |
| 1102 | final HttpAuthHandler handler, final String host, |
| 1103 | final String realm) { |
| 1104 | String username = null; |
| 1105 | String password = null; |
| 1106 | |
| 1107 | boolean reuseHttpAuthUsernamePassword |
| 1108 | = handler.useHttpAuthUsernamePassword(); |
| 1109 | |
| 1110 | if (reuseHttpAuthUsernamePassword && view != null) { |
| 1111 | String[] credentials = view.getHttpAuthUsernamePassword(host, realm); |
| 1112 | if (credentials != null && credentials.length == 2) { |
| 1113 | username = credentials[0]; |
| 1114 | password = credentials[1]; |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | if (username != null && password != null) { |
| 1119 | handler.proceed(username, password); |
| 1120 | } else { |
Ben Murdoch | fdd3711 | 2011-08-05 15:48:14 +0100 | [diff] [blame] | 1121 | if (tab.inForeground() && !handler.suppressDialog()) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1122 | mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm); |
| 1123 | } else { |
| 1124 | handler.cancel(); |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | @Override |
| 1130 | public void onDownloadStart(Tab tab, String url, String userAgent, |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 1131 | String contentDisposition, String mimetype, String referer, |
| 1132 | long contentLength) { |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 1133 | WebView w = tab.getWebView(); |
qqzhou | a95a2e2 | 2013-04-18 17:28:31 +0800 | [diff] [blame] | 1134 | boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent, |
luxiaol | 62677b0 | 2013-07-22 07:54:49 +0800 | [diff] [blame] | 1135 | contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength); |
qqzhou | a95a2e2 | 2013-04-18 17:28:31 +0800 | [diff] [blame] | 1136 | if (ret == false && w.copyBackForwardList().getSize() == 0) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1137 | // This Tab was opened for the sole purpose of downloading a |
| 1138 | // file. Remove it. |
| 1139 | if (tab == mTabControl.getCurrentTab()) { |
| 1140 | // In this case, the Tab is still on top. |
| 1141 | goBackOnePageOrQuit(); |
| 1142 | } else { |
| 1143 | // In this case, it is not. |
| 1144 | closeTab(tab); |
| 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | @Override |
| 1150 | public Bitmap getDefaultVideoPoster() { |
| 1151 | return mUi.getDefaultVideoPoster(); |
| 1152 | } |
| 1153 | |
| 1154 | @Override |
| 1155 | public View getVideoLoadingProgressView() { |
| 1156 | return mUi.getVideoLoadingProgressView(); |
| 1157 | } |
| 1158 | |
| 1159 | @Override |
| 1160 | public void showSslCertificateOnError(WebView view, SslErrorHandler handler, |
| 1161 | SslError error) { |
| 1162 | mPageDialogsHandler.showSSLCertificateOnError(view, handler, error); |
| 1163 | } |
| 1164 | |
Patrick Scott | 9206677 | 2011-03-10 08:46:27 -0500 | [diff] [blame] | 1165 | @Override |
| 1166 | public void showAutoLogin(Tab tab) { |
| 1167 | assert tab.inForeground(); |
| 1168 | // Update the title bar to show the auto-login request. |
| 1169 | mUi.showAutoLogin(tab); |
| 1170 | } |
| 1171 | |
| 1172 | @Override |
| 1173 | public void hideAutoLogin(Tab tab) { |
| 1174 | assert tab.inForeground(); |
| 1175 | mUi.hideAutoLogin(tab); |
| 1176 | } |
| 1177 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1178 | // helper method |
| 1179 | |
| 1180 | /* |
| 1181 | * Update the favorites icon if the private browsing isn't enabled and the |
| 1182 | * icon is valid. |
| 1183 | */ |
| 1184 | private void maybeUpdateFavicon(Tab tab, final String originalUrl, |
| 1185 | final String url, Bitmap favicon) { |
| 1186 | if (favicon == null) { |
| 1187 | return; |
| 1188 | } |
| 1189 | if (!tab.isPrivateBrowsingEnabled()) { |
| 1190 | Bookmarks.updateFavicon(mActivity |
| 1191 | .getContentResolver(), originalUrl, url, favicon); |
| 1192 | } |
| 1193 | } |
| 1194 | |
Leon Scroggins | 4cd9779 | 2010-12-03 15:31:56 -0500 | [diff] [blame] | 1195 | @Override |
| 1196 | public void bookmarkedStatusHasChanged(Tab tab) { |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1197 | // TODO: Switch to using onTabDataChanged after b/3262950 is fixed |
Leon Scroggins | 4cd9779 | 2010-12-03 15:31:56 -0500 | [diff] [blame] | 1198 | mUi.bookmarkedStatusHasChanged(tab); |
| 1199 | } |
| 1200 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1201 | // end WebViewController |
| 1202 | |
| 1203 | protected void pageUp() { |
| 1204 | getCurrentTopWebView().pageUp(false); |
| 1205 | } |
| 1206 | |
| 1207 | protected void pageDown() { |
| 1208 | getCurrentTopWebView().pageDown(false); |
| 1209 | } |
| 1210 | |
| 1211 | // callback from phone title bar |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 1212 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1213 | public void editUrl() { |
| 1214 | if (mOptionsMenuOpen) mActivity.closeOptionsMenu(); |
Michael Kolb | 1f9b356 | 2012-04-24 14:38:34 -0700 | [diff] [blame] | 1215 | mUi.editUrl(false, true); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 1218 | @Override |
Derek Sollenberger | 2d4f1e2 | 2011-06-01 14:50:42 -0400 | [diff] [blame] | 1219 | public void showCustomView(Tab tab, View view, int requestedOrientation, |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1220 | WebChromeClient.CustomViewCallback callback) { |
| 1221 | if (tab.inForeground()) { |
| 1222 | if (mUi.isCustomViewShowing()) { |
| 1223 | callback.onCustomViewHidden(); |
| 1224 | return; |
| 1225 | } |
Derek Sollenberger | 2d4f1e2 | 2011-06-01 14:50:42 -0400 | [diff] [blame] | 1226 | mUi.showCustomView(view, requestedOrientation, callback); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1227 | // Save the menu state and set it to empty while the custom |
| 1228 | // view is showing. |
| 1229 | mOldMenuState = mMenuState; |
| 1230 | mMenuState = EMPTY_MENU; |
John Reck | d73c5a2 | 2010-12-22 10:22:50 -0800 | [diff] [blame] | 1231 | mActivity.invalidateOptionsMenu(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | @Override |
| 1236 | public void hideCustomView() { |
| 1237 | if (mUi.isCustomViewShowing()) { |
| 1238 | mUi.onHideCustomView(); |
| 1239 | // Reset the old menu state. |
| 1240 | mMenuState = mOldMenuState; |
| 1241 | mOldMenuState = EMPTY_MENU; |
John Reck | d73c5a2 | 2010-12-22 10:22:50 -0800 | [diff] [blame] | 1242 | mActivity.invalidateOptionsMenu(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 1246 | @Override |
| 1247 | public void onActivityResult(int requestCode, int resultCode, |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1248 | Intent intent) { |
| 1249 | if (getCurrentTopWebView() == null) return; |
| 1250 | switch (requestCode) { |
| 1251 | case PREFERENCES_PAGE: |
| 1252 | if (resultCode == Activity.RESULT_OK && intent != null) { |
| 1253 | String action = intent.getStringExtra(Intent.EXTRA_TEXT); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 1254 | if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1255 | mTabControl.removeParentChildRelationShips(); |
| 1256 | } |
| 1257 | } |
| 1258 | break; |
| 1259 | case FILE_SELECTED: |
Ben Murdoch | 51f6a2f | 2011-02-21 12:27:07 +0000 | [diff] [blame] | 1260 | // Chose a file from the file picker. |
John Reck | 9dfcdb1 | 2011-02-22 16:40:46 -0800 | [diff] [blame] | 1261 | if (null == mUploadHandler) break; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1262 | mUploadHandler.onResult(resultCode, intent); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1263 | break; |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 1264 | case AUTOFILL_SETUP: |
| 1265 | // Determine whether a profile was actually set up or not |
| 1266 | // and if so, send the message back to the WebTextView to |
| 1267 | // fill the form with the new profile. |
| 1268 | if (getSettings().getAutoFillProfile() != null) { |
| 1269 | mAutoFillSetupMessage.sendToTarget(); |
| 1270 | mAutoFillSetupMessage = null; |
| 1271 | } |
| 1272 | break; |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 1273 | case COMBO_VIEW: |
| 1274 | if (intent == null || resultCode != Activity.RESULT_OK) { |
| 1275 | break; |
| 1276 | } |
John Reck | 3ba4553 | 2011-08-11 16:26:53 -0700 | [diff] [blame] | 1277 | mUi.showWeb(false); |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 1278 | if (Intent.ACTION_VIEW.equals(intent.getAction())) { |
| 1279 | Tab t = getCurrentTab(); |
| 1280 | Uri uri = intent.getData(); |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 1281 | mUpdateMyNavThumbnail = true; |
| 1282 | mUpdateMyNavThumbnailUrl = uri.toString(); |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 1283 | loadUrl(t, uri.toString()); |
| 1284 | } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) { |
| 1285 | String[] urls = intent.getStringArrayExtra( |
| 1286 | ComboViewActivity.EXTRA_OPEN_ALL); |
| 1287 | Tab parent = getCurrentTab(); |
| 1288 | for (String url : urls) { |
kaiyiz | 47097d6 | 2013-08-09 09:30:28 +0800 | [diff] [blame] | 1289 | if (url != null) { |
| 1290 | parent = openTab(url, parent, |
| 1291 | !mSettings.openInBackground(), true); |
| 1292 | } |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 1293 | } |
| 1294 | } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) { |
| 1295 | long id = intent.getLongExtra( |
| 1296 | ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1); |
| 1297 | if (id >= 0) { |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 1298 | if (BrowserWebView.isClassic()) { |
| 1299 | createNewSnapshotTab(id, true); |
| 1300 | } else { |
| 1301 | Toast.makeText(mActivity, "Snapshot Tab requires WebViewClassic", |
| 1302 | Toast.LENGTH_LONG).show(); |
| 1303 | } |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 1304 | } |
| 1305 | } |
| 1306 | break; |
Michael Kolb | 0b12912 | 2012-06-04 16:31:58 -0700 | [diff] [blame] | 1307 | case VOICE_RESULT: |
| 1308 | if (resultCode == Activity.RESULT_OK && intent != null) { |
| 1309 | ArrayList<String> results = intent.getStringArrayListExtra( |
| 1310 | RecognizerIntent.EXTRA_RESULTS); |
| 1311 | if (results.size() >= 1) { |
| 1312 | mVoiceResult = results.get(0); |
| 1313 | } |
| 1314 | } |
| 1315 | break; |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 1316 | |
| 1317 | case MY_NAVIGATION: |
| 1318 | if (intent == null || resultCode != Activity.RESULT_OK) { |
| 1319 | break; |
| 1320 | } |
| 1321 | |
| 1322 | if (intent.getBooleanExtra("need_refresh", false) && |
| 1323 | getCurrentTopWebView() != null) { |
| 1324 | getCurrentTopWebView().reload(); |
| 1325 | } |
| 1326 | break; |
| 1327 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1328 | default: |
| 1329 | break; |
| 1330 | } |
| 1331 | getCurrentTopWebView().requestFocus(); |
| 1332 | } |
| 1333 | |
| 1334 | /** |
| 1335 | * Open the Go page. |
| 1336 | * @param startWithHistory If true, open starting on the history tab. |
| 1337 | * Otherwise, start with the bookmarks tab. |
| 1338 | */ |
| 1339 | @Override |
Michael Kolb | 315d502 | 2011-10-13 12:47:11 -0700 | [diff] [blame] | 1340 | public void bookmarksOrHistoryPicker(ComboViews startView) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1341 | if (mTabControl.getCurrentWebView() == null) { |
| 1342 | return; |
| 1343 | } |
Michael Kolb | bd3dd94 | 2011-01-12 11:09:38 -0800 | [diff] [blame] | 1344 | // clear action mode |
| 1345 | if (isInCustomActionMode()) { |
| 1346 | endActionMode(); |
| 1347 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1348 | Bundle extras = new Bundle(); |
| 1349 | // Disable opening in a new window if we have maxed out the windows |
| 1350 | extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW, |
| 1351 | !mTabControl.canCreateNewTab()); |
Michael Kolb | 315d502 | 2011-10-13 12:47:11 -0700 | [diff] [blame] | 1352 | mUi.showComboView(startView, extras); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | // combo view callbacks |
| 1356 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1357 | // key handling |
| 1358 | protected void onBackKey() { |
| 1359 | if (!mUi.onBackKey()) { |
| 1360 | WebView subwindow = mTabControl.getCurrentSubWindow(); |
| 1361 | if (subwindow != null) { |
| 1362 | if (subwindow.canGoBack()) { |
| 1363 | subwindow.goBack(); |
| 1364 | } else { |
| 1365 | dismissSubWindow(mTabControl.getCurrentTab()); |
| 1366 | } |
| 1367 | } else { |
| 1368 | goBackOnePageOrQuit(); |
| 1369 | } |
| 1370 | } |
| 1371 | } |
| 1372 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 1373 | protected boolean onMenuKey() { |
| 1374 | return mUi.onMenuKey(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1377 | // menu handling and state |
| 1378 | // TODO: maybe put into separate handler |
| 1379 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 1380 | @Override |
| 1381 | public boolean onCreateOptionsMenu(Menu menu) { |
John Reck | d73c5a2 | 2010-12-22 10:22:50 -0800 | [diff] [blame] | 1382 | if (mMenuState == EMPTY_MENU) { |
| 1383 | return false; |
| 1384 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1385 | MenuInflater inflater = mActivity.getMenuInflater(); |
| 1386 | inflater.inflate(R.menu.browser, menu); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1387 | return true; |
| 1388 | } |
| 1389 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 1390 | @Override |
| 1391 | public void onCreateContextMenu(ContextMenu menu, View v, |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1392 | ContextMenuInfo menuInfo) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 1393 | if (v instanceof TitleBar) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1394 | return; |
| 1395 | } |
| 1396 | if (!(v instanceof WebView)) { |
| 1397 | return; |
| 1398 | } |
Leon Scroggins | 026f254 | 2010-11-22 13:26:12 -0500 | [diff] [blame] | 1399 | final WebView webview = (WebView) v; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1400 | WebView.HitTestResult result = webview.getHitTestResult(); |
| 1401 | if (result == null) { |
| 1402 | return; |
| 1403 | } |
| 1404 | |
| 1405 | int type = result.getType(); |
| 1406 | if (type == WebView.HitTestResult.UNKNOWN_TYPE) { |
| 1407 | Log.w(LOGTAG, |
| 1408 | "We should not show context menu when nothing is touched"); |
| 1409 | return; |
| 1410 | } |
| 1411 | if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) { |
| 1412 | // let TextView handles context menu |
| 1413 | return; |
| 1414 | } |
| 1415 | |
| 1416 | // Note, http://b/issue?id=1106666 is requesting that |
| 1417 | // an inflated menu can be used again. This is not available |
| 1418 | // yet, so inflate each time (yuk!) |
| 1419 | MenuInflater inflater = mActivity.getMenuInflater(); |
| 1420 | inflater.inflate(R.menu.browsercontext, menu); |
| 1421 | |
| 1422 | // Show the correct menu group |
| 1423 | final String extra = result.getExtra(); |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 1424 | final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra); |
Michael Kolb | c159c1a | 2011-12-15 16:06:38 -0800 | [diff] [blame] | 1425 | if (extra == null) return; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1426 | menu.setGroupVisible(R.id.PHONE_MENU, |
| 1427 | type == WebView.HitTestResult.PHONE_TYPE); |
| 1428 | menu.setGroupVisible(R.id.EMAIL_MENU, |
| 1429 | type == WebView.HitTestResult.EMAIL_TYPE); |
| 1430 | menu.setGroupVisible(R.id.GEO_MENU, |
| 1431 | type == WebView.HitTestResult.GEO_TYPE); |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 1432 | |
| 1433 | String itemUrl = null; |
| 1434 | String url = webview.getOriginalUrl(); |
| 1435 | if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) { |
| 1436 | itemUrl = Uri.decode(navigationUrl); |
| 1437 | if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) { |
| 1438 | menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, |
| 1439 | type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE); |
| 1440 | } else { |
| 1441 | menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false); |
| 1442 | } |
| 1443 | menu.setGroupVisible(R.id.IMAGE_MENU, false); |
| 1444 | menu.setGroupVisible(R.id.ANCHOR_MENU, false); |
| 1445 | } else { |
| 1446 | menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false); |
| 1447 | |
| 1448 | menu.setGroupVisible(R.id.IMAGE_MENU, |
| 1449 | type == WebView.HitTestResult.IMAGE_TYPE |
| 1450 | || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE); |
| 1451 | menu.setGroupVisible(R.id.ANCHOR_MENU, |
| 1452 | type == WebView.HitTestResult.SRC_ANCHOR_TYPE |
| 1453 | || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE); |
| 1454 | } |
| 1455 | |
Cary Clark | 8974d28 | 2010-11-22 10:46:05 -0500 | [diff] [blame] | 1456 | boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE |
| 1457 | || type == WebView.HitTestResult.PHONE_TYPE |
| 1458 | || type == WebView.HitTestResult.EMAIL_TYPE |
| 1459 | || type == WebView.HitTestResult.GEO_TYPE; |
| 1460 | menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText); |
| 1461 | if (hitText) { |
| 1462 | menu.findItem(R.id.select_text_menu_id) |
| 1463 | .setOnMenuItemClickListener(new SelectText(webview)); |
| 1464 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1465 | // Setup custom handling depending on the type |
| 1466 | switch (type) { |
| 1467 | case WebView.HitTestResult.PHONE_TYPE: |
| 1468 | menu.setHeaderTitle(Uri.decode(extra)); |
| 1469 | menu.findItem(R.id.dial_context_menu_id).setIntent( |
| 1470 | new Intent(Intent.ACTION_VIEW, Uri |
| 1471 | .parse(WebView.SCHEME_TEL + extra))); |
| 1472 | Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT); |
| 1473 | addIntent.putExtra(Insert.PHONE, Uri.decode(extra)); |
| 1474 | addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE); |
| 1475 | menu.findItem(R.id.add_contact_context_menu_id).setIntent( |
| 1476 | addIntent); |
| 1477 | menu.findItem(R.id.copy_phone_context_menu_id) |
| 1478 | .setOnMenuItemClickListener( |
| 1479 | new Copy(extra)); |
| 1480 | break; |
| 1481 | |
| 1482 | case WebView.HitTestResult.EMAIL_TYPE: |
| 1483 | menu.setHeaderTitle(extra); |
| 1484 | menu.findItem(R.id.email_context_menu_id).setIntent( |
| 1485 | new Intent(Intent.ACTION_VIEW, Uri |
| 1486 | .parse(WebView.SCHEME_MAILTO + extra))); |
| 1487 | menu.findItem(R.id.copy_mail_context_menu_id) |
| 1488 | .setOnMenuItemClickListener( |
| 1489 | new Copy(extra)); |
| 1490 | break; |
| 1491 | |
| 1492 | case WebView.HitTestResult.GEO_TYPE: |
| 1493 | menu.setHeaderTitle(extra); |
| 1494 | menu.findItem(R.id.map_context_menu_id).setIntent( |
| 1495 | new Intent(Intent.ACTION_VIEW, Uri |
| 1496 | .parse(WebView.SCHEME_GEO |
| 1497 | + URLEncoder.encode(extra)))); |
| 1498 | menu.findItem(R.id.copy_geo_context_menu_id) |
| 1499 | .setOnMenuItemClickListener( |
| 1500 | new Copy(extra)); |
| 1501 | break; |
| 1502 | |
| 1503 | case WebView.HitTestResult.SRC_ANCHOR_TYPE: |
| 1504 | case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE: |
Michael Kolb | 4c537ce | 2011-01-13 15:19:33 -0800 | [diff] [blame] | 1505 | menu.setHeaderTitle(extra); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1506 | // decide whether to show the open link in new tab option |
| 1507 | boolean showNewTab = mTabControl.canCreateNewTab(); |
| 1508 | MenuItem newTabItem |
| 1509 | = menu.findItem(R.id.open_newtab_context_menu_id); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 1510 | newTabItem.setTitle(getSettings().openInBackground() |
Michael Kolb | 2dd65c8 | 2011-01-14 11:07:38 -0800 | [diff] [blame] | 1511 | ? R.string.contextmenu_openlink_newwindow_background |
| 1512 | : R.string.contextmenu_openlink_newwindow); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1513 | newTabItem.setVisible(showNewTab); |
| 1514 | if (showNewTab) { |
Leon Scroggins | 026f254 | 2010-11-22 13:26:12 -0500 | [diff] [blame] | 1515 | if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) { |
| 1516 | newTabItem.setOnMenuItemClickListener( |
| 1517 | new MenuItem.OnMenuItemClickListener() { |
| 1518 | @Override |
| 1519 | public boolean onMenuItemClick(MenuItem item) { |
| 1520 | final HashMap<String, WebView> hrefMap = |
| 1521 | new HashMap<String, WebView>(); |
| 1522 | hrefMap.put("webview", webview); |
| 1523 | final Message msg = mHandler.obtainMessage( |
| 1524 | FOCUS_NODE_HREF, |
| 1525 | R.id.open_newtab_context_menu_id, |
| 1526 | 0, hrefMap); |
| 1527 | webview.requestFocusNodeHref(msg); |
| 1528 | return true; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1529 | } |
Leon Scroggins | 026f254 | 2010-11-22 13:26:12 -0500 | [diff] [blame] | 1530 | }); |
| 1531 | } else { |
| 1532 | newTabItem.setOnMenuItemClickListener( |
| 1533 | new MenuItem.OnMenuItemClickListener() { |
| 1534 | @Override |
| 1535 | public boolean onMenuItemClick(MenuItem item) { |
| 1536 | final Tab parent = mTabControl.getCurrentTab(); |
John Reck | 5949c66 | 2011-05-27 09:52:29 -0700 | [diff] [blame] | 1537 | openTab(extra, parent, |
| 1538 | !mSettings.openInBackground(), |
| 1539 | true); |
Leon Scroggins | 026f254 | 2010-11-22 13:26:12 -0500 | [diff] [blame] | 1540 | return true; |
| 1541 | } |
| 1542 | }); |
| 1543 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1544 | } |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 1545 | if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) { |
| 1546 | menu.setHeaderTitle(navigationUrl); |
| 1547 | menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false); |
| 1548 | |
| 1549 | if (itemUrl != null) { |
| 1550 | if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) { |
| 1551 | menu.findItem(R.id.edit_my_navigation_context_menu_id) |
| 1552 | .setOnMenuItemClickListener(new OnMenuItemClickListener() { |
| 1553 | @Override |
| 1554 | public boolean onMenuItemClick(MenuItem item) { |
| 1555 | final Intent intent = new Intent(Controller.this |
| 1556 | .getContext(), |
| 1557 | AddMyNavigationPage.class); |
| 1558 | Bundle bundle = new Bundle(); |
| 1559 | String url = Uri.decode(navigationUrl); |
| 1560 | bundle.putBoolean("isAdding", false); |
| 1561 | bundle.putString("url", url); |
| 1562 | bundle.putString("name", getNameFromUrl(url)); |
| 1563 | intent.putExtra("websites", bundle); |
| 1564 | mActivity.startActivityForResult(intent, MY_NAVIGATION); |
| 1565 | return false; |
| 1566 | } |
| 1567 | }); |
| 1568 | menu.findItem(R.id.delete_my_navigation_context_menu_id) |
| 1569 | .setOnMenuItemClickListener(new OnMenuItemClickListener() { |
| 1570 | @Override |
| 1571 | public boolean onMenuItemClick(MenuItem item) { |
| 1572 | showMyNavigationDeleteDialog(Uri.decode(navigationUrl)); |
| 1573 | return false; |
| 1574 | } |
| 1575 | }); |
| 1576 | } |
| 1577 | } else { |
| 1578 | Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!"); |
| 1579 | } |
| 1580 | } |
| 1581 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1582 | if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) { |
| 1583 | break; |
| 1584 | } |
| 1585 | // otherwise fall through to handle image part |
| 1586 | case WebView.HitTestResult.IMAGE_TYPE: |
Victoria Lease | 5987b80 | 2012-03-12 13:05:23 -0700 | [diff] [blame] | 1587 | MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id); |
| 1588 | shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1589 | if (type == WebView.HitTestResult.IMAGE_TYPE) { |
| 1590 | menu.setHeaderTitle(extra); |
Victoria Lease | 5987b80 | 2012-03-12 13:05:23 -0700 | [diff] [blame] | 1591 | shareItem.setOnMenuItemClickListener( |
| 1592 | new MenuItem.OnMenuItemClickListener() { |
| 1593 | @Override |
| 1594 | public boolean onMenuItemClick(MenuItem item) { |
| 1595 | sharePage(mActivity, null, extra, null, |
| 1596 | null); |
| 1597 | return true; |
| 1598 | } |
| 1599 | } |
| 1600 | ); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1601 | } |
Michael Kolb | 3639c4c | 2011-09-28 15:36:40 -0700 | [diff] [blame] | 1602 | menu.findItem(R.id.view_image_context_menu_id) |
| 1603 | .setOnMenuItemClickListener(new OnMenuItemClickListener() { |
| 1604 | @Override |
| 1605 | public boolean onMenuItemClick(MenuItem item) { |
| 1606 | openTab(extra, mTabControl.getCurrentTab(), true, true); |
| 1607 | return false; |
| 1608 | } |
| 1609 | }); |
Andreas Sandblad | 8e4ce66 | 2012-06-11 11:02:49 +0200 | [diff] [blame] | 1610 | menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener( |
| 1611 | new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(), |
| 1612 | webview.getSettings().getUserAgentString())); |
John Reck | 3527dd1 | 2011-02-22 10:35:29 -0800 | [diff] [blame] | 1613 | menu.findItem(R.id.set_wallpaper_context_menu_id). |
| 1614 | setOnMenuItemClickListener(new WallpaperHandler(mActivity, |
| 1615 | extra)); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1616 | break; |
| 1617 | |
| 1618 | default: |
| 1619 | Log.w(LOGTAG, "We should not get here."); |
| 1620 | break; |
| 1621 | } |
| 1622 | //update the ui |
| 1623 | mUi.onContextMenuCreated(menu); |
| 1624 | } |
| 1625 | |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 1626 | public void startAddMyNavigation(String url) { |
| 1627 | final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class); |
| 1628 | Bundle bundle = new Bundle(); |
| 1629 | bundle.putBoolean("isAdding", true); |
| 1630 | bundle.putString("url", url); |
| 1631 | bundle.putString("name", getNameFromUrl(url)); |
| 1632 | intent.putExtra("websites", bundle); |
| 1633 | mActivity.startActivityForResult(intent, MY_NAVIGATION); |
| 1634 | } |
| 1635 | |
| 1636 | private void showMyNavigationDeleteDialog(final String itemUrl) { |
| 1637 | new AlertDialog.Builder(this.getContext()) |
| 1638 | .setTitle(R.string.my_navigation_delete_label) |
| 1639 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 1640 | .setMessage(R.string.my_navigation_delete_msg) |
| 1641 | .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
| 1642 | @Override |
| 1643 | public void onClick(DialogInterface dialog, int whichButton) { |
| 1644 | deleteMyNavigationItem(itemUrl); |
| 1645 | } |
| 1646 | }) |
| 1647 | .setNegativeButton(R.string.cancel, null) |
| 1648 | .show(); |
| 1649 | } |
| 1650 | |
| 1651 | private void deleteMyNavigationItem(final String itemUrl) { |
| 1652 | ContentResolver cr = this.getContext().getContentResolver(); |
| 1653 | Cursor cursor = null; |
| 1654 | |
| 1655 | try { |
| 1656 | cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI, |
| 1657 | new String[] { |
| 1658 | MyNavigationUtil.ID |
| 1659 | }, "url = ?", new String[] { |
| 1660 | itemUrl |
| 1661 | }, null); |
| 1662 | if (null != cursor && cursor.moveToFirst()) { |
| 1663 | Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI, |
| 1664 | cursor.getLong(0)); |
| 1665 | |
| 1666 | ContentValues values = new ContentValues(); |
| 1667 | values.put(MyNavigationUtil.TITLE, ""); |
| 1668 | values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav"); |
| 1669 | values.put(MyNavigationUtil.WEBSITE, 0 + ""); |
| 1670 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 1671 | Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(), |
| 1672 | R.raw.my_navigation_add); |
| 1673 | bm.compress(Bitmap.CompressFormat.PNG, 100, os); |
| 1674 | values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray()); |
| 1675 | Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri); |
| 1676 | cr.update(uri, values, null, null); |
| 1677 | } else { |
| 1678 | Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!"); |
| 1679 | } |
| 1680 | } catch (IllegalStateException e) { |
| 1681 | Log.e(LOGTAG, "deleteMyNavigationItem", e); |
| 1682 | } finally { |
| 1683 | if (null != cursor) { |
| 1684 | cursor.close(); |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | if (getCurrentTopWebView() != null) { |
| 1689 | getCurrentTopWebView().reload(); |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | private String getNameFromUrl(String itemUrl) { |
| 1694 | ContentResolver cr = this.getContext().getContentResolver(); |
| 1695 | Cursor cursor = null; |
| 1696 | String name = null; |
| 1697 | |
| 1698 | try { |
| 1699 | cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI, |
| 1700 | new String[] { |
| 1701 | MyNavigationUtil.TITLE |
| 1702 | }, "url = ?", new String[] { |
| 1703 | itemUrl |
| 1704 | }, null); |
| 1705 | if (null != cursor && cursor.moveToFirst()) { |
| 1706 | name = cursor.getString(0); |
| 1707 | } else { |
| 1708 | Log.e(LOGTAG, "this item does not exist!"); |
| 1709 | } |
| 1710 | } catch (IllegalStateException e) { |
| 1711 | Log.e(LOGTAG, "getNameFromUrl", e); |
| 1712 | } finally { |
| 1713 | if (null != cursor) { |
| 1714 | cursor.close(); |
| 1715 | } |
| 1716 | } |
| 1717 | return name; |
| 1718 | } |
| 1719 | |
| 1720 | private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) { |
| 1721 | int width = mActivity.getResources().getDimensionPixelOffset( |
| 1722 | R.dimen.myNavigationThumbnailWidth); |
| 1723 | int height = mActivity.getResources().getDimensionPixelOffset( |
| 1724 | R.dimen.myNavigationThumbnailHeight); |
| 1725 | |
| 1726 | final Bitmap bm = createScreenshot(webView, width, height); |
| 1727 | |
| 1728 | if (bm == null) { |
| 1729 | Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!"); |
| 1730 | return; |
| 1731 | } |
| 1732 | |
| 1733 | final ContentResolver cr = mActivity.getContentResolver(); |
| 1734 | new AsyncTask<Void, Void, Void>() { |
| 1735 | @Override |
| 1736 | protected Void doInBackground(Void... unused) { |
| 1737 | ContentResolver cr = mActivity.getContentResolver(); |
| 1738 | Cursor cursor = null; |
| 1739 | try { |
| 1740 | cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI, |
| 1741 | new String[] { |
| 1742 | MyNavigationUtil.ID |
| 1743 | }, "url = ?", new String[] { |
| 1744 | itemUrl |
| 1745 | }, null); |
| 1746 | if (null != cursor && cursor.moveToFirst()) { |
| 1747 | final ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 1748 | bm.compress(Bitmap.CompressFormat.PNG, 100, os); |
| 1749 | |
| 1750 | ContentValues values = new ContentValues(); |
| 1751 | values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray()); |
| 1752 | Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI, |
| 1753 | cursor.getLong(0)); |
| 1754 | Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri); |
| 1755 | cr.update(uri, values, null, null); |
| 1756 | os.close(); |
| 1757 | } |
| 1758 | } catch (IllegalStateException e) { |
| 1759 | Log.e(LOGTAG, "updateMyNavigationThumbnail", e); |
| 1760 | } catch (IOException e) { |
| 1761 | Log.e(LOGTAG, "updateMyNavigationThumbnail", e); |
| 1762 | } finally { |
| 1763 | if (null != cursor) { |
| 1764 | cursor.close(); |
| 1765 | } |
| 1766 | } |
| 1767 | return null; |
| 1768 | } |
| 1769 | }.execute(); |
| 1770 | } |
| 1771 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1772 | /** |
| 1773 | * As the menu can be open when loading state changes |
| 1774 | * we must manually update the state of the stop/reload menu |
| 1775 | * item |
| 1776 | */ |
Michael Kolb | b1fb70c | 2011-11-21 12:38:14 -0800 | [diff] [blame] | 1777 | private void updateInLoadMenuItems(Menu menu, Tab tab) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1778 | if (menu == null) { |
| 1779 | return; |
| 1780 | } |
| 1781 | MenuItem dest = menu.findItem(R.id.stop_reload_menu_id); |
Michael Kolb | b833713 | 2011-11-28 15:07:39 -0800 | [diff] [blame] | 1782 | MenuItem src = ((tab != null) && tab.inPageLoad()) ? |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1783 | menu.findItem(R.id.stop_menu_id): |
| 1784 | menu.findItem(R.id.reload_menu_id); |
| 1785 | if (src != null) { |
| 1786 | dest.setIcon(src.getIcon()); |
| 1787 | dest.setTitle(src.getTitle()); |
| 1788 | } |
| 1789 | } |
| 1790 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 1791 | @Override |
| 1792 | public boolean onPrepareOptionsMenu(Menu menu) { |
Michael Kolb | b1fb70c | 2011-11-21 12:38:14 -0800 | [diff] [blame] | 1793 | updateInLoadMenuItems(menu, getCurrentTab()); |
John Reck | e1a03a3 | 2011-09-14 17:04:16 -0700 | [diff] [blame] | 1794 | // hold on to the menu reference here; it is used by the page callbacks |
| 1795 | // to update the menu based on loading state |
| 1796 | mCachedMenu = menu; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1797 | // Note: setVisible will decide whether an item is visible; while |
| 1798 | // setEnabled() will decide whether an item is enabled, which also means |
| 1799 | // whether the matching shortcut key will function. |
| 1800 | switch (mMenuState) { |
| 1801 | case EMPTY_MENU: |
| 1802 | if (mCurrentMenuState != mMenuState) { |
| 1803 | menu.setGroupVisible(R.id.MAIN_MENU, false); |
| 1804 | menu.setGroupEnabled(R.id.MAIN_MENU, false); |
| 1805 | menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false); |
| 1806 | } |
| 1807 | break; |
| 1808 | default: |
| 1809 | if (mCurrentMenuState != mMenuState) { |
| 1810 | menu.setGroupVisible(R.id.MAIN_MENU, true); |
| 1811 | menu.setGroupEnabled(R.id.MAIN_MENU, true); |
| 1812 | menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true); |
| 1813 | } |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 1814 | updateMenuState(getCurrentTab(), menu); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1815 | break; |
| 1816 | } |
| 1817 | mCurrentMenuState = mMenuState; |
Michael Kolb | 1acef69 | 2011-03-08 14:12:06 -0800 | [diff] [blame] | 1818 | return mUi.onPrepareOptionsMenu(menu); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 1821 | @Override |
| 1822 | public void updateMenuState(Tab tab, Menu menu) { |
| 1823 | boolean canGoBack = false; |
| 1824 | boolean canGoForward = false; |
John Reck | 42229bc | 2011-08-19 13:26:43 -0700 | [diff] [blame] | 1825 | boolean isDesktopUa = false; |
John Reck | e1a03a3 | 2011-09-14 17:04:16 -0700 | [diff] [blame] | 1826 | boolean isLive = false; |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 1827 | if (tab != null) { |
| 1828 | canGoBack = tab.canGoBack(); |
| 1829 | canGoForward = tab.canGoForward(); |
John Reck | 42229bc | 2011-08-19 13:26:43 -0700 | [diff] [blame] | 1830 | isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView()); |
John Reck | e1a03a3 | 2011-09-14 17:04:16 -0700 | [diff] [blame] | 1831 | isLive = !tab.isSnapshot(); |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 1832 | } |
| 1833 | final MenuItem back = menu.findItem(R.id.back_menu_id); |
| 1834 | back.setEnabled(canGoBack); |
| 1835 | |
| 1836 | final MenuItem home = menu.findItem(R.id.homepage_menu_id); |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 1837 | |
| 1838 | final MenuItem forward = menu.findItem(R.id.forward_menu_id); |
| 1839 | forward.setEnabled(canGoForward); |
| 1840 | |
Michael Kolb | b1fb70c | 2011-11-21 12:38:14 -0800 | [diff] [blame] | 1841 | final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id |
| 1842 | : R.id.reload_menu_id); |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 1843 | final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id); |
Michael Kolb | 7ab75ee | 2011-07-14 16:36:38 -0700 | [diff] [blame] | 1844 | if (source != null && dest != null) { |
| 1845 | dest.setTitle(source.getTitle()); |
| 1846 | dest.setIcon(source.getIcon()); |
| 1847 | } |
John Reck | e1a03a3 | 2011-09-14 17:04:16 -0700 | [diff] [blame] | 1848 | menu.setGroupVisible(R.id.NAV_MENU, isLive); |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 1849 | |
| 1850 | // decide whether to show the share link option |
| 1851 | PackageManager pm = mActivity.getPackageManager(); |
| 1852 | Intent send = new Intent(Intent.ACTION_SEND); |
| 1853 | send.setType("text/plain"); |
| 1854 | ResolveInfo ri = pm.resolveActivity(send, |
| 1855 | PackageManager.MATCH_DEFAULT_ONLY); |
| 1856 | menu.findItem(R.id.share_page_menu_id).setVisible(ri != null); |
| 1857 | |
| 1858 | boolean isNavDump = mSettings.enableNavDump(); |
| 1859 | final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id); |
| 1860 | nav.setVisible(isNavDump); |
| 1861 | nav.setEnabled(isNavDump); |
| 1862 | |
| 1863 | boolean showDebugSettings = mSettings.isDebugEnabled(); |
John Reck | 42229bc | 2011-08-19 13:26:43 -0700 | [diff] [blame] | 1864 | final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id); |
| 1865 | uaSwitcher.setChecked(isDesktopUa); |
John Reck | e1a03a3 | 2011-09-14 17:04:16 -0700 | [diff] [blame] | 1866 | menu.setGroupVisible(R.id.LIVE_MENU, isLive); |
| 1867 | menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive); |
kaiyiz | a8b6dbb | 2013-07-29 18:11:22 +0800 | [diff] [blame] | 1868 | |
| 1869 | // history and snapshots item are the members of COMBO menu group, |
| 1870 | // so if show history item, only make snapshots item invisible. |
| 1871 | menu.findItem(R.id.snapshots_menu_id).setVisible(false); |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 1872 | |
Michael Kolb | 7bdee0b | 2011-08-01 11:55:38 -0700 | [diff] [blame] | 1873 | mUi.updateMenuState(tab, menu); |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 1876 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1877 | public boolean onOptionsItemSelected(MenuItem item) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1878 | if (null == getCurrentTopWebView()) { |
| 1879 | return false; |
| 1880 | } |
| 1881 | if (mMenuIsDown) { |
| 1882 | // The shortcut action consumes the MENU. Even if it is still down, |
| 1883 | // it won't trigger the next shortcut action. In the case of the |
| 1884 | // shortcut action triggering a new activity, like Bookmarks, we |
| 1885 | // won't get onKeyUp for MENU. So it is important to reset it here. |
| 1886 | mMenuIsDown = false; |
| 1887 | } |
Michael Kolb | 3ca1275 | 2011-07-20 13:52:25 -0700 | [diff] [blame] | 1888 | if (mUi.onOptionsItemSelected(item)) { |
| 1889 | // ui callback handled it |
| 1890 | return true; |
| 1891 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1892 | switch (item.getItemId()) { |
| 1893 | // -- Main menu |
| 1894 | case R.id.new_tab_menu_id: |
| 1895 | openTabToHomePage(); |
| 1896 | break; |
| 1897 | |
| 1898 | case R.id.incognito_menu_id: |
Michael Kolb | 519d228 | 2011-05-09 17:03:19 -0700 | [diff] [blame] | 1899 | openIncognitoTab(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1900 | break; |
| 1901 | |
Afzal Najam | d4e3331 | 2012-04-26 01:54:01 -0400 | [diff] [blame] | 1902 | case R.id.close_other_tabs_id: |
| 1903 | closeOtherTabs(); |
| 1904 | break; |
| 1905 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1906 | case R.id.goto_menu_id: |
| 1907 | editUrl(); |
| 1908 | break; |
| 1909 | |
| 1910 | case R.id.bookmarks_menu_id: |
Michael Kolb | 315d502 | 2011-10-13 12:47:11 -0700 | [diff] [blame] | 1911 | bookmarksOrHistoryPicker(ComboViews.Bookmarks); |
| 1912 | break; |
| 1913 | |
| 1914 | case R.id.history_menu_id: |
| 1915 | bookmarksOrHistoryPicker(ComboViews.History); |
| 1916 | break; |
| 1917 | |
| 1918 | case R.id.snapshots_menu_id: |
| 1919 | bookmarksOrHistoryPicker(ComboViews.Snapshots); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1920 | break; |
| 1921 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1922 | case R.id.add_bookmark_menu_id: |
Michael Kolb | 80f7508 | 2012-04-10 10:50:06 -0700 | [diff] [blame] | 1923 | bookmarkCurrentPage(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1924 | break; |
| 1925 | |
| 1926 | case R.id.stop_reload_menu_id: |
Michael Kolb | b1fb70c | 2011-11-21 12:38:14 -0800 | [diff] [blame] | 1927 | if (isInLoad()) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1928 | stopLoading(); |
| 1929 | } else { |
| 1930 | getCurrentTopWebView().reload(); |
| 1931 | } |
| 1932 | break; |
| 1933 | |
| 1934 | case R.id.back_menu_id: |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 1935 | getCurrentTab().goBack(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1936 | break; |
| 1937 | |
| 1938 | case R.id.forward_menu_id: |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 1939 | getCurrentTab().goForward(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1940 | break; |
| 1941 | |
| 1942 | case R.id.close_menu_id: |
| 1943 | // Close the subwindow if it exists. |
| 1944 | if (mTabControl.getCurrentSubWindow() != null) { |
| 1945 | dismissSubWindow(mTabControl.getCurrentTab()); |
| 1946 | break; |
| 1947 | } |
| 1948 | closeCurrentTab(); |
| 1949 | break; |
| 1950 | |
kaiyiz | a8b6dbb | 2013-07-29 18:11:22 +0800 | [diff] [blame] | 1951 | case R.id.exit_menu_id: |
| 1952 | String ret = SystemProperties.get("persist.debug.browsermonkeytest"); |
| 1953 | if (ret != null && ret.equals("enable")) |
| 1954 | break; |
luxiaol | b40014b | 2013-07-19 10:01:43 +0800 | [diff] [blame] | 1955 | showExitDialog(mActivity); |
| 1956 | return true; |
kaiyiz | a8b6dbb | 2013-07-29 18:11:22 +0800 | [diff] [blame] | 1957 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1958 | case R.id.homepage_menu_id: |
| 1959 | Tab current = mTabControl.getCurrentTab(); |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 1960 | loadUrl(current, mSettings.getHomePage()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1961 | break; |
| 1962 | |
| 1963 | case R.id.preferences_menu_id: |
Michael Kolb | 80f7508 | 2012-04-10 10:50:06 -0700 | [diff] [blame] | 1964 | openPreferences(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1965 | break; |
| 1966 | |
| 1967 | case R.id.find_menu_id: |
Michael Kolb | e11b921 | 2012-04-13 13:39:54 -0700 | [diff] [blame] | 1968 | findOnPage(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1969 | break; |
| 1970 | |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 1971 | case R.id.save_snapshot_menu_id: |
| 1972 | final Tab source = getTabControl().getCurrentTab(); |
John Reck | f33b163 | 2011-06-04 20:00:23 -0700 | [diff] [blame] | 1973 | if (source == null) break; |
John Reck | 68234a9 | 2012-04-19 15:27:12 -0700 | [diff] [blame] | 1974 | new SaveSnapshotTask(source).execute(); |
Leon Scroggins | ac99384 | 2011-02-02 12:54:07 -0500 | [diff] [blame] | 1975 | break; |
| 1976 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1977 | case R.id.page_info_menu_id: |
Michael Kolb | 315d502 | 2011-10-13 12:47:11 -0700 | [diff] [blame] | 1978 | showPageInfo(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1979 | break; |
| 1980 | |
John Reck | e1a03a3 | 2011-09-14 17:04:16 -0700 | [diff] [blame] | 1981 | case R.id.snapshot_go_live: |
| 1982 | goLive(); |
| 1983 | return true; |
| 1984 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1985 | case R.id.share_page_menu_id: |
| 1986 | Tab currentTab = mTabControl.getCurrentTab(); |
| 1987 | if (null == currentTab) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1988 | return false; |
| 1989 | } |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 1990 | shareCurrentPage(currentTab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1991 | break; |
| 1992 | |
| 1993 | case R.id.dump_nav_menu_id: |
| 1994 | getCurrentTopWebView().debugDump(); |
| 1995 | break; |
| 1996 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1997 | case R.id.zoom_in_menu_id: |
| 1998 | getCurrentTopWebView().zoomIn(); |
| 1999 | break; |
| 2000 | |
| 2001 | case R.id.zoom_out_menu_id: |
| 2002 | getCurrentTopWebView().zoomOut(); |
| 2003 | break; |
| 2004 | |
| 2005 | case R.id.view_downloads_menu_id: |
| 2006 | viewDownloads(); |
| 2007 | break; |
| 2008 | |
John Reck | 42229bc | 2011-08-19 13:26:43 -0700 | [diff] [blame] | 2009 | case R.id.ua_desktop_menu_id: |
Michael Kolb | 80f7508 | 2012-04-10 10:50:06 -0700 | [diff] [blame] | 2010 | toggleUserAgent(); |
John Reck | 42229bc | 2011-08-19 13:26:43 -0700 | [diff] [blame] | 2011 | break; |
| 2012 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2013 | case R.id.window_one_menu_id: |
| 2014 | case R.id.window_two_menu_id: |
| 2015 | case R.id.window_three_menu_id: |
| 2016 | case R.id.window_four_menu_id: |
| 2017 | case R.id.window_five_menu_id: |
| 2018 | case R.id.window_six_menu_id: |
| 2019 | case R.id.window_seven_menu_id: |
| 2020 | case R.id.window_eight_menu_id: |
| 2021 | { |
| 2022 | int menuid = item.getItemId(); |
| 2023 | for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) { |
| 2024 | if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) { |
| 2025 | Tab desiredTab = mTabControl.getTab(id); |
| 2026 | if (desiredTab != null && |
| 2027 | desiredTab != mTabControl.getCurrentTab()) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2028 | switchToTab(desiredTab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2029 | } |
| 2030 | break; |
| 2031 | } |
| 2032 | } |
| 2033 | } |
| 2034 | break; |
| 2035 | |
| 2036 | default: |
| 2037 | return false; |
| 2038 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2039 | return true; |
| 2040 | } |
| 2041 | |
John Reck | 68234a9 | 2012-04-19 15:27:12 -0700 | [diff] [blame] | 2042 | private class SaveSnapshotTask extends AsyncTask<Void, Void, Long> |
| 2043 | implements OnCancelListener { |
| 2044 | |
| 2045 | private Tab mTab; |
| 2046 | private Dialog mProgressDialog; |
| 2047 | private ContentValues mValues; |
| 2048 | |
| 2049 | private SaveSnapshotTask(Tab tab) { |
| 2050 | mTab = tab; |
| 2051 | } |
| 2052 | |
| 2053 | @Override |
| 2054 | protected void onPreExecute() { |
| 2055 | CharSequence message = mActivity.getText(R.string.saving_snapshot); |
| 2056 | mProgressDialog = ProgressDialog.show(mActivity, null, message, |
| 2057 | true, true, this); |
| 2058 | mValues = mTab.createSnapshotValues(); |
| 2059 | } |
| 2060 | |
| 2061 | @Override |
| 2062 | protected Long doInBackground(Void... params) { |
| 2063 | if (!mTab.saveViewState(mValues)) { |
| 2064 | return null; |
| 2065 | } |
| 2066 | if (isCancelled()) { |
| 2067 | String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH); |
| 2068 | File file = mActivity.getFileStreamPath(path); |
| 2069 | if (!file.delete()) { |
| 2070 | file.deleteOnExit(); |
| 2071 | } |
| 2072 | return null; |
| 2073 | } |
| 2074 | final ContentResolver cr = mActivity.getContentResolver(); |
| 2075 | Uri result = cr.insert(Snapshots.CONTENT_URI, mValues); |
| 2076 | if (result == null) { |
| 2077 | return null; |
| 2078 | } |
| 2079 | long id = ContentUris.parseId(result); |
| 2080 | return id; |
| 2081 | } |
| 2082 | |
| 2083 | @Override |
| 2084 | protected void onPostExecute(Long id) { |
| 2085 | if (isCancelled()) { |
| 2086 | return; |
| 2087 | } |
| 2088 | mProgressDialog.dismiss(); |
| 2089 | if (id == null) { |
| 2090 | Toast.makeText(mActivity, R.string.snapshot_failed, |
| 2091 | Toast.LENGTH_SHORT).show(); |
| 2092 | return; |
| 2093 | } |
| 2094 | Bundle b = new Bundle(); |
| 2095 | b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id); |
| 2096 | mUi.showComboView(ComboViews.Snapshots, b); |
| 2097 | } |
| 2098 | |
| 2099 | @Override |
| 2100 | public void onCancel(DialogInterface dialog) { |
| 2101 | cancel(true); |
| 2102 | } |
| 2103 | } |
| 2104 | |
Michael Kolb | 80f7508 | 2012-04-10 10:50:06 -0700 | [diff] [blame] | 2105 | @Override |
| 2106 | public void toggleUserAgent() { |
| 2107 | WebView web = getCurrentWebView(); |
| 2108 | mSettings.toggleDesktopUseragent(web); |
| 2109 | web.loadUrl(web.getOriginalUrl()); |
| 2110 | } |
| 2111 | |
| 2112 | @Override |
| 2113 | public void findOnPage() { |
| 2114 | getCurrentTopWebView().showFindDialog(null, true); |
| 2115 | } |
| 2116 | |
| 2117 | @Override |
| 2118 | public void openPreferences() { |
| 2119 | Intent intent = new Intent(mActivity, BrowserPreferencesPage.class); |
| 2120 | intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE, |
| 2121 | getCurrentTopWebView().getUrl()); |
| 2122 | mActivity.startActivityForResult(intent, PREFERENCES_PAGE); |
| 2123 | } |
| 2124 | |
| 2125 | @Override |
| 2126 | public void bookmarkCurrentPage() { |
| 2127 | Intent bookmarkIntent = createBookmarkCurrentPageIntent(false); |
| 2128 | if (bookmarkIntent != null) { |
| 2129 | mActivity.startActivity(bookmarkIntent); |
| 2130 | } |
| 2131 | } |
| 2132 | |
John Reck | e1a03a3 | 2011-09-14 17:04:16 -0700 | [diff] [blame] | 2133 | private void goLive() { |
| 2134 | Tab t = getCurrentTab(); |
| 2135 | t.loadUrl(t.getUrl(), null); |
| 2136 | } |
| 2137 | |
luxiaol | b40014b | 2013-07-19 10:01:43 +0800 | [diff] [blame] | 2138 | private void showExitDialog(final Activity activity) { |
| 2139 | new AlertDialog.Builder(activity) |
| 2140 | .setTitle(R.string.exit_browser_title) |
| 2141 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 2142 | .setMessage(R.string.exit_browser_msg) |
| 2143 | .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() { |
| 2144 | public void onClick(DialogInterface dialog, int which) { |
| 2145 | activity.moveTaskToBack(true); |
| 2146 | dialog.dismiss(); |
| 2147 | } |
| 2148 | }) |
| 2149 | .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() { |
| 2150 | public void onClick(DialogInterface dialog, int which) { |
| 2151 | activity.finish(); |
| 2152 | mHandler.postDelayed(new Runnable() { |
| 2153 | @Override |
| 2154 | public void run() { |
| 2155 | // TODO Auto-generated method stub |
| 2156 | mCrashRecoveryHandler.clearState(true); |
| 2157 | int pid = android.os.Process.myPid(); |
| 2158 | android.os.Process.killProcess(pid); |
| 2159 | } |
| 2160 | }, 300); |
| 2161 | dialog.dismiss(); |
| 2162 | } |
| 2163 | }) |
| 2164 | .show(); |
| 2165 | } |
| 2166 | |
Michael Kolb | 315d502 | 2011-10-13 12:47:11 -0700 | [diff] [blame] | 2167 | @Override |
| 2168 | public void showPageInfo() { |
| 2169 | mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null); |
| 2170 | } |
| 2171 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 2172 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2173 | public boolean onContextItemSelected(MenuItem item) { |
John Reck | dbf57df | 2010-11-09 16:34:03 -0800 | [diff] [blame] | 2174 | // Let the History and Bookmark fragments handle menus they created. |
| 2175 | if (item.getGroupId() == R.id.CONTEXT_MENU) { |
| 2176 | return false; |
| 2177 | } |
| 2178 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2179 | int id = item.getItemId(); |
| 2180 | boolean result = true; |
| 2181 | switch (id) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2182 | // -- Browser context menu |
| 2183 | case R.id.open_context_menu_id: |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2184 | case R.id.save_link_context_menu_id: |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2185 | case R.id.copy_link_context_menu_id: |
| 2186 | final WebView webView = getCurrentTopWebView(); |
| 2187 | if (null == webView) { |
| 2188 | result = false; |
| 2189 | break; |
| 2190 | } |
| 2191 | final HashMap<String, WebView> hrefMap = |
| 2192 | new HashMap<String, WebView>(); |
| 2193 | hrefMap.put("webview", webView); |
| 2194 | final Message msg = mHandler.obtainMessage( |
| 2195 | FOCUS_NODE_HREF, id, 0, hrefMap); |
| 2196 | webView.requestFocusNodeHref(msg); |
| 2197 | break; |
| 2198 | |
| 2199 | default: |
| 2200 | // For other context menus |
| 2201 | result = onOptionsItemSelected(item); |
| 2202 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2203 | return result; |
| 2204 | } |
| 2205 | |
| 2206 | /** |
| 2207 | * support programmatically opening the context menu |
| 2208 | */ |
| 2209 | public void openContextMenu(View view) { |
| 2210 | mActivity.openContextMenu(view); |
| 2211 | } |
| 2212 | |
| 2213 | /** |
| 2214 | * programmatically open the options menu |
| 2215 | */ |
| 2216 | public void openOptionsMenu() { |
| 2217 | mActivity.openOptionsMenu(); |
| 2218 | } |
| 2219 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 2220 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2221 | public boolean onMenuOpened(int featureId, Menu menu) { |
| 2222 | if (mOptionsMenuOpen) { |
| 2223 | if (mConfigChanged) { |
| 2224 | // We do not need to make any changes to the state of the |
| 2225 | // title bar, since the only thing that happened was a |
| 2226 | // change in orientation |
| 2227 | mConfigChanged = false; |
| 2228 | } else { |
| 2229 | if (!mExtendedMenuOpen) { |
| 2230 | mExtendedMenuOpen = true; |
| 2231 | mUi.onExtendedMenuOpened(); |
| 2232 | } else { |
| 2233 | // Switching the menu back to icon view, so show the |
| 2234 | // title bar once again. |
| 2235 | mExtendedMenuOpen = false; |
Michael Kolb | b1fb70c | 2011-11-21 12:38:14 -0800 | [diff] [blame] | 2236 | mUi.onExtendedMenuClosed(isInLoad()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2237 | } |
| 2238 | } |
| 2239 | } else { |
| 2240 | // The options menu is closed, so open it, and show the title |
| 2241 | mOptionsMenuOpen = true; |
| 2242 | mConfigChanged = false; |
| 2243 | mExtendedMenuOpen = false; |
| 2244 | mUi.onOptionsMenuOpened(); |
| 2245 | } |
| 2246 | return true; |
| 2247 | } |
| 2248 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 2249 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2250 | public void onOptionsMenuClosed(Menu menu) { |
| 2251 | mOptionsMenuOpen = false; |
Michael Kolb | b1fb70c | 2011-11-21 12:38:14 -0800 | [diff] [blame] | 2252 | mUi.onOptionsMenuClosed(isInLoad()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 2255 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2256 | public void onContextMenuClosed(Menu menu) { |
Michael Kolb | b1fb70c | 2011-11-21 12:38:14 -0800 | [diff] [blame] | 2257 | mUi.onContextMenuClosed(menu, isInLoad()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | // Helper method for getting the top window. |
| 2261 | @Override |
| 2262 | public WebView getCurrentTopWebView() { |
| 2263 | return mTabControl.getCurrentTopWebView(); |
| 2264 | } |
| 2265 | |
| 2266 | @Override |
| 2267 | public WebView getCurrentWebView() { |
| 2268 | return mTabControl.getCurrentWebView(); |
| 2269 | } |
| 2270 | |
| 2271 | /* |
| 2272 | * This method is called as a result of the user selecting the options |
| 2273 | * menu to see the download window. It shows the download window on top of |
| 2274 | * the current window. |
| 2275 | */ |
| 2276 | void viewDownloads() { |
| 2277 | Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS); |
| 2278 | mActivity.startActivity(intent); |
| 2279 | } |
| 2280 | |
John Reck | 30b065e | 2011-07-19 10:58:05 -0700 | [diff] [blame] | 2281 | int getActionModeHeight() { |
| 2282 | TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes( |
| 2283 | new int[] { android.R.attr.actionBarSize }); |
| 2284 | int size = (int) actionBarSizeTypedArray.getDimension(0, 0f); |
| 2285 | actionBarSizeTypedArray.recycle(); |
| 2286 | return size; |
| 2287 | } |
| 2288 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2289 | // action mode |
| 2290 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 2291 | @Override |
| 2292 | public void onActionModeStarted(ActionMode mode) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2293 | mUi.onActionModeStarted(mode); |
| 2294 | mActionMode = mode; |
| 2295 | } |
| 2296 | |
| 2297 | /* |
| 2298 | * True if a custom ActionMode (i.e. find or select) is in use. |
| 2299 | */ |
| 2300 | @Override |
| 2301 | public boolean isInCustomActionMode() { |
| 2302 | return mActionMode != null; |
| 2303 | } |
| 2304 | |
| 2305 | /* |
| 2306 | * End the current ActionMode. |
| 2307 | */ |
| 2308 | @Override |
| 2309 | public void endActionMode() { |
| 2310 | if (mActionMode != null) { |
| 2311 | mActionMode.finish(); |
| 2312 | } |
| 2313 | } |
| 2314 | |
| 2315 | /* |
| 2316 | * Called by find and select when they are finished. Replace title bars |
| 2317 | * as necessary. |
| 2318 | */ |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 2319 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2320 | public void onActionModeFinished(ActionMode mode) { |
| 2321 | if (!isInCustomActionMode()) return; |
Michael Kolb | b1fb70c | 2011-11-21 12:38:14 -0800 | [diff] [blame] | 2322 | mUi.onActionModeFinished(isInLoad()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2323 | mActionMode = null; |
| 2324 | } |
| 2325 | |
| 2326 | boolean isInLoad() { |
Michael Kolb | c5b0b2d | 2011-11-29 16:13:35 -0800 | [diff] [blame] | 2327 | final Tab tab = getCurrentTab(); |
| 2328 | return (tab != null) && tab.inPageLoad(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | // bookmark handling |
| 2332 | |
| 2333 | /** |
| 2334 | * add the current page as a bookmark to the given folder id |
| 2335 | * @param folderId use -1 for the default folder |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 2336 | * @param editExisting If true, check to see whether the site is already |
Leon Scroggins | bdff8a7 | 2011-02-11 15:49:04 -0500 | [diff] [blame] | 2337 | * bookmarked, and if it is, edit that bookmark. If false, and |
| 2338 | * the site is already bookmarked, do not attempt to edit the |
| 2339 | * existing bookmark. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2340 | */ |
| 2341 | @Override |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 2342 | public Intent createBookmarkCurrentPageIntent(boolean editExisting) { |
John Reck | a60fffa | 2011-09-06 16:30:29 -0700 | [diff] [blame] | 2343 | WebView w = getCurrentTopWebView(); |
| 2344 | if (w == null) { |
| 2345 | return null; |
| 2346 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2347 | Intent i = new Intent(mActivity, |
| 2348 | AddBookmarkPage.class); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2349 | i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl()); |
| 2350 | i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle()); |
| 2351 | String touchIconUrl = w.getTouchIconUrl(); |
| 2352 | if (touchIconUrl != null) { |
| 2353 | i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl); |
| 2354 | WebSettings settings = w.getSettings(); |
| 2355 | if (settings != null) { |
| 2356 | i.putExtra(AddBookmarkPage.USER_AGENT, |
| 2357 | settings.getUserAgentString()); |
| 2358 | } |
| 2359 | } |
| 2360 | i.putExtra(BrowserContract.Bookmarks.THUMBNAIL, |
| 2361 | createScreenshot(w, getDesiredThumbnailWidth(mActivity), |
| 2362 | getDesiredThumbnailHeight(mActivity))); |
| 2363 | i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon()); |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 2364 | if (editExisting) { |
Leon Scroggins | bdff8a7 | 2011-02-11 15:49:04 -0500 | [diff] [blame] | 2365 | i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true); |
| 2366 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2367 | // Put the dialog at the upper right of the screen, covering the |
| 2368 | // star on the title bar. |
| 2369 | i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP); |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 2370 | return i; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | // file chooser |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 2374 | @Override |
Ben Murdoch | 8cad413 | 2012-01-11 10:56:43 +0000 | [diff] [blame] | 2375 | public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2376 | mUploadHandler = new UploadHandler(this); |
Ben Murdoch | 8cad413 | 2012-01-11 10:56:43 +0000 | [diff] [blame] | 2377 | mUploadHandler.openFileChooser(uploadMsg, acceptType, capture); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | // thumbnails |
| 2381 | |
| 2382 | /** |
| 2383 | * Return the desired width for thumbnail screenshots, which are stored in |
| 2384 | * the database, and used on the bookmarks screen. |
| 2385 | * @param context Context for finding out the density of the screen. |
| 2386 | * @return desired width for thumbnail screenshot. |
| 2387 | */ |
| 2388 | static int getDesiredThumbnailWidth(Context context) { |
| 2389 | return context.getResources().getDimensionPixelOffset( |
| 2390 | R.dimen.bookmarkThumbnailWidth); |
| 2391 | } |
| 2392 | |
| 2393 | /** |
| 2394 | * Return the desired height for thumbnail screenshots, which are stored in |
| 2395 | * the database, and used on the bookmarks screen. |
| 2396 | * @param context Context for finding out the density of the screen. |
| 2397 | * @return desired height for thumbnail screenshot. |
| 2398 | */ |
| 2399 | static int getDesiredThumbnailHeight(Context context) { |
| 2400 | return context.getResources().getDimensionPixelOffset( |
| 2401 | R.dimen.bookmarkThumbnailHeight); |
| 2402 | } |
| 2403 | |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 2404 | static Bitmap createScreenshot(WebView view, int width, int height) { |
John Reck | d7dd9b2 | 2011-08-30 09:18:29 -0700 | [diff] [blame] | 2405 | if (view == null || view.getContentHeight() == 0 |
| 2406 | || view.getContentWidth() == 0) { |
| 2407 | return null; |
| 2408 | } |
John Reck | 5c6ac2f | 2011-01-05 10:18:03 -0800 | [diff] [blame] | 2409 | // We render to a bitmap 2x the desired size so that we can then |
| 2410 | // re-scale it with filtering since canvas.scale doesn't filter |
| 2411 | // This helps reduce aliasing at the cost of being slightly blurry |
| 2412 | final int filter_scale = 2; |
John Reck | d7dd9b2 | 2011-08-30 09:18:29 -0700 | [diff] [blame] | 2413 | int scaledWidth = width * filter_scale; |
| 2414 | int scaledHeight = height * filter_scale; |
| 2415 | if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth |
| 2416 | || sThumbnailBitmap.getHeight() != scaledHeight) { |
| 2417 | if (sThumbnailBitmap != null) { |
| 2418 | sThumbnailBitmap.recycle(); |
| 2419 | sThumbnailBitmap = null; |
| 2420 | } |
| 2421 | sThumbnailBitmap = |
| 2422 | Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2423 | } |
John Reck | d7dd9b2 | 2011-08-30 09:18:29 -0700 | [diff] [blame] | 2424 | Canvas canvas = new Canvas(sThumbnailBitmap); |
| 2425 | int contentWidth = view.getContentWidth(); |
| 2426 | float overviewScale = scaledWidth / (view.getScale() * contentWidth); |
| 2427 | if (view instanceof BrowserWebView) { |
| 2428 | int dy = -((BrowserWebView)view).getTitleHeight(); |
| 2429 | canvas.translate(0, dy * overviewScale); |
| 2430 | } |
| 2431 | |
| 2432 | canvas.scale(overviewScale, overviewScale); |
| 2433 | |
| 2434 | if (view instanceof BrowserWebView) { |
| 2435 | ((BrowserWebView)view).drawContent(canvas); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2436 | } else { |
John Reck | d7dd9b2 | 2011-08-30 09:18:29 -0700 | [diff] [blame] | 2437 | view.draw(canvas); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2438 | } |
John Reck | d7dd9b2 | 2011-08-30 09:18:29 -0700 | [diff] [blame] | 2439 | Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap, |
| 2440 | width, height, true); |
Dianne Hackborn | 43cfe8a | 2011-08-02 16:59:35 -0700 | [diff] [blame] | 2441 | canvas.setBitmap(null); |
John Reck | 5c6ac2f | 2011-01-05 10:18:03 -0800 | [diff] [blame] | 2442 | return ret; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2443 | } |
| 2444 | |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2445 | private void updateScreenshot(Tab tab) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2446 | // If this is a bookmarked site, add a screenshot to the database. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2447 | // FIXME: Would like to make sure there is actually something to |
| 2448 | // draw, but the API for that (WebViewCore.pictureReady()) is not |
| 2449 | // currently accessible here. |
| 2450 | |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2451 | WebView view = tab.getWebView(); |
John Reck | 7a59120 | 2011-02-16 15:44:01 -0800 | [diff] [blame] | 2452 | if (view == null) { |
| 2453 | // Tab was destroyed |
| 2454 | return; |
| 2455 | } |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2456 | final String url = tab.getUrl(); |
| 2457 | final String originalUrl = view.getOriginalUrl(); |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 2458 | final String thumbnailUrl = mUpdateMyNavThumbnailUrl; |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2459 | if (TextUtils.isEmpty(url)) { |
| 2460 | return; |
| 2461 | } |
| 2462 | |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 2463 | //update My Navigation Thumbnails |
| 2464 | boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url); |
| 2465 | if (isMyNavigationUrl) { |
| 2466 | updateMyNavigationThumbnail(url, view); |
| 2467 | } |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2468 | // Only update thumbnails for web urls (http(s)://), not for |
| 2469 | // about:, javascript:, data:, etc... |
| 2470 | // Unless it is a bookmarked site, then always update |
| 2471 | if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) { |
| 2472 | return; |
| 2473 | } |
| 2474 | |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 2475 | if (url != null && mUpdateMyNavThumbnailUrl != null |
| 2476 | && Patterns.WEB_URL.matcher(url).matches() |
| 2477 | && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) { |
| 2478 | String urlHost = (new WebAddress(url)).getHost(); |
| 2479 | String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost(); |
| 2480 | if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null |
| 2481 | || bookmarkHost.length() == 0) { |
| 2482 | return; |
| 2483 | } |
| 2484 | String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length()); |
| 2485 | String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'), |
| 2486 | bookmarkHost.length()); |
| 2487 | Log.d(LOGTAG, "addressUrl domain is " + urlDomain); |
| 2488 | Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain); |
| 2489 | if (!bookmarkDomain.equals(urlDomain)) { |
| 2490 | return; |
| 2491 | } |
| 2492 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2493 | final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity), |
| 2494 | getDesiredThumbnailHeight(mActivity)); |
| 2495 | if (bm == null) { |
| 2496 | return; |
| 2497 | } |
| 2498 | |
| 2499 | final ContentResolver cr = mActivity.getContentResolver(); |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2500 | new AsyncTask<Void, Void, Void>() { |
| 2501 | @Override |
| 2502 | protected Void doInBackground(Void... unused) { |
| 2503 | Cursor cursor = null; |
| 2504 | try { |
| 2505 | // TODO: Clean this up |
kaiyiz | 6e5b3e0 | 2013-08-19 20:02:01 +0800 | [diff] [blame^] | 2506 | cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, |
| 2507 | mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url) |
| 2508 | : url); |
| 2509 | if (mUpdateMyNavThumbnail) { |
| 2510 | mUpdateMyNavThumbnail = false; |
| 2511 | mUpdateMyNavThumbnailUrl = null; |
| 2512 | } |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2513 | if (cursor != null && cursor.moveToFirst()) { |
| 2514 | final ByteArrayOutputStream os = |
| 2515 | new ByteArrayOutputStream(); |
| 2516 | bm.compress(Bitmap.CompressFormat.PNG, 100, os); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2517 | |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2518 | ContentValues values = new ContentValues(); |
| 2519 | values.put(Images.THUMBNAIL, os.toByteArray()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2520 | |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2521 | do { |
John Reck | 617fd83 | 2011-02-16 14:35:59 -0800 | [diff] [blame] | 2522 | values.put(Images.URL, cursor.getString(0)); |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2523 | cr.update(Images.CONTENT_URI, values, null, null); |
| 2524 | } while (cursor.moveToNext()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2525 | } |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2526 | } catch (IllegalStateException e) { |
| 2527 | // Ignore |
Mattias Nilsson | 561d195 | 2011-10-04 10:18:50 +0200 | [diff] [blame] | 2528 | } catch (SQLiteException s) { |
| 2529 | // Added for possible error when user tries to remove the same bookmark |
| 2530 | // that is being updated with a screen shot |
| 2531 | Log.w(LOGTAG, "Error when running updateScreenshot ", s); |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2532 | } finally { |
| 2533 | if (cursor != null) cursor.close(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2534 | } |
John Reck | 34ef267 | 2011-02-10 11:30:55 -0800 | [diff] [blame] | 2535 | return null; |
| 2536 | } |
| 2537 | }.execute(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | private class Copy implements OnMenuItemClickListener { |
| 2541 | private CharSequence mText; |
| 2542 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 2543 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2544 | public boolean onMenuItemClick(MenuItem item) { |
| 2545 | copy(mText); |
| 2546 | return true; |
| 2547 | } |
| 2548 | |
| 2549 | public Copy(CharSequence toCopy) { |
| 2550 | mText = toCopy; |
| 2551 | } |
| 2552 | } |
| 2553 | |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 2554 | private static class Download implements OnMenuItemClickListener { |
| 2555 | private Activity mActivity; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2556 | private String mText; |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 2557 | private boolean mPrivateBrowsing; |
Andreas Sandblad | 8e4ce66 | 2012-06-11 11:02:49 +0200 | [diff] [blame] | 2558 | private String mUserAgent; |
George Mount | 387d45d | 2011-10-07 15:57:53 -0700 | [diff] [blame] | 2559 | private static final String FALLBACK_EXTENSION = "dat"; |
| 2560 | private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-"; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2561 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 2562 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2563 | public boolean onMenuItemClick(MenuItem item) { |
George Mount | 387d45d | 2011-10-07 15:57:53 -0700 | [diff] [blame] | 2564 | if (DataUri.isDataUri(mText)) { |
| 2565 | saveDataUri(); |
| 2566 | } else { |
Andreas Sandblad | 8e4ce66 | 2012-06-11 11:02:49 +0200 | [diff] [blame] | 2567 | DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent, |
luxiaol | 62677b0 | 2013-07-22 07:54:49 +0800 | [diff] [blame] | 2568 | null, null, null, mPrivateBrowsing, 0); |
George Mount | 387d45d | 2011-10-07 15:57:53 -0700 | [diff] [blame] | 2569 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2570 | return true; |
| 2571 | } |
| 2572 | |
Andreas Sandblad | 8e4ce66 | 2012-06-11 11:02:49 +0200 | [diff] [blame] | 2573 | public Download(Activity activity, String toDownload, boolean privateBrowsing, |
| 2574 | String userAgent) { |
Leon Scroggins | 63c0266 | 2010-11-18 15:16:27 -0500 | [diff] [blame] | 2575 | mActivity = activity; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2576 | mText = toDownload; |
Kristian Monsen | bc5cc75 | 2011-03-02 13:14:03 +0000 | [diff] [blame] | 2577 | mPrivateBrowsing = privateBrowsing; |
Andreas Sandblad | 8e4ce66 | 2012-06-11 11:02:49 +0200 | [diff] [blame] | 2578 | mUserAgent = userAgent; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2579 | } |
George Mount | 387d45d | 2011-10-07 15:57:53 -0700 | [diff] [blame] | 2580 | |
| 2581 | /** |
| 2582 | * Treats mText as a data URI and writes its contents to a file |
| 2583 | * based on the current time. |
| 2584 | */ |
| 2585 | private void saveDataUri() { |
| 2586 | FileOutputStream outputStream = null; |
| 2587 | try { |
| 2588 | DataUri uri = new DataUri(mText); |
| 2589 | File target = getTarget(uri); |
| 2590 | outputStream = new FileOutputStream(target); |
| 2591 | outputStream.write(uri.getData()); |
| 2592 | final DownloadManager manager = |
| 2593 | (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE); |
| 2594 | manager.addCompletedDownload(target.getName(), |
| 2595 | mActivity.getTitle().toString(), false, |
| 2596 | uri.getMimeType(), target.getAbsolutePath(), |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 2597 | uri.getData().length, true); |
George Mount | 387d45d | 2011-10-07 15:57:53 -0700 | [diff] [blame] | 2598 | } catch (IOException e) { |
| 2599 | Log.e(LOGTAG, "Could not save data URL"); |
| 2600 | } finally { |
| 2601 | if (outputStream != null) { |
| 2602 | try { |
| 2603 | outputStream.close(); |
| 2604 | } catch (IOException e) { |
| 2605 | // ignore close errors |
| 2606 | } |
| 2607 | } |
| 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | /** |
| 2612 | * Creates a File based on the current time stamp and uses |
| 2613 | * the mime type of the DataUri to get the extension. |
| 2614 | */ |
| 2615 | private File getTarget(DataUri uri) throws IOException { |
| 2616 | File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS); |
Johan Redestig | aa67618 | 2012-10-03 13:33:01 +0200 | [diff] [blame] | 2617 | DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US); |
George Mount | 387d45d | 2011-10-07 15:57:53 -0700 | [diff] [blame] | 2618 | String nameBase = format.format(new Date()); |
| 2619 | String mimeType = uri.getMimeType(); |
| 2620 | MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); |
| 2621 | String extension = mimeTypeMap.getExtensionFromMimeType(mimeType); |
| 2622 | if (extension == null) { |
| 2623 | Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType); |
| 2624 | extension = FALLBACK_EXTENSION; |
| 2625 | } |
| 2626 | extension = "." + extension; // createTempFile needs the '.' |
| 2627 | File targetFile = File.createTempFile(nameBase, extension, dir); |
| 2628 | return targetFile; |
| 2629 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2630 | } |
| 2631 | |
Cary Clark | 8974d28 | 2010-11-22 10:46:05 -0500 | [diff] [blame] | 2632 | private static class SelectText implements OnMenuItemClickListener { |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 2633 | private WebViewClassic mWebView; |
Cary Clark | 8974d28 | 2010-11-22 10:46:05 -0500 | [diff] [blame] | 2634 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 2635 | @Override |
Cary Clark | 8974d28 | 2010-11-22 10:46:05 -0500 | [diff] [blame] | 2636 | public boolean onMenuItemClick(MenuItem item) { |
| 2637 | if (mWebView != null) { |
| 2638 | return mWebView.selectText(); |
| 2639 | } |
| 2640 | return false; |
| 2641 | } |
| 2642 | |
| 2643 | public SelectText(WebView webView) { |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 2644 | if (BrowserWebView.isClassic()) { |
| 2645 | mWebView = WebViewClassic.fromWebView(webView); |
| 2646 | } |
Cary Clark | 8974d28 | 2010-11-22 10:46:05 -0500 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | } |
| 2650 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2651 | /********************** TODO: UI stuff *****************************/ |
| 2652 | |
| 2653 | // these methods have been copied, they still need to be cleaned up |
| 2654 | |
| 2655 | /****************** tabs ***************************************************/ |
| 2656 | |
| 2657 | // basic tab interactions: |
| 2658 | |
| 2659 | // it is assumed that tabcontrol already knows about the tab |
| 2660 | protected void addTab(Tab tab) { |
| 2661 | mUi.addTab(tab); |
| 2662 | } |
| 2663 | |
| 2664 | protected void removeTab(Tab tab) { |
| 2665 | mUi.removeTab(tab); |
| 2666 | mTabControl.removeTab(tab); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 2667 | mCrashRecoveryHandler.backupState(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2668 | } |
| 2669 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 2670 | @Override |
| 2671 | public void setActiveTab(Tab tab) { |
Michael Kolb | cd424e9 | 2011-02-24 10:26:26 -0800 | [diff] [blame] | 2672 | // monkey protection against delayed start |
| 2673 | if (tab != null) { |
| 2674 | mTabControl.setCurrentTab(tab); |
| 2675 | // the tab is guaranteed to have a webview after setCurrentTab |
| 2676 | mUi.setActiveTab(tab); |
| 2677 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2678 | } |
| 2679 | |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 2680 | protected void closeEmptyTab() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2681 | Tab current = mTabControl.getCurrentTab(); |
| 2682 | if (current != null |
| 2683 | && current.getWebView().copyBackForwardList().getSize() == 0) { |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 2684 | closeCurrentTab(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2685 | } |
| 2686 | } |
| 2687 | |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2688 | protected void reuseTab(Tab appTab, UrlData urlData) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2689 | // Dismiss the subwindow if applicable. |
| 2690 | dismissSubWindow(appTab); |
| 2691 | // Since we might kill the WebView, remove it from the |
| 2692 | // content view first. |
| 2693 | mUi.detachTab(appTab); |
| 2694 | // Recreate the main WebView after destroying the old one. |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 2695 | mTabControl.recreateWebView(appTab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2696 | // TODO: analyze why the remove and add are necessary |
| 2697 | mUi.attachTab(appTab); |
| 2698 | if (mTabControl.getCurrentTab() != appTab) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2699 | switchToTab(appTab); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 2700 | loadUrlDataIn(appTab, urlData); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2701 | } else { |
| 2702 | // If the tab was the current tab, we have to attach |
| 2703 | // it to the view system again. |
| 2704 | setActiveTab(appTab); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 2705 | loadUrlDataIn(appTab, urlData); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2706 | } |
| 2707 | } |
| 2708 | |
| 2709 | // Remove the sub window if it exists. Also called by TabControl when the |
| 2710 | // user clicks the 'X' to dismiss a sub window. |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 2711 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2712 | public void dismissSubWindow(Tab tab) { |
| 2713 | removeSubWindow(tab); |
| 2714 | // dismiss the subwindow. This will destroy the WebView. |
| 2715 | tab.dismissSubWindow(); |
Michael Kolb | e8c9757 | 2011-11-21 14:03:56 -0800 | [diff] [blame] | 2716 | WebView wv = getCurrentTopWebView(); |
| 2717 | if (wv != null) { |
| 2718 | wv.requestFocus(); |
| 2719 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | @Override |
| 2723 | public void removeSubWindow(Tab t) { |
| 2724 | if (t.getSubWebView() != null) { |
| 2725 | mUi.removeSubWindow(t.getSubViewContainer()); |
| 2726 | } |
| 2727 | } |
| 2728 | |
| 2729 | @Override |
| 2730 | public void attachSubWindow(Tab tab) { |
| 2731 | if (tab.getSubWebView() != null) { |
| 2732 | mUi.attachSubWindow(tab.getSubViewContainer()); |
| 2733 | getCurrentTopWebView().requestFocus(); |
| 2734 | } |
| 2735 | } |
| 2736 | |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 2737 | private Tab showPreloadedTab(final UrlData urlData) { |
| 2738 | if (!urlData.isPreloaded()) { |
| 2739 | return null; |
| 2740 | } |
| 2741 | final PreloadedTabControl tabControl = urlData.getPreloadedTab(); |
| 2742 | final String sbQuery = urlData.getSearchBoxQueryToSubmit(); |
| 2743 | if (sbQuery != null) { |
Mathew Inwood | 9ad1eac | 2011-09-15 11:29:50 +0100 | [diff] [blame] | 2744 | if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) { |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 2745 | // Could not submit query. Fallback to regular tab creation |
| 2746 | tabControl.destroy(); |
| 2747 | return null; |
| 2748 | } |
| 2749 | } |
Michael Kolb | ff6a748 | 2011-07-26 16:37:15 -0700 | [diff] [blame] | 2750 | // check tab count and make room for new tab |
| 2751 | if (!mTabControl.canCreateNewTab()) { |
| 2752 | Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab()); |
| 2753 | if (leastUsed != null) { |
| 2754 | closeTab(leastUsed); |
| 2755 | } |
| 2756 | } |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 2757 | Tab t = tabControl.getTab(); |
Mathew Inwood | e09305e | 2011-09-02 12:03:26 +0100 | [diff] [blame] | 2758 | t.refreshIdAfterPreload(); |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 2759 | mTabControl.addPreloadedTab(t); |
| 2760 | addTab(t); |
| 2761 | setActiveTab(t); |
| 2762 | return t; |
| 2763 | } |
| 2764 | |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2765 | // open a non inconito tab with the given url data |
| 2766 | // and set as active tab |
| 2767 | public Tab openTab(UrlData urlData) { |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 2768 | Tab tab = showPreloadedTab(urlData); |
| 2769 | if (tab == null) { |
| 2770 | tab = createNewTab(false, true, true); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 2771 | if ((tab != null) && !urlData.isEmpty()) { |
| 2772 | loadUrlDataIn(tab, urlData); |
| 2773 | } |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2774 | } |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 2775 | return tab; |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
Michael Kolb | 843510f | 2010-12-09 10:51:49 -0800 | [diff] [blame] | 2778 | @Override |
| 2779 | public Tab openTabToHomePage() { |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2780 | return openTab(mSettings.getHomePage(), false, true, false); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2781 | } |
| 2782 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2783 | @Override |
Michael Kolb | 519d228 | 2011-05-09 17:03:19 -0700 | [diff] [blame] | 2784 | public Tab openIncognitoTab() { |
| 2785 | return openTab(INCOGNITO_URI, true, true, false); |
| 2786 | } |
| 2787 | |
| 2788 | @Override |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2789 | public Tab openTab(String url, boolean incognito, boolean setActive, |
| 2790 | boolean useCurrent) { |
John Reck | 5949c66 | 2011-05-27 09:52:29 -0700 | [diff] [blame] | 2791 | return openTab(url, incognito, setActive, useCurrent, null); |
| 2792 | } |
| 2793 | |
| 2794 | @Override |
| 2795 | public Tab openTab(String url, Tab parent, boolean setActive, |
| 2796 | boolean useCurrent) { |
| 2797 | return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(), |
| 2798 | setActive, useCurrent, parent); |
| 2799 | } |
| 2800 | |
| 2801 | public Tab openTab(String url, boolean incognito, boolean setActive, |
| 2802 | boolean useCurrent, Tab parent) { |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2803 | Tab tab = createNewTab(incognito, setActive, useCurrent); |
| 2804 | if (tab != null) { |
John Reck | 5949c66 | 2011-05-27 09:52:29 -0700 | [diff] [blame] | 2805 | if (parent != null && parent != tab) { |
| 2806 | parent.addChildTab(tab); |
| 2807 | } |
Michael Kolb | 519d228 | 2011-05-09 17:03:19 -0700 | [diff] [blame] | 2808 | if (url != null) { |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2809 | loadUrl(tab, url); |
Michael Kolb | 519d228 | 2011-05-09 17:03:19 -0700 | [diff] [blame] | 2810 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2811 | } |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2812 | return tab; |
| 2813 | } |
| 2814 | |
| 2815 | // this method will attempt to create a new tab |
| 2816 | // incognito: private browsing tab |
| 2817 | // setActive: ste tab as current tab |
| 2818 | // useCurrent: if no new tab can be created, return current tab |
| 2819 | private Tab createNewTab(boolean incognito, boolean setActive, |
| 2820 | boolean useCurrent) { |
| 2821 | Tab tab = null; |
| 2822 | if (mTabControl.canCreateNewTab()) { |
| 2823 | tab = mTabControl.createNewTab(incognito); |
| 2824 | addTab(tab); |
| 2825 | if (setActive) { |
| 2826 | setActiveTab(tab); |
| 2827 | } |
| 2828 | } else { |
| 2829 | if (useCurrent) { |
| 2830 | tab = mTabControl.getCurrentTab(); |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2831 | reuseTab(tab, null); |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 2832 | } else { |
| 2833 | mUi.showMaxTabsWarning(); |
| 2834 | } |
| 2835 | } |
| 2836 | return tab; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2837 | } |
| 2838 | |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 2839 | @Override |
| 2840 | public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) { |
| 2841 | SnapshotTab tab = null; |
| 2842 | if (mTabControl.canCreateNewTab()) { |
| 2843 | tab = mTabControl.createSnapshotTab(snapshotId); |
| 2844 | addTab(tab); |
| 2845 | if (setActive) { |
| 2846 | setActiveTab(tab); |
| 2847 | } |
| 2848 | } else { |
| 2849 | mUi.showMaxTabsWarning(); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 2850 | } |
| 2851 | return tab; |
| 2852 | } |
| 2853 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2854 | /** |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2855 | * @param tab the tab to switch to |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2856 | * @return boolean True if we successfully switched to a different tab. If |
| 2857 | * the indexth tab is null, or if that tab is the same as |
| 2858 | * the current one, return false. |
| 2859 | */ |
| 2860 | @Override |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2861 | public boolean switchToTab(Tab tab) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2862 | Tab currentTab = mTabControl.getCurrentTab(); |
| 2863 | if (tab == null || tab == currentTab) { |
| 2864 | return false; |
| 2865 | } |
| 2866 | setActiveTab(tab); |
| 2867 | return true; |
| 2868 | } |
| 2869 | |
| 2870 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2871 | public void closeCurrentTab() { |
Michael Kolb | 2ae6ef7 | 2011-08-22 14:49:34 -0700 | [diff] [blame] | 2872 | closeCurrentTab(false); |
| 2873 | } |
| 2874 | |
| 2875 | protected void closeCurrentTab(boolean andQuit) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2876 | if (mTabControl.getTabCount() == 1) { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 2877 | mCrashRecoveryHandler.clearState(); |
Michael Kolb | c1eeb12 | 2011-08-01 09:56:26 -0700 | [diff] [blame] | 2878 | mTabControl.removeTab(getCurrentTab()); |
John Reck | 958b242 | 2010-12-03 17:56:17 -0800 | [diff] [blame] | 2879 | mActivity.finish(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2880 | return; |
| 2881 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 2882 | final Tab current = mTabControl.getCurrentTab(); |
| 2883 | final int pos = mTabControl.getCurrentPosition(); |
| 2884 | Tab newTab = current.getParent(); |
| 2885 | if (newTab == null) { |
| 2886 | newTab = mTabControl.getTab(pos + 1); |
| 2887 | if (newTab == null) { |
| 2888 | newTab = mTabControl.getTab(pos - 1); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2889 | } |
| 2890 | } |
Michael Kolb | 2ae6ef7 | 2011-08-22 14:49:34 -0700 | [diff] [blame] | 2891 | if (andQuit) { |
| 2892 | mTabControl.setCurrentTab(newTab); |
| 2893 | closeTab(current); |
| 2894 | } else if (switchToTab(newTab)) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2895 | // Close window |
| 2896 | closeTab(current); |
| 2897 | } |
| 2898 | } |
| 2899 | |
| 2900 | /** |
| 2901 | * Close the tab, remove its associated title bar, and adjust mTabControl's |
| 2902 | * current tab to a valid value. |
| 2903 | */ |
| 2904 | @Override |
| 2905 | public void closeTab(Tab tab) { |
John Reck | 2826377 | 2011-10-11 17:13:42 -0700 | [diff] [blame] | 2906 | if (tab == mTabControl.getCurrentTab()) { |
| 2907 | closeCurrentTab(); |
| 2908 | } else { |
| 2909 | removeTab(tab); |
| 2910 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2911 | } |
| 2912 | |
Afzal Najam | d4e3331 | 2012-04-26 01:54:01 -0400 | [diff] [blame] | 2913 | /** |
| 2914 | * Close all tabs except the current one |
| 2915 | */ |
| 2916 | @Override |
| 2917 | public void closeOtherTabs() { |
| 2918 | int inactiveTabs = mTabControl.getTabCount() - 1; |
| 2919 | for (int i = inactiveTabs; i >= 0; i--) { |
| 2920 | Tab tab = mTabControl.getTab(i); |
| 2921 | if (tab != mTabControl.getCurrentTab()) { |
| 2922 | removeTab(tab); |
| 2923 | } |
| 2924 | } |
| 2925 | } |
| 2926 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2927 | // Called when loading from context menu or LOAD_URL message |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2928 | protected void loadUrlFromContext(String url) { |
| 2929 | Tab tab = getCurrentTab(); |
| 2930 | WebView view = tab != null ? tab.getWebView() : null; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2931 | // In case the user enters nothing. |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2932 | if (url != null && url.length() != 0 && tab != null && view != null) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2933 | url = UrlUtils.smartUrlFilter(url); |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 2934 | if (!((BrowserWebView) view).getWebViewClient(). |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 2935 | shouldOverrideUrlLoading(view, url)) { |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2936 | loadUrl(tab, url); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2937 | } |
| 2938 | } |
| 2939 | } |
| 2940 | |
| 2941 | /** |
| 2942 | * Load the URL into the given WebView and update the title bar |
| 2943 | * to reflect the new load. Call this instead of WebView.loadUrl |
| 2944 | * directly. |
| 2945 | * @param view The WebView used to load url. |
| 2946 | * @param url The URL to load. |
| 2947 | */ |
John Reck | 71e5142 | 2011-07-01 16:49:28 -0700 | [diff] [blame] | 2948 | @Override |
| 2949 | public void loadUrl(Tab tab, String url) { |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2950 | loadUrl(tab, url, null); |
| 2951 | } |
| 2952 | |
| 2953 | protected void loadUrl(Tab tab, String url, Map<String, String> headers) { |
| 2954 | if (tab != null) { |
| 2955 | dismissSubWindow(tab); |
| 2956 | tab.loadUrl(url, headers); |
Michael Kolb | a53c989 | 2011-10-05 13:31:40 -0700 | [diff] [blame] | 2957 | mUi.onProgressChanged(tab); |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2958 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | /** |
| 2962 | * Load UrlData into a Tab and update the title bar to reflect the new |
| 2963 | * load. Call this instead of UrlData.loadIn directly. |
| 2964 | * @param t The Tab used to load. |
| 2965 | * @param data The UrlData being loaded. |
| 2966 | */ |
| 2967 | protected void loadUrlDataIn(Tab t, UrlData data) { |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2968 | if (data != null) { |
Michael Kolb | 5ff5c8b | 2012-05-03 11:37:58 -0700 | [diff] [blame] | 2969 | if (data.isPreloaded()) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 2970 | // this isn't called for preloaded tabs |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2971 | } else { |
John Reck | 38b3965 | 2012-06-05 09:22:59 -0700 | [diff] [blame] | 2972 | if (t != null && data.mDisableUrlOverride) { |
| 2973 | t.disableUrlOverridingForLoad(); |
| 2974 | } |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2975 | loadUrl(t, data.mUrl, data.mHeaders); |
| 2976 | } |
| 2977 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2978 | } |
| 2979 | |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 2980 | @Override |
| 2981 | public void onUserCanceledSsl(Tab tab) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 2982 | // TODO: Figure out the "right" behavior |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 2983 | if (tab.canGoBack()) { |
| 2984 | tab.goBack(); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 2985 | } else { |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 2986 | tab.loadUrl(mSettings.getHomePage(), null); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 2987 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | void goBackOnePageOrQuit() { |
| 2991 | Tab current = mTabControl.getCurrentTab(); |
| 2992 | if (current == null) { |
| 2993 | /* |
| 2994 | * Instead of finishing the activity, simply push this to the back |
| 2995 | * of the stack and let ActivityManager to choose the foreground |
| 2996 | * activity. As BrowserActivity is singleTask, it will be always the |
| 2997 | * root of the task. So we can use either true or false for |
| 2998 | * moveTaskToBack(). |
| 2999 | */ |
luxiaol | b40014b | 2013-07-19 10:01:43 +0800 | [diff] [blame] | 3000 | showExitDialog(mActivity); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3001 | return; |
| 3002 | } |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 3003 | if (current.canGoBack()) { |
| 3004 | current.goBack(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3005 | } else { |
| 3006 | // Check to see if we are closing a window that was created by |
| 3007 | // another window. If so, we switch back to that window. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 3008 | Tab parent = current.getParent(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3009 | if (parent != null) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 3010 | switchToTab(parent); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3011 | // Now we close the other tab |
| 3012 | closeTab(current); |
| 3013 | } else { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3014 | /* |
| 3015 | * Instead of finishing the activity, simply push this to the back |
| 3016 | * of the stack and let ActivityManager to choose the foreground |
| 3017 | * activity. As BrowserActivity is singleTask, it will be always the |
| 3018 | * root of the task. So we can use either true or false for |
| 3019 | * moveTaskToBack(). |
| 3020 | */ |
luxiaol | b40014b | 2013-07-19 10:01:43 +0800 | [diff] [blame] | 3021 | showExitDialog(mActivity); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3022 | } |
| 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | /** |
Michael Kolb | 0035fad | 2011-03-14 13:25:28 -0700 | [diff] [blame] | 3027 | * helper method for key handler |
| 3028 | * returns the current tab if it can't advance |
| 3029 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 3030 | private Tab getNextTab() { |
Michael Kolb | f5261da | 2011-12-15 15:07:35 -0800 | [diff] [blame] | 3031 | int pos = mTabControl.getCurrentPosition() + 1; |
| 3032 | if (pos >= mTabControl.getTabCount()) { |
| 3033 | pos = 0; |
| 3034 | } |
| 3035 | return mTabControl.getTab(pos); |
Michael Kolb | 0035fad | 2011-03-14 13:25:28 -0700 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | /** |
| 3039 | * helper method for key handler |
| 3040 | * returns the current tab if it can't advance |
| 3041 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 3042 | private Tab getPrevTab() { |
Michael Kolb | f5261da | 2011-12-15 15:07:35 -0800 | [diff] [blame] | 3043 | int pos = mTabControl.getCurrentPosition() - 1; |
| 3044 | if ( pos < 0) { |
| 3045 | pos = mTabControl.getTabCount() - 1; |
| 3046 | } |
| 3047 | return mTabControl.getTab(pos); |
Michael Kolb | 0035fad | 2011-03-14 13:25:28 -0700 | [diff] [blame] | 3048 | } |
| 3049 | |
John Reck | bcef87f | 2012-02-03 14:58:34 -0800 | [diff] [blame] | 3050 | boolean isMenuOrCtrlKey(int keyCode) { |
| 3051 | return (KeyEvent.KEYCODE_MENU == keyCode) |
| 3052 | || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode) |
| 3053 | || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode); |
| 3054 | } |
| 3055 | |
Michael Kolb | 0035fad | 2011-03-14 13:25:28 -0700 | [diff] [blame] | 3056 | /** |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3057 | * handle key events in browser |
| 3058 | * |
| 3059 | * @param keyCode |
| 3060 | * @param event |
| 3061 | * @return true if handled, false to pass to super |
| 3062 | */ |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 3063 | @Override |
| 3064 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
Cary Clark | 160bbb9 | 2011-01-10 11:17:07 -0500 | [diff] [blame] | 3065 | boolean noModifiers = event.hasNoModifiers(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3066 | // Even if MENU is already held down, we need to call to super to open |
| 3067 | // the IME on long press. |
John Reck | bcef87f | 2012-02-03 14:58:34 -0800 | [diff] [blame] | 3068 | if (!noModifiers && isMenuOrCtrlKey(keyCode)) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3069 | mMenuIsDown = true; |
| 3070 | return false; |
| 3071 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3072 | |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3073 | WebView webView = getCurrentTopWebView(); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 3074 | Tab tab = getCurrentTab(); |
| 3075 | if (webView == null || tab == null) return false; |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3076 | |
Cary Clark | 160bbb9 | 2011-01-10 11:17:07 -0500 | [diff] [blame] | 3077 | boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON); |
| 3078 | boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON); |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3079 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3080 | switch(keyCode) { |
Michael Kolb | 0035fad | 2011-03-14 13:25:28 -0700 | [diff] [blame] | 3081 | case KeyEvent.KEYCODE_TAB: |
| 3082 | if (event.isCtrlPressed()) { |
| 3083 | if (event.isShiftPressed()) { |
| 3084 | // prev tab |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 3085 | switchToTab(getPrevTab()); |
Michael Kolb | 0035fad | 2011-03-14 13:25:28 -0700 | [diff] [blame] | 3086 | } else { |
| 3087 | // next tab |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 3088 | switchToTab(getNextTab()); |
Michael Kolb | 0035fad | 2011-03-14 13:25:28 -0700 | [diff] [blame] | 3089 | } |
| 3090 | return true; |
| 3091 | } |
| 3092 | break; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3093 | case KeyEvent.KEYCODE_SPACE: |
| 3094 | // WebView/WebTextView handle the keys in the KeyDown. As |
| 3095 | // the Activity's shortcut keys are only handled when WebView |
| 3096 | // doesn't, have to do it in onKeyDown instead of onKeyUp. |
Cary Clark | 160bbb9 | 2011-01-10 11:17:07 -0500 | [diff] [blame] | 3097 | if (shift) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3098 | pageUp(); |
Cary Clark | 160bbb9 | 2011-01-10 11:17:07 -0500 | [diff] [blame] | 3099 | } else if (noModifiers) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3100 | pageDown(); |
| 3101 | } |
| 3102 | return true; |
| 3103 | case KeyEvent.KEYCODE_BACK: |
Cary Clark | 160bbb9 | 2011-01-10 11:17:07 -0500 | [diff] [blame] | 3104 | if (!noModifiers) break; |
John Reck | e6bf4ab | 2011-02-24 15:48:05 -0800 | [diff] [blame] | 3105 | event.startTracking(); |
| 3106 | return true; |
Michael Kolb | e9e1d4a | 2011-07-14 15:02:17 -0700 | [diff] [blame] | 3107 | case KeyEvent.KEYCODE_FORWARD: |
| 3108 | if (!noModifiers) break; |
| 3109 | tab.goForward(); |
| 3110 | return true; |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3111 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 3112 | if (ctrl) { |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 3113 | tab.goBack(); |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3114 | return true; |
| 3115 | } |
| 3116 | break; |
| 3117 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 3118 | if (ctrl) { |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 3119 | tab.goForward(); |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3120 | return true; |
| 3121 | } |
| 3122 | break; |
| 3123 | case KeyEvent.KEYCODE_A: |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 3124 | if (ctrl && BrowserWebView.isClassic()) { |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 3125 | WebViewClassic.fromWebView(webView).selectAll(); |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3126 | return true; |
| 3127 | } |
| 3128 | break; |
Michael Kolb | a418306 | 2011-01-16 10:43:21 -0800 | [diff] [blame] | 3129 | // case KeyEvent.KEYCODE_B: // menu |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3130 | case KeyEvent.KEYCODE_C: |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 3131 | if (ctrl && BrowserWebView.isClassic()) { |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 3132 | WebViewClassic.fromWebView(webView).copySelection(); |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3133 | return true; |
| 3134 | } |
| 3135 | break; |
Michael Kolb | a418306 | 2011-01-16 10:43:21 -0800 | [diff] [blame] | 3136 | // case KeyEvent.KEYCODE_D: // menu |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3137 | // case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar |
Michael Kolb | a418306 | 2011-01-16 10:43:21 -0800 | [diff] [blame] | 3138 | // case KeyEvent.KEYCODE_F: // menu |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3139 | // case KeyEvent.KEYCODE_G: // in Chrome: finds next match |
Michael Kolb | a418306 | 2011-01-16 10:43:21 -0800 | [diff] [blame] | 3140 | // case KeyEvent.KEYCODE_H: // menu |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3141 | // case KeyEvent.KEYCODE_I: // unused |
Michael Kolb | a418306 | 2011-01-16 10:43:21 -0800 | [diff] [blame] | 3142 | // case KeyEvent.KEYCODE_J: // menu |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3143 | // case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar |
Michael Kolb | a418306 | 2011-01-16 10:43:21 -0800 | [diff] [blame] | 3144 | // case KeyEvent.KEYCODE_L: // menu |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3145 | // case KeyEvent.KEYCODE_M: // unused |
| 3146 | // case KeyEvent.KEYCODE_N: // in Chrome: new window |
| 3147 | // case KeyEvent.KEYCODE_O: // in Chrome: open file |
| 3148 | // case KeyEvent.KEYCODE_P: // in Chrome: print page |
| 3149 | // case KeyEvent.KEYCODE_Q: // unused |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 3150 | // case KeyEvent.KEYCODE_R: |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3151 | // case KeyEvent.KEYCODE_S: // in Chrome: saves page |
| 3152 | case KeyEvent.KEYCODE_T: |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 3153 | // we can't use the ctrl/shift flags, they check for |
| 3154 | // exclusive use of a modifier |
| 3155 | if (event.isCtrlPressed()) { |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3156 | if (event.isShiftPressed()) { |
Michael Kolb | 519d228 | 2011-05-09 17:03:19 -0700 | [diff] [blame] | 3157 | openIncognitoTab(); |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3158 | } else { |
| 3159 | openTabToHomePage(); |
| 3160 | } |
| 3161 | return true; |
| 3162 | } |
| 3163 | break; |
| 3164 | // case KeyEvent.KEYCODE_U: // in Chrome: opens source of page |
| 3165 | // case KeyEvent.KEYCODE_V: // text view intercepts to paste |
Michael Kolb | 5ae15bd | 2011-08-16 17:09:27 -0700 | [diff] [blame] | 3166 | // case KeyEvent.KEYCODE_W: // menu |
Cary Clark | 8ff8c66 | 2010-12-29 15:03:05 -0500 | [diff] [blame] | 3167 | // case KeyEvent.KEYCODE_X: // text view intercepts to cut |
| 3168 | // case KeyEvent.KEYCODE_Y: // unused |
| 3169 | // case KeyEvent.KEYCODE_Z: // unused |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3170 | } |
Michael Kolb | dc2ee1b | 2011-02-14 14:34:40 -0800 | [diff] [blame] | 3171 | // it is a regular key and webview is not null |
| 3172 | return mUi.dispatchKey(keyCode, event); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3173 | } |
| 3174 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 3175 | @Override |
| 3176 | public boolean onKeyLongPress(int keyCode, KeyEvent event) { |
John Reck | e6bf4ab | 2011-02-24 15:48:05 -0800 | [diff] [blame] | 3177 | switch(keyCode) { |
| 3178 | case KeyEvent.KEYCODE_BACK: |
John Reck | 3ba4553 | 2011-08-11 16:26:53 -0700 | [diff] [blame] | 3179 | if (mUi.isWebShowing()) { |
Michael Kolb | 315d502 | 2011-10-13 12:47:11 -0700 | [diff] [blame] | 3180 | bookmarksOrHistoryPicker(ComboViews.History); |
John Reck | e6bf4ab | 2011-02-24 15:48:05 -0800 | [diff] [blame] | 3181 | return true; |
| 3182 | } |
| 3183 | break; |
| 3184 | } |
| 3185 | return false; |
| 3186 | } |
| 3187 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 3188 | @Override |
| 3189 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
John Reck | bcef87f | 2012-02-03 14:58:34 -0800 | [diff] [blame] | 3190 | if (isMenuOrCtrlKey(keyCode)) { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 3191 | mMenuIsDown = false; |
John Reck | bcef87f | 2012-02-03 14:58:34 -0800 | [diff] [blame] | 3192 | if (KeyEvent.KEYCODE_MENU == keyCode |
| 3193 | && event.isTracking() && !event.isCanceled()) { |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 3194 | return onMenuKey(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 3195 | } |
| 3196 | } |
Cary Clark | 160bbb9 | 2011-01-10 11:17:07 -0500 | [diff] [blame] | 3197 | if (!event.hasNoModifiers()) return false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3198 | switch(keyCode) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3199 | case KeyEvent.KEYCODE_BACK: |
| 3200 | if (event.isTracking() && !event.isCanceled()) { |
| 3201 | onBackKey(); |
| 3202 | return true; |
| 3203 | } |
| 3204 | break; |
| 3205 | } |
| 3206 | return false; |
| 3207 | } |
| 3208 | |
| 3209 | public boolean isMenuDown() { |
| 3210 | return mMenuIsDown; |
| 3211 | } |
| 3212 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 3213 | @Override |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 3214 | public void setupAutoFill(Message message) { |
| 3215 | // Open the settings activity at the AutoFill profile fragment so that |
| 3216 | // the user can create a new profile. When they return, we will dispatch |
| 3217 | // the message so that we can autofill the form using their new profile. |
| 3218 | Intent intent = new Intent(mActivity, BrowserPreferencesPage.class); |
| 3219 | intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, |
| 3220 | AutoFillSettingsFragment.class.getName()); |
| 3221 | mAutoFillSetupMessage = message; |
| 3222 | mActivity.startActivityForResult(intent, AUTOFILL_SETUP); |
| 3223 | } |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 3224 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 3225 | @Override |
Michael Kolb | fbc579a | 2011-07-07 15:59:33 -0700 | [diff] [blame] | 3226 | public boolean onSearchRequested() { |
Michael Kolb | 1f9b356 | 2012-04-24 14:38:34 -0700 | [diff] [blame] | 3227 | mUi.editUrl(false, true); |
Michael Kolb | fbc579a | 2011-07-07 15:59:33 -0700 | [diff] [blame] | 3228 | return true; |
| 3229 | } |
| 3230 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 3231 | @Override |
| 3232 | public boolean shouldCaptureThumbnails() { |
| 3233 | return mUi.shouldCaptureThumbnails(); |
| 3234 | } |
| 3235 | |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 3236 | @Override |
Michael Kolb | 0b12912 | 2012-06-04 16:31:58 -0700 | [diff] [blame] | 3237 | public boolean supportsVoice() { |
| 3238 | PackageManager pm = mActivity.getPackageManager(); |
| 3239 | List activities = pm.queryIntentActivities(new Intent( |
| 3240 | RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); |
| 3241 | return activities.size() != 0; |
| 3242 | } |
| 3243 | |
| 3244 | @Override |
| 3245 | public void startVoiceRecognizer() { |
| 3246 | Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
| 3247 | voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, |
| 3248 | RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); |
| 3249 | voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); |
| 3250 | mActivity.startActivityForResult(voice, VOICE_RESULT); |
| 3251 | } |
| 3252 | |
| 3253 | @Override |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 3254 | public void setBlockEvents(boolean block) { |
| 3255 | mBlockEvents = block; |
| 3256 | } |
| 3257 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 3258 | @Override |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 3259 | public boolean dispatchKeyEvent(KeyEvent event) { |
Michael Kolb | 8735764 | 2011-08-24 14:19:18 -0700 | [diff] [blame] | 3260 | return mBlockEvents; |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 3261 | } |
| 3262 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 3263 | @Override |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 3264 | public boolean dispatchKeyShortcutEvent(KeyEvent event) { |
Michael Kolb | 8735764 | 2011-08-24 14:19:18 -0700 | [diff] [blame] | 3265 | return mBlockEvents; |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 3266 | } |
| 3267 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 3268 | @Override |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 3269 | public boolean dispatchTouchEvent(MotionEvent ev) { |
Michael Kolb | 8735764 | 2011-08-24 14:19:18 -0700 | [diff] [blame] | 3270 | return mBlockEvents; |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 3271 | } |
| 3272 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 3273 | @Override |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 3274 | public boolean dispatchTrackballEvent(MotionEvent ev) { |
Michael Kolb | 8735764 | 2011-08-24 14:19:18 -0700 | [diff] [blame] | 3275 | return mBlockEvents; |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 3276 | } |
| 3277 | |
John Reck | 9c35b9c | 2012-05-30 10:08:50 -0700 | [diff] [blame] | 3278 | @Override |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 3279 | public boolean dispatchGenericMotionEvent(MotionEvent ev) { |
Michael Kolb | 8735764 | 2011-08-24 14:19:18 -0700 | [diff] [blame] | 3280 | return mBlockEvents; |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 3281 | } |
| 3282 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 3283 | } |