Permalink
Browse files

Added ContactView plugin for Android

  • Loading branch information...
1 parent 99c996f commit ad210d318b17c7679b673ed1959806835384aa35 @grandecomplex grandecomplex committed Jan 7, 2011
Showing with 101 additions and 0 deletions.
  1. +54 −0 Android/ContactView/ContactView.java
  2. +24 −0 Android/ContactView/ContactView.js
  3. +23 −0 Android/ContactView/README.txt
@@ -0,0 +1,54 @@
+package com.rearden;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.app.Activity;
+import android.content.ActivityNotFoundException;
+import android.content.Intent;
+import android.database.Cursor;
+import android.net.Uri;
+import android.provider.ContactsContract;
+import android.util.Log;
+
+import com.phonegap.api.Plugin;
+import com.phonegap.api.PluginResult;
+
+public class ContactView extends Plugin {
+ private static final int PICK_CONTACT = 1;
+ private String name = null;
+ public String callback;
+
+ @Override
+ public PluginResult execute(String action, JSONArray args, String callbackId) {
+ startContactActivity();
+ PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);
+ mPlugin.setKeepCallback(true);
+ this.callback = callbackId;
+ return mPlugin;
+ }
+
+ public void startContactActivity() {
+ Intent intent = new Intent(Intent.ACTION_PICK);
+ intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
+ this.ctx.startActivityForResult((Plugin) this, intent, PICK_CONTACT);
+ }
+
+ @Override
+ public void onActivityResult(int reqCode, int resultCode, Intent data) {
+ 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()) {
+ name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
+ this.success(new PluginResult(PluginResult.Status.OK, name), this.callback);
+ }
+ }
+ break;
+ }
+ }
+
+}
@@ -0,0 +1,24 @@
+var ContactView = function() {};
+
+ContactView.prototype.show = function(element) {
+
+ function success(args) {
+ var el = document.getElementById(element);
+ el.value = args;
+ }
+
+ function fail(args) {
+ alert("fail "+args)
+ }
+
+ return PhoneGap.exec(function(args) {
+ success(args);
+ }, function(args) {
+ fail(args);
+ }, 'ContactView', '', []);
+};
+
+PhoneGap.addConstructor(function() {
+ PhoneGap.addPlugin('contactView', new ContactView());
+ PluginManager.addService("ContactView","com.rearden.ContactView");
+});
@@ -0,0 +1,23 @@
+This plugin is a different philosophy to how contacts can work with PhoneGap Android. Rather than pulling in the contacts into HTML, this plugin allows you to leave the PhoneGap webview and enter into a native contact picker. Once the user has selected a contact, they will be sent back into the PhoneGap webview with the name of the contact available.
+
+
+How to use:
+
+Put the following javascript code in your initializer, after the DOM has loaded:
+
+document.querySelector("#name-contact-to-native").addEventListener("touchstart", function() {
+ window.plugins.contactView.show("contact-name-from-native");
+}, false);
+
+"#name-contact-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
+
+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.
+
+ContactView.js should go in the asset folder and should be referenced in your index.html file.
+
+
+Limitations:
+
+Currently, only the name is grabbed. I'd like to have future versions include telephone number and/or email addresses.

0 comments on commit ad210d3

Please sign in to comment.