1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-30 13:12:25 -05:00

Add support for "Unified Inbox" and "All messages" to unread widget

This commit is contained in:
cketti 2012-12-08 18:15:34 +01:00
parent cc3580c675
commit ba691612dd
3 changed files with 113 additions and 82 deletions

View File

@ -7,7 +7,6 @@ import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.os.Bundle; import android.os.Bundle;
import com.fsck.k9.Account;
import com.fsck.k9.BaseAccount; import com.fsck.k9.BaseAccount;
import com.fsck.k9.R; import com.fsck.k9.R;
import com.fsck.k9.provider.UnreadWidgetProvider; import com.fsck.k9.provider.UnreadWidgetProvider;
@ -57,18 +56,11 @@ public class UnreadWidgetConfiguration extends AccountList {
@Override @Override
protected boolean displaySpecialAccounts() { protected boolean displaySpecialAccounts() {
return false; return true;
} }
@Override @Override
protected void onAccountSelected(BaseAccount baseAccount) { protected void onAccountSelected(BaseAccount account) {
if (!(baseAccount instanceof Account)) {
finish();
return;
}
Account account = (Account) baseAccount;
// Save widget configuration // Save widget configuration
String accountUuid = account.getUuid(); String accountUuid = account.getUuid();
saveAccountUuid(this, mAppWidgetId, accountUuid); saveAccountUuid(this, mAppWidgetId, accountUuid);

View File

@ -3391,67 +3391,77 @@ public class MessagingController implements Runnable {
threadPool.execute(new Runnable() { threadPool.execute(new Runnable() {
@Override @Override
public void run() { public void run() {
Preferences preferences = Preferences.getPreferences(mApplication); getSearchAccountStatsSynchronous(searchAccount, listener);
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<String> queryArgs = new ArrayList<String>();
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);
} }
}); });
} }
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<String> queryArgs = new ArrayList<String>();
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, public void getFolderUnreadMessageCount(final Account account, final String folderName,
final MessagingListener l) { final MessagingListener l) {
Runnable unreadRunnable = new Runnable() { Runnable unreadRunnable = new Runnable() {

View File

@ -2,13 +2,16 @@ package com.fsck.k9.provider;
import com.fsck.k9.Account; import com.fsck.k9.Account;
import com.fsck.k9.AccountStats; import com.fsck.k9.AccountStats;
import com.fsck.k9.BaseAccount;
import com.fsck.k9.K9; import com.fsck.k9.K9;
import com.fsck.k9.Preferences; import com.fsck.k9.Preferences;
import com.fsck.k9.R; import com.fsck.k9.R;
import com.fsck.k9.activity.UnreadWidgetConfiguration; import com.fsck.k9.activity.UnreadWidgetConfiguration;
import com.fsck.k9.activity.FolderList; import com.fsck.k9.activity.FolderList;
import com.fsck.k9.activity.MessageList; import com.fsck.k9.activity.MessageList;
import com.fsck.k9.controller.MessagingController;
import com.fsck.k9.search.LocalSearch; import com.fsck.k9.search.LocalSearch;
import com.fsck.k9.search.SearchAccount;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
@ -53,22 +56,48 @@ public class UnreadWidgetProvider extends AppWidgetProvider {
String accountName = context.getString(R.string.app_name); String accountName = context.getString(R.string.app_name);
Intent clickIntent = null; Intent clickIntent = null;
try { try {
Account account = Preferences.getPreferences(context).getAccount(accountUuid); BaseAccount account = null;
if (account != null) { AccountStats stats = null;
AccountStats stats = account.getStats(context);
unreadCount = stats.unreadMessageCount; SearchAccount searchAccount = null;
accountName = account.getDescription(); if (SearchAccount.UNIFIED_INBOX.equals(accountUuid)) {
if (K9.FOLDER_NONE.equals(account.getAutoExpandFolderName())) { searchAccount = SearchAccount.createUnifiedInboxAccount(context);
clickIntent = FolderList.actionHandleAccountIntent(context, account, null, } else if (SearchAccount.ALL_MESSAGES.equals(accountUuid)) {
false); searchAccount = SearchAccount.createAllMessagesAccount(context);
} else { }
LocalSearch search = new LocalSearch(account.getAutoExpandFolderName());
search.addAllowedFolder(account.getAutoExpandFolderName()); if (searchAccount != null) {
search.addAccountUuid(account.getUuid()); account = searchAccount;
clickIntent = MessageList.intentDisplaySearch(context, search, false, true, MessagingController controller = MessagingController.getInstance(K9.app);
true); 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) { } catch (Exception e) {
if (K9.DEBUG) { if (K9.DEBUG) {