d2cf5dff32518c56ba8e3f79d90ccecf129f34d0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / XWalkPreferences.java
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.
4
5 package org.xwalk.core;
6
7 import java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.Map;
10
11 /**
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.
15  */
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>();
20
21     /**
22      * The key string to enable/disable remote debugging.
23      */
24     public static final String REMOTE_DEBUGGING = "remote-debugging";
25
26     static {
27         sPrefMap.put(REMOTE_DEBUGGING, Boolean.FALSE);
28     }
29
30     /**
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.
35      */
36     public static synchronized void setValue(String key, boolean enabled) throws RuntimeException {
37         checkKey(key);
38         if (sPrefMap.get(key) != enabled) {
39             sPrefMap.put(key, new Boolean(enabled));
40             onKeyValueChanged(key, enabled);
41         }
42     }
43
44     /**
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.
49      */
50     public static synchronized boolean getValue(String key) throws RuntimeException {
51         checkKey(key);
52         return sPrefMap.get(key);
53     }
54
55     // TODO(yongsheng): I believe this is needed?
56     /*public static synchronized void setValue(String key, int value) throws RuntimeException {
57     }*/
58
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());
63         }
64
65         registerListener(listener);
66     }
67
68     // Listen to value changes.
69     interface KeyValueChangeListener {
70         public void onKeyValueChanged(String key, boolean value);
71     }
72
73     private static synchronized void registerListener(KeyValueChangeListener listener) {
74         sListeners.add(listener);
75     }
76
77     private static void onKeyValueChanged(String key, boolean enabled) {
78         for (KeyValueChangeListener listener : sListeners) {
79             listener.onKeyValueChanged(key, enabled);
80         }
81     }
82
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.");
87         }
88     }
89
90 }