blob: 13b8b06b39034a3833d1745aa94e4708ed6c742b [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
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050065 // render the browser in OpenGL
Patrick Scott539e2ec2011-01-13 11:27:38 -050066 if (settings.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
Michael Kolb8233fac2010-10-26 16:08:53 -070075 // If this was a web search request, pass it on to the default web
76 // search provider and finish this activity.
77 if (IntentHandler.handleWebSearchIntent(this, null, getIntent())) {
78 finish();
79 return;
80 }
81
82 if (((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
83 .isEnabled()) {
Svetoslav Ganov2b345992010-05-06 06:13:54 -070084 setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
Svetoslav Ganov2b345992010-05-06 06:13:54 -070085 }
The Android Open Source Project0c908882009-03-03 19:32:16 -080086
Michael Kolb8233fac2010-10-26 16:08:53 -070087 mController = new Controller(this);
John Recka5176f32011-05-17 12:35:37 -070088 boolean xlarge = isTablet(this);
Michael Kolb66706532010-12-12 19:50:22 -080089 if (xlarge) {
90 mUi = new XLargeUi(this, mController);
91 } else {
92 mUi = new PhoneUi(this, mController);
93 }
Michael Kolb8233fac2010-10-26 16:08:53 -070094 mController.setUi(mUi);
The Android Open Source Project0c908882009-03-03 19:32:16 -080095
John Reck63bb6872010-12-01 19:29:32 -080096 Bundle state = getIntent().getBundleExtra(EXTRA_STATE);
97 if (state != null && icicle == null) {
98 icicle = state;
99 }
Patrick Scott539e2ec2011-01-13 11:27:38 -0500100
Patrick Scott7d50a932011-02-04 09:27:26 -0500101 mController.start(icicle, getIntent());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800102 }
103
John Recka5176f32011-05-17 12:35:37 -0700104 public static boolean isTablet(Context context) {
105 return context.getResources().getBoolean(R.bool.isTablet);
John Reck9d27ff52011-02-11 17:47:54 -0800106 }
107
John Reck41554852010-12-01 12:53:37 -0800108 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -0700109 Controller getController() {
110 return mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700111 }
112
The Android Open Source Project0c908882009-03-03 19:32:16 -0800113 @Override
114 protected void onNewIntent(Intent intent) {
John Reck63bb6872010-12-01 19:29:32 -0800115 if (ACTION_RESTART.equals(intent.getAction())) {
116 Bundle outState = new Bundle();
John Reckaed9c542011-05-27 16:08:53 -0700117 mController.onSaveInstanceState(outState, true);
John Reck63bb6872010-12-01 19:29:32 -0800118 finish();
119 getApplicationContext().startActivity(
120 new Intent(getApplicationContext(), BrowserActivity.class)
121 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
122 .putExtra(EXTRA_STATE, outState));
123 return;
124 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700125 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800126 }
127
Grace Kloba22ac16e2009-10-07 18:00:23 -0700128 @Override
129 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800130 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700131 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
133 }
Michael Kolb06f03e42010-12-06 10:18:26 -0800134 if (mController != null) {
135 mController.onResume();
136 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137 }
138
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400139 @Override
140 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400141 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700142 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400143 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400144 return true;
145 }
146
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400147 @Override
148 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700149 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400150 }
151
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400152 @Override
153 public void onContextMenuClosed(Menu menu) {
154 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700155 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400156 }
157
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400158 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800159 * onSaveInstanceState(Bundle map)
160 * onSaveInstanceState is called right before onStop(). The map contains
161 * the saved state.
162 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700163 @Override
164 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700165 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800166 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
167 }
John Reckaed9c542011-05-27 16:08:53 -0700168 mController.onSaveInstanceState(outState, true);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169 }
170
Grace Kloba22ac16e2009-10-07 18:00:23 -0700171 @Override
172 protected void onPause() {
Michael Kolb06f03e42010-12-06 10:18:26 -0800173 if (mController != null) {
174 mController.onPause();
175 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800176 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800177 }
178
Grace Kloba22ac16e2009-10-07 18:00:23 -0700179 @Override
180 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700181 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800182 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
183 }
184 super.onDestroy();
Michael Kolb06f03e42010-12-06 10:18:26 -0800185 if (mController != null) {
186 mController.onDestroy();
187 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700188 mUi = null;
189 mController = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 }
191
192 @Override
193 public void onConfigurationChanged(Configuration newConfig) {
194 super.onConfigurationChanged(newConfig);
Michael Kolb8233fac2010-10-26 16:08:53 -0700195 mController.onConfgurationChanged(newConfig);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196 }
197
Grace Kloba22ac16e2009-10-07 18:00:23 -0700198 @Override
199 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800200 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700201 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700202 }
203
The Android Open Source Project0c908882009-03-03 19:32:16 -0800204 @Override
205 public boolean onCreateOptionsMenu(Menu menu) {
206 super.onCreateOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800208 }
209
210 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700211 public boolean onPrepareOptionsMenu(Menu menu) {
212 super.onPrepareOptionsMenu(menu);
John Reckb3417f02011-01-14 11:01:05 -0800213 return mController.onPrepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400214 }
215
The Android Open Source Project0c908882009-03-03 19:32:16 -0800216 @Override
217 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700218 if (!mController.onOptionsItemSelected(item)) {
219 return super.onOptionsItemSelected(item);
Michael Kolb370a4f32010-10-06 10:45:32 -0700220 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800221 return true;
222 }
223
224 @Override
225 public void onCreateContextMenu(ContextMenu menu, View v,
226 ContextMenuInfo menuInfo) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700227 mController.onCreateContextMenu(menu, v, menuInfo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800228 }
229
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 @Override
231 public boolean onContextItemSelected(MenuItem item) {
232 return mController.onContextItemSelected(item);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700233 }
234
Grace Kloba5942df02009-09-18 11:48:29 -0700235 @Override
236 public boolean onKeyDown(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 return mController.onKeyDown(keyCode, event) ||
238 super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800239 }
240
Grace Kloba5942df02009-09-18 11:48:29 -0700241 @Override
John Recke6bf4ab2011-02-24 15:48:05 -0800242 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
243 return mController.onKeyLongPress(keyCode, event) ||
244 super.onKeyLongPress(keyCode, event);
245 }
246
247 @Override
Grace Kloba5942df02009-09-18 11:48:29 -0700248 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
The Android Open Source Project0c908882009-03-03 19:32:16 -0800271}