1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/PrefsHelper.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,193 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko; 1.10 + 1.11 +import org.mozilla.gecko.util.GeckoEventListener; 1.12 + 1.13 +import org.json.JSONArray; 1.14 +import org.json.JSONException; 1.15 +import org.json.JSONObject; 1.16 + 1.17 +import android.util.Log; 1.18 +import android.util.SparseArray; 1.19 + 1.20 +import java.util.ArrayList; 1.21 + 1.22 +/** 1.23 + * Helper class to get/set gecko prefs. 1.24 + */ 1.25 +public final class PrefsHelper { 1.26 + private static final String LOGTAG = "GeckoPrefsHelper"; 1.27 + 1.28 + private static boolean sRegistered = false; 1.29 + private static final SparseArray<PrefHandler> sCallbacks = new SparseArray<PrefHandler>(); 1.30 + private static int sUniqueRequestId = 1; 1.31 + 1.32 + public static int getPref(String prefName, PrefHandler callback) { 1.33 + return getPrefsInternal(new String[] { prefName }, callback); 1.34 + } 1.35 + 1.36 + public static int getPrefs(String[] prefNames, PrefHandler callback) { 1.37 + return getPrefsInternal(prefNames, callback); 1.38 + } 1.39 + 1.40 + public static int getPrefs(ArrayList<String> prefNames, PrefHandler callback) { 1.41 + return getPrefsInternal(prefNames.toArray(new String[prefNames.size()]), callback); 1.42 + } 1.43 + 1.44 + private static int getPrefsInternal(String[] prefNames, PrefHandler callback) { 1.45 + int requestId; 1.46 + synchronized (PrefsHelper.class) { 1.47 + ensureRegistered(); 1.48 + 1.49 + requestId = sUniqueRequestId++; 1.50 + sCallbacks.put(requestId, callback); 1.51 + } 1.52 + 1.53 + GeckoEvent event; 1.54 + if (callback.isObserver()) { 1.55 + event = GeckoEvent.createPreferencesObserveEvent(requestId, prefNames); 1.56 + } else { 1.57 + event = GeckoEvent.createPreferencesGetEvent(requestId, prefNames); 1.58 + } 1.59 + GeckoAppShell.sendEventToGecko(event); 1.60 + 1.61 + return requestId; 1.62 + } 1.63 + 1.64 + private static void ensureRegistered() { 1.65 + if (sRegistered) { 1.66 + return; 1.67 + } 1.68 + 1.69 + GeckoAppShell.getEventDispatcher().registerEventListener("Preferences:Data", new GeckoEventListener() { 1.70 + @Override public void handleMessage(String event, JSONObject message) { 1.71 + try { 1.72 + PrefHandler callback; 1.73 + synchronized (PrefsHelper.class) { 1.74 + try { 1.75 + int requestId = message.getInt("requestId"); 1.76 + callback = sCallbacks.get(requestId); 1.77 + if (callback != null && !callback.isObserver()) { 1.78 + sCallbacks.delete(requestId); 1.79 + } 1.80 + } catch (Exception e) { 1.81 + callback = null; 1.82 + } 1.83 + } 1.84 + if (callback == null) { 1.85 + Log.d(LOGTAG, "Preferences:Data message had an unknown requestId; ignoring"); 1.86 + return; 1.87 + } 1.88 + 1.89 + JSONArray jsonPrefs = message.getJSONArray("preferences"); 1.90 + for (int i = 0; i < jsonPrefs.length(); i++) { 1.91 + JSONObject pref = jsonPrefs.getJSONObject(i); 1.92 + String name = pref.getString("name"); 1.93 + String type = pref.getString("type"); 1.94 + try { 1.95 + if ("bool".equals(type)) { 1.96 + callback.prefValue(name, pref.getBoolean("value")); 1.97 + } else if ("int".equals(type)) { 1.98 + callback.prefValue(name, pref.getInt("value")); 1.99 + } else if ("string".equals(type)) { 1.100 + callback.prefValue(name, pref.getString("value")); 1.101 + } else { 1.102 + Log.e(LOGTAG, "Unknown pref value type [" + type + "] for pref [" + name + "]"); 1.103 + } 1.104 + } catch (Exception e) { 1.105 + Log.e(LOGTAG, "Handler for preference [" + name + "] threw exception", e); 1.106 + } 1.107 + } 1.108 + callback.finish(); 1.109 + } catch (Exception e) { 1.110 + Log.e(LOGTAG, "Error handling Preferences:Data message", e); 1.111 + } 1.112 + } 1.113 + }); 1.114 + sRegistered = true; 1.115 + } 1.116 + 1.117 + public static void setPref(String pref, Object value) { 1.118 + if (pref == null || pref.length() == 0) { 1.119 + throw new IllegalArgumentException("Pref name must be non-empty"); 1.120 + } 1.121 + 1.122 + try { 1.123 + JSONObject jsonPref = new JSONObject(); 1.124 + jsonPref.put("name", pref); 1.125 + if (value instanceof Boolean) { 1.126 + jsonPref.put("type", "bool"); 1.127 + jsonPref.put("value", ((Boolean)value).booleanValue()); 1.128 + } else if (value instanceof Integer) { 1.129 + jsonPref.put("type", "int"); 1.130 + jsonPref.put("value", ((Integer)value).intValue()); 1.131 + } else { 1.132 + jsonPref.put("type", "string"); 1.133 + jsonPref.put("value", String.valueOf(value)); 1.134 + } 1.135 + 1.136 + GeckoEvent event = GeckoEvent.createBroadcastEvent("Preferences:Set", jsonPref.toString()); 1.137 + GeckoAppShell.sendEventToGecko(event); 1.138 + } catch (JSONException e) { 1.139 + Log.e(LOGTAG, "Error setting pref [" + pref + "]", e); 1.140 + } 1.141 + } 1.142 + 1.143 + public static void removeObserver(int requestId) { 1.144 + if (requestId < 0) { 1.145 + throw new IllegalArgumentException("Invalid request ID"); 1.146 + } 1.147 + 1.148 + synchronized (PrefsHelper.class) { 1.149 + PrefHandler callback = sCallbacks.get(requestId); 1.150 + sCallbacks.delete(requestId); 1.151 + 1.152 + if (callback == null) { 1.153 + Log.e(LOGTAG, "Unknown request ID " + requestId); 1.154 + return; 1.155 + } 1.156 + } 1.157 + 1.158 + GeckoEvent event = GeckoEvent.createBroadcastEvent("Preferences:RemoveObserver", 1.159 + Integer.toString(requestId)); 1.160 + GeckoAppShell.sendEventToGecko(event); 1.161 + } 1.162 + 1.163 + public interface PrefHandler { 1.164 + void prefValue(String pref, boolean value); 1.165 + void prefValue(String pref, int value); 1.166 + void prefValue(String pref, String value); 1.167 + boolean isObserver(); 1.168 + void finish(); 1.169 + } 1.170 + 1.171 + public static abstract class PrefHandlerBase implements PrefHandler { 1.172 + @Override 1.173 + public void prefValue(String pref, boolean value) { 1.174 + Log.w(LOGTAG, "Unhandled boolean value for pref [" + pref + "]"); 1.175 + } 1.176 + 1.177 + @Override 1.178 + public void prefValue(String pref, int value) { 1.179 + Log.w(LOGTAG, "Unhandled int value for pref [" + pref + "]"); 1.180 + } 1.181 + 1.182 + @Override 1.183 + public void prefValue(String pref, String value) { 1.184 + Log.w(LOGTAG, "Unhandled String value for pref [" + pref + "]"); 1.185 + } 1.186 + 1.187 + @Override 1.188 + public void finish() { 1.189 + } 1.190 + 1.191 + @Override 1.192 + public boolean isObserver() { 1.193 + return false; 1.194 + } 1.195 + } 1.196 +}