summaryrefslogtreecommitdiff
path: root/src/com/android/mail/compose/FromAddressSpinner.java
blob: b8448ee4b78fcce30312d022a503f2e3575039db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
 * Copyright (c) 2012, Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.mail.compose;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;

import com.android.mail.providers.Account;
import com.android.mail.providers.Message;
import com.android.mail.providers.ReplyFromAccount;
import com.android.mail.utils.AccountUtils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

import java.util.List;

public class FromAddressSpinner extends Spinner implements OnItemSelectedListener {
    private List<Account> mAccounts;
    private ReplyFromAccount mAccount;
    private final List<ReplyFromAccount> mReplyFromAccounts = Lists.newArrayList();
    private OnAccountChangedListener mAccountChangedListener;

    public FromAddressSpinner(Context context) {
        this(context, null);
    }

    public FromAddressSpinner(Context context, AttributeSet set) {
        super(context, set);
    }

    public void setCurrentAccount(ReplyFromAccount account) {
        mAccount = account;
        selectCurrentAccount();
    }

    private void selectCurrentAccount() {
        if (mAccount == null) {
            return;
        }
        int currentIndex = 0;
        for (ReplyFromAccount acct : mReplyFromAccounts) {
            if (TextUtils.equals(mAccount.name, acct.name)
                    && TextUtils.equals(mAccount.address, acct.address)) {
                setSelection(currentIndex, true);
                break;
            }
            currentIndex++;
        }
    }

    public ReplyFromAccount getMatchingReplyFromAccount(String accountString) {
        if (!TextUtils.isEmpty(accountString)) {
            for (ReplyFromAccount acct : mReplyFromAccounts) {
                if (accountString.equals(acct.address)) {
                    return acct;
                }
            }
        }
        return null;
    }

    public ReplyFromAccount getCurrentAccount() {
        return mAccount;
    }

    /**
     * @param action Action being performed; if this is COMPOSE, show all
     *            accounts. Otherwise, show just the account this was launched
     *            with.
     * @param currentAccount Account used to launch activity.
     * @param syncingAccounts
     */
    public void initialize(int action, Account currentAccount, Account[] syncingAccounts,
                Message refMessage) {
        final List<Account> accounts = AccountUtils.mergeAccountLists(mAccounts,
                syncingAccounts, true /* prioritizeAccountList */);
        if (action == ComposeActivity.COMPOSE) {
            mAccounts = accounts;
        } else {
            // First assume that we are going to use the current account as the reply account
            Account replyAccount = currentAccount;

            if (refMessage != null && refMessage.accountUri != null) {
                // This is a reply or forward of a message access through the "combined" account.
                // We want to make sure that the real account is in the spinner
                for (Account account : accounts) {
                    if (account.uri.equals(refMessage.accountUri)) {
                        replyAccount = account;
                        break;
                    }
                }
            }
            mAccounts = ImmutableList.of(replyAccount);
        }
        initFromSpinner();
    }

    @VisibleForTesting
    protected void initFromSpinner() {
        // If there are not yet any accounts in the cached synced accounts
        // because this is the first time mail was opened, and it was opened
        // directly to the compose activity, don't bother populating the reply
        // from spinner yet.
        if (mAccounts == null || mAccounts.size() == 0) {
            return;
        }
        FromAddressSpinnerAdapter adapter =
                new FromAddressSpinnerAdapter(getContext());

        mReplyFromAccounts.clear();
        for (Account account : mAccounts) {
            mReplyFromAccounts.addAll(account.getReplyFroms());
        }
        adapter.addAccounts(mReplyFromAccounts);

        setAdapter(adapter);
        selectCurrentAccount();
        setOnItemSelectedListener(this);
    }

    public List<ReplyFromAccount> getReplyFromAccounts() {
        return mReplyFromAccounts;
    }

    public void setOnAccountChangedListener(OnAccountChangedListener listener) {
        mAccountChangedListener = listener;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        ReplyFromAccount selection = (ReplyFromAccount) getItemAtPosition(position);
        if (!selection.address.equals(mAccount.address)) {
            mAccount = selection;
            mAccountChangedListener.onAccountChanged();
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // Do nothing.
    }

    /**
     * Classes that want to know when a different account in the
     * FromAddressSpinner has been selected should implement this interface.
     * Note: if the user chooses the same account as the one that has already
     * been selected, this method will not be called.
     */
    public static interface OnAccountChangedListener {
        public void onAccountChanged();
    }
}