Home | History | Annotate | Download | only in ui
      1 /**
      2  * Copyright (c) 2013, Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.mail.ui;
     17 
     18 import android.content.Context;
     19 import android.graphics.Typeface;
     20 import android.text.TextUtils;
     21 import android.util.AttributeSet;
     22 import android.view.View;
     23 import android.widget.ImageView;
     24 import android.widget.LinearLayout;
     25 import android.widget.TextView;
     26 
     27 import com.android.bitmap.BitmapCache;
     28 import com.android.mail.R;
     29 import com.android.mail.bitmap.AccountAvatarDrawable;
     30 import com.android.mail.bitmap.ContactResolver;
     31 import com.android.mail.providers.Account;
     32 
     33 /**
     34  * The view for each account in the folder list/drawer.
     35  */
     36 public class AccountItemView extends LinearLayout {
     37     private TextView mAccountDisplayName;
     38     private TextView mAccountAddress;
     39     private ImageView mAvatar;
     40     private ImageView mCheckmark;
     41 
     42     public AccountItemView(Context context) {
     43         super(context);
     44     }
     45 
     46     public AccountItemView(Context context, AttributeSet attrs) {
     47         super(context, attrs);
     48     }
     49 
     50     public AccountItemView(Context context, AttributeSet attrs, int defStyle) {
     51         super(context, attrs, defStyle);
     52     }
     53 
     54     @Override
     55     protected void onFinishInflate() {
     56         super.onFinishInflate();
     57         mAccountDisplayName = (TextView)findViewById(R.id.account_display_name);
     58         mAccountAddress = (TextView)findViewById(R.id.account_address);
     59         mAvatar = (ImageView)findViewById(R.id.avatar);
     60         mCheckmark = (ImageView)findViewById(R.id.checkmark);
     61     }
     62 
     63     /**
     64      * Sets the account name and draws the unread count. Depending on the account state (current or
     65      * unused), the colors and drawables will change through the call to setSelected for each
     66      * necessary element.
     67      *
     68      * @param account account whose name will be displayed
     69      * @param isCurrentAccount true if the account is the one in use, false otherwise
     70      */
     71     public void bind(final Context context, final Account account, final boolean isCurrentAccount,
     72             final BitmapCache imagesCache, final ContactResolver contactResolver) {
     73         if (!TextUtils.isEmpty(account.getSenderName())) {
     74             mAccountDisplayName.setText(account.getSenderName());
     75             mAccountAddress.setText(account.getEmailAddress());
     76             mAccountAddress.setVisibility(View.VISIBLE);
     77         } else if (!TextUtils.isEmpty(account.getDisplayName()) &&
     78                 !TextUtils.equals(account.getDisplayName(), account.getEmailAddress())) {
     79             mAccountDisplayName.setText(account.getDisplayName());
     80             mAccountAddress.setText(account.getEmailAddress());
     81             mAccountAddress.setVisibility(View.VISIBLE);
     82         } else {
     83             mAccountDisplayName.setText(account.getEmailAddress());
     84             mAccountAddress.setVisibility(View.GONE);
     85         }
     86         if (isCurrentAccount) {
     87             mCheckmark.setVisibility(View.VISIBLE);
     88             mAccountDisplayName.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
     89             final int blackColor = getResources().getColor(R.color.text_color_black);
     90             mAccountDisplayName.setTextColor(blackColor);
     91             mAccountAddress.setTextColor(blackColor);
     92         } else {
     93             mCheckmark.setVisibility(View.GONE);
     94             mAccountDisplayName.setTypeface(Typeface.DEFAULT);
     95             final int greyColor = getResources().getColor(R.color.text_color_grey);
     96             mAccountDisplayName.setTextColor(greyColor);
     97             mAccountAddress.setTextColor(greyColor);
     98         }
     99 
    100         ImageView v = (ImageView) mAvatar.findViewById(R.id.avatar);
    101         AccountAvatarDrawable drawable = new AccountAvatarDrawable(
    102                 context.getResources(), imagesCache, contactResolver);
    103         final int size = context.getResources().getDimensionPixelSize(
    104                 R.dimen.account_avatar_dimension);
    105         drawable.setDecodeDimensions(size, size);
    106         drawable.bind(account.getSenderName(), account.getEmailAddress());
    107         v.setImageDrawable(drawable);
    108 
    109     }
    110 }
    111