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