Upstream version 6.35.131.0
[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 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.
16  */
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>();
21
22     /**
23      * The key string to enable/disable remote debugging.
24      */
25     public static final String REMOTE_DEBUGGING = "remote-debugging";
26
27     static {
28         sPrefMap.put(REMOTE_DEBUGGING, Boolean.FALSE);
29     }
30
31     /**
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.
36      */
37     public static synchronized void setValue(String key, boolean enabled) throws RuntimeException {
38         checkKey(key);
39         if (sPrefMap.get(key) != enabled) {
40             sPrefMap.put(key, new Boolean(enabled));
41             onKeyValueChanged(key, enabled);
42         }
43     }
44
45     /**
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.
50      */
51     public static synchronized boolean getValue(String key) throws RuntimeException {
52         checkKey(key);
53         return sPrefMap.get(key);
54     }
55
56     // TODO(yongsheng): I believe this is needed?
57     /*public static synchronized void setValue(String key, int value) throws RuntimeException {
58     }*/
59
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());
64         }
65
66         registerListener(listener);
67     }
68
69     // Listen to value changes.
70     interface KeyValueChangeListener {
71         public void onKeyValueChanged(String key, boolean value);
72     }
73
74     private static synchronized void registerListener(KeyValueChangeListener listener) {
75         sListeners.add(listener);
76     }
77
78     private static void onKeyValueChanged(String key, boolean enabled) {
79         for (KeyValueChangeListener listener : sListeners) {
80             listener.onKeyValueChanged(key, enabled);
81         }
82     }
83
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.");
88         }
89     }
90
91 }