blob: 3c025d2045a84b9bdc8fa7afc159021705acaa51 [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;
John Reck9d27ff52011-02-11 17:47:54 -080022import android.content.Context;
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);
Svetoslav Ganov2b345992010-05-06 06:13:54 -070096 }
The Android Open Source Project0c908882009-03-03 19:32:16 -080097
Michael Kolb8233fac2010-10-26 16:08:53 -070098 mController = new Controller(this);
John Reck9d27ff52011-02-11 17:47:54 -080099 boolean xlarge = isXlarge(this);
Michael Kolb66706532010-12-12 19:50:22 -0800100 if (xlarge) {
101 mUi = new XLargeUi(this, mController);
102 } else {
103 mUi = new PhoneUi(this, mController);
104 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700105 mController.setUi(mUi);
106 mController.setWebViewFactory((BaseUi) mUi);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800107
John Reck63bb6872010-12-01 19:29:32 -0800108 Bundle state = getIntent().getBundleExtra(EXTRA_STATE);
109 if (state != null && icicle == null) {
110 icicle = state;
111 }
Patrick Scott539e2ec2011-01-13 11:27:38 -0500112
Patrick Scott7d50a932011-02-04 09:27:26 -0500113 mController.start(icicle, getIntent());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114 }
115
John Reck9d27ff52011-02-11 17:47:54 -0800116 public static boolean isXlarge(Context context) {
117 return (context.getResources().getConfiguration().screenLayout
118 & Configuration.SCREENLAYOUT_SIZE_MASK)
119 == Configuration.SCREENLAYOUT_SIZE_XLARGE;
120 }
121
John Reck41554852010-12-01 12:53:37 -0800122 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -0700123 Controller getController() {
124 return mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700125 }
126
The Android Open Source Project0c908882009-03-03 19:32:16 -0800127 @Override
128 protected void onNewIntent(Intent intent) {
John Reck63bb6872010-12-01 19:29:32 -0800129 if (ACTION_RESTART.equals(intent.getAction())) {
130 Bundle outState = new Bundle();
131 mController.onSaveInstanceState(outState);
132 finish();
133 getApplicationContext().startActivity(
134 new Intent(getApplicationContext(), BrowserActivity.class)
135 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
136 .putExtra(EXTRA_STATE, outState));
137 return;
138 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700139 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140 }
141
Grace Kloba22ac16e2009-10-07 18:00:23 -0700142 @Override
143 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700145 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
147 }
Michael Kolb06f03e42010-12-06 10:18:26 -0800148 if (mController != null) {
149 mController.onResume();
150 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800151 }
152
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400153 @Override
154 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400155 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400157 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400158 return true;
159 }
160
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400161 @Override
162 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400164 }
165
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400166 @Override
167 public void onContextMenuClosed(Menu menu) {
168 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700169 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400170 }
171
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400172 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800173 * onSaveInstanceState(Bundle map)
174 * onSaveInstanceState is called right before onStop(). The map contains
175 * the saved state.
176 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700177 @Override
178 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700179 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
181 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700182 mController.onSaveInstanceState(outState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183 }
184
Grace Kloba22ac16e2009-10-07 18:00:23 -0700185 @Override
186 protected void onPause() {
Michael Kolb06f03e42010-12-06 10:18:26 -0800187 if (mController != null) {
188 mController.onPause();
189 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800191 }
192
Grace Kloba22ac16e2009-10-07 18:00:23 -0700193 @Override
194 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700195 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
197 }
198 super.onDestroy();
Michael Kolb06f03e42010-12-06 10:18:26 -0800199 if (mController != null) {
200 mController.onDestroy();
201 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700202 mUi = null;
203 mController = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800204 }
205
206 @Override
207 public void onConfigurationChanged(Configuration newConfig) {
208 super.onConfigurationChanged(newConfig);
Michael Kolb8233fac2010-10-26 16:08:53 -0700209 mController.onConfgurationChanged(newConfig);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800210 }
211
Grace Kloba22ac16e2009-10-07 18:00:23 -0700212 @Override
213 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800214 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700216 }
217
The Android Open Source Project0c908882009-03-03 19:32:16 -0800218 @Override
219 public boolean onCreateOptionsMenu(Menu menu) {
220 super.onCreateOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700221 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800222 }
223
224 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700225 public boolean onPrepareOptionsMenu(Menu menu) {
226 super.onPrepareOptionsMenu(menu);
John Reckb3417f02011-01-14 11:01:05 -0800227 return mController.onPrepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400228 }
229
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 @Override
231 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700232 if (!mController.onOptionsItemSelected(item)) {
233 return super.onOptionsItemSelected(item);
Michael Kolb370a4f32010-10-06 10:45:32 -0700234 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800235 return true;
236 }
237
238 @Override
239 public void onCreateContextMenu(ContextMenu menu, View v,
240 ContextMenuInfo menuInfo) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700241 mController.onCreateContextMenu(menu, v, menuInfo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800242 }
243
Michael Kolb8233fac2010-10-26 16:08:53 -0700244 @Override
245 public boolean onContextItemSelected(MenuItem item) {
246 return mController.onContextItemSelected(item);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700247 }
248
Grace Kloba5942df02009-09-18 11:48:29 -0700249 @Override
250 public boolean onKeyDown(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700251 return mController.onKeyDown(keyCode, event) ||
252 super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800253 }
254
Grace Kloba5942df02009-09-18 11:48:29 -0700255 @Override
John Recke6bf4ab2011-02-24 15:48:05 -0800256 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
257 return mController.onKeyLongPress(keyCode, event) ||
258 super.onKeyLongPress(keyCode, event);
259 }
260
261 @Override
Grace Kloba5942df02009-09-18 11:48:29 -0700262 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700263 return mController.onKeyUp(keyCode, event) ||
264 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800265 }
266
Michael Kolbe421c242010-10-04 19:29:01 -0700267 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700268 public void onActionModeStarted(ActionMode mode) {
269 super.onActionModeStarted(mode);
270 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700271 }
272
Michael Kolbe421c242010-10-04 19:29:01 -0700273 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700274 public void onActionModeFinished(ActionMode mode) {
275 super.onActionModeFinished(mode);
276 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700277 }
278
The Android Open Source Project0c908882009-03-03 19:32:16 -0800279 @Override
280 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 Intent intent) {
282 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100283 }
284
The Android Open Source Project0c908882009-03-03 19:32:16 -0800285}