Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 98b3b9ad authored by Alon Albert's avatar Alon Albert
Browse files

Support Photo URL in Encoded Contacts

Encoded contacts may have a photo url as an HTTP link rather than a content://
, file:// or android.resource:// URL. This adds support for such photos URLs

Bug: 10671856
Change-Id: If861bca096454dac043c63eea26731bf487fd824
parent db2adcb4
Loading
Loading
Loading
Loading
+21 −9
Original line number Diff line number Diff line
@@ -59,8 +59,9 @@ import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -361,6 +362,7 @@ public class ContactLoader extends AsyncTaskLoader<Contact> {
        final String altDisplayName = json.optString(
                Contacts.DISPLAY_NAME_ALTERNATIVE, displayName);
        final int displayNameSource = json.getInt(Contacts.DISPLAY_NAME_SOURCE);
        final String photoUri = json.optString(Contacts.PHOTO_URI, null);
        final Contact contact = new Contact(
                uri, uri,
                mLookupUri,
@@ -370,7 +372,7 @@ public class ContactLoader extends AsyncTaskLoader<Contact> {
                -1 /* nameRawContactId */,
                displayNameSource,
                -1 /* photoId */,
                null /* photoUri */,
                photoUri,
                displayName,
                altDisplayName,
                null /* phoneticName */,
@@ -489,26 +491,36 @@ public class ContactLoader extends AsyncTaskLoader<Contact> {
     * not found, returns null
     */
    private void loadPhotoBinaryData(Contact contactData) {

        // If we have a photo URI, try loading that first.
        String photoUri = contactData.getPhotoUri();
        if (photoUri != null) {
            try {
                AssetFileDescriptor fd = getContext().getContentResolver()
                       .openAssetFileDescriptor(Uri.parse(photoUri), "r");
                final InputStream inputStream;
                final AssetFileDescriptor fd;
                final Uri uri = Uri.parse(photoUri);
                final String scheme = uri.getScheme();
                if ("http".equals(scheme) || "https".equals(scheme)) {
                    // Support HTTP urls that might come from extended directories
                    inputStream = new URL(photoUri).openStream();
                    fd = null;
                } else {
                    fd = getContext().getContentResolver().openAssetFileDescriptor(uri, "r");
                    inputStream = fd.createInputStream();
                }
                byte[] buffer = new byte[16 * 1024];
                FileInputStream fis = fd.createInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try {
                    int size;
                    while ((size = fis.read(buffer)) != -1) {
                    while ((size = inputStream.read(buffer)) != -1) {
                        baos.write(buffer, 0, size);
                    }
                    contactData.setPhotoBinaryData(baos.toByteArray());
                } finally {
                    fis.close();
                    inputStream.close();
                    if (fd != null) {
                        fd.close();
                    }
                }
                return;
            } catch (IOException ioe) {
                // Just fall back to the case below.