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