1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.core;
7 import java.util.ArrayList;
8 import java.util.HashMap;
12 * This class is not thread-safe and must be called on the UI thread.
13 * Afterwards, the preference could be read from all threads and can impact
14 * all XWalkView instances.
16 public final class XWalkPreferences {
17 private static HashMap<String, Boolean> sPrefMap = new HashMap<String, Boolean>();
18 private static ArrayList<KeyValueChangeListener> sListeners =
19 new ArrayList<KeyValueChangeListener>();
22 * The key string to enable/disable remote debugging.
24 public static final String REMOTE_DEBUGGING = "remote-debugging";
27 sPrefMap.put(REMOTE_DEBUGGING, Boolean.FALSE);
31 * Set a preference value into Crosswalk. An exception will be thrown if
32 * the key for the preference is not valid.
33 * @param key the string name of the key.
34 * @param enabled true if setting it as enabled.
36 public static synchronized void setValue(String key, boolean enabled) throws RuntimeException {
38 if (sPrefMap.get(key) != enabled) {
39 sPrefMap.put(key, new Boolean(enabled));
40 onKeyValueChanged(key, enabled);
45 * Get a preference value into Crosswalk. An exception will be thrown if
46 * the key for the preference is not valid.
47 * @param key the string name of the key.
48 * @return true if it's enabled.
50 public static synchronized boolean getValue(String key) throws RuntimeException {
52 return sPrefMap.get(key);
55 // TODO(yongsheng): I believe this is needed?
56 /*public static synchronized void setValue(String key, int value) throws RuntimeException {
59 static synchronized void load(KeyValueChangeListener listener) {
60 // Load current settings for initialization of a listener implementor.
61 for (Map.Entry<String, Boolean> entry : sPrefMap.entrySet()) {
62 listener.onKeyValueChanged(entry.getKey(), entry.getValue());
65 registerListener(listener);
68 // Listen to value changes.
69 interface KeyValueChangeListener {
70 public void onKeyValueChanged(String key, boolean value);
73 private static synchronized void registerListener(KeyValueChangeListener listener) {
74 sListeners.add(listener);
77 private static void onKeyValueChanged(String key, boolean enabled) {
78 for (KeyValueChangeListener listener : sListeners) {
79 listener.onKeyValueChanged(key, enabled);
83 private static void checkKey(String key) throws RuntimeException {
84 if (!sPrefMap.containsKey(key)) {
85 throw new RuntimeException("Warning: the preference key " + key +
86 " is not supported by Crosswalk.");