blob: d140d5a7100728248a515fe34311481f42d8a138 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 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 Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080018
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.app.Activity;
qqzhoue6ff8b42013-07-23 17:28:48 +080020import android.app.AlertDialog;
John Reck7878f222012-04-18 16:23:14 -070021import android.app.KeyguardManager;
John Reck9d27ff52011-02-11 17:47:54 -080022import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.content.Intent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.content.res.Configuration;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.os.Bundle;
qqzhou8c5b0a32013-07-22 15:31:03 +080026import android.os.Handler;
John Reck7878f222012-04-18 16:23:14 -070027import android.os.PowerManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.util.Log;
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -040029import android.view.ActionMode;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.view.ContextMenu;
Michael Kolba2b2ba82010-08-04 17:54:03 -070031import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.view.KeyEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.view.Menu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.view.MenuItem;
Michael Kolbc3af0672011-08-09 10:24:41 -070035import android.view.MotionEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.view.View;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.view.Window;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038
John Reckd3e4d5b2011-07-13 15:48:43 -070039import com.google.common.annotations.VisibleForTesting;
Bijan Amirzada41242f22014-03-21 12:12:18 -070040import com.android.browser.R;
Bijan Amirzada41242f22014-03-21 12:12:18 -070041import com.android.browser.search.DefaultSearchEngine;
42import com.android.browser.search.SearchEngine;
43import com.android.browser.stub.NullController;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080044
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080045import org.codeaurora.swe.WebSettings;
46import org.codeaurora.swe.WebView;
Tarun Nainanief749cb2014-05-19 18:16:53 -070047import org.chromium.content.browser.TracingControllerAndroid;
John Reckd3e4d5b2011-07-13 15:48:43 -070048
Michael Kolb8233fac2010-10-26 16:08:53 -070049public class BrowserActivity extends Activity {
The Android Open Source Project0c908882009-03-03 19:32:16 -080050
John Reck439c9a52010-12-14 10:04:39 -080051 public static final String ACTION_SHOW_BOOKMARKS = "show_bookmarks";
52 public static final String ACTION_SHOW_BROWSER = "show_browser";
John Reck63bb6872010-12-01 19:29:32 -080053 public static final String ACTION_RESTART = "--restart--";
54 private static final String EXTRA_STATE = "state";
John Reck38b39652012-06-05 09:22:59 -070055 public static final String EXTRA_DISABLE_URL_OVERRIDE = "disable_url_override";
John Reck63bb6872010-12-01 19:29:32 -080056
Michael Kolb8233fac2010-10-26 16:08:53 -070057 private final static String LOGTAG = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -080058
John Reck6c2e2f32011-08-22 13:41:23 -070059 private final static boolean LOGV_ENABLED = Browser.LOGV_ENABLED;
Dave Bort31a6d1c2009-04-13 15:56:49 -070060
John Reck9c35b9c2012-05-30 10:08:50 -070061 private ActivityController mController = NullController.INSTANCE;
Tarun Nainanief749cb2014-05-19 18:16:53 -070062 private TracingControllerAndroid mTracingController;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080063
64
qqzhou8c5b0a32013-07-22 15:31:03 +080065 private Handler mHandler = new Handler();
Jeff Davidson43610292010-07-16 16:03:58 -070066
kaiyizbb2db872013-08-01 22:24:07 -040067 private UiController mUiController;
68 private Handler mHandlerEx = new Handler();
69 private Runnable runnable = new Runnable() {
70 @Override
71 public void run() {
72 if (mUiController != null) {
73 WebView current = mUiController.getCurrentWebView();
74 if (current != null) {
75 current.postInvalidate();
76 }
77 }
78 }
79 };
80
Tarun Nainanief749cb2014-05-19 18:16:53 -070081 private TracingControllerAndroid getTracingController() {
82 if (mTracingController == null) {
83 mTracingController = new TracingControllerAndroid(this);
84 }
85 return mTracingController;
86 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087
Grace Kloba22ac16e2009-10-07 18:00:23 -070088 @Override
89 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -070090 if (LOGV_ENABLED) {
John Reck6c2e2f32011-08-22 13:41:23 -070091 Log.v(LOGTAG, this + " onStart, has state: "
92 + (icicle == null ? "false" : "true"));
The Android Open Source Project0c908882009-03-03 19:32:16 -080093 }
94 super.onCreate(icicle);
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050095
Pankaj Gargf04dbda2014-10-02 13:52:46 -070096 Thread.setDefaultUncaughtExceptionHandler(new CrashLogExceptionHandler(this));
97
John Reck7878f222012-04-18 16:23:14 -070098 if (shouldIgnoreIntents()) {
99 finish();
100 return;
101 }
102
John Reckf57c0292011-07-21 18:15:39 -0700103 // If this was a web search request, pass it on to the default web
104 // search provider and finish this activity.
Bijan Amirzada41242f22014-03-21 12:12:18 -0700105 /*
kaiyiza2368f12013-09-02 10:43:31 +0800106 SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
107 boolean result = IntentHandler.handleWebSearchIntent(this, null, getIntent());
108 if (result && (searchEngine instanceof DefaultSearchEngine)) {
John Reckf57c0292011-07-21 18:15:39 -0700109 finish();
110 return;
111 }
Bijan Amirzada41242f22014-03-21 12:12:18 -0700112 */
John Reck9c35b9c2012-05-30 10:08:50 -0700113 mController = createController();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114
George Mount3636d0a2011-11-21 09:08:21 -0800115 Intent intent = (icicle == null) ? getIntent() : null;
116 mController.start(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117 }
118
John Recka5176f32011-05-17 12:35:37 -0700119 public static boolean isTablet(Context context) {
120 return context.getResources().getBoolean(R.bool.isTablet);
John Reck9d27ff52011-02-11 17:47:54 -0800121 }
122
John Reck9c35b9c2012-05-30 10:08:50 -0700123 private Controller createController() {
124 Controller controller = new Controller(this);
125 boolean xlarge = isTablet(this);
126 UI ui = null;
127 if (xlarge) {
kaiyizbb2db872013-08-01 22:24:07 -0400128 XLargeUi tablet = new XLargeUi(this, controller);
129 ui = tablet;
130 mUiController = tablet.getUiController();
John Reck9c35b9c2012-05-30 10:08:50 -0700131 } else {
kaiyizbb2db872013-08-01 22:24:07 -0400132 PhoneUi phone = new PhoneUi(this, controller);
133 ui = phone;
134 mUiController = phone.getUiController();
John Reck9c35b9c2012-05-30 10:08:50 -0700135 }
136 controller.setUi(ui);
137 return controller;
138 }
139
John Reck41554852010-12-01 12:53:37 -0800140 @VisibleForTesting
Tarun Nainani436b8732014-06-10 13:58:41 -0700141 //public to facilitate testing
142 public Controller getController() {
John Reck9c35b9c2012-05-30 10:08:50 -0700143 return (Controller) mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700144 }
145
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 @Override
147 protected void onNewIntent(Intent intent) {
John Reck7878f222012-04-18 16:23:14 -0700148 if (shouldIgnoreIntents()) return;
John Reck63bb6872010-12-01 19:29:32 -0800149 if (ACTION_RESTART.equals(intent.getAction())) {
150 Bundle outState = new Bundle();
John Reck1cf4b792011-07-26 10:22:22 -0700151 mController.onSaveInstanceState(outState);
John Reck63bb6872010-12-01 19:29:32 -0800152 finish();
153 getApplicationContext().startActivity(
154 new Intent(getApplicationContext(), BrowserActivity.class)
155 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
156 .putExtra(EXTRA_STATE, outState));
157 return;
158 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800160 }
161
John Reck7878f222012-04-18 16:23:14 -0700162 private KeyguardManager mKeyguardManager;
163 private PowerManager mPowerManager;
164 private boolean shouldIgnoreIntents() {
165 // Only process intents if the screen is on and the device is unlocked
166 // aka, if we will be user-visible
167 if (mKeyguardManager == null) {
168 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
169 }
170 if (mPowerManager == null) {
171 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
172 }
173 boolean ignore = !mPowerManager.isScreenOn();
174 ignore |= mKeyguardManager.inKeyguardRestrictedInputMode();
175 if (LOGV_ENABLED) {
176 Log.v(LOGTAG, "ignore intents: " + ignore);
177 }
178 return ignore;
179 }
180
Grace Kloba22ac16e2009-10-07 18:00:23 -0700181 @Override
182 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700184 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800185 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
186 }
John Reck9c35b9c2012-05-30 10:08:50 -0700187 mController.onResume();
Tarun Nainanief749cb2014-05-19 18:16:53 -0700188
189 getTracingController().registerReceiver(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 }
191
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400192 @Override
193 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400194 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700195 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400196 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400197 return true;
198 }
199
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400200 @Override
201 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700202 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400203 }
204
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400205 @Override
206 public void onContextMenuClosed(Menu menu) {
207 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700208 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400209 }
210
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400211 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800212 * onSaveInstanceState(Bundle map)
213 * onSaveInstanceState is called right before onStop(). The map contains
214 * the saved state.
215 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700216 @Override
217 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700218 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800219 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
220 }
John Reck9c35b9c2012-05-30 10:08:50 -0700221 mController.onSaveInstanceState(outState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800222 }
223
Grace Kloba22ac16e2009-10-07 18:00:23 -0700224 @Override
225 protected void onPause() {
John Reck9c35b9c2012-05-30 10:08:50 -0700226 mController.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800227 super.onPause();
Tarun Nainanief749cb2014-05-19 18:16:53 -0700228 getTracingController().unregisterReceiver(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800229 }
230
Grace Kloba22ac16e2009-10-07 18:00:23 -0700231 @Override
232 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700233 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800234 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
235 }
236 super.onDestroy();
John Reck9c35b9c2012-05-30 10:08:50 -0700237 mController.onDestroy();
238 mController = NullController.INSTANCE;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800239 }
240
241 @Override
242 public void onConfigurationChanged(Configuration newConfig) {
243 super.onConfigurationChanged(newConfig);
John Reck9c35b9c2012-05-30 10:08:50 -0700244 mController.onConfgurationChanged(newConfig);
kaiyizbb2db872013-08-01 22:24:07 -0400245
246 //For avoiding bug CR520353 temporarily, delay 300ms to refresh WebView.
247 mHandlerEx.postDelayed(runnable, 300);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800248 }
249
Grace Kloba22ac16e2009-10-07 18:00:23 -0700250 @Override
251 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800252 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700253 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700254 }
255
The Android Open Source Project0c908882009-03-03 19:32:16 -0800256 @Override
257 public boolean onCreateOptionsMenu(Menu menu) {
258 super.onCreateOptionsMenu(menu);
John Reck9c35b9c2012-05-30 10:08:50 -0700259 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800260 }
261
262 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700263 public boolean onPrepareOptionsMenu(Menu menu) {
264 super.onPrepareOptionsMenu(menu);
John Reckb3417f02011-01-14 11:01:05 -0800265 return mController.onPrepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400266 }
267
The Android Open Source Project0c908882009-03-03 19:32:16 -0800268 @Override
269 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700270 if (!mController.onOptionsItemSelected(item)) {
271 return super.onOptionsItemSelected(item);
Michael Kolb370a4f32010-10-06 10:45:32 -0700272 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800273 return true;
274 }
275
276 @Override
277 public void onCreateContextMenu(ContextMenu menu, View v,
278 ContextMenuInfo menuInfo) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 mController.onCreateContextMenu(menu, v, menuInfo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800280 }
281
Michael Kolb8233fac2010-10-26 16:08:53 -0700282 @Override
283 public boolean onContextItemSelected(MenuItem item) {
284 return mController.onContextItemSelected(item);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700285 }
286
Grace Kloba5942df02009-09-18 11:48:29 -0700287 @Override
288 public boolean onKeyDown(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700289 return mController.onKeyDown(keyCode, event) ||
290 super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800291 }
292
Grace Kloba5942df02009-09-18 11:48:29 -0700293 @Override
John Recke6bf4ab2011-02-24 15:48:05 -0800294 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
295 return mController.onKeyLongPress(keyCode, event) ||
296 super.onKeyLongPress(keyCode, event);
297 }
298
299 @Override
Grace Kloba5942df02009-09-18 11:48:29 -0700300 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700301 return mController.onKeyUp(keyCode, event) ||
302 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800303 }
304
Michael Kolbe421c242010-10-04 19:29:01 -0700305 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700306 public void onActionModeStarted(ActionMode mode) {
307 super.onActionModeStarted(mode);
308 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700309 }
310
Michael Kolbe421c242010-10-04 19:29:01 -0700311 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700312 public void onActionModeFinished(ActionMode mode) {
313 super.onActionModeFinished(mode);
314 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700315 }
316
The Android Open Source Project0c908882009-03-03 19:32:16 -0800317 @Override
318 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700319 Intent intent) {
320 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100321 }
322
Michael Kolbfbc579a2011-07-07 15:59:33 -0700323 @Override
324 public boolean onSearchRequested() {
325 return mController.onSearchRequested();
326 }
327
Michael Kolbc3af0672011-08-09 10:24:41 -0700328 @Override
329 public boolean dispatchKeyEvent(KeyEvent event) {
330 return mController.dispatchKeyEvent(event)
331 || super.dispatchKeyEvent(event);
332 }
333
334 @Override
335 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
336 return mController.dispatchKeyShortcutEvent(event)
337 || super.dispatchKeyShortcutEvent(event);
338 }
339
340 @Override
341 public boolean dispatchTouchEvent(MotionEvent ev) {
342 return mController.dispatchTouchEvent(ev)
343 || super.dispatchTouchEvent(ev);
344 }
345
346 @Override
347 public boolean dispatchTrackballEvent(MotionEvent ev) {
348 return mController.dispatchTrackballEvent(ev)
349 || super.dispatchTrackballEvent(ev);
350 }
351
352 @Override
353 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
354 return mController.dispatchGenericMotionEvent(ev) ||
355 super.dispatchGenericMotionEvent(ev);
356 }
357
The Android Open Source Project0c908882009-03-03 19:32:16 -0800358}