6 package com.google.appinventor.components.runtime.util;
8 import android.util.Log;
13 import java.io.BufferedInputStream;
14 import java.io.BufferedOutputStream;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
19 import java.net.HttpURLConnection;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.Executors;
27 import org.json.JSONArray;
28 import org.json.JSONException;
52 private static final String LOG_TAG =
AssetFetcher.class.getSimpleName();
55 private static ExecutorService background = Executors.newSingleThreadExecutor();
57 private static volatile boolean inError =
false;
58 private static final Object semaphore =
new Object();
65 final String projectId,
final String uri,
final String asset) {
66 background.submit(
new Runnable() {
69 String fileName = uri +
"/ode/download/file/" + projectId +
"/" + asset;
70 if (getFile(fileName, cookieValue, asset, 0) !=
null) {
105 Log.d(LOG_TAG,
"loadExtensions called jsonString = " + jsonString);
108 JSONArray array =
new JSONArray(jsonString);
109 List<String> extensionsToLoad =
new ArrayList<String>();
110 if (array.length() == 0) {
111 Log.d(LOG_TAG,
"loadExtensions: No Extensions");
115 for (
int i = 0; i < array.length(); i++) {
116 String extensionName = array.optString(i);
117 if (extensionName !=
null) {
118 Log.d(LOG_TAG,
"loadExtensions, extensionName = " + extensionName);
119 extensionsToLoad.add(extensionName);
121 Log.e(LOG_TAG,
"extensionName was null");
128 }
catch (Exception e) {
129 Log.e(LOG_TAG,
"Error in form.loadComponents", e);
131 }
catch (JSONException e) {
132 Log.e(LOG_TAG,
"JSON Exception parsing extension string", e);
136 private static File getFile(
final String fileName, String cookieValue, String asset,
int depth) {
139 synchronized (semaphore) {
144 form.runOnUiThread(
new Runnable() {
146 RuntimeErrorAlert.alert(
Form.
getActiveForm(),
"Unable to load file: " + fileName,
147 "Error!",
"End Application");
155 boolean error =
false;
157 URL url =
new URL(fileName);
158 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
159 if (connection !=
null) {
160 connection.setRequestMethod(
"GET");
161 connection.addRequestProperty(
"Cookie",
"AppInventor = " + cookieValue);
162 int responseCode = connection.getResponseCode();
163 Log.d(LOG_TAG,
"asset = " + asset +
" responseCode = " + responseCode);
164 outFile =
new File(QUtil.getReplAssetPath(form), asset.substring(
"assets/".length()));
165 File parentOutFile = outFile.getParentFile();
166 if (!parentOutFile.exists() && !parentOutFile.mkdirs()) {
167 throw new IOException(
"Unable to create assets directory " + parentOutFile);
169 BufferedInputStream in =
new BufferedInputStream(connection.getInputStream(), 0x1000);
170 BufferedOutputStream out =
new BufferedOutputStream(
new FileOutputStream(outFile), 0x1000);
180 }
catch (IOException e) {
181 Log.e(LOG_TAG,
"copying assets", e);
186 connection.disconnect();
191 return getFile(fileName, cookieValue, asset, depth + 1);
194 }
catch (Exception e) {
195 Log.e(LOG_TAG,
"Exception while fetching " + fileName, e);
197 return getFile(fileName, cookieValue, asset, depth + 1);