AccountUtils.java revision dfa1dec0cfdc263c51e971dc3c41b90386a24340
1/*
2 * Copyright (C) 2012 The Android Open Source Project
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 */
16package com.android.mail.utils;
17
18import com.android.mail.providers.Account;
19import com.android.mail.providers.MailAppProvider;
20import com.android.mail.providers.UIProvider;
21
22import android.content.ContentResolver;
23import android.content.Context;
24import android.database.Cursor;
25import com.google.common.collect.Lists;
26
27import java.util.ArrayList;
28import java.util.List;
29
30public class AccountUtils {
31    /**
32     * Merge two lists of accounts into one list of accounts without duplicates.
33     *
34     * @param existingList List of accounts.
35     * @param accounts Accounts to merge in.
36     * @param prioritizeAccountList Boolean indicating whether this method
37     *            should prioritize the list of Account objects when merging the
38     *            lists
39     * @return Merged list of accounts.
40     */
41    public static List<Account> mergeAccountLists(List<Account> inList, Account[] accounts,
42            boolean prioritizeAccountList) {
43
44        List<Account> newAccountList = new ArrayList<Account>();
45        List<String> existingList = new ArrayList<String>();
46        if (inList != null) {
47            for (Account account : inList) {
48                existingList.add(account.name);
49            }
50        }
51        // Make sure the accounts are actually synchronized
52        // (we won't be able to save/send for accounts that
53        // have never been synchronized)
54        for (int i = 0; i < accounts.length; i++) {
55            final String accountName = accounts[i].name;
56            // If the account is in the cached list or the caller requested
57            // that we prioritize the list of Account objects, put it in the new list
58            if (prioritizeAccountList
59                    || (existingList != null && existingList.contains(accountName))) {
60                newAccountList.add(accounts[i]);
61            }
62        }
63        return newAccountList;
64    }
65
66    /**
67     * Synchronous method which returns registered accounts that are syncing.
68     * @param context
69     * @return
70     */
71    public static Account[] getSyncingAccounts(Context context) {
72        final ContentResolver resolver = context.getContentResolver();
73        Cursor accountsCursor = null;
74        final List<Account> accounts = Lists.newArrayList();
75        Account account;
76        try {
77            accountsCursor = resolver.query(MailAppProvider.getAccountsUri(),
78                    UIProvider.ACCOUNTS_PROJECTION, null, null, null);
79            if (accountsCursor != null) {
80                while (accountsCursor.moveToNext()) {
81                    account = new Account(accountsCursor);
82                    if (!account.isAccountSyncRequired()) {
83                        accounts.add(account);
84                    }
85                }
86            }
87        } finally {
88            if (accountsCursor != null) {
89                accountsCursor.close();
90            }
91        }
92        return accounts.toArray(new Account[accounts.size()]);
93    }
94
95    /**
96     * Synchronous method which returns registered accounts.
97     * @param context
98     * @return
99     */
100    public static Account[] getAccounts(Context context) {
101        final ContentResolver resolver = context.getContentResolver();
102        Cursor accountsCursor = null;
103        final List<Account> accounts = Lists.newArrayList();
104        try {
105            accountsCursor = resolver.query(MailAppProvider.getAccountsUri(),
106                    UIProvider.ACCOUNTS_PROJECTION, null, null, null);
107            if (accountsCursor != null) {
108                while (accountsCursor.moveToNext()) {
109                    accounts.add(new Account(accountsCursor));
110                }
111            }
112        } finally {
113            if (accountsCursor != null) {
114                accountsCursor.close();
115            }
116        }
117        return accounts.toArray(new Account[accounts.size()]);
118    }
119}
120