X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fxwalk%2Fruntime%2Fandroid%2Fcore%2Fsrc%2Forg%2Fxwalk%2Fcore%2FXWalkPreferences.java;h=3c0a7c56eaec051ef893e4b635abeade0356f5cd;hb=7ff13776d9adf6dd69919761cbe6ea5a97d63522;hp=6605711dfbc54c305570a824b85d5c18cf7e4831;hpb=04892e462a62c07008a1901beb56d64f95b1087d;p=platform%2Fframework%2Fweb%2Fcrosswalk.git diff --git a/src/xwalk/runtime/android/core/src/org/xwalk/core/XWalkPreferences.java b/src/xwalk/runtime/android/core/src/org/xwalk/core/XWalkPreferences.java index 6605711..3c0a7c5 100644 --- a/src/xwalk/runtime/android/core/src/org/xwalk/core/XWalkPreferences.java +++ b/src/xwalk/runtime/android/core/src/org/xwalk/core/XWalkPreferences.java @@ -4,11 +4,7 @@ package org.xwalk.core; -import java.lang.ref.ReferenceQueue; -import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; +import org.xwalk.core.internal.XWalkPreferencesInternal; /** * This class represents the preferences and could be set by callers. @@ -16,17 +12,10 @@ import java.util.Map; * Afterwards, the preference could be read from all threads and can impact * all XWalkView instances. */ -public final class XWalkPreferences { - private static HashMap sPrefMap = new HashMap(); - // Here we use WeakReference to make sure the KeyValueChangeListener instance - // can be GC-ed to avoid memory leaking issue. - private static ArrayList > sListeners = - new ArrayList >(); - private static ReferenceQueue sRefQueue = - new ReferenceQueue(); - +public final class XWalkPreferences extends XWalkPreferencesInternal { /** * The key string to enable/disable remote debugging. + * @since 1.0 */ public static final String REMOTE_DEBUGGING = "remote-debugging"; @@ -51,32 +40,19 @@ public final class XWalkPreferences { * * Note this key MUST be set before creating the first XWalkView, otherwise * a RuntimeException will be thrown. + * @since 2.0 */ public static final String ANIMATABLE_XWALK_VIEW = "animatable-xwalk-view"; - static { - sPrefMap.put(REMOTE_DEBUGGING, Boolean.FALSE); - sPrefMap.put(ANIMATABLE_XWALK_VIEW, Boolean.FALSE); - } - /** * Set a preference value into Crosswalk. An exception will be thrown if * the key for the preference is not valid. * @param key the string name of the key. * @param enabled true if setting it as enabled. + * @since 1.0 */ public static synchronized void setValue(String key, boolean enabled) throws RuntimeException { - checkKey(key); - // If the listener list is not empty, we consider the preference is - // loaded by Crosswalk and taken effect already. - if (key == ANIMATABLE_XWALK_VIEW && !sListeners.isEmpty()) { - throw new RuntimeException("Warning: the preference key " + key + - " can not be set if the preference is already loaded by Crosswalk"); - } - if (sPrefMap.get(key) != enabled) { - sPrefMap.put(key, new Boolean(enabled)); - onKeyValueChanged(key, enabled); - } + XWalkPreferencesInternal.setValue(key, enabled); } /** @@ -84,75 +60,9 @@ public final class XWalkPreferences { * the key for the preference is not valid. * @param key the string name of the key. * @return true if it's enabled. + * @since 1.0 */ public static synchronized boolean getValue(String key) throws RuntimeException { - checkKey(key); - return sPrefMap.get(key); - } - - // TODO(yongsheng): I believe this is needed? - /*public static synchronized void setValue(String key, int value) throws RuntimeException { - }*/ - - static synchronized void load(KeyValueChangeListener listener) { - // Load current settings for initialization of a listener implementor. - for (Map.Entry entry : sPrefMap.entrySet()) { - listener.onKeyValueChanged(entry.getKey(), entry.getValue()); - } - - registerListener(listener); - } - - static synchronized void unload(KeyValueChangeListener listener) { - unregisterListener(listener); - } - - // Listen to value changes. - interface KeyValueChangeListener { - public void onKeyValueChanged(String key, boolean value); - } - - private static synchronized void registerListener(KeyValueChangeListener listener) { - removeEnqueuedReference(); - WeakReference weakListener = - new WeakReference(listener, sRefQueue); - sListeners.add(weakListener); - } - - private static synchronized void unregisterListener(KeyValueChangeListener listener) { - removeEnqueuedReference(); - for (WeakReference weakListener : sListeners) { - if (weakListener.get() == listener) { - sListeners.remove(weakListener); - break; - } - } - } - - private static void onKeyValueChanged(String key, boolean enabled) { - for (WeakReference weakListener : sListeners) { - KeyValueChangeListener listener = weakListener.get(); - if (listener != null) listener.onKeyValueChanged(key, enabled); - } - } - - private static void checkKey(String key) throws RuntimeException { - removeEnqueuedReference(); - if (!sPrefMap.containsKey(key)) { - throw new RuntimeException("Warning: the preference key " + key + - " is not supported by Crosswalk."); - } - } - - /** - * Internal method to keep track of weak references and remove the enqueued - * references from listener list by polling the reference queue. - */ - @SuppressWarnings("unchecked") - private static void removeEnqueuedReference() { - WeakReference toRemove; - while ((toRemove = (WeakReference) sRefQueue.poll()) != null) { - sListeners.remove(toRemove); - } + return XWalkPreferencesInternal.getValue(key); } }