Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_internal / src / org / xwalk / core / internal / XWalkPreferencesInternal.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.internal;
6
7 import java.lang.ref.ReferenceQueue;
8 import java.lang.ref.WeakReference;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.Map;
12
13 /**
14  * This class represents the preferences and could be set by callers.
15  * It is not thread-safe and must be called on the UI thread.
16  * Afterwards, the preference could be read from all threads and can impact
17  * all XWalkViewInternal instances.
18  */
19 public class XWalkPreferencesInternal {
20     private static HashMap<String, Boolean> sPrefMap = new HashMap<String, Boolean>();
21     // Here we use WeakReference to make sure the KeyValueChangeListener instance
22     // can be GC-ed to avoid memory leaking issue.
23     private static ArrayList<WeakReference<KeyValueChangeListener> > sListeners =
24             new ArrayList<WeakReference<KeyValueChangeListener> >();
25     private static ReferenceQueue<KeyValueChangeListener> sRefQueue =
26             new ReferenceQueue<KeyValueChangeListener>();
27
28     /**
29      * The key string to enable/disable remote debugging.
30      * @since 1.0
31      */
32     public static final String REMOTE_DEBUGGING = "remote-debugging";
33
34     /**
35      * The key string to enable/disable animatable XWalkViewInternal. Default value is
36      * false.
37      *
38      * If this key is set to True, the XWalkViewInternal created by Crosswalk can be
39      * transformed and animated. Internally, Crosswalk is alternatively using
40      * TextureView as the backend of XWalkViewInternal.
41      *
42      * <a href="http://developer.android.com/reference/android/view/TextureView.html">
43      * TextureView</a> is a kind of
44      * <a href="http://developer.android.com/reference/android/view/View.html">
45      * android.view.View</a> that is different from
46      * <a href="http://developer.android.com/reference/android/view/SurfaceView.html">
47      * SurfaceView</a>. Unlike SurfaceView, it can be resized, transformed and
48      * animated. Once this key is set to True, all XWalkViewInternal will use TextureView
49      * as the rendering target instead of SurfaceView. The downside of TextureView
50      * is, it would consume more graphics memory than SurfaceView and may have
51      * 1~3 extra frames of latency to display updates.
52      *
53      * Note this key MUST be set before creating the first XWalkViewInternal, otherwise
54      * a RuntimeException will be thrown.
55      *
56      * @since 2.0
57      */
58     public static final String ANIMATABLE_XWALK_VIEW = "animatable-xwalk-view";
59
60     static {
61         sPrefMap.put(REMOTE_DEBUGGING, Boolean.FALSE);
62         sPrefMap.put(ANIMATABLE_XWALK_VIEW, Boolean.FALSE);
63     }
64
65     /**
66      * Set a preference value into Crosswalk. An exception will be thrown if
67      * the key for the preference is not valid.
68      * @param key the string name of the key.
69      * @param enabled true if setting it as enabled.
70      * @since 1.0
71      */
72     public static synchronized void setValue(String key, boolean enabled) throws RuntimeException {
73         checkKey(key);
74         // If the listener list is not empty, we consider the preference is
75         // loaded by Crosswalk and taken effect already.
76         if (key == ANIMATABLE_XWALK_VIEW && !sListeners.isEmpty()) {
77             throw new RuntimeException("Warning: the preference key " + key +
78                     " can not be set if the preference is already loaded by Crosswalk");
79         }
80         if (sPrefMap.get(key) != enabled) {
81             sPrefMap.put(key, new Boolean(enabled));
82             onKeyValueChanged(key, enabled);
83         }
84     }
85
86     /**
87      * Get a preference value from Crosswalk. An exception will be thrown if
88      * the key for the preference is not valid.
89      * @param key the string name of the key.
90      * @return true if it's enabled.
91      * @since 1.0
92      */
93     public static synchronized boolean getValue(String key) throws RuntimeException {
94         checkKey(key);
95         return sPrefMap.get(key);
96     }
97
98     // TODO(yongsheng): I believe this is needed?
99     /*public static synchronized void setValue(String key, int value) throws RuntimeException {
100     }*/
101
102     static synchronized void load(KeyValueChangeListener listener) {
103         // Load current settings for initialization of a listener implementor.
104         for (Map.Entry<String, Boolean> entry : sPrefMap.entrySet()) {
105             listener.onKeyValueChanged(entry.getKey(), entry.getValue());
106         }
107
108         registerListener(listener);
109     }
110
111     static synchronized void unload(KeyValueChangeListener listener) {
112         unregisterListener(listener);
113     }
114
115     // Listen to value changes.
116     interface KeyValueChangeListener {
117         public void onKeyValueChanged(String key, boolean value);
118     }
119
120     private static synchronized void registerListener(KeyValueChangeListener listener) {
121         removeEnqueuedReference();
122         WeakReference<KeyValueChangeListener> weakListener =
123                 new WeakReference<KeyValueChangeListener>(listener, sRefQueue);
124         sListeners.add(weakListener);
125     }
126
127     private static synchronized void unregisterListener(KeyValueChangeListener listener) {
128         removeEnqueuedReference();
129         for (WeakReference<KeyValueChangeListener> weakListener : sListeners) {
130             if (weakListener.get() == listener) {
131                 sListeners.remove(weakListener);
132                 break;
133             }
134         }
135     }
136
137     private static void onKeyValueChanged(String key, boolean enabled) {
138         for (WeakReference<KeyValueChangeListener> weakListener : sListeners) {
139             KeyValueChangeListener listener = weakListener.get();
140             if (listener != null) listener.onKeyValueChanged(key, enabled);
141         }
142     }
143
144     private static void checkKey(String key) throws RuntimeException {
145         removeEnqueuedReference();
146         if (!sPrefMap.containsKey(key)) {
147             throw new RuntimeException("Warning: the preference key " + key +
148                     " is not supported by Crosswalk.");
149         }
150     }
151
152     /**
153      * Internal method to keep track of weak references and remove the enqueued
154      * references from listener list by polling the reference queue.
155      */
156     @SuppressWarnings("unchecked")
157     private static void removeEnqueuedReference() {
158         WeakReference<KeyValueChangeListener> toRemove;
159         while ((toRemove = (WeakReference<KeyValueChangeListener>) sRefQueue.poll()) != null) {
160             sListeners.remove(toRemove);
161         }
162     }
163 }