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_address);
     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         if (fromItem.isCustomFrom) {
     81             ((TextView) fromEntry.findViewById(R.id.spinner_account_name)).setText(fromItem.name);
     82 
     83             ((TextView) fromEntry.findViewById(R.id.spinner_account_address))
     84                     .setText(formatAddress(fromItem.address));
     85         } else {
     86             ((TextView) fromEntry.findViewById(R.id.spinner_account_address))
     87                     .setText(fromItem.address);
     88         }
     89         return fromEntry;
     90     }
     91 
     92     @Override
     93     public View getDropDownView(int position, View convertView, ViewGroup parent) {
     94         ReplyFromAccount fromItem = getItem(position);
     95         int res = fromItem.isCustomFrom ? R.layout.custom_from_dropdown_item
     96                 : R.layout.from_dropdown_item;
     97         View fromEntry = getInflater().inflate(res, null);
     98         if (fromItem.isCustomFrom) {
     99             ((TextView) fromEntry.findViewById(R.id.spinner_account_name)).setText(fromItem.name);
    100             ((TextView) fromEntry.findViewById(R.id.spinner_account_address))
    101                     .setText(formatAddress(fromItem.address));
    102         } else {
    103             ((TextView) fromEntry.findViewById(R.id.spinner_account_address))
    104                     .setText(fromItem.address);
    105         }
    106         return fromEntry;
    107     }
    108 
    109     private CharSequence formatAddress(String address) {
    110         if (TextUtils.isEmpty(address)) {
    111             return "";
    112         }
    113         return String.format(sFormatString, Rfc822Tokenizer.tokenize(address)[0].getAddress());
    114     }
    115 
    116     public void addAccounts(List<ReplyFromAccount> replyFromAccounts) {
    117         // Get the position of the current account
    118         for (ReplyFromAccount account : replyFromAccounts) {
    119             // Add the account to the Adapter
    120             add(account);
    121         }
    122     }
    123 }
    124