OLD | NEW |
| (Empty) |
1 // Copyright 2014 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.chrome.shell; | |
6 | |
7 import android.app.Activity; | |
8 import android.content.Intent; | |
9 import android.content.res.TypedArray; | |
10 import android.os.Bundle; | |
11 import android.text.TextUtils; | |
12 import android.util.Log; | |
13 import android.view.KeyEvent; | |
14 import android.view.Menu; | |
15 import android.view.MenuItem; | |
16 import android.widget.Toast; | |
17 | |
18 import com.google.common.annotations.VisibleForTesting; | |
19 | |
20 import org.chromium.base.ApiCompatibilityUtils; | |
21 import org.chromium.base.BaseSwitches; | |
22 import org.chromium.base.CommandLine; | |
23 import org.chromium.base.MemoryPressureListener; | |
24 import org.chromium.base.library_loader.ProcessInitException; | |
25 import org.chromium.chrome.browser.DevToolsServer; | |
26 import org.chromium.chrome.browser.appmenu.AppMenuHandler; | |
27 import org.chromium.chrome.browser.appmenu.AppMenuPropertiesDelegate; | |
28 import org.chromium.chrome.browser.printing.PrintingControllerFactory; | |
29 import org.chromium.chrome.browser.printing.TabPrinter; | |
30 import org.chromium.chrome.shell.sync.SyncController; | |
31 import org.chromium.content.browser.ActivityContentVideoViewClient; | |
32 import org.chromium.content.browser.BrowserStartupController; | |
33 import org.chromium.content.browser.ContentView; | |
34 import org.chromium.content.browser.DeviceUtils; | |
35 import org.chromium.printing.PrintingController; | |
36 import org.chromium.sync.signin.ChromeSigninController; | |
37 import org.chromium.ui.base.ActivityWindowAndroid; | |
38 import org.chromium.ui.base.WindowAndroid; | |
39 | |
40 /** | |
41 * The {@link android.app.Activity} component of a basic test shell to test Chro
me features. | |
42 */ | |
43 public class ChromeShellActivity extends Activity implements AppMenuPropertiesDe
legate { | |
44 private static final String TAG = "ChromeShellActivity"; | |
45 | |
46 private WindowAndroid mWindow; | |
47 private TabManager mTabManager; | |
48 private DevToolsServer mDevToolsServer; | |
49 private SyncController mSyncController; | |
50 private PrintingController mPrintingController; | |
51 | |
52 private AppMenuHandler mAppMenuHandler; | |
53 | |
54 @Override | |
55 protected void onCreate(final Bundle savedInstanceState) { | |
56 super.onCreate(savedInstanceState); | |
57 | |
58 ChromeShellApplication.initCommandLine(); | |
59 waitForDebuggerIfNeeded(); | |
60 | |
61 DeviceUtils.addDeviceSpecificUserAgentSwitch(this); | |
62 | |
63 BrowserStartupController.StartupCallback callback = | |
64 new BrowserStartupController.StartupCallback() { | |
65 @Override | |
66 public void onSuccess(boolean alreadyStarted) { | |
67 finishInitialization(savedInstanceState); | |
68 } | |
69 | |
70 @Override | |
71 public void onFailure() { | |
72 Toast.makeText(ChromeShellActivity.this, | |
73 R.string.browser_process_initialization_f
ailed, | |
74 Toast.LENGTH_SHORT).show(); | |
75 Log.e(TAG, "Chromium browser process initialization fail
ed"); | |
76 finish(); | |
77 } | |
78 }; | |
79 try { | |
80 BrowserStartupController.get(this).startBrowserProcessesAsync(callba
ck); | |
81 } | |
82 catch (ProcessInitException e) { | |
83 Log.e(TAG, "Unable to load native library.", e); | |
84 System.exit(-1); | |
85 } | |
86 } | |
87 | |
88 private void finishInitialization(final Bundle savedInstanceState) { | |
89 setContentView(R.layout.testshell_activity); | |
90 mTabManager = (TabManager) findViewById(R.id.tab_manager); | |
91 | |
92 mWindow = new ActivityWindowAndroid(this); | |
93 mWindow.restoreInstanceState(savedInstanceState); | |
94 mTabManager.initialize(mWindow, new ActivityContentVideoViewClient(this)
); | |
95 | |
96 String startupUrl = getUrlFromIntent(getIntent()); | |
97 if (!TextUtils.isEmpty(startupUrl)) { | |
98 mTabManager.setStartupUrl(startupUrl); | |
99 } | |
100 TestShellToolbar mToolbar = (TestShellToolbar) findViewById(R.id.toolbar
); | |
101 mAppMenuHandler = new AppMenuHandler(this, this, R.menu.main_menu); | |
102 mToolbar.setMenuHandler(mAppMenuHandler); | |
103 | |
104 mDevToolsServer = new DevToolsServer("chromium_testshell"); | |
105 mDevToolsServer.setRemoteDebuggingEnabled(true); | |
106 | |
107 mPrintingController = PrintingControllerFactory.create(this); | |
108 | |
109 mSyncController = SyncController.get(this); | |
110 // In case this method is called after the first onStart(), we need to i
nform the | |
111 // SyncController that we have started. | |
112 mSyncController.onStart(); | |
113 } | |
114 | |
115 @Override | |
116 protected void onDestroy() { | |
117 super.onDestroy(); | |
118 | |
119 if (mDevToolsServer != null) mDevToolsServer.destroy(); | |
120 mDevToolsServer = null; | |
121 } | |
122 | |
123 @Override | |
124 protected void onSaveInstanceState(Bundle outState) { | |
125 // TODO(dtrainor): Save/restore the tab state. | |
126 if (mWindow != null) mWindow.saveInstanceState(outState); | |
127 } | |
128 | |
129 @Override | |
130 public boolean onKeyUp(int keyCode, KeyEvent event) { | |
131 if (keyCode == KeyEvent.KEYCODE_BACK) { | |
132 TestShellTab tab = getActiveTab(); | |
133 if (tab != null && tab.getContentView().canGoBack()) { | |
134 tab.getContentView().goBack(); | |
135 return true; | |
136 } | |
137 } | |
138 | |
139 return super.onKeyUp(keyCode, event); | |
140 } | |
141 | |
142 @Override | |
143 protected void onNewIntent(Intent intent) { | |
144 if (MemoryPressureListener.handleDebugIntent(this, intent.getAction()))
return; | |
145 | |
146 String url = getUrlFromIntent(intent); | |
147 if (!TextUtils.isEmpty(url)) { | |
148 TestShellTab tab = getActiveTab(); | |
149 if (tab != null) tab.loadUrlWithSanitization(url); | |
150 } | |
151 } | |
152 | |
153 @Override | |
154 protected void onStop() { | |
155 super.onStop(); | |
156 | |
157 ContentView view = getActiveContentView(); | |
158 if (view != null) view.onHide(); | |
159 } | |
160 | |
161 @Override | |
162 protected void onStart() { | |
163 super.onStart(); | |
164 | |
165 ContentView view = getActiveContentView(); | |
166 if (view != null) view.onShow(); | |
167 | |
168 if (mSyncController != null) { | |
169 mSyncController.onStart(); | |
170 } | |
171 } | |
172 | |
173 @Override | |
174 public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
175 mWindow.onActivityResult(requestCode, resultCode, data); | |
176 } | |
177 | |
178 /** | |
179 * @return The {@link WindowAndroid} associated with this activity. | |
180 */ | |
181 public WindowAndroid getWindowAndroid() { | |
182 return mWindow; | |
183 } | |
184 | |
185 /** | |
186 * @return The {@link TestShellTab} that is currently visible. | |
187 */ | |
188 public TestShellTab getActiveTab() { | |
189 return mTabManager != null ? mTabManager.getCurrentTab() : null; | |
190 } | |
191 | |
192 /** | |
193 * @return The ContentView of the active tab. | |
194 */ | |
195 public ContentView getActiveContentView() { | |
196 TestShellTab tab = getActiveTab(); | |
197 return tab != null ? tab.getContentView() : null; | |
198 } | |
199 | |
200 /** | |
201 * Creates a {@link TestShellTab} with a URL specified by {@code url}. | |
202 * | |
203 * @param url The URL the new {@link TestShellTab} should start with. | |
204 */ | |
205 @VisibleForTesting | |
206 public void createTab(String url) { | |
207 mTabManager.createTab(url); | |
208 } | |
209 | |
210 /** | |
211 * Override the menu key event to show AppMenu. | |
212 */ | |
213 @Override | |
214 public boolean onKeyDown(int keyCode, KeyEvent event) { | |
215 if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) { | |
216 mAppMenuHandler.showAppMenu(findViewById(R.id.menu_button), true, fa
lse); | |
217 return true; | |
218 } | |
219 return super.onKeyDown(keyCode, event); | |
220 } | |
221 | |
222 @Override | |
223 public boolean onOptionsItemSelected(MenuItem item) { | |
224 switch (item.getItemId()) { | |
225 case R.id.signin: | |
226 if (ChromeSigninController.get(this).isSignedIn()) | |
227 SyncController.openSignOutDialog(getFragmentManager()); | |
228 else | |
229 SyncController.openSigninDialog(getFragmentManager()); | |
230 return true; | |
231 case R.id.print: | |
232 if (getActiveTab() != null) { | |
233 mPrintingController.startPrint(new TabPrinter(getActiveTab()
)); | |
234 } | |
235 return true; | |
236 case R.id.back_menu_id: | |
237 if (getActiveTab().canGoBack()) getActiveTab().goBack(); | |
238 return true; | |
239 case R.id.forward_menu_id: | |
240 if (getActiveTab().canGoForward()) getActiveTab().goForward(); | |
241 return true; | |
242 default: | |
243 return super.onOptionsItemSelected(item); | |
244 } | |
245 } | |
246 | |
247 private void waitForDebuggerIfNeeded() { | |
248 if (CommandLine.getInstance().hasSwitch(BaseSwitches.WAIT_FOR_JAVA_DEBUG
GER)) { | |
249 Log.e(TAG, "Waiting for Java debugger to connect..."); | |
250 android.os.Debug.waitForDebugger(); | |
251 Log.e(TAG, "Java debugger connected. Resuming execution."); | |
252 } | |
253 } | |
254 | |
255 private static String getUrlFromIntent(Intent intent) { | |
256 return intent != null ? intent.getDataString() : null; | |
257 } | |
258 | |
259 @Override | |
260 public boolean shouldShowAppMenu() { | |
261 return true; | |
262 } | |
263 | |
264 @Override | |
265 public void prepareMenu(Menu menu) { | |
266 // Disable the "Back" menu item if there is no page to go to. | |
267 MenuItem backMenuItem = menu.findItem(R.id.back_menu_id); | |
268 backMenuItem.setEnabled(getActiveTab().canGoBack()); | |
269 | |
270 // Disable the "Forward" menu item if there is no page to go to. | |
271 MenuItem forwardMenuItem = menu.findItem(R.id.forward_menu_id); | |
272 forwardMenuItem.setEnabled(getActiveTab().canGoForward()); | |
273 | |
274 // ChromeShell does not know about bookmarks yet | |
275 menu.findItem(R.id.bookmark_this_page_id).setEnabled(false); | |
276 | |
277 MenuItem signinItem = menu.findItem(R.id.signin); | |
278 if (ChromeSigninController.get(this).isSignedIn()) { | |
279 signinItem.setTitle(ChromeSigninController.get(this).getSignedInAcco
untName()); | |
280 } else { | |
281 signinItem.setTitle(R.string.signin_sign_in); | |
282 } | |
283 | |
284 menu.findItem(R.id.print).setVisible(ApiCompatibilityUtils.isPrintingSup
ported()); | |
285 | |
286 menu.setGroupVisible(R.id.MAIN_MENU, true); | |
287 } | |
288 | |
289 @Override | |
290 public boolean shouldShowIconRow() { | |
291 return true; | |
292 } | |
293 | |
294 @Override | |
295 public int getMenuThemeResourceId() { | |
296 return android.R.style.Theme_Holo_Light; | |
297 } | |
298 | |
299 @Override | |
300 public int getItemRowHeight() { | |
301 TypedArray a = obtainStyledAttributes( | |
302 new int[] {android.R.attr.listPreferredItemHeightSmall}); | |
303 int itemRowHeight = a.getDimensionPixelSize(0, 0); | |
304 a.recycle(); | |
305 return itemRowHeight; | |
306 } | |
307 } | |
OLD | NEW |