Permalink
Browse files

Added phone to the plugin

  • Loading branch information...
1 parent 867409b commit 4a67afe6f27b933dc97cf7ee2332a5152c7c0c01 @grandecomplex grandecomplex committed Jan 7, 2011
Showing with 35 additions and 7 deletions.
  1. +27 −1 Android/ContactView/ContactView.java
  2. +4 −3 Android/ContactView/ContactView.js
  3. +4 −3 Android/ContactView/README.txt
@@ -1,12 +1,15 @@
package com.rearden;
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.net.Uri;
import android.provider.ContactsContract;
+
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
@@ -32,14 +35,37 @@ public void startContactActivity() {
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
String name = null;
+ String number = null;
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = this.ctx.managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
+ String ContactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
+ String hasPhone =c.getString(
+ c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
+
+ if(Integer.parseInt(hasPhone) == 1){
+ Cursor phoneCursor = this.ctx.managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
+ null,
+ ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"='"+ContactID+"'",
+ null, null);
+ while(phoneCursor.moveToNext()){
+ number = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
+ }
+ }
+
name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
- this.success(new PluginResult(PluginResult.Status.OK, name),this.callback);
+ JSONObject contactObject = new JSONObject();
+ try {
+ contactObject.put("name", name);
+ contactObject.put("phone", number);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+
+ this.success(new PluginResult(PluginResult.Status.OK, contactObject),this.callback);
}
}
break;
@@ -1,10 +1,11 @@
var ContactView = function() {};
-ContactView.prototype.show = function(element) {
+ContactView.prototype.show = function(nameElement, phoneElement) {
function success(args) {
- var el = document.getElementById(element);
- el.value = args;
+ alert(args.name +", "+ args.phone);
+ nameElement.value = typeof args.name != "undefined" ? args.name : "";
+ phoneElement.value = typeof args.phone != "undefined" ? args.phone : "";
}
function fail(args) {
@@ -6,12 +6,12 @@ How to use:
Put the following javascript code in your initializer, after the DOM has loaded:
document.querySelector("#contact-name-to-native").addEventListener("touchstart", function() {
- window.plugins.contactView.show("contact-name-from-native");
+ window.plugins.contactView.show(document.getElementById("contact-name-from-native"), document.getElementById("phonenumber-contact"));
}, false);
"#contact-name-to-native" is the element id that triggers the contact view.
-"contact-name-from-native" is the element id in html that receives the contact's name. By convention, it is an input field's value that receives it. But you can edit this in ContactView.js
+"contact-name-from-native" is the element id in html that receives the contact's name. By convention, it is an input field's value that receives it. And "phonenumber-contact" is the phone number input field in the HTML. But you can edit this in ContactView.js to ignore phone or name.
For the current files to work, you'll need to create a package (folders) called com.rearden. You can change this to whatever you like, just update the ContactView.js and ContactView.java.
@@ -20,4 +20,5 @@ ContactView.js should go in the asset folder and should be referenced in your in
Limitations:
-Currently, only the name is grabbed. I'd like to have future versions include telephone number and/or email addresses.
+It only grabs the Name and Phone number, but not any other information.
+It only works with Android API 2.0 and above. Future versions will include older APIs.

0 comments on commit 4a67afe

Please sign in to comment.