Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.background.common.GlobalConstants; |
michael@0 | 9 | import org.mozilla.gecko.EventDispatcher; |
michael@0 | 10 | import org.mozilla.gecko.util.GeckoEventListener; |
michael@0 | 11 | |
michael@0 | 12 | import org.json.JSONException; |
michael@0 | 13 | import org.json.JSONObject; |
michael@0 | 14 | |
michael@0 | 15 | import android.app.Activity; |
michael@0 | 16 | |
michael@0 | 17 | import android.content.BroadcastReceiver; |
michael@0 | 18 | import android.content.Context; |
michael@0 | 19 | import android.content.Intent; |
michael@0 | 20 | |
michael@0 | 21 | import android.util.Log; |
michael@0 | 22 | |
michael@0 | 23 | /** |
michael@0 | 24 | * Helper class to send Android Ordered Broadcasts. |
michael@0 | 25 | */ |
michael@0 | 26 | public final class OrderedBroadcastHelper |
michael@0 | 27 | implements GeckoEventListener |
michael@0 | 28 | { |
michael@0 | 29 | public static final String LOGTAG = "GeckoOrdBroadcast"; |
michael@0 | 30 | |
michael@0 | 31 | public static final String SEND_EVENT = "OrderedBroadcast:Send"; |
michael@0 | 32 | |
michael@0 | 33 | protected final Context mContext; |
michael@0 | 34 | |
michael@0 | 35 | public OrderedBroadcastHelper(Context context) { |
michael@0 | 36 | mContext = context; |
michael@0 | 37 | |
michael@0 | 38 | EventDispatcher dispatcher = GeckoAppShell.getEventDispatcher(); |
michael@0 | 39 | if (dispatcher == null) { |
michael@0 | 40 | Log.e(LOGTAG, "Gecko event dispatcher must not be null", new RuntimeException()); |
michael@0 | 41 | return; |
michael@0 | 42 | } |
michael@0 | 43 | dispatcher.registerEventListener(SEND_EVENT, this); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | public synchronized void uninit() { |
michael@0 | 47 | EventDispatcher dispatcher = GeckoAppShell.getEventDispatcher(); |
michael@0 | 48 | if (dispatcher == null) { |
michael@0 | 49 | Log.e(LOGTAG, "Gecko event dispatcher must not be null", new RuntimeException()); |
michael@0 | 50 | return; |
michael@0 | 51 | } |
michael@0 | 52 | dispatcher.unregisterEventListener(SEND_EVENT, this); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | @Override |
michael@0 | 56 | public void handleMessage(String event, JSONObject message) { |
michael@0 | 57 | if (!SEND_EVENT.equals(event)) { |
michael@0 | 58 | Log.e(LOGTAG, "OrderedBroadcastHelper got unexpected message " + event); |
michael@0 | 59 | return; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | try { |
michael@0 | 63 | final String action = message.getString("action"); |
michael@0 | 64 | if (action == null) { |
michael@0 | 65 | Log.e(LOGTAG, "action must not be null"); |
michael@0 | 66 | return; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | final String responseEvent = message.getString("responseEvent"); |
michael@0 | 70 | if (responseEvent == null) { |
michael@0 | 71 | Log.e(LOGTAG, "responseEvent must not be null"); |
michael@0 | 72 | return; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | // It's fine if the caller-provided token is missing or null. |
michael@0 | 76 | final JSONObject token = (message.has("token") && !message.isNull("token")) ? |
michael@0 | 77 | message.getJSONObject("token") : null; |
michael@0 | 78 | |
michael@0 | 79 | // A missing (undefined) permission means the intent will be limited |
michael@0 | 80 | // to the current package. A null means no permission, so any |
michael@0 | 81 | // package can receive the intent. |
michael@0 | 82 | final String permission = message.has("permission") ? |
michael@0 | 83 | (message.isNull("permission") ? null : message.getString("permission")) : |
michael@0 | 84 | GlobalConstants.PER_ANDROID_PACKAGE_PERMISSION; |
michael@0 | 85 | |
michael@0 | 86 | final BroadcastReceiver resultReceiver = new BroadcastReceiver() { |
michael@0 | 87 | @Override |
michael@0 | 88 | public void onReceive(Context context, Intent intent) { |
michael@0 | 89 | int code = getResultCode(); |
michael@0 | 90 | |
michael@0 | 91 | if (code == Activity.RESULT_OK) { |
michael@0 | 92 | String data = getResultData(); |
michael@0 | 93 | |
michael@0 | 94 | JSONObject res = new JSONObject(); |
michael@0 | 95 | try { |
michael@0 | 96 | res.put("action", action); |
michael@0 | 97 | res.put("token", token); |
michael@0 | 98 | res.put("data", data); |
michael@0 | 99 | } catch (JSONException e) { |
michael@0 | 100 | Log.e(LOGTAG, "Got exception in onReceive handling action " + action, e); |
michael@0 | 101 | return; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | GeckoEvent event = GeckoEvent.createBroadcastEvent(responseEvent, res.toString()); |
michael@0 | 105 | GeckoAppShell.sendEventToGecko(event); |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | }; |
michael@0 | 109 | |
michael@0 | 110 | Intent intent = new Intent(action); |
michael@0 | 111 | // OrderedBroadcast.jsm adds its callback ID to the caller's token; |
michael@0 | 112 | // this unwraps that wrapping. |
michael@0 | 113 | if (token != null && token.has("data")) { |
michael@0 | 114 | intent.putExtra("token", token.getString("data")); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | mContext.sendOrderedBroadcast(intent, |
michael@0 | 118 | permission, |
michael@0 | 119 | resultReceiver, |
michael@0 | 120 | null, |
michael@0 | 121 | Activity.RESULT_OK, |
michael@0 | 122 | null, |
michael@0 | 123 | null); |
michael@0 | 124 | } catch (JSONException e) { |
michael@0 | 125 | Log.e(LOGTAG, "Got exception in handleMessage handling event " + event, e); |
michael@0 | 126 | return; |
michael@0 | 127 | } |
michael@0 | 128 | } |
michael@0 | 129 | } |