diff --git a/mobile/android/base/webapp/EventListener.java b/mobile/android/base/webapp/EventListener.java index a1091a7cda6f..08777a9f5e05 100644 --- a/mobile/android/base/webapp/EventListener.java +++ b/mobile/android/base/webapp/EventListener.java @@ -8,12 +8,15 @@ package org.mozilla.gecko.webapp; import org.mozilla.gecko.ActivityHandlerHelper; import org.mozilla.gecko.AppConstants; import org.mozilla.gecko.GeckoAppShell; +import org.mozilla.gecko.GeckoEvent; import org.mozilla.gecko.GeckoProfile; import org.mozilla.gecko.favicons.decoders.FaviconDecoder; import org.mozilla.gecko.gfx.BitmapUtils; import org.mozilla.gecko.util.ActivityResultHandler; import org.mozilla.gecko.EventDispatcher; -import org.mozilla.gecko.util.GeckoEventListener; +import org.mozilla.gecko.util.EventCallback; +import org.mozilla.gecko.util.NativeEventListener; +import org.mozilla.gecko.util.NativeJSObject; import org.mozilla.gecko.util.ThreadUtils; import org.mozilla.gecko.WebappAllocator; @@ -30,15 +33,15 @@ import android.net.Uri; import android.util.Log; import java.io.File; +import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; -import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -public class EventListener implements GeckoEventListener { +public class EventListener implements NativeEventListener { private static final String LOGTAG = "GeckoWebappEventListener"; @@ -63,10 +66,10 @@ public class EventListener implements GeckoEventListener { } @Override - public void handleMessage(String event, JSONObject message) { + public void handleMessage(String event, NativeJSObject message, EventCallback callback) { try { if (AppConstants.MOZ_ANDROID_SYNTHAPKS && event.equals("Webapps:InstallApk")) { - installApk(GeckoAppShell.getGeckoInterface().getActivity(), message.getString("filePath"), message.getString("data")); + installApk(GeckoAppShell.getGeckoInterface().getActivity(), message, callback); } else if (event.equals("Webapps:Postinstall")) { if (AppConstants.MOZ_ANDROID_SYNTHAPKS) { postInstallWebapp(message.getString("apkPackageName"), message.getString("origin")); @@ -94,12 +97,12 @@ public class EventListener implements GeckoEventListener { JSONObject obj = new JSONObject(); obj.put("profile", preInstallWebapp(name, manifestURL, origin).toString()); - EventDispatcher.sendResponse(message, obj); + callback.sendSuccess(obj); } else if (event.equals("Webapps:GetApkVersions")) { JSONObject obj = new JSONObject(); obj.put("versions", getApkVersions(GeckoAppShell.getGeckoInterface().getActivity(), - message.getJSONArray("packageNames"))); - EventDispatcher.sendResponse(message, obj); + message.getStringArray("packageNames"))); + callback.sendSuccess(obj); } } catch (Exception e) { Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e); @@ -180,25 +183,27 @@ public class EventListener implements GeckoEventListener { } } - public static void installApk(final Activity context, String filePath, String data) { - // This is the data that mozApps.install sent to Webapps.jsm. - JSONObject argsObj = null; + public static void installApk(final Activity context, NativeJSObject message, EventCallback callback) { + final JSONObject messageData; // We get the manifest url out of javascript here so we can use it as a checksum // in a minute, when a package has been installed. String manifestUrl = null; + String filePath = null; + try { - argsObj = new JSONObject(data); - manifestUrl = argsObj.getJSONObject("app").getString("manifestURL"); + filePath = message.getString("filePath"); + messageData = new JSONObject(message.getObject("data").toString()); + manifestUrl = messageData.getJSONObject("app").getString("manifestURL"); } catch (JSONException e) { - Log.e(LOGTAG, "can't get manifest URL from JSON data", e); - // TODO: propagate the error back to the mozApps.install caller. + Log.wtf(LOGTAG, "Error getting file path and data", e); + callback.sendError("Error getting file path and data: " + e.toString()); return; } // We will check the manifestUrl from the one in the APK. // Thus, we can have a one-to-one mapping of apk to receiver. - final InstallListener receiver = new InstallListener(manifestUrl, argsObj); + final InstallListener receiver = new InstallListener(manifestUrl, messageData); // Listen for packages being installed. IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); @@ -208,7 +213,7 @@ public class EventListener implements GeckoEventListener { File file = new File(filePath); if (!file.exists()) { Log.wtf(LOGTAG, "APK file doesn't exist at path " + filePath); - // TODO: propagate the error back to the mozApps.install caller. + callback.sendError("APK file doesn't exist at path " + filePath); return; } @@ -239,15 +244,9 @@ public class EventListener implements GeckoEventListener { private static final int DEFAULT_VERSION_CODE = -1; - public static JSONObject getApkVersions(Activity context, JSONArray packageNames) { + public static JSONObject getApkVersions(Activity context, String[] packageNames) { Set packageNameSet = new HashSet(); - for (int i = 0; i < packageNames.length(); i++) { - try { - packageNameSet.add(packageNames.getString(i)); - } catch (JSONException e) { - Log.w(LOGTAG, "exception populating settings item", e); - } - } + packageNameSet.addAll(Arrays.asList(packageNames)); final PackageManager pm = context.getPackageManager(); List apps = pm.getInstalledApplications(0); diff --git a/mobile/android/modules/WebappManager.jsm b/mobile/android/modules/WebappManager.jsm index 967a12c98c39..970e2651ba5d 100644 --- a/mobile/android/modules/WebappManager.jsm +++ b/mobile/android/modules/WebappManager.jsm @@ -103,7 +103,13 @@ this.WebappManager = { sendMessageToJava({ type: "Webapps:InstallApk", filePath: filePath, - data: JSON.stringify(aMessage), + data: aMessage, + }, (data, error) => { + if (!!error) { + aMessage.error = error; + aMessageManager.sendAsyncMessage("Webapps:Install:Return:KO", aMessage); + debug("error downloading APK: " + error); + } }); }).bind(this)); }, @@ -493,7 +499,7 @@ this.WebappManager = { sendMessageToJava({ type: "Webapps:InstallApk", filePath: apk.filePath, - data: JSON.stringify(msg), + data: msg, }); } } else {