1 // Copyright (c) 2013 Intel Corporation. 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.xwalk.core;
7 import android.app.Activity;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.graphics.Rect;
11 import android.os.Bundle;
12 import android.os.Looper;
13 import android.util.AttributeSet;
14 import android.view.KeyEvent;
15 import android.view.ViewGroup;
16 import android.webkit.WebSettings;
17 import android.widget.FrameLayout;
19 import org.xwalk.core.client.XWalkDefaultClient;
20 import org.xwalk.core.client.XWalkDefaultDownloadListener;
21 import org.xwalk.core.client.XWalkDefaultNavigationHandler;
22 import org.xwalk.core.client.XWalkDefaultNotificationService;
23 import org.xwalk.core.client.XWalkDefaultWebChromeClient;
25 public class XWalkView extends FrameLayout {
27 private XWalkContent mContent;
28 private XWalkDevToolsServer mDevToolsServer;
29 private Activity mActivity;
30 private Context mContext;
32 public XWalkView(Context context, Activity activity) {
36 // Make sure mActivity is initialized before calling 'init' method.
42 public Activity getActivity() {
43 if (mActivity != null) {
45 } else if (getContext() instanceof Activity) {
46 return (Activity)getContext();
49 // Never achieve here.
54 public Context getViewContext() {
59 * Constructors for inflating via XML.
61 public XWalkView(Context context, AttributeSet attrs) {
62 super(context, attrs);
69 private void init(Context context, AttributeSet attrs) {
70 // Initialize chromium resources. Assign them the correct ids in
72 XWalkInternalResources.resetIds(context);
74 // Intialize library, paks and others.
75 XWalkViewDelegate.init(this);
77 initXWalkContent(context, attrs);
80 private void initXWalkContent(Context context, AttributeSet attrs) {
81 mContent = new XWalkContent(context, attrs, this);
83 new FrameLayout.LayoutParams(
84 FrameLayout.LayoutParams.MATCH_PARENT,
85 FrameLayout.LayoutParams.MATCH_PARENT));
88 // Set default XWalkDefaultClient.
89 setXWalkClient(new XWalkDefaultClient(context, this));
90 // Set default XWalkWebChromeClient and DownloadListener. The default actions
91 // are provided via the following clients if special actions are not needed.
92 setXWalkWebChromeClient(new XWalkDefaultWebChromeClient(context, this));
93 setDownloadListener(new XWalkDefaultDownloadListener(context));
94 setNavigationHandler(new XWalkDefaultNavigationHandler(context));
95 setNotificationService(new XWalkDefaultNotificationService(context, this));
98 public void loadUrl(String url) {
100 mContent.loadUrl(url);
103 public void loadAppFromManifest(String path, String manifest) {
104 mContent.loadAppFromManifest(path, manifest);
107 public void reload() {
112 public void addJavascriptInterface(Object object, String name) {
114 mContent.addJavascriptInterface(object, name);
117 public String getUrl() {
119 return mContent.getUrl();
122 public String getTitle() {
124 return mContent.getTitle();
127 public void clearCache(boolean includeDiskFiles) {
129 mContent.clearCache(includeDiskFiles);
132 public void clearHistory() {
134 mContent.clearHistory();
137 public boolean canGoBack() {
139 return mContent.canGoBack();
142 public void goBack() {
147 public boolean canGoForward() {
149 return mContent.canGoForward();
152 public void goForward() {
154 mContent.goForward();
157 public boolean isFullscreen() {
159 return mContent.isFullscreen();
162 public void exitFullscreen() {
164 mContent.exitFullscreen();
167 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
172 public void setLayoutParams(ViewGroup.LayoutParams params) {
174 super.setLayoutParams(params);
177 public XWalkSettings getSettings() {
179 return mContent.getSettings();
182 public String getOriginalUrl() {
184 return mContent.getOriginalUrl();
187 public void setNetworkAvailable(boolean networkUp) {
189 mContent.setNetworkAvailable(networkUp);
192 public void setInitialScale(int scaleInPercent) {
196 public void setXWalkWebChromeClient(XWalkWebChromeClient client) {
198 mContent.setXWalkWebChromeClient(client);
201 public void setXWalkClient(XWalkClient client) {
203 mContent.setXWalkClient(client);
206 public void stopLoading() {
208 mContent.stopLoading();
211 public void pauseTimers() {
213 mContent.pauseTimers();
216 public void resumeTimers() {
218 mContent.resumeTimers();
221 public void setDownloadListener(DownloadListener listener) {
223 mContent.setDownloadListener(listener);
226 public void setNavigationHandler(XWalkNavigationHandler handler) {
228 mContent.setNavigationHandler(handler);
231 public void setNotificationService(XWalkNotificationService service) {
233 mContent.setNotificationService(service);
236 // Enables remote debugging and returns the URL at which the dev tools server is listening
237 // for commands. The allowedUid argument can be used to specify the uid of the process that is
238 // permitted to connect.
239 public String enableRemoteDebugging(int allowedUid) {
241 // Chrome looks for "devtools_remote" pattern in the name of a unix domain socket
242 // to identify a debugging page
243 final String socketName = getContext().getApplicationContext().getPackageName() + "_devtools_remote";
244 if (mDevToolsServer == null) {
245 mDevToolsServer = new XWalkDevToolsServer(socketName);
246 mDevToolsServer.allowConnectionFromUid(allowedUid);
247 mDevToolsServer.setRemoteDebuggingEnabled(true);
249 // devtools/page is hardcoded in devtools_http_handler_impl.cc (kPageUrlPrefix)
250 return "ws://" + socketName + "/devtools/page/" + mContent.devToolsAgentId();
253 // Enables remote debugging and returns the URL at which the dev tools server is listening
254 // for commands. Only the current process is allowed to connect to the server.
255 public String enableRemoteDebugging() {
256 return enableRemoteDebugging(mContext.getApplicationInfo().uid);
259 public void disableRemoteDebugging() {
261 if (mDevToolsServer == null) return;
263 if (mDevToolsServer.isRemoteDebuggingEnabled()) {
264 mDevToolsServer.setRemoteDebuggingEnabled(false);
266 mDevToolsServer.destroy();
267 mDevToolsServer = null;
270 public void onPause() {
274 public void onResume() {
278 public boolean onKeyUp(int keyCode, KeyEvent event) {
279 if (keyCode == KeyEvent.KEYCODE_BACK) {
280 // If there's navigation happens when app is fullscreen,
281 // the content will still be fullscreen after navigation.
282 // In such case, the back key will exit fullscreen first.
283 if (isFullscreen()) {
286 } else if (canGoBack()) {
294 public void onDestroy() {
298 public void destroy() {
300 disableRemoteDebugging();
303 public void onActivityResult(int requestCode, int resultCode, Intent data) {
304 mContent.onActivityResult(requestCode, resultCode, data);
307 public boolean onNewIntent(Intent intent) {
308 return mContent.onNewIntent(intent);
311 public String getVersion() {
312 return mContent.getVersion();
315 public int getContentID() {
316 return mContent.getRoutingID();
319 // TODO(shouqun): requestFocusFromTouch, setVerticalScrollBarEnabled are
320 // from android.view.View;
322 // For instrumentation test.
323 public XWalkContent getXWalkViewContentForTest() {
328 public XWalkWebChromeClient getXWalkWebChromeClientForTest() {
329 return mContent.getXWalkWebChromeClient();
332 public WebBackForwardList copyBackForwardList() {
333 return mContent.copyBackForwardList();
336 public WebBackForwardList saveState(Bundle outState) {
337 return mContent.saveState(outState);
340 public WebBackForwardList restoreState(Bundle inState) {
341 return mContent.restoreState(inState);
344 private static void checkThreadSafety() {
345 if (Looper.myLooper() != Looper.getMainLooper()) {
346 Throwable throwable = new Throwable(
347 "Warning: A XWalkView method was called on thread '" +
348 Thread.currentThread().getName() + "'. " +
349 "All XWalkView methods must be called on the UI thread. ");
350 throw new RuntimeException(throwable);