7 package com.google.appinventor.components.runtime.util;
9 import android.util.Log;
14 import java.util.ArrayList;
16 import org.json.JSONArray;
17 import org.json.JSONException;
18 import org.json.JSONObject;
29 private static final String LOG_TAG =
"RetValManager";
30 private static final Object semaphore =
new Object();
31 private static final long TENSECONDS = 10000;
34 private static ArrayList<JSONObject> currentArray =
new ArrayList<JSONObject>(10);
52 synchronized (semaphore) {
53 JSONObject retval =
new JSONObject();
55 retval.put(
"status", ok);
56 retval.put(
"type",
"return");
57 retval.put(
"value", item);
58 retval.put(
"blockid", blockid);
59 }
catch (JSONException e) {
60 Log.e(LOG_TAG,
"Error building retval", e);
63 boolean sendNotify = currentArray.isEmpty();
64 currentArray.add(retval);
67 }
else if (sendNotify) {
68 semaphore.notifyAll();
74 synchronized (semaphore) {
75 JSONObject retval =
new JSONObject();
77 retval.put(
"status",
"OK");
78 retval.put(
"type",
"error");
79 retval.put(
"value", error);
80 }
catch (JSONException e) {
81 Log.e(LOG_TAG,
"Error building retval", e);
84 boolean sendNotify = currentArray.isEmpty();
85 currentArray.add(retval);
88 }
else if (sendNotify) {
89 semaphore.notifyAll();
100 public static void pushScreen(String screenName, Object value) {
101 synchronized (semaphore) {
102 JSONObject retval =
new JSONObject();
104 retval.put(
"status",
"OK");
105 retval.put(
"type",
"pushScreen");
106 retval.put(
"screen", screenName);
108 retval.put(
"value", value.toString());
109 }
catch (JSONException e) {
110 Log.e(LOG_TAG,
"Error building retval", e);
113 boolean sendNotify = currentArray.isEmpty();
114 currentArray.add(retval);
117 }
else if (sendNotify) {
118 semaphore.notifyAll();
130 synchronized (semaphore) {
131 JSONObject retval =
new JSONObject();
133 retval.put(
"status",
"OK");
134 retval.put(
"type",
"popScreen");
136 retval.put(
"value", value.toString());
137 }
catch (JSONException e) {
138 Log.e(LOG_TAG,
"Error building retval", e);
141 boolean sendNotify = currentArray.isEmpty();
142 currentArray.add(retval);
145 }
else if (sendNotify) {
146 semaphore.notifyAll();
157 synchronized (semaphore) {
158 JSONObject retval =
new JSONObject();
160 retval.put(
"status",
"OK");
161 retval.put(
"type",
"assetTransferred");
163 retval.put(
"value", name.toString());
164 }
catch (JSONException e) {
165 Log.e(LOG_TAG,
"Error building retval", e);
168 boolean sendNotify = currentArray.isEmpty();
169 currentArray.add(retval);
172 }
else if (sendNotify) {
173 semaphore.notifyAll();
183 synchronized (semaphore) {
184 JSONObject retval =
new JSONObject();
186 retval.put(
"status",
"OK");
187 retval.put(
"type",
"extensionsLoaded");
188 }
catch (JSONException e) {
189 Log.e(LOG_TAG,
"Error building retval", e);
192 boolean sendNotify = currentArray.isEmpty();
193 currentArray.add(retval);
196 }
else if (sendNotify) {
197 semaphore.notifyAll();
210 public static String
fetch(
boolean block) {
211 long startTime = System.currentTimeMillis();
212 synchronized (semaphore) {
213 while (currentArray.isEmpty() && block) {
214 long time = System.currentTimeMillis();
215 if ((time - startTime) > (TENSECONDS - 100))
218 semaphore.wait(TENSECONDS);
219 }
catch (InterruptedException e) {
222 JSONArray arrayoutput =
new JSONArray(currentArray);
223 JSONObject output =
new JSONObject();
225 output.put(
"status",
"OK");
226 output.put(
"values", arrayoutput);
227 }
catch (JSONException e) {
228 Log.e(LOG_TAG,
"Error fetching retvals", e);
229 return(
"{\"status\" : \"BAD\", \"message\" : \"Failure in RetValManager\"}");
231 currentArray.clear();
232 return output.toString();
238 private static void webRTCsendCurrent() {
240 JSONObject output =
new JSONObject();
241 output.put(
"status",
"OK");
242 output.put(
"values",
new JSONArray(currentArray));
244 }
catch (JSONException e) {
245 Log.e(LOG_TAG,
"Error building retval", e);
248 currentArray.clear();