• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.googlecode.android_scripting.facade;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 
22 import com.googlecode.android_scripting.Constants;
23 import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
24 import com.googlecode.android_scripting.rpc.Rpc;
25 import com.googlecode.android_scripting.rpc.RpcParameter;
26 
27 import java.io.Serializable;
28 
29 /**
30  * Allows you to return results to a startActivityForResult call.
31  *
32  */
33 public class ActivityResultFacade extends RpcReceiver {
34 
35   private static final String sRpcDescription =
36       "Sets the result of a script execution. Whenever the script APK is called via "
37           + "startActivityForResult(), the resulting intent will contain " + Constants.EXTRA_RESULT
38           + " extra with the given value.";
39   private static final String sCodeDescription =
40       "The result code to propagate back to the originating activity, often RESULT_CANCELED (0) "
41           + "or RESULT_OK (-1)";
42 
43   private Activity mActivity = null;
44   private Intent mResult = null;
45   private int mResultCode;
46 
ActivityResultFacade(FacadeManager manager)47   public ActivityResultFacade(FacadeManager manager) {
48     super(manager);
49   }
50 
51   @Rpc(description = sRpcDescription)
setResultBoolean( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Boolean resultValue)52   public synchronized void setResultBoolean(
53       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
54       @RpcParameter(name = "resultValue") Boolean resultValue) {
55     mResult = new Intent();
56     mResult.putExtra(Constants.EXTRA_RESULT, resultValue.booleanValue());
57     mResultCode = resultCode;
58     if (mActivity != null) {
59       setResult();
60     }
61   }
62 
63   @Rpc(description = sRpcDescription)
setResultByte( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Byte resultValue)64   public synchronized void setResultByte(
65       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
66       @RpcParameter(name = "resultValue") Byte resultValue) {
67     mResult = new Intent();
68     mResult.putExtra(Constants.EXTRA_RESULT, resultValue.byteValue());
69     mResultCode = resultCode;
70     if (mActivity != null) {
71       setResult();
72     }
73   }
74 
75   @Rpc(description = sRpcDescription)
setResultShort( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Short resultValue)76   public synchronized void setResultShort(
77       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
78       @RpcParameter(name = "resultValue") Short resultValue) {
79     mResult = new Intent();
80     mResult.putExtra(Constants.EXTRA_RESULT, resultValue.shortValue());
81     mResultCode = resultCode;
82     if (mActivity != null) {
83       setResult();
84     }
85   }
86 
87   @Rpc(description = sRpcDescription)
setResultChar( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Character resultValue)88   public synchronized void setResultChar(
89       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
90       @RpcParameter(name = "resultValue") Character resultValue) {
91     mResult = new Intent();
92     mResult.putExtra(Constants.EXTRA_RESULT, resultValue.charValue());
93     mResultCode = resultCode;
94     if (mActivity != null) {
95       setResult();
96     }
97   }
98 
99   @Rpc(description = sRpcDescription)
setResultInteger( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Integer resultValue)100   public synchronized void setResultInteger(
101       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
102       @RpcParameter(name = "resultValue") Integer resultValue) {
103     mResult = new Intent();
104     mResult.putExtra(Constants.EXTRA_RESULT, resultValue.intValue());
105     mResultCode = resultCode;
106     if (mActivity != null) {
107       setResult();
108     }
109   }
110 
111   @Rpc(description = sRpcDescription)
setResultLong( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Long resultValue)112   public synchronized void setResultLong(
113       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
114       @RpcParameter(name = "resultValue") Long resultValue) {
115     mResult = new Intent();
116     mResult.putExtra(Constants.EXTRA_RESULT, resultValue.longValue());
117     mResultCode = resultCode;
118     if (mActivity != null) {
119       setResult();
120     }
121   }
122 
123   @Rpc(description = sRpcDescription)
setResultFloat( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Float resultValue)124   public synchronized void setResultFloat(
125       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
126       @RpcParameter(name = "resultValue") Float resultValue) {
127     mResult = new Intent();
128     mResult.putExtra(Constants.EXTRA_RESULT, resultValue.floatValue());
129     mResultCode = resultCode;
130     if (mActivity != null) {
131       setResult();
132     }
133   }
134 
135   @Rpc(description = sRpcDescription)
setResultDouble( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Double resultValue)136   public synchronized void setResultDouble(
137       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
138       @RpcParameter(name = "resultValue") Double resultValue) {
139     mResult = new Intent();
140     mResult.putExtra(Constants.EXTRA_RESULT, resultValue.doubleValue());
141     mResultCode = resultCode;
142     if (mActivity != null) {
143       setResult();
144     }
145   }
146 
147   @Rpc(description = sRpcDescription)
setResultString( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") String resultValue)148   public synchronized void setResultString(
149       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
150       @RpcParameter(name = "resultValue") String resultValue) {
151     mResult = new Intent();
152     mResult.putExtra(Constants.EXTRA_RESULT, resultValue);
153     mResultCode = resultCode;
154     if (mActivity != null) {
155       setResult();
156     }
157   }
158 
159   @Rpc(description = sRpcDescription)
setResultBooleanArray( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Boolean[] resultValue)160   public synchronized void setResultBooleanArray(
161       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
162       @RpcParameter(name = "resultValue") Boolean[] resultValue) {
163     mResult = new Intent();
164     boolean[] array = new boolean[resultValue.length];
165     for (int i = 0; i < resultValue.length; i++) {
166       array[i] = resultValue[i];
167     }
168     mResult.putExtra(Constants.EXTRA_RESULT, array);
169     mResultCode = resultCode;
170     if (mActivity != null) {
171       setResult();
172     }
173   }
174 
175   @Rpc(description = sRpcDescription)
setResultByteArray( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Byte[] resultValue)176   public synchronized void setResultByteArray(
177       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
178       @RpcParameter(name = "resultValue") Byte[] resultValue) {
179     mResult = new Intent();
180     byte[] array = new byte[resultValue.length];
181     for (int i = 0; i < resultValue.length; i++) {
182       array[i] = resultValue[i];
183     }
184     mResult.putExtra(Constants.EXTRA_RESULT, array);
185     mResultCode = resultCode;
186     if (mActivity != null) {
187       setResult();
188     }
189   }
190 
191   @Rpc(description = sRpcDescription)
setResultShortArray( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Short[] resultValue)192   public synchronized void setResultShortArray(
193       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
194       @RpcParameter(name = "resultValue") Short[] resultValue) {
195     mResult = new Intent();
196     short[] array = new short[resultValue.length];
197     for (int i = 0; i < resultValue.length; i++) {
198       array[i] = resultValue[i];
199     }
200     mResult.putExtra(Constants.EXTRA_RESULT, array);
201     mResultCode = resultCode;
202     if (mActivity != null) {
203       setResult();
204     }
205   }
206 
207   @Rpc(description = sRpcDescription)
setResultCharArray( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Character[] resultValue)208   public synchronized void setResultCharArray(
209       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
210       @RpcParameter(name = "resultValue") Character[] resultValue) {
211     mResult = new Intent();
212     char[] array = new char[resultValue.length];
213     for (int i = 0; i < resultValue.length; i++) {
214       array[i] = resultValue[i];
215     }
216     mResult.putExtra(Constants.EXTRA_RESULT, array);
217     mResultCode = resultCode;
218     if (mActivity != null) {
219       setResult();
220     }
221   }
222 
223   @Rpc(description = sRpcDescription)
setResultIntegerArray( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Integer[] resultValue)224   public synchronized void setResultIntegerArray(
225       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
226       @RpcParameter(name = "resultValue") Integer[] resultValue) {
227     mResult = new Intent();
228     int[] array = new int[resultValue.length];
229     for (int i = 0; i < resultValue.length; i++) {
230       array[i] = resultValue[i];
231     }
232     mResult.putExtra(Constants.EXTRA_RESULT, array);
233     mResultCode = resultCode;
234     if (mActivity != null) {
235       setResult();
236     }
237   }
238 
239   @Rpc(description = sRpcDescription)
setResultLongArray( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Long[] resultValue)240   public synchronized void setResultLongArray(
241       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
242       @RpcParameter(name = "resultValue") Long[] resultValue) {
243     mResult = new Intent();
244     long[] array = new long[resultValue.length];
245     for (int i = 0; i < resultValue.length; i++) {
246       array[i] = resultValue[i];
247     }
248     mResult.putExtra(Constants.EXTRA_RESULT, array);
249     mResultCode = resultCode;
250     if (mActivity != null) {
251       setResult();
252     }
253   }
254 
255   @Rpc(description = sRpcDescription)
setResultFloatArray( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Float[] resultValue)256   public synchronized void setResultFloatArray(
257       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
258       @RpcParameter(name = "resultValue") Float[] resultValue) {
259     mResult = new Intent();
260     float[] array = new float[resultValue.length];
261     for (int i = 0; i < resultValue.length; i++) {
262       array[i] = resultValue[i];
263     }
264     mResult.putExtra(Constants.EXTRA_RESULT, array);
265     mResultCode = resultCode;
266     if (mActivity != null) {
267       setResult();
268     }
269   }
270 
271   @Rpc(description = sRpcDescription)
setResultDoubleArray( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Double[] resultValue)272   public synchronized void setResultDoubleArray(
273       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
274       @RpcParameter(name = "resultValue") Double[] resultValue) {
275     mResult = new Intent();
276     double[] array = new double[resultValue.length];
277     for (int i = 0; i < resultValue.length; i++) {
278       array[i] = resultValue[i];
279     }
280     mResult.putExtra(Constants.EXTRA_RESULT, array);
281     mResultCode = resultCode;
282     if (mActivity != null) {
283       setResult();
284     }
285   }
286 
287   @Rpc(description = sRpcDescription)
setResultStringArray( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") String[] resultValue)288   public synchronized void setResultStringArray(
289       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
290       @RpcParameter(name = "resultValue") String[] resultValue) {
291     mResult = new Intent();
292     mResult.putExtra(Constants.EXTRA_RESULT, resultValue);
293     mResultCode = resultCode;
294     if (mActivity != null) {
295       setResult();
296     }
297   }
298 
299   @Rpc(description = sRpcDescription)
setResultSerializable( @pcParametername = "resultCode", description = sCodeDescription) Integer resultCode, @RpcParameter(name = "resultValue") Serializable resultValue)300   public synchronized void setResultSerializable(
301       @RpcParameter(name = "resultCode", description = sCodeDescription) Integer resultCode,
302       @RpcParameter(name = "resultValue") Serializable resultValue) {
303     mResult = new Intent();
304     mResult.putExtra(Constants.EXTRA_RESULT, resultValue);
305     mResultCode = resultCode;
306     if (mActivity != null) {
307       setResult();
308     }
309   }
310 
setActivity(Activity activity)311   public synchronized void setActivity(Activity activity) {
312     mActivity = activity;
313     if (mResult != null) {
314       setResult();
315     }
316   }
317 
setResult()318   private void setResult() {
319     mActivity.setResult(mResultCode, mResult);
320     mActivity.finish();
321   }
322 
323   @Override
shutdown()324   public void shutdown() {
325   }
326 }
327