If you think the Android project kirin-for-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
Copyright 2011 Future Platforms/*www.java2s.com*/
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/package com.futureplatforms.kirin.extensions.settings;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;
import com.futureplatforms.kirin.extensions.KirinExtensionAdapter;
import com.futureplatforms.kirin.generated.preferences.KirinPreferenceListener;
import com.futureplatforms.kirin.generated.preferences.KirinPreferences;
import com.futureplatforms.kirin.generated.preferences.KirinPreferencesBackend;
/**
* This is the new implementation of the app-preferences backend.
*
* It is not yet ready for even testing.
*
* @author james
*
*/publicclass PreferencesBackendImpl extends KirinExtensionAdapter implements KirinPreferencesBackend {
private SharedPreferences mPreferences;
private KirinPreferences mModule;
public PreferencesBackendImpl(Context context) {
this(context, null);
}
public PreferencesBackendImpl(Context context, SharedPreferences preferences) {
super(context, "app-preferences-alpha");
if (preferences == null) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
}
mPreferences = preferences;
}
@Override
publicvoid onLoad() {
super.onLoad();
mModule = this.bindExtensionModule(KirinPreferences.class);
mModule.mergeOrOverwrite(new JSONObject(mPreferences.getAll()));
mKirinHelper.jsMethod("mergeOrOverwrite", mPreferences.getAll());
mKirinHelper.jsMethod("resetEnvironment");
}
@Override
publicvoid onUnload() {
super.onUnload();
}
/* (non-Javadoc)
* @see com.futureplatforms.android.jscore.services.ISettingsBackend#updateContents_withDeletes_(org.json.JSONObject, org.json.JSONArray)
*/
@Override
publicvoid updateStoreWithChangesAndDeletes(JSONObject changes,
JSONArray deletes) {
try {
Editor editor = mPreferences.edit();
for (int i = 0; i < deletes.length(); i++) {
String key = deletes.getString(i);
editor.remove(key);
}
for (Iterator<?> iterator = changes.keys(); iterator.hasNext();) {
String key = (String) iterator.next();
Object value = changes.get(key);
if (value instanceof String) {
editor.putString(key, (String) value);
} elseif (value instanceof Boolean) {
editor.putBoolean(key, (Boolean) value);
} elseif (value instanceofInteger) {
editor.putInt(key, (Integer) value);
} elseif (value instanceof Float) {
editor.putFloat(key, (Float) value);
} elseif (value instanceof Long) {
editor.putLong(key, (Long) value);
}
}
editor.commit();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* (non-Javadoc)
* @see com.futureplatforms.android.jscore.services.ISettingsBackend#requestPopulateJSWithCallback_(java.lang.String)
*/publicvoid requestPopulateJSWithCallback_(String callbackToken) {
Map<String, ?> copy = mPreferences.getAll();
JSONObject obj = new JSONObject(copy);
mKirinHelper.jsCallback(callbackToken, obj);
mKirinHelper.cleanupCallback(callbackToken);
}
@Override
publicvoid addPreferenceListener(final KirinPreferenceListener listener) {
mPreferences.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
@Override
publicvoid onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
listener.onPreferenceChange(key, sharedPreferences.getAll().get(key));
}
});
}
@Override
publicvoid removePreferenceListener() {
// TODO Auto-generated method stub
}
@Override
publicvoid addInterestFor(String preferenceName) {
// TODO Auto-generated method stub
}
@Override
publicvoid removeInterestFor(String preferenceName) {
// TODO Auto-generated method stub
}
}