add Cordova Contacts plugin

This commit is contained in:
Yaroslav Dmytrotsa 2015-08-11 15:34:27 +03:00
parent ec08d0ec65
commit 7777cea0be
16 changed files with 3307 additions and 1 deletions

View File

@ -16,6 +16,10 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<application
android:name=".app.SyngApplication"
android:allowBackup="true"

View File

@ -125,6 +125,69 @@ module.exports = [
"clobbers": [
"window.StatusBar"
]
},
{
"file": "plugins/cordova-plugin-contacts/www/contacts.js",
"id": "cordova-plugin-contacts.contacts",
"clobbers": [
"navigator.contacts"
]
},
{
"file": "plugins/cordova-plugin-contacts/www/Contact.js",
"id": "cordova-plugin-contacts.Contact",
"clobbers": [
"Contact"
]
},
{
"file": "plugins/cordova-plugin-contacts/www/ContactAddress.js",
"id": "cordova-plugin-contacts.ContactAddress",
"clobbers": [
"ContactAddress"
]
},
{
"file": "plugins/cordova-plugin-contacts/www/ContactError.js",
"id": "cordova-plugin-contacts.ContactError",
"clobbers": [
"ContactError"
]
},
{
"file": "plugins/cordova-plugin-contacts/www/ContactField.js",
"id": "cordova-plugin-contacts.ContactField",
"clobbers": [
"ContactField"
]
},
{
"file": "plugins/cordova-plugin-contacts/www/ContactFindOptions.js",
"id": "cordova-plugin-contacts.ContactFindOptions",
"clobbers": [
"ContactFindOptions"
]
},
{
"file": "plugins/cordova-plugin-contacts/www/ContactName.js",
"id": "cordova-plugin-contacts.ContactName",
"clobbers": [
"ContactName"
]
},
{
"file": "plugins/cordova-plugin-contacts/www/ContactOrganization.js",
"id": "cordova-plugin-contacts.ContactOrganization",
"clobbers": [
"ContactOrganization"
]
},
{
"file": "plugins/cordova-plugin-contacts/www/ContactFieldType.js",
"id": "cordova-plugin-contacts.ContactFieldType",
"merges": [
""
]
}
];
module.exports.metadata =
@ -140,7 +203,8 @@ module.exports.metadata =
"cordova-plugin-camera": "1.2.0",
"cordova-plugin-dialogs": "1.1.1",
"cordova-plugin-vibration": "1.2.0",
"cordova-plugin-statusbar": "1.0.1"
"cordova-plugin-statusbar": "1.0.1",
"cordova-plugin-contacts": "1.1.0"
}
// BOTTOM OF METADATA
});

View File

@ -0,0 +1,179 @@
cordova.define("cordova-plugin-contacts.Contact", function(require, exports, module) { /*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*/
var argscheck = require('cordova/argscheck'),
exec = require('cordova/exec'),
ContactError = require('./ContactError'),
utils = require('cordova/utils');
/**
* Converts primitives into Complex Object
* Currently only used for Date fields
*/
function convertIn(contact) {
var value = contact.birthday;
try {
contact.birthday = new Date(parseFloat(value));
} catch (exception){
console.log("Cordova Contact convertIn error: exception creating date.");
}
return contact;
}
/**
* Converts Complex objects into primitives
* Only conversion at present is for Dates.
**/
function convertOut(contact) {
var value = contact.birthday;
if (value !== null) {
// try to make it a Date object if it is not already
if (!utils.isDate(value)){
try {
value = new Date(value);
} catch(exception){
value = null;
}
}
if (utils.isDate(value)){
value = value.valueOf(); // convert to milliseconds
}
contact.birthday = value;
}
return contact;
}
/**
* Contains information about a single contact.
* @constructor
* @param {DOMString} id unique identifier
* @param {DOMString} displayName
* @param {ContactName} name
* @param {DOMString} nickname
* @param {Array.<ContactField>} phoneNumbers array of phone numbers
* @param {Array.<ContactField>} emails array of email addresses
* @param {Array.<ContactAddress>} addresses array of addresses
* @param {Array.<ContactField>} ims instant messaging user ids
* @param {Array.<ContactOrganization>} organizations
* @param {DOMString} birthday contact's birthday
* @param {DOMString} note user notes about contact
* @param {Array.<ContactField>} photos
* @param {Array.<ContactField>} categories
* @param {Array.<ContactField>} urls contact's web sites
*/
var Contact = function (id, displayName, name, nickname, phoneNumbers, emails, addresses,
ims, organizations, birthday, note, photos, categories, urls) {
this.id = id || null;
this.rawId = null;
this.displayName = displayName || null;
this.name = name || null; // ContactName
this.nickname = nickname || null;
this.phoneNumbers = phoneNumbers || null; // ContactField[]
this.emails = emails || null; // ContactField[]
this.addresses = addresses || null; // ContactAddress[]
this.ims = ims || null; // ContactField[]
this.organizations = organizations || null; // ContactOrganization[]
this.birthday = birthday || null;
this.note = note || null;
this.photos = photos || null; // ContactField[]
this.categories = categories || null; // ContactField[]
this.urls = urls || null; // ContactField[]
};
/**
* Removes contact from device storage.
* @param successCB success callback
* @param errorCB error callback
*/
Contact.prototype.remove = function(successCB, errorCB) {
argscheck.checkArgs('FF', 'Contact.remove', arguments);
var fail = errorCB && function(code) {
errorCB(new ContactError(code));
};
if (this.id === null) {
fail(ContactError.UNKNOWN_ERROR);
}
else {
exec(successCB, fail, "Contacts", "remove", [this.id]);
}
};
/**
* Creates a deep copy of this Contact.
* With the contact ID set to null.
* @return copy of this Contact
*/
Contact.prototype.clone = function() {
var clonedContact = utils.clone(this);
clonedContact.id = null;
clonedContact.rawId = null;
function nullIds(arr) {
if (arr) {
for (var i = 0; i < arr.length; ++i) {
arr[i].id = null;
}
}
}
// Loop through and clear out any id's in phones, emails, etc.
nullIds(clonedContact.phoneNumbers);
nullIds(clonedContact.emails);
nullIds(clonedContact.addresses);
nullIds(clonedContact.ims);
nullIds(clonedContact.organizations);
nullIds(clonedContact.categories);
nullIds(clonedContact.photos);
nullIds(clonedContact.urls);
return clonedContact;
};
/**
* Persists contact to device storage.
* @param successCB success callback
* @param errorCB error callback
*/
Contact.prototype.save = function(successCB, errorCB) {
argscheck.checkArgs('FFO', 'Contact.save', arguments);
var fail = errorCB && function(code) {
errorCB(new ContactError(code));
};
var success = function(result) {
if (result) {
if (successCB) {
var fullContact = require('./contacts').create(result);
successCB(convertIn(fullContact));
}
}
else {
// no Entry object returned
fail(ContactError.UNKNOWN_ERROR);
}
};
var dupContact = convertOut(utils.clone(this));
exec(success, fail, "Contacts", "save", [dupContact]);
};
module.exports = Contact;
});

View File

@ -0,0 +1,48 @@
cordova.define("cordova-plugin-contacts.ContactAddress", function(require, exports, module) { /*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*/
/**
* Contact address.
* @constructor
* @param {DOMString} id unique identifier, should only be set by native code
* @param formatted // NOTE: not a W3C standard
* @param streetAddress
* @param locality
* @param region
* @param postalCode
* @param country
*/
var ContactAddress = function(pref, type, formatted, streetAddress, locality, region, postalCode, country) {
this.id = null;
this.pref = (typeof pref != 'undefined' ? pref : false);
this.type = type || null;
this.formatted = formatted || null;
this.streetAddress = streetAddress || null;
this.locality = locality || null;
this.region = region || null;
this.postalCode = postalCode || null;
this.country = country || null;
};
module.exports = ContactAddress;
});

View File

@ -0,0 +1,44 @@
cordova.define("cordova-plugin-contacts.ContactError", function(require, exports, module) { /*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*/
/**
* ContactError.
* An error code assigned by an implementation when an error has occurred
* @constructor
*/
var ContactError = function(err) {
this.code = (typeof err != 'undefined' ? err : null);
};
/**
* Error codes
*/
ContactError.UNKNOWN_ERROR = 0;
ContactError.INVALID_ARGUMENT_ERROR = 1;
ContactError.TIMEOUT_ERROR = 2;
ContactError.PENDING_OPERATION_ERROR = 3;
ContactError.IO_ERROR = 4;
ContactError.NOT_SUPPORTED_ERROR = 5;
ContactError.PERMISSION_DENIED_ERROR = 20;
module.exports = ContactError;
});

View File

@ -0,0 +1,39 @@
cordova.define("cordova-plugin-contacts.ContactField", function(require, exports, module) { /*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*/
/**
* Generic contact field.
* @constructor
* @param {DOMString} id unique identifier, should only be set by native code // NOTE: not a W3C standard
* @param type
* @param value
* @param pref
*/
var ContactField = function(type, value, pref) {
this.id = null;
this.type = (type && type.toString()) || null;
this.value = (value && value.toString()) || null;
this.pref = (typeof pref != 'undefined' ? pref : false);
};
module.exports = ContactField;
});

View File

@ -0,0 +1,57 @@
cordova.define("cordova-plugin-contacts.ContactFieldType", function(require, exports, module) { /*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*/
// Possible field names for various platforms.
// Some field names are platform specific
var fieldType = {
addresses: "addresses",
birthday: "birthday",
categories: "categories",
country: "country",
department: "department",
displayName: "displayName",
emails: "emails",
familyName: "familyName",
formatted: "formatted",
givenName: "givenName",
honorificPrefix: "honorificPrefix",
honorificSuffix: "honorificSuffix",
id: "id",
ims: "ims",
locality: "locality",
middleName: "middleName",
name: "name",
nickname: "nickname",
note: "note",
organizations: "organizations",
phoneNumbers: "phoneNumbers",
photos: "photos",
postalCode: "postalCode",
region: "region",
streetAddress: "streetAddress",
title: "title",
urls: "urls"
};
module.exports = fieldType;
});

View File

@ -0,0 +1,37 @@
cordova.define("cordova-plugin-contacts.ContactFindOptions", function(require, exports, module) { /*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*/
/**
* ContactFindOptions.
* @constructor
* @param filter used to match contacts against
* @param multiple boolean used to determine if more than one contact should be returned
*/
var ContactFindOptions = function(filter, multiple, desiredFields) {
this.filter = filter || '';
this.multiple = (typeof multiple != 'undefined' ? multiple : false);
this.desiredFields = typeof desiredFields != 'undefined' ? desiredFields : [];
};
module.exports = ContactFindOptions;
});

View File

@ -0,0 +1,43 @@
cordova.define("cordova-plugin-contacts.ContactName", function(require, exports, module) { /*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*/
/**
* Contact name.
* @constructor
* @param formatted // NOTE: not part of W3C standard
* @param familyName
* @param givenName
* @param middle
* @param prefix
* @param suffix
*/
var ContactName = function(formatted, familyName, givenName, middle, prefix, suffix) {
this.formatted = formatted || null;
this.familyName = familyName || null;
this.givenName = givenName || null;
this.middleName = middle || null;
this.honorificPrefix = prefix || null;
this.honorificSuffix = suffix || null;
};
module.exports = ContactName;
});

View File

@ -0,0 +1,43 @@
cordova.define("cordova-plugin-contacts.ContactOrganization", function(require, exports, module) { /*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*/
/**
* Contact organization.
* @constructor
* @param pref
* @param type
* @param name
* @param dept
* @param title
*/
var ContactOrganization = function(pref, type, name, dept, title) {
this.id = null;
this.pref = (typeof pref != 'undefined' ? pref : false);
this.type = type || null;
this.name = name || null;
this.department = dept || null;
this.title = title || null;
};
module.exports = ContactOrganization;
});

View File

@ -0,0 +1,100 @@
cordova.define("cordova-plugin-contacts.contacts", function(require, exports, module) { /*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*/
var argscheck = require('cordova/argscheck'),
exec = require('cordova/exec'),
ContactError = require('./ContactError'),
utils = require('cordova/utils'),
Contact = require('./Contact'),
fieldType = require('./ContactFieldType');
/**
* Represents a group of Contacts.
* @constructor
*/
var contacts = {
fieldType: fieldType,
/**
* Returns an array of Contacts matching the search criteria.
* @param fields that should be searched
* @param successCB success callback
* @param errorCB error callback
* @param {ContactFindOptions} options that can be applied to contact searching
* @return array of Contacts matching search criteria
*/
find:function(fields, successCB, errorCB, options) {
argscheck.checkArgs('afFO', 'contacts.find', arguments);
if (!fields.length) {
errorCB && errorCB(new ContactError(ContactError.INVALID_ARGUMENT_ERROR));
} else {
// missing 'options' param means return all contacts
options = options || {filter: '', multiple: true}
var win = function(result) {
var cs = [];
for (var i = 0, l = result.length; i < l; i++) {
cs.push(contacts.create(result[i]));
}
successCB(cs);
};
exec(win, errorCB, "Contacts", "search", [fields, options]);
}
},
/**
* This function picks contact from phone using contact picker UI
* @returns new Contact object
*/
pickContact: function (successCB, errorCB) {
argscheck.checkArgs('fF', 'contacts.pick', arguments);
var win = function (result) {
// if Contacts.pickContact return instance of Contact object
// don't create new Contact object, use current
var contact = result instanceof Contact ? result : contacts.create(result);
successCB(contact);
};
exec(win, errorCB, "Contacts", "pickContact", []);
},
/**
* This function creates a new contact, but it does not persist the contact
* to device storage. To persist the contact to device storage, invoke
* contact.save().
* @param properties an object whose properties will be examined to create a new Contact
* @returns new Contact object
*/
create:function(properties) {
argscheck.checkArgs('O', 'contacts.create', arguments);
var contact = new Contact();
for (var i in properties) {
if (typeof contact[i] !== 'undefined' && properties.hasOwnProperty(i)) {
contact[i] = properties[i];
}
}
return contact;
}
};
module.exports = contacts;
});

View File

@ -0,0 +1,192 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* 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 org.apache.cordova.contacts;
import java.util.HashMap;
import android.util.Log;
import org.apache.cordova.CordovaInterface;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* This abstract class defines SDK-independent API for communication with
* Contacts Provider. The actual implementation used by the application depends
* on the level of API available on the device. If the API level is Cupcake or
* Donut, we want to use the {@link ContactAccessorSdk3_4} class. If it is
* Eclair or higher, we want to use {@link ContactAccessorSdk5}.
*/
public abstract class ContactAccessor {
protected final String LOG_TAG = "ContactsAccessor";
protected CordovaInterface mApp;
/**
* Check to see if the data associated with the key is required to
* be populated in the Contact object.
* @param key
* @param map created by running buildPopulationSet.
* @return true if the key data is required
*/
protected boolean isRequired(String key, HashMap<String,Boolean> map) {
Boolean retVal = map.get(key);
return (retVal == null) ? false : retVal.booleanValue();
}
/**
* Create a hash map of what data needs to be populated in the Contact object
* @param fields the list of fields to populate
* @return the hash map of required data
*/
protected HashMap<String, Boolean> buildPopulationSet(JSONObject options) {
HashMap<String, Boolean> map = new HashMap<String, Boolean>();
String key;
try {
JSONArray desiredFields = null;
if (options!=null && options.has("desiredFields")) {
desiredFields = options.getJSONArray("desiredFields");
}
if (desiredFields == null || desiredFields.length() == 0) {
map.put("displayName", true);
map.put("name", true);
map.put("nickname", true);
map.put("phoneNumbers", true);
map.put("emails", true);
map.put("addresses", true);
map.put("ims", true);
map.put("organizations", true);
map.put("birthday", true);
map.put("note", true);
map.put("urls", true);
map.put("photos", true);
map.put("categories", true);
} else {
for (int i = 0; i < desiredFields.length(); i++) {
key = desiredFields.getString(i);
if (key.startsWith("displayName")) {
map.put("displayName", true);
} else if (key.startsWith("name")) {
map.put("displayName", true);
map.put("name", true);
} else if (key.startsWith("nickname")) {
map.put("nickname", true);
} else if (key.startsWith("phoneNumbers")) {
map.put("phoneNumbers", true);
} else if (key.startsWith("emails")) {
map.put("emails", true);
} else if (key.startsWith("addresses")) {
map.put("addresses", true);
} else if (key.startsWith("ims")) {
map.put("ims", true);
} else if (key.startsWith("organizations")) {
map.put("organizations", true);
} else if (key.startsWith("birthday")) {
map.put("birthday", true);
} else if (key.startsWith("note")) {
map.put("note", true);
} else if (key.startsWith("urls")) {
map.put("urls", true);
} else if (key.startsWith("photos")) {
map.put("photos", true);
} else if (key.startsWith("categories")) {
map.put("categories", true);
}
}
}
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
return map;
}
/**
* Convenience method to get a string from a JSON object. Saves a
* lot of try/catch writing.
* If the property is not found in the object null will be returned.
*
* @param obj contact object to search
* @param property to be looked up
* @return The value of the property
*/
protected String getJsonString(JSONObject obj, String property) {
String value = null;
try {
if (obj != null) {
value = obj.getString(property);
if (value.equals("null")) {
Log.d(LOG_TAG, property + " is string called 'null'");
value = null;
}
}
}
catch (JSONException e) {
Log.d(LOG_TAG, "Could not get = " + e.getMessage());
}
return value;
}
/**
* Handles adding a JSON Contact object into the database.
* @return TODO
*/
public abstract String save(JSONObject contact);
/**
* Handles searching through SDK-specific contacts API.
*/
public abstract JSONArray search(JSONArray filter, JSONObject options);
/**
* Handles searching through SDK-specific contacts API.
* @throws JSONException
*/
public abstract JSONObject getContactById(String id) throws JSONException;
/**
* Handles searching through SDK-specific contacts API.
* @param desiredFields fields that will filled. All fields will be filled if null
* @throws JSONException
*/
public abstract JSONObject getContactById(String id, JSONArray desiredFields) throws JSONException;
/**
* Handles removing a contact from the database.
*/
public abstract boolean remove(String id);
/**
* A class that represents the where clause to be used in the database query
*/
class WhereOptions {
private String where;
private String[] whereArgs;
public void setWhere(String where) {
this.where = where;
}
public String getWhere() {
return where;
}
public void setWhereArgs(String[] whereArgs) {
this.whereArgs = whereArgs;
}
public String[] getWhereArgs() {
return whereArgs;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,59 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 org.apache.cordova.contacts;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;
public class ContactInfoDTO {
String displayName;
JSONObject name;
JSONArray organizations;
JSONArray addresses;
JSONArray phones;
JSONArray emails;
JSONArray ims;
JSONArray websites;
JSONArray photos;
String note;
String nickname;
String birthday;
HashMap<String, Object> desiredFieldsWithVals;
public ContactInfoDTO() {
displayName = "";
name = new JSONObject();
organizations = new JSONArray();
addresses = new JSONArray();
phones = new JSONArray();
emails = new JSONArray();
ims = new JSONArray();
websites = new JSONArray();
photos = new JSONArray();
note = "";
nickname = "";
desiredFieldsWithVals = new HashMap<String, Object>();
}
}

View File

@ -0,0 +1,191 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 org.apache.cordova.contacts;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.RawContacts;
import android.util.Log;
public class ContactManager extends CordovaPlugin {
private ContactAccessor contactAccessor;
private CallbackContext callbackContext; // The callback context from which we were invoked.
private JSONArray executeArgs;
private static final String LOG_TAG = "Contact Query";
public static final int UNKNOWN_ERROR = 0;
public static final int INVALID_ARGUMENT_ERROR = 1;
public static final int TIMEOUT_ERROR = 2;
public static final int PENDING_OPERATION_ERROR = 3;
public static final int IO_ERROR = 4;
public static final int NOT_SUPPORTED_ERROR = 5;
public static final int PERMISSION_DENIED_ERROR = 20;
private static final int CONTACT_PICKER_RESULT = 1000;
/**
* Constructor.
*/
public ContactManager() {
}
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArray of arguments for the plugin.
* @param callbackContext The callback context used when calling back into JavaScript.
* @return True if the action was valid, false otherwise.
*/
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
this.callbackContext = callbackContext;
this.executeArgs = args;
/**
* Check to see if we are on an Android 1.X device. If we are return an error as we
* do not support this as of Cordova 1.0.
*/
if (android.os.Build.VERSION.RELEASE.startsWith("1.")) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, ContactManager.NOT_SUPPORTED_ERROR));
return true;
}
/**
* Only create the contactAccessor after we check the Android version or the program will crash
* older phones.
*/
if (this.contactAccessor == null) {
this.contactAccessor = new ContactAccessorSdk5(this.cordova);
}
if (action.equals("search")) {
final JSONArray filter = args.getJSONArray(0);
final JSONObject options = args.get(1) == null ? null : args.getJSONObject(1);
this.cordova.getThreadPool().execute(new Runnable() {
public void run() {
JSONArray res = contactAccessor.search(filter, options);
callbackContext.success(res);
}
});
}
else if (action.equals("save")) {
final JSONObject contact = args.getJSONObject(0);
this.cordova.getThreadPool().execute(new Runnable(){
public void run() {
JSONObject res = null;
String id = contactAccessor.save(contact);
if (id != null) {
try {
res = contactAccessor.getContactById(id);
} catch (JSONException e) {
Log.e(LOG_TAG, "JSON fail.", e);
}
}
if (res != null) {
callbackContext.success(res);
} else {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, UNKNOWN_ERROR));
}
}
});
}
else if (action.equals("remove")) {
final String contactId = args.getString(0);
this.cordova.getThreadPool().execute(new Runnable() {
public void run() {
if (contactAccessor.remove(contactId)) {
callbackContext.success();
} else {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, UNKNOWN_ERROR));
}
}
});
}
else if (action.equals("pickContact")) {
pickContactAsync();
}
else {
return false;
}
return true;
}
/**
* Launches the Contact Picker to select a single contact.
*/
private void pickContactAsync() {
final CordovaPlugin plugin = (CordovaPlugin) this;
Runnable worker = new Runnable() {
public void run() {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
plugin.cordova.startActivityForResult(plugin, contactPickerIntent, CONTACT_PICKER_RESULT);
}
};
this.cordova.getThreadPool().execute(worker);
}
/**
* Called when user picks contact.
* @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult().
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
* @throws JSONException
*/
public void onActivityResult(int requestCode, int resultCode, final Intent intent) {
if (requestCode == CONTACT_PICKER_RESULT) {
if (resultCode == Activity.RESULT_OK) {
String contactId = intent.getData().getLastPathSegment();
// to populate contact data we require Raw Contact ID
// so we do look up for contact raw id first
Cursor c = this.cordova.getActivity().getContentResolver().query(RawContacts.CONTENT_URI,
new String[] {RawContacts._ID}, RawContacts.CONTACT_ID + " = " + contactId, null, null);
if (!c.moveToFirst()) {
this.callbackContext.error("Error occured while retrieving contact raw id");
return;
}
String id = c.getString(c.getColumnIndex(RawContacts._ID));
c.close();
try {
JSONObject contact = contactAccessor.getContactById(id);
this.callbackContext.success(contact);
return;
} catch (JSONException e) {
Log.e(LOG_TAG, "JSON fail.", e);
}
} else if (resultCode == Activity.RESULT_CANCELED){
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.NO_RESULT, UNKNOWN_ERROR));
return;
}
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, UNKNOWN_ERROR));
}
}
}

View File

@ -65,6 +65,10 @@
<param name="onload" value="true" />
</feature>
<feature name="Contacts">
<param name="android-package" value="org.apache.cordova.contacts.ContactManager"/>
</feature>
<feature name="DappURL">
<param name="android-package" value="io.syng.cordova.plugin.DappURL"/>
<param name="onload" value="true" />