1acea2070cce47fa8668700bc9544230d5a64730
[platform/framework/web/crosswalk.git] / src / content / shell / android / shell_apk / src / org / chromium / content_shell_apk / ContentShellActivity.java
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.
4
5 package org.chromium.content_shell_apk;
6
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;
14
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;
28
29 /**
30  * Activity for managing the Content Shell.
31  */
32 public class ContentShellActivity extends Activity {
33
34     public static final String COMMAND_LINE_FILE = "/data/local/tmp/content-shell-command-line";
35     private static final String TAG = "ContentShellActivity";
36
37     private static final String ACTIVE_SHELL_URL_KEY = "activeUrl";
38     public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
39
40     private ShellManager mShellManager;
41     private WindowAndroid mWindowAndroid;
42
43     @Override
44     protected void onCreate(final Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46
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);
53             }
54         }
55         waitForDebuggerIfNeeded();
56
57         DeviceUtils.addDeviceSpecificUserAgentSwitch(this);
58         try {
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
64             System.exit(-1);
65             return;
66         }
67
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);
73
74         String startupUrl = getUrlFromIntent(getIntent());
75         if (!TextUtils.isEmpty(startupUrl)) {
76             mShellManager.setStartupUrl(Shell.sanitizeUrl(startupUrl));
77         }
78
79         if (CommandLine.getInstance().hasSwitch(ContentSwitches.DUMP_RENDER_TREE)) {
80             try {
81                 BrowserStartupController.get(this).startBrowserProcessesSync(false);
82             } catch (ProcessInitException e) {
83                 Log.e(TAG, "Failed to load native library.", e);
84                 System.exit(-1);
85             }
86         } else {
87             try {
88                 BrowserStartupController.get(this).startBrowserProcessesAsync(
89                         new BrowserStartupController.StartupCallback() {
90                             @Override
91                             public void onSuccess(boolean alreadyStarted) {
92                                 finishInitialization(savedInstanceState);
93                             }
94
95                             @Override
96                             public void onFailure() {
97                                 initializationFailed();
98                             }
99                         });
100             } catch (ProcessInitException e) {
101                 Log.e(TAG, "Unable to load native library.", e);
102                 System.exit(-1);
103             }
104         }
105     }
106
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);
112         }
113         mShellManager.launchShell(shellUrl);
114     }
115
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();
121         finish();
122     }
123
124     @Override
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.getWebContents().getUrl());
130         }
131
132         mWindowAndroid.saveInstanceState(outState);
133     }
134
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.");
140         }
141     }
142
143     @Override
144     public boolean onKeyUp(int keyCode, KeyEvent event) {
145         if (keyCode == KeyEvent.KEYCODE_BACK) {
146             ContentViewCore contentViewCore = getActiveContentViewCore();
147             if (contentViewCore != null && contentViewCore.getWebContents()
148                     .getNavigationController().canGoBack()) {
149                 contentViewCore.getWebContents().getNavigationController().goBack();
150                 return true;
151             }
152         }
153
154         return super.onKeyUp(keyCode, event);
155     }
156
157     @Override
158     protected void onNewIntent(Intent intent) {
159         if (getCommandLineParamsFromIntent(intent) != null) {
160             Log.i(TAG, "Ignoring command line params: can only be set when creating the activity.");
161         }
162
163         if (MemoryPressureListener.handleDebugIntent(this, intent.getAction())) return;
164
165         String url = getUrlFromIntent(intent);
166         if (!TextUtils.isEmpty(url)) {
167             Shell activeView = getActiveShell();
168             if (activeView != null) {
169                 activeView.loadUrl(url);
170             }
171         }
172     }
173
174     @Override
175     protected void onStop() {
176         super.onStop();
177
178         ContentViewCore contentViewCore = getActiveContentViewCore();
179         if (contentViewCore != null) contentViewCore.onHide();
180     }
181
182     @Override
183     protected void onStart() {
184         super.onStart();
185
186         ContentViewCore contentViewCore = getActiveContentViewCore();
187         if (contentViewCore != null) contentViewCore.onShow();
188     }
189
190     @Override
191     public void onActivityResult(int requestCode, int resultCode, Intent data) {
192         super.onActivityResult(requestCode, resultCode, data);
193         mWindowAndroid.onActivityResult(requestCode, resultCode, data);
194     }
195
196     private static String getUrlFromIntent(Intent intent) {
197         return intent != null ? intent.getDataString() : null;
198     }
199
200     private static String[] getCommandLineParamsFromIntent(Intent intent) {
201         return intent != null ? intent.getStringArrayExtra(COMMAND_LINE_ARGS_KEY) : null;
202     }
203
204     /**
205      * @return The {@link ShellManager} configured for the activity or null if it has not been
206      *         created yet.
207      */
208     public ShellManager getShellManager() {
209         return mShellManager;
210     }
211
212     /**
213      * @return The currently visible {@link Shell} or null if one is not showing.
214      */
215     public Shell getActiveShell() {
216         return mShellManager != null ? mShellManager.getActiveShell() : null;
217     }
218
219     /**
220      * @return The {@link ContentViewCore} owned by the currently visible {@link Shell} or null if
221      *         one is not showing.
222      */
223     public ContentViewCore getActiveContentViewCore() {
224         Shell shell = getActiveShell();
225         return shell != null ? shell.getContentViewCore() : null;
226     }
227 }