From c87b09c5f078e4177a2a8f20ac43adf5c2ce6dc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=80lex=20Fiestas?= Date: Sun, 6 Mar 2016 21:50:58 +0100 Subject: [PATCH] If the contact has a photo, include it in the packet Basically adds as base64 in the packet the contact Photo. We should not have to worry about the size of it since it is always 96x96 and android takes care of that. --- .../kdeconnect/Helpers/ContactsHelper.java | 29 +++++++++++++++++++ .../TelephonyPlugin/TelephonyPlugin.java | 6 ++++ 2 files changed, 35 insertions(+) diff --git a/src/org/kde/kdeconnect/Helpers/ContactsHelper.java b/src/org/kde/kdeconnect/Helpers/ContactsHelper.java index 0f4574b4..d2085070 100644 --- a/src/org/kde/kdeconnect/Helpers/ContactsHelper.java +++ b/src/org/kde/kdeconnect/Helpers/ContactsHelper.java @@ -21,12 +21,17 @@ package org.kde.kdeconnect.Helpers; import android.content.Context; +import android.content.res.AssetFileDescriptor; import android.database.Cursor; import android.net.Uri; import android.provider.ContactsContract; import android.provider.ContactsContract.PhoneLookup; +import android.util.Base64; +import android.util.Base64OutputStream; import android.util.Log; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; import java.util.HashMap; import java.util.Map; @@ -75,5 +80,29 @@ public class ContactsHelper { return contactInfo; } + + public static String photoId64Encoded(Context context, String photoId) { + AssetFileDescriptor fd = null; + Uri photoUri = Uri.parse(photoId); + Uri displayPhotoUri = Uri.withAppendedPath(photoUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO); + + byte[] buffer = null; + Base64OutputStream out = null; + ByteArrayOutputStream encodedPhoto = null; + try { + encodedPhoto = new ByteArrayOutputStream(); + out = new Base64OutputStream(encodedPhoto, Base64.DEFAULT); + InputStream fd2 = context.getContentResolver().openInputStream(photoUri); + buffer = new byte[1024]; + int len; + while ((len = fd2.read(buffer)) != -1) { + out.write(buffer, 0, len); + } + return encodedPhoto.toString(); + } catch (Exception ex) { + Log.e("ContactsHelper", ex.toString()); + return new String(); + } + } } diff --git a/src/org/kde/kdeconnect/Plugins/TelephonyPlugin/TelephonyPlugin.java b/src/org/kde/kdeconnect/Plugins/TelephonyPlugin/TelephonyPlugin.java index 5c494db3..23105367 100644 --- a/src/org/kde/kdeconnect/Plugins/TelephonyPlugin/TelephonyPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/TelephonyPlugin/TelephonyPlugin.java @@ -102,8 +102,14 @@ public class TelephonyPlugin extends Plugin { NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_TELEPHONY); if (phoneNumber != null) { np.set("phoneNumber", phoneNumber); + } + + if (contactInfo.containsKey("name")) { np.set("contactName", contactInfo.get("name")); } + + if (contactInfo.containsKey("photoID")) { + np.set("phoneThumbnail", ContactsHelper.photoId64Encoded(context, contactInfo.get("photoID"))); } switch (state) {