Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / runtime / src / org / xwalk / runtime / XWalkRuntimeView.java
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.
4
5 package org.xwalk.runtime;
6
7 import android.app.Activity;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.util.AttributeSet;
11 import android.widget.FrameLayout;
12
13 /**
14  * This class is to provide public APIs which are called by web application
15  * APKs. Since the runtime is shared as a library APK, web application APKs
16  * have to call them via class loader but not direct API calling.
17  *
18  * A web application APK should create its Activity and set this view as
19  * its content view.
20  */
21 // Implementation notes.
22 // Please be careful to change any public APIs for the backward compatibility
23 // is very important to us. Don't change any of them without permisson.
24 public class XWalkRuntimeView extends FrameLayout {
25     // The actual implementation to hide the internals to API users.
26     private XWalkRuntimeViewProvider mProvider;
27
28     /**
29      * Contructs a XWalkRuntimeView with Activity and library Context. Called
30      * from runtime client.
31      *
32      * @param activity the activity from runtime client
33      * @param context a context when creating this package
34      * @param attrs the attributes of the XML tag that is inflating the view
35      */
36     public XWalkRuntimeView(Activity activity, Context libContext, AttributeSet attrs) {
37         super(libContext, attrs);
38
39         // MixedContext is needed for cross package because the application
40         // context is different.
41         init(new MixedContext(libContext, activity), activity);
42     }
43
44     /**
45      * This is for inflating this view from XML. Called from test shell.
46      * @param context a context to construct View
47      * @param attrs the attributes of the XML tag that is inflating the view
48      */
49     public XWalkRuntimeView(Context context, AttributeSet attrs) {
50         super(context, attrs);
51
52         init(context, (Activity)context);
53     }
54
55     private void init(Context context, Activity activity) {
56         mProvider = XWalkRuntimeViewProviderFactory.getProvider(context, activity);
57         this.addView(mProvider.getView(),
58                 new FrameLayout.LayoutParams(
59                         FrameLayout.LayoutParams.MATCH_PARENT,
60                         FrameLayout.LayoutParams.MATCH_PARENT));
61     }
62
63     /**
64      * Get the version information of current runtime library.
65      *
66      * @return the string containing the version information.
67      */
68     public String getVersion() {
69         return mProvider.getVersion();
70     }
71
72     /**
73      * Load a web application through the entry url. It may be
74      * a file from assets or a url from network.
75      *
76      * @param url the url of loaded html resource.
77      */
78     public void loadAppFromUrl(String url) {
79         mProvider.loadAppFromUrl(url);
80     }
81
82     /**
83      * Load a web application through the url of the manifest file.
84      * The manifest file typically is placed in android assets. Now it is
85      * compliant to W3C SysApps spec.
86      *
87      * @param manifestUrl the url of the manifest file
88      */
89     public void loadAppFromManifest(String manifestUrl) {
90         mProvider.loadAppFromManifest(manifestUrl);
91     }
92
93     /**
94      * Tell runtime that the application is on creating. This can make runtime
95      * be aware of application life cycle.
96      */
97     public void onCreate() {
98         mProvider.onCreate();
99     }
100
101     /**
102      * Tell runtime that the application is on resuming. This can make runtime
103      * be aware of application life cycle.
104      */
105     public void onResume() {
106         mProvider.onResume();
107     }
108
109     /**
110      * Tell runtime that the application is on pausing. This can make runtime
111      * be aware of application life cycle.
112      */
113     public void onPause() {
114         mProvider.onPause();
115     }
116
117     /**
118      * Tell runtime that the application is on destroying. This can make runtime
119      * be aware of application life cycle.
120      */
121     public void onDestroy() {
122         mProvider.onDestroy();
123     }
124
125     /**
126      * Tell runtime that one activity exists so that it can know the result code
127      * of the exit code.
128      *
129      * @param requestCode the request code to identify where the result is from
130      * @param resultCode the result code of the activity
131      * @param data the data to contain the result data
132      */
133     public void onActivityResult(int requestCode, int resultCode, Intent data) {
134         mProvider.onActivityResult(requestCode, resultCode, data);
135     }
136
137     /**
138      * Tell runtime that the activity receive a new Intent to start it. It may contains
139      * data that runtime want to deal with.
140      * @param intent the new coming Intent.
141      * @return boolean whether runtime consumed it. 
142      */
143     public boolean onNewIntent(Intent intent) {
144         return mProvider.onNewIntent(intent);
145     }
146
147     /**
148      * Enable remote debugging for the loaded web application. The caller
149      * can set the url of debugging url. Besides, the socket name for remote
150      * debugging has to be unique so typically the string can be appended
151      * with the package name of the application.
152      *
153      * @param frontEndUrl the url of debugging url. If it's empty, then a
154      *                    default url will be used.
155      * @param socketName the unique socket name for setting up socket for
156      *                   remote debugging. If it's empty, then a default
157      *                   name will be used.
158      * @return the url of web socket for remote debugging
159      */
160     public void enableRemoteDebugging(String frontEndUrl, String socketName) {
161         // TODO(yongsheng): Figure out which parameters are needed once we
162         // have a conclusion.
163         mProvider.enableRemoteDebugging(frontEndUrl,socketName);
164     }
165
166     /**
167      * Disable remote debugging so runtime can close related stuff for
168      * this feature.
169      */
170     public void disableRemoteDebugging() {
171         mProvider.disableRemoteDebugging();
172     }
173
174     // For instrumentation test.
175     public String getTitleForTest() {
176         return mProvider.getTitleForTest();
177     }
178
179     public void setCallbackForTest(Object callback) {
180         mProvider.setCallbackForTest(callback);
181     }
182
183     public void loadDataForTest(String data, String mimeType, boolean isBase64Encoded) {
184         mProvider.loadDataForTest(data, mimeType, isBase64Encoded);
185     }
186 }