blob: ba5ba29fe82e864638480030c51f1f07db81b909 [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 Reck439c9a52010-12-14 10:04:39 -080042 public static final String ACTION_SHOW_BOOKMARKS = "show_bookmarks";
43 public static final String ACTION_SHOW_BROWSER = "show_browser";
John Reck63bb6872010-12-01 19:29:32 -080044 public static final String ACTION_RESTART = "--restart--";
45 private static final String EXTRA_STATE = "state";
46
Michael Kolb8233fac2010-10-26 16:08:53 -070047 private final static String LOGTAG = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -080048
Michael Kolb8233fac2010-10-26 16:08:53 -070049 private final static boolean LOGV_ENABLED =
50 com.android.browser.Browser.LOGV_ENABLED;
Dave Bort31a6d1c2009-04-13 15:56:49 -070051
Michael Kolb8233fac2010-10-26 16:08:53 -070052 private Controller mController;
53 private UI mUi;
Jeff Davidson43610292010-07-16 16:03:58 -070054
Grace Kloba22ac16e2009-10-07 18:00:23 -070055 @Override
56 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -070057 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -080058 Log.v(LOGTAG, this + " onStart");
59 }
60 super.onCreate(icicle);
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050061
Ben Murdochef671652010-11-25 17:17:58 +000062 // We load the first set of BrowserSettings from the db asynchronously
63 // but if it has not completed at this point, we have no choice but
64 // to block waiting for them to finish loading. :(
65 BrowserSettings.getInstance().waitForLoadFromDbToComplete();
66
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050067 // render the browser in OpenGL
Michael Kolb8233fac2010-10-26 16:08:53 -070068 if (BrowserSettings.getInstance().isHardwareAccelerated()) {
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050069 // Set the flag in the activity's window
70 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
71 WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
72 } else {
73 // Clear the flag in the activity's window
74 this.getWindow().setFlags(0, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
75 }
The Android Open Source Project0c908882009-03-03 19:32:16 -080076
Mike Reedd334bf52010-01-26 15:21:44 -050077 // enable this to test the browser in 32bit
78 if (false) {
79 getWindow().setFormat(PixelFormat.RGBX_8888);
80 BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
81 }
82
Michael Kolb8233fac2010-10-26 16:08:53 -070083 // If this was a web search request, pass it on to the default web
84 // search provider and finish this activity.
85 if (IntentHandler.handleWebSearchIntent(this, null, getIntent())) {
86 finish();
87 return;
88 }
89
90 if (((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
91 .isEnabled()) {
Svetoslav Ganov2b345992010-05-06 06:13:54 -070092 setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
93 } else {
94 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
95 }
The Android Open Source Project0c908882009-03-03 19:32:16 -080096
Michael Kolb8233fac2010-10-26 16:08:53 -070097 mController = new Controller(this);
Michael Kolb66706532010-12-12 19:50:22 -080098 boolean xlarge = (getResources().getConfiguration().screenLayout
99 & Configuration.SCREENLAYOUT_SIZE_MASK)
100 == Configuration.SCREENLAYOUT_SIZE_XLARGE;
101 if (xlarge) {
102 mUi = new XLargeUi(this, mController);
103 } else {
104 mUi = new PhoneUi(this, mController);
105 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700106 mController.setUi(mUi);
107 mController.setWebViewFactory((BaseUi) mUi);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800108
John Reck63bb6872010-12-01 19:29:32 -0800109 Bundle state = getIntent().getBundleExtra(EXTRA_STATE);
110 if (state != null && icicle == null) {
111 icicle = state;
112 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700113 mController.start(icicle, getIntent());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114 }
115
John Reck41554852010-12-01 12:53:37 -0800116 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -0700117 Controller getController() {
118 return mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700119 }
120
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121 @Override
122 protected void onNewIntent(Intent intent) {
John Reck63bb6872010-12-01 19:29:32 -0800123 if (ACTION_RESTART.equals(intent.getAction())) {
124 Bundle outState = new Bundle();
125 mController.onSaveInstanceState(outState);
126 finish();
127 getApplicationContext().startActivity(
128 new Intent(getApplicationContext(), BrowserActivity.class)
129 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
130 .putExtra(EXTRA_STATE, outState));
131 return;
132 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700133 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800134 }
135
Grace Kloba22ac16e2009-10-07 18:00:23 -0700136 @Override
137 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800138 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700139 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
141 }
Michael Kolb06f03e42010-12-06 10:18:26 -0800142 if (mController != null) {
143 mController.onResume();
144 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800145 }
146
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400147 @Override
148 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400149 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700150 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400151 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400152 return true;
153 }
154
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400155 @Override
156 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400158 }
159
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400160 @Override
161 public void onContextMenuClosed(Menu menu) {
162 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400164 }
165
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400166 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800167 * onSaveInstanceState(Bundle map)
168 * onSaveInstanceState is called right before onStop(). The map contains
169 * the saved state.
170 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700171 @Override
172 protected void onSaveInstanceState(Bundle outState) {
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.onSaveInstanceState: this=" + this);
175 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700176 mController.onSaveInstanceState(outState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800177 }
178
Grace Kloba22ac16e2009-10-07 18:00:23 -0700179 @Override
180 protected void onPause() {
Michael Kolb06f03e42010-12-06 10:18:26 -0800181 if (mController != null) {
182 mController.onPause();
183 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800184 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800185 }
186
Grace Kloba22ac16e2009-10-07 18:00:23 -0700187 @Override
188 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700189 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
191 }
192 super.onDestroy();
Michael Kolb06f03e42010-12-06 10:18:26 -0800193 if (mController != null) {
194 mController.onDestroy();
195 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700196 mUi = null;
197 mController = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800198 }
199
200 @Override
201 public void onConfigurationChanged(Configuration newConfig) {
202 super.onConfigurationChanged(newConfig);
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 mController.onConfgurationChanged(newConfig);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800204 }
205
Grace Kloba22ac16e2009-10-07 18:00:23 -0700206 @Override
207 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800208 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700209 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700210 }
211
The Android Open Source Project0c908882009-03-03 19:32:16 -0800212 @Override
213 public boolean onCreateOptionsMenu(Menu menu) {
214 super.onCreateOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800216 }
217
218 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700219 public boolean onPrepareOptionsMenu(Menu menu) {
220 super.onPrepareOptionsMenu(menu);
221 return mController.prepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400222 }
223
The Android Open Source Project0c908882009-03-03 19:32:16 -0800224 @Override
225 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700226 if (!mController.onOptionsItemSelected(item)) {
227 return super.onOptionsItemSelected(item);
Michael Kolb370a4f32010-10-06 10:45:32 -0700228 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800229 return true;
230 }
231
232 @Override
233 public void onCreateContextMenu(ContextMenu menu, View v,
234 ContextMenuInfo menuInfo) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700235 mController.onCreateContextMenu(menu, v, menuInfo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800236 }
237
Michael Kolb8233fac2010-10-26 16:08:53 -0700238 @Override
239 public boolean onContextItemSelected(MenuItem item) {
240 return mController.onContextItemSelected(item);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700241 }
242
Grace Kloba5942df02009-09-18 11:48:29 -0700243 @Override
244 public boolean onKeyDown(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700245 return mController.onKeyDown(keyCode, event) ||
246 super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800247 }
248
Grace Kloba5942df02009-09-18 11:48:29 -0700249 @Override
250 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700251 return mController.onKeyUp(keyCode, event) ||
252 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800253 }
254
Michael Kolbe421c242010-10-04 19:29:01 -0700255 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700256 public void onActionModeStarted(ActionMode mode) {
257 super.onActionModeStarted(mode);
258 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700259 }
260
Michael Kolbe421c242010-10-04 19:29:01 -0700261 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 public void onActionModeFinished(ActionMode mode) {
263 super.onActionModeFinished(mode);
264 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700265 }
266
The Android Open Source Project0c908882009-03-03 19:32:16 -0800267 @Override
268 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 Intent intent) {
270 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100271 }
272
Andrei Popescu56199cc2010-01-12 22:39:16 +0000273
The Android Open Source Project0c908882009-03-03 19:32:16 -0800274}