2015-06-23 10:15:33 -07:00
|
|
|
package org.thoughtcrime.securesms.contacts.avatars;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-07-24 17:07:33 -07:00
|
|
|
import android.graphics.Bitmap;
|
2015-06-23 10:15:33 -07:00
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.net.Uri;
|
2015-07-27 10:49:14 -07:00
|
|
|
import android.support.annotation.DrawableRes;
|
2015-06-23 10:15:33 -07:00
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.text.TextUtils;
|
2015-07-24 17:07:33 -07:00
|
|
|
|
|
|
|
import com.bumptech.glide.Glide;
|
2015-06-23 10:15:33 -07:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-07-24 17:07:33 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.ContactPhotoUriLoader.ContactPhotoUri;
|
2015-06-23 10:15:33 -07:00
|
|
|
|
2015-07-24 17:07:33 -07:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2015-06-23 10:15:33 -07:00
|
|
|
|
|
|
|
public class ContactPhotoFactory {
|
|
|
|
|
2015-07-27 10:49:14 -07:00
|
|
|
private static final String TAG = ContactPhotoFactory.class.getSimpleName();
|
|
|
|
|
2015-06-23 10:15:33 -07:00
|
|
|
public static ContactPhoto getLoadingPhoto() {
|
|
|
|
return new TransparentContactPhoto();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ContactPhoto getDefaultContactPhoto(@Nullable String name) {
|
|
|
|
if (!TextUtils.isEmpty(name)) return new GeneratedContactPhoto(name);
|
2015-06-23 15:10:50 -07:00
|
|
|
else return new GeneratedContactPhoto("#");
|
2015-06-23 10:15:33 -07:00
|
|
|
}
|
|
|
|
|
2015-07-27 10:49:14 -07:00
|
|
|
public static ContactPhoto getResourceContactPhoto(@DrawableRes int resourceId) {
|
|
|
|
return new ResourceContactPhoto(resourceId);
|
|
|
|
}
|
|
|
|
|
2015-06-23 10:15:33 -07:00
|
|
|
public static ContactPhoto getDefaultGroupPhoto() {
|
2015-06-23 15:10:50 -07:00
|
|
|
return new ResourceContactPhoto(R.drawable.ic_group_white_24dp);
|
2015-06-23 10:15:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static ContactPhoto getContactPhoto(Context context, Uri uri, String name) {
|
2015-12-27 01:34:44 +01:00
|
|
|
int targetSize = context.getResources().getDimensionPixelSize(R.dimen.contact_photo_target_size);
|
|
|
|
return getContactPhoto(context, uri, name, targetSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ContactPhoto getContactPhoto(Context context, Uri uri, String name, int targetSize) {
|
2015-06-23 10:15:33 -07:00
|
|
|
try {
|
2015-07-24 17:07:33 -07:00
|
|
|
Bitmap bitmap = Glide.with(context)
|
|
|
|
.load(new ContactPhotoUri(uri)).asBitmap()
|
|
|
|
.centerCrop().into(targetSize, targetSize).get();
|
|
|
|
return new BitmapContactPhoto(bitmap);
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
return getDefaultContactPhoto(name);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new AssertionError(e);
|
2015-06-23 10:15:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ContactPhoto getGroupContactPhoto(@Nullable byte[] avatar) {
|
|
|
|
if (avatar == null) return getDefaultGroupPhoto();
|
|
|
|
|
|
|
|
return new BitmapContactPhoto(BitmapFactory.decodeByteArray(avatar, 0, avatar.length));
|
|
|
|
}
|
|
|
|
}
|