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