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@google.com 37 */ 38 public class FromAddressSpinnerAdapter extends ArrayAdapter<ReplyFromAccount> { 39 private static String sFormatString; 40 41 private LayoutInflater mInflater; 42 FromAddressSpinnerAdapter(Context context)43 public FromAddressSpinnerAdapter(Context context) { 44 super(context, R.layout.from_item, R.id.spinner_account_address); 45 sFormatString = getContext().getString(R.string.formatted_email_address); 46 } 47 getInflater()48 protected LayoutInflater getInflater() { 49 if (mInflater == null) { 50 mInflater = (LayoutInflater) getContext().getSystemService( 51 Context.LAYOUT_INFLATER_SERVICE); 52 } 53 return mInflater; 54 } 55 56 @Override getView(int position, View convertView, ViewGroup parent)57 public View getView(int position, View convertView, ViewGroup parent) { 58 final ReplyFromAccount fromItem = getItem(position); 59 final View fromEntry = convertView == null ? 60 getInflater().inflate(R.layout.from_item, null) : convertView; 61 final TextView nameView = (TextView) fromEntry.findViewById(R.id.spinner_account_name); 62 if (fromItem.isCustomFrom) { 63 nameView.setText(fromItem.name); 64 nameView.setVisibility(View.VISIBLE); 65 66 ((TextView) fromEntry.findViewById(R.id.spinner_account_address)) 67 .setText(formatAddress(fromItem.address)); 68 } else { 69 nameView.setVisibility(View.GONE); 70 ((TextView) fromEntry.findViewById(R.id.spinner_account_address)) 71 .setText(fromItem.address); 72 } 73 return fromEntry; 74 } 75 76 @Override getDropDownView(int position, View convertView, ViewGroup parent)77 public View getDropDownView(int position, View convertView, ViewGroup parent) { 78 final ReplyFromAccount fromItem = getItem(position); 79 final int res = fromItem.isCustomFrom ? R.layout.custom_from_dropdown_item 80 : R.layout.from_dropdown_item; 81 final View fromEntry = getInflater().inflate(res, null); 82 if (fromItem.isCustomFrom) { 83 ((TextView) fromEntry.findViewById(R.id.spinner_account_name)).setText(fromItem.name); 84 ((TextView) fromEntry.findViewById(R.id.spinner_account_address)) 85 .setText(formatAddress(fromItem.address)); 86 } else { 87 ((TextView) fromEntry.findViewById(R.id.spinner_account_address)) 88 .setText(fromItem.address); 89 } 90 return fromEntry; 91 } 92 formatAddress(String address)93 private CharSequence formatAddress(String address) { 94 if (TextUtils.isEmpty(address)) { 95 return ""; 96 } 97 return String.format(sFormatString, Rfc822Tokenizer.tokenize(address)[0].getAddress()); 98 } 99 addAccounts(List<ReplyFromAccount> replyFromAccounts)100 public void addAccounts(List<ReplyFromAccount> replyFromAccounts) { 101 // Get the position of the current account 102 for (ReplyFromAccount account : replyFromAccounts) { 103 // Add the account to the Adapter 104 add(account); 105 } 106 } 107 } 108