From ee4ac077882d53b0df576557e7747c0a9dcc3500 Mon Sep 17 00:00:00 2001 From: Daniel Applebaum Date: Wed, 8 Apr 2009 18:14:52 +0000 Subject: [PATCH] Issue 364 Provide preference for which folder is automatically expanded when entering the Folder/Message list. Can be set to -NONE-, so that no folder is automatically expanded. Defaults to Inbox, which is like the default behavior of the core Android Email. Fails to move the automatically opened folder to the top of the screen. This needs more work. The already selected folder should be highlighted (or otherwise indicated) in the ChooseFolder Activity. This needs more work. --- res/values/strings.xml | 4 +- res/xml/account_settings_preferences.xml | 7 ++ src/com/android/email/Account.java | 13 +++ src/com/android/email/Email.java | 5 + src/com/android/email/activity/Accounts.java | 4 +- .../android/email/activity/ChooseFolder.java | 63 ++++++++++- .../email/activity/FolderMessageList.java | 106 +++++++++--------- .../email/activity/setup/AccountSettings.java | 72 ++++++++++++ .../android/email/service/MailService.java | 8 +- 9 files changed, 216 insertions(+), 66 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 2a5bea351..ae2505019 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -263,7 +263,9 @@ Welcome to K-9 Mail setup. K-9 is an open source email client for Android based Sent folder name Trash folder name Outbox folder name - + + Auto-expand folder + WebDav(Exchange) path Optional diff --git a/res/xml/account_settings_preferences.xml b/res/xml/account_settings_preferences.xml index 0ddfec7d5..9affb8db5 100644 --- a/res/xml/account_settings_preferences.xml +++ b/res/xml/account_settings_preferences.xml @@ -45,6 +45,13 @@ android:entryValues="@array/account_settings_hide_buttons_values" android:dialogTitle="@string/account_settings_hide_buttons_label" /> + + diff --git a/src/com/android/email/Account.java b/src/com/android/email/Account.java index 97f41edf4..79cf722ed 100644 --- a/src/com/android/email/Account.java +++ b/src/com/android/email/Account.java @@ -47,6 +47,7 @@ public class Account implements Serializable { String mSentFolderName; String mTrashFolderName; String mOutboxFolderName; + String mAutoExpandFolderName; FolderMode mFolderDisplayMode; FolderMode mFolderSyncMode; FolderMode mFolderTargetMode; @@ -128,6 +129,8 @@ public class Account implements Serializable { "Trash"); mOutboxFolderName = preferences.mSharedPreferences.getString(mUuid + ".outboxFolderName", "Outbox"); + mAutoExpandFolderName = preferences.mSharedPreferences.getString(mUuid + ".autoExpandFolderName", + "Inbox"); mAccountNumber = preferences.mSharedPreferences.getInt(mUuid + ".accountNumber", 0); mVibrate = preferences.mSharedPreferences.getBoolean(mUuid + ".vibrate", false); @@ -282,6 +285,7 @@ public class Account implements Serializable { editor.remove(mUuid + ".sentFolderName"); editor.remove(mUuid + ".trashFolderName"); editor.remove(mUuid + ".outboxFolderName"); + editor.remove(mUuid + ".autoExpandFolderName"); editor.remove(mUuid + ".accountNumber"); editor.remove(mUuid + ".vibrate"); editor.remove(mUuid + ".ringtone"); @@ -349,6 +353,7 @@ public class Account implements Serializable { editor.putString(mUuid + ".sentFolderName", mSentFolderName); editor.putString(mUuid + ".trashFolderName", mTrashFolderName); editor.putString(mUuid + ".outboxFolderName", mOutboxFolderName); + editor.putString(mUuid + ".autoExpandFolderName", mAutoExpandFolderName); editor.putInt(mUuid + ".accountNumber", mAccountNumber); editor.putBoolean(mUuid + ".vibrate", mVibrate); editor.putString(mUuid + ".hideButtonsEnum", mHideMessageViewButtons.name()); @@ -518,6 +523,14 @@ public class Account implements Serializable { mOutboxFolderName = outboxFolderName; } + public String getAutoExpandFolderName() { + return mAutoExpandFolderName; + } + + public void setAutoExpandFolderName(String autoExpandFolderName) { + mAutoExpandFolderName = autoExpandFolderName; + } + public int getAccountNumber() { return mAccountNumber; } diff --git a/src/com/android/email/Email.java b/src/com/android/email/Email.java index 004ce7f54..a5bf4e727 100644 --- a/src/com/android/email/Email.java +++ b/src/com/android/email/Email.java @@ -93,6 +93,11 @@ public class Email extends Application { * the server refers to as the user's Inbox. Placed here to ease use. */ public static final String INBOX = "INBOX"; + + /** + * For use when displaying that no folder is selected + */ + public static final String FOLDER_NONE = "-NONE-"; /** * Specifies how many messages will be shown in a folder by default. This number is set diff --git a/src/com/android/email/activity/Accounts.java b/src/com/android/email/activity/Accounts.java index a7b20f779..6b4756686 100644 --- a/src/com/android/email/activity/Accounts.java +++ b/src/com/android/email/activity/Accounts.java @@ -358,9 +358,7 @@ public class Accounts extends ListActivity implements OnItemClickListener, OnCli } private void onOpenAccount(Account account) { - //FolderMessageList.actionHandleAccount(this, account); // Dan's way - - FolderMessageList.actionHandleAccount(this, account, Email.INBOX); // Everbody else's way + FolderMessageList.actionHandleAccount(this, account); } public void onClick(View view) { diff --git a/src/com/android/email/activity/ChooseFolder.java b/src/com/android/email/activity/ChooseFolder.java index 2ab7b11ce..b497efed9 100644 --- a/src/com/android/email/activity/ChooseFolder.java +++ b/src/com/android/email/activity/ChooseFolder.java @@ -36,12 +36,16 @@ public class ChooseFolder extends ListActivity private ChooseFolderHandler mHandler = new ChooseFolderHandler(); String heldInbox = null; boolean hideCurrentFolder = true; + boolean showOptionNone = false; + boolean showDisplayableOnly = false; public static final String EXTRA_ACCOUNT = "com.android.email.ChooseFolder_account"; public static final String EXTRA_CUR_FOLDER = "com.android.email.ChooseFolder_curfolder"; public static final String EXTRA_NEW_FOLDER = "com.android.email.ChooseFolder_newfolder"; public static final String EXTRA_MESSAGE_UID = "com.android.email.ChooseFolder_messageuid"; public static final String EXTRA_SHOW_CURRENT = "com.android.email.ChooseFolder_showcurrent"; + public static final String EXTRA_SHOW_FOLDER_NONE = "com.android.email.ChooseFolder_showOptionNone"; + public static final String EXTRA_SHOW_DISPLAYABLE_ONLY = "com.android.email.ChooseFolder_showDisplayableOnly"; @Override public void onCreate(Bundle savedInstanceState) @@ -60,6 +64,12 @@ public class ChooseFolder extends ListActivity if (intent.getStringExtra(EXTRA_SHOW_CURRENT) != null) { hideCurrentFolder = false; } + if (intent.getStringExtra(EXTRA_SHOW_FOLDER_NONE) != null) { + showOptionNone = true; + } + if (intent.getStringExtra(EXTRA_SHOW_DISPLAYABLE_ONLY) != null) { + showDisplayableOnly = true; + } if(mFolder == null) mFolder = ""; @@ -88,9 +98,7 @@ public class ChooseFolder extends ListActivity { destFolderName = heldInbox; } - intent.putExtra(EXTRA_NEW_FOLDER, destFolderName); - intent.putExtra(EXTRA_MESSAGE_UID, mUID); setResult(RESULT_OK, intent); finish(); @@ -105,6 +113,7 @@ public class ChooseFolder extends ListActivity private static final int MSG_PROGRESS = 2; private static final int MSG_DATA_CHANGED = 3; + private static final int MSG_SET_SELECTED_FOLDER = 4; public void handleMessage(android.os.Message msg) { @@ -115,6 +124,12 @@ public class ChooseFolder extends ListActivity break; case MSG_DATA_CHANGED: adapter.notifyDataSetChanged(); + break; + case MSG_SET_SELECTED_FOLDER: + // TODO: I want this to highlight the chosen folder, but this doesn't work. +// getListView().setSelection(msg.arg1); +// getListView().setItemChecked(msg.arg1, true); + break; } } @@ -125,6 +140,14 @@ public class ChooseFolder extends ListActivity msg.arg1 = progress ? 1 : 0; sendMessage(msg); } + + public void setSelectedFolder(int position) + { + android.os.Message msg = new android.os.Message(); + msg.what = MSG_SET_SELECTED_FOLDER; + msg.arg1 = position; + sendMessage(msg); + } public void dataChanged() { @@ -169,7 +192,15 @@ public class ChooseFolder extends ListActivity { return; } - Account.FolderMode aMode = account.getFolderTargetMode(); + Account.FolderMode aMode = Account.FolderMode.ALL; + if (showDisplayableOnly) + { + aMode = account.getFolderDisplayMode(); + } + else + { + aMode = account.getFolderTargetMode(); + } Preferences prefs = Preferences.getPreferences(getApplication().getApplicationContext()); ArrayList localFolders = new ArrayList(); @@ -204,6 +235,11 @@ public class ChooseFolder extends ListActivity } + if (showOptionNone) + { + localFolders.add("-NONE-"); + } + Collections.sort(localFolders, new Comparator() { public int compare(String aName, String bName) { @@ -215,11 +251,22 @@ public class ChooseFolder extends ListActivity { return 1; } + if (Email.FOLDER_NONE.equalsIgnoreCase(aName)) + { + return 1; + } + if (Email.FOLDER_NONE.equalsIgnoreCase(bName)) + { + return -1; + } + return aName.compareToIgnoreCase(bName); } }); adapter.setNotifyOnChange(false); adapter.clear(); + int selectedFolder = -1; + int position = 0; for (String name : localFolders) { if (Email.INBOX.equalsIgnoreCase(name)) { @@ -229,8 +276,18 @@ public class ChooseFolder extends ListActivity else { adapter.add(name); } + + if((name.equals(mFolder) || (Email.INBOX.equalsIgnoreCase(mFolder) && Email.INBOX.equalsIgnoreCase(name)))) { + selectedFolder = position; + } + position++; + } + if (selectedFolder != -1) + { + mHandler.setSelectedFolder(selectedFolder); } mHandler.dataChanged(); + } }; } diff --git a/src/com/android/email/activity/FolderMessageList.java b/src/com/android/email/activity/FolderMessageList.java index 041ff86f7..089fbbaae 100644 --- a/src/com/android/email/activity/FolderMessageList.java +++ b/src/com/android/email/activity/FolderMessageList.java @@ -1,14 +1,9 @@ package com.android.email.activity; -import java.lang.reflect.Array; import java.text.DateFormat; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; -import java.util.EnumMap; -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -46,12 +41,11 @@ import android.widget.ExpandableListView.ExpandableListContextMenuInfo; import com.android.email.Account; import com.android.email.Email; import com.android.email.MessagingController; -import static com.android.email.MessagingController.SORT_TYPE; import com.android.email.MessagingListener; +import com.android.email.Preferences; import com.android.email.R; import com.android.email.Utility; -import com.android.email.Preferences; -import com.android.email.activity.Accounts; +import com.android.email.MessagingController.SORT_TYPE; import com.android.email.activity.FolderMessageList.FolderMessageListAdapter.FolderInfoHolder; import com.android.email.activity.FolderMessageList.FolderMessageListAdapter.MessageInfoHolder; import com.android.email.activity.setup.AccountSettings; @@ -61,13 +55,11 @@ import com.android.email.mail.Flag; import com.android.email.mail.Folder; import com.android.email.mail.Message; import com.android.email.mail.MessagingException; -import com.android.email.mail.Part; import com.android.email.mail.Store; import com.android.email.mail.Message.RecipientType; -import com.android.email.mail.internet.MimeUtility; +import com.android.email.mail.store.LocalStore; import com.android.email.mail.store.LocalStore.LocalFolder; import com.android.email.mail.store.LocalStore.LocalMessage; -import com.android.email.mail.store.LocalStore; /** * FolderMessageList is the primary user interface for the program. This @@ -562,7 +554,12 @@ public class FolderMessageList extends ExpandableListActivity if (savedInstanceState == null) { mInitialFolder = intent.getStringExtra(EXTRA_INITIAL_FOLDER); - } + if (mInitialFolder == null) + { + mInitialFolder = mAccount.getAutoExpandFolderName(); + } + } + /* * Since the color chip is always the same color for a given account we just @@ -669,33 +666,34 @@ public class FolderMessageList extends ExpandableListActivity } @Override - public void onGroupExpand(int groupPosition) - { - super.onGroupExpand(groupPosition); - if (mExpandedGroup != -1) - { - mListView.collapseGroup(mExpandedGroup); - } - mExpandedGroup = groupPosition; + public void onGroupExpand(int groupPosition) + { - if (!mRestoringState) - { - /* - * Scroll the selected item to the top of the screen. - */ - int position = mListView.getFlatListPosition(ExpandableListView - .getPackedPositionForGroup(groupPosition)); - - mListView.setSelectionFromTop(position, 0); - } + Log.i(Email.LOG_TAG, "onGroupExpand(" + groupPosition + "), mRestoringState = " + mRestoringState); + super.onGroupExpand(groupPosition); + if (mExpandedGroup != -1) + { + mListView.collapseGroup(mExpandedGroup); + } + mExpandedGroup = groupPosition; - final FolderInfoHolder folder = (FolderInfoHolder) mAdapter - .getGroup(groupPosition); - if (folder.messages.size() == 0 || folder.needsRefresh) - { - folder.needsRefresh = false; - new Thread(new FolderUpdateWorker(folder.name, false)).start(); - } + if (!mRestoringState) + { + /* + * Scroll the selected item to the top of the screen. + */ + int position = mListView.getFlatListPosition(ExpandableListView + .getPackedPositionForGroup(groupPosition)); + + mListView.setSelectionFromTop(position, 0); + } + + final FolderInfoHolder folder = (FolderInfoHolder) mAdapter.getGroup(groupPosition); + if (folder.messages.size() == 0 || folder.needsRefresh) + { + folder.needsRefresh = false; + new Thread(new FolderUpdateWorker(folder.name, false)).start(); + } } @@ -1456,23 +1454,25 @@ public class FolderMessageList extends ExpandableListActivity } @Override - public void listFoldersFinished(Account account) - { - if (!account.equals(mAccount)) - { - return; + public void listFoldersFinished(Account account) + { + if (!account.equals(mAccount)) + { + return; + } + mHandler.progress(false); + + mHandler.dataChanged(); + + if (mInitialFolder != null && Email.FOLDER_NONE.equals(mInitialFolder) == false) + { + int groupPosition = getFolderPosition(mInitialFolder); + mInitialFolder = null; + if (groupPosition != -1) + { + mHandler.expandGroup(groupPosition); } - mHandler.progress(false); - if (mInitialFolder != null) - { - int groupPosition = getFolderPosition(mInitialFolder); - mInitialFolder = null; - if (groupPosition != -1) - { - mHandler.expandGroup(groupPosition); - } - } - mHandler.dataChanged(); + } } @Override public void accountReset(Account account) diff --git a/src/com/android/email/activity/setup/AccountSettings.java b/src/com/android/email/activity/setup/AccountSettings.java index dc1c933fa..45b058d8c 100644 --- a/src/com/android/email/activity/setup/AccountSettings.java +++ b/src/com/android/email/activity/setup/AccountSettings.java @@ -6,20 +6,28 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.KeyEvent; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; import android.preference.PreferenceActivity; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.CheckBoxPreference; import android.preference.Preference; +import android.preference.PreferenceScreen; import android.preference.RingtonePreference; +import android.preference.Preference.OnPreferenceClickListener; import com.android.email.Account; import com.android.email.Email; import com.android.email.Preferences; import com.android.email.R; +import com.android.email.activity.ChooseFolder; public class AccountSettings extends PreferenceActivity { private static final String EXTRA_ACCOUNT = "account"; + + private static final int SELECT_AUTO_EXPAND_FOLDER = 1; private static final String PREFERENCE_TOP_CATERGORY = "account_settings"; private static final String PREFERENCE_DESCRIPTION = "account_description"; @@ -38,6 +46,7 @@ public class AccountSettings extends PreferenceActivity { private static final String PREFERENCE_SYNC_MODE = "folder_sync_mode"; private static final String PREFERENCE_TARGET_MODE = "folder_target_mode"; private static final String PREFERENCE_DELETE_POLICY = "delete_policy"; + private static final String PREFERENCE_AUTO_EXPAND_FOLDER = "account_setup_auto_expand_folder"; private Account mAccount; @@ -54,6 +63,7 @@ public class AccountSettings extends PreferenceActivity { private ListPreference mSyncMode; private ListPreference mTargetMode; private ListPreference mDeletePolicy; + private Preference mAutoExpandFolder; public static void actionSettings(Context context, Account account) { Intent i = new Intent(context, AccountSettings.class); @@ -196,7 +206,18 @@ public class AccountSettings extends PreferenceActivity { mAccountVibrate = (CheckBoxPreference) findPreference(PREFERENCE_VIBRATE); mAccountVibrate.setChecked(mAccount.isVibrate()); + mAutoExpandFolder = (Preference)findPreference(PREFERENCE_AUTO_EXPAND_FOLDER); + mAutoExpandFolder.setSummary(translateFolder(mAccount.getAutoExpandFolderName())); + + mAutoExpandFolder.setOnPreferenceClickListener( + new Preference.OnPreferenceClickListener() { + public boolean onPreferenceClick(Preference preference) { + onChooseAutoExpandFolder(); + return false; + } + }); + findPreference(PREFERENCE_COMPOSITION).setOnPreferenceClickListener( new Preference.OnPreferenceClickListener() { public boolean onPreferenceClick(Preference preference) { @@ -245,10 +266,22 @@ public class AccountSettings extends PreferenceActivity { SharedPreferences prefs = mAccountRingtone.getPreferenceManager().getSharedPreferences(); mAccount.setRingtone(prefs.getString(PREFERENCE_RINGTONE, null)); mAccount.setHideMessageViewButtons(Account.HideButtons.valueOf(mAccountHideButtons.getValue())); + mAccount.setAutoExpandFolderName(reverseTranslateFolder(mAutoExpandFolder.getSummary().toString())); mAccount.save(Preferences.getPreferences(this)); Email.setServicesEnabled(this); // TODO: refresh folder list here } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (resultCode == RESULT_OK) { + switch (requestCode) { + case SELECT_AUTO_EXPAND_FOLDER: + mAutoExpandFolder.setSummary(translateFolder(data.getStringExtra(ChooseFolder.EXTRA_NEW_FOLDER))); + return; + } + } + } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { @@ -270,4 +303,43 @@ public class AccountSettings extends PreferenceActivity { AccountSetupOutgoing.actionEditOutgoingSettings(this, mAccount); } + public void onChooseAutoExpandFolder() + { + Intent selectIntent = new Intent(this, ChooseFolder.class); + selectIntent.putExtra(ChooseFolder.EXTRA_ACCOUNT, mAccount); + + selectIntent.putExtra(ChooseFolder.EXTRA_CUR_FOLDER, mAutoExpandFolder.getSummary()); + selectIntent.putExtra(ChooseFolder.EXTRA_SHOW_CURRENT, "yes"); + selectIntent.putExtra(ChooseFolder.EXTRA_SHOW_FOLDER_NONE, "yes"); + selectIntent.putExtra(ChooseFolder.EXTRA_SHOW_DISPLAYABLE_ONLY, "yes"); + startActivityForResult(selectIntent, SELECT_AUTO_EXPAND_FOLDER); + + } + + private String translateFolder(String in) + { + + if (Email.INBOX.equalsIgnoreCase(in)) + { + return getString(R.string.special_mailbox_name_inbox); + } + else + { + return in; + } + } + + private String reverseTranslateFolder(String in) + { + + if (getString(R.string.special_mailbox_name_inbox).equals(in)) + { + return Email.INBOX; + } + else + { + return in; + } + } + } diff --git a/src/com/android/email/service/MailService.java b/src/com/android/email/service/MailService.java index 06e70168a..708047bad 100644 --- a/src/com/android/email/service/MailService.java +++ b/src/com/android/email/service/MailService.java @@ -272,12 +272,8 @@ public class MailService extends Service { if (unreadMessageCount > 0) { notif.number = unreadMessageCount; - -// Dan's way - //Intent i = FolderMessageList.actionHandleAccountIntent(context, thisAccount); -// Everybody else's way - Intent i = FolderMessageList.actionHandleAccountIntent(context, thisAccount, Email.INBOX); - + + Intent i = FolderMessageList.actionHandleAccountIntent(context, thisAccount); PendingIntent pi = PendingIntent.getActivity(context, 0, i, 0);