blob: 3ecdcb4b35c6b14bd35cc5bd2352318950fca6ac [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;
qqzhou8c5b0a32013-07-22 15:31:03 +080025import android.os.Handler;
John Reck7878f222012-04-18 16:23:14 -070026import android.os.PowerManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080027import android.util.Log;
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -040028import android.view.ActionMode;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.view.ContextMenu;
Michael Kolba2b2ba82010-08-04 17:54:03 -070030import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.view.KeyEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.view.Menu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.view.MenuItem;
Michael Kolbc3af0672011-08-09 10:24:41 -070034import android.view.MotionEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.view.View;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.view.Window;
qqzhou8c5b0a32013-07-22 15:31:03 +080037import android.webkit.JavascriptInterface;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038
qqzhou8c5b0a32013-07-22 15:31:03 +080039import com.android.browser.UI.ComboViews;
John Reck9c35b9c2012-05-30 10:08:50 -070040import com.android.browser.stub.NullController;
qqzhou8c5b0a32013-07-22 15:31:03 +080041
John Reckd3e4d5b2011-07-13 15:48:43 -070042import com.google.common.annotations.VisibleForTesting;
43
Michael Kolb8233fac2010-10-26 16:08:53 -070044public class BrowserActivity extends Activity {
The Android Open Source Project0c908882009-03-03 19:32:16 -080045
John Reck439c9a52010-12-14 10:04:39 -080046 public static final String ACTION_SHOW_BOOKMARKS = "show_bookmarks";
47 public static final String ACTION_SHOW_BROWSER = "show_browser";
John Reck63bb6872010-12-01 19:29:32 -080048 public static final String ACTION_RESTART = "--restart--";
49 private static final String EXTRA_STATE = "state";
John Reck38b39652012-06-05 09:22:59 -070050 public static final String EXTRA_DISABLE_URL_OVERRIDE = "disable_url_override";
John Reck63bb6872010-12-01 19:29:32 -080051
Michael Kolb8233fac2010-10-26 16:08:53 -070052 private final static String LOGTAG = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -080053
John Reck6c2e2f32011-08-22 13:41:23 -070054 private final static boolean LOGV_ENABLED = Browser.LOGV_ENABLED;
Dave Bort31a6d1c2009-04-13 15:56:49 -070055
John Reck9c35b9c2012-05-30 10:08:50 -070056 private ActivityController mController = NullController.INSTANCE;
qqzhou8c5b0a32013-07-22 15:31:03 +080057 private Handler mHandler = new Handler();
Jeff Davidson43610292010-07-16 16:03:58 -070058
Grace Kloba22ac16e2009-10-07 18:00:23 -070059 @Override
60 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -070061 if (LOGV_ENABLED) {
John Reck6c2e2f32011-08-22 13:41:23 -070062 Log.v(LOGTAG, this + " onStart, has state: "
63 + (icicle == null ? "false" : "true"));
The Android Open Source Project0c908882009-03-03 19:32:16 -080064 }
65 super.onCreate(icicle);
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050066
John Reck7878f222012-04-18 16:23:14 -070067 if (shouldIgnoreIntents()) {
68 finish();
69 return;
70 }
71
John Reckf57c0292011-07-21 18:15:39 -070072 // If this was a web search request, pass it on to the default web
73 // search provider and finish this activity.
74 if (IntentHandler.handleWebSearchIntent(this, null, getIntent())) {
75 finish();
76 return;
77 }
John Reck9c35b9c2012-05-30 10:08:50 -070078 mController = createController();
The Android Open Source Project0c908882009-03-03 19:32:16 -080079
George Mount3636d0a2011-11-21 09:08:21 -080080 Intent intent = (icicle == null) ? getIntent() : null;
81 mController.start(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -080082 }
83
John Recka5176f32011-05-17 12:35:37 -070084 public static boolean isTablet(Context context) {
85 return context.getResources().getBoolean(R.bool.isTablet);
John Reck9d27ff52011-02-11 17:47:54 -080086 }
87
John Reck9c35b9c2012-05-30 10:08:50 -070088 private Controller createController() {
89 Controller controller = new Controller(this);
90 boolean xlarge = isTablet(this);
91 UI ui = null;
92 if (xlarge) {
93 ui = new XLargeUi(this, controller);
94 } else {
95 ui = new PhoneUi(this, controller);
96 }
97 controller.setUi(ui);
98 return controller;
99 }
100
John Reck41554852010-12-01 12:53:37 -0800101 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -0700102 Controller getController() {
John Reck9c35b9c2012-05-30 10:08:50 -0700103 return (Controller) mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700104 }
105
The Android Open Source Project0c908882009-03-03 19:32:16 -0800106 @Override
107 protected void onNewIntent(Intent intent) {
John Reck7878f222012-04-18 16:23:14 -0700108 if (shouldIgnoreIntents()) return;
John Reck63bb6872010-12-01 19:29:32 -0800109 if (ACTION_RESTART.equals(intent.getAction())) {
110 Bundle outState = new Bundle();
John Reck1cf4b792011-07-26 10:22:22 -0700111 mController.onSaveInstanceState(outState);
John Reck63bb6872010-12-01 19:29:32 -0800112 finish();
113 getApplicationContext().startActivity(
114 new Intent(getApplicationContext(), BrowserActivity.class)
115 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
116 .putExtra(EXTRA_STATE, outState));
117 return;
118 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700119 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800120 }
121
John Reck7878f222012-04-18 16:23:14 -0700122 private KeyguardManager mKeyguardManager;
123 private PowerManager mPowerManager;
124 private boolean shouldIgnoreIntents() {
125 // Only process intents if the screen is on and the device is unlocked
126 // aka, if we will be user-visible
127 if (mKeyguardManager == null) {
128 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
129 }
130 if (mPowerManager == null) {
131 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
132 }
133 boolean ignore = !mPowerManager.isScreenOn();
134 ignore |= mKeyguardManager.inKeyguardRestrictedInputMode();
135 if (LOGV_ENABLED) {
136 Log.v(LOGTAG, "ignore intents: " + ignore);
137 }
138 return ignore;
139 }
140
Grace Kloba22ac16e2009-10-07 18:00:23 -0700141 @Override
142 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800143 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700144 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800145 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
146 }
John Reck9c35b9c2012-05-30 10:08:50 -0700147 mController.onResume();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148 }
149
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400150 @Override
151 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400152 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700153 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400154 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400155 return true;
156 }
157
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400158 @Override
159 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700160 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400161 }
162
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400163 @Override
164 public void onContextMenuClosed(Menu menu) {
165 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700166 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400167 }
168
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400169 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800170 * onSaveInstanceState(Bundle map)
171 * onSaveInstanceState is called right before onStop(). The map contains
172 * the saved state.
173 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700174 @Override
175 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700176 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800177 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
178 }
John Reck9c35b9c2012-05-30 10:08:50 -0700179 mController.onSaveInstanceState(outState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180 }
181
Grace Kloba22ac16e2009-10-07 18:00:23 -0700182 @Override
183 protected void onPause() {
John Reck9c35b9c2012-05-30 10:08:50 -0700184 mController.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800185 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 }
187
Grace Kloba22ac16e2009-10-07 18:00:23 -0700188 @Override
189 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700190 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800191 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
192 }
193 super.onDestroy();
John Reck9c35b9c2012-05-30 10:08:50 -0700194 mController.onDestroy();
195 mController = NullController.INSTANCE;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196 }
197
198 @Override
199 public void onConfigurationChanged(Configuration newConfig) {
200 super.onConfigurationChanged(newConfig);
John Reck9c35b9c2012-05-30 10:08:50 -0700201 mController.onConfgurationChanged(newConfig);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800202 }
203
Grace Kloba22ac16e2009-10-07 18:00:23 -0700204 @Override
205 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700208 }
209
The Android Open Source Project0c908882009-03-03 19:32:16 -0800210 @Override
211 public boolean onCreateOptionsMenu(Menu menu) {
212 super.onCreateOptionsMenu(menu);
John Reck9c35b9c2012-05-30 10:08:50 -0700213 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800214 }
215
216 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700217 public boolean onPrepareOptionsMenu(Menu menu) {
218 super.onPrepareOptionsMenu(menu);
John Reckb3417f02011-01-14 11:01:05 -0800219 return mController.onPrepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400220 }
221
The Android Open Source Project0c908882009-03-03 19:32:16 -0800222 @Override
223 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700224 if (!mController.onOptionsItemSelected(item)) {
225 return super.onOptionsItemSelected(item);
Michael Kolb370a4f32010-10-06 10:45:32 -0700226 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800227 return true;
228 }
229
230 @Override
231 public void onCreateContextMenu(ContextMenu menu, View v,
232 ContextMenuInfo menuInfo) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700233 mController.onCreateContextMenu(menu, v, menuInfo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800234 }
235
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 @Override
237 public boolean onContextItemSelected(MenuItem item) {
238 return mController.onContextItemSelected(item);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700239 }
240
Grace Kloba5942df02009-09-18 11:48:29 -0700241 @Override
242 public boolean onKeyDown(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700243 return mController.onKeyDown(keyCode, event) ||
244 super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800245 }
246
Grace Kloba5942df02009-09-18 11:48:29 -0700247 @Override
John Recke6bf4ab2011-02-24 15:48:05 -0800248 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
249 return mController.onKeyLongPress(keyCode, event) ||
250 super.onKeyLongPress(keyCode, event);
251 }
252
253 @Override
Grace Kloba5942df02009-09-18 11:48:29 -0700254 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700255 return mController.onKeyUp(keyCode, event) ||
256 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800257 }
258
Michael Kolbe421c242010-10-04 19:29:01 -0700259 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 public void onActionModeStarted(ActionMode mode) {
261 super.onActionModeStarted(mode);
262 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700263 }
264
Michael Kolbe421c242010-10-04 19:29:01 -0700265 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700266 public void onActionModeFinished(ActionMode mode) {
267 super.onActionModeFinished(mode);
268 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700269 }
270
The Android Open Source Project0c908882009-03-03 19:32:16 -0800271 @Override
272 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 Intent intent) {
274 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100275 }
276
Michael Kolbfbc579a2011-07-07 15:59:33 -0700277 @Override
278 public boolean onSearchRequested() {
279 return mController.onSearchRequested();
280 }
281
Michael Kolbc3af0672011-08-09 10:24:41 -0700282 @Override
283 public boolean dispatchKeyEvent(KeyEvent event) {
284 return mController.dispatchKeyEvent(event)
285 || super.dispatchKeyEvent(event);
286 }
287
288 @Override
289 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
290 return mController.dispatchKeyShortcutEvent(event)
291 || super.dispatchKeyShortcutEvent(event);
292 }
293
294 @Override
295 public boolean dispatchTouchEvent(MotionEvent ev) {
296 return mController.dispatchTouchEvent(ev)
297 || super.dispatchTouchEvent(ev);
298 }
299
300 @Override
301 public boolean dispatchTrackballEvent(MotionEvent ev) {
302 return mController.dispatchTrackballEvent(ev)
303 || super.dispatchTrackballEvent(ev);
304 }
305
306 @Override
307 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
308 return mController.dispatchGenericMotionEvent(ev) ||
309 super.dispatchGenericMotionEvent(ev);
310 }
311
qqzhou8c5b0a32013-07-22 15:31:03 +0800312 // add for carrier homepage feature
313 @JavascriptInterface
314 public void loadBookmarks() {
315 mHandler.post(new Runnable() {
316 @Override
317 public void run() {
318 if (mController instanceof Controller) {
319 ((Controller)mController).bookmarksOrHistoryPicker(ComboViews.Bookmarks);
320 }
321 }
322 });
323 }
324
325 // add for carrier homepage feature
326 @JavascriptInterface
327 public void loadHistory() {
328 mHandler.post(new Runnable() {
329 @Override
330 public void run() {
331 if (mController instanceof Controller) {
332 ((Controller)mController).bookmarksOrHistoryPicker(ComboViews.History);
333 }
334 }
335 });
336 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800337}