blob: 0fce014aee207c7c195f1c65e46427f35448a672 [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
John Reck41554852010-12-01 12:53:37 -080019import com.google.common.annotations.VisibleForTesting;
20
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.app.Activity;
Patrick Scott539e2ec2011-01-13 11:27:38 -050022import android.app.ProgressDialog;
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.graphics.Bitmap;
Andrei Popescu540035d2009-09-18 18:59:20 +010026import android.graphics.BitmapFactory;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040027import android.graphics.PixelFormat;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.os.Bundle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.util.Log;
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -040030import android.view.ActionMode;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.view.ContextMenu;
Michael Kolba2b2ba82010-08-04 17:54:03 -070032import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.view.KeyEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.view.Menu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.view.MenuItem;
36import android.view.View;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.view.Window;
38import android.view.WindowManager;
Svetoslav Ganov2b345992010-05-06 06:13:54 -070039import android.view.accessibility.AccessibilityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040
Michael Kolb8233fac2010-10-26 16:08:53 -070041public class BrowserActivity extends Activity {
The Android Open Source Project0c908882009-03-03 19:32:16 -080042
John Reck439c9a52010-12-14 10:04:39 -080043 public static final String ACTION_SHOW_BOOKMARKS = "show_bookmarks";
44 public static final String ACTION_SHOW_BROWSER = "show_browser";
John Reck63bb6872010-12-01 19:29:32 -080045 public static final String ACTION_RESTART = "--restart--";
46 private static final String EXTRA_STATE = "state";
47
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
Michael Kolb8233fac2010-10-26 16:08:53 -070050 private final static boolean LOGV_ENABLED =
51 com.android.browser.Browser.LOGV_ENABLED;
Dave Bort31a6d1c2009-04-13 15:56:49 -070052
Michael Kolb8233fac2010-10-26 16:08:53 -070053 private Controller mController;
54 private UI mUi;
Jeff Davidson43610292010-07-16 16:03:58 -070055
Grace Kloba22ac16e2009-10-07 18:00:23 -070056 @Override
57 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -070058 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -080059 Log.v(LOGTAG, this + " onStart");
60 }
61 super.onCreate(icicle);
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050062
Patrick Scott539e2ec2011-01-13 11:27:38 -050063 BrowserSettings settings = BrowserSettings.getInstance();
64
Ben Murdochef671652010-11-25 17:17:58 +000065 // We load the first set of BrowserSettings from the db asynchronously
66 // but if it has not completed at this point, we have no choice but
67 // to block waiting for them to finish loading. :(
Patrick Scott539e2ec2011-01-13 11:27:38 -050068 settings.waitForLoadFromDbToComplete();
Ben Murdochef671652010-11-25 17:17:58 +000069
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050070 // render the browser in OpenGL
Patrick Scott539e2ec2011-01-13 11:27:38 -050071 if (settings.isHardwareAccelerated()) {
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050072 // Set the flag in the activity's window
73 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
74 WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
75 } else {
76 // Clear the flag in the activity's window
77 this.getWindow().setFlags(0, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
78 }
The Android Open Source Project0c908882009-03-03 19:32:16 -080079
Mike Reedd334bf52010-01-26 15:21:44 -050080 // enable this to test the browser in 32bit
81 if (false) {
82 getWindow().setFormat(PixelFormat.RGBX_8888);
83 BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
84 }
85
Michael Kolb8233fac2010-10-26 16:08:53 -070086 // If this was a web search request, pass it on to the default web
87 // search provider and finish this activity.
88 if (IntentHandler.handleWebSearchIntent(this, null, getIntent())) {
89 finish();
90 return;
91 }
92
93 if (((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
94 .isEnabled()) {
Svetoslav Ganov2b345992010-05-06 06:13:54 -070095 setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
96 } else {
97 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
98 }
The Android Open Source Project0c908882009-03-03 19:32:16 -080099
Michael Kolb8233fac2010-10-26 16:08:53 -0700100 mController = new Controller(this);
Michael Kolb66706532010-12-12 19:50:22 -0800101 boolean xlarge = (getResources().getConfiguration().screenLayout
102 & Configuration.SCREENLAYOUT_SIZE_MASK)
103 == Configuration.SCREENLAYOUT_SIZE_XLARGE;
104 if (xlarge) {
105 mUi = new XLargeUi(this, mController);
106 } else {
107 mUi = new PhoneUi(this, mController);
108 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700109 mController.setUi(mUi);
110 mController.setWebViewFactory((BaseUi) mUi);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800111
John Reck63bb6872010-12-01 19:29:32 -0800112 Bundle state = getIntent().getBundleExtra(EXTRA_STATE);
113 if (state != null && icicle == null) {
114 icicle = state;
115 }
Patrick Scott539e2ec2011-01-13 11:27:38 -0500116
117 String account = settings.getAutoLoginAccount(this);
118 if (settings.isAutoLoginEnabled() && account != null) {
119 GoogleAccountLogin login =
120 new GoogleAccountLogin(this, account);
121 final ProgressDialog dialog = ProgressDialog.show(this,
122 getString(R.string.pref_autologin_title),
123 getString(R.string.pref_autologin_progress, account),
124 true /* indeterminate */,
125 true /* cancelable */,
126 login);
127 final Bundle b = icicle;
128 final Runnable start = new Runnable() {
129 @Override public void run() {
130 dialog.dismiss();
131 mController.start(b, getIntent());
132 }
133 };
134 login.startLogin(start);
135 } else {
136 mController.start(icicle, getIntent());
137 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800138 }
139
John Reck41554852010-12-01 12:53:37 -0800140 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -0700141 Controller getController() {
142 return mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700143 }
144
The Android Open Source Project0c908882009-03-03 19:32:16 -0800145 @Override
146 protected void onNewIntent(Intent intent) {
John Reck63bb6872010-12-01 19:29:32 -0800147 if (ACTION_RESTART.equals(intent.getAction())) {
148 Bundle outState = new Bundle();
149 mController.onSaveInstanceState(outState);
150 finish();
151 getApplicationContext().startActivity(
152 new Intent(getApplicationContext(), BrowserActivity.class)
153 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
154 .putExtra(EXTRA_STATE, outState));
155 return;
156 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158 }
159
Grace Kloba22ac16e2009-10-07 18:00:23 -0700160 @Override
161 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800162 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700163 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
165 }
Michael Kolb06f03e42010-12-06 10:18:26 -0800166 if (mController != null) {
167 mController.onResume();
168 }
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 }
Michael Kolb8233fac2010-10-26 16:08:53 -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() {
Michael Kolb06f03e42010-12-06 10:18:26 -0800205 if (mController != null) {
206 mController.onPause();
207 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800208 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800209 }
210
Grace Kloba22ac16e2009-10-07 18:00:23 -0700211 @Override
212 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700213 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800214 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
215 }
216 super.onDestroy();
Michael Kolb06f03e42010-12-06 10:18:26 -0800217 if (mController != null) {
218 mController.onDestroy();
219 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700220 mUi = null;
221 mController = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800222 }
223
224 @Override
225 public void onConfigurationChanged(Configuration newConfig) {
226 super.onConfigurationChanged(newConfig);
Michael Kolb8233fac2010-10-26 16:08:53 -0700227 mController.onConfgurationChanged(newConfig);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800228 }
229
Grace Kloba22ac16e2009-10-07 18:00:23 -0700230 @Override
231 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800232 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700233 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700234 }
235
The Android Open Source Project0c908882009-03-03 19:32:16 -0800236 @Override
237 public boolean onCreateOptionsMenu(Menu menu) {
238 super.onCreateOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700239 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800240 }
241
242 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700243 public boolean onPrepareOptionsMenu(Menu menu) {
244 super.onPrepareOptionsMenu(menu);
245 return mController.prepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400246 }
247
The Android Open Source Project0c908882009-03-03 19:32:16 -0800248 @Override
249 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700250 if (!mController.onOptionsItemSelected(item)) {
251 return super.onOptionsItemSelected(item);
Michael Kolb370a4f32010-10-06 10:45:32 -0700252 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800253 return true;
254 }
255
256 @Override
257 public void onCreateContextMenu(ContextMenu menu, View v,
258 ContextMenuInfo menuInfo) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700259 mController.onCreateContextMenu(menu, v, menuInfo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800260 }
261
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 @Override
263 public boolean onContextItemSelected(MenuItem item) {
264 return mController.onContextItemSelected(item);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700265 }
266
Grace Kloba5942df02009-09-18 11:48:29 -0700267 @Override
268 public boolean onKeyDown(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 return mController.onKeyDown(keyCode, event) ||
270 super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800271 }
272
Grace Kloba5942df02009-09-18 11:48:29 -0700273 @Override
274 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 return mController.onKeyUp(keyCode, event) ||
276 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800277 }
278
Michael Kolbe421c242010-10-04 19:29:01 -0700279 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 public void onActionModeStarted(ActionMode mode) {
281 super.onActionModeStarted(mode);
282 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700283 }
284
Michael Kolbe421c242010-10-04 19:29:01 -0700285 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700286 public void onActionModeFinished(ActionMode mode) {
287 super.onActionModeFinished(mode);
288 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700289 }
290
The Android Open Source Project0c908882009-03-03 19:32:16 -0800291 @Override
292 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700293 Intent intent) {
294 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100295 }
296
Andrei Popescu56199cc2010-01-12 22:39:16 +0000297
The Android Open Source Project0c908882009-03-03 19:32:16 -0800298}