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.internal;
7 import java.lang.ref.ReferenceQueue;
8 import java.lang.ref.WeakReference;
9 import java.util.ArrayList;
10 import java.util.HashMap;
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.
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>();
29 * The key string to enable/disable remote debugging.
32 public static final String REMOTE_DEBUGGING = "remote-debugging";
35 * The key string to enable/disable animatable XWalkViewInternal. Default value is
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.
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.
53 * Note this key MUST be set before creating the first XWalkViewInternal, otherwise
54 * a RuntimeException will be thrown.
58 public static final String ANIMATABLE_XWALK_VIEW = "animatable-xwalk-view";
61 sPrefMap.put(REMOTE_DEBUGGING, Boolean.FALSE);
62 sPrefMap.put(ANIMATABLE_XWALK_VIEW, Boolean.FALSE);
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.
72 public static synchronized void setValue(String key, boolean enabled) throws RuntimeException {
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");
80 if (sPrefMap.get(key) != enabled) {
81 sPrefMap.put(key, new Boolean(enabled));
82 onKeyValueChanged(key, enabled);
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.
93 public static synchronized boolean getValue(String key) throws RuntimeException {
95 return sPrefMap.get(key);
98 // TODO(yongsheng): I believe this is needed?
99 /*public static synchronized void setValue(String key, int value) throws RuntimeException {
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());
108 registerListener(listener);
111 static synchronized void unload(KeyValueChangeListener listener) {
112 unregisterListener(listener);
115 // Listen to value changes.
116 interface KeyValueChangeListener {
117 public void onKeyValueChanged(String key, boolean value);
120 private static synchronized void registerListener(KeyValueChangeListener listener) {
121 removeEnqueuedReference();
122 WeakReference<KeyValueChangeListener> weakListener =
123 new WeakReference<KeyValueChangeListener>(listener, sRefQueue);
124 sListeners.add(weakListener);
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);
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);
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.");
153 * Internal method to keep track of weak references and remove the enqueued
154 * references from listener list by polling the reference queue.
156 @SuppressWarnings("unchecked")
157 private static void removeEnqueuedReference() {
158 WeakReference<KeyValueChangeListener> toRemove;
159 while ((toRemove = (WeakReference<KeyValueChangeListener>) sRefQueue.poll()) != null) {
160 sListeners.remove(toRemove);