blob: c54832274cac735d6e738a3a4250e403590243e2 [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
Grace Kloba22ac16e2009-10-07 18:00:23 -070062 @Override
63 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -070064 if (LOGV_ENABLED) {
John Reck6c2e2f32011-08-22 13:41:23 -070065 Log.v(LOGTAG, this + " onStart, has state: "
66 + (icicle == null ? "false" : "true"));
The Android Open Source Project0c908882009-03-03 19:32:16 -080067 }
68 super.onCreate(icicle);
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050069
John Reck7878f222012-04-18 16:23:14 -070070 if (shouldIgnoreIntents()) {
71 finish();
72 return;
73 }
74
John Reckf57c0292011-07-21 18:15:39 -070075 // If this was a web search request, pass it on to the default web
76 // search provider and finish this activity.
77 if (IntentHandler.handleWebSearchIntent(this, null, getIntent())) {
78 finish();
79 return;
80 }
John Reck9c35b9c2012-05-30 10:08:50 -070081 mController = createController();
The Android Open Source Project0c908882009-03-03 19:32:16 -080082
George Mount3636d0a2011-11-21 09:08:21 -080083 Intent intent = (icicle == null) ? getIntent() : null;
84 mController.start(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -080085 }
86
John Recka5176f32011-05-17 12:35:37 -070087 public static boolean isTablet(Context context) {
88 return context.getResources().getBoolean(R.bool.isTablet);
John Reck9d27ff52011-02-11 17:47:54 -080089 }
90
John Reck9c35b9c2012-05-30 10:08:50 -070091 private Controller createController() {
92 Controller controller = new Controller(this);
93 boolean xlarge = isTablet(this);
94 UI ui = null;
95 if (xlarge) {
96 ui = new XLargeUi(this, controller);
97 } else {
98 ui = new PhoneUi(this, controller);
99 }
100 controller.setUi(ui);
101 return controller;
102 }
103
John Reck41554852010-12-01 12:53:37 -0800104 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -0700105 Controller getController() {
John Reck9c35b9c2012-05-30 10:08:50 -0700106 return (Controller) mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700107 }
108
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109 @Override
110 protected void onNewIntent(Intent intent) {
John Reck7878f222012-04-18 16:23:14 -0700111 if (shouldIgnoreIntents()) return;
John Reck63bb6872010-12-01 19:29:32 -0800112 if (ACTION_RESTART.equals(intent.getAction())) {
113 Bundle outState = new Bundle();
John Reck1cf4b792011-07-26 10:22:22 -0700114 mController.onSaveInstanceState(outState);
John Reck63bb6872010-12-01 19:29:32 -0800115 finish();
116 getApplicationContext().startActivity(
117 new Intent(getApplicationContext(), BrowserActivity.class)
118 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
119 .putExtra(EXTRA_STATE, outState));
120 return;
121 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700122 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123 }
124
John Reck7878f222012-04-18 16:23:14 -0700125 private KeyguardManager mKeyguardManager;
126 private PowerManager mPowerManager;
127 private boolean shouldIgnoreIntents() {
128 // Only process intents if the screen is on and the device is unlocked
129 // aka, if we will be user-visible
130 if (mKeyguardManager == null) {
131 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
132 }
133 if (mPowerManager == null) {
134 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
135 }
136 boolean ignore = !mPowerManager.isScreenOn();
137 ignore |= mKeyguardManager.inKeyguardRestrictedInputMode();
138 if (LOGV_ENABLED) {
139 Log.v(LOGTAG, "ignore intents: " + ignore);
140 }
141 return ignore;
142 }
143
Grace Kloba22ac16e2009-10-07 18:00:23 -0700144 @Override
145 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700147 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
149 }
John Reck9c35b9c2012-05-30 10:08:50 -0700150 mController.onResume();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800151 }
152
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400153 @Override
154 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400155 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400157 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400158 return true;
159 }
160
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400161 @Override
162 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400164 }
165
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400166 @Override
167 public void onContextMenuClosed(Menu menu) {
168 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700169 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400170 }
171
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400172 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800173 * onSaveInstanceState(Bundle map)
174 * onSaveInstanceState is called right before onStop(). The map contains
175 * the saved state.
176 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700177 @Override
178 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700179 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
181 }
John Reck9c35b9c2012-05-30 10:08:50 -0700182 mController.onSaveInstanceState(outState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183 }
184
Grace Kloba22ac16e2009-10-07 18:00:23 -0700185 @Override
186 protected void onPause() {
John Reck9c35b9c2012-05-30 10:08:50 -0700187 mController.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800188 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189 }
190
Grace Kloba22ac16e2009-10-07 18:00:23 -0700191 @Override
192 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700193 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800194 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
195 }
196 super.onDestroy();
John Reck9c35b9c2012-05-30 10:08:50 -0700197 mController.onDestroy();
198 mController = NullController.INSTANCE;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800199 }
200
201 @Override
202 public void onConfigurationChanged(Configuration newConfig) {
203 super.onConfigurationChanged(newConfig);
John Reck9c35b9c2012-05-30 10:08:50 -0700204 mController.onConfgurationChanged(newConfig);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800205 }
206
Grace Kloba22ac16e2009-10-07 18:00:23 -0700207 @Override
208 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800209 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700211 }
212
The Android Open Source Project0c908882009-03-03 19:32:16 -0800213 @Override
214 public boolean onCreateOptionsMenu(Menu menu) {
215 super.onCreateOptionsMenu(menu);
John Reck9c35b9c2012-05-30 10:08:50 -0700216 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217 }
218
219 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700220 public boolean onPrepareOptionsMenu(Menu menu) {
221 super.onPrepareOptionsMenu(menu);
John Reckb3417f02011-01-14 11:01:05 -0800222 return mController.onPrepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400223 }
224
The Android Open Source Project0c908882009-03-03 19:32:16 -0800225 @Override
226 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700227 if (!mController.onOptionsItemSelected(item)) {
kaiyiza8b6dbb2013-07-29 18:11:22 +0800228 if (item.getItemId() == R.id.exit_menu_id) {
229 finish();
230 new Handler().postDelayed(new Runnable() {
231 @Override
232 public void run() {
233 // Make sure all tabs are closed.
234 if (mController != NullController.INSTANCE) {
235 mController.onPause();
236 mController.onDestroy();
237 // Clear the state before kill the browser process.
238 CrashRecoveryHandler.getInstance().clearState(true);
239 mController = NullController.INSTANCE;
240 }
241 android.os.Process.killProcess(android.os.Process.myPid());
242 }
243 }, 300);
244 }
qqzhoue6ff8b42013-07-23 17:28:48 +0800245 if (item.getItemId() == R.id.about_menu_id) {
246 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
247 builder.setTitle(R.string.about);
248 builder.setCancelable(true);
249 String ua = "";
250 final WebView current = getController().getCurrentWebView();
251 if (current != null) {
252 final WebSettings s = current.getSettings();
253 if (s != null) {
254 ua = s.getUserAgentString();
255 }
256 }
257 builder.setMessage("Agent:" + ua);
258 builder.setPositiveButton(android.R.string.ok, null);
259 builder.create().show();
260 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700261 return super.onOptionsItemSelected(item);
Michael Kolb370a4f32010-10-06 10:45:32 -0700262 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800263 return true;
264 }
265
266 @Override
267 public void onCreateContextMenu(ContextMenu menu, View v,
268 ContextMenuInfo menuInfo) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 mController.onCreateContextMenu(menu, v, menuInfo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 }
271
Michael Kolb8233fac2010-10-26 16:08:53 -0700272 @Override
273 public boolean onContextItemSelected(MenuItem item) {
274 return mController.onContextItemSelected(item);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700275 }
276
Grace Kloba5942df02009-09-18 11:48:29 -0700277 @Override
278 public boolean onKeyDown(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 return mController.onKeyDown(keyCode, event) ||
280 super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800281 }
282
Grace Kloba5942df02009-09-18 11:48:29 -0700283 @Override
John Recke6bf4ab2011-02-24 15:48:05 -0800284 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
285 return mController.onKeyLongPress(keyCode, event) ||
286 super.onKeyLongPress(keyCode, event);
287 }
288
289 @Override
Grace Kloba5942df02009-09-18 11:48:29 -0700290 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700291 return mController.onKeyUp(keyCode, event) ||
292 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800293 }
294
Michael Kolbe421c242010-10-04 19:29:01 -0700295 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700296 public void onActionModeStarted(ActionMode mode) {
297 super.onActionModeStarted(mode);
298 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700299 }
300
Michael Kolbe421c242010-10-04 19:29:01 -0700301 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700302 public void onActionModeFinished(ActionMode mode) {
303 super.onActionModeFinished(mode);
304 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700305 }
306
The Android Open Source Project0c908882009-03-03 19:32:16 -0800307 @Override
308 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700309 Intent intent) {
310 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100311 }
312
Michael Kolbfbc579a2011-07-07 15:59:33 -0700313 @Override
314 public boolean onSearchRequested() {
315 return mController.onSearchRequested();
316 }
317
Michael Kolbc3af0672011-08-09 10:24:41 -0700318 @Override
319 public boolean dispatchKeyEvent(KeyEvent event) {
320 return mController.dispatchKeyEvent(event)
321 || super.dispatchKeyEvent(event);
322 }
323
324 @Override
325 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
326 return mController.dispatchKeyShortcutEvent(event)
327 || super.dispatchKeyShortcutEvent(event);
328 }
329
330 @Override
331 public boolean dispatchTouchEvent(MotionEvent ev) {
332 return mController.dispatchTouchEvent(ev)
333 || super.dispatchTouchEvent(ev);
334 }
335
336 @Override
337 public boolean dispatchTrackballEvent(MotionEvent ev) {
338 return mController.dispatchTrackballEvent(ev)
339 || super.dispatchTrackballEvent(ev);
340 }
341
342 @Override
343 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
344 return mController.dispatchGenericMotionEvent(ev) ||
345 super.dispatchGenericMotionEvent(ev);
346 }
347
qqzhou8c5b0a32013-07-22 15:31:03 +0800348 // add for carrier homepage feature
349 @JavascriptInterface
350 public void loadBookmarks() {
351 mHandler.post(new Runnable() {
352 @Override
353 public void run() {
354 if (mController instanceof Controller) {
355 ((Controller)mController).bookmarksOrHistoryPicker(ComboViews.Bookmarks);
356 }
357 }
358 });
359 }
360
361 // add for carrier homepage feature
362 @JavascriptInterface
363 public void loadHistory() {
364 mHandler.post(new Runnable() {
365 @Override
366 public void run() {
367 if (mController instanceof Controller) {
368 ((Controller)mController).bookmarksOrHistoryPicker(ComboViews.History);
369 }
370 }
371 });
372 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800373}