Upstream version 10.39.233.0
[platform/framework/web/crosswalk.git] / src / xwalk / app / android / runtime_client / src / org / xwalk / app / 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.app.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.LinearLayout;
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 LinearLayout {
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 context, AttributeSet attrs) {
37         super(context, attrs);
38
39         // MixedContext is needed for cross package because the application
40         // context is different.
41         init(context, 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         setOrientation(LinearLayout.VERTICAL);
58         this.addView(mProvider.getView(),
59                 new LinearLayout.LayoutParams(
60                         LinearLayout.LayoutParams.MATCH_PARENT,
61                         LinearLayout.LayoutParams.MATCH_PARENT));
62     }
63
64     /**
65      * Get the version information of current runtime library.
66      *
67      * @return the string containing the version information.
68      */
69     public String getVersion() {
70         return mProvider.getVersion();
71     }
72
73     /**
74      * Load a web application through the entry url. It may be
75      * a file from assets or a url from network.
76      *
77      * @param url the url of loaded html resource.
78      */
79     public void loadAppFromUrl(String url) {
80         mProvider.loadAppFromUrl(url);
81     }
82
83     /**
84      * Load a web application through the url of the manifest file.
85      * The manifest file typically is placed in android assets. Now it is
86      * compliant to W3C SysApps spec.
87      *
88      * @param manifestUrl the url of the manifest file
89      */
90     public void loadAppFromManifest(String manifestUrl) {
91         mProvider.loadAppFromManifest(manifestUrl);
92     }
93
94     /**
95      * Tell runtime that the application is on creating. This can make runtime
96      * be aware of application life cycle.
97      */
98     public void onCreate() {
99         mProvider.onCreate();
100     }
101
102     /**
103      * Tell runtime that the application is on resuming. This can make runtime
104      * be aware of application life cycle.
105      */
106     public void onResume() {
107         mProvider.onResume();
108     }
109
110     /**
111      * Tell runtime that the application is on pausing. This can make runtime
112      * be aware of application life cycle.
113      */
114     public void onPause() {
115         mProvider.onPause();
116     }
117
118     /**
119      * Tell runtime that the application is on destroying. This can make runtime
120      * be aware of application life cycle.
121      */
122     public void onDestroy() {
123         mProvider.onDestroy();
124     }
125
126     /**
127      * Tell runtime that one activity exists so that it can know the result code
128      * of the exit code.
129      *
130      * @param requestCode the request code to identify where the result is from
131      * @param resultCode the result code of the activity
132      * @param data the data to contain the result data
133      */
134     public void onActivityResult(int requestCode, int resultCode, Intent data) {
135         mProvider.onActivityResult(requestCode, resultCode, data);
136     }
137
138     /**
139      * Tell runtime that the activity receive a new Intent to start it. It may contains
140      * data that runtime want to deal with.
141      * @param intent the new coming Intent.
142      * @return boolean whether runtime consumed it. 
143      */
144     public boolean onNewIntent(Intent intent) {
145         return mProvider.onNewIntent(intent);
146     }
147
148     /**
149      * Enable remote debugging for the loaded web application. The caller
150      * can set the url of debugging url. Besides, the socket name for remote
151      * debugging has to be unique so typically the string can be appended
152      * with the package name of the application.
153      *
154      * @param frontEndUrl the url of debugging url. If it's empty, then a
155      *                    default url will be used.
156      * @param socketName the unique socket name for setting up socket for
157      *                   remote debugging. If it's empty, then a default
158      *                   name will be used.
159      * @return the url of web socket for remote debugging
160      */
161     public void enableRemoteDebugging(String frontEndUrl, String socketName) {
162         // TODO(yongsheng): Figure out which parameters are needed once we
163         // have a conclusion.
164         mProvider.enableRemoteDebugging(frontEndUrl,socketName);
165     }
166
167     /**
168      * Disable remote debugging so runtime can close related stuff for
169      * this feature.
170      */
171     public void disableRemoteDebugging() {
172         mProvider.disableRemoteDebugging();
173     }
174
175     // For instrumentation test.
176     public String getTitleForTest() {
177         return mProvider.getTitleForTest();
178     }
179
180     public void setCallbackForTest(Object callback) {
181         mProvider.setCallbackForTest(callback);
182     }
183
184     public void loadDataForTest(String data, String mimeType, boolean isBase64Encoded) {
185         mProvider.loadDataForTest(data, mimeType, isBase64Encoded);
186     }
187 }