1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.content_shell_apk;
7 import android.app.Activity;
8 import android.content.Intent;
9 import android.os.Bundle;
10 import android.text.TextUtils;
11 import android.util.Log;
12 import android.view.KeyEvent;
13 import android.widget.Toast;
15 import org.chromium.base.BaseSwitches;
16 import org.chromium.base.CommandLine;
17 import org.chromium.base.MemoryPressureListener;
18 import org.chromium.base.library_loader.LibraryLoader;
19 import org.chromium.base.library_loader.ProcessInitException;
20 import org.chromium.content.browser.BrowserStartupController;
21 import org.chromium.content.browser.ContentViewCore;
22 import org.chromium.content.browser.DeviceUtils;
23 import org.chromium.content.common.ContentSwitches;
24 import org.chromium.content_shell.Shell;
25 import org.chromium.content_shell.ShellManager;
26 import org.chromium.ui.base.ActivityWindowAndroid;
27 import org.chromium.ui.base.WindowAndroid;
30 * Activity for managing the Content Shell.
32 public class ContentShellActivity extends Activity {
34 public static final String COMMAND_LINE_FILE = "/data/local/tmp/content-shell-command-line";
35 private static final String TAG = "ContentShellActivity";
37 private static final String ACTIVE_SHELL_URL_KEY = "activeUrl";
38 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
40 private ShellManager mShellManager;
41 private WindowAndroid mWindowAndroid;
44 protected void onCreate(final Bundle savedInstanceState) {
45 super.onCreate(savedInstanceState);
47 // Initializing the command line must occur before loading the library.
48 if (!CommandLine.isInitialized()) {
49 CommandLine.initFromFile(COMMAND_LINE_FILE);
50 String[] commandLineParams = getCommandLineParamsFromIntent(getIntent());
51 if (commandLineParams != null) {
52 CommandLine.getInstance().appendSwitchesAndArguments(commandLineParams);
55 waitForDebuggerIfNeeded();
57 DeviceUtils.addDeviceSpecificUserAgentSwitch(this);
59 LibraryLoader.ensureInitialized();
60 } catch (ProcessInitException e) {
61 Log.e(TAG, "ContentView initialization failed.", e);
62 // Since the library failed to initialize nothing in the application
63 // can work, so kill the whole application not just the activity
68 setContentView(R.layout.content_shell_activity);
69 mShellManager = (ShellManager) findViewById(R.id.shell_container);
70 mWindowAndroid = new ActivityWindowAndroid(this);
71 mWindowAndroid.restoreInstanceState(savedInstanceState);
72 mShellManager.setWindow(mWindowAndroid);
74 String startupUrl = getUrlFromIntent(getIntent());
75 if (!TextUtils.isEmpty(startupUrl)) {
76 mShellManager.setStartupUrl(Shell.sanitizeUrl(startupUrl));
79 if (CommandLine.getInstance().hasSwitch(ContentSwitches.DUMP_RENDER_TREE)) {
81 BrowserStartupController.get(this).startBrowserProcessesSync(false);
82 } catch (ProcessInitException e) {
83 Log.e(TAG, "Failed to load native library.", e);
88 BrowserStartupController.get(this).startBrowserProcessesAsync(
89 new BrowserStartupController.StartupCallback() {
91 public void onSuccess(boolean alreadyStarted) {
92 finishInitialization(savedInstanceState);
96 public void onFailure() {
97 initializationFailed();
100 } catch (ProcessInitException e) {
101 Log.e(TAG, "Unable to load native library.", e);
107 private void finishInitialization(Bundle savedInstanceState) {
108 String shellUrl = ShellManager.DEFAULT_SHELL_URL;
109 if (savedInstanceState != null
110 && savedInstanceState.containsKey(ACTIVE_SHELL_URL_KEY)) {
111 shellUrl = savedInstanceState.getString(ACTIVE_SHELL_URL_KEY);
113 mShellManager.launchShell(shellUrl);
116 private void initializationFailed() {
117 Log.e(TAG, "ContentView initialization failed.");
118 Toast.makeText(ContentShellActivity.this,
119 R.string.browser_process_initialization_failed,
120 Toast.LENGTH_SHORT).show();
125 protected void onSaveInstanceState(Bundle outState) {
126 super.onSaveInstanceState(outState);
127 ContentViewCore contentViewCore = getActiveContentViewCore();
128 if (contentViewCore != null) {
129 outState.putString(ACTIVE_SHELL_URL_KEY, contentViewCore.getUrl());
132 mWindowAndroid.saveInstanceState(outState);
135 private void waitForDebuggerIfNeeded() {
136 if (CommandLine.getInstance().hasSwitch(BaseSwitches.WAIT_FOR_JAVA_DEBUGGER)) {
137 Log.e(TAG, "Waiting for Java debugger to connect...");
138 android.os.Debug.waitForDebugger();
139 Log.e(TAG, "Java debugger connected. Resuming execution.");
144 public boolean onKeyUp(int keyCode, KeyEvent event) {
145 if (keyCode == KeyEvent.KEYCODE_BACK) {
146 ContentViewCore contentViewCore = getActiveContentViewCore();
147 if (contentViewCore != null && contentViewCore.canGoBack()) {
148 contentViewCore.goBack();
153 return super.onKeyUp(keyCode, event);
157 protected void onNewIntent(Intent intent) {
158 if (getCommandLineParamsFromIntent(intent) != null) {
159 Log.i(TAG, "Ignoring command line params: can only be set when creating the activity.");
162 if (MemoryPressureListener.handleDebugIntent(this, intent.getAction())) return;
164 String url = getUrlFromIntent(intent);
165 if (!TextUtils.isEmpty(url)) {
166 Shell activeView = getActiveShell();
167 if (activeView != null) {
168 activeView.loadUrl(url);
174 protected void onStop() {
177 ContentViewCore contentViewCore = getActiveContentViewCore();
178 if (contentViewCore != null) contentViewCore.onHide();
182 protected void onStart() {
185 ContentViewCore contentViewCore = getActiveContentViewCore();
186 if (contentViewCore != null) contentViewCore.onShow();
190 public void onActivityResult(int requestCode, int resultCode, Intent data) {
191 super.onActivityResult(requestCode, resultCode, data);
192 mWindowAndroid.onActivityResult(requestCode, resultCode, data);
195 private static String getUrlFromIntent(Intent intent) {
196 return intent != null ? intent.getDataString() : null;
199 private static String[] getCommandLineParamsFromIntent(Intent intent) {
200 return intent != null ? intent.getStringArrayExtra(COMMAND_LINE_ARGS_KEY) : null;
204 * @return The {@link ShellManager} configured for the activity or null if it has not been
207 public ShellManager getShellManager() {
208 return mShellManager;
212 * @return The currently visible {@link Shell} or null if one is not showing.
214 public Shell getActiveShell() {
215 return mShellManager != null ? mShellManager.getActiveShell() : null;
219 * @return The {@link ContentViewCore} owned by the currently visible {@link Shell} or null if
220 * one is not showing.
222 public ContentViewCore getActiveContentViewCore() {
223 Shell shell = getActiveShell();
224 return shell != null ? shell.getContentViewCore() : null;