2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 13:47:41 +00:00

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.
This commit is contained in:
Àlex Fiestas
2016-03-06 21:50:58 +01:00
parent 708bcf9928
commit c87b09c5f0
2 changed files with 35 additions and 0 deletions

View File

@@ -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();
}
}
}

View File

@@ -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) {