diff --git a/1.8.1/assets/www/applicationPreferences.js b/1.8.1/assets/www/applicationPreferences.js deleted file mode 100644 index 05b9ae3..0000000 --- a/1.8.1/assets/www/applicationPreferences.js +++ /dev/null @@ -1,29 +0,0 @@ -var AppPreferences = function () {}; - -var AppPreferencesError = function(code, message) { - this.code = code || null; - this.message = message || ''; -}; - -AppPreferencesError.NO_PROPERTY = 0; -AppPreferencesError.NO_PREFERENCE_ACTIVITY = 1; - -AppPreferences.prototype.get = function(key,success,fail) { - cordova.exec(success,fail,"applicationPreferences","get",[key]); -}; - -AppPreferences.prototype.set = function(key,value,success,fail) { - cordova.exec(success,fail,"applicationPreferences","set",[key, value]); -}; - -AppPreferences.prototype.load = function(success,fail) { - cordova.exec(success,fail,"applicationPreferences","load",[]); -}; - -AppPreferences.prototype.show = function(activity,success,fail) { - cordova.exec(success,fail,"applicationPreferences","show",[activity]); -}; - -cordova.addConstructor(function() { - cordova.addPlugin("applicationPreferences", new AppPreferences()); -}); \ No newline at end of file diff --git a/1.8.1/src/com/simonmacdonald/prefs/AppPreferences.java b/1.8.1/src/com/simonmacdonald/prefs/AppPreferences.java deleted file mode 100644 index de63d4a..0000000 --- a/1.8.1/src/com/simonmacdonald/prefs/AppPreferences.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.simonmacdonald.prefs; - -import java.util.Iterator; -import java.util.Map; - -import org.apache.cordova.api.Plugin; -import org.apache.cordova.api.PluginResult; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import android.content.ActivityNotFoundException; -import android.content.Intent; -import android.content.SharedPreferences; -import android.content.SharedPreferences.Editor; -import android.preference.PreferenceManager; -import android.util.Log; - -public class AppPreferences extends Plugin { - - private static final String LOG_TAG = "AppPrefs"; - private static final int NO_PROPERTY = 0; - private static final int NO_PREFERENCE_ACTIVITY = 1; - - @Override - public PluginResult execute(String action, JSONArray args, String callbackId) { - PluginResult.Status status = PluginResult.Status.OK; - String result = ""; - - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.ctx.getContext()); - - try { - if (action.equals("get")) { - String key = args.getString(0); - if (sharedPrefs.contains(key)) { - Object obj = sharedPrefs.getAll().get(key); - return new PluginResult(status, obj.toString()); - } else { - return createErrorObj(NO_PROPERTY, "No such property called " + key); - } - } else if (action.equals("set")) { - String key = args.getString(0); - String value = args.getString(1); - if (sharedPrefs.contains(key)) { - Editor editor = sharedPrefs.edit(); - if ("true".equals(value.toLowerCase()) || "false".equals(value.toLowerCase())) { - editor.putBoolean(key, Boolean.parseBoolean(value)); - } else { - editor.putString(key, value); - } - return new PluginResult(status, editor.commit()); - } else { - return createErrorObj(NO_PROPERTY, "No such property called " + key); - } - } else if (action.equals("load")) { - JSONObject obj = new JSONObject(); - Map prefs = sharedPrefs.getAll(); - Iterator it = prefs.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pairs = (Map.Entry)it.next(); - obj.put(pairs.getKey().toString(), pairs.getValue().toString()); - } - return new PluginResult(status, obj); - } else if (action.equals("show")) { - String activityName = args.getString(0); - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setClassName(this.ctx.getContext(), activityName); - try { - this.ctx.startActivity(intent); - } catch (ActivityNotFoundException e) { - return createErrorObj(NO_PREFERENCE_ACTIVITY, "No preferences activity called " + activityName); - } - } - } catch (JSONException e) { - status = PluginResult.Status.JSON_EXCEPTION; - } - return new PluginResult(status, result); - } - - private PluginResult createErrorObj(int code, String message) throws JSONException { - JSONObject errorObj = new JSONObject(); - errorObj.put("code", code); - errorObj.put("message", message); - return new PluginResult(PluginResult.Status.ERROR, errorObj); - } - -} diff --git a/2.0.0/README.md b/2.0.0/README.md deleted file mode 100644 index b0251a7..0000000 --- a/2.0.0/README.md +++ /dev/null @@ -1,170 +0,0 @@ -# Application Preferences plugin for Phonegap # -Originally by Simon MacDonald (@macdonst) - -Please note that the following steps are for PhoneGap 2.0 - -Information on writing plugins for PhoneGap 2.0 was taken from [this blog](http://simonmacdonald.blogspot.com/2012/08/so-you-wanna-write-phonegap-200-android.html) by Simon MacDonald (@macdonst) - -## Adding the Plugin to your project ## - -1) To install the plugin, move applicationPreferences.js to your project's www folder and include a reference to it in your html files. - -`` - -2) Create a folder called 'com/simonmacdonald/prefs' within your project's src folder. -3) And copy the AppPreferences.java file into that new folder. - -`mkdir /src/com/simonmacdonald/prefs` - -`cp ./src/com/simonmacdonald/prefs/AppPreferences.java /src/com/simonmacdonald/prefs` - -4) In your `res/xml/config.xml` file add the following element as a child to the `` element. - - `` - -## Using the plugin ## - -Create an object to be used to call the defined plugin methods. - - var preferences = cordova.require("cordova/plugin/applicationpreferences"); - -The `preferences` object created above will be used in the following examples. - -### get ### - -In order to get the value a property you would call the get method. - - /** - * Get the value of the named property. - * - * @param key - */ - get(key, success, fail) - -Sample use: - - preferences.get("myKey", function(value) { - alert("Value is " + value); - }, function(error) { - alert("Error! " + JSON.stringify(error)); - }); - -### set ### - -In order to set the value a property you would call the set method. - - /** - * Set the value of the named property. - * - * @param key - * @param value - */ - set(key, value, success, fail) - -Sample use: - - preferences.set("myKey", "myValue", function() { - alert("Successfully saved!"); - }, function(error) { - alert("Error! " + JSON.stringify(error)); - }); - - -### remove ### - -In order to remove a key along with the value, you would call the remove method. - - /** - * Remove the key along with the value - * - * @param key - */ - remove(key, success, fail) - -Sample use: - - preferences.remove("myKey", function(value) { - alert("Value removed!"); - }, function(error) { - alert("Error! " + JSON.stringify(error)); - }); - -### clear ### - -In order to remove all shared preferences, you would call the clear method. - - /** - * Clear all shared preferences - * - */ - clear(success, fail) - -Sample use: - - preferences.clear(function() { - alert("Cleared all preferences!"); - }, function(error) { - alert("Error! " + JSON.stringify(error)); - }); - -### load ### - -In order to get all the properties you can call the load method. The success callback of the load method will be called with a JSONObject which contains all the preferences. - - /** - * Get all the preference values. - * - */ - load(success, fail) - -Sample use: - - preferences.load(function(prefs) { - alert(JSON.stringify(prefs)); - }, function() { - alert("Error! " + JSON.stringify(error)); - }); - -### show ### - -If you want to load the PreferenceActivity of your application that displays all the preferences you can call the show method with the class name. - - /** - * Get all the preference values. - * - */ - show(activity, success, fail) - -Sample use: - - function showPreferenceActivity() { - preferences.show("com.ranhiru.apppreferences.PreferenceActivity", function() { - alert("Showing Preferences Activity!"); - }, function(error) { - alert("Error! " + JSON.stringify(error)); - }); - } - -## Licence ## - -The MIT License - -Copyright (c) 2012 Simon MacDonald - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. \ No newline at end of file diff --git a/2.0.0/src/com/simonmacdonald/prefs/AppPreferences.java b/2.0.0/src/com/simonmacdonald/prefs/AppPreferences.java deleted file mode 100644 index 268d382..0000000 --- a/2.0.0/src/com/simonmacdonald/prefs/AppPreferences.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.simonmacdonald.prefs; - -import java.util.Iterator; -import java.util.Map; - -import org.apache.cordova.api.Plugin; -import org.apache.cordova.api.PluginResult; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import android.content.ActivityNotFoundException; -import android.content.Intent; -import android.content.SharedPreferences; -import android.content.SharedPreferences.Editor; -import android.preference.PreferenceManager; -import android.util.Log; - -public class AppPreferences extends Plugin { - - private static final String LOG_TAG = "AppPrefs"; - private static final int NO_PROPERTY = 0; - private static final int NO_PREFERENCE_ACTIVITY = 1; - - @Override - public PluginResult execute(String action, JSONArray args, String callbackId) { - PluginResult.Status status = PluginResult.Status.OK; - String result = ""; - - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.cordova.getActivity()); - - try { - if (action.equals("get")) { - String key = args.getString(0); - if (sharedPrefs.contains(key)) { - Object obj = sharedPrefs.getAll().get(key); - return new PluginResult(status, obj.toString()); - } else { - return createErrorObj(NO_PROPERTY, "No such property called " + key); - } - } else if (action.equals("set")) { - String key = args.getString(0); - String value = args.getString(1); - Editor editor = sharedPrefs.edit(); - if ("true".equals(value.toLowerCase()) || "false".equals(value.toLowerCase())) { - editor.putBoolean(key, Boolean.parseBoolean(value)); - } else { - editor.putString(key, value); - } - return new PluginResult(status, editor.commit()); - } else if (action.equals("load")) { - JSONObject obj = new JSONObject(); - Map prefs = sharedPrefs.getAll(); - Iterator it = prefs.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pairs = (Map.Entry)it.next(); - obj.put(pairs.getKey().toString(), pairs.getValue().toString()); - } - return new PluginResult(status, obj); - } else if (action.equals("show")) { - String activityName = args.getString(0); - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setClassName(this.cordova.getActivity(), activityName); - try { - this.cordova.getActivity().startActivity(intent); - } catch (ActivityNotFoundException e) { - return createErrorObj(NO_PREFERENCE_ACTIVITY, "No preferences activity called " + activityName); - } - } else if (action.equals("clear")) { - Editor editor = sharedPrefs.edit(); - editor.clear(); - return new PluginResult(status, editor.commit()); - } else if (action.equals("remove")) { - String key = args.getString(0); - if (sharedPrefs.contains(key)) { - Editor editor = sharedPrefs.edit(); - editor.remove(key); - return new PluginResult(status, editor.commit()); - } else { - return createErrorObj(NO_PROPERTY, "No such property called " + key); - } - - } - } catch (JSONException e) { - status = PluginResult.Status.JSON_EXCEPTION; - } - return new PluginResult(status, result); - } - - private PluginResult createErrorObj(int code, String message) throws JSONException { - JSONObject errorObj = new JSONObject(); - errorObj.put("code", code); - errorObj.put("message", message); - return new PluginResult(PluginResult.Status.ERROR, errorObj); - } - -} diff --git a/2.2.0/.DS_Store b/2.2.0/.DS_Store deleted file mode 100644 index 9774c0a..0000000 Binary files a/2.2.0/.DS_Store and /dev/null differ diff --git a/2.2.0/README.md b/2.2.0/README.md deleted file mode 100644 index b0251a7..0000000 --- a/2.2.0/README.md +++ /dev/null @@ -1,170 +0,0 @@ -# Application Preferences plugin for Phonegap # -Originally by Simon MacDonald (@macdonst) - -Please note that the following steps are for PhoneGap 2.0 - -Information on writing plugins for PhoneGap 2.0 was taken from [this blog](http://simonmacdonald.blogspot.com/2012/08/so-you-wanna-write-phonegap-200-android.html) by Simon MacDonald (@macdonst) - -## Adding the Plugin to your project ## - -1) To install the plugin, move applicationPreferences.js to your project's www folder and include a reference to it in your html files. - -`` - -2) Create a folder called 'com/simonmacdonald/prefs' within your project's src folder. -3) And copy the AppPreferences.java file into that new folder. - -`mkdir /src/com/simonmacdonald/prefs` - -`cp ./src/com/simonmacdonald/prefs/AppPreferences.java /src/com/simonmacdonald/prefs` - -4) In your `res/xml/config.xml` file add the following element as a child to the `` element. - - `` - -## Using the plugin ## - -Create an object to be used to call the defined plugin methods. - - var preferences = cordova.require("cordova/plugin/applicationpreferences"); - -The `preferences` object created above will be used in the following examples. - -### get ### - -In order to get the value a property you would call the get method. - - /** - * Get the value of the named property. - * - * @param key - */ - get(key, success, fail) - -Sample use: - - preferences.get("myKey", function(value) { - alert("Value is " + value); - }, function(error) { - alert("Error! " + JSON.stringify(error)); - }); - -### set ### - -In order to set the value a property you would call the set method. - - /** - * Set the value of the named property. - * - * @param key - * @param value - */ - set(key, value, success, fail) - -Sample use: - - preferences.set("myKey", "myValue", function() { - alert("Successfully saved!"); - }, function(error) { - alert("Error! " + JSON.stringify(error)); - }); - - -### remove ### - -In order to remove a key along with the value, you would call the remove method. - - /** - * Remove the key along with the value - * - * @param key - */ - remove(key, success, fail) - -Sample use: - - preferences.remove("myKey", function(value) { - alert("Value removed!"); - }, function(error) { - alert("Error! " + JSON.stringify(error)); - }); - -### clear ### - -In order to remove all shared preferences, you would call the clear method. - - /** - * Clear all shared preferences - * - */ - clear(success, fail) - -Sample use: - - preferences.clear(function() { - alert("Cleared all preferences!"); - }, function(error) { - alert("Error! " + JSON.stringify(error)); - }); - -### load ### - -In order to get all the properties you can call the load method. The success callback of the load method will be called with a JSONObject which contains all the preferences. - - /** - * Get all the preference values. - * - */ - load(success, fail) - -Sample use: - - preferences.load(function(prefs) { - alert(JSON.stringify(prefs)); - }, function() { - alert("Error! " + JSON.stringify(error)); - }); - -### show ### - -If you want to load the PreferenceActivity of your application that displays all the preferences you can call the show method with the class name. - - /** - * Get all the preference values. - * - */ - show(activity, success, fail) - -Sample use: - - function showPreferenceActivity() { - preferences.show("com.ranhiru.apppreferences.PreferenceActivity", function() { - alert("Showing Preferences Activity!"); - }, function(error) { - alert("Error! " + JSON.stringify(error)); - }); - } - -## Licence ## - -The MIT License - -Copyright (c) 2012 Simon MacDonald - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. \ No newline at end of file diff --git a/2.2.0/assets/.DS_Store b/2.2.0/assets/.DS_Store deleted file mode 100644 index d15a0a7..0000000 Binary files a/2.2.0/assets/.DS_Store and /dev/null differ diff --git a/2.2.0/assets/www/.DS_Store b/2.2.0/assets/www/.DS_Store deleted file mode 100644 index 66b39aa..0000000 Binary files a/2.2.0/assets/www/.DS_Store and /dev/null differ diff --git a/2.2.0/assets/www/applicationPreferences.js b/2.2.0/assets/www/applicationPreferences.js deleted file mode 100644 index e61fb8a..0000000 --- a/2.2.0/assets/www/applicationPreferences.js +++ /dev/null @@ -1,46 +0,0 @@ -cordova.define("cordova/plugin/applicationpreferences", function(require, exports, module) { - var exec = require("cordova/exec"); - var AppPreferences = function () {}; - - var AppPreferencesError = function(code, message) { - this.code = code || null; - this.message = message || ''; - }; - - AppPreferencesError.NO_PROPERTY = 0; - AppPreferencesError.NO_PREFERENCE_ACTIVITY = 1; - - AppPreferences.prototype.get = function(key,success,fail) { - cordova.exec(success,fail,"applicationPreferences","get",[key]); - }; - - AppPreferences.prototype.set = function(key,value,success,fail) { - cordova.exec(success,fail,"applicationPreferences","set",[key, value]); - }; - - AppPreferences.prototype.load = function(success,fail) { - cordova.exec(success,fail,"applicationPreferences","load",[]); - }; - - AppPreferences.prototype.show = function(activity,success,fail) { - cordova.exec(success,fail,"applicationPreferences","show",[activity]); - }; - - AppPreferences.prototype.clear = function(success,fail) { - cordova.exec(success,fail,"applicationPreferences","clear", []); - }; - - AppPreferences.prototype.remove = function(keyToRemove, success,fail) { - cordova.exec(success,fail,"applicationPreferences","remove", [keyToRemove]); - }; - - var appPreferences = new AppPreferences(); - module.exports = appPreferences; -}); - -if (!window.plugins) { - window.plugins = {}; -} -if (!window.plugins.applicationPreference) { - window.plugins.applicationPreference = cordova.require("cordova/plugin/applicationpreferences"); -} diff --git a/2.2.0/src/.DS_Store b/2.2.0/src/.DS_Store deleted file mode 100644 index b48b28d..0000000 Binary files a/2.2.0/src/.DS_Store and /dev/null differ diff --git a/2.2.0/src/com/.DS_Store b/2.2.0/src/com/.DS_Store deleted file mode 100644 index 856137d..0000000 Binary files a/2.2.0/src/com/.DS_Store and /dev/null differ diff --git a/2.2.0/src/com/simonmacdonald/.DS_Store b/2.2.0/src/com/simonmacdonald/.DS_Store deleted file mode 100644 index 8f4ab3b..0000000 Binary files a/2.2.0/src/com/simonmacdonald/.DS_Store and /dev/null differ diff --git a/2.2.0/src/com/simonmacdonald/prefs/.DS_Store b/2.2.0/src/com/simonmacdonald/prefs/.DS_Store deleted file mode 100644 index 11b22ab..0000000 Binary files a/2.2.0/src/com/simonmacdonald/prefs/.DS_Store and /dev/null differ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..314d790 --- /dev/null +++ b/LICENSE @@ -0,0 +1,25 @@ +## Licence ## + +The MIT License + +Copyright (c) 2011 Tue Topholm / Sugee +Copyright (c) 2012 Simon MacDonald +Copyright (c) 2013 Dan Moore + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md index 15fd2dc..02b4cc3 100644 --- a/README.md +++ b/README.md @@ -1,105 +1,59 @@ -# Application Preferences plugin for Phonegap # -Originally by Simon MacDonald (@macdonst) - -## Adding the Plugin to your project ## - -1) To install the plugin, move applicationPreferences.js to your project's www folder and include a reference to it in your html files. - -`` - -2) Create a folder called 'com/simonmacdonald/prefs' within your project's src folder. -3) And copy the AppPreferences.java file into that new folder. - -`mkdir /src/com/simonmacdonald/prefs` - -`cp ./src/com/simonmacdonald/prefs/AppPreferences.java /src/com/simonmacdonald/prefs` - -4) In your res/xml/plugins.xml file add the following line: - - `` - -## Using the plugin ## -The plugin creates the object `window.plugins.applicationPreferences` - -### get ### - -In order to get the value a property you would call the get method. - - /** - * Get the value of the named property. - * - * @param key - */ - get(key, success, fail) - -Sample use: - - window.plugins.applicationPreference.get("key", success, fail); - -### set ### - -In order to set the value a property you would call the set method. - - /** - * Set the value of the named property. - * - * @param key - * @param value - */ - set(key, value, success, fail) - -Sample use: - - window.plugins.applicationPreference.set("key", "value", success, fail); - -### load ### - -In order to get all the properties you can call the load method. The success callback of the load method will be called with a JSONObject which contains all the preferences. - - /** - * Get all the preference values. - * - */ - load(success, fail) - -Sample use: - - window.plugins.applicationPreference.load(success, fail); - -### show ### - -If you want to load the PreferenceActivity of your application that displays all the preferences you can call the show method with the class name. - - /** - * Get all the preference values. - * - */ - show(activity, success, fail) - -Sample use: - - window.plugins.applicationPreference.show("com.simonmacdonald.prefs.PreferenceActivity", success, fail); - -## Licence ## - -The MIT License - -Copyright (c) 2012 Simon MacDonald - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. \ No newline at end of file +# Application Preferences plugin for Phonegap # +Originally by Simon MacDonald (@macdonst), ported to plugman and some other cleanup by Dan Moore (@mooreds). Dan Moore also pulled in the iOS code, written by Tue Topholm / Sugee + +Information on writing plugins for PhoneGap 2.0 was taken from [this blog](http://simonmacdonald.blogspot.com/2012/08/so-you-wanna-write-phonegap-200-android.html) by Simon MacDonald (@macdonst) + +This code only supports Phonegap/Cordova 2.9. If you want to have support for 3.0 or greater, check out [this repository](https://github.com/chrisekelley/AppPreferences/). + +## Install + +This plugin uses [plugman](https://github.com/apache/cordova-plugman) + +`cordova plugins add https://github.com/8zrealestate/AppPreferences` + +## Using the plugin ## + +There will be a window.applicationPreferences object defined after the plugin is installed. + +The `applicationPreferences` object created above will be used in the following examples. + +### get ### + +In order to get the value a property you would call the get method. + + /** + * Get the value of the named property. + * + * @param key + */ + get(key, success, fail) + +Sample use: + + window.applicationPreferences.get("myKey", function(value) { + alert("Value is " + value); + }, function(error) { + alert("Error! " + JSON.stringify(error)); + }); + +### set ### + +In order to set the value a property you would call the set method. + + /** + * Set the value of the named property. + * + * @param key + * @param value + */ + set(key, value, success, fail) + +Sample use: + + window.applicationPreferences.set("myKey", "myValue", function() { + alert("Successfully saved!"); + }, function(error) { + alert("Error! " + JSON.stringify(error)); + }); + + diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..1b67df2 --- /dev/null +++ b/plugin.xml @@ -0,0 +1,38 @@ + + + + + + Preferences Access Plugin + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/2.2.0/src/com/simonmacdonald/prefs/AppPreferences.java b/src/android/AppPreferences.java similarity index 100% rename from 2.2.0/src/com/simonmacdonald/prefs/AppPreferences.java rename to src/android/AppPreferences.java diff --git a/2.0.0/assets/www/applicationPreferences.js b/src/android/applicationPreferences.js similarity index 90% rename from 2.0.0/assets/www/applicationPreferences.js rename to src/android/applicationPreferences.js index 09f0c57..5b1bffe 100644 --- a/2.0.0/assets/www/applicationPreferences.js +++ b/src/android/applicationPreferences.js @@ -1,4 +1,3 @@ -cordova.define("cordova/plugin/applicationpreferences", function(require, exports, module) { var exec = require("cordova/exec"); var AppPreferences = function () {}; @@ -36,4 +35,3 @@ cordova.define("cordova/plugin/applicationpreferences", function(require, export var appPreferences = new AppPreferences(); module.exports = appPreferences; -}); \ No newline at end of file diff --git a/src/ios/applicationPreferences.h b/src/ios/applicationPreferences.h new file mode 100755 index 0000000..7bc5c18 --- /dev/null +++ b/src/ios/applicationPreferences.h @@ -0,0 +1,23 @@ +// +// applicationPreferences.h +// +// +// Created by Tue Topholm on 31/01/11. +// Copyright 2011 Sugee. All rights reserved. +// + +#import + +#import + +@interface applicationPreferences : CDVPlugin +{ + +} + +- (void) getSetting:(CDVInvokedUrlCommand*)command; +- (void) setSetting:(CDVInvokedUrlCommand*)command; +- (NSString*) getSettingFromBundle:(NSString*)settingName; + + +@end diff --git a/src/ios/applicationPreferences.js b/src/ios/applicationPreferences.js new file mode 100755 index 0000000..821f7ed --- /dev/null +++ b/src/ios/applicationPreferences.js @@ -0,0 +1,19 @@ +function applicationPreferences() {} + +applicationPreferences.prototype.get = function(key,success,fail) +{ + var args = {}; + args.key = key; + cordova.exec(success,fail,"applicationPreferences","getSetting",[args]); +}; + +applicationPreferences.prototype.set = function(key,value,success,fail) +{ + var args = {}; + args.key = key; + args.value = value; + cordova.exec(success,fail,"applicationPreferences","setSetting",[args]); +}; + + module.exports = new applicationPreferences(); + diff --git a/src/ios/applicationPreferences.m b/src/ios/applicationPreferences.m new file mode 100755 index 0000000..e532ff2 --- /dev/null +++ b/src/ios/applicationPreferences.m @@ -0,0 +1,103 @@ +// +// applicationPreferences.m +// +// +// Created by Tue Topholm on 31/01/11. +// Copyright 2011 Sugee. All rights reserved. +// +// THIS HAVEN'T BEEN TESTED WITH CHILD PANELS YET. + +#import "applicationPreferences.h" + + +@implementation applicationPreferences + + + +- (void)getSetting:(CDVInvokedUrlCommand*)command +{ + NSString* callbackID = command.callbackId; + NSString* jsString; + + NSDictionary *options = [command.arguments objectAtIndex:0]; + NSString *settingsName = [options objectForKey:@"key"]; + CDVPluginResult* result = nil; + + @try + { + //At the moment we only return strings + //bool: true = 1, false=0 + NSString *returnVar = [[NSUserDefaults standardUserDefaults] stringForKey:settingsName]; + if(returnVar == nil) + { + returnVar = [self getSettingFromBundle:settingsName]; //Parsing Root.plist + + if (returnVar == nil) + @throw [NSException exceptionWithName:nil reason:@"Key not found" userInfo:nil];; + } + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:returnVar]; + jsString = [result toSuccessCallbackString:callbackID]; + } + @catch (NSException * e) + { + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT messageAsString:[e reason]]; + jsString = [result toErrorCallbackString:callbackID]; + } + @finally + { + [self writeJavascript:jsString]; //Write back to JS + } +} + +- (void)setSetting:(CDVInvokedUrlCommand*)command +{ + NSString* callbackID = command.callbackId; + NSString* jsString; + CDVPluginResult* result; + + NSDictionary *options = [command.arguments objectAtIndex:0]; + NSString *settingsName = [options objectForKey:@"key"]; + NSString *settingsValue = [options objectForKey:@"value"]; + + + @try + { + [[NSUserDefaults standardUserDefaults] setValue:settingsValue forKey:settingsName]; + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + jsString = [result toSuccessCallbackString:callbackID]; + + } + @catch (NSException * e) + { + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT messageAsString:[e reason]]; + jsString = [result toErrorCallbackString:callbackID]; + } + @finally + { + [self writeJavascript:jsString]; //Write back to JS + } +} +/* + Parsing the Root.plist for the key, because there is a bug/feature in Settings.bundle + So if the user haven't entered the Settings for the app, the default values aren't accessible through NSUserDefaults. +*/ + + +- (NSString*)getSettingFromBundle:(NSString*)settingsName +{ + NSString *pathStr = [[NSBundle mainBundle] bundlePath]; + NSString *settingsBundlePath = [pathStr stringByAppendingPathComponent:@"Settings.bundle"]; + NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"Root.plist"]; + + NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath]; + NSArray *prefSpecifierArray = [settingsDict objectForKey:@"PreferenceSpecifiers"]; + NSDictionary *prefItem; + for (prefItem in prefSpecifierArray) + { + if ([[prefItem objectForKey:@"Key"] isEqualToString:settingsName]) + return [prefItem objectForKey:@"DefaultValue"]; + } + return nil; + +} +@end