Home | History | Annotate | Download | only in compose
      1 /**
      2  * Copyright (c) 2011, 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.compose;
     17 
     18 import android.content.Context;
     19 import android.text.TextUtils;
     20 import android.text.util.Rfc822Tokenizer;
     21 import android.view.LayoutInflater;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     24 import android.widget.ArrayAdapter;
     25 import android.widget.TextView;
     26 
     27 import com.android.mail.R;
     28 import com.android.mail.providers.ReplyFromAccount;
     29 
     30 import java.util.List;
     31 
     32 /**
     33  * FromAddressSpinnerAdapter returns the correct spinner adapter for reply from
     34  * addresses based on device size.
     35  *
     36  * @author mindyp (at) google.com
     37  */
     38 public class FromAddressSpinnerAdapter extends ArrayAdapter<ReplyFromAccount> {
     39     private static final int FROM = 0;
     40     private static final int CUSTOM_FROM = 1;
     41     private static String sFormatString;
     42 
     43     public static int REAL_ACCOUNT = 2;
     44 
     45     public static int ACCOUNT_DISPLAY = 0;
     46 
     47     public static int ACCOUNT_ADDRESS = 1;
     48 
     49     private LayoutInflater mInflater;
     50 
     51     public FromAddressSpinnerAdapter(Context context) {
     52         super(context, R.layout.from_item, R.id.spinner_account_name);
     53         sFormatString = getContext().getString(R.string.formatted_email_address);
     54     }
     55 
     56     protected LayoutInflater getInflater() {
     57         if (mInflater == null) {
     58             mInflater = (LayoutInflater) getContext().getSystemService(
     59                     Context.LAYOUT_INFLATER_SERVICE);
     60         }
     61         return mInflater;
     62     }
     63 
     64     @Override
     65     public int getViewTypeCount() {
     66         // FROM and CUSTOM_FROM
     67         return 2;
     68     }
     69 
     70     @Override
     71     public int getItemViewType(int pos) {
     72         return getItem(pos).isCustomFrom ? CUSTOM_FROM : FROM;
     73     }
     74 
     75     @Override
     76     public View getView(int position, View convertView, ViewGroup parent) {
     77         ReplyFromAccount fromItem = getItem(position);
     78         int res = fromItem.isCustomFrom ? R.layout.custom_from_item : R.layout.from_item;
     79         View fromEntry = convertView == null ? getInflater().inflate(res, null) : convertView;
     80         ((TextView) fromEntry.findViewById(R.id.spinner_account_name)).setText(fromItem.name);
     81         if (fromItem.isCustomFrom) {
     82             ((TextView) fromEntry.findViewById(R.id.spinner_account_address))
     83                     .setText(formatAddress(fromItem.address));
     84         }
     85         return fromEntry;
     86     }
     87 
     88     @Override
     89     public View getDropDownView(int position, View convertView, ViewGroup parent) {
     90         ReplyFromAccount fromItem = getItem(position);
     91         int res = fromItem.isCustomFrom ? R.layout.custom_from_dropdown_item
     92                 : R.layout.from_dropdown_item;
     93         View fromEntry = getInflater().inflate(res, null);
     94         TextView acctName = ((TextView) fromEntry.findViewById(R.id.spinner_account_name));
     95         acctName.setText(fromItem.name);
     96         if (fromItem.isCustomFrom) {
     97             ((TextView) fromEntry.findViewById(R.id.spinner_account_address))
     98                     .setText(formatAddress(fromItem.address));
     99         }
    100         return fromEntry;
    101     }
    102 
    103     private static CharSequence formatAddress(String address) {
    104         if (TextUtils.isEmpty(address)) {
    105             return "";
    106         }
    107         return String.format(sFormatString, Rfc822Tokenizer.tokenize(address)[0].getAddress());
    108     }
    109 
    110     public void addAccounts(List<ReplyFromAccount> replyFromAccounts) {
    111         // Get the position of the current account
    112         for (ReplyFromAccount account : replyFromAccounts) {
    113             // Add the account to the Adapter
    114             add(account);
    115         }
    116     }
    117 }
    118