1 // Copyright (c) 2013-2014 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.internal;
7 import android.app.Activity;
8 import android.app.AlertDialog;
9 import android.app.ApplicationErrorReport;
10 import android.content.Context;
11 import android.content.DialogInterface;
12 import android.content.Intent;
13 import android.graphics.Rect;
14 import android.net.Uri;
15 import android.os.Bundle;
16 import android.os.Environment;
17 import android.os.Looper;
18 import android.util.AttributeSet;
19 import android.view.KeyEvent;
20 import android.view.ViewGroup;
21 import android.webkit.ValueCallback;
22 import android.widget.FrameLayout;
25 import java.io.PrintWriter;
26 import java.io.StringWriter;
27 import java.lang.ref.WeakReference;
29 import org.chromium.base.ActivityState;
30 import org.chromium.base.ApplicationStatus;
31 import org.chromium.base.ApplicationStatus.ActivityStateListener;
32 import org.chromium.base.CommandLine;
34 import org.xwalk.core.internal.extension.XWalkExtensionManager;
35 import org.xwalk.core.internal.extension.XWalkPathHelper;
38 * <p>XWalkViewInternal represents an Android view for web apps/pages. Thus most of attributes
39 * for Android view are valid for this class. Since it internally uses
40 * <a href="http://developer.android.com/reference/android/view/SurfaceView.html">
41 * android.view.SurfaceView</a> for rendering web pages by default, it can't be resized,
42 * rotated, transformed and animated due to the limitations of SurfaceView.
43 * Alternatively, if the preference key {@link XWalkPreferencesInternal#ANIMATABLE_XWALK_VIEW}
44 * is set to True, XWalkViewInternal can be transformed and animated because
45 * <a href="http://developer.android.com/reference/android/view/TextureView.html">
46 * TextureView</a> is intentionally used to render web pages for animation support.
47 * Besides, XWalkViewInternal won't be rendered if it's invisible.</p>
49 * <p>XWalkViewInternal needs hardware acceleration to render web pages. As a result, the
50 * AndroidManifest.xml of the caller's app must be appended with the attribute
51 * "android:hardwareAccelerated" and its value must be set as "true".</p>
53 * <application android:name="android.app.Application" android:label="XWalkUsers"
54 * android:hardwareAccelerated="true">
57 * <p>Crosswalk provides 2 major callback classes, namely {@link XWalkResourceClientInternal} and
58 * {@link XWalkUIClientInternal} for listening to the events related to resource loading and UI.
59 * By default, Crosswalk has a default implementation. Callers can override them if needed.</p>
61 * <p>Unlike other Android views, this class has to listen to system events like intents and activity result.
62 * The web engine inside this view need to get and handle them.
63 * With contianer activity's lifecycle change, XWalkViewInternal will pause all timers and other
64 * components like videos when activity paused, resume back them when activity resumed.
65 * When activity is about to destroy, XWalkViewInternal will destroy itself as well.
66 * Embedders can also call onHide() and pauseTimers() to explicitly pause XWalkViewInternal.
67 * Similarily with onShow(), resumeTimers() and onDestroy().
72 * import android.app.Activity;
73 * import android.os.Bundle;
75 * import org.xwalk.core.internal.XWalkResourceClientInternal;
76 * import org.xwalk.core.internal.XWalkUIClientInternal;
77 * import org.xwalk.core.internal.XWalkViewInternal;
79 * public class MyActivity extends Activity {
80 * XWalkViewInternal mXwalkView;
82 * class MyResourceClient extends XWalkResourceClientInternal {
83 * MyResourceClient(XWalkViewInternal view) {
88 * WebResourceResponse shouldInterceptLoadRequest(XWalkViewInternal view, String url) {
94 * class MyUIClient extends XWalkUIClientInternal {
95 * MyUIClient(XWalkViewInternal view) {
100 * void onFullscreenToggled(XWalkViewInternal view, String url) {
107 * protected void onCreate(Bundle savedInstanceState) {
108 * mXwalkView = new XWalkViewInternal(this, null);
109 * setContentView(mXwalkView);
110 * mXwalkView.setResourceClient(new MyResourceClient(mXwalkView));
111 * mXwalkView.setUIClient(new MyUIClient(mXwalkView));
112 * mXwalkView.load("http://www.crosswalk-project.org", null);
116 * protected void onActivityResult(int requestCode, int resultCode, Intent data) {
117 * if (mXwalkView != null) {
118 * mXwalkView.onActivityResult(requestCode, resultCode, data);
123 * protected void onNewIntent(Intent intent) {
124 * if (mXwalkView != null) {
125 * mXwalkView.onNewIntent(intent);
131 public class XWalkViewInternal extends android.widget.FrameLayout {
133 private class XWalkActivityStateListener implements ActivityStateListener {
134 WeakReference<XWalkViewInternal> mXWalkViewRef;
136 XWalkActivityStateListener(XWalkViewInternal view) {
137 mXWalkViewRef = new WeakReference<XWalkViewInternal>(view);
141 public void onActivityStateChange(Activity activity, int newState) {
142 XWalkViewInternal view = mXWalkViewRef.get();
143 if (view == null) return;
144 view.onActivityStateChange(activity, newState);
148 static final String PLAYSTORE_DETAIL_URI = "market://details?id=";
150 private XWalkContent mContent;
151 private Activity mActivity;
152 private Context mContext;
153 private XWalkExtensionManager mExtensionManager;
154 private boolean mIsHidden;
155 private XWalkActivityStateListener mActivityStateListener;
158 * Normal reload mode as default.
161 public static final int RELOAD_NORMAL = 0;
163 * Reload mode with bypassing the cache.
166 public static final int RELOAD_IGNORE_CACHE = 1;
169 * Constructor for inflating via XML.
170 * @param context a Context object used to access application assets.
171 * @param attrs an AttributeSet passed to our parent.
174 public XWalkViewInternal(Context context, AttributeSet attrs) {
175 super(context, attrs);
179 init(context, attrs);
183 * Constructor for Crosswalk runtime. In shared mode, context isi
184 * different from activity. In embedded mode, they're same.
185 * @param context a Context object used to access application assets
186 * @param activity the activity for this XWalkViewInternal.
189 public XWalkViewInternal(Context context, Activity activity) {
190 super(context, null);
193 // Make sure mActivity is initialized before calling 'init' method.
194 mActivity = activity;
200 * Get the current activity passed from callers. It's never null.
201 * @return the activity instance passed from callers.
205 public Activity getActivity() {
206 if (mActivity != null) {
208 } else if (getContext() instanceof Activity) {
209 return (Activity)getContext();
212 // Never achieve here.
217 // TODO(yongsheng): we should remove this since we have getContext()?
221 public Context getViewContext() {
225 private void init(Context context, AttributeSet attrs) {
226 // Initialize chromium resources. Assign them the correct ids in
228 XWalkInternalResources.resetIds(context);
230 // Intialize library, paks and others.
232 XWalkViewDelegate.init(this);
233 mActivityStateListener = new XWalkActivityStateListener(this);
234 ApplicationStatus.registerStateListenerForActivity(
235 mActivityStateListener, getActivity());
236 } catch (Throwable e) {
237 // Try to find if there is UnsatisfiedLinkError in the cause chain of the met Throwable.
238 Throwable linkError = e;
240 if (linkError == null) throw new RuntimeException(e);
241 if (linkError instanceof UnsatisfiedLinkError) break;
242 if (linkError.getCause() == null ||
243 linkError.getCause().equals(linkError)) {
244 throw new RuntimeException(e);
246 linkError = linkError.getCause();
248 final UnsatisfiedLinkError err = (UnsatisfiedLinkError) linkError;
249 final Activity activity = getActivity();
250 final String packageName = context.getPackageName();
251 String missingArch = XWalkViewDelegate.isRunningOnIA() ? "Intel" : "ARM";
252 final String message =
253 context.getString(R.string.cpu_arch_mismatch_message, missingArch);
255 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
256 builder.setTitle(R.string.cpu_arch_mismatch_title)
258 .setOnCancelListener(new DialogInterface.OnCancelListener() {
260 public void onCancel(DialogInterface dialog) {
263 }).setPositiveButton(R.string.goto_store_button_label,
264 new DialogInterface.OnClickListener() {
266 public void onClick(DialogInterface dialog, int which) {
267 activity.startActivity(new Intent(Intent.ACTION_VIEW,
268 Uri.parse(PLAYSTORE_DETAIL_URI + packageName)));
271 }).setNeutralButton(R.string.report_feedback_button_label,
272 new DialogInterface.OnClickListener() {
274 public void onClick(DialogInterface dialog, int which) {
275 ApplicationErrorReport report = new ApplicationErrorReport();
276 report.type = ApplicationErrorReport.TYPE_CRASH;
277 report.packageName = report.processName = packageName;
279 ApplicationErrorReport.CrashInfo crash =
280 new ApplicationErrorReport.CrashInfo();
281 crash.exceptionClassName = err.getClass().getSimpleName();
282 crash.exceptionMessage = "CPU architecture mismatch";
283 StringWriter writer = new StringWriter();
284 PrintWriter print = new PrintWriter(writer);
285 err.printStackTrace(print);
286 crash.stackTrace = writer.toString();
287 StackTraceElement stack = err.getStackTrace()[0];
288 crash.throwClassName = stack.getClassName();
289 crash.throwFileName = stack.getFileName();
290 crash.throwLineNumber = stack.getLineNumber();
291 crash.throwMethodName = stack.getMethodName();
293 report.crashInfo = crash;
294 report.systemApp = false;
295 report.time = System.currentTimeMillis();
297 Intent intent = new Intent(Intent.ACTION_APP_ERROR);
298 intent.putExtra(Intent.EXTRA_BUG_REPORT, report);
299 activity.startActivity(intent);
303 builder.create().show();
307 initXWalkContent(context, attrs);
310 private void initXWalkContent(Context context, AttributeSet attrs) {
312 mContent = new XWalkContent(context, attrs, this);
314 new FrameLayout.LayoutParams(
315 FrameLayout.LayoutParams.MATCH_PARENT,
316 FrameLayout.LayoutParams.MATCH_PARENT));
319 // Set default XWalkClientImpl.
320 setXWalkClient(new XWalkClient(this));
321 // Set default XWalkWebChromeClient and DownloadListener. The default actions
322 // are provided via the following clients if special actions are not needed.
323 setXWalkWebChromeClient(new XWalkWebChromeClient(this));
325 // Set with internal implementation. Could be overwritten by embedders'
327 setUIClient(new XWalkUIClientInternal(this));
328 setResourceClient(new XWalkResourceClientInternal(this));
330 setDownloadListener(new XWalkDownloadListenerImpl(context));
331 setNavigationHandler(new XWalkNavigationHandlerImpl(context));
332 setNotificationService(new XWalkNotificationServiceImpl(context, this));
334 if (!CommandLine.getInstance().hasSwitch("disable-xwalk-extensions")) {
335 // Enable xwalk extension mechanism and start load extensions here.
336 // Note that it has to be after above initialization.
337 mExtensionManager = new XWalkExtensionManager(context, getActivity());
338 mExtensionManager.loadExtensions();
341 XWalkPathHelper.initialize();
342 XWalkPathHelper.setCacheDirectory(
343 mContext.getApplicationContext().getCacheDir().getPath());
345 String state = Environment.getExternalStorageState();
346 if (Environment.MEDIA_MOUNTED.equals(state) ||
347 Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
348 File extCacheDir = mContext.getApplicationContext().getExternalCacheDir();
349 if (null != extCacheDir) {
350 XWalkPathHelper.setExternalCacheDirectory(extCacheDir.getPath());
356 * Load a web page/app from a given base URL or a content.
357 * If url is null or empty and content is null or empty, then this function
359 * If content is not null, load the web page/app from the content.
360 * If content is not null and the url is not set, return "about:blank" ifi
361 * calling {@link XWalkViewInternal#getUrl()}.
362 * If content is null, try to load the content from the url.
364 * It supports URL schemes like 'http:', 'https:' and 'file:'.
365 * It can also load files from Android assets, e.g. 'file:///android_asset/'.
366 * @param url the url for web page/app.
367 * @param content the content for the web page/app. Could be empty.
370 public void load(String url, String content) {
371 if (mContent == null) return;
373 mContent.loadUrl(url, content);
377 * Load a web app from a given manifest.json file. If content is not null,
378 * load the manifest.json from the content. If content is null, try to load
379 * the manifest.json from the url. Note that url should not be null if the
380 * launched path defined in manifest.json is relative.
382 * It supports URL schemes like 'http:', 'https:' and 'file:'.
383 * It can also load files from Android assets, e.g. 'file:///android_asset/'.
384 * @param url the url for manifest.json.
385 * @param content the content for manifest.json.
388 public void loadAppFromManifest(String url, String content) {
389 if (mContent == null) return;
391 mContent.loadAppFromManifest(url, content);
395 * Reload a web app with a given mode.
396 * @param mode the reload mode.
399 public void reload(int mode) {
400 if (mContent == null) return;
402 mContent.reload(mode);
406 * Stop current loading progress.
409 public void stopLoading() {
410 if (mContent == null) return;
412 mContent.stopLoading();
416 * Get the url of current web page/app. This may be different from what's passed
418 * @return the url for current web page/app.
421 public String getUrl() {
422 if (mContent == null) return null;
424 return mContent.getUrl();
428 * Get the title of current web page/app. This may be different from what's passed
430 * @return the title for current web page/app.
433 public String getTitle() {
434 if (mContent == null) return null;
436 return mContent.getTitle();
440 * Get the original url specified by caller.
441 * @return the original url.
444 public String getOriginalUrl() {
445 if (mContent == null) return null;
447 return mContent.getOriginalUrl();
451 * Get the navigation history for current XWalkViewInternal. It's synchronized with
452 * this XWalkViewInternal if any backward/forward and navigation operations.
453 * @return the navigation history.
456 public XWalkNavigationHistoryInternal getNavigationHistory() {
457 if (mContent == null) return null;
459 return mContent.getNavigationHistory();
463 * Injects the supplied Java object into this XWalkViewInternal.
464 * Each method defined in the class of the object should be
465 * marked with {@link JavascriptInterface} if it's called by JavaScript.
466 * @param object the supplied Java object, called by JavaScript.
467 * @param name the name injected in JavaScript.
470 public void addJavascriptInterface(Object object, String name) {
471 if (mContent == null) return;
473 mContent.addJavascriptInterface(object, name);
477 * Evaluate a fragment of JavaScript code and get the result via callback.
478 * @param script the JavaScript string.
479 * @param callback the callback to handle the evaluated result.
482 public void evaluateJavascript(String script, ValueCallback<String> callback) {
483 if (mContent == null) return;
485 mContent.evaluateJavascript(script, callback);
489 * Clear the resource cache. Note that the cache is per-application, so this
490 * will clear the cache for all XWalkViews used.
491 * @param includeDiskFiles indicate whether to clear disk files for cache.
494 public void clearCache(boolean includeDiskFiles) {
495 if (mContent == null) return;
497 mContent.clearCache(includeDiskFiles);
501 * Indicate that a HTML element is occupying the whole screen.
502 * @return true if any HTML element is occupying the whole screen.
505 public boolean hasEnteredFullscreen() {
506 if (mContent == null) return false;
508 return mContent.hasEnteredFullscreen();
512 * Leave fullscreen mode if it's. Do nothing if it's not
516 public void leaveFullscreen() {
517 if (mContent == null) return;
519 mContent.exitFullscreen();
523 * Pause all layout, parsing and JavaScript timers for all XWalkViewInternal instances.
524 * It will be called when the container Activity get paused. It can also be explicitly
525 * called to pause timers.
527 * Note that it will globally impact all XWalkViewInternal instances, not limited to
528 * just this XWalkViewInternal.
532 public void pauseTimers() {
533 if (mContent == null) return;
535 mContent.pauseTimers();
539 * Resume all layout, parsing and JavaScript timers for all XWalkViewInternal instances.
540 * It will be called when the container Activity get resumed. It can also be explicitly
541 * called to resume timers.
543 * Note that it will globally impact all XWalkViewInternal instances, not limited to
544 * just this XWalkViewInternal.
548 public void resumeTimers() {
549 if (mContent == null) return;
551 mContent.resumeTimers();
555 * Pause many other things except JavaScript timers inside rendering engine,
556 * like video player, modal dialogs, etc. See {@link #pauseTimers} about pausing
558 * It will be called when the container Activity get paused. It can also be explicitly
559 * called to pause above things.
562 public void onHide() {
563 if (mContent == null || mIsHidden) return;
564 if (null != mExtensionManager) mExtensionManager.onPause();
570 * Resume video player, modal dialogs. Embedders are in charge of calling
571 * this during resuming this activity if they call onHide.
572 * Typically it should be called when the activity for this view is resumed.
573 * It will be called when the container Activity get resumed. It can also be explicitly
574 * called to resume above things.
577 public void onShow() {
578 if (mContent == null || !mIsHidden ) return;
579 if (null != mExtensionManager) mExtensionManager.onResume();
585 * Release internal resources occupied by this XWalkViewInternal.
586 * It will be called when the container Activity get destroyed. It can also be explicitly
587 * called to release resources.
590 public void onDestroy() {
595 * Pass through activity result to XWalkViewInternal. Many internal facilities need this
596 * to handle activity result like JavaScript dialog, Crosswalk extensions, etc.
597 * See <a href="http://developer.android.com/reference/android/app/Activity.html">
598 * android.app.Activity.onActivityResult()</a>.
599 * @param requestCode passed from android.app.Activity.onActivityResult().
600 * @param resultCode passed from android.app.Activity.onActivityResult().
601 * @param data passed from android.app.Activity.onActivityResult().
604 public void onActivityResult(int requestCode, int resultCode, Intent data) {
605 if (mContent == null) return;
606 if (null != mExtensionManager)
607 mExtensionManager.onActivityResult(requestCode, resultCode, data);
608 mContent.onActivityResult(requestCode, resultCode, data);
612 * Pass through intents to XWalkViewInternal. Many internal facilities need this
613 * to receive the intents like web notification. See
614 * <a href="http://developer.android.com/reference/android/app/Activity.html">
615 * android.app.Activity.onNewIntent()</a>.
616 * @param intent passed from android.app.Activity.onNewIntent().
619 public boolean onNewIntent(Intent intent) {
620 if (mContent == null) return false;
621 return mContent.onNewIntent(intent);
625 * Save current internal state of this XWalkViewInternal. This can help restore this state
626 * afterwards restoring.
627 * @param outState the saved state for restoring.
630 public boolean saveState(Bundle outState) {
631 if (mContent == null) return false;
632 mContent.saveState(outState);
637 * Restore the state from the saved bundle data.
638 * @param inState the state saved from saveState().
639 * @return true if it can restore the state.
642 public boolean restoreState(Bundle inState) {
643 if (mContent == null) return false;
644 if (mContent.restoreState(inState) != null) return true;
649 * Get the API version of Crosswalk embedding API.
650 * @return the string of API level.
653 // TODO(yongsheng): make it static?
654 public String getAPIVersion() {
659 * Get the Crosswalk version.
660 * @return the string of Crosswalk.
663 // TODO(yongsheng): make it static?
664 public String getXWalkVersion() {
665 if (mContent == null) return null;
666 return mContent.getXWalkVersion();
670 * Embedders use this to customize their handlers to events/callbacks related
672 * @param client the XWalkUIClientInternal defined by callers.
675 public void setUIClient(XWalkUIClientInternal client) {
676 if (mContent == null) return;
678 mContent.setUIClient(client);
682 * Embedders use this to customize their handlers to events/callbacks related
683 * to resource loading.
684 * @param client the XWalkResourceClientInternal defined by callers.
687 public void setResourceClient(XWalkResourceClientInternal client) {
688 if (mContent == null) return;
690 mContent.setResourceClient(client);
693 // TODO(yongsheng): this is not public.
697 public XWalkSettings getSettings() {
698 if (mContent == null) return null;
700 return mContent.getSettings();
704 * This method is used by Cordova for hacking.
705 * TODO(yongsheng): remove this and related test cases?
709 public void setNetworkAvailable(boolean networkUp) {
710 if (mContent == null) return;
712 mContent.setNetworkAvailable(networkUp);
716 * Enables remote debugging and returns the URL at which the dev tools server is listening
717 * for commands. The allowedUid argument can be used to specify the uid of the process that is
718 * permitted to connect.
719 * TODO(yongsheng): how to enable this in XWalkPreferencesInternal?
723 public String enableRemoteDebugging(int allowedUid) {
724 if (mContent == null) return null;
726 return mContent.enableRemoteDebugging(allowedUid);
730 * It's used for Presentation API.
733 public int getContentID() {
734 if (mContent == null) return -1;
735 return mContent.getRoutingID();
738 boolean canGoBack() {
739 if (mContent == null) return false;
741 return mContent.canGoBack();
745 if (mContent == null) return;
750 boolean canGoForward() {
751 if (mContent == null) return false;
753 return mContent.canGoForward();
757 if (mContent == null) return;
759 mContent.goForward();
762 void clearHistory() {
763 if (mContent == null) return;
765 mContent.clearHistory();
769 if (mContent == null) return;
770 ApplicationStatus.unregisterActivityStateListener(mActivityStateListener);
771 mActivityStateListener = null;
772 if (null != mExtensionManager) mExtensionManager.onDestroy();
774 disableRemoteDebugging();
777 // Enables remote debugging and returns the URL at which the dev tools server is listening
778 // for commands. Only the current process is allowed to connect to the server.
779 String enableRemoteDebugging() {
780 return enableRemoteDebugging(mContext.getApplicationInfo().uid);
783 void disableRemoteDebugging() {
784 if (mContent == null) return;
786 mContent.disableRemoteDebugging();
789 private static void checkThreadSafety() {
790 if (Looper.myLooper() != Looper.getMainLooper()) {
791 Throwable throwable = new Throwable(
792 "Warning: A XWalkViewInternal method was called on thread '" +
793 Thread.currentThread().getName() + "'. " +
794 "All XWalkViewInternal methods must be called on the UI thread. ");
795 throw new RuntimeException(throwable);
799 boolean isOwnerActivityRunning() {
800 int status = ApplicationStatus.getStateForActivity(getActivity());
801 if (status == ActivityState.DESTROYED) return false;
805 void navigateTo(int offset) {
806 if (mContent == null) return;
807 mContent.navigateTo(offset);
810 void setOverlayVideoMode(boolean enabled) {
811 mContent.setOverlayVideoMode(enabled);
814 // Below methods are for test shell and instrumentation tests.
818 public void setXWalkClient(XWalkClient client) {
819 if (mContent == null) return;
821 mContent.setXWalkClient(client);
827 public void setXWalkWebChromeClient(XWalkWebChromeClient client) {
828 if (mContent == null) return;
830 mContent.setXWalkWebChromeClient(client);
836 public void setDownloadListener(DownloadListener listener) {
837 if (mContent == null) return;
839 mContent.setDownloadListener(listener);
845 public void setNavigationHandler(XWalkNavigationHandler handler) {
846 if (mContent == null) return;
848 mContent.setNavigationHandler(handler);
854 public void setNotificationService(XWalkNotificationService service) {
855 if (mContent == null) return;
857 mContent.setNotificationService(service);
864 public boolean dispatchKeyEvent(KeyEvent event) {
865 if (event.getAction() == KeyEvent.ACTION_UP &&
866 event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
867 // If there's navigation happens when app is fullscreen,
868 // the content will still be fullscreen after navigation.
869 // In such case, the back key will exit fullscreen first.
870 if (hasEnteredFullscreen()) {
873 } else if (canGoBack()) {
878 return super.dispatchKeyEvent(event);
881 private void onActivityStateChange(Activity activity, int newState) {
882 assert(getActivity() == activity);
884 case ActivityState.STARTED:
887 case ActivityState.PAUSED:
890 case ActivityState.RESUMED:
893 case ActivityState.DESTROYED:
896 case ActivityState.STOPPED: