From 8d9becff304c58606ae2d2142e7f2e1fe0343526 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Thu, 12 Oct 2017 08:42:18 +0200 Subject: [PATCH] trying to first show old one, but asyncDrawable is not working --- .../datamodel/ThumbnailsCacheManager.java | 10 +++++-- .../android/ui/activity/DrawerActivity.java | 4 +-- .../android/ui/activity/UserInfoActivity.java | 5 ++-- .../ui/adapter/AccountListAdapter.java | 6 ++-- .../owncloud/android/utils/DisplayUtils.java | 29 +++++++++++++++---- 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java b/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java index 2ffc5af2c48d..e1c1cc7fa237 100644 --- a/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java +++ b/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java @@ -73,7 +73,7 @@ public class ThumbnailsCacheManager { private static final String TAG = ThumbnailsCacheManager.class.getSimpleName(); private static final String CACHE_FOLDER = "thumbnailCache"; - private static final String AVATAR = "avatar"; + public static final String AVATAR = "avatar"; private static final String ETAG = "ETag"; private static final Object mThumbnailsDiskCacheLock = new Object(); @@ -648,6 +648,12 @@ Drawable doAvatarInBackground() { final String imageKey = "a_" + username + "_" + eTag; + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + int px = getAvatarDimension(); // Download avatar from server @@ -899,7 +905,7 @@ public MediaThumbnailGenerationTask getBitmapWorkerTask() { public static class AsyncAvatarDrawable extends BitmapDrawable { private final WeakReference avatarWorkerTaskReference; - public AsyncAvatarDrawable(Resources res, Bitmap bitmap, AvatarGenerationTask avatarWorkerTask) { + public AsyncAvatarDrawable(Resources res, Drawable bitmap, AvatarGenerationTask avatarWorkerTask) { super(res, bitmap); avatarWorkerTaskReference = new WeakReference<>(avatarWorkerTask); } diff --git a/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java b/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java index 3e70e6feba48..d9b27c20b949 100644 --- a/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java @@ -750,8 +750,8 @@ protected void setAccountInDrawer(Account account) { View currentAccountView = findNavigationViewChildById(R.id.drawer_current_account); currentAccountView.setTag(account.name); - DisplayUtils.setAvatar(account, this, - mCurrentAccountAvatarRadiusDimension, getResources(), getStorageManager(), currentAccountView); + DisplayUtils.setAvatar(account, this, mCurrentAccountAvatarRadiusDimension, getResources(), + getStorageManager(), currentAccountView); // check and show quota info if available getAndDisplayUserQuota(); diff --git a/src/main/java/com/owncloud/android/ui/activity/UserInfoActivity.java b/src/main/java/com/owncloud/android/ui/activity/UserInfoActivity.java index 03238cb99aac..734e0e5026db 100644 --- a/src/main/java/com/owncloud/android/ui/activity/UserInfoActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/UserInfoActivity.java @@ -310,8 +310,9 @@ public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) { private void populateUserInfoUi(UserInfo userInfo) { userName.setText(account.name); - DisplayUtils.setAvatar(account, UserInfoActivity.this, - mCurrentAccountAvatarRadiusDimension, getResources(), getStorageManager(), avatar); + avatar.setTag(account.name); + DisplayUtils.setAvatar(account, UserInfoActivity.this, mCurrentAccountAvatarRadiusDimension, getResources(), + getStorageManager(), avatar); int tint = ThemeUtils.primaryColor(account); diff --git a/src/main/java/com/owncloud/android/ui/adapter/AccountListAdapter.java b/src/main/java/com/owncloud/android/ui/adapter/AccountListAdapter.java index 42e652e39c03..309aedaf5453 100644 --- a/src/main/java/com/owncloud/android/ui/adapter/AccountListAdapter.java +++ b/src/main/java/com/owncloud/android/ui/adapter/AccountListAdapter.java @@ -150,8 +150,10 @@ private void setCurrentlyActiveState(AccountViewHolderItem viewHolder, Account a private void setAvatar(AccountViewHolderItem viewHolder, Account account) { try { - DisplayUtils.setAvatar(account, this, mAccountAvatarRadiusDimension, - mContext.getResources(), mContext.getStorageManager(), viewHolder.imageViewItem); + View viewItem = viewHolder.imageViewItem; + viewItem.setTag(account.name); + DisplayUtils.setAvatar(account, this, mAccountAvatarRadiusDimension, mContext.getResources(), + mContext.getStorageManager(), viewItem); } catch (Exception e) { Log_OC.e(TAG, "Error calculating RGB value for account list item.", e); // use user icon as a fallback diff --git a/src/main/java/com/owncloud/android/utils/DisplayUtils.java b/src/main/java/com/owncloud/android/utils/DisplayUtils.java index 211fac16d7be..3955f62591a6 100644 --- a/src/main/java/com/owncloud/android/utils/DisplayUtils.java +++ b/src/main/java/com/owncloud/android/utils/DisplayUtils.java @@ -30,7 +30,6 @@ import android.content.Context; import android.content.Intent; import android.content.res.Resources; -import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.Point; import android.graphics.drawable.Drawable; @@ -57,12 +56,14 @@ import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.authentication.AccountUtils; +import com.owncloud.android.datamodel.ArbitraryDataProvider; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.ThumbnailsCacheManager; import com.owncloud.android.lib.common.OwnCloudAccount; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.resources.files.SearchOperation; +import com.owncloud.android.ui.TextDrawable; import com.owncloud.android.ui.activity.FileDisplayActivity; import com.owncloud.android.ui.events.MenuItemClickEvent; import com.owncloud.android.ui.events.SearchEvent; @@ -420,22 +421,40 @@ public interface AvatarGenerationListener { * @param resources reference for density information * @param storageManager reference for caching purposes */ - public static void setAvatar(Account account, AvatarGenerationListener listener, float avatarRadius, Resources resources, - FileDataStorageManager storageManager, Object callContext) { + public static void setAvatar(Account account, AvatarGenerationListener listener, float avatarRadius, + Resources resources, FileDataStorageManager storageManager, Object callContext) { if (account != null) { if (callContext instanceof View) { ((View) callContext).setContentDescription(account.name); } - Bitmap thumbnail = null; + ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider( + MainApp.getAppContext().getContentResolver()); + String eTag = arbitraryDataProvider.getValue(account, ThumbnailsCacheManager.AVATAR); + + // first show old one + Drawable avatar = BitmapUtils.bitmapToCircularBitmapDrawable(resources, + ThumbnailsCacheManager.getBitmapFromDiskCache("a_" + account.name + "_" + eTag)); + + // if no one exists, show colored icon with initial char + if (avatar == null) { + try { + avatar = TextDrawable.createAvatar(account.name, avatarRadius); + } catch (Exception e) { + Log_OC.e(TAG, "Error calculating RGB value for active account icon.", e); + avatar = resources.getDrawable(R.drawable.ic_account_circle); + } + } + + // check for new avatar, eTag is compared, so only new one is downloaded if (ThumbnailsCacheManager.cancelPotentialAvatarWork(account.name, callContext)) { final ThumbnailsCacheManager.AvatarGenerationTask task = new ThumbnailsCacheManager.AvatarGenerationTask(listener, callContext, storageManager, account, resources, avatarRadius); final ThumbnailsCacheManager.AsyncAvatarDrawable asyncDrawable = - new ThumbnailsCacheManager.AsyncAvatarDrawable(resources, thumbnail, task); + new ThumbnailsCacheManager.AsyncAvatarDrawable(resources, avatar, task); listener.avatarGenerated(asyncDrawable, callContext); task.execute(account.name); }