From 47042f0f126439d40b309e1468cd98a15c933449 Mon Sep 17 00:00:00 2001 From: Mateusz Herych Date: Tue, 23 Apr 2013 11:58:54 +0200 Subject: [PATCH] caching avatars using urls, not userId Closes #349 --- .gitignore | 1 + .../com/github/mobile/util/AvatarLoader.java | 26 ++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 80c551df1..1848b97c7 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ gen-external-apklibs *.iml .DS_Store *.swp +out diff --git a/app/src/main/java/com/github/mobile/util/AvatarLoader.java b/app/src/main/java/com/github/mobile/util/AvatarLoader.java index b6f926c33..b5b844ee5 100644 --- a/app/src/main/java/com/github/mobile/util/AvatarLoader.java +++ b/app/src/main/java/com/github/mobile/util/AvatarLoader.java @@ -25,6 +25,7 @@ import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.text.TextUtils; +import android.util.Base64; import android.util.Log; import android.widget.ImageView; @@ -140,15 +141,19 @@ private Bitmap decode(final File file) { return BitmapFactory.decodeFile(file.getAbsolutePath(), options); } + private String getAvatarFilenameForUrl(String avatarUrl) { + return Base64.encodeToString(avatarUrl.getBytes(), Base64.DEFAULT); + } + /** * Fetch avatar from URL * * @param url - * @param userId + * @param cachedAvatarFilename * @return bitmap */ - protected BitmapDrawable fetchAvatar(final String url, final String userId) { - File rawAvatar = new File(avatarDir, userId + "-raw"); + protected BitmapDrawable fetchAvatar(final String url, final String cachedAvatarFilename) { + File rawAvatar = new File(avatarDir, cachedAvatarFilename + "-raw"); HttpRequest request = HttpRequest.get(url); if (request.ok()) request.receive(rawAvatar); @@ -168,7 +173,7 @@ protected BitmapDrawable fetchAvatar(final String url, final String userId) { return null; } - File roundedAvatar = new File(avatarDir, userId); + File roundedAvatar = new File(avatarDir, cachedAvatarFilename); FileOutputStream output = null; try { output = new FileOutputStream(roundedAvatar); @@ -232,11 +237,12 @@ public AvatarLoader bind(final ActionBar actionBar, @Override public BitmapDrawable call() throws Exception { - final BitmapDrawable image = getImageBy(userId); + final String avatarFilename = getAvatarFilenameForUrl(getAvatarUrl(user)); + final BitmapDrawable image = getImageBy(avatarFilename); if (image != null) return image; else - return fetchAvatar(avatarUrl, userId); + return fetchAvatar(avatarUrl, avatarFilename); } @Override @@ -361,16 +367,16 @@ public BitmapDrawable call() throws Exception { if (!userId.equals(view.getTag(id.iv_avatar))) return null; - final BitmapDrawable image = getImageBy(userId); + final String avatarFilename = getAvatarFilenameForUrl(avatarUrl); + final BitmapDrawable image = getImageBy(avatarFilename); if (image != null) return image; else - return fetchAvatar(avatarUrl, userId); + return fetchAvatar(avatarUrl, avatarFilename); } @Override - protected void onSuccess(final BitmapDrawable image) - throws Exception { + protected void onSuccess(final BitmapDrawable image) throws Exception { if (image == null) return; loaded.put(userId, image);