blob: fe472c51697114303b7b573933c9631117a9832c [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
17package com.android.browser;
18
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;
qqzhoue6ff8b42013-07-23 17:28:48 +080038import android.webkit.WebSettings;
39import android.webkit.WebView;
qqzhou8c5b0a32013-07-22 15:31:03 +080040import android.webkit.JavascriptInterface;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041
qqzhou8c5b0a32013-07-22 15:31:03 +080042import com.android.browser.UI.ComboViews;
John Reck9c35b9c2012-05-30 10:08:50 -070043import com.android.browser.stub.NullController;
qqzhou8c5b0a32013-07-22 15:31:03 +080044
John Reckd3e4d5b2011-07-13 15:48:43 -070045import com.google.common.annotations.VisibleForTesting;
46
Michael Kolb8233fac2010-10-26 16:08:53 -070047public class BrowserActivity extends Activity {
The Android Open Source Project0c908882009-03-03 19:32:16 -080048
John Reck439c9a52010-12-14 10:04:39 -080049 public static final String ACTION_SHOW_BOOKMARKS = "show_bookmarks";
50 public static final String ACTION_SHOW_BROWSER = "show_browser";
John Reck63bb6872010-12-01 19:29:32 -080051 public static final String ACTION_RESTART = "--restart--";
52 private static final String EXTRA_STATE = "state";
John Reck38b39652012-06-05 09:22:59 -070053 public static final String EXTRA_DISABLE_URL_OVERRIDE = "disable_url_override";
John Reck63bb6872010-12-01 19:29:32 -080054
Michael Kolb8233fac2010-10-26 16:08:53 -070055 private final static String LOGTAG = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -080056
John Reck6c2e2f32011-08-22 13:41:23 -070057 private final static boolean LOGV_ENABLED = Browser.LOGV_ENABLED;
Dave Bort31a6d1c2009-04-13 15:56:49 -070058
John Reck9c35b9c2012-05-30 10:08:50 -070059 private ActivityController mController = NullController.INSTANCE;
qqzhou8c5b0a32013-07-22 15:31:03 +080060 private Handler mHandler = new Handler();
Jeff Davidson43610292010-07-16 16:03:58 -070061
kaiyizbb2db872013-08-01 22:24:07 -040062 private UiController mUiController;
63 private Handler mHandlerEx = new Handler();
64 private Runnable runnable = new Runnable() {
65 @Override
66 public void run() {
67 if (mUiController != null) {
68 WebView current = mUiController.getCurrentWebView();
69 if (current != null) {
70 current.postInvalidate();
71 }
72 }
73 }
74 };
75
Grace Kloba22ac16e2009-10-07 18:00:23 -070076 @Override
77 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -070078 if (LOGV_ENABLED) {
John Reck6c2e2f32011-08-22 13:41:23 -070079 Log.v(LOGTAG, this + " onStart, has state: "
80 + (icicle == null ? "false" : "true"));
The Android Open Source Project0c908882009-03-03 19:32:16 -080081 }
82 super.onCreate(icicle);
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050083
John Reck7878f222012-04-18 16:23:14 -070084 if (shouldIgnoreIntents()) {
85 finish();
86 return;
87 }
88
John Reckf57c0292011-07-21 18:15:39 -070089 // If this was a web search request, pass it on to the default web
90 // search provider and finish this activity.
91 if (IntentHandler.handleWebSearchIntent(this, null, getIntent())) {
92 finish();
93 return;
94 }
John Reck9c35b9c2012-05-30 10:08:50 -070095 mController = createController();
The Android Open Source Project0c908882009-03-03 19:32:16 -080096
George Mount3636d0a2011-11-21 09:08:21 -080097 Intent intent = (icicle == null) ? getIntent() : null;
98 mController.start(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -080099 }
100
John Recka5176f32011-05-17 12:35:37 -0700101 public static boolean isTablet(Context context) {
102 return context.getResources().getBoolean(R.bool.isTablet);
John Reck9d27ff52011-02-11 17:47:54 -0800103 }
104
John Reck9c35b9c2012-05-30 10:08:50 -0700105 private Controller createController() {
106 Controller controller = new Controller(this);
107 boolean xlarge = isTablet(this);
108 UI ui = null;
109 if (xlarge) {
kaiyizbb2db872013-08-01 22:24:07 -0400110 XLargeUi tablet = new XLargeUi(this, controller);
111 ui = tablet;
112 mUiController = tablet.getUiController();
John Reck9c35b9c2012-05-30 10:08:50 -0700113 } else {
kaiyizbb2db872013-08-01 22:24:07 -0400114 PhoneUi phone = new PhoneUi(this, controller);
115 ui = phone;
116 mUiController = phone.getUiController();
John Reck9c35b9c2012-05-30 10:08:50 -0700117 }
118 controller.setUi(ui);
119 return controller;
120 }
121
John Reck41554852010-12-01 12:53:37 -0800122 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -0700123 Controller getController() {
John Reck9c35b9c2012-05-30 10:08:50 -0700124 return (Controller) mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700125 }
126
The Android Open Source Project0c908882009-03-03 19:32:16 -0800127 @Override
128 protected void onNewIntent(Intent intent) {
John Reck7878f222012-04-18 16:23:14 -0700129 if (shouldIgnoreIntents()) return;
John Reck63bb6872010-12-01 19:29:32 -0800130 if (ACTION_RESTART.equals(intent.getAction())) {
131 Bundle outState = new Bundle();
John Reck1cf4b792011-07-26 10:22:22 -0700132 mController.onSaveInstanceState(outState);
John Reck63bb6872010-12-01 19:29:32 -0800133 finish();
134 getApplicationContext().startActivity(
135 new Intent(getApplicationContext(), BrowserActivity.class)
136 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
137 .putExtra(EXTRA_STATE, outState));
138 return;
139 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700140 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800141 }
142
John Reck7878f222012-04-18 16:23:14 -0700143 private KeyguardManager mKeyguardManager;
144 private PowerManager mPowerManager;
145 private boolean shouldIgnoreIntents() {
146 // Only process intents if the screen is on and the device is unlocked
147 // aka, if we will be user-visible
148 if (mKeyguardManager == null) {
149 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
150 }
151 if (mPowerManager == null) {
152 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
153 }
154 boolean ignore = !mPowerManager.isScreenOn();
155 ignore |= mKeyguardManager.inKeyguardRestrictedInputMode();
156 if (LOGV_ENABLED) {
157 Log.v(LOGTAG, "ignore intents: " + ignore);
158 }
159 return ignore;
160 }
161
Grace Kloba22ac16e2009-10-07 18:00:23 -0700162 @Override
163 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700165 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800166 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
167 }
John Reck9c35b9c2012-05-30 10:08:50 -0700168 mController.onResume();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169 }
170
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400171 @Override
172 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400173 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700174 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400175 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400176 return true;
177 }
178
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400179 @Override
180 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700181 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400182 }
183
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400184 @Override
185 public void onContextMenuClosed(Menu menu) {
186 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700187 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400188 }
189
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400190 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800191 * onSaveInstanceState(Bundle map)
192 * onSaveInstanceState is called right before onStop(). The map contains
193 * the saved state.
194 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700195 @Override
196 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700197 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800198 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
199 }
John Reck9c35b9c2012-05-30 10:08:50 -0700200 mController.onSaveInstanceState(outState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800201 }
202
Grace Kloba22ac16e2009-10-07 18:00:23 -0700203 @Override
204 protected void onPause() {
John Reck9c35b9c2012-05-30 10:08:50 -0700205 mController.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800207 }
208
Grace Kloba22ac16e2009-10-07 18:00:23 -0700209 @Override
210 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700211 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800212 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
213 }
214 super.onDestroy();
John Reck9c35b9c2012-05-30 10:08:50 -0700215 mController.onDestroy();
216 mController = NullController.INSTANCE;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217 }
218
219 @Override
220 public void onConfigurationChanged(Configuration newConfig) {
221 super.onConfigurationChanged(newConfig);
John Reck9c35b9c2012-05-30 10:08:50 -0700222 mController.onConfgurationChanged(newConfig);
kaiyizbb2db872013-08-01 22:24:07 -0400223
224 //For avoiding bug CR520353 temporarily, delay 300ms to refresh WebView.
225 mHandlerEx.postDelayed(runnable, 300);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800226 }
227
Grace Kloba22ac16e2009-10-07 18:00:23 -0700228 @Override
229 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700231 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700232 }
233
The Android Open Source Project0c908882009-03-03 19:32:16 -0800234 @Override
235 public boolean onCreateOptionsMenu(Menu menu) {
236 super.onCreateOptionsMenu(menu);
John Reck9c35b9c2012-05-30 10:08:50 -0700237 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800238 }
239
240 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700241 public boolean onPrepareOptionsMenu(Menu menu) {
242 super.onPrepareOptionsMenu(menu);
John Reckb3417f02011-01-14 11:01:05 -0800243 return mController.onPrepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400244 }
245
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246 @Override
247 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 if (!mController.onOptionsItemSelected(item)) {
kaiyiza8b6dbb2013-07-29 18:11:22 +0800249 if (item.getItemId() == R.id.exit_menu_id) {
250 finish();
251 new Handler().postDelayed(new Runnable() {
252 @Override
253 public void run() {
254 // Make sure all tabs are closed.
255 if (mController != NullController.INSTANCE) {
256 mController.onPause();
257 mController.onDestroy();
258 // Clear the state before kill the browser process.
259 CrashRecoveryHandler.getInstance().clearState(true);
260 mController = NullController.INSTANCE;
261 }
262 android.os.Process.killProcess(android.os.Process.myPid());
263 }
264 }, 300);
265 }
qqzhoue6ff8b42013-07-23 17:28:48 +0800266 if (item.getItemId() == R.id.about_menu_id) {
267 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
268 builder.setTitle(R.string.about);
269 builder.setCancelable(true);
270 String ua = "";
271 final WebView current = getController().getCurrentWebView();
272 if (current != null) {
273 final WebSettings s = current.getSettings();
274 if (s != null) {
275 ua = s.getUserAgentString();
276 }
277 }
278 builder.setMessage("Agent:" + ua);
279 builder.setPositiveButton(android.R.string.ok, null);
280 builder.create().show();
281 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700282 return super.onOptionsItemSelected(item);
Michael Kolb370a4f32010-10-06 10:45:32 -0700283 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800284 return true;
285 }
286
287 @Override
288 public void onCreateContextMenu(ContextMenu menu, View v,
289 ContextMenuInfo menuInfo) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700290 mController.onCreateContextMenu(menu, v, menuInfo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800291 }
292
Michael Kolb8233fac2010-10-26 16:08:53 -0700293 @Override
294 public boolean onContextItemSelected(MenuItem item) {
295 return mController.onContextItemSelected(item);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700296 }
297
Grace Kloba5942df02009-09-18 11:48:29 -0700298 @Override
299 public boolean onKeyDown(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700300 return mController.onKeyDown(keyCode, event) ||
301 super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800302 }
303
Grace Kloba5942df02009-09-18 11:48:29 -0700304 @Override
John Recke6bf4ab2011-02-24 15:48:05 -0800305 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
306 return mController.onKeyLongPress(keyCode, event) ||
307 super.onKeyLongPress(keyCode, event);
308 }
309
310 @Override
Grace Kloba5942df02009-09-18 11:48:29 -0700311 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700312 return mController.onKeyUp(keyCode, event) ||
313 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800314 }
315
Michael Kolbe421c242010-10-04 19:29:01 -0700316 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700317 public void onActionModeStarted(ActionMode mode) {
318 super.onActionModeStarted(mode);
319 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700320 }
321
Michael Kolbe421c242010-10-04 19:29:01 -0700322 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700323 public void onActionModeFinished(ActionMode mode) {
324 super.onActionModeFinished(mode);
325 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700326 }
327
The Android Open Source Project0c908882009-03-03 19:32:16 -0800328 @Override
329 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700330 Intent intent) {
331 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100332 }
333
Michael Kolbfbc579a2011-07-07 15:59:33 -0700334 @Override
335 public boolean onSearchRequested() {
336 return mController.onSearchRequested();
337 }
338
Michael Kolbc3af0672011-08-09 10:24:41 -0700339 @Override
340 public boolean dispatchKeyEvent(KeyEvent event) {
341 return mController.dispatchKeyEvent(event)
342 || super.dispatchKeyEvent(event);
343 }
344
345 @Override
346 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
347 return mController.dispatchKeyShortcutEvent(event)
348 || super.dispatchKeyShortcutEvent(event);
349 }
350
351 @Override
352 public boolean dispatchTouchEvent(MotionEvent ev) {
353 return mController.dispatchTouchEvent(ev)
354 || super.dispatchTouchEvent(ev);
355 }
356
357 @Override
358 public boolean dispatchTrackballEvent(MotionEvent ev) {
359 return mController.dispatchTrackballEvent(ev)
360 || super.dispatchTrackballEvent(ev);
361 }
362
363 @Override
364 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
365 return mController.dispatchGenericMotionEvent(ev) ||
366 super.dispatchGenericMotionEvent(ev);
367 }
368
qqzhou8c5b0a32013-07-22 15:31:03 +0800369 // add for carrier homepage feature
370 @JavascriptInterface
371 public void loadBookmarks() {
372 mHandler.post(new Runnable() {
373 @Override
374 public void run() {
375 if (mController instanceof Controller) {
376 ((Controller)mController).bookmarksOrHistoryPicker(ComboViews.Bookmarks);
377 }
378 }
379 });
380 }
381
382 // add for carrier homepage feature
383 @JavascriptInterface
384 public void loadHistory() {
385 mHandler.post(new Runnable() {
386 @Override
387 public void run() {
388 if (mController instanceof Controller) {
389 ((Controller)mController).bookmarksOrHistoryPicker(ComboViews.History);
390 }
391 }
392 });
393 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800394}