blob: c26d8506d6e9098e2c76b85a591fed50e15d8b5b [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 Reck9d27ff52011-02-11 17:47:54 -080088 boolean xlarge = isXlarge(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);
95 mController.setWebViewFactory((BaseUi) mUi);
The Android Open Source Project0c908882009-03-03 19:32:16 -080096
John Reck63bb6872010-12-01 19:29:32 -080097 Bundle state = getIntent().getBundleExtra(EXTRA_STATE);
98 if (state != null && icicle == null) {
99 icicle = state;
100 }
Patrick Scott539e2ec2011-01-13 11:27:38 -0500101
Patrick Scott7d50a932011-02-04 09:27:26 -0500102 mController.start(icicle, getIntent());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800103 }
104
John Reck9d27ff52011-02-11 17:47:54 -0800105 public static boolean isXlarge(Context context) {
106 return (context.getResources().getConfiguration().screenLayout
107 & Configuration.SCREENLAYOUT_SIZE_MASK)
108 == Configuration.SCREENLAYOUT_SIZE_XLARGE;
109 }
110
John Reck41554852010-12-01 12:53:37 -0800111 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -0700112 Controller getController() {
113 return mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700114 }
115
The Android Open Source Project0c908882009-03-03 19:32:16 -0800116 @Override
117 protected void onNewIntent(Intent intent) {
John Reck63bb6872010-12-01 19:29:32 -0800118 if (ACTION_RESTART.equals(intent.getAction())) {
119 Bundle outState = new Bundle();
120 mController.onSaveInstanceState(outState);
121 finish();
122 getApplicationContext().startActivity(
123 new Intent(getApplicationContext(), BrowserActivity.class)
124 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
125 .putExtra(EXTRA_STATE, outState));
126 return;
127 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700128 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129 }
130
Grace Kloba22ac16e2009-10-07 18:00:23 -0700131 @Override
132 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800133 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700134 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800135 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
136 }
Michael Kolb06f03e42010-12-06 10:18:26 -0800137 if (mController != null) {
138 mController.onResume();
139 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140 }
141
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400142 @Override
143 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400144 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700145 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400146 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400147 return true;
148 }
149
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400150 @Override
151 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700152 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400153 }
154
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400155 @Override
156 public void onContextMenuClosed(Menu menu) {
157 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400159 }
160
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400161 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800162 * onSaveInstanceState(Bundle map)
163 * onSaveInstanceState is called right before onStop(). The map contains
164 * the saved state.
165 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700166 @Override
167 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700168 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
170 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700171 mController.onSaveInstanceState(outState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800172 }
173
Grace Kloba22ac16e2009-10-07 18:00:23 -0700174 @Override
175 protected void onPause() {
Michael Kolb06f03e42010-12-06 10:18:26 -0800176 if (mController != null) {
177 mController.onPause();
178 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800179 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180 }
181
Grace Kloba22ac16e2009-10-07 18:00:23 -0700182 @Override
183 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700184 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800185 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
186 }
187 super.onDestroy();
Michael Kolb06f03e42010-12-06 10:18:26 -0800188 if (mController != null) {
189 mController.onDestroy();
190 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700191 mUi = null;
192 mController = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800193 }
194
195 @Override
196 public void onConfigurationChanged(Configuration newConfig) {
197 super.onConfigurationChanged(newConfig);
Michael Kolb8233fac2010-10-26 16:08:53 -0700198 mController.onConfgurationChanged(newConfig);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800199 }
200
Grace Kloba22ac16e2009-10-07 18:00:23 -0700201 @Override
202 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800203 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700205 }
206
The Android Open Source Project0c908882009-03-03 19:32:16 -0800207 @Override
208 public boolean onCreateOptionsMenu(Menu menu) {
209 super.onCreateOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800211 }
212
213 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700214 public boolean onPrepareOptionsMenu(Menu menu) {
215 super.onPrepareOptionsMenu(menu);
John Reckb3417f02011-01-14 11:01:05 -0800216 return mController.onPrepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400217 }
218
The Android Open Source Project0c908882009-03-03 19:32:16 -0800219 @Override
220 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700221 if (!mController.onOptionsItemSelected(item)) {
222 return super.onOptionsItemSelected(item);
Michael Kolb370a4f32010-10-06 10:45:32 -0700223 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800224 return true;
225 }
226
227 @Override
228 public void onCreateContextMenu(ContextMenu menu, View v,
229 ContextMenuInfo menuInfo) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 mController.onCreateContextMenu(menu, v, menuInfo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800231 }
232
Michael Kolb8233fac2010-10-26 16:08:53 -0700233 @Override
234 public boolean onContextItemSelected(MenuItem item) {
235 return mController.onContextItemSelected(item);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700236 }
237
Grace Kloba5942df02009-09-18 11:48:29 -0700238 @Override
239 public boolean onKeyDown(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 return mController.onKeyDown(keyCode, event) ||
241 super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800242 }
243
Grace Kloba5942df02009-09-18 11:48:29 -0700244 @Override
John Recke6bf4ab2011-02-24 15:48:05 -0800245 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
246 return mController.onKeyLongPress(keyCode, event) ||
247 super.onKeyLongPress(keyCode, event);
248 }
249
250 @Override
Grace Kloba5942df02009-09-18 11:48:29 -0700251 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700252 return mController.onKeyUp(keyCode, event) ||
253 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800254 }
255
Michael Kolbe421c242010-10-04 19:29:01 -0700256 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700257 public void onActionModeStarted(ActionMode mode) {
258 super.onActionModeStarted(mode);
259 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700260 }
261
Michael Kolbe421c242010-10-04 19:29:01 -0700262 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700263 public void onActionModeFinished(ActionMode mode) {
264 super.onActionModeFinished(mode);
265 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700266 }
267
The Android Open Source Project0c908882009-03-03 19:32:16 -0800268 @Override
269 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700270 Intent intent) {
271 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100272 }
273
The Android Open Source Project0c908882009-03-03 19:32:16 -0800274}