Skip to content

Commit

Permalink
Fix duplicated avatars for different accounts with same username, and…
Browse files Browse the repository at this point in the history
… some clean-up
  • Loading branch information
davivel committed Oct 14, 2016
1 parent 7c8a2e9 commit 328f4de
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 127 deletions.
152 changes: 35 additions & 117 deletions src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v4.content.ContextCompat;
import android.view.MenuItem;
import android.widget.ImageView;

Expand Down Expand Up @@ -149,7 +150,7 @@ public static class ThumbnailGenerationTask extends AsyncTask<Object, Void, Bitm
public ThumbnailGenerationTask(ImageView imageView, FileDataStorageManager storageManager,
Account account) {
// Use a WeakReference to ensure the ImageView can be garbage collected
mImageViewReference = new WeakReference<ImageView>(imageView);
mImageViewReference = new WeakReference<>(imageView);
if (storageManager == null)
throw new IllegalArgumentException("storageManager must not be NULL");
mStorageManager = storageManager;
Expand All @@ -158,7 +159,7 @@ public ThumbnailGenerationTask(ImageView imageView, FileDataStorageManager stora

public ThumbnailGenerationTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
mImageViewReference = new WeakReference<ImageView>(imageView);
mImageViewReference = new WeakReference<>(imageView);
}

@Override
Expand Down Expand Up @@ -328,8 +329,7 @@ private Bitmap handlePNG(Bitmap bitmap, int px){
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(resultBitmap);

c.drawColor(MainApp.getAppContext().getResources().
getColor(R.color.background_color));
c.drawColor(ContextCompat.getColor(MainApp.getAppContext(), R.color.background_color));
c.drawBitmap(bitmap, 0, 0, null);

return resultBitmap;
Expand Down Expand Up @@ -363,56 +363,40 @@ private Bitmap doFileInBackground() {
public static class AvatarGenerationTask extends AsyncTask<Object, Void, Bitmap> {
private final WeakReference<ImageView> mImageViewReference;
private final WeakReference<MenuItem> mMenuItemReference;
private static Account mAccount;
private Object mUsername;
private FileDataStorageManager mStorageManager;

private Account mAccount;
private String mUsername;
private OwnCloudClient mClient;

public AvatarGenerationTask(ImageView imageView, FileDataStorageManager storageManager,
Account account) {
// Use a WeakReference to ensure the ImageView can be garbage collected
public AvatarGenerationTask(ImageView imageView, Account account) {
if (account == null) {
throw new IllegalArgumentException("Received NULL account");
}
mMenuItemReference = null;
mImageViewReference = new WeakReference<ImageView>(imageView);
if (storageManager == null)
throw new IllegalArgumentException("storageManager must not be NULL");
mStorageManager = storageManager;
mImageViewReference = new WeakReference<>(imageView);
mAccount = account;
}

public AvatarGenerationTask(MenuItem menuItem, FileDataStorageManager storageManager,
Account account) {
// Use a WeakReference to ensure the ImageView can be garbage collected
public AvatarGenerationTask(MenuItem menuItem, Account account) {
if (account == null) {
throw new IllegalArgumentException("Received NULL account");
}
mImageViewReference = null;
mMenuItemReference = new WeakReference<MenuItem>(menuItem);
if (storageManager == null)
throw new IllegalArgumentException("storageManager must not be NULL");
mStorageManager = storageManager;
mMenuItemReference = new WeakReference<>(menuItem);
mAccount = account;
}

public AvatarGenerationTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
mMenuItemReference = null;
mImageViewReference = new WeakReference<ImageView>(imageView);
}

@Override
protected Bitmap doInBackground(Object... params) {
Bitmap thumbnail = null;

try {
if (mAccount != null) {
OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount,
MainApp.getAppContext());
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
getClientFor(ocAccount, MainApp.getAppContext());
}

mUsername = params[0];
OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount,
MainApp.getAppContext());
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
getClientFor(ocAccount, MainApp.getAppContext());

if (mUsername instanceof String) {
thumbnail = doAvatarInBackground();
}
mUsername = mAccount.name;
thumbnail = doAvatarInBackground();

} catch(Throwable t){
// the app should never break due to a problem with avatars
Expand All @@ -431,52 +415,23 @@ protected void onPostExecute(Bitmap bitmap) {
ImageView imageView = mImageViewReference.get();
AvatarGenerationTask avatarWorkerTask = getAvatarWorkerTask(imageView);
if (this == avatarWorkerTask) {
String tagId = "";
if (mUsername instanceof String) {
tagId = (String) mUsername;
if (String.valueOf(imageView.getTag()).equals(tagId)) {
imageView.setImageBitmap(bitmap);
}
if (String.valueOf(imageView.getTag()).equals(mUsername)) {
imageView.setImageBitmap(bitmap);
}
}
} else {
} else if (mMenuItemReference != null) {
MenuItem menuItem = mMenuItemReference.get();
AvatarGenerationTask avatarWorkerTask = getAvatarWorkerTask(menuItem);
if (this == avatarWorkerTask) {
String tagId = "";
if (mUsername instanceof String) {
tagId = (String) mUsername;
if (String.valueOf(menuItem.getTitle()).equals(tagId)) {
menuItem.setIcon(new BitmapDrawable(MainApp.getAppContext().getResources(),
bitmap));
}
if (String.valueOf(menuItem.getTitle()).equals(mUsername)) {
menuItem.setIcon(new BitmapDrawable(MainApp.getAppContext().getResources(),
bitmap));
}
}
}
}
}

/**
* Add thumbnail to cache
* @param imageKey: thumb key
* @param bitmap: image for extracting thumbnail
* @param path: image path
* @param px: thumbnail dp
* @return Bitmap
*/
private Bitmap addThumbnailToCache(String imageKey, Bitmap bitmap, String path, int px){

Bitmap thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);

// Rotate image, obeying exif tag
thumbnail = BitmapUtils.rotateImage(thumbnail,path);

// Add thumbnail to cache
addBitmapToCache(imageKey, thumbnail);

return thumbnail;
}

/**
* Converts size of file icon from dp to pixel
* @return int
Expand All @@ -488,9 +443,8 @@ private int getAvatarDimension(){
}

private Bitmap doAvatarInBackground() {
String username = (String) mUsername;

final String imageKey = "a_" + username;
final String imageKey = "a_" + mUsername;

// Check disk cache in background thread
Bitmap avatar = getBitmapFromDiskCache(imageKey);
Expand All @@ -507,7 +461,7 @@ private Bitmap doAvatarInBackground() {
GetMethod get = null;
try {
String uri = mClient.getBaseUri() + "" +
"/index.php/avatar/" + AccountUtils.getUsernameOfAccount(username) + "/" + px;
"/index.php/avatar/" + AccountUtils.getUsernameOfAccount(mUsername) + "/" + px;
Log_OC.d("Avatar", "URI: " + uri);
get = new GetMethod(uri);
int status = mClient.executeMethod(get);
Expand Down Expand Up @@ -538,42 +492,6 @@ private Bitmap doAvatarInBackground() {
return avatar;
}

private Bitmap handlePNG(Bitmap bitmap, int px){
Bitmap resultBitmap = Bitmap.createBitmap(px,
px,
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(resultBitmap);

c.drawColor(MainApp.getAppContext().getResources().
getColor(R.color.background_color));
c.drawBitmap(bitmap, 0, 0, null);

return resultBitmap;
}

private Bitmap doFileInBackground() {
File file = (File) mUsername;

final String imageKey = String.valueOf(file.hashCode());

// Check disk cache in background thread
Bitmap thumbnail = getBitmapFromDiskCache(imageKey);

// Not found in disk cache
if (thumbnail == null) {

int px = getAvatarDimension();

Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(
file.getAbsolutePath(), px, px);

if (bitmap != null) {
thumbnail = addThumbnailToCache(imageKey, bitmap, file.getPath(), px);
}
}
return thumbnail;
}

}

public static boolean cancelPotentialThumbnailWork(Object file, ImageView imageView) {
Expand Down Expand Up @@ -633,7 +551,7 @@ public static boolean cancelPotentialAvatarWork(Object file, MenuItem menuItem)
return true;
}

public static ThumbnailGenerationTask getBitmapWorkerTask(ImageView imageView) {
private static ThumbnailGenerationTask getBitmapWorkerTask(ImageView imageView) {
if (imageView != null) {
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncThumbnailDrawable) {
Expand All @@ -644,21 +562,21 @@ public static ThumbnailGenerationTask getBitmapWorkerTask(ImageView imageView) {
return null;
}

public static AvatarGenerationTask getAvatarWorkerTask(ImageView imageView) {
private static AvatarGenerationTask getAvatarWorkerTask(ImageView imageView) {
if (imageView != null) {
return getAvatarWorkerTask(imageView.getDrawable());
}
return null;
}

public static AvatarGenerationTask getAvatarWorkerTask(MenuItem menuItem) {
private static AvatarGenerationTask getAvatarWorkerTask(MenuItem menuItem) {
if (menuItem != null) {
return getAvatarWorkerTask(menuItem.getIcon());
}
return null;
}

public static AvatarGenerationTask getAvatarWorkerTask(Drawable drawable) {
private static AvatarGenerationTask getAvatarWorkerTask(Drawable drawable) {
if (drawable instanceof AsyncAvatarDrawable) {
final AsyncAvatarDrawable asyncDrawable = (AsyncAvatarDrawable) drawable;
return asyncDrawable.getAvatarWorkerTask();
Expand Down
8 changes: 4 additions & 4 deletions src/com/owncloud/android/ui/activity/DrawerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public void updateAccountList() {
if (mAvatars[1] != null) {
DisplayUtils.setAvatar(mAvatars[1],
(ImageView) findNavigationViewChildById(R.id.drawer_account_end),
mOtherAccountAvatarRadiusDimension, getResources(), getStorageManager());
mOtherAccountAvatarRadiusDimension, getResources());
mAccountEndAccountAvatar.setVisibility(View.VISIBLE);
} else {
mAccountEndAccountAvatar.setVisibility(View.GONE);
Expand All @@ -355,7 +355,7 @@ public void updateAccountList() {
if (mAvatars[2] != null) {
DisplayUtils.setAvatar(mAvatars[2],
(ImageView) findNavigationViewChildById(R.id.drawer_account_middle),
mOtherAccountAvatarRadiusDimension, getResources(), getStorageManager());
mOtherAccountAvatarRadiusDimension, getResources());
mAccountMiddleAccountAvatar.setVisibility(View.VISIBLE);
} else {
mAccountMiddleAccountAvatar.setVisibility(View.GONE);
Expand Down Expand Up @@ -463,7 +463,7 @@ protected void setAccountInDrawer(Account account) {
}

DisplayUtils.setAvatar(account, (ImageView) findNavigationViewChildById(R.id.drawer_current_account),
mCurrentAccountAvatarRadiusDimension, getResources(), getStorageManager());
mCurrentAccountAvatarRadiusDimension, getResources());
}
}

Expand All @@ -488,7 +488,7 @@ private void setAvatar(Account account, MenuItem menuItem) {
if (ThumbnailsCacheManager.cancelPotentialAvatarWork(account.name, menuItem)) {
final ThumbnailsCacheManager.AvatarGenerationTask task =
new ThumbnailsCacheManager.AvatarGenerationTask(
menuItem, getStorageManager(), account
menuItem, account
);
if (thumbnail == null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public View getView(final int position, View convertView, ViewGroup parent) {

try {
DisplayUtils.setAvatar(account, viewHolder.imageViewItem, mAccountAvatarRadiusDimension,
mContext.getResources(), mContext.getStorageManager());
mContext.getResources());
} catch (Exception e) {
Log_OC.e(TAG, "Error calculating RGB value for account list item.", e);
// use user icon as a fallback
Expand Down
7 changes: 2 additions & 5 deletions src/com/owncloud/android/utils/DisplayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

import com.owncloud.android.MainApp;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.datamodel.ThumbnailsCacheManager;
import com.owncloud.android.lib.common.utils.Log_OC;
Expand Down Expand Up @@ -308,10 +307,8 @@ public static void colorSnackbar(Context context, Snackbar snackbar) {
* @param userIcon the image view to set the avatar on
* @param avatarRadius the avatar radius
* @param resources reference for density information
* @param storageManager reference for caching purposes
*/
public static void setAvatar(Account account, ImageView userIcon, float avatarRadius, Resources resources,
FileDataStorageManager storageManager) {
public static void setAvatar(Account account, ImageView userIcon, float avatarRadius, Resources resources) {
if (account != null) {

userIcon.setContentDescription(account.name);
Expand All @@ -329,7 +326,7 @@ public static void setAvatar(Account account, ImageView userIcon, float avatarRa
// generate new avatar
if (ThumbnailsCacheManager.cancelPotentialAvatarWork(account.name, userIcon)) {
final ThumbnailsCacheManager.AvatarGenerationTask task =
new ThumbnailsCacheManager.AvatarGenerationTask(userIcon, storageManager, account);
new ThumbnailsCacheManager.AvatarGenerationTask(userIcon, account);
if (thumbnail == null) {
try {
userIcon.setImageDrawable(
Expand Down

0 comments on commit 328f4de

Please sign in to comment.