blob: 23aeed5e753e0b967c4b12075e09bd386abb978d [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
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.app.Activity;
John Reck9d27ff52011-02-11 17:47:54 -080020import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.content.Intent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.content.res.Configuration;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.os.Bundle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.util.Log;
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -040025import android.view.ActionMode;
The Android Open Source Project0c908882009-03-03 19:32:16 -080026import android.view.ContextMenu;
Michael Kolba2b2ba82010-08-04 17:54:03 -070027import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.view.KeyEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.view.Menu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.view.MenuItem;
Michael Kolbc3af0672011-08-09 10:24:41 -070031import android.view.MotionEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.view.View;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.view.Window;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034
John Reckd3e4d5b2011-07-13 15:48:43 -070035import com.google.common.annotations.VisibleForTesting;
36
Michael Kolb8233fac2010-10-26 16:08:53 -070037public class BrowserActivity extends Activity {
The Android Open Source Project0c908882009-03-03 19:32:16 -080038
John Reck439c9a52010-12-14 10:04:39 -080039 public static final String ACTION_SHOW_BOOKMARKS = "show_bookmarks";
40 public static final String ACTION_SHOW_BROWSER = "show_browser";
John Reck63bb6872010-12-01 19:29:32 -080041 public static final String ACTION_RESTART = "--restart--";
42 private static final String EXTRA_STATE = "state";
43
Michael Kolb8233fac2010-10-26 16:08:53 -070044 private final static String LOGTAG = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -080045
John Reck6c2e2f32011-08-22 13:41:23 -070046 private final static boolean LOGV_ENABLED = Browser.LOGV_ENABLED;
Dave Bort31a6d1c2009-04-13 15:56:49 -070047
Michael Kolb8233fac2010-10-26 16:08:53 -070048 private Controller mController;
49 private UI mUi;
Jeff Davidson43610292010-07-16 16:03:58 -070050
Grace Kloba22ac16e2009-10-07 18:00:23 -070051 @Override
52 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -070053 if (LOGV_ENABLED) {
John Reck6c2e2f32011-08-22 13:41:23 -070054 Log.v(LOGTAG, this + " onStart, has state: "
55 + (icicle == null ? "false" : "true"));
The Android Open Source Project0c908882009-03-03 19:32:16 -080056 }
57 super.onCreate(icicle);
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050058
John Reckf57c0292011-07-21 18:15:39 -070059 // If this was a web search request, pass it on to the default web
60 // search provider and finish this activity.
61 if (IntentHandler.handleWebSearchIntent(this, null, getIntent())) {
62 finish();
63 return;
64 }
John Reckbc490d22011-07-22 15:04:59 -070065 mController = new Controller(this, icicle == null);
John Recka5176f32011-05-17 12:35:37 -070066 boolean xlarge = isTablet(this);
Michael Kolb66706532010-12-12 19:50:22 -080067 if (xlarge) {
68 mUi = new XLargeUi(this, mController);
69 } else {
70 mUi = new PhoneUi(this, mController);
71 }
Michael Kolb8233fac2010-10-26 16:08:53 -070072 mController.setUi(mUi);
The Android Open Source Project0c908882009-03-03 19:32:16 -080073
John Reck63bb6872010-12-01 19:29:32 -080074 Bundle state = getIntent().getBundleExtra(EXTRA_STATE);
75 if (state != null && icicle == null) {
76 icicle = state;
77 }
Patrick Scott539e2ec2011-01-13 11:27:38 -050078
Patrick Scott7d50a932011-02-04 09:27:26 -050079 mController.start(icicle, getIntent());
The Android Open Source Project0c908882009-03-03 19:32:16 -080080 }
81
John Recka5176f32011-05-17 12:35:37 -070082 public static boolean isTablet(Context context) {
83 return context.getResources().getBoolean(R.bool.isTablet);
John Reck9d27ff52011-02-11 17:47:54 -080084 }
85
John Reck41554852010-12-01 12:53:37 -080086 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -070087 Controller getController() {
88 return mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -070089 }
90
The Android Open Source Project0c908882009-03-03 19:32:16 -080091 @Override
92 protected void onNewIntent(Intent intent) {
John Reck63bb6872010-12-01 19:29:32 -080093 if (ACTION_RESTART.equals(intent.getAction())) {
94 Bundle outState = new Bundle();
John Reck1cf4b792011-07-26 10:22:22 -070095 mController.onSaveInstanceState(outState);
John Reck63bb6872010-12-01 19:29:32 -080096 finish();
97 getApplicationContext().startActivity(
98 new Intent(getApplicationContext(), BrowserActivity.class)
99 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
100 .putExtra(EXTRA_STATE, outState));
101 return;
102 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700103 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800104 }
105
Grace Kloba22ac16e2009-10-07 18:00:23 -0700106 @Override
107 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800108 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700109 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
111 }
Michael Kolb06f03e42010-12-06 10:18:26 -0800112 if (mController != null) {
113 mController.onResume();
114 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115 }
116
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400117 @Override
118 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400119 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700120 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400121 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400122 return true;
123 }
124
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400125 @Override
126 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700127 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400128 }
129
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400130 @Override
131 public void onContextMenuClosed(Menu menu) {
132 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700133 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400134 }
135
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400136 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137 * onSaveInstanceState(Bundle map)
138 * onSaveInstanceState is called right before onStop(). The map contains
139 * the saved state.
140 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700141 @Override
142 protected void onSaveInstanceState(Bundle outState) {
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.onSaveInstanceState: this=" + this);
145 }
John Reck1cf4b792011-07-26 10:22:22 -0700146 mController.onSaveInstanceState(outState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800147 }
148
Grace Kloba22ac16e2009-10-07 18:00:23 -0700149 @Override
150 protected void onPause() {
Michael Kolb06f03e42010-12-06 10:18:26 -0800151 if (mController != null) {
152 mController.onPause();
153 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800155 }
156
Grace Kloba22ac16e2009-10-07 18:00:23 -0700157 @Override
158 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700159 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800160 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
161 }
162 super.onDestroy();
Michael Kolb06f03e42010-12-06 10:18:26 -0800163 if (mController != null) {
164 mController.onDestroy();
165 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700166 mUi = null;
167 mController = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800168 }
169
170 @Override
171 public void onConfigurationChanged(Configuration newConfig) {
172 super.onConfigurationChanged(newConfig);
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 mController.onConfgurationChanged(newConfig);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800174 }
175
Grace Kloba22ac16e2009-10-07 18:00:23 -0700176 @Override
177 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700179 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700180 }
181
The Android Open Source Project0c908882009-03-03 19:32:16 -0800182 @Override
183 public boolean onCreateOptionsMenu(Menu menu) {
184 super.onCreateOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 }
187
188 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 public boolean onPrepareOptionsMenu(Menu menu) {
190 super.onPrepareOptionsMenu(menu);
John Reckb3417f02011-01-14 11:01:05 -0800191 return mController.onPrepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400192 }
193
The Android Open Source Project0c908882009-03-03 19:32:16 -0800194 @Override
195 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700196 if (!mController.onOptionsItemSelected(item)) {
197 return super.onOptionsItemSelected(item);
Michael Kolb370a4f32010-10-06 10:45:32 -0700198 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800199 return true;
200 }
201
202 @Override
203 public void onCreateContextMenu(ContextMenu menu, View v,
204 ContextMenuInfo menuInfo) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700205 mController.onCreateContextMenu(menu, v, menuInfo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206 }
207
Michael Kolb8233fac2010-10-26 16:08:53 -0700208 @Override
209 public boolean onContextItemSelected(MenuItem item) {
210 return mController.onContextItemSelected(item);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700211 }
212
Grace Kloba5942df02009-09-18 11:48:29 -0700213 @Override
214 public boolean onKeyDown(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 return mController.onKeyDown(keyCode, event) ||
216 super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217 }
218
Grace Kloba5942df02009-09-18 11:48:29 -0700219 @Override
John Recke6bf4ab2011-02-24 15:48:05 -0800220 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
221 return mController.onKeyLongPress(keyCode, event) ||
222 super.onKeyLongPress(keyCode, event);
223 }
224
225 @Override
Grace Kloba5942df02009-09-18 11:48:29 -0700226 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700227 return mController.onKeyUp(keyCode, event) ||
228 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800229 }
230
Michael Kolbe421c242010-10-04 19:29:01 -0700231 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700232 public void onActionModeStarted(ActionMode mode) {
233 super.onActionModeStarted(mode);
234 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700235 }
236
Michael Kolbe421c242010-10-04 19:29:01 -0700237 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700238 public void onActionModeFinished(ActionMode mode) {
239 super.onActionModeFinished(mode);
240 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700241 }
242
The Android Open Source Project0c908882009-03-03 19:32:16 -0800243 @Override
244 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700245 Intent intent) {
246 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100247 }
248
Michael Kolbfbc579a2011-07-07 15:59:33 -0700249 @Override
250 public boolean onSearchRequested() {
251 return mController.onSearchRequested();
252 }
253
Michael Kolbc3af0672011-08-09 10:24:41 -0700254 @Override
255 public boolean dispatchKeyEvent(KeyEvent event) {
256 return mController.dispatchKeyEvent(event)
257 || super.dispatchKeyEvent(event);
258 }
259
260 @Override
261 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
262 return mController.dispatchKeyShortcutEvent(event)
263 || super.dispatchKeyShortcutEvent(event);
264 }
265
266 @Override
267 public boolean dispatchTouchEvent(MotionEvent ev) {
268 return mController.dispatchTouchEvent(ev)
269 || super.dispatchTouchEvent(ev);
270 }
271
272 @Override
273 public boolean dispatchTrackballEvent(MotionEvent ev) {
274 return mController.dispatchTrackballEvent(ev)
275 || super.dispatchTrackballEvent(ev);
276 }
277
278 @Override
279 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
280 return mController.dispatchGenericMotionEvent(ev) ||
281 super.dispatchGenericMotionEvent(ev);
282 }
283
The Android Open Source Project0c908882009-03-03 19:32:16 -0800284}