Search in sources :

Example 6 with AccountStats

use of com.fsck.k9.AccountStats in project k-9 by k9mail.

the class MessagingController method getSearchAccountStatsSynchronous.

public AccountStats getSearchAccountStatsSynchronous(final SearchAccount searchAccount, final MessagingListener listener) {
    Preferences preferences = Preferences.getPreferences(context);
    LocalSearch search = searchAccount.getRelatedSearch();
    // Collect accounts that belong to the search
    String[] accountUuids = search.getAccountUuids();
    List<Account> accounts;
    if (search.searchAllAccounts()) {
        accounts = preferences.getAccounts();
    } else {
        accounts = new ArrayList<>(accountUuids.length);
        for (int i = 0, len = accountUuids.length; i < len; i++) {
            String accountUuid = accountUuids[i];
            accounts.set(i, preferences.getAccount(accountUuid));
        }
    }
    ContentResolver cr = context.getContentResolver();
    int unreadMessageCount = 0;
    int flaggedMessageCount = 0;
    String[] projection = { StatsColumns.UNREAD_COUNT, StatsColumns.FLAGGED_COUNT };
    for (Account account : accounts) {
        StringBuilder query = new StringBuilder();
        List<String> queryArgs = new ArrayList<>();
        ConditionsTreeNode conditions = search.getConditions();
        SqlQueryBuilder.buildWhereClause(account, conditions, query, queryArgs);
        String selection = query.toString();
        String[] selectionArgs = queryArgs.toArray(new String[queryArgs.size()]);
        Uri uri = Uri.withAppendedPath(EmailProvider.CONTENT_URI, "account/" + account.getUuid() + "/stats");
        // Query content provider to get the account stats
        Cursor cursor = cr.query(uri, projection, selection, selectionArgs, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                unreadMessageCount += cursor.getInt(0);
                flaggedMessageCount += cursor.getInt(1);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    // Create AccountStats instance...
    AccountStats stats = new AccountStats();
    stats.unreadMessageCount = unreadMessageCount;
    stats.flaggedMessageCount = flaggedMessageCount;
    // ...and notify the listener
    if (listener != null) {
        listener.accountStatusChanged(searchAccount, stats);
    }
    return stats;
}
Also used : ConditionsTreeNode(com.fsck.k9.search.ConditionsTreeNode) SearchAccount(com.fsck.k9.search.SearchAccount) Account(com.fsck.k9.Account) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Uri(android.net.Uri) SuppressLint(android.annotation.SuppressLint) ContentResolver(android.content.ContentResolver) LocalSearch(com.fsck.k9.search.LocalSearch) Preferences(com.fsck.k9.Preferences) AccountStats(com.fsck.k9.AccountStats)

Aggregations

AccountStats (com.fsck.k9.AccountStats)5 Account (com.fsck.k9.Account)4 SearchAccount (com.fsck.k9.search.SearchAccount)4 ArrayList (java.util.ArrayList)4 MessagingException (com.fsck.k9.mail.MessagingException)3 LocalStore (com.fsck.k9.mailstore.LocalStore)3 LocalSearch (com.fsck.k9.search.LocalSearch)3 SuppressLint (android.annotation.SuppressLint)2 ContentResolver (android.content.ContentResolver)2 Cursor (android.database.Cursor)2 Uri (android.net.Uri)2 LocalMessage (com.fsck.k9.mailstore.LocalMessage)2 ConditionsTreeNode (com.fsck.k9.search.ConditionsTreeNode)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 VisibleForTesting (android.support.annotation.VisibleForTesting)1 RemoteViews (android.widget.RemoteViews)1 BaseAccount (com.fsck.k9.BaseAccount)1 Preferences (com.fsck.k9.Preferences)1 UnreadWidgetConfiguration (com.fsck.k9.activity.UnreadWidgetConfiguration)1