Also use Glide for contacts' avatar
This commit is contained in:
parent
25d4c1b430
commit
fadcdf0825
2 changed files with 46 additions and 87 deletions
|
@ -32,65 +32,14 @@ import android.provider.MediaStore;
|
||||||
import android.util.DisplayMetrics;
|
import android.util.DisplayMetrics;
|
||||||
import android.util.TypedValue;
|
import android.util.TypedValue;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import java.io.IOException;
|
import com.bumptech.glide.Glide;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.URL;
|
|
||||||
import org.linphone.R;
|
import org.linphone.R;
|
||||||
import org.linphone.contacts.ContactsManager;
|
|
||||||
import org.linphone.core.tools.Log;
|
|
||||||
|
|
||||||
public class ImageUtils {
|
public class ImageUtils {
|
||||||
|
|
||||||
public static Bitmap downloadBitmap(Uri uri) {
|
|
||||||
URL url;
|
|
||||||
InputStream is = null;
|
|
||||||
try {
|
|
||||||
url = new URL(uri.toString());
|
|
||||||
is = url.openStream();
|
|
||||||
return BitmapFactory.decodeStream(is);
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e(e, e.getMessage());
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
is.close();
|
|
||||||
} catch (IOException x) {
|
|
||||||
Log.e(x);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void setImagePictureFromUri(
|
public static void setImagePictureFromUri(
|
||||||
Context c, ImageView view, Uri pictureUri, Uri thumbnailUri) {
|
Context c, ImageView view, Uri pictureUri, Uri thumbnailUri) {
|
||||||
if (pictureUri == null && thumbnailUri == null) {
|
Glide.with(c).load(pictureUri).thumbnail(Glide.with(c).load(thumbnailUri)).into(view);
|
||||||
view.setImageBitmap(ContactsManager.getInstance().getDefaultAvatarBitmap());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (pictureUri.getScheme().startsWith("http")) {
|
|
||||||
Bitmap bm = downloadBitmap(pictureUri);
|
|
||||||
if (bm == null) view.setImageResource(R.drawable.avatar);
|
|
||||||
view.setImageBitmap(bm);
|
|
||||||
} else {
|
|
||||||
Bitmap bm = null;
|
|
||||||
try {
|
|
||||||
bm = MediaStore.Images.Media.getBitmap(c.getContentResolver(), pictureUri);
|
|
||||||
} catch (IOException e) {
|
|
||||||
if (thumbnailUri != null) {
|
|
||||||
try {
|
|
||||||
bm =
|
|
||||||
MediaStore.Images.Media.getBitmap(
|
|
||||||
c.getContentResolver(), thumbnailUri);
|
|
||||||
} catch (IOException ie) {
|
|
||||||
Log.e(ie);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (bm != null) {
|
|
||||||
view.setImageBitmap(bm);
|
|
||||||
} else {
|
|
||||||
view.setImageBitmap(ContactsManager.getInstance().getDefaultAvatarBitmap());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Bitmap getRoundBitmapFromUri(Context context, Uri fromPictureUri) {
|
public static Bitmap getRoundBitmapFromUri(Context context, Uri fromPictureUri) {
|
||||||
|
|
|
@ -19,20 +19,23 @@ along with this program; if not, write to the Free Software
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import android.graphics.Bitmap;
|
import android.content.Context;
|
||||||
import android.provider.MediaStore;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import java.io.IOException;
|
import androidx.annotation.Nullable;
|
||||||
|
import com.bumptech.glide.Glide;
|
||||||
|
import com.bumptech.glide.load.DataSource;
|
||||||
|
import com.bumptech.glide.load.engine.GlideException;
|
||||||
|
import com.bumptech.glide.request.RequestListener;
|
||||||
|
import com.bumptech.glide.request.target.Target;
|
||||||
import org.linphone.LinphoneService;
|
import org.linphone.LinphoneService;
|
||||||
import org.linphone.R;
|
import org.linphone.R;
|
||||||
import org.linphone.contacts.LinphoneContact;
|
import org.linphone.contacts.LinphoneContact;
|
||||||
import org.linphone.core.ChatRoomSecurityLevel;
|
import org.linphone.core.ChatRoomSecurityLevel;
|
||||||
import org.linphone.core.tools.Log;
|
|
||||||
import org.linphone.utils.ImageUtils;
|
|
||||||
|
|
||||||
class ContactAvatarHolder {
|
class ContactAvatarHolder implements RequestListener<Drawable> {
|
||||||
public final ImageView contactPicture;
|
public final ImageView contactPicture;
|
||||||
public final ImageView avatarMask;
|
public final ImageView avatarMask;
|
||||||
public final ImageView securityLevel;
|
public final ImageView securityLevel;
|
||||||
|
@ -50,6 +53,27 @@ class ContactAvatarHolder {
|
||||||
generatedAvatar.setVisibility(View.VISIBLE);
|
generatedAvatar.setVisibility(View.VISIBLE);
|
||||||
securityLevel.setVisibility(View.GONE);
|
securityLevel.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onLoadFailed(
|
||||||
|
@Nullable GlideException e,
|
||||||
|
Object model,
|
||||||
|
Target<Drawable> target,
|
||||||
|
boolean isFirstResource) {
|
||||||
|
generatedAvatar.setVisibility(View.VISIBLE);
|
||||||
|
contactPicture.setVisibility(View.GONE);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onResourceReady(
|
||||||
|
Drawable resource,
|
||||||
|
Object model,
|
||||||
|
Target<Drawable> target,
|
||||||
|
DataSource dataSource,
|
||||||
|
boolean isFirstResource) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ContactAvatar {
|
public class ContactAvatar {
|
||||||
|
@ -126,38 +150,24 @@ public class ContactAvatar {
|
||||||
public static void displayAvatar(LinphoneContact contact, View v) {
|
public static void displayAvatar(LinphoneContact contact, View v) {
|
||||||
if (contact == null || v == null) return;
|
if (contact == null || v == null) return;
|
||||||
|
|
||||||
Bitmap bm = null;
|
|
||||||
ContactAvatarHolder holder = new ContactAvatarHolder(v);
|
ContactAvatarHolder holder = new ContactAvatarHolder(v);
|
||||||
holder.init();
|
holder.init();
|
||||||
|
|
||||||
if (contact.getThumbnailUri() != null
|
// Kepp the generated avatar ready in case of failure while loading picture
|
||||||
&& contact.getThumbnailUri().getScheme().startsWith("http")) {
|
holder.generatedAvatar.setText(
|
||||||
bm = ImageUtils.downloadBitmap(contact.getThumbnailUri());
|
generateAvatar(
|
||||||
} else {
|
contact.getFullName() == null
|
||||||
if (contact.getThumbnailUri() != null) {
|
? contact.getFirstName() + " " + contact.getLastName()
|
||||||
try {
|
: contact.getFullName()));
|
||||||
bm =
|
holder.generatedAvatar.setVisibility(View.GONE);
|
||||||
MediaStore.Images.Media.getBitmap(
|
|
||||||
LinphoneService.instance().getContentResolver(),
|
|
||||||
contact.getThumbnailUri());
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bm != null) {
|
Context c = LinphoneService.instance().getApplicationContext();
|
||||||
holder.contactPicture.setImageBitmap(bm);
|
holder.contactPicture.setVisibility(View.VISIBLE);
|
||||||
holder.contactPicture.setVisibility(View.VISIBLE);
|
Glide.with(c)
|
||||||
holder.generatedAvatar.setVisibility(View.GONE);
|
.load(contact.getPhotoUri())
|
||||||
} else {
|
.thumbnail(Glide.with(c).load(contact.getThumbnailUri()))
|
||||||
holder.generatedAvatar.setText(
|
.listener(holder)
|
||||||
generateAvatar(
|
.into(holder.contactPicture);
|
||||||
contact.getFullName() == null
|
|
||||||
? contact.getFirstName() + " " + contact.getLastName()
|
|
||||||
: contact.getFullName()));
|
|
||||||
holder.generatedAvatar.setVisibility(View.VISIBLE);
|
|
||||||
}
|
|
||||||
holder.securityLevel.setVisibility(View.GONE);
|
holder.securityLevel.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue