1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 11:42:16 -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.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);

View File

@ -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<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);
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<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,
final MessagingListener l) {
Runnable unreadRunnable = new Runnable() {

View File

@ -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) {