blob: 4166b1109c2a80bb1711de91a656da672c2a8220 [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;
John Reck7878f222012-04-18 16:23:14 -070020import android.app.KeyguardManager;
John Reck9d27ff52011-02-11 17:47:54 -080021import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.content.Intent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.content.res.Configuration;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.os.Bundle;
John Reck7878f222012-04-18 16:23:14 -070025import android.os.PowerManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080026import android.util.Log;
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -040027import android.view.ActionMode;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.view.ContextMenu;
Michael Kolba2b2ba82010-08-04 17:54:03 -070029import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.view.KeyEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.view.Menu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.view.MenuItem;
Michael Kolbc3af0672011-08-09 10:24:41 -070033import android.view.MotionEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.view.View;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.view.Window;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036
John Reck9c35b9c2012-05-30 10:08:50 -070037import com.android.browser.stub.NullController;
John Reckd3e4d5b2011-07-13 15:48:43 -070038import com.google.common.annotations.VisibleForTesting;
39
Michael Kolb8233fac2010-10-26 16:08:53 -070040public class BrowserActivity extends Activity {
The Android Open Source Project0c908882009-03-03 19:32:16 -080041
John Reck439c9a52010-12-14 10:04:39 -080042 public static final String ACTION_SHOW_BOOKMARKS = "show_bookmarks";
43 public static final String ACTION_SHOW_BROWSER = "show_browser";
John Reck63bb6872010-12-01 19:29:32 -080044 public static final String ACTION_RESTART = "--restart--";
45 private static final String EXTRA_STATE = "state";
John Reck38b39652012-06-05 09:22:59 -070046 public static final String EXTRA_DISABLE_URL_OVERRIDE = "disable_url_override";
John Reck63bb6872010-12-01 19:29:32 -080047
Michael Kolb8233fac2010-10-26 16:08:53 -070048 private final static String LOGTAG = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -080049
John Reck6c2e2f32011-08-22 13:41:23 -070050 private final static boolean LOGV_ENABLED = Browser.LOGV_ENABLED;
Dave Bort31a6d1c2009-04-13 15:56:49 -070051
John Reck9c35b9c2012-05-30 10:08:50 -070052 private ActivityController mController = NullController.INSTANCE;
Jeff Davidson43610292010-07-16 16:03:58 -070053
Grace Kloba22ac16e2009-10-07 18:00:23 -070054 @Override
55 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -070056 if (LOGV_ENABLED) {
John Reck6c2e2f32011-08-22 13:41:23 -070057 Log.v(LOGTAG, this + " onStart, has state: "
58 + (icicle == null ? "false" : "true"));
The Android Open Source Project0c908882009-03-03 19:32:16 -080059 }
60 super.onCreate(icicle);
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050061
John Reck7878f222012-04-18 16:23:14 -070062 if (shouldIgnoreIntents()) {
63 finish();
64 return;
65 }
66
John Reckf57c0292011-07-21 18:15:39 -070067 // If this was a web search request, pass it on to the default web
68 // search provider and finish this activity.
69 if (IntentHandler.handleWebSearchIntent(this, null, getIntent())) {
70 finish();
71 return;
72 }
John Reck9c35b9c2012-05-30 10:08:50 -070073 mController = createController();
The Android Open Source Project0c908882009-03-03 19:32:16 -080074
George Mount3636d0a2011-11-21 09:08:21 -080075 Intent intent = (icicle == null) ? getIntent() : null;
76 mController.start(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -080077 }
78
John Recka5176f32011-05-17 12:35:37 -070079 public static boolean isTablet(Context context) {
80 return context.getResources().getBoolean(R.bool.isTablet);
John Reck9d27ff52011-02-11 17:47:54 -080081 }
82
John Reck9c35b9c2012-05-30 10:08:50 -070083 private Controller createController() {
84 Controller controller = new Controller(this);
85 boolean xlarge = isTablet(this);
86 UI ui = null;
87 if (xlarge) {
88 ui = new XLargeUi(this, controller);
89 } else {
90 ui = new PhoneUi(this, controller);
91 }
92 controller.setUi(ui);
93 return controller;
94 }
95
John Reck41554852010-12-01 12:53:37 -080096 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -070097 Controller getController() {
John Reck9c35b9c2012-05-30 10:08:50 -070098 return (Controller) mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -070099 }
100
The Android Open Source Project0c908882009-03-03 19:32:16 -0800101 @Override
102 protected void onNewIntent(Intent intent) {
John Reck7878f222012-04-18 16:23:14 -0700103 if (shouldIgnoreIntents()) return;
John Reck63bb6872010-12-01 19:29:32 -0800104 if (ACTION_RESTART.equals(intent.getAction())) {
105 Bundle outState = new Bundle();
John Reck1cf4b792011-07-26 10:22:22 -0700106 mController.onSaveInstanceState(outState);
John Reck63bb6872010-12-01 19:29:32 -0800107 finish();
108 getApplicationContext().startActivity(
109 new Intent(getApplicationContext(), BrowserActivity.class)
110 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
111 .putExtra(EXTRA_STATE, outState));
112 return;
113 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700114 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115 }
116
John Reck7878f222012-04-18 16:23:14 -0700117 private KeyguardManager mKeyguardManager;
118 private PowerManager mPowerManager;
119 private boolean shouldIgnoreIntents() {
120 // Only process intents if the screen is on and the device is unlocked
121 // aka, if we will be user-visible
122 if (mKeyguardManager == null) {
123 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
124 }
125 if (mPowerManager == null) {
126 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
127 }
128 boolean ignore = !mPowerManager.isScreenOn();
129 ignore |= mKeyguardManager.inKeyguardRestrictedInputMode();
130 if (LOGV_ENABLED) {
131 Log.v(LOGTAG, "ignore intents: " + ignore);
132 }
133 return ignore;
134 }
135
Grace Kloba22ac16e2009-10-07 18:00:23 -0700136 @Override
137 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800138 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700139 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
141 }
John Reck9c35b9c2012-05-30 10:08:50 -0700142 mController.onResume();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800143 }
144
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400145 @Override
146 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400147 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700148 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400149 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400150 return true;
151 }
152
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400153 @Override
154 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700155 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400156 }
157
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400158 @Override
159 public void onContextMenuClosed(Menu menu) {
160 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400162 }
163
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400164 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800165 * onSaveInstanceState(Bundle map)
166 * onSaveInstanceState is called right before onStop(). The map contains
167 * the saved state.
168 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700169 @Override
170 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700171 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800172 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
173 }
John Reck9c35b9c2012-05-30 10:08:50 -0700174 mController.onSaveInstanceState(outState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800175 }
176
Grace Kloba22ac16e2009-10-07 18:00:23 -0700177 @Override
178 protected void onPause() {
John Reck9c35b9c2012-05-30 10:08:50 -0700179 mController.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181 }
182
Grace Kloba22ac16e2009-10-07 18:00:23 -0700183 @Override
184 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700185 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
187 }
188 super.onDestroy();
John Reck9c35b9c2012-05-30 10:08:50 -0700189 mController.onDestroy();
190 mController = NullController.INSTANCE;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800191 }
192
193 @Override
194 public void onConfigurationChanged(Configuration newConfig) {
195 super.onConfigurationChanged(newConfig);
John Reck9c35b9c2012-05-30 10:08:50 -0700196 mController.onConfgurationChanged(newConfig);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800197 }
198
Grace Kloba22ac16e2009-10-07 18:00:23 -0700199 @Override
200 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800201 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700202 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700203 }
204
The Android Open Source Project0c908882009-03-03 19:32:16 -0800205 @Override
206 public boolean onCreateOptionsMenu(Menu menu) {
207 super.onCreateOptionsMenu(menu);
John Reck9c35b9c2012-05-30 10:08:50 -0700208 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800209 }
210
211 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 public boolean onPrepareOptionsMenu(Menu menu) {
213 super.onPrepareOptionsMenu(menu);
John Reckb3417f02011-01-14 11:01:05 -0800214 return mController.onPrepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400215 }
216
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217 @Override
218 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700219 if (!mController.onOptionsItemSelected(item)) {
220 return super.onOptionsItemSelected(item);
Michael Kolb370a4f32010-10-06 10:45:32 -0700221 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800222 return true;
223 }
224
225 @Override
226 public void onCreateContextMenu(ContextMenu menu, View v,
227 ContextMenuInfo menuInfo) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700228 mController.onCreateContextMenu(menu, v, menuInfo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800229 }
230
Michael Kolb8233fac2010-10-26 16:08:53 -0700231 @Override
232 public boolean onContextItemSelected(MenuItem item) {
233 return mController.onContextItemSelected(item);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700234 }
235
Grace Kloba5942df02009-09-18 11:48:29 -0700236 @Override
237 public boolean onKeyDown(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700238 return mController.onKeyDown(keyCode, event) ||
239 super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800240 }
241
Grace Kloba5942df02009-09-18 11:48:29 -0700242 @Override
John Recke6bf4ab2011-02-24 15:48:05 -0800243 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
244 return mController.onKeyLongPress(keyCode, event) ||
245 super.onKeyLongPress(keyCode, event);
246 }
247
248 @Override
Grace Kloba5942df02009-09-18 11:48:29 -0700249 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700250 return mController.onKeyUp(keyCode, event) ||
251 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800252 }
253
Michael Kolbe421c242010-10-04 19:29:01 -0700254 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700255 public void onActionModeStarted(ActionMode mode) {
256 super.onActionModeStarted(mode);
257 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700258 }
259
Michael Kolbe421c242010-10-04 19:29:01 -0700260 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700261 public void onActionModeFinished(ActionMode mode) {
262 super.onActionModeFinished(mode);
263 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700264 }
265
The Android Open Source Project0c908882009-03-03 19:32:16 -0800266 @Override
267 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700268 Intent intent) {
269 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100270 }
271
Michael Kolbfbc579a2011-07-07 15:59:33 -0700272 @Override
273 public boolean onSearchRequested() {
274 return mController.onSearchRequested();
275 }
276
Michael Kolbc3af0672011-08-09 10:24:41 -0700277 @Override
278 public boolean dispatchKeyEvent(KeyEvent event) {
279 return mController.dispatchKeyEvent(event)
280 || super.dispatchKeyEvent(event);
281 }
282
283 @Override
284 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
285 return mController.dispatchKeyShortcutEvent(event)
286 || super.dispatchKeyShortcutEvent(event);
287 }
288
289 @Override
290 public boolean dispatchTouchEvent(MotionEvent ev) {
291 return mController.dispatchTouchEvent(ev)
292 || super.dispatchTouchEvent(ev);
293 }
294
295 @Override
296 public boolean dispatchTrackballEvent(MotionEvent ev) {
297 return mController.dispatchTrackballEvent(ev)
298 || super.dispatchTrackballEvent(ev);
299 }
300
301 @Override
302 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
303 return mController.dispatchGenericMotionEvent(ev) ||
304 super.dispatchGenericMotionEvent(ev);
305 }
306
The Android Open Source Project0c908882009-03-03 19:32:16 -0800307}