Upstream version 9.38.198.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 starting. This can make runtime
103      * be aware of application life cycle.
104      */
105     public void onStart() {
106         mProvider.onStart();
107     }
108
109     /**
110      * Tell runtime that the application is on resuming. This can make runtime
111      * be aware of application life cycle.
112      */
113     public void onResume() {
114         mProvider.onResume();
115     }
116
117     /**
118      * Tell runtime that the application is on pausing. This can make runtime
119      * be aware of application life cycle.
120      */
121     public void onPause() {
122         mProvider.onPause();
123     }
124
125     /**
126      * Tell runtime that the application is on stoping. This can make runtime
127      * be aware of application life cycle.
128      */
129     public void onStop() {
130         mProvider.onStop();
131     }
132
133     /**
134      * Tell runtime that the application is on destroying. This can make runtime
135      * be aware of application life cycle.
136      */
137     public void onDestroy() {
138         mProvider.onDestroy();
139     }
140
141     /**
142      * Tell runtime that one activity exists so that it can know the result code
143      * of the exit code.
144      *
145      * @param requestCode the request code to identify where the result is from
146      * @param resultCode the result code of the activity
147      * @param data the data to contain the result data
148      */
149     public void onActivityResult(int requestCode, int resultCode, Intent data) {
150         mProvider.onActivityResult(requestCode, resultCode, data);
151     }
152
153     /**
154      * Tell runtime that the activity receive a new Intent to start it. It may contains
155      * data that runtime want to deal with.
156      * @param intent the new coming Intent.
157      * @return boolean whether runtime consumed it. 
158      */
159     public boolean onNewIntent(Intent intent) {
160         return mProvider.onNewIntent(intent);
161     }
162
163     /**
164      * Enable remote debugging for the loaded web application. The caller
165      * can set the url of debugging url. Besides, the socket name for remote
166      * debugging has to be unique so typically the string can be appended
167      * with the package name of the application.
168      *
169      * @param frontEndUrl the url of debugging url. If it's empty, then a
170      *                    default url will be used.
171      * @param socketName the unique socket name for setting up socket for
172      *                   remote debugging. If it's empty, then a default
173      *                   name will be used.
174      * @return the url of web socket for remote debugging
175      */
176     public void enableRemoteDebugging(String frontEndUrl, String socketName) {
177         // TODO(yongsheng): Figure out which parameters are needed once we
178         // have a conclusion.
179         mProvider.enableRemoteDebugging(frontEndUrl,socketName);
180     }
181
182     /**
183      * Disable remote debugging so runtime can close related stuff for
184      * this feature.
185      */
186     public void disableRemoteDebugging() {
187         mProvider.disableRemoteDebugging();
188     }
189
190     // For instrumentation test.
191     public String getTitleForTest() {
192         return mProvider.getTitleForTest();
193     }
194
195     public void setCallbackForTest(Object callback) {
196         mProvider.setCallbackForTest(callback);
197     }
198
199     public void loadDataForTest(String data, String mimeType, boolean isBase64Encoded) {
200         mProvider.loadDataForTest(data, mimeType, isBase64Encoded);
201     }
202 }