Browse Source

extend implementation to also set avatars in menu items

Andy Scherzinger 9 years ago
parent
commit
87359121f8

+ 74 - 12
src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java

@@ -34,6 +34,7 @@ import android.net.Uri;
 import android.os.AsyncTask;
 import android.os.AsyncTask;
 import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
 import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
 import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
 import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
+import android.view.MenuItem;
 import android.widget.ImageView;
 import android.widget.ImageView;
 
 
 import com.owncloud.android.MainApp;
 import com.owncloud.android.MainApp;
@@ -363,6 +364,7 @@ public class ThumbnailsCacheManager {
 
 
     public static class AvatarGenerationTask extends AsyncTask<Object, Void, Bitmap> {
     public static class AvatarGenerationTask extends AsyncTask<Object, Void, Bitmap> {
         private final WeakReference<ImageView> mImageViewReference;
         private final WeakReference<ImageView> mImageViewReference;
+        private final WeakReference<MenuItem> mMenuItemReference;
         private static Account mAccount;
         private static Account mAccount;
         private Object mUsername;
         private Object mUsername;
         private FileDataStorageManager mStorageManager;
         private FileDataStorageManager mStorageManager;
@@ -371,6 +373,7 @@ public class ThumbnailsCacheManager {
         public AvatarGenerationTask(ImageView imageView, FileDataStorageManager storageManager,
         public AvatarGenerationTask(ImageView imageView, FileDataStorageManager storageManager,
                                        Account account) {
                                        Account account) {
             // Use a WeakReference to ensure the ImageView can be garbage collected
             // Use a WeakReference to ensure the ImageView can be garbage collected
+            mMenuItemReference = null;
             mImageViewReference = new WeakReference<ImageView>(imageView);
             mImageViewReference = new WeakReference<ImageView>(imageView);
             if (storageManager == null)
             if (storageManager == null)
                 throw new IllegalArgumentException("storageManager must not be NULL");
                 throw new IllegalArgumentException("storageManager must not be NULL");
@@ -378,8 +381,20 @@ public class ThumbnailsCacheManager {
             mAccount = account;
             mAccount = account;
         }
         }
 
 
+        public AvatarGenerationTask(MenuItem menuItem, FileDataStorageManager storageManager,
+                                    Account account) {
+            // Use a WeakReference to ensure the ImageView can be garbage collected
+            mImageViewReference = null;
+            mMenuItemReference = new WeakReference<MenuItem>(menuItem);
+            if (storageManager == null)
+                throw new IllegalArgumentException("storageManager must not be NULL");
+            mStorageManager = storageManager;
+            mAccount = account;
+        }
+
         public AvatarGenerationTask(ImageView imageView) {
         public AvatarGenerationTask(ImageView imageView) {
             // Use a WeakReference to ensure the ImageView can be garbage collected
             // Use a WeakReference to ensure the ImageView can be garbage collected
+            mMenuItemReference = null;
             mImageViewReference = new WeakReference<ImageView>(imageView);
             mImageViewReference = new WeakReference<ImageView>(imageView);
         }
         }
 
 
@@ -414,14 +429,29 @@ public class ThumbnailsCacheManager {
 
 
         protected void onPostExecute(Bitmap bitmap) {
         protected void onPostExecute(Bitmap bitmap) {
             if (bitmap != null) {
             if (bitmap != null) {
-                final ImageView imageView = mImageViewReference.get();
-                final 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 (mImageViewReference != null) {
+                    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);
+                            }
+                        }
+                    }
+                } else {
+                    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));
+                            }
                         }
                         }
                     }
                     }
                 }
                 }
@@ -586,6 +616,25 @@ public class ThumbnailsCacheManager {
         return true;
         return true;
     }
     }
 
 
+    public static boolean cancelPotentialAvatarWork(Object file, MenuItem menuItem) {
+        final AvatarGenerationTask avatarWorkerTask = getAvatarWorkerTask(menuItem);
+
+        if (avatarWorkerTask != null) {
+            final Object usernameData = avatarWorkerTask.mUsername;
+            // If usernameData is not yet set or it differs from the new data
+            if (usernameData == null || usernameData != file) {
+                // Cancel previous task
+                avatarWorkerTask.cancel(true);
+                Log_OC.v(TAG, "Cancelled generation of avatar for a reused imageView");
+            } else {
+                // The same work is already in progress
+                return false;
+            }
+        }
+        // No task associated with the ImageView, or an existing task was cancelled
+        return true;
+    }
+
     public static ThumbnailGenerationTask getBitmapWorkerTask(ImageView imageView) {
     public static ThumbnailGenerationTask getBitmapWorkerTask(ImageView imageView) {
         if (imageView != null) {
         if (imageView != null) {
             final Drawable drawable = imageView.getDrawable();
             final Drawable drawable = imageView.getDrawable();
@@ -599,15 +648,28 @@ public class ThumbnailsCacheManager {
 
 
     public static AvatarGenerationTask getAvatarWorkerTask(ImageView imageView) {
     public static AvatarGenerationTask getAvatarWorkerTask(ImageView imageView) {
         if (imageView != null) {
         if (imageView != null) {
-            final Drawable drawable = imageView.getDrawable();
-            if (drawable instanceof AsyncAvatarDrawable) {
-                final AsyncAvatarDrawable asyncDrawable = (AsyncAvatarDrawable) drawable;
-                return asyncDrawable.getAvatarWorkerTask();
+            if (imageView != null) {
+                return getAvatarWorkerTask(imageView.getDrawable());
             }
             }
         }
         }
         return null;
         return null;
     }
     }
 
 
+    public static AvatarGenerationTask getAvatarWorkerTask(MenuItem menuItem) {
+        if (menuItem != null) {
+            return getAvatarWorkerTask(menuItem.getIcon());
+        }
+        return null;
+    }
+
+    public static AvatarGenerationTask getAvatarWorkerTask(Drawable drawable) {
+        if (drawable instanceof AsyncAvatarDrawable) {
+            final AsyncAvatarDrawable asyncDrawable = (AsyncAvatarDrawable) drawable;
+            return asyncDrawable.getAvatarWorkerTask();
+        }
+        return null;
+    }
+
     public static class AsyncThumbnailDrawable extends BitmapDrawable {
     public static class AsyncThumbnailDrawable extends BitmapDrawable {
         private final WeakReference<ThumbnailGenerationTask> bitmapWorkerTaskReference;
         private final WeakReference<ThumbnailGenerationTask> bitmapWorkerTaskReference;
 
 

+ 58 - 1
src/com/owncloud/android/ui/activity/DrawerActivity.java

@@ -371,7 +371,7 @@ public abstract class DrawerActivity extends ToolbarActivity {
         // add all accounts to list
         // add all accounts to list
         for (int i = 0; i < accounts.length; i++) {
         for (int i = 0; i < accounts.length; i++) {
             try {
             try {
-                mNavigationView.getMenu().add(
+                MenuItem accountMenuItem = mNavigationView.getMenu().add(
                         R.id.drawer_menu_accounts,
                         R.id.drawer_menu_accounts,
                         Menu.NONE,
                         Menu.NONE,
                         MENU_ORDER_ACCOUNT,
                         MENU_ORDER_ACCOUNT,
@@ -380,6 +380,7 @@ public abstract class DrawerActivity extends ToolbarActivity {
                                 accounts[i].name,
                                 accounts[i].name,
                                 mMenuAccountAvatarRadiusDimension)
                                 mMenuAccountAvatarRadiusDimension)
                         );
                         );
+                setAvatar(accounts[i], accountMenuItem);
             } catch (Exception e) {
             } catch (Exception e) {
                 Log_OC.e(TAG, "Error calculating RGB value for account menu item.", e);
                 Log_OC.e(TAG, "Error calculating RGB value for account menu item.", e);
                 mNavigationView.getMenu().add(
                 mNavigationView.getMenu().add(
@@ -509,6 +510,62 @@ public abstract class DrawerActivity extends ToolbarActivity {
         }
         }
     }
     }
 
 
+    /**
+     * fetches and sets the avatar of the current account in the drawer in case the drawer is available.
+     *
+     * @param account        the account to be set in the drawer
+     * @param menuItem       the menuItem to set the avatar on
+     */
+    private void setAvatar(Account account, MenuItem menuItem) {
+        if (mDrawerLayout != null && account != null) {
+            int lastAtPos = account.name.lastIndexOf("@");
+            String username = account.name.substring(0, lastAtPos);
+
+            // Thumbnail in Cache?
+            Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache("a_" + username);
+
+            if (thumbnail != null) {
+                RoundedBitmapDrawable roundedAvatar = RoundedBitmapDrawableFactory.create
+                        (MainApp.getAppContext().getResources(), thumbnail);
+                roundedAvatar.setCircular(true);
+                menuItem.setIcon(roundedAvatar);
+            } else {
+                // generate new avatar
+                if (ThumbnailsCacheManager.cancelPotentialAvatarWork(username, menuItem)) {
+                    final ThumbnailsCacheManager.AvatarGenerationTask task =
+                            new ThumbnailsCacheManager.AvatarGenerationTask(
+                                    menuItem, getStorageManager(), account
+                            );
+                    if (thumbnail == null) {
+                        try {
+                                menuItem.setIcon(
+                                        TextDrawable.createAvatar(
+                                                account.name,
+                                                mMenuAccountAvatarRadiusDimension
+                                        )
+                                );
+                        } catch (Exception e) {
+                            Log_OC.e(TAG, "Error calculating RGB value for active account icon.", e);
+                            menuItem.setIcon(R.drawable.ic_account_circle);
+                        }
+                    } else {
+                        final ThumbnailsCacheManager.AsyncAvatarDrawable asyncDrawable =
+                                new ThumbnailsCacheManager.AsyncAvatarDrawable(
+                                        getResources(),
+                                        thumbnail,
+                                        task
+                                );
+                        RoundedBitmapDrawable roundedAvatar = RoundedBitmapDrawableFactory.create
+                                (MainApp.getAppContext().getResources(), asyncDrawable.getBitmap());
+                        roundedAvatar.setCircular(true);
+                        menuItem.setIcon(roundedAvatar);
+                    }
+                    task.execute(username);
+                }
+            }
+        }
+    }
+
     /**
     /**
      * Toggle between standard menu and account list including saving the state.
      * Toggle between standard menu and account list including saving the state.
      */
      */