blob: 018744dd9a779a4ae1c4405efcad72aee1d9d819 [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;
Patrick Scott97efb1e2011-01-13 14:31:54 -050029import android.os.Handler;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.util.Log;
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -040031import android.view.ActionMode;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.view.ContextMenu;
Michael Kolba2b2ba82010-08-04 17:54:03 -070033import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.view.KeyEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.view.Menu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.view.MenuItem;
37import android.view.View;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.view.Window;
39import android.view.WindowManager;
Svetoslav Ganov2b345992010-05-06 06:13:54 -070040import android.view.accessibility.AccessibilityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041
Michael Kolb8233fac2010-10-26 16:08:53 -070042public class BrowserActivity extends Activity {
The Android Open Source Project0c908882009-03-03 19:32:16 -080043
John Reck439c9a52010-12-14 10:04:39 -080044 public static final String ACTION_SHOW_BOOKMARKS = "show_bookmarks";
45 public static final String ACTION_SHOW_BROWSER = "show_browser";
John Reck63bb6872010-12-01 19:29:32 -080046 public static final String ACTION_RESTART = "--restart--";
47 private static final String EXTRA_STATE = "state";
48
Michael Kolb8233fac2010-10-26 16:08:53 -070049 private final static String LOGTAG = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -080050
Michael Kolb8233fac2010-10-26 16:08:53 -070051 private final static boolean LOGV_ENABLED =
52 com.android.browser.Browser.LOGV_ENABLED;
Dave Bort31a6d1c2009-04-13 15:56:49 -070053
Michael Kolb8233fac2010-10-26 16:08:53 -070054 private Controller mController;
55 private UI mUi;
Jeff Davidson43610292010-07-16 16:03:58 -070056
Grace Kloba22ac16e2009-10-07 18:00:23 -070057 @Override
58 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -070059 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -080060 Log.v(LOGTAG, this + " onStart");
61 }
62 super.onCreate(icicle);
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050063
Patrick Scott539e2ec2011-01-13 11:27:38 -050064 BrowserSettings settings = BrowserSettings.getInstance();
65
Ben Murdochef671652010-11-25 17:17:58 +000066 // We load the first set of BrowserSettings from the db asynchronously
67 // but if it has not completed at this point, we have no choice but
68 // to block waiting for them to finish loading. :(
Patrick Scott539e2ec2011-01-13 11:27:38 -050069 settings.waitForLoadFromDbToComplete();
Ben Murdochef671652010-11-25 17:17:58 +000070
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050071 // render the browser in OpenGL
Patrick Scott539e2ec2011-01-13 11:27:38 -050072 if (settings.isHardwareAccelerated()) {
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050073 // Set the flag in the activity's window
74 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
75 WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
76 } else {
77 // Clear the flag in the activity's window
78 this.getWindow().setFlags(0, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
79 }
The Android Open Source Project0c908882009-03-03 19:32:16 -080080
Mike Reedd334bf52010-01-26 15:21:44 -050081 // enable this to test the browser in 32bit
82 if (false) {
83 getWindow().setFormat(PixelFormat.RGBX_8888);
84 BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
85 }
86
Michael Kolb8233fac2010-10-26 16:08:53 -070087 // If this was a web search request, pass it on to the default web
88 // search provider and finish this activity.
89 if (IntentHandler.handleWebSearchIntent(this, null, getIntent())) {
90 finish();
91 return;
92 }
93
94 if (((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
95 .isEnabled()) {
Svetoslav Ganov2b345992010-05-06 06:13:54 -070096 setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
97 } else {
98 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
99 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800100
Michael Kolb8233fac2010-10-26 16:08:53 -0700101 mController = new Controller(this);
Michael Kolb66706532010-12-12 19:50:22 -0800102 boolean xlarge = (getResources().getConfiguration().screenLayout
103 & Configuration.SCREENLAYOUT_SIZE_MASK)
104 == Configuration.SCREENLAYOUT_SIZE_XLARGE;
105 if (xlarge) {
106 mUi = new XLargeUi(this, mController);
107 } else {
108 mUi = new PhoneUi(this, mController);
109 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700110 mController.setUi(mUi);
111 mController.setWebViewFactory((BaseUi) mUi);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112
John Reck63bb6872010-12-01 19:29:32 -0800113 Bundle state = getIntent().getBundleExtra(EXTRA_STATE);
114 if (state != null && icicle == null) {
115 icicle = state;
116 }
Patrick Scott539e2ec2011-01-13 11:27:38 -0500117
118 String account = settings.getAutoLoginAccount(this);
Patrick Scott97efb1e2011-01-13 14:31:54 -0500119 if (settings.isAutoLoginEnabled() && account != null &&
120 !GoogleAccountLogin.isLoggedIn()) {
Patrick Scott539e2ec2011-01-13 11:27:38 -0500121 GoogleAccountLogin login =
122 new GoogleAccountLogin(this, account);
123 final ProgressDialog dialog = ProgressDialog.show(this,
124 getString(R.string.pref_autologin_title),
125 getString(R.string.pref_autologin_progress, account),
126 true /* indeterminate */,
127 true /* cancelable */,
128 login);
129 final Bundle b = icicle;
Patrick Scott97efb1e2011-01-13 14:31:54 -0500130 final Handler handler = new Handler();
131 final Runnable dismiss = new Runnable() {
Patrick Scott539e2ec2011-01-13 11:27:38 -0500132 @Override public void run() {
133 dialog.dismiss();
Patrick Scott97efb1e2011-01-13 14:31:54 -0500134 }
135 };
136 final Runnable start = new Runnable() {
137 @Override public void run() {
138 // Post a delayed dismiss message to avoid a flash of the
139 // progress dialog.
140 handler.postDelayed(dismiss, 1000);
Patrick Scott539e2ec2011-01-13 11:27:38 -0500141 mController.start(b, getIntent());
142 }
143 };
144 login.startLogin(start);
145 } else {
146 mController.start(icicle, getIntent());
147 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148 }
149
John Reck41554852010-12-01 12:53:37 -0800150 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -0700151 Controller getController() {
152 return mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700153 }
154
The Android Open Source Project0c908882009-03-03 19:32:16 -0800155 @Override
156 protected void onNewIntent(Intent intent) {
John Reck63bb6872010-12-01 19:29:32 -0800157 if (ACTION_RESTART.equals(intent.getAction())) {
158 Bundle outState = new Bundle();
159 mController.onSaveInstanceState(outState);
160 finish();
161 getApplicationContext().startActivity(
162 new Intent(getApplicationContext(), BrowserActivity.class)
163 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
164 .putExtra(EXTRA_STATE, outState));
165 return;
166 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700167 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800168 }
169
Grace Kloba22ac16e2009-10-07 18:00:23 -0700170 @Override
171 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800172 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700173 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800174 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
175 }
Michael Kolb06f03e42010-12-06 10:18:26 -0800176 if (mController != null) {
177 mController.onResume();
178 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800179 }
180
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400181 @Override
182 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400183 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700184 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400185 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400186 return true;
187 }
188
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400189 @Override
190 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700191 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400192 }
193
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400194 @Override
195 public void onContextMenuClosed(Menu menu) {
196 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400198 }
199
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400200 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800201 * onSaveInstanceState(Bundle map)
202 * onSaveInstanceState is called right before onStop(). The map contains
203 * the saved state.
204 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700205 @Override
206 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700207 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800208 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
209 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 mController.onSaveInstanceState(outState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800211 }
212
Grace Kloba22ac16e2009-10-07 18:00:23 -0700213 @Override
214 protected void onPause() {
Michael Kolb06f03e42010-12-06 10:18:26 -0800215 if (mController != null) {
216 mController.onPause();
217 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800218 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800219 }
220
Grace Kloba22ac16e2009-10-07 18:00:23 -0700221 @Override
222 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700223 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800224 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
225 }
226 super.onDestroy();
Michael Kolb06f03e42010-12-06 10:18:26 -0800227 if (mController != null) {
228 mController.onDestroy();
229 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 mUi = null;
231 mController = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800232 }
233
234 @Override
235 public void onConfigurationChanged(Configuration newConfig) {
236 super.onConfigurationChanged(newConfig);
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 mController.onConfgurationChanged(newConfig);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800238 }
239
Grace Kloba22ac16e2009-10-07 18:00:23 -0700240 @Override
241 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800242 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700243 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700244 }
245
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246 @Override
247 public boolean onCreateOptionsMenu(Menu menu) {
248 super.onCreateOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700249 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800250 }
251
252 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700253 public boolean onPrepareOptionsMenu(Menu menu) {
254 super.onPrepareOptionsMenu(menu);
255 return mController.prepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400256 }
257
The Android Open Source Project0c908882009-03-03 19:32:16 -0800258 @Override
259 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 if (!mController.onOptionsItemSelected(item)) {
261 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
284 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700285 return mController.onKeyUp(keyCode, event) ||
286 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800287 }
288
Michael Kolbe421c242010-10-04 19:29:01 -0700289 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700290 public void onActionModeStarted(ActionMode mode) {
291 super.onActionModeStarted(mode);
292 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700293 }
294
Michael Kolbe421c242010-10-04 19:29:01 -0700295 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700296 public void onActionModeFinished(ActionMode mode) {
297 super.onActionModeFinished(mode);
298 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700299 }
300
The Android Open Source Project0c908882009-03-03 19:32:16 -0800301 @Override
302 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700303 Intent intent) {
304 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100305 }
306
Andrei Popescu56199cc2010-01-12 22:39:16 +0000307
The Android Open Source Project0c908882009-03-03 19:32:16 -0800308}