rudimentary drawer header / avatar based account switching.

This commit is contained in:
Andy Scherzinger 2016-04-05 16:28:56 +02:00
parent c813003e8b
commit 583dca5cbc
3 changed files with 145 additions and 18 deletions

View file

@ -29,19 +29,43 @@
android:padding="@dimen/standard_padding"
>
<FrameLayout
android:id="@+id/drawer_user_avatars"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="@dimen/standard_half_margin">
<ImageView
android:id="@+id/drawer_usericon"
android:id="@+id/drawer_current_account"
android:layout_width="@dimen/nav_drawer_header_avatar"
android:layout_height="@dimen/nav_drawer_header_avatar"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_account_circle"/>
<ImageView
android:id="@+id/drawer_account_middle"
android:layout_width="@dimen/nav_drawer_header_avatar_other_accounts_size"
android:layout_height="@dimen/nav_drawer_header_avatar_other_accounts_size"
android:layout_gravity="right"
android:layout_marginEnd="@dimen/nav_drawer_header_avatar_second_account_margin"
android:layout_marginRight="@dimen/nav_drawer_header_avatar_second_account_margin"
android:src="@drawable/ic_account_circle"
android:layout_marginBottom="@dimen/standard_half_margin"
/>
android:onClick="onAccountDrawerClick"/>
<ImageView
android:id="@+id/drawer_account_end"
android:layout_width="@dimen/nav_drawer_header_avatar_other_accounts_size"
android:layout_height="@dimen/nav_drawer_header_avatar_other_accounts_size"
android:layout_gravity="right"
android:src="@drawable/ic_account_circle"
android:onClick="onAccountDrawerClick"/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/drawer_usericon"
android:layout_below="@+id/drawer_user_avatars"
>
<LinearLayout

View file

@ -25,6 +25,9 @@
<dimen name="nav_drawer_header_avatar">56dp</dimen>
<!-- avatar radius needs to 1/2 of the avatar dp value -->
<dimen name="nav_drawer_header_avatar_radius">28px</dimen>
<dimen name="nav_drawer_header_avatar_other_accounts_size">40dp</dimen>
<!-- margin must be nav_drawer_header_avatar_other_accounts_size+16dp -->
<dimen name="nav_drawer_header_avatar_second_account_margin">56dp</dimen>
<dimen name="file_icon_size">32dp</dimen>
<dimen name="file_icon_size_grid">128dp</dimen>

View file

@ -22,10 +22,13 @@ package com.owncloud.android.ui.activity;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
@ -53,7 +56,8 @@ import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
/**
* Base class to handle setup of the drawer implementation.
* Base class to handle setup of the drawer implementation including user switching and avatar fetching and fallback
* generation.
*/
public abstract class DrawerActivity extends ToolbarActivity {
private static final String TAG = DrawerActivity.class.getSimpleName();
@ -83,6 +87,21 @@ public abstract class DrawerActivity extends ToolbarActivity {
*/
private ImageView mAccountChooserToggle;
/**
* Reference to the current account avatar.
*/
private ImageView mAccountCurrentAccountAvatar;
/**
* Reference to the middle account avatar.
*/
private ImageView mAccountMiddleAccountAvatar;
/**
* Reference to the end account avatar.
*/
private ImageView mAccountEndAccountAvatar;
/**
* Flag to signal if the account chooser is active.
*/
@ -93,6 +112,11 @@ public abstract class DrawerActivity extends ToolbarActivity {
*/
private int mCheckedMenuItem = Menu.NONE;
/**
* accounts for the (max) three displayed accounts in the drawer header.
*/
private Account[] mAvatars = new Account[3];
/**
* Initializes the drawer, its content and highlights the menu item with the given id.
* This method needs to be called after the content view has been set.
@ -113,11 +137,16 @@ public abstract class DrawerActivity extends ToolbarActivity {
mNavigationView = (NavigationView) findViewById(R.id.nav_view);
if (mNavigationView != null) {
setupDrawerContent(mNavigationView);
mAccountChooserToggle = (ImageView) findNavigationViewChildById(R.id.drawer_account_chooser_toogle);
mAccountChooserToggle.setImageResource(R.drawable.ic_down);
mIsAccountChooserActive = false;
mAccountCurrentAccountAvatar = (ImageView) findNavigationViewChildById(R.id.drawer_current_account);
mAccountMiddleAccountAvatar = (ImageView) findNavigationViewChildById(R.id.drawer_account_middle);
mAccountEndAccountAvatar = (ImageView) findNavigationViewChildById(R.id.drawer_account_end);
setupDrawerContent(mNavigationView);
findNavigationViewChildById(R.id.drawer_active_user)
.setOnClickListener(new View.OnClickListener() {
@Override
@ -193,9 +222,7 @@ public abstract class DrawerActivity extends ToolbarActivity {
break;
case Menu.NONE:
// account clicked
AccountUtils.setCurrentOwnCloudAccount(
getApplicationContext(), menuItem.getTitle().toString());
restart();
accountClicked(menuItem.getTitle().toString());
default:
Log_OC.i(TAG, "Unknown drawer menu item clicked: " + menuItem.getTitle());
}
@ -212,6 +239,28 @@ public abstract class DrawerActivity extends ToolbarActivity {
}
}
/**
* sets the new/current account and restarts. In case the given account equals the actual/current account the
* call will be ignored.
*
* @param accountName The account name to be set
*/
private void accountClicked(String accountName) {
if (!AccountUtils.getCurrentOwnCloudAccount(getApplicationContext()).name.equals(accountName)) {
AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), accountName);
restart();
}
}
/**
* click method for mini avatars in drawer header.
*
* @param view the clicked ImageView
*/
public void onAccountDrawerClick(View view) {
accountClicked(view.getContentDescription().toString());
}
/**
* checks if the drawer exists and is opened.
*
@ -270,6 +319,26 @@ public abstract class DrawerActivity extends ToolbarActivity {
if (accounts.length > 0 && mNavigationView != null) {
repopulateAccountList(accounts);
setAccountInDrawer(AccountUtils.getCurrentOwnCloudAccount(this));
populateDrawerOwnCloudAccounts();
// activate second/end account avatar
if(mAvatars[1] != null) {
setAvatar(mAvatars[1], R.id.drawer_account_end);
mAccountEndAccountAvatar.setVisibility(View.VISIBLE);
} else {
mAccountEndAccountAvatar.setVisibility(View.GONE);
}
// activate third/middle account avatar
if(mAvatars[2] != null) {
setAvatar(mAvatars[2], R.id.drawer_account_middle);
mAccountMiddleAccountAvatar.setVisibility(View.VISIBLE);
} else {
mAccountMiddleAccountAvatar.setVisibility(View.GONE);
}
} else {
mAccountEndAccountAvatar.setVisibility(View.GONE);
mAccountMiddleAccountAvatar.setVisibility(View.GONE);
}
}
@ -347,8 +416,7 @@ public abstract class DrawerActivity extends ToolbarActivity {
int lastAtPos = account.name.lastIndexOf("@");
username.setText(account.name.substring(0, lastAtPos));
ImageView userIcon = (ImageView) findNavigationViewChildById(R.id.drawer_usericon);
setAvatarInDrawer(account);
setAvatar(account, R.id.drawer_current_account);
}
}
@ -357,12 +425,14 @@ public abstract class DrawerActivity extends ToolbarActivity {
*
* @param account the account to be set in the drawer
*/
private void setAvatarInDrawer(Account account) {
private void setAvatar(Account account, int avatarViewId) {
if (mDrawerLayout != null && account != null) {
ImageView userIcon = (ImageView) findNavigationViewChildById(R.id.drawer_usericon);
int lastAtPos = account.name.lastIndexOf("@");
String username = account.name.substring(0, lastAtPos);
ImageView userIcon = (ImageView) findNavigationViewChildById(avatarViewId);
userIcon.setContentDescription(account.name);
// Thumbnail in Cache?
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache("a_" + username);
@ -408,6 +478,16 @@ public abstract class DrawerActivity extends ToolbarActivity {
}
}
/**
* creates an avatar in form of a TextDrawable with the first letter of the account name in a circle with the
* given radius.
*
* @param accountName the account name
* @param radiusInDp the circle's radius
* @return the avatar as a TextDrawable
* @throws UnsupportedEncodingException if the charset is not supported when calculating the color values
* @throws NoSuchAlgorithmException if the specified algorithm is not available when calculating the color values
*/
@NonNull
private TextDrawable createAvatar(String accountName, float radiusInDp) throws UnsupportedEncodingException,
NoSuchAlgorithmException {
@ -572,4 +652,24 @@ public abstract class DrawerActivity extends ToolbarActivity {
updateAccountList();
restart();
}
/**
* populates the avatar drawer array with the first three ownCloud {@link Account}s while the first element is
* always the current account.
*/
private void populateDrawerOwnCloudAccounts() {
mAvatars = new Account[3];
Account[] accountsAll = AccountManager.get(this).getAccountsByType
(MainApp.getAccountType());
Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(this);
mAvatars[0] = currentAccount;
int j = 0;
for(int i = 1 ; i <= 2 && i < accountsAll.length && j < accountsAll.length; j++) {
if(!currentAccount.equals(accountsAll[j])) {
mAvatars[i] = accountsAll[j];
i++;
}
}
}
}