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