Merge pull request #307 from seltzlab/master

new Android plugin: AccountList
This commit is contained in:
macdonst
2012-01-01 09:10:47 -08:00
3 changed files with 89 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
package com.seltzlab.mobile;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.accounts.Account;
import android.accounts.AccountManager;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
public class AccountList extends Plugin {
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
try {
JSONObject obj = args.getJSONObject(0);
AccountManager am = AccountManager.get(this.ctx);
Account[] accounts;
if (obj.has("type"))
accounts = am.getAccountsByType(obj.getString("type"));
else
accounts = am.getAccounts();
JSONArray res = new JSONArray();
for (int i = 0; i < accounts.length; i++) {
Account a = accounts[i];
res.put(a.name);
}
return new PluginResult(PluginResult.Status.OK, res);
} catch (JSONException e) {
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
}

View File

@@ -0,0 +1,34 @@
AccountList Phonegap Plugin for Android
=======================================
This plugin allows you to obtain an array containing all the accounts configured on the device
Adding the Plugin to your project
=================================
To install the plugin, copy accountlist.js to your project's www folder and include a reference to it in your html files.
<script type="text/javascript" src="accountlist.js"></script>
Create a folder called 'com/seltzlab/mobile' within your project's src folder and copy AccountList.java file into that new folder.
Add a plugin line to res/xml/plugins.xml
<plugin name="AccountList" value="com.seltzlab.com.AccountList" />
Add a permission line to the AndroidManifest.xml
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
Using the plugin
================
window.plugins.AccountList.get(
{
type: 'account type' // if not specified get all accounts
},
function (result) {
console.log(res.length);
for (i in res)
console.log(res[i]);
},
function (error) {
console.log(error);
}
);

View File

@@ -0,0 +1,13 @@
var AccountList = function() {};
AccountList.prototype.get = function(params, success, fail) {
return PhoneGap.exec( function(args) {
success(args);
}, function(args) {
fail(args);
}, 'AccountList', '', [params]);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin('AccountList', new AccountList());
});