blob: 1ace9fd70188b770cd6df13f864b283ba2ae300c [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080018
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.app.Activity;
qqzhoue6ff8b42013-07-23 17:28:48 +080020import android.app.AlertDialog;
John Reck7878f222012-04-18 16:23:14 -070021import android.app.KeyguardManager;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080022import android.content.BroadcastReceiver;
John Reck9d27ff52011-02-11 17:47:54 -080023import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.content.Intent;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080025import android.content.IntentFilter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080026import android.content.res.Configuration;
The Android Open Source Project0c908882009-03-03 19:32:16 -080027import android.os.Bundle;
qqzhou8c5b0a32013-07-22 15:31:03 +080028import android.os.Handler;
John Reck7878f222012-04-18 16:23:14 -070029import android.os.PowerManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.util.Log;
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -040031import android.view.ActionMode;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.view.ContextMenu;
Michael Kolba2b2ba82010-08-04 17:54:03 -070033import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.view.KeyEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.view.Menu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.view.MenuItem;
Michael Kolbc3af0672011-08-09 10:24:41 -070037import android.view.MotionEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.view.View;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.view.Window;
qqzhou8c5b0a32013-07-22 15:31:03 +080040import android.webkit.JavascriptInterface;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041
John Reckd3e4d5b2011-07-13 15:48:43 -070042import com.google.common.annotations.VisibleForTesting;
Bijan Amirzada41242f22014-03-21 12:12:18 -070043import com.android.browser.R;
44import com.android.browser.UI.ComboViews;
45import com.android.browser.search.DefaultSearchEngine;
46import com.android.browser.search.SearchEngine;
47import com.android.browser.stub.NullController;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080048
49import org.chromium.content.browser.TracingIntentHandler;
50import org.codeaurora.swe.WebSettings;
51import org.codeaurora.swe.WebView;
John Reckd3e4d5b2011-07-13 15:48:43 -070052
Michael Kolb8233fac2010-10-26 16:08:53 -070053public class BrowserActivity extends Activity {
The Android Open Source Project0c908882009-03-03 19:32:16 -080054
John Reck439c9a52010-12-14 10:04:39 -080055 public static final String ACTION_SHOW_BOOKMARKS = "show_bookmarks";
56 public static final String ACTION_SHOW_BROWSER = "show_browser";
John Reck63bb6872010-12-01 19:29:32 -080057 public static final String ACTION_RESTART = "--restart--";
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080058 private static final String ACTION_START_TRACE =
59 "org.chromium.content_shell.action.PROFILE_START";
60 private static final String ACTION_STOP_TRACE =
61 "org.chromium.content_shell.action.PROFILE_STOP";
John Reck63bb6872010-12-01 19:29:32 -080062 private static final String EXTRA_STATE = "state";
John Reck38b39652012-06-05 09:22:59 -070063 public static final String EXTRA_DISABLE_URL_OVERRIDE = "disable_url_override";
John Reck63bb6872010-12-01 19:29:32 -080064
Michael Kolb8233fac2010-10-26 16:08:53 -070065 private final static String LOGTAG = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -080066
John Reck6c2e2f32011-08-22 13:41:23 -070067 private final static boolean LOGV_ENABLED = Browser.LOGV_ENABLED;
Dave Bort31a6d1c2009-04-13 15:56:49 -070068
John Reck9c35b9c2012-05-30 10:08:50 -070069 private ActivityController mController = NullController.INSTANCE;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080070
71
qqzhou8c5b0a32013-07-22 15:31:03 +080072 private Handler mHandler = new Handler();
Jeff Davidson43610292010-07-16 16:03:58 -070073
kaiyizbb2db872013-08-01 22:24:07 -040074 private UiController mUiController;
75 private Handler mHandlerEx = new Handler();
76 private Runnable runnable = new Runnable() {
77 @Override
78 public void run() {
79 if (mUiController != null) {
80 WebView current = mUiController.getCurrentWebView();
81 if (current != null) {
82 current.postInvalidate();
83 }
84 }
85 }
86 };
87
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080088 private BroadcastReceiver mReceiver;
89
90
Grace Kloba22ac16e2009-10-07 18:00:23 -070091 @Override
92 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -070093 if (LOGV_ENABLED) {
John Reck6c2e2f32011-08-22 13:41:23 -070094 Log.v(LOGTAG, this + " onStart, has state: "
95 + (icicle == null ? "false" : "true"));
The Android Open Source Project0c908882009-03-03 19:32:16 -080096 }
97 super.onCreate(icicle);
Derek Sollenbergerffa561e2010-11-16 14:19:01 -050098
John Reck7878f222012-04-18 16:23:14 -070099 if (shouldIgnoreIntents()) {
100 finish();
101 return;
102 }
103
John Reckf57c0292011-07-21 18:15:39 -0700104 // If this was a web search request, pass it on to the default web
105 // search provider and finish this activity.
Bijan Amirzada41242f22014-03-21 12:12:18 -0700106 /*
kaiyiza2368f12013-09-02 10:43:31 +0800107 SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
108 boolean result = IntentHandler.handleWebSearchIntent(this, null, getIntent());
109 if (result && (searchEngine instanceof DefaultSearchEngine)) {
John Reckf57c0292011-07-21 18:15:39 -0700110 finish();
111 return;
112 }
Bijan Amirzada41242f22014-03-21 12:12:18 -0700113 */
John Reck9c35b9c2012-05-30 10:08:50 -0700114 mController = createController();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115
George Mount3636d0a2011-11-21 09:08:21 -0800116 Intent intent = (icicle == null) ? getIntent() : null;
117 mController.start(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118 }
119
John Recka5176f32011-05-17 12:35:37 -0700120 public static boolean isTablet(Context context) {
121 return context.getResources().getBoolean(R.bool.isTablet);
John Reck9d27ff52011-02-11 17:47:54 -0800122 }
123
John Reck9c35b9c2012-05-30 10:08:50 -0700124 private Controller createController() {
125 Controller controller = new Controller(this);
126 boolean xlarge = isTablet(this);
127 UI ui = null;
128 if (xlarge) {
kaiyizbb2db872013-08-01 22:24:07 -0400129 XLargeUi tablet = new XLargeUi(this, controller);
130 ui = tablet;
131 mUiController = tablet.getUiController();
John Reck9c35b9c2012-05-30 10:08:50 -0700132 } else {
kaiyizbb2db872013-08-01 22:24:07 -0400133 PhoneUi phone = new PhoneUi(this, controller);
134 ui = phone;
135 mUiController = phone.getUiController();
John Reck9c35b9c2012-05-30 10:08:50 -0700136 }
137 controller.setUi(ui);
138 return controller;
139 }
140
John Reck41554852010-12-01 12:53:37 -0800141 @VisibleForTesting
Michael Kolb8233fac2010-10-26 16:08:53 -0700142 Controller getController() {
John Reck9c35b9c2012-05-30 10:08:50 -0700143 return (Controller) mController;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700144 }
145
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 @Override
147 protected void onNewIntent(Intent intent) {
John Reck7878f222012-04-18 16:23:14 -0700148 if (shouldIgnoreIntents()) return;
John Reck63bb6872010-12-01 19:29:32 -0800149 if (ACTION_RESTART.equals(intent.getAction())) {
150 Bundle outState = new Bundle();
John Reck1cf4b792011-07-26 10:22:22 -0700151 mController.onSaveInstanceState(outState);
John Reck63bb6872010-12-01 19:29:32 -0800152 finish();
153 getApplicationContext().startActivity(
154 new Intent(getApplicationContext(), BrowserActivity.class)
155 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
156 .putExtra(EXTRA_STATE, outState));
157 return;
158 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 mController.handleNewIntent(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800160 }
161
John Reck7878f222012-04-18 16:23:14 -0700162 private KeyguardManager mKeyguardManager;
163 private PowerManager mPowerManager;
164 private boolean shouldIgnoreIntents() {
165 // Only process intents if the screen is on and the device is unlocked
166 // aka, if we will be user-visible
167 if (mKeyguardManager == null) {
168 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
169 }
170 if (mPowerManager == null) {
171 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
172 }
173 boolean ignore = !mPowerManager.isScreenOn();
174 ignore |= mKeyguardManager.inKeyguardRestrictedInputMode();
175 if (LOGV_ENABLED) {
176 Log.v(LOGTAG, "ignore intents: " + ignore);
177 }
178 return ignore;
179 }
180
Grace Kloba22ac16e2009-10-07 18:00:23 -0700181 @Override
182 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183 super.onResume();
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.onResume: this=" + this);
186 }
John Reck9c35b9c2012-05-30 10:08:50 -0700187 mController.onResume();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800188 IntentFilter intentFilter = new IntentFilter(ACTION_START_TRACE);
189 intentFilter.addAction(ACTION_STOP_TRACE);
190 mReceiver = new BroadcastReceiver() {
191 @Override
192 public void onReceive(Context context, Intent intent) {
193 String action = intent.getAction();
194 String extra = intent.getStringExtra("file");
195 if (ACTION_START_TRACE.equals(action)) {
196 if (extra.isEmpty()) {
197 Log.e(LOGTAG, "Can not start tracing without specifing saving location");
198 } else {
199 TracingIntentHandler.beginTracing(extra);
200 Log.i(LOGTAG, "start tracing");
201 }
202 } else if (ACTION_STOP_TRACE.equals(action)) {
203 Log.i(LOGTAG, "stop tracing");
204 TracingIntentHandler.endTracing();
205 }
206 }
207 };
208 registerReceiver(mReceiver, intentFilter);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800209 }
210
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400211 @Override
212 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400213 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700214 mController.onMenuOpened(featureId, menu);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400215 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400216 return true;
217 }
218
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400219 @Override
220 public void onOptionsMenuClosed(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700221 mController.onOptionsMenuClosed(menu);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400222 }
223
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400224 @Override
225 public void onContextMenuClosed(Menu menu) {
226 super.onContextMenuClosed(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700227 mController.onContextMenuClosed(menu);
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400228 }
229
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400230 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800231 * onSaveInstanceState(Bundle map)
232 * onSaveInstanceState is called right before onStop(). The map contains
233 * the saved state.
234 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700235 @Override
236 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700237 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800238 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
239 }
John Reck9c35b9c2012-05-30 10:08:50 -0700240 mController.onSaveInstanceState(outState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800241 }
242
Grace Kloba22ac16e2009-10-07 18:00:23 -0700243 @Override
244 protected void onPause() {
John Reck9c35b9c2012-05-30 10:08:50 -0700245 mController.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246 super.onPause();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800247 }
248
Grace Kloba22ac16e2009-10-07 18:00:23 -0700249 @Override
250 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700251 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800252 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
253 }
254 super.onDestroy();
John Reck9c35b9c2012-05-30 10:08:50 -0700255 mController.onDestroy();
256 mController = NullController.INSTANCE;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800257 }
258
259 @Override
260 public void onConfigurationChanged(Configuration newConfig) {
261 super.onConfigurationChanged(newConfig);
John Reck9c35b9c2012-05-30 10:08:50 -0700262 mController.onConfgurationChanged(newConfig);
kaiyizbb2db872013-08-01 22:24:07 -0400263
264 //For avoiding bug CR520353 temporarily, delay 300ms to refresh WebView.
265 mHandlerEx.postDelayed(runnable, 300);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800266 }
267
Grace Kloba22ac16e2009-10-07 18:00:23 -0700268 @Override
269 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 super.onLowMemory();
Michael Kolb8233fac2010-10-26 16:08:53 -0700271 mController.onLowMemory();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700272 }
273
The Android Open Source Project0c908882009-03-03 19:32:16 -0800274 @Override
275 public boolean onCreateOptionsMenu(Menu menu) {
276 super.onCreateOptionsMenu(menu);
John Reck9c35b9c2012-05-30 10:08:50 -0700277 return mController.onCreateOptionsMenu(menu);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800278 }
279
280 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 public boolean onPrepareOptionsMenu(Menu menu) {
282 super.onPrepareOptionsMenu(menu);
John Reckb3417f02011-01-14 11:01:05 -0800283 return mController.onPrepareOptionsMenu(menu);
Cary Clark01cfcdd2010-06-04 16:36:45 -0400284 }
285
The Android Open Source Project0c908882009-03-03 19:32:16 -0800286 @Override
287 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700288 if (!mController.onOptionsItemSelected(item)) {
qqzhoue6ff8b42013-07-23 17:28:48 +0800289 if (item.getItemId() == R.id.about_menu_id) {
290 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
291 builder.setTitle(R.string.about);
292 builder.setCancelable(true);
293 String ua = "";
294 final WebView current = getController().getCurrentWebView();
295 if (current != null) {
296 final WebSettings s = current.getSettings();
297 if (s != null) {
298 ua = s.getUserAgentString();
299 }
300 }
301 builder.setMessage("Agent:" + ua);
302 builder.setPositiveButton(android.R.string.ok, null);
303 builder.create().show();
304 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700305 return super.onOptionsItemSelected(item);
Michael Kolb370a4f32010-10-06 10:45:32 -0700306 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800307 return true;
308 }
309
310 @Override
311 public void onCreateContextMenu(ContextMenu menu, View v,
312 ContextMenuInfo menuInfo) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700313 mController.onCreateContextMenu(menu, v, menuInfo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800314 }
315
Michael Kolb8233fac2010-10-26 16:08:53 -0700316 @Override
317 public boolean onContextItemSelected(MenuItem item) {
318 return mController.onContextItemSelected(item);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700319 }
320
Grace Kloba5942df02009-09-18 11:48:29 -0700321 @Override
322 public boolean onKeyDown(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700323 return mController.onKeyDown(keyCode, event) ||
324 super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800325 }
326
Grace Kloba5942df02009-09-18 11:48:29 -0700327 @Override
John Recke6bf4ab2011-02-24 15:48:05 -0800328 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
329 return mController.onKeyLongPress(keyCode, event) ||
330 super.onKeyLongPress(keyCode, event);
331 }
332
333 @Override
Grace Kloba5942df02009-09-18 11:48:29 -0700334 public boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 return mController.onKeyUp(keyCode, event) ||
336 super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800337 }
338
Michael Kolbe421c242010-10-04 19:29:01 -0700339 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700340 public void onActionModeStarted(ActionMode mode) {
341 super.onActionModeStarted(mode);
342 mController.onActionModeStarted(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700343 }
344
Michael Kolbe421c242010-10-04 19:29:01 -0700345 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700346 public void onActionModeFinished(ActionMode mode) {
347 super.onActionModeFinished(mode);
348 mController.onActionModeFinished(mode);
Michael Kolbe421c242010-10-04 19:29:01 -0700349 }
350
The Android Open Source Project0c908882009-03-03 19:32:16 -0800351 @Override
352 protected void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -0700353 Intent intent) {
354 mController.onActivityResult(requestCode, resultCode, intent);
Andrei Popescu163ab742009-10-20 17:58:23 +0100355 }
356
Michael Kolbfbc579a2011-07-07 15:59:33 -0700357 @Override
358 public boolean onSearchRequested() {
359 return mController.onSearchRequested();
360 }
361
Michael Kolbc3af0672011-08-09 10:24:41 -0700362 @Override
363 public boolean dispatchKeyEvent(KeyEvent event) {
364 return mController.dispatchKeyEvent(event)
365 || super.dispatchKeyEvent(event);
366 }
367
368 @Override
369 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
370 return mController.dispatchKeyShortcutEvent(event)
371 || super.dispatchKeyShortcutEvent(event);
372 }
373
374 @Override
375 public boolean dispatchTouchEvent(MotionEvent ev) {
376 return mController.dispatchTouchEvent(ev)
377 || super.dispatchTouchEvent(ev);
378 }
379
380 @Override
381 public boolean dispatchTrackballEvent(MotionEvent ev) {
382 return mController.dispatchTrackballEvent(ev)
383 || super.dispatchTrackballEvent(ev);
384 }
385
386 @Override
387 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
388 return mController.dispatchGenericMotionEvent(ev) ||
389 super.dispatchGenericMotionEvent(ev);
390 }
391
qqzhou8c5b0a32013-07-22 15:31:03 +0800392 // add for carrier homepage feature
393 @JavascriptInterface
394 public void loadBookmarks() {
395 mHandler.post(new Runnable() {
396 @Override
397 public void run() {
398 if (mController instanceof Controller) {
399 ((Controller)mController).bookmarksOrHistoryPicker(ComboViews.Bookmarks);
400 }
401 }
402 });
403 }
404
405 // add for carrier homepage feature
406 @JavascriptInterface
407 public void loadHistory() {
408 mHandler.post(new Runnable() {
409 @Override
410 public void run() {
411 if (mController instanceof Controller) {
412 ((Controller)mController).bookmarksOrHistoryPicker(ComboViews.History);
413 }
414 }
415 });
416 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800417}