Home | History | Annotate | Download | only in list
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      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 
     17 package com.android.contacts.list;
     18 
     19 import android.content.Context;
     20 import android.text.TextUtils;
     21 import android.util.AttributeSet;
     22 import android.util.Log;
     23 import android.view.View;
     24 import android.widget.ImageView;
     25 import android.widget.LinearLayout;
     26 import android.widget.RadioButton;
     27 import android.widget.TextView;
     28 
     29 import com.android.contacts.R;
     30 import com.android.contacts.model.AccountTypeManager;
     31 import com.android.contacts.model.account.AccountType;
     32 
     33 /**
     34  * Contact list filter parameters.
     35  */
     36 public class ContactListFilterView extends LinearLayout {
     37 
     38     private static final String TAG = ContactListFilterView.class.getSimpleName();
     39 
     40     private ImageView mIcon;
     41     private TextView mAccountType;
     42     private TextView mAccountUserName;
     43     private RadioButton mRadioButton;
     44     private ContactListFilter mFilter;
     45     private boolean mSingleAccount;
     46 
     47     public ContactListFilterView(Context context) {
     48         super(context);
     49     }
     50 
     51     public ContactListFilterView(Context context, AttributeSet attrs) {
     52         super(context, attrs);
     53     }
     54 
     55     public void setContactListFilter(ContactListFilter filter) {
     56         mFilter = filter;
     57     }
     58 
     59     public ContactListFilter getContactListFilter() {
     60         return mFilter;
     61     }
     62 
     63     public void setSingleAccount(boolean flag) {
     64         this.mSingleAccount = flag;
     65     }
     66 
     67     @Override
     68     public void setActivated(boolean activated) {
     69         super.setActivated(activated);
     70         if (mRadioButton != null) {
     71             mRadioButton.setChecked(activated);
     72         } else {
     73             // We're guarding against null-pointer exceptions,
     74             // but otherwise this code is not expected to work
     75             // properly if the button hasn't been initialized.
     76             Log.wtf(TAG, "radio-button cannot be activated because it is null");
     77         }
     78         setContentDescription(generateContentDescription());
     79     }
     80 
     81     public boolean isChecked() {
     82         return mRadioButton.isChecked();
     83     }
     84 
     85     public void bindView(AccountTypeManager accountTypes) {
     86         if (mAccountType == null) {
     87             mIcon = (ImageView) findViewById(R.id.icon);
     88             mAccountType = (TextView) findViewById(R.id.accountType);
     89             mAccountUserName = (TextView) findViewById(R.id.accountUserName);
     90             mRadioButton = (RadioButton) findViewById(R.id.radioButton);
     91             mRadioButton.setChecked(isActivated());
     92         }
     93 
     94         if (mFilter == null) {
     95             mAccountType.setText(R.string.contactsList);
     96             return;
     97         }
     98 
     99         mAccountUserName.setVisibility(View.GONE);
    100         switch (mFilter.filterType) {
    101             case ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS: {
    102                 bindView(0, R.string.list_filter_all_accounts);
    103                 break;
    104             }
    105             case ContactListFilter.FILTER_TYPE_STARRED: {
    106                 bindView(R.drawable.quantum_ic_star_vd_theme_24, R.string.list_filter_all_starred);
    107                 break;
    108             }
    109             case ContactListFilter.FILTER_TYPE_CUSTOM: {
    110                 bindView(0, R.string.list_filter_customize);
    111                 break;
    112             }
    113             case ContactListFilter.FILTER_TYPE_WITH_PHONE_NUMBERS_ONLY: {
    114                 bindView(0, R.string.list_filter_phones);
    115                 break;
    116             }
    117             case ContactListFilter.FILTER_TYPE_SINGLE_CONTACT: {
    118                 bindView(0, R.string.list_filter_single);
    119                 break;
    120             }
    121             case ContactListFilter.FILTER_TYPE_ACCOUNT: {
    122                 mAccountUserName.setVisibility(View.VISIBLE);
    123                 mIcon.setVisibility(View.VISIBLE);
    124                 if (mFilter.icon != null) {
    125                     mIcon.setImageDrawable(mFilter.icon);
    126                 } else {
    127                     mIcon.setImageResource(R.drawable.unknown_source);
    128                 }
    129                 final AccountType accountType =
    130                         accountTypes.getAccountType(mFilter.accountType, mFilter.dataSet);
    131                 mAccountUserName.setText(mFilter.accountName);
    132                 mAccountType.setText(accountType.getDisplayLabel(getContext()));
    133                 break;
    134             }
    135         }
    136         setContentDescription(generateContentDescription());
    137     }
    138 
    139     private void bindView(int iconResource, int textResource) {
    140         if (iconResource != 0) {
    141             mIcon.setVisibility(View.VISIBLE);
    142             mIcon.setImageResource(iconResource);
    143         } else {
    144             mIcon.setVisibility(View.GONE);
    145         }
    146 
    147         mAccountType.setText(textResource);
    148     }
    149 
    150     String generateContentDescription() {
    151         final StringBuilder sb = new StringBuilder();
    152         if (!TextUtils.isEmpty(mAccountType.getText())) {
    153             sb.append(mAccountType.getText());
    154         }
    155         if (!TextUtils.isEmpty(mAccountUserName.getText())) {
    156             if (sb.length() > 0) {
    157                 sb.append(" ");
    158             }
    159             sb.append(mAccountUserName.getText());
    160         }
    161         return getContext().getString(isActivated() ? R.string.account_filter_view_checked :
    162                 R.string.account_filter_view_not_checked, sb.toString());
    163     }
    164 }
    165