blob: 8282eb002c714bbc8c1a1b20a25d966513744808 [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;
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.graphics.Bitmap;
Andrei Popescu540035d2009-09-18 18:59:20 +010025import android.graphics.BitmapFactory;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040026import android.graphics.PixelFormat;
The Android Open Source Project0c908882009-03-03 19:32:16 -080027import android.os.Bundle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.util.Log;
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -040029import android.view.ActionMode;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.view.ContextMenu;
Michael Kolba2b2ba82010-08-04 17:54:03 -070031import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.view.KeyEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.view.Menu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.view.MenuItem;
35import android.view.View;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.view.Window;
37import android.view.WindowManager;
Svetoslav Ganov2b345992010-05-06 06:13:54 -070038import android.view.accessibility.AccessibilityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039
Michael Kolb8233fac2010-10-26 16:08:53 -070040public class BrowserActivity extends Activity {
The Android Open Source Project0c908882009-03-03 19:32:16 -080041
John Reck63bb6872010-12-01 19:29:32 -080042 public static final String ACTION_RESTART = "--restart--";
43 private static final String EXTRA_STATE = "state";
44
Michael Kolb8233fac2010-10-26 16:08:53 -070045 private final static String LOGTAG = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -080046
Michael Kolb8233fac2010-10-26 16:08:53 -070047 private final static boolean LOGV_ENABLED =
48 com.android.browser.Browser.LOGV_ENABLED;
Dave Bort31a6d1c2009-04-13 15:56:49 -070049
Michael Kolb8233fac2010-10-26 16:08:53 -070050 private Controller mController;
51 private UI mUi;
Jeff Davidson43610292010-07-16 16:03:58 -070052
Grace Kloba22ac16e2009-10-07 18:00:23 -070053 @Override
54 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -070055 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -080056 Log.v(LOGTAG, this + " onStart");
57 }
58 super.onCreate(icicle);
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050059
Ben Murdochef671652010-11-25 17:17:58 +000060 // We load the first set of BrowserSettings from the db asynchronously
61 // but if it has not completed at this point, we have no choice but
62 // to block waiting for them to finish loading. :(
63 BrowserSettings.getInstance().waitForLoadFromDbToComplete();
64
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050065 // render the browser in OpenGL
Michael Kolb8233fac2010-10-26 16:08:53 -070066 if (BrowserSettings.getInstance().isHardwareAccelerated()) {
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050067 // Set the flag in the activity's window
68 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
69 WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
70 } else {
71 // Clear the flag in the activity's window
72 this.getWindow().setFlags(0, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
73 }
The Android Open Source Project0c908882009-03-03 19:32:16 -080074
Mike Reedd334bf52010-01-26 15:21:44 -050075 // enable this to test the browser in 32bit
76 if (false) {
77 getWindow().setFormat(PixelFormat.RGBX_8888);
78 BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
79 }
80
Michael Kolb8233fac2010-10-26 16:08:53 -070081 // If this was a web search request, pass it on to the default web
82 // search provider and finish this activity.
83 if (IntentHandler.handleWebSearchIntent(this, null, getIntent())) {
84 finish();
85 return;
86 }
87
88 if (((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
89 .isEnabled()) {
Svetoslav Ganov2b345992010-05-06 06:13:54 -070090 setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
91 } else {
92 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
93 }
The Android Open Source Project0c908882009-03-03 19:32:16 -080094
Michael Kolb8233fac2010-10-26 16:08:53 -070095 mController = new Controller(this);
Michael Kolb66706532010-12-12 19:50:22 -080096 boolean xlarge = (getResources().getConfiguration().screenLayout
97 & Configuration.SCREENLAYOUT_SIZE_MASK)
98 == Configuration.SCREENLAYOUT_SIZE_XLARGE;
99 if (xlarge) {
100 mUi = new XLargeUi(this, mController);
101 } else {
102 mUi = new PhoneUi(this, mController);
103 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700104 mController.setUi(mUi);
105 mController.setWebViewFactory((BaseUi) mUi);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800106
John Reck63bb6872010-12-01 19:29:32 -0800107 Bundle state = getIntent().getBundleExtra(EXTRA_STATE);
108 if (state != null && icicle == null) {
109 icicle = state;
110 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700111 mController.start(icicle, getIntent());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112 }
113
John Reck41554852010-12-01 12:53:37 -0800114 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -0700115 Controller getController() {
116 return mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700117 }
118
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119 @Override
120 protected void onNewIntent(Intent intent) {
John Reck63bb6872010-12-01 19:29:32 -0800121 if (ACTION_RESTART.equals(intent.getAction())) {
122 Bundle outState = new Bundle();
123 mController.onSaveInstanceState(outState);
124 finish();
125 getApplicationContext().startActivity(
126 new Intent(getApplicationContext(), BrowserActivity.class)
127 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
128 .putExtra(EXTRA_STATE, outState));
129 return;
130 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700131 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132 }
133
Grace Kloba22ac16e2009-10-07 18:00:23 -0700134 @Override
135 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800136 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700137 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800138 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
139 }
Michael Kolb06f03e42010-12-06 10:18:26 -0800140 if (mController != null) {
141 mController.onResume();
142 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800143 }
144
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400145 @Override
146 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400147 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700148 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400149 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400150 return true;
151 }
152
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400153 @Override
154 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700155 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400156 }
157
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400158 @Override
159 public void onContextMenuClosed(Menu menu) {
160 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400162 }
163
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400164 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800165 * onSaveInstanceState(Bundle map)
166 * onSaveInstanceState is called right before onStop(). The map contains
167 * the saved state.
168 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700169 @Override
170 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700171 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800172 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
173 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700174 mController.onSaveInstanceState(outState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800175 }
176
Grace Kloba22ac16e2009-10-07 18:00:23 -0700177 @Override
178 protected void onPause() {
Michael Kolb06f03e42010-12-06 10:18:26 -0800179 if (mController != null) {
180 mController.onPause();
181 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800182 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183 }
184
Grace Kloba22ac16e2009-10-07 18:00:23 -0700185 @Override
186 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700187 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800188 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
189 }
190 super.onDestroy();
Michael Kolb06f03e42010-12-06 10:18:26 -0800191 if (mController != null) {
192 mController.onDestroy();
193 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700194 mUi = null;
195 mController = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196 }
197
198 @Override
199 public void onConfigurationChanged(Configuration newConfig) {
200 super.onConfigurationChanged(newConfig);
Michael Kolb8233fac2010-10-26 16:08:53 -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);
Michael Kolb8233fac2010-10-26 16:08:53 -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);
219 return mController.prepareOptionsMenu(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
248 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700249 return mController.onKeyUp(keyCode, event) ||
250 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800251 }
252
Michael Kolbe421c242010-10-04 19:29:01 -0700253 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700254 public void onActionModeStarted(ActionMode mode) {
255 super.onActionModeStarted(mode);
256 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700257 }
258
Michael Kolbe421c242010-10-04 19:29:01 -0700259 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 public void onActionModeFinished(ActionMode mode) {
261 super.onActionModeFinished(mode);
262 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700263 }
264
The Android Open Source Project0c908882009-03-03 19:32:16 -0800265 @Override
266 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 Intent intent) {
268 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100269 }
270
Andrei Popescu56199cc2010-01-12 22:39:16 +0000271
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272}