blob: 851177808b663ddda8ea3c1f6ffbc41a0f0e69ea [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";
46
Michael Kolb8233fac2010-10-26 16:08:53 -070047 private final static String LOGTAG = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -080048
John Reck6c2e2f32011-08-22 13:41:23 -070049 private final static boolean LOGV_ENABLED = Browser.LOGV_ENABLED;
Dave Bort31a6d1c2009-04-13 15:56:49 -070050
John Reck9c35b9c2012-05-30 10:08:50 -070051 private ActivityController mController = NullController.INSTANCE;
Jeff Davidson43610292010-07-16 16:03:58 -070052
Grace Kloba22ac16e2009-10-07 18:00:23 -070053 @Override
54 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -070055 if (LOGV_ENABLED) {
John Reck6c2e2f32011-08-22 13:41:23 -070056 Log.v(LOGTAG, this + " onStart, has state: "
57 + (icicle == null ? "false" : "true"));
The Android Open Source Project0c908882009-03-03 19:32:16 -080058 }
59 super.onCreate(icicle);
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050060
John Reck7878f222012-04-18 16:23:14 -070061 if (shouldIgnoreIntents()) {
62 finish();
63 return;
64 }
65
John Reckf57c0292011-07-21 18:15:39 -070066 // If this was a web search request, pass it on to the default web
67 // search provider and finish this activity.
68 if (IntentHandler.handleWebSearchIntent(this, null, getIntent())) {
69 finish();
70 return;
71 }
John Reck9c35b9c2012-05-30 10:08:50 -070072 mController = createController();
The Android Open Source Project0c908882009-03-03 19:32:16 -080073
George Mount3636d0a2011-11-21 09:08:21 -080074 Intent intent = (icicle == null) ? getIntent() : null;
75 mController.start(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -080076 }
77
John Recka5176f32011-05-17 12:35:37 -070078 public static boolean isTablet(Context context) {
79 return context.getResources().getBoolean(R.bool.isTablet);
John Reck9d27ff52011-02-11 17:47:54 -080080 }
81
John Reck9c35b9c2012-05-30 10:08:50 -070082 private Controller createController() {
83 Controller controller = new Controller(this);
84 boolean xlarge = isTablet(this);
85 UI ui = null;
86 if (xlarge) {
87 ui = new XLargeUi(this, controller);
88 } else {
89 ui = new PhoneUi(this, controller);
90 }
91 controller.setUi(ui);
92 return controller;
93 }
94
John Reck41554852010-12-01 12:53:37 -080095 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -070096 Controller getController() {
John Reck9c35b9c2012-05-30 10:08:50 -070097 return (Controller) mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -070098 }
99
The Android Open Source Project0c908882009-03-03 19:32:16 -0800100 @Override
101 protected void onNewIntent(Intent intent) {
John Reck7878f222012-04-18 16:23:14 -0700102 if (shouldIgnoreIntents()) return;
John Reck63bb6872010-12-01 19:29:32 -0800103 if (ACTION_RESTART.equals(intent.getAction())) {
104 Bundle outState = new Bundle();
John Reck1cf4b792011-07-26 10:22:22 -0700105 mController.onSaveInstanceState(outState);
John Reck63bb6872010-12-01 19:29:32 -0800106 finish();
107 getApplicationContext().startActivity(
108 new Intent(getApplicationContext(), BrowserActivity.class)
109 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
110 .putExtra(EXTRA_STATE, outState));
111 return;
112 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700113 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114 }
115
John Reck7878f222012-04-18 16:23:14 -0700116 private KeyguardManager mKeyguardManager;
117 private PowerManager mPowerManager;
118 private boolean shouldIgnoreIntents() {
119 // Only process intents if the screen is on and the device is unlocked
120 // aka, if we will be user-visible
121 if (mKeyguardManager == null) {
122 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
123 }
124 if (mPowerManager == null) {
125 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
126 }
127 boolean ignore = !mPowerManager.isScreenOn();
128 ignore |= mKeyguardManager.inKeyguardRestrictedInputMode();
129 if (LOGV_ENABLED) {
130 Log.v(LOGTAG, "ignore intents: " + ignore);
131 }
132 return ignore;
133 }
134
Grace Kloba22ac16e2009-10-07 18:00:23 -0700135 @Override
136 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700138 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800139 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
140 }
John Reck9c35b9c2012-05-30 10:08:50 -0700141 mController.onResume();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800142 }
143
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400144 @Override
145 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400146 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700147 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400148 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400149 return true;
150 }
151
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400152 @Override
153 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700154 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400155 }
156
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400157 @Override
158 public void onContextMenuClosed(Menu menu) {
159 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700160 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400161 }
162
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400163 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164 * onSaveInstanceState(Bundle map)
165 * onSaveInstanceState is called right before onStop(). The map contains
166 * the saved state.
167 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700168 @Override
169 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700170 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800171 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
172 }
John Reck9c35b9c2012-05-30 10:08:50 -0700173 mController.onSaveInstanceState(outState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800174 }
175
Grace Kloba22ac16e2009-10-07 18:00:23 -0700176 @Override
177 protected void onPause() {
John Reck9c35b9c2012-05-30 10:08:50 -0700178 mController.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800179 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180 }
181
Grace Kloba22ac16e2009-10-07 18:00:23 -0700182 @Override
183 protected void onDestroy() {
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.onDestroy: this=" + this);
186 }
187 super.onDestroy();
John Reck9c35b9c2012-05-30 10:08:50 -0700188 mController.onDestroy();
189 mController = NullController.INSTANCE;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 }
191
192 @Override
193 public void onConfigurationChanged(Configuration newConfig) {
194 super.onConfigurationChanged(newConfig);
John Reck9c35b9c2012-05-30 10:08:50 -0700195 mController.onConfgurationChanged(newConfig);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196 }
197
Grace Kloba22ac16e2009-10-07 18:00:23 -0700198 @Override
199 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800200 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700201 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700202 }
203
The Android Open Source Project0c908882009-03-03 19:32:16 -0800204 @Override
205 public boolean onCreateOptionsMenu(Menu menu) {
206 super.onCreateOptionsMenu(menu);
John Reck9c35b9c2012-05-30 10:08:50 -0700207 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800208 }
209
210 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700211 public boolean onPrepareOptionsMenu(Menu menu) {
212 super.onPrepareOptionsMenu(menu);
John Reckb3417f02011-01-14 11:01:05 -0800213 return mController.onPrepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400214 }
215
The Android Open Source Project0c908882009-03-03 19:32:16 -0800216 @Override
217 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700218 if (!mController.onOptionsItemSelected(item)) {
219 return super.onOptionsItemSelected(item);
Michael Kolb370a4f32010-10-06 10:45:32 -0700220 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800221 return true;
222 }
223
224 @Override
225 public void onCreateContextMenu(ContextMenu menu, View v,
226 ContextMenuInfo menuInfo) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700227 mController.onCreateContextMenu(menu, v, menuInfo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800228 }
229
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 @Override
231 public boolean onContextItemSelected(MenuItem item) {
232 return mController.onContextItemSelected(item);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700233 }
234
Grace Kloba5942df02009-09-18 11:48:29 -0700235 @Override
236 public boolean onKeyDown(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 return mController.onKeyDown(keyCode, event) ||
238 super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800239 }
240
Grace Kloba5942df02009-09-18 11:48:29 -0700241 @Override
John Recke6bf4ab2011-02-24 15:48:05 -0800242 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
243 return mController.onKeyLongPress(keyCode, event) ||
244 super.onKeyLongPress(keyCode, event);
245 }
246
247 @Override
Grace Kloba5942df02009-09-18 11:48:29 -0700248 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700249 return mController.onKeyUp(keyCode, event) ||
250 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800251 }
252
Michael Kolbe421c242010-10-04 19:29:01 -0700253 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700254 public void onActionModeStarted(ActionMode mode) {
255 super.onActionModeStarted(mode);
256 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700257 }
258
Michael Kolbe421c242010-10-04 19:29:01 -0700259 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 public void onActionModeFinished(ActionMode mode) {
261 super.onActionModeFinished(mode);
262 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700263 }
264
The Android Open Source Project0c908882009-03-03 19:32:16 -0800265 @Override
266 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 Intent intent) {
268 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100269 }
270
Michael Kolbfbc579a2011-07-07 15:59:33 -0700271 @Override
272 public boolean onSearchRequested() {
273 return mController.onSearchRequested();
274 }
275
Michael Kolbc3af0672011-08-09 10:24:41 -0700276 @Override
277 public boolean dispatchKeyEvent(KeyEvent event) {
278 return mController.dispatchKeyEvent(event)
279 || super.dispatchKeyEvent(event);
280 }
281
282 @Override
283 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
284 return mController.dispatchKeyShortcutEvent(event)
285 || super.dispatchKeyShortcutEvent(event);
286 }
287
288 @Override
289 public boolean dispatchTouchEvent(MotionEvent ev) {
290 return mController.dispatchTouchEvent(ev)
291 || super.dispatchTouchEvent(ev);
292 }
293
294 @Override
295 public boolean dispatchTrackballEvent(MotionEvent ev) {
296 return mController.dispatchTrackballEvent(ev)
297 || super.dispatchTrackballEvent(ev);
298 }
299
300 @Override
301 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
302 return mController.dispatchGenericMotionEvent(ev) ||
303 super.dispatchGenericMotionEvent(ev);
304 }
305
The Android Open Source Project0c908882009-03-03 19:32:16 -0800306}