Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
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.util.GeckoEventListener; |
michael@0 | 9 | |
michael@0 | 10 | import org.json.JSONArray; |
michael@0 | 11 | import org.json.JSONException; |
michael@0 | 12 | import org.json.JSONObject; |
michael@0 | 13 | |
michael@0 | 14 | import android.util.Log; |
michael@0 | 15 | import android.util.SparseArray; |
michael@0 | 16 | |
michael@0 | 17 | import java.util.ArrayList; |
michael@0 | 18 | |
michael@0 | 19 | /** |
michael@0 | 20 | * Helper class to get/set gecko prefs. |
michael@0 | 21 | */ |
michael@0 | 22 | public final class PrefsHelper { |
michael@0 | 23 | private static final String LOGTAG = "GeckoPrefsHelper"; |
michael@0 | 24 | |
michael@0 | 25 | private static boolean sRegistered = false; |
michael@0 | 26 | private static final SparseArray<PrefHandler> sCallbacks = new SparseArray<PrefHandler>(); |
michael@0 | 27 | private static int sUniqueRequestId = 1; |
michael@0 | 28 | |
michael@0 | 29 | public static int getPref(String prefName, PrefHandler callback) { |
michael@0 | 30 | return getPrefsInternal(new String[] { prefName }, callback); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | public static int getPrefs(String[] prefNames, PrefHandler callback) { |
michael@0 | 34 | return getPrefsInternal(prefNames, callback); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | public static int getPrefs(ArrayList<String> prefNames, PrefHandler callback) { |
michael@0 | 38 | return getPrefsInternal(prefNames.toArray(new String[prefNames.size()]), callback); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | private static int getPrefsInternal(String[] prefNames, PrefHandler callback) { |
michael@0 | 42 | int requestId; |
michael@0 | 43 | synchronized (PrefsHelper.class) { |
michael@0 | 44 | ensureRegistered(); |
michael@0 | 45 | |
michael@0 | 46 | requestId = sUniqueRequestId++; |
michael@0 | 47 | sCallbacks.put(requestId, callback); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | GeckoEvent event; |
michael@0 | 51 | if (callback.isObserver()) { |
michael@0 | 52 | event = GeckoEvent.createPreferencesObserveEvent(requestId, prefNames); |
michael@0 | 53 | } else { |
michael@0 | 54 | event = GeckoEvent.createPreferencesGetEvent(requestId, prefNames); |
michael@0 | 55 | } |
michael@0 | 56 | GeckoAppShell.sendEventToGecko(event); |
michael@0 | 57 | |
michael@0 | 58 | return requestId; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | private static void ensureRegistered() { |
michael@0 | 62 | if (sRegistered) { |
michael@0 | 63 | return; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | GeckoAppShell.getEventDispatcher().registerEventListener("Preferences:Data", new GeckoEventListener() { |
michael@0 | 67 | @Override public void handleMessage(String event, JSONObject message) { |
michael@0 | 68 | try { |
michael@0 | 69 | PrefHandler callback; |
michael@0 | 70 | synchronized (PrefsHelper.class) { |
michael@0 | 71 | try { |
michael@0 | 72 | int requestId = message.getInt("requestId"); |
michael@0 | 73 | callback = sCallbacks.get(requestId); |
michael@0 | 74 | if (callback != null && !callback.isObserver()) { |
michael@0 | 75 | sCallbacks.delete(requestId); |
michael@0 | 76 | } |
michael@0 | 77 | } catch (Exception e) { |
michael@0 | 78 | callback = null; |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | if (callback == null) { |
michael@0 | 82 | Log.d(LOGTAG, "Preferences:Data message had an unknown requestId; ignoring"); |
michael@0 | 83 | return; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | JSONArray jsonPrefs = message.getJSONArray("preferences"); |
michael@0 | 87 | for (int i = 0; i < jsonPrefs.length(); i++) { |
michael@0 | 88 | JSONObject pref = jsonPrefs.getJSONObject(i); |
michael@0 | 89 | String name = pref.getString("name"); |
michael@0 | 90 | String type = pref.getString("type"); |
michael@0 | 91 | try { |
michael@0 | 92 | if ("bool".equals(type)) { |
michael@0 | 93 | callback.prefValue(name, pref.getBoolean("value")); |
michael@0 | 94 | } else if ("int".equals(type)) { |
michael@0 | 95 | callback.prefValue(name, pref.getInt("value")); |
michael@0 | 96 | } else if ("string".equals(type)) { |
michael@0 | 97 | callback.prefValue(name, pref.getString("value")); |
michael@0 | 98 | } else { |
michael@0 | 99 | Log.e(LOGTAG, "Unknown pref value type [" + type + "] for pref [" + name + "]"); |
michael@0 | 100 | } |
michael@0 | 101 | } catch (Exception e) { |
michael@0 | 102 | Log.e(LOGTAG, "Handler for preference [" + name + "] threw exception", e); |
michael@0 | 103 | } |
michael@0 | 104 | } |
michael@0 | 105 | callback.finish(); |
michael@0 | 106 | } catch (Exception e) { |
michael@0 | 107 | Log.e(LOGTAG, "Error handling Preferences:Data message", e); |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | }); |
michael@0 | 111 | sRegistered = true; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | public static void setPref(String pref, Object value) { |
michael@0 | 115 | if (pref == null || pref.length() == 0) { |
michael@0 | 116 | throw new IllegalArgumentException("Pref name must be non-empty"); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | try { |
michael@0 | 120 | JSONObject jsonPref = new JSONObject(); |
michael@0 | 121 | jsonPref.put("name", pref); |
michael@0 | 122 | if (value instanceof Boolean) { |
michael@0 | 123 | jsonPref.put("type", "bool"); |
michael@0 | 124 | jsonPref.put("value", ((Boolean)value).booleanValue()); |
michael@0 | 125 | } else if (value instanceof Integer) { |
michael@0 | 126 | jsonPref.put("type", "int"); |
michael@0 | 127 | jsonPref.put("value", ((Integer)value).intValue()); |
michael@0 | 128 | } else { |
michael@0 | 129 | jsonPref.put("type", "string"); |
michael@0 | 130 | jsonPref.put("value", String.valueOf(value)); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | GeckoEvent event = GeckoEvent.createBroadcastEvent("Preferences:Set", jsonPref.toString()); |
michael@0 | 134 | GeckoAppShell.sendEventToGecko(event); |
michael@0 | 135 | } catch (JSONException e) { |
michael@0 | 136 | Log.e(LOGTAG, "Error setting pref [" + pref + "]", e); |
michael@0 | 137 | } |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | public static void removeObserver(int requestId) { |
michael@0 | 141 | if (requestId < 0) { |
michael@0 | 142 | throw new IllegalArgumentException("Invalid request ID"); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | synchronized (PrefsHelper.class) { |
michael@0 | 146 | PrefHandler callback = sCallbacks.get(requestId); |
michael@0 | 147 | sCallbacks.delete(requestId); |
michael@0 | 148 | |
michael@0 | 149 | if (callback == null) { |
michael@0 | 150 | Log.e(LOGTAG, "Unknown request ID " + requestId); |
michael@0 | 151 | return; |
michael@0 | 152 | } |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | GeckoEvent event = GeckoEvent.createBroadcastEvent("Preferences:RemoveObserver", |
michael@0 | 156 | Integer.toString(requestId)); |
michael@0 | 157 | GeckoAppShell.sendEventToGecko(event); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | public interface PrefHandler { |
michael@0 | 161 | void prefValue(String pref, boolean value); |
michael@0 | 162 | void prefValue(String pref, int value); |
michael@0 | 163 | void prefValue(String pref, String value); |
michael@0 | 164 | boolean isObserver(); |
michael@0 | 165 | void finish(); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | public static abstract class PrefHandlerBase implements PrefHandler { |
michael@0 | 169 | @Override |
michael@0 | 170 | public void prefValue(String pref, boolean value) { |
michael@0 | 171 | Log.w(LOGTAG, "Unhandled boolean value for pref [" + pref + "]"); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | @Override |
michael@0 | 175 | public void prefValue(String pref, int value) { |
michael@0 | 176 | Log.w(LOGTAG, "Unhandled int value for pref [" + pref + "]"); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | @Override |
michael@0 | 180 | public void prefValue(String pref, String value) { |
michael@0 | 181 | Log.w(LOGTAG, "Unhandled String value for pref [" + pref + "]"); |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | @Override |
michael@0 | 185 | public void finish() { |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | @Override |
michael@0 | 189 | public boolean isObserver() { |
michael@0 | 190 | return false; |
michael@0 | 191 | } |
michael@0 | 192 | } |
michael@0 | 193 | } |