Skip to content

Commit

Permalink
trying to first show old one, but asyncDrawable is not working
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasKaminsky committed Oct 12, 2017
1 parent e265dfb commit 8d9becf
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -899,7 +905,7 @@ public MediaThumbnailGenerationTask getBitmapWorkerTask() {
public static class AsyncAvatarDrawable extends BitmapDrawable {
private final WeakReference<AvatarGenerationTask> avatarWorkerTaskReference;

public AsyncAvatarDrawable(Resources res, Bitmap bitmap, AvatarGenerationTask avatarWorkerTask) {
public AsyncAvatarDrawable(Resources res, Drawable bitmap, AvatarGenerationTask avatarWorkerTask) {
super(res, bitmap);
avatarWorkerTaskReference = new WeakReference<>(avatarWorkerTask);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 24 additions & 5 deletions src/main/java/com/owncloud/android/utils/DisplayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 8d9becf

Please sign in to comment.