drawer: vcard display in the menu's accounts list

This commit adds photo in the drawer's accounts list. Alias is replaced by the
name of the vcard.

Change-Id: Id1c5b4e9b7b2a6bd2b931ac0d87e437bc6d5d4cb
Tuleap: #1279
diff --git a/ring-android/app/src/main/java/cx/ring/navigation/AccountAdapter.java b/ring-android/app/src/main/java/cx/ring/navigation/AccountAdapter.java
index 41e983a..0cb8381 100644
--- a/ring-android/app/src/main/java/cx/ring/navigation/AccountAdapter.java
+++ b/ring-android/app/src/main/java/cx/ring/navigation/AccountAdapter.java
@@ -17,6 +17,11 @@
  */
 package cx.ring.navigation;
 
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.drawable.Drawable;
+import android.support.v4.content.res.ResourcesCompat;
 import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -24,15 +29,20 @@
 import android.widget.ImageView;
 import android.widget.TextView;
 
+import java.io.File;
 import java.util.List;
 
 import butterknife.BindView;
 import butterknife.ButterKnife;
 import cx.ring.R;
 import cx.ring.model.Account;
+import cx.ring.utils.BitmapUtils;
+import cx.ring.utils.VCardUtils;
+import ezvcard.VCard;
 
 class AccountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
     private List<Account> mDataset;
+    private Context mContext;
 
     private static final int TYPE_ACCOUNT = 0;
     private static final int TYPE_ADD_RING_ACCOUNT = 1;
@@ -49,8 +59,9 @@
         void onAddAccountSelected();
     }
 
-    AccountAdapter(List<Account> accounts) {
+    AccountAdapter(List<Account> accounts, Context context) {
         mDataset = accounts;
+        mContext = context;
     }
 
     void setOnAccountActionClickedListener(OnAccountActionClicked listener) {
@@ -65,12 +76,15 @@
     private void setAccounts(List<Account> results) {
         mDataset.clear();
         for (Account account : results) {
-                mDataset.add(account);
+            mDataset.add(account);
         }
     }
 
     class AccountView extends RecyclerView.ViewHolder implements View.OnClickListener {
 
+        @BindView(R.id.account_photo)
+        ImageView photo;
+
         @BindView(R.id.account_alias)
         TextView alias;
 
@@ -85,6 +99,7 @@
             ButterKnife.bind(this, view);
             view.setOnClickListener(this);
         }
+
         @Override
         public void onClick(View v) {
             if (mListener != null) {
@@ -120,6 +135,7 @@
             ButterKnife.bind(this, view);
             view.setOnClickListener(this);
         }
+
         @Override
         public void onClick(View v) {
             if (mListener == null) {
@@ -163,7 +179,15 @@
         switch (getItemViewType(position)) {
             case TYPE_ACCOUNT:
                 Account account = mDataset.get(position);
-                ((AccountView) holder).alias.setText(account.getAlias());
+                VCard vcard = VCardUtils.loadLocalProfileFromDisk(mContext.getFilesDir(), account.getAccountID(), mContext.getString(R.string.unknown));
+                if (!vcard.getPhotos().isEmpty()) {
+                    Bitmap photo = BitmapUtils.cropImageToCircle(vcard.getPhotos().get(0).getData());
+                    ((AccountView) holder).photo.setImageBitmap(photo);
+                } else {
+                    Drawable photo = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.ic_contact_picture, null);
+                    ((AccountView) holder).photo.setImageDrawable(photo);
+                }
+                ((AccountView) holder).alias.setText(vcard.getFormattedName().getValue());
                 if (account.isRing()) {
                     ((AccountView) holder).host.setText(account.getUsername());
                 } else if (account.isSip() && !account.isIP2IP()) {
diff --git a/ring-android/app/src/main/java/cx/ring/navigation/RingNavigationFragment.java b/ring-android/app/src/main/java/cx/ring/navigation/RingNavigationFragment.java
index cc713e0..55c60db 100644
--- a/ring-android/app/src/main/java/cx/ring/navigation/RingNavigationFragment.java
+++ b/ring-android/app/src/main/java/cx/ring/navigation/RingNavigationFragment.java
@@ -95,9 +95,6 @@
 
     @BindView(R.id.user_photo)
     ImageView mUserImage;
-
-    @BindView(R.id.user_name)
-    TextView mUserName;
     private Bitmap mSourcePhoto;
     private ImageView mProfilePhoto;
     private VCard mVCardProfile;
@@ -165,6 +162,7 @@
 
     @Override
     public void update(Observable o, Object arg) {
+        updateUserView();
         updateSelectedAccountView();
     }
 
@@ -184,6 +182,7 @@
     public void onResume() {
         super.onResume();
         mStateService.addObserver(this);
+        updateUserView();
         updateSelectedAccountView();
     }
 
@@ -253,7 +252,7 @@
     }
 
     private void setupAccountList() {
-        mAccountAdapter = new AccountAdapter(new ArrayList<Account>());
+        mAccountAdapter = new AccountAdapter(new ArrayList<Account>(), getActivity());
         mAccountsView.setHasFixedSize(true);
         mAccountAdapter.setOnAccountActionClickedListener(this);
         LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
@@ -313,7 +312,7 @@
         } else {
             mUserImage.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_contact_picture, null));
         }
-        mUserName.setText(mVCardProfile.getFormattedName().getValue());
+        mSelectedAccountAlias.setText(mVCardProfile.getFormattedName().getValue());
         Log.d(TAG, "User did change, updating user view.");
     }
 
@@ -363,7 +362,7 @@
         ViewGroup view = (ViewGroup) inflater.inflate(R.layout.dialog_profile, null);
 
         final EditText editText = (EditText) view.findViewById(R.id.user_name);
-        editText.setText(mUserName.getText());
+        editText.setText(mSelectedAccountAlias.getText());
         mProfilePhoto = (ImageView) view.findViewById(R.id.profile_photo);
         mProfilePhoto.setImageDrawable(mUserImage.getDrawable());
 
@@ -429,6 +428,7 @@
                 mVCardProfile.removeProperties(RawProperty.class);
                 VCardUtils.saveLocalProfileToDisk(mVCardProfile, mStateService.getCurrentAccount().getAccountID(), getActivity().getFilesDir());
                 updateUserView();
+                updateAccounts(mLocalService.getAccounts());
             }
         });
 
@@ -457,7 +457,7 @@
         if (selectedAccount == null) {
             return;
         }
-        mSelectedAccountAlias.setText(selectedAccount.getAlias());
+        mSelectedAccountAlias.setText(mVCardProfile.getFormattedName().getValue());
         if (selectedAccount.isRing()) {
             mSelectedAccountHost.setText(selectedAccount.getUsername());
         } else if (selectedAccount.isSip() && !selectedAccount.isIP2IP()) {
diff --git a/ring-android/app/src/main/res/layout/frag_navigation.xml b/ring-android/app/src/main/res/layout/frag_navigation.xml
index 2eb3dbb..e1ec742 100644
--- a/ring-android/app/src/main/res/layout/frag_navigation.xml
+++ b/ring-android/app/src/main/res/layout/frag_navigation.xml
@@ -51,20 +51,6 @@
                     app:backgroundTint="@color/transparent_light"
                     app:fabSize="mini" />
 
-                <TextView
-                    android:id="@+id/user_name"
-                    android:layout_width="wrap_content"
-                    android:layout_height="wrap_content"
-                    android:layout_below="@+id/edit_profile"
-                    android:layout_centerHorizontal="true"
-                    android:layout_marginBottom="8dp"
-                    android:layout_marginTop="8dp"
-                    android:maxLines="1"
-                    android:textColor="@color/white"
-                    android:textSize="20sp"
-                    android:textStyle="bold"
-                    tools:text="Username" />
-
             </RelativeLayout>
 
 
diff --git a/ring-android/app/src/main/res/layout/item_account.xml b/ring-android/app/src/main/res/layout/item_account.xml
index 871ca8f..65bb628 100644
--- a/ring-android/app/src/main/res/layout/item_account.xml
+++ b/ring-android/app/src/main/res/layout/item_account.xml
@@ -17,18 +17,30 @@
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 -->
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="?android:attr/selectableItemBackground"
     android:padding="16dp">
 
+    <android.support.v7.widget.AppCompatImageView
+        android:id="@+id/account_photo"
+        android:layout_width="36dp"
+        android:layout_height="36dp"
+        android:layout_centerVertical="true"
+        android:layout_marginEnd="8dp"
+        android:layout_marginRight="8dp"
+        tools:src="@drawable/ic_contact_picture" />
+
     <android.support.v7.widget.AppCompatTextView
         android:id="@+id/account_alias"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="false"
-        android:layout_alignParentStart="true"
+        android:layout_alignParentStart="false"
         android:layout_alignParentTop="true"
+        android:layout_toEndOf="@+id/account_photo"
+        android:layout_toRightOf="@+id/account_photo"
         android:textAppearance="@style/ListPrimary" />
 
     <android.support.v7.widget.AppCompatTextView
@@ -36,9 +48,11 @@
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="false"
-        android:layout_alignParentStart="true"
+        android:layout_alignParentStart="false"
         android:layout_below="@+id/account_alias"
+        android:layout_toEndOf="@+id/account_photo"
         android:layout_toLeftOf="@+id/error_indicator"
+        android:layout_toRightOf="@+id/account_photo"
         android:layout_toStartOf="@+id/error_indicator"
         android:ellipsize="end"
         android:maxLines="1"