- checkKey(key);
- return sPrefMap.get(key);
- }
-
- // TODO(yongsheng): I believe this is needed?
- /*public static synchronized void setValue(String key, int value) throws RuntimeException {
- }*/
-
- static synchronized void load(KeyValueChangeListener listener) {
- // Load current settings for initialization of a listener implementor.
- for (Map.Entry<String, Boolean> entry : sPrefMap.entrySet()) {
- listener.onKeyValueChanged(entry.getKey(), entry.getValue());
- }
-
- registerListener(listener);
- }
-
- static synchronized void unload(KeyValueChangeListener listener) {
- unregisterListener(listener);
- }
-
- // Listen to value changes.
- interface KeyValueChangeListener {
- public void onKeyValueChanged(String key, boolean value);
- }
-
- private static synchronized void registerListener(KeyValueChangeListener listener) {
- removeEnqueuedReference();
- WeakReference<KeyValueChangeListener> weakListener =
- new WeakReference<KeyValueChangeListener>(listener, sRefQueue);
- sListeners.add(weakListener);
- }
-
- private static synchronized void unregisterListener(KeyValueChangeListener listener) {
- removeEnqueuedReference();
- for (WeakReference<KeyValueChangeListener> weakListener : sListeners) {
- if (weakListener.get() == listener) {
- sListeners.remove(weakListener);
- break;
- }
- }
- }
-
- private static void onKeyValueChanged(String key, boolean enabled) {
- for (WeakReference<KeyValueChangeListener> weakListener : sListeners) {
- KeyValueChangeListener listener = weakListener.get();
- if (listener != null) listener.onKeyValueChanged(key, enabled);
- }
- }
-
- private static void checkKey(String key) throws RuntimeException {
- removeEnqueuedReference();
- if (!sPrefMap.containsKey(key)) {
- throw new RuntimeException("Warning: the preference key " + key +
- " is not supported by Crosswalk.");
- }
- }
-
- /**
- * Internal method to keep track of weak references and remove the enqueued
- * references from listener list by polling the reference queue.
- */
- @SuppressWarnings("unchecked")
- private static void removeEnqueuedReference() {
- WeakReference<KeyValueChangeListener> toRemove;
- while ((toRemove = (WeakReference<KeyValueChangeListener>) sRefQueue.poll()) != null) {
- sListeners.remove(toRemove);
- }