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 represents the preferences and could be set by callers.
13 * It is not thread-safe and must be called on the UI thread.
14 * Afterwards, the preference could be read from all threads and can impact
15 * all XWalkView instances.
17 public final class XWalkPreferences {
18 private static HashMap<String, Boolean> sPrefMap = new HashMap<String, Boolean>();
19 private static ArrayList<KeyValueChangeListener> sListeners =
20 new ArrayList<KeyValueChangeListener>();
23 * The key string to enable/disable remote debugging.
25 public static final String REMOTE_DEBUGGING = "remote-debugging";
28 sPrefMap.put(REMOTE_DEBUGGING, Boolean.FALSE);
32 * Set a preference value into Crosswalk. An exception will be thrown if
33 * the key for the preference is not valid.
34 * @param key the string name of the key.
35 * @param enabled true if setting it as enabled.
37 public static synchronized void setValue(String key, boolean enabled) throws RuntimeException {
39 if (sPrefMap.get(key) != enabled) {
40 sPrefMap.put(key, new Boolean(enabled));
41 onKeyValueChanged(key, enabled);
46 * Get a preference value from Crosswalk. An exception will be thrown if
47 * the key for the preference is not valid.
48 * @param key the string name of the key.
49 * @return true if it's enabled.
51 public static synchronized boolean getValue(String key) throws RuntimeException {
53 return sPrefMap.get(key);
56 // TODO(yongsheng): I believe this is needed?
57 /*public static synchronized void setValue(String key, int value) throws RuntimeException {
60 static synchronized void load(KeyValueChangeListener listener) {
61 // Load current settings for initialization of a listener implementor.
62 for (Map.Entry<String, Boolean> entry : sPrefMap.entrySet()) {
63 listener.onKeyValueChanged(entry.getKey(), entry.getValue());
66 registerListener(listener);
69 // Listen to value changes.
70 interface KeyValueChangeListener {
71 public void onKeyValueChanged(String key, boolean value);
74 private static synchronized void registerListener(KeyValueChangeListener listener) {
75 sListeners.add(listener);
78 private static void onKeyValueChanged(String key, boolean enabled) {
79 for (KeyValueChangeListener listener : sListeners) {
80 listener.onKeyValueChanged(key, enabled);
84 private static void checkKey(String key) throws RuntimeException {
85 if (!sPrefMap.containsKey(key)) {
86 throw new RuntimeException("Warning: the preference key " + key +
87 " is not supported by Crosswalk.");