Browse Source

initial account handling in drawer + initial icon generation (still broken)

Andy Scherzinger 9 years ago
parent
commit
d9d3424968

+ 3 - 3
src/com/owncloud/android/ui/TextDrawable.java

@@ -35,9 +35,9 @@ public class TextDrawable extends Drawable {
 
     @Override
     public void draw(Canvas canvas) {
-        RectF rf = new RectF(0,24,24,0);
-        canvas.drawRoundRect(rf,24,24,bg);
-        //canvas.drawRect(0,-20,20,40,bg);
+        //RectF rf = new RectF(0,24,24,0);
+        //canvas.drawRoundRect(rf,24,24,bg);
+        canvas.drawRect(0,-20,20,40,bg);
         canvas.drawText(text, 4, 6, paint);
     }
 

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

@@ -1,5 +1,6 @@
 package com.owncloud.android.ui.activity;
 
+import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.content.Intent;
 import android.content.res.Configuration;
@@ -17,19 +18,35 @@ import android.widget.Toast;
 
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
+import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.ui.TextDrawable;
+import com.owncloud.android.utils.BitmapUtils;
+
+import java.io.UnsupportedEncodingException;
+import java.math.BigInteger;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
 
 /**
  * Base class to handle setup of the drawer implementation.
  */
 public abstract class DrawerActivity extends ToolbarActivity {
+    private static final String TAG = DrawerActivity.class.getSimpleName();
+
     // Navigation Drawer
     private DrawerLayout mDrawerLayout;
     private ActionBarDrawerToggle mDrawerToggle;
     private NavigationView mNavigationView;
     private ImageView mAccountChooserToggle;
 
+    private List<Integer> mAccountMenuItemIds = new ArrayList<Integer>();
+    private Account mCurrentAccount;
+
     private boolean mIsAccountChooserActive;
 
     /**
@@ -148,7 +165,7 @@ public abstract class DrawerActivity extends ToolbarActivity {
                                 am.addAccount(MainApp.getAccountType(), null, null, null, DrawerActivity.this,
                                         null, null);
                             case R.id.drawer_menu_account_manage:
-                                Toast.makeText(getApplicationContext(),"Not implemented yet",Toast.LENGTH_SHORT);
+                                Toast.makeText(getApplicationContext(), "Not implemented yet", Toast.LENGTH_SHORT);
                         }
 
                         return true;
@@ -209,6 +226,64 @@ public abstract class DrawerActivity extends ToolbarActivity {
         }
     }
 
+    /**
+     * updates the account list in the drawer.
+     */
+    // TODO call updateAccountList() after n.o. accounts changed
+    public void updateAccountList() {
+        AccountManager am = (AccountManager) this.getSystemService(this.ACCOUNT_SERVICE);
+
+        // populate UI
+        repopulateAccountList(am.getAccountsByType(MainApp.getAccountType()));
+        setUsernameInDrawer(AccountUtils.getCurrentOwnCloudAccount(this).name);
+    }
+
+    /**
+     * re-populates the account list.
+     *
+     * @param accounts list of accounts
+     */
+    private void repopulateAccountList(Account[] accounts) {
+        // remove all accounts from list
+        mNavigationView.getMenu().removeItem(Menu.NONE);
+
+        // add all accounts to list
+        for (int i = 0; i < accounts.length; i++) {
+            try {
+                int[] rgb = calculateRGB(accounts[i]);
+                TextDrawable icon = new TextDrawable(accounts[i].name.substring(0, 1).toUpperCase(),
+                        rgb[0], rgb[1], rgb[2]);
+                mNavigationView.getMenu().add(R.id.drawer_menu_accounts, Menu.NONE, 0, accounts[i].name).setIcon(icon);
+            } catch (Exception e) {
+                Log_OC.e(TAG, "Error calculating RGB value for account menu item.", e);
+                mNavigationView.getMenu().add(R.id.drawer_menu_accounts, Menu.NONE, 0, accounts[i].name).setIcon(R
+                        .drawable.ic_account_circle);
+            }
+        }
+
+        // adding sets menu group back to visible, so safety check and setting invisible
+        if (!mIsAccountChooserActive) {
+            mNavigationView.getMenu().setGroupVisible(R.id.drawer_menu_accounts, false);
+        }
+    }
+
+    private int[] calculateRGB(Account account) throws UnsupportedEncodingException, NoSuchAlgorithmException {
+        // using adapted algorithm from /core/js/placeholder.js:50
+        int lastAtPos = account.name.lastIndexOf("@");
+        String username = account.name.substring(0, lastAtPos);
+        byte[] seed = username.getBytes("UTF-8");
+        MessageDigest md = MessageDigest.getInstance("MD5");
+//                        Integer seedMd5Int = Math.abs(new String(Hex.encodeHex(seedMd5))
+//                      .hashCode());
+        Integer seedMd5Int = String.format(Locale.ROOT, "%032x",
+                new BigInteger(1, md.digest(seed))).hashCode();
+
+        double maxRange = Integer.MAX_VALUE;
+        float hue = (float) (seedMd5Int / maxRange * 360);
+
+        return BitmapUtils.HSLtoRGB(hue, 90.0f, 65.0f, 1.0f);
+    }
+
     /**
      * Method that gets called on drawer menu click for 'All Files'.
      */
@@ -275,6 +350,7 @@ public abstract class DrawerActivity extends ToolbarActivity {
                 mDrawerToggle.setDrawerIndicatorEnabled(true);
             }
         }
+        updateAccountList();
     }
 
     @Override