Home | History | Annotate | Download | only in utils
      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  */
     16 package com.android.mail.utils;
     17 
     18 import com.android.mail.providers.Account;
     19 import com.android.mail.providers.MailAppProvider;
     20 import com.android.mail.providers.UIProvider;
     21 
     22 import android.content.ContentResolver;
     23 import android.content.Context;
     24 import android.database.Cursor;
     25 import com.google.common.collect.Lists;
     26 
     27 import java.util.ArrayList;
     28 import java.util.List;
     29 
     30 public class AccountUtils {
     31     /**
     32      * Merge two lists of accounts into one list of accounts without duplicates.
     33      *
     34      * @param inList 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.getEmailAddress());
     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].getEmailAddress();
     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 || existingList.contains(accountName)) {
     59                 newAccountList.add(accounts[i]);
     60             }
     61         }
     62         return newAccountList;
     63     }
     64 
     65     /**
     66      * Synchronous method which returns registered accounts that are syncing.
     67      * @param context
     68      * @return
     69      */
     70     public static Account[] getSyncingAccounts(Context context) {
     71         final ContentResolver resolver = context.getContentResolver();
     72         Cursor accountsCursor = null;
     73         final List<Account> accounts = Lists.newArrayList();
     74         Account account;
     75         try {
     76             accountsCursor = resolver.query(MailAppProvider.getAccountsUri(),
     77                     UIProvider.ACCOUNTS_PROJECTION, null, null, null);
     78             if (accountsCursor != null) {
     79                 while (accountsCursor.moveToNext()) {
     80                     account = Account.builder().buildFrom(accountsCursor);
     81                     if (!account.isAccountSyncRequired()) {
     82                         accounts.add(account);
     83                     }
     84                 }
     85             }
     86         } finally {
     87             if (accountsCursor != null) {
     88                 accountsCursor.close();
     89             }
     90         }
     91         return accounts.toArray(new Account[accounts.size()]);
     92     }
     93 
     94     /**
     95      * Synchronous method which returns registered accounts.
     96      * @param context
     97      * @return
     98      */
     99     public static Account[] getAccounts(Context context) {
    100         final ContentResolver resolver = context.getContentResolver();
    101         Cursor accountsCursor = null;
    102         final List<Account> accounts = Lists.newArrayList();
    103         try {
    104             accountsCursor = resolver.query(MailAppProvider.getAccountsUri(),
    105                     UIProvider.ACCOUNTS_PROJECTION, null, null, null);
    106             if (accountsCursor != null) {
    107                 while (accountsCursor.moveToNext()) {
    108                     accounts.add(Account.builder().buildFrom(accountsCursor));
    109                 }
    110             }
    111         } finally {
    112             if (accountsCursor != null) {
    113                 accountsCursor.close();
    114             }
    115         }
    116         return accounts.toArray(new Account[accounts.size()]);
    117     }
    118 }
    119