diff --git a/Android/ContactView/ContactView.java b/Android/ContactView/ContactView.java index 9f5a4e3d..f570dc7b 100644 --- a/Android/ContactView/ContactView.java +++ b/Android/ContactView/ContactView.java @@ -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); @@ -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 @@ -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(); } @@ -92,4 +129,4 @@ public void onActivityResult(int reqCode, int resultCode, Intent data) { break; } } -} +} \ No newline at end of file diff --git a/Android/ContactView/ContactView.js b/Android/ContactView/ContactView.js index 0f6c7a9d..44caabe2 100644 --- a/Android/ContactView/ContactView.js +++ b/Android/ContactView/ContactView.js @@ -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"); -}); \ No newline at end of file +// optionally add the object to windows.plugins +// windows.plugins.contactView = contactView; \ No newline at end of file