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//fromwww.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.preference.PreferenceManager;
import com.futureplatforms.kirin.extensions.KirinExtensionAdapter;
publicclass SettingsBackend extends KirinExtensionAdapter implements ISettingsBackend {
private SharedPreferences mPreferences;
public SettingsBackend(Context context) {
this(context, null);
}
public SettingsBackend(Context context, SharedPreferences preferences) {
super(context, "app-preferences");
if (preferences == null) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
}
mPreferences = preferences;
}
@Override
publicvoid onLoad() {
super.onLoad();
mKirinHelper.jsMethod("mergeOrOverwrite", mPreferences.getAll());
mKirinHelper.jsMethod("resetEnvironment");
}
/* (non-Javadoc)
* @see com.futureplatforms.android.jscore.services.ISettingsBackend#updateContents_withDeletes_(org.json.JSONObject, org.json.JSONArray)
*/publicvoid updateContents_withDeletes_(JSONObject contents, 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 = contents.keys(); iterator.hasNext();) {
String key = (String) iterator.next();
Object value = contents.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);
}
}