blob: a9b65fd03f93f4d33b6bb32b8ea5c6ac7dca8304 [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 Recka5176f32011-05-17 12:35:37 -070099 boolean xlarge = isTablet(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 Recka5176f32011-05-17 12:35:37 -0700116 public static boolean isTablet(Context context) {
117 return context.getResources().getBoolean(R.bool.isTablet);
John Reck9d27ff52011-02-11 17:47:54 -0800118 }
119
John Reck41554852010-12-01 12:53:37 -0800120 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -0700121 Controller getController() {
122 return mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700123 }
124
The Android Open Source Project0c908882009-03-03 19:32:16 -0800125 @Override
126 protected void onNewIntent(Intent intent) {
John Reck63bb6872010-12-01 19:29:32 -0800127 if (ACTION_RESTART.equals(intent.getAction())) {
128 Bundle outState = new Bundle();
129 mController.onSaveInstanceState(outState);
130 finish();
131 getApplicationContext().startActivity(
132 new Intent(getApplicationContext(), BrowserActivity.class)
133 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
134 .putExtra(EXTRA_STATE, outState));
135 return;
136 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700137 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800138 }
139
Grace Kloba22ac16e2009-10-07 18:00:23 -0700140 @Override
141 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800142 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700143 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
145 }
Michael Kolb06f03e42010-12-06 10:18:26 -0800146 if (mController != null) {
147 mController.onResume();
148 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800149 }
150
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400151 @Override
152 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400153 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700154 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400155 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400156 return true;
157 }
158
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400159 @Override
160 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400162 }
163
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400164 @Override
165 public void onContextMenuClosed(Menu menu) {
166 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700167 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400168 }
169
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400170 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800171 * onSaveInstanceState(Bundle map)
172 * onSaveInstanceState is called right before onStop(). The map contains
173 * the saved state.
174 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700175 @Override
176 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700177 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
179 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700180 mController.onSaveInstanceState(outState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181 }
182
Grace Kloba22ac16e2009-10-07 18:00:23 -0700183 @Override
184 protected void onPause() {
Michael Kolb06f03e42010-12-06 10:18:26 -0800185 if (mController != null) {
186 mController.onPause();
187 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800188 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189 }
190
Grace Kloba22ac16e2009-10-07 18:00:23 -0700191 @Override
192 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700193 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800194 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
195 }
196 super.onDestroy();
Michael Kolb06f03e42010-12-06 10:18:26 -0800197 if (mController != null) {
198 mController.onDestroy();
199 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700200 mUi = null;
201 mController = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800202 }
203
204 @Override
205 public void onConfigurationChanged(Configuration newConfig) {
206 super.onConfigurationChanged(newConfig);
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 mController.onConfgurationChanged(newConfig);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800208 }
209
Grace Kloba22ac16e2009-10-07 18:00:23 -0700210 @Override
211 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800212 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700213 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700214 }
215
The Android Open Source Project0c908882009-03-03 19:32:16 -0800216 @Override
217 public boolean onCreateOptionsMenu(Menu menu) {
218 super.onCreateOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700219 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800220 }
221
222 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700223 public boolean onPrepareOptionsMenu(Menu menu) {
224 super.onPrepareOptionsMenu(menu);
John Reckb3417f02011-01-14 11:01:05 -0800225 return mController.onPrepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400226 }
227
The Android Open Source Project0c908882009-03-03 19:32:16 -0800228 @Override
229 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 if (!mController.onOptionsItemSelected(item)) {
231 return super.onOptionsItemSelected(item);
Michael Kolb370a4f32010-10-06 10:45:32 -0700232 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800233 return true;
234 }
235
236 @Override
237 public void onCreateContextMenu(ContextMenu menu, View v,
238 ContextMenuInfo menuInfo) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700239 mController.onCreateContextMenu(menu, v, menuInfo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800240 }
241
Michael Kolb8233fac2010-10-26 16:08:53 -0700242 @Override
243 public boolean onContextItemSelected(MenuItem item) {
244 return mController.onContextItemSelected(item);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700245 }
246
Grace Kloba5942df02009-09-18 11:48:29 -0700247 @Override
248 public boolean onKeyDown(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700249 return mController.onKeyDown(keyCode, event) ||
250 super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800251 }
252
Grace Kloba5942df02009-09-18 11:48:29 -0700253 @Override
John Recke6bf4ab2011-02-24 15:48:05 -0800254 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
255 return mController.onKeyLongPress(keyCode, event) ||
256 super.onKeyLongPress(keyCode, event);
257 }
258
259 @Override
Grace Kloba5942df02009-09-18 11:48:29 -0700260 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700261 return mController.onKeyUp(keyCode, event) ||
262 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800263 }
264
Michael Kolbe421c242010-10-04 19:29:01 -0700265 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700266 public void onActionModeStarted(ActionMode mode) {
267 super.onActionModeStarted(mode);
268 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700269 }
270
Michael Kolbe421c242010-10-04 19:29:01 -0700271 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700272 public void onActionModeFinished(ActionMode mode) {
273 super.onActionModeFinished(mode);
274 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700275 }
276
The Android Open Source Project0c908882009-03-03 19:32:16 -0800277 @Override
278 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 Intent intent) {
280 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100281 }
282
The Android Open Source Project0c908882009-03-03 19:32:16 -0800283}