Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cordova 2.0.1 port #110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 62 additions & 25 deletions Android/ContactView/ContactView.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
package com.rearden;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Base64;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import org.apache.cordova.api.LOG;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;

public class ContactView extends Plugin {
private static final int PICK_CONTACT = 1;
private static final int PICK_CONTACT = 0;
private String callback;
private final String pluginName = "ContactView";

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
LOG.d(pluginName, "ContactView called with options: " + args + " cb: "+ callbackId);
startContactActivity();
PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);
mPlugin.setKeepCallback(true);
Expand All @@ -29,26 +40,46 @@ public PluginResult execute(String action, JSONArray args, String callbackId) {
public void startContactActivity() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
this.ctx.startActivityForResult((Plugin) this, intent, PICK_CONTACT);
this.cordova.startActivityForResult((Plugin) this, intent, PICK_CONTACT);
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
String name = null;
JSONArray phone = new JSONArray();
String number = null;
String email = null;
LOG.d(pluginName, "Pick contact result? : " + (reqCode==PICK_CONTACT));
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = this.ctx.managedQuery(contactData, null, null, null, null);
Cursor c = this.cordova.getActivity().managedQuery(contactData,
null, null, null, null);
if (c.moveToFirst()) {
String ContactID = c.getString(c
long ContactID = c.getLong(c
.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

String hasPhone = c
.getString(c
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
ContentResolver cr = this.cordova.getActivity().getContentResolver();
Uri photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, ContactID);
String photoBase64 = null;
if (photoUri != null) {
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(
cr, photoUri);
if (input != null) {
Bitmap bitmap = BitmapFactory.decodeStream(input);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
photoBase64 = new String(Base64.encode(image, Base64.DEFAULT));
}
}
if (Integer.parseInt(hasPhone) == 1) {
Cursor phoneCursor = this.ctx.managedQuery(
Cursor phoneCursor = this.cordova
.getActivity()
.managedQuery(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
Expand All @@ -57,30 +88,36 @@ public void onActivityResult(int reqCode, int resultCode, Intent data) {
while (phoneCursor.moveToNext()) {
number = phoneCursor
.getString(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phone.put(number);
}
}
// get email address
Cursor emailCur = this.ctx.managedQuery(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
Cursor emailCur = this.cordova.getActivity().managedQuery(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + "='" + ContactID + "'", null,null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
//String emailType = emailCur.getString(
// emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ "='" + ContactID + "'", null, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
email = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
// String emailType = emailCur.getString(
// emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
//emailCur.close();

name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
name = c.getString(c
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
JSONObject contactObject = new JSONObject();
try {
//LOG.d(pluginName, "photo: " + photoBase64);
contactObject.put("name", name);
contactObject.put("phone", number);
contactObject.put("phone", phone);
contactObject.put("email", email);
contactObject.put("photo", photoBase64!=null?photoBase64:JSONObject.NULL);
} catch (JSONException e) {
e.printStackTrace();
}
Expand All @@ -92,4 +129,4 @@ public void onActivityResult(int reqCode, int resultCode, Intent data) {
break;
}
}
}
}
31 changes: 10 additions & 21 deletions Android/ContactView/ContactView.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
var ContactView = function() {};

ContactView.prototype.show = function(successCallback, failCallback) {

function success(args) {
successCallback(args);
var contactView = {
show: function(success, fail){
cordova.exec(function(args) {
success(args)
}, function(args) {
fail(args);
}, "ContactView", "show", []);
}

function fail(args) {
failCallback(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");
});
// optionally add the object to windows.plugins
// windows.plugins.contactView = contactView;