1 /** 2 * Copyright (c) 2012, 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.util.AttributeSet; 21 import android.view.View; 22 import android.widget.AdapterView; 23 import android.widget.AdapterView.OnItemSelectedListener; 24 import android.widget.Spinner; 25 26 import com.android.mail.providers.Account; 27 import com.android.mail.providers.Message; 28 import com.android.mail.providers.ReplyFromAccount; 29 import com.android.mail.utils.AccountUtils; 30 import com.google.common.annotations.VisibleForTesting; 31 import com.google.common.collect.ImmutableList; 32 import com.google.common.collect.Lists; 33 34 import java.util.List; 35 36 public class FromAddressSpinner extends Spinner implements OnItemSelectedListener { 37 private List<Account> mAccounts; 38 private ReplyFromAccount mAccount; 39 private final List<ReplyFromAccount> mReplyFromAccounts = Lists.newArrayList(); 40 private OnAccountChangedListener mAccountChangedListener; 41 42 public FromAddressSpinner(Context context) { 43 this(context, null); 44 } 45 46 public FromAddressSpinner(Context context, AttributeSet set) { 47 super(context, set); 48 } 49 50 public void setCurrentAccount(ReplyFromAccount account) { 51 mAccount = account; 52 selectCurrentAccount(); 53 } 54 55 private void selectCurrentAccount() { 56 if (mAccount == null) { 57 return; 58 } 59 int currentIndex = 0; 60 for (ReplyFromAccount acct : mReplyFromAccounts) { 61 if (TextUtils.equals(mAccount.name, acct.name) 62 && TextUtils.equals(mAccount.address, acct.address)) { 63 setSelection(currentIndex, true); 64 break; 65 } 66 currentIndex++; 67 } 68 } 69 70 public ReplyFromAccount getMatchingReplyFromAccount(String accountString) { 71 if (!TextUtils.isEmpty(accountString)) { 72 for (ReplyFromAccount acct : mReplyFromAccounts) { 73 // TODO: Do not key off ReplyFromAccount.name b/11292541 74 if (accountString.equals(acct.name)) { 75 return acct; 76 } 77 } 78 } 79 return null; 80 } 81 82 public ReplyFromAccount getCurrentAccount() { 83 return mAccount; 84 } 85 86 /** 87 * @param action Action being performed; if this is COMPOSE, show all 88 * accounts. Otherwise, show just the account this was launched 89 * with. 90 * @param currentAccount Account used to launch activity. 91 * @param syncingAccounts 92 */ 93 public void initialize(int action, Account currentAccount, Account[] syncingAccounts, 94 Message refMessage) { 95 final List<Account> accounts = AccountUtils.mergeAccountLists(mAccounts, 96 syncingAccounts, true /* prioritizeAccountList */); 97 if (action == ComposeActivity.COMPOSE) { 98 mAccounts = accounts; 99 } else { 100 // First assume that we are going to use the current account as the reply account 101 Account replyAccount = currentAccount; 102 103 if (refMessage != null && refMessage.accountUri != null) { 104 // This is a reply or forward of a message access through the "combined" account. 105 // We want to make sure that the real account is in the spinner 106 for (Account account : accounts) { 107 if (account.uri.equals(refMessage.accountUri)) { 108 replyAccount = account; 109 break; 110 } 111 } 112 } 113 mAccounts = ImmutableList.of(replyAccount); 114 } 115 initFromSpinner(); 116 } 117 118 @VisibleForTesting 119 protected void initFromSpinner() { 120 // If there are not yet any accounts in the cached synced accounts 121 // because this is the first time mail was opened, and it was opened 122 // directly to the compose activity, don't bother populating the reply 123 // from spinner yet. 124 if (mAccounts == null || mAccounts.size() == 0) { 125 return; 126 } 127 FromAddressSpinnerAdapter adapter = new FromAddressSpinnerAdapter(getContext()); 128 129 mReplyFromAccounts.clear(); 130 for (Account account : mAccounts) { 131 mReplyFromAccounts.addAll(account.getReplyFroms()); 132 } 133 adapter.addAccounts(mReplyFromAccounts); 134 135 setAdapter(adapter); 136 selectCurrentAccount(); 137 setOnItemSelectedListener(this); 138 } 139 140 public List<ReplyFromAccount> getReplyFromAccounts() { 141 return mReplyFromAccounts; 142 } 143 144 public void setOnAccountChangedListener(OnAccountChangedListener listener) { 145 mAccountChangedListener = listener; 146 } 147 148 @Override 149 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 150 ReplyFromAccount selection = (ReplyFromAccount) getItemAtPosition(position); 151 // TODO: Do not key off ReplyFromAccount.name b/11292541 152 if (!selection.name.equals(mAccount.name)) { 153 mAccount = selection; 154 mAccountChangedListener.onAccountChanged(); 155 } 156 } 157 158 @Override 159 public void onNothingSelected(AdapterView<?> parent) { 160 // Do nothing. 161 } 162 163 /** 164 * Classes that want to know when a different account in the 165 * FromAddressSpinner has been selected should implement this interface. 166 * Note: if the user chooses the same account as the one that has already 167 * been selected, this method will not be called. 168 */ 169 public static interface OnAccountChangedListener { 170 public void onAccountChanged(); 171 } 172 } 173