OLD | NEW |
| (Empty) |
1 // Copyright (c) 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; | |
6 | |
7 import android.app.Activity; | |
8 import android.content.BroadcastReceiver; | |
9 import android.content.Context; | |
10 import android.content.Intent; | |
11 import android.content.IntentFilter; | |
12 import android.os.Bundle; | |
13 import android.text.TextUtils; | |
14 import android.util.Log; | |
15 import android.view.KeyEvent; | |
16 | |
17 import org.chromium.base.ChromiumActivity; | |
18 import org.chromium.content.app.LibraryLoader; | |
19 import org.chromium.content.browser.ActivityContentVideoViewDelegate; | |
20 import org.chromium.content.browser.ContentVideoView; | |
21 import org.chromium.content.browser.ContentView; | |
22 import org.chromium.content.browser.DeviceUtils; | |
23 import org.chromium.content.browser.TracingIntentHandler; | |
24 import org.chromium.content.common.CommandLine; | |
25 import org.chromium.content.common.ProcessInitException; | |
26 import org.chromium.ui.gfx.ActivityNativeWindow; | |
27 | |
28 /** | |
29 * Activity for managing the Content Shell. | |
30 */ | |
31 public class ContentShellActivity extends ChromiumActivity { | |
32 | |
33 public static final String COMMAND_LINE_FILE = "/data/local/tmp/content-shel
l-command-line"; | |
34 private static final String TAG = ContentShellActivity.class.getName(); | |
35 | |
36 private static final String ACTIVE_SHELL_URL_KEY = "activeUrl"; | |
37 private static final String ACTION_START_TRACE = | |
38 "org.chromium.content_shell.action.PROFILE_START"; | |
39 private static final String ACTION_STOP_TRACE = | |
40 "org.chromium.content_shell.action.PROFILE_STOP"; | |
41 public static final String DEFAULT_SHELL_URL = "http://www.google.com"; | |
42 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs"; | |
43 | |
44 private ShellManager mShellManager; | |
45 private ActivityNativeWindow mActivityNativeWindow; | |
46 private BroadcastReceiver mReceiver; | |
47 | |
48 @Override | |
49 protected void onCreate(Bundle savedInstanceState) { | |
50 super.onCreate(savedInstanceState); | |
51 | |
52 // Initializing the command line must occur before loading the library. | |
53 if (!CommandLine.isInitialized()) { | |
54 CommandLine.initFromFile(COMMAND_LINE_FILE); | |
55 String[] commandLineParams = getCommandLineParamsFromIntent(getInten
t()); | |
56 if (commandLineParams != null) { | |
57 CommandLine.getInstance().appendSwitchesAndArguments(commandLine
Params); | |
58 } | |
59 } | |
60 waitForDebuggerIfNeeded(); | |
61 | |
62 DeviceUtils.addDeviceSpecificUserAgentSwitch(this); | |
63 try { | |
64 LibraryLoader.ensureInitialized(); | |
65 | |
66 setContentView(R.layout.content_shell_activity); | |
67 mShellManager = (ShellManager) findViewById(R.id.shell_container); | |
68 mActivityNativeWindow = new ActivityNativeWindow(this); | |
69 mActivityNativeWindow.restoreInstanceState(savedInstanceState); | |
70 mShellManager.setWindow(mActivityNativeWindow); | |
71 ContentVideoView.registerContentVideoViewContextDelegate( | |
72 new ActivityContentVideoViewDelegate(this)); | |
73 | |
74 String startupUrl = getUrlFromIntent(getIntent()); | |
75 if (!TextUtils.isEmpty(startupUrl)) { | |
76 mShellManager.setStartupUrl(Shell.sanitizeUrl(startupUrl)); | |
77 } | |
78 if (!ContentView.enableMultiProcess(this, ContentView.MAX_RENDERERS_
AUTOMATIC)) { | |
79 String shellUrl = DEFAULT_SHELL_URL; | |
80 if (savedInstanceState != null | |
81 && savedInstanceState.containsKey(ACTIVE_SHELL_URL_KEY)) { | |
82 shellUrl = savedInstanceState.getString(ACTIVE_SHELL_URL_KEY
); | |
83 } | |
84 mShellManager.launchShell(shellUrl); | |
85 } | |
86 } catch (ProcessInitException e) { | |
87 Log.e(TAG, "ContentView initialization failed.", e); | |
88 finish(); | |
89 } | |
90 } | |
91 | |
92 @Override | |
93 protected void onSaveInstanceState(Bundle outState) { | |
94 super.onSaveInstanceState(outState); | |
95 Shell activeShell = getActiveShell(); | |
96 if (activeShell != null) { | |
97 outState.putString(ACTIVE_SHELL_URL_KEY, activeShell.getContentView(
).getUrl()); | |
98 } | |
99 | |
100 mActivityNativeWindow.saveInstanceState(outState); | |
101 } | |
102 | |
103 private void waitForDebuggerIfNeeded() { | |
104 if (CommandLine.getInstance().hasSwitch(CommandLine.WAIT_FOR_JAVA_DEBUGG
ER)) { | |
105 Log.e(TAG, "Waiting for Java debugger to connect..."); | |
106 android.os.Debug.waitForDebugger(); | |
107 Log.e(TAG, "Java debugger connected. Resuming execution."); | |
108 } | |
109 } | |
110 | |
111 @Override | |
112 public boolean onKeyUp(int keyCode, KeyEvent event) { | |
113 if (keyCode != KeyEvent.KEYCODE_BACK) return super.onKeyUp(keyCode, even
t); | |
114 | |
115 Shell activeView = getActiveShell(); | |
116 if (activeView != null && activeView.getContentView().canGoBack()) { | |
117 activeView.getContentView().goBack(); | |
118 return true; | |
119 } | |
120 | |
121 return super.onKeyUp(keyCode, event); | |
122 } | |
123 | |
124 @Override | |
125 protected void onNewIntent(Intent intent) { | |
126 if (getCommandLineParamsFromIntent(intent) != null) { | |
127 Log.i(TAG, "Ignoring command line params: can only be set when creat
ing the activity."); | |
128 } | |
129 | |
130 String url = getUrlFromIntent(intent); | |
131 if (!TextUtils.isEmpty(url)) { | |
132 Shell activeView = getActiveShell(); | |
133 if (activeView != null) { | |
134 activeView.loadUrl(url); | |
135 } | |
136 } | |
137 } | |
138 | |
139 @Override | |
140 protected void onPause() { | |
141 ContentView view = getActiveContentView(); | |
142 if (view != null) view.onActivityPause(); | |
143 | |
144 super.onPause(); | |
145 unregisterReceiver(mReceiver); | |
146 } | |
147 | |
148 @Override | |
149 protected void onResume() { | |
150 super.onResume(); | |
151 | |
152 ContentView view = getActiveContentView(); | |
153 if (view != null) view.onActivityResume(); | |
154 IntentFilter intentFilter = new IntentFilter(ACTION_START_TRACE); | |
155 intentFilter.addAction(ACTION_STOP_TRACE); | |
156 mReceiver = new BroadcastReceiver() { | |
157 @Override | |
158 public void onReceive(Context context, Intent intent) { | |
159 String action = intent.getAction(); | |
160 String extra = intent.getStringExtra("file"); | |
161 if (ACTION_START_TRACE.equals(action)) { | |
162 if (extra.isEmpty()) { | |
163 Log.e(TAG, "Can not start tracing without specifing savi
ng location"); | |
164 } else { | |
165 TracingIntentHandler.beginTracing(extra); | |
166 Log.i(TAG, "start tracing"); | |
167 } | |
168 } else if (ACTION_STOP_TRACE.equals(action)) { | |
169 Log.i(TAG, "stop tracing"); | |
170 TracingIntentHandler.endTracing(); | |
171 } | |
172 } | |
173 }; | |
174 registerReceiver(mReceiver, intentFilter); | |
175 } | |
176 | |
177 @Override | |
178 public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
179 super.onActivityResult(requestCode, resultCode, data); | |
180 mActivityNativeWindow.onActivityResult(requestCode, resultCode, data); | |
181 } | |
182 | |
183 private static String getUrlFromIntent(Intent intent) { | |
184 return intent != null ? intent.getDataString() : null; | |
185 } | |
186 | |
187 private static String[] getCommandLineParamsFromIntent(Intent intent) { | |
188 return intent != null ? intent.getStringArrayExtra(COMMAND_LINE_ARGS_KEY
) : null; | |
189 } | |
190 | |
191 /** | |
192 * @return The {@link ShellManager} configured for the activity or null if i
t has not been | |
193 * created yet. | |
194 */ | |
195 public ShellManager getShellManager() { | |
196 return mShellManager; | |
197 } | |
198 | |
199 /** | |
200 * @return The currently visible {@link Shell} or null if one is not showing
. | |
201 */ | |
202 public Shell getActiveShell() { | |
203 return mShellManager != null ? mShellManager.getActiveShell() : null; | |
204 } | |
205 | |
206 /** | |
207 * @return The {@link ContentView} owned by the currently visible {@link She
ll} or null if one | |
208 * is not showing. | |
209 */ | |
210 public ContentView getActiveContentView() { | |
211 Shell shell = getActiveShell(); | |
212 return shell != null ? shell.getContentView() : null; | |
213 } | |
214 } | |
OLD | NEW |