From ba691612dd2ff6a3e00e520eed27bf67ec840c66 Mon Sep 17 00:00:00 2001 From: cketti Date: Sat, 8 Dec 2012 18:15:34 +0100 Subject: [PATCH] Add support for "Unified Inbox" and "All messages" to unread widget --- .../activity/UnreadWidgetConfiguration.java | 12 +- .../k9/controller/MessagingController.java | 124 ++++++++++-------- .../k9/provider/UnreadWidgetProvider.java | 59 ++++++--- 3 files changed, 113 insertions(+), 82 deletions(-) diff --git a/src/com/fsck/k9/activity/UnreadWidgetConfiguration.java b/src/com/fsck/k9/activity/UnreadWidgetConfiguration.java index 341f895ef..6b049b688 100644 --- a/src/com/fsck/k9/activity/UnreadWidgetConfiguration.java +++ b/src/com/fsck/k9/activity/UnreadWidgetConfiguration.java @@ -7,7 +7,6 @@ import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; -import com.fsck.k9.Account; import com.fsck.k9.BaseAccount; import com.fsck.k9.R; import com.fsck.k9.provider.UnreadWidgetProvider; @@ -57,18 +56,11 @@ public class UnreadWidgetConfiguration extends AccountList { @Override protected boolean displaySpecialAccounts() { - return false; + return true; } @Override - protected void onAccountSelected(BaseAccount baseAccount) { - if (!(baseAccount instanceof Account)) { - finish(); - return; - } - - Account account = (Account) baseAccount; - + protected void onAccountSelected(BaseAccount account) { // Save widget configuration String accountUuid = account.getUuid(); saveAccountUuid(this, mAppWidgetId, accountUuid); diff --git a/src/com/fsck/k9/controller/MessagingController.java b/src/com/fsck/k9/controller/MessagingController.java index 1242d843d..9b9db5214 100644 --- a/src/com/fsck/k9/controller/MessagingController.java +++ b/src/com/fsck/k9/controller/MessagingController.java @@ -3391,67 +3391,77 @@ public class MessagingController implements Runnable { threadPool.execute(new Runnable() { @Override public void run() { - Preferences preferences = Preferences.getPreferences(mApplication); - LocalSearch search = searchAccount.getRelatedSearch(); - - // Collect accounts that belong to the search - String[] accountUuids = search.getAccountUuids(); - Account[] accounts; - if (search.searchAllAccounts()) { - accounts = preferences.getAccounts(); - } else { - accounts = new Account[accountUuids.length]; - for (int i = 0, len = accountUuids.length; i < len; i++) { - String accountUuid = accountUuids[i]; - accounts[i] = preferences.getAccount(accountUuid); - } - } - - ContentResolver cr = mApplication.getContentResolver(); - - int unreadMessageCount = 0; - int flaggedMessageCount = 0; - - String[] projection = { - StatsColumns.UNREAD_COUNT, - StatsColumns.FLAGGED_COUNT - }; - - for (Account account : accounts) { - StringBuilder query = new StringBuilder(); - List queryArgs = new ArrayList(); - ConditionsTreeNode conditions = search.getConditions(); - SqlQueryBuilder.buildWhereClause(account, conditions, query, queryArgs); - - String selection = query.toString(); - String[] selectionArgs = queryArgs.toArray(EMPTY_STRING_ARRAY); - - 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.moveToFirst()) { - unreadMessageCount += cursor.getInt(0); - flaggedMessageCount += cursor.getInt(1); - } - } finally { - cursor.close(); - } - } - - // Create AccountStats instance... - AccountStats stats = new AccountStats(); - stats.unreadMessageCount = unreadMessageCount; - stats.flaggedMessageCount = flaggedMessageCount; - - // ...and notify the listener - listener.accountStatusChanged(searchAccount, stats); + getSearchAccountStatsSynchronous(searchAccount, listener); } }); } + public AccountStats getSearchAccountStatsSynchronous(final SearchAccount searchAccount, + final MessagingListener listener) { + + Preferences preferences = Preferences.getPreferences(mApplication); + LocalSearch search = searchAccount.getRelatedSearch(); + + // Collect accounts that belong to the search + String[] accountUuids = search.getAccountUuids(); + Account[] accounts; + if (search.searchAllAccounts()) { + accounts = preferences.getAccounts(); + } else { + accounts = new Account[accountUuids.length]; + for (int i = 0, len = accountUuids.length; i < len; i++) { + String accountUuid = accountUuids[i]; + accounts[i] = preferences.getAccount(accountUuid); + } + } + + ContentResolver cr = mApplication.getContentResolver(); + + int unreadMessageCount = 0; + int flaggedMessageCount = 0; + + String[] projection = { + StatsColumns.UNREAD_COUNT, + StatsColumns.FLAGGED_COUNT + }; + + for (Account account : accounts) { + StringBuilder query = new StringBuilder(); + List queryArgs = new ArrayList(); + ConditionsTreeNode conditions = search.getConditions(); + SqlQueryBuilder.buildWhereClause(account, conditions, query, queryArgs); + + String selection = query.toString(); + String[] selectionArgs = queryArgs.toArray(EMPTY_STRING_ARRAY); + + 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.moveToFirst()) { + unreadMessageCount += cursor.getInt(0); + flaggedMessageCount += cursor.getInt(1); + } + } finally { + 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; + } + public void getFolderUnreadMessageCount(final Account account, final String folderName, final MessagingListener l) { Runnable unreadRunnable = new Runnable() { diff --git a/src/com/fsck/k9/provider/UnreadWidgetProvider.java b/src/com/fsck/k9/provider/UnreadWidgetProvider.java index 2d904f1f9..8a56bdd11 100644 --- a/src/com/fsck/k9/provider/UnreadWidgetProvider.java +++ b/src/com/fsck/k9/provider/UnreadWidgetProvider.java @@ -2,13 +2,16 @@ package com.fsck.k9.provider; import com.fsck.k9.Account; import com.fsck.k9.AccountStats; +import com.fsck.k9.BaseAccount; import com.fsck.k9.K9; import com.fsck.k9.Preferences; import com.fsck.k9.R; import com.fsck.k9.activity.UnreadWidgetConfiguration; import com.fsck.k9.activity.FolderList; import com.fsck.k9.activity.MessageList; +import com.fsck.k9.controller.MessagingController; import com.fsck.k9.search.LocalSearch; +import com.fsck.k9.search.SearchAccount; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; @@ -53,22 +56,48 @@ public class UnreadWidgetProvider extends AppWidgetProvider { String accountName = context.getString(R.string.app_name); Intent clickIntent = null; try { - Account account = Preferences.getPreferences(context).getAccount(accountUuid); - if (account != null) { - AccountStats stats = account.getStats(context); - unreadCount = stats.unreadMessageCount; - accountName = account.getDescription(); - if (K9.FOLDER_NONE.equals(account.getAutoExpandFolderName())) { - clickIntent = FolderList.actionHandleAccountIntent(context, account, null, - false); - } else { - LocalSearch search = new LocalSearch(account.getAutoExpandFolderName()); - search.addAllowedFolder(account.getAutoExpandFolderName()); - search.addAccountUuid(account.getUuid()); - clickIntent = MessageList.intentDisplaySearch(context, search, false, true, - true); + BaseAccount account = null; + AccountStats stats = null; + + SearchAccount searchAccount = null; + if (SearchAccount.UNIFIED_INBOX.equals(accountUuid)) { + searchAccount = SearchAccount.createUnifiedInboxAccount(context); + } else if (SearchAccount.ALL_MESSAGES.equals(accountUuid)) { + searchAccount = SearchAccount.createAllMessagesAccount(context); + } + + if (searchAccount != null) { + account = searchAccount; + MessagingController controller = MessagingController.getInstance(K9.app); + stats = controller.getSearchAccountStatsSynchronous(searchAccount, null); + clickIntent = MessageList.intentDisplaySearch(context, + searchAccount.getRelatedSearch(), false, true, true); + } else { + Account realAccount = Preferences.getPreferences(context).getAccount(accountUuid); + if (realAccount != null) { + account = realAccount; + stats = realAccount.getStats(context); + + if (K9.FOLDER_NONE.equals(realAccount.getAutoExpandFolderName())) { + clickIntent = FolderList.actionHandleAccountIntent(context, realAccount, + null, false); + } else { + LocalSearch search = new LocalSearch(realAccount.getAutoExpandFolderName()); + search.addAllowedFolder(realAccount.getAutoExpandFolderName()); + search.addAccountUuid(account.getUuid()); + clickIntent = MessageList.intentDisplaySearch(context, search, false, true, + true); + } + clickIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); } - clickIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); + } + + if (account != null) { + accountName = account.getDescription(); + } + + if (stats != null) { + unreadCount = stats.unreadMessageCount; } } catch (Exception e) { if (K9.DEBUG) {