2009-03-05 02:32:45 -05:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
package com.fsck.k9.activity;
|
2009-03-05 02:32:45 -05:00
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.Window;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.ArrayAdapter;
|
2010-08-08 16:27:39 -04:00
|
|
|
import android.widget.Filter;
|
2009-03-05 02:32:45 -05:00
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.TextView;
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.*;
|
2010-05-19 14:17:06 -04:00
|
|
|
import com.fsck.k9.controller.MessagingController;
|
|
|
|
import com.fsck.k9.controller.MessagingListener;
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.mail.Folder;
|
|
|
|
import com.fsck.k9.mail.MessagingException;
|
2009-12-09 22:16:42 -05:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public class ChooseFolder extends K9ListActivity
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
String mFolder;
|
2010-07-04 19:29:57 -04:00
|
|
|
String mSelectFolder;
|
2009-11-21 17:45:39 -05:00
|
|
|
Account mAccount;
|
2010-05-01 16:06:52 -04:00
|
|
|
MessageReference mMessageReference;
|
2010-08-08 16:27:39 -04:00
|
|
|
ArrayAdapter<String> mAdapter;
|
2009-11-21 17:45:39 -05:00
|
|
|
private ChooseFolderHandler mHandler = new ChooseFolderHandler();
|
|
|
|
String heldInbox = null;
|
|
|
|
boolean hideCurrentFolder = true;
|
|
|
|
boolean showOptionNone = false;
|
|
|
|
boolean showDisplayableOnly = false;
|
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
public static final String EXTRA_ACCOUNT = "com.fsck.k9.ChooseFolder_account";
|
|
|
|
public static final String EXTRA_CUR_FOLDER = "com.fsck.k9.ChooseFolder_curfolder";
|
2010-07-04 19:29:57 -04:00
|
|
|
public static final String EXTRA_SEL_FOLDER = "com.fsck.k9.ChooseFolder_selfolder";
|
2009-12-14 21:50:53 -05:00
|
|
|
public static final String EXTRA_NEW_FOLDER = "com.fsck.k9.ChooseFolder_newfolder";
|
2010-05-01 16:06:52 -04:00
|
|
|
public static final String EXTRA_MESSAGE = "com.fsck.k9.ChooseFolder_message";
|
2009-12-14 21:50:53 -05:00
|
|
|
public static final String EXTRA_SHOW_CURRENT = "com.fsck.k9.ChooseFolder_showcurrent";
|
|
|
|
public static final String EXTRA_SHOW_FOLDER_NONE = "com.fsck.k9.ChooseFolder_showOptionNone";
|
|
|
|
public static final String EXTRA_SHOW_DISPLAYABLE_ONLY = "com.fsck.k9.ChooseFolder_showDisplayableOnly";
|
2009-11-21 17:45:39 -05:00
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void onCreate(Bundle savedInstanceState)
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
|
|
|
|
getListView().setFastScrollEnabled(true);
|
|
|
|
getListView().setItemsCanFocus(false);
|
|
|
|
getListView().setChoiceMode(ListView.CHOICE_MODE_NONE);
|
|
|
|
Intent intent = getIntent();
|
2010-03-03 23:00:30 -05:00
|
|
|
String accountUuid = intent.getStringExtra(EXTRA_ACCOUNT);
|
|
|
|
mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
|
2010-05-01 16:06:52 -04:00
|
|
|
mMessageReference = (MessageReference)intent.getSerializableExtra(EXTRA_MESSAGE);
|
2009-11-21 17:45:39 -05:00
|
|
|
mFolder = intent.getStringExtra(EXTRA_CUR_FOLDER);
|
2010-07-04 19:29:57 -04:00
|
|
|
mSelectFolder = intent.getStringExtra(EXTRA_SEL_FOLDER);
|
2009-11-24 19:40:29 -05:00
|
|
|
if (intent.getStringExtra(EXTRA_SHOW_CURRENT) != null)
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
hideCurrentFolder = false;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
if (intent.getStringExtra(EXTRA_SHOW_FOLDER_NONE) != null)
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
showOptionNone = true;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
if (intent.getStringExtra(EXTRA_SHOW_DISPLAYABLE_ONLY) != null)
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
showDisplayableOnly = true;
|
|
|
|
}
|
|
|
|
if (mFolder == null)
|
|
|
|
mFolder = "";
|
|
|
|
|
2010-08-08 16:27:39 -04:00
|
|
|
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1)
|
|
|
|
{
|
|
|
|
private Filter myFilter = null;
|
2009-11-21 17:45:39 -05:00
|
|
|
|
2010-08-08 16:27:39 -04:00
|
|
|
@Override
|
|
|
|
public Filter getFilter()
|
|
|
|
{
|
|
|
|
if (myFilter == null)
|
|
|
|
{
|
|
|
|
myFilter = new FolderListFilter<String>(this);
|
|
|
|
}
|
|
|
|
return myFilter;
|
|
|
|
}
|
|
|
|
};
|
2009-03-05 02:32:45 -05:00
|
|
|
|
2010-08-08 16:27:39 -04:00
|
|
|
setListAdapter(mAdapter);
|
2009-03-05 02:32:45 -05:00
|
|
|
|
2010-08-08 16:27:39 -04:00
|
|
|
|
|
|
|
MessagingController.getInstance(getApplication()).listFolders(mAccount, false, mListener);
|
2009-11-21 17:45:39 -05:00
|
|
|
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
this.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener()
|
|
|
|
{
|
2010-04-16 08:20:10 -04:00
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
Intent intent = new Intent();
|
2010-07-04 19:29:57 -04:00
|
|
|
intent.putExtra(EXTRA_ACCOUNT, mAccount.getUuid());
|
2009-11-21 17:45:39 -05:00
|
|
|
intent.putExtra(EXTRA_CUR_FOLDER, mFolder);
|
|
|
|
String destFolderName = (String)((TextView)view).getText();
|
2009-11-24 19:40:29 -05:00
|
|
|
if (heldInbox != null && getString(R.string.special_mailbox_name_inbox).equals(destFolderName))
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
destFolderName = heldInbox;
|
|
|
|
}
|
|
|
|
intent.putExtra(EXTRA_NEW_FOLDER, destFolderName);
|
2010-05-01 16:06:52 -04:00
|
|
|
intent.putExtra(EXTRA_MESSAGE, mMessageReference);
|
2009-11-21 17:45:39 -05:00
|
|
|
setResult(RESULT_OK, intent);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
});
|
2009-03-05 02:32:45 -05:00
|
|
|
|
2009-04-08 14:14:52 -04:00
|
|
|
}
|
2009-03-05 02:32:45 -05:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
class ChooseFolderHandler extends Handler
|
|
|
|
{
|
2009-03-05 02:32:45 -05:00
|
|
|
|
2009-11-21 17:45:39 -05:00
|
|
|
private static final int MSG_PROGRESS = 2;
|
2009-03-05 02:32:45 -05:00
|
|
|
|
2009-11-21 17:45:39 -05:00
|
|
|
private static final int MSG_DATA_CHANGED = 3;
|
|
|
|
private static final int MSG_SET_SELECTED_FOLDER = 4;
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void handleMessage(android.os.Message msg)
|
|
|
|
{
|
|
|
|
switch (msg.what)
|
|
|
|
{
|
|
|
|
case MSG_PROGRESS:
|
|
|
|
setProgressBarIndeterminateVisibility(msg.arg1 != 0);
|
|
|
|
break;
|
|
|
|
case MSG_DATA_CHANGED:
|
2010-08-08 16:27:39 -04:00
|
|
|
mAdapter.notifyDataSetChanged();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only enable the text filter after the list has been
|
|
|
|
* populated to avoid possible race conditions because our
|
|
|
|
* FolderListFilter isn't really thread-safe.
|
|
|
|
*/
|
|
|
|
getListView().setTextFilterEnabled(true);
|
2009-11-24 19:40:29 -05:00
|
|
|
break;
|
|
|
|
case MSG_SET_SELECTED_FOLDER:
|
2010-07-04 19:29:57 -04:00
|
|
|
getListView().setSelection(msg.arg1);
|
2009-11-24 19:40:29 -05:00
|
|
|
break;
|
2009-11-21 17:45:39 -05:00
|
|
|
}
|
2009-03-05 02:32:45 -05:00
|
|
|
}
|
2009-11-21 17:45:39 -05:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public void progress(boolean progress)
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
android.os.Message msg = new android.os.Message();
|
|
|
|
msg.what = MSG_PROGRESS;
|
|
|
|
msg.arg1 = progress ? 1 : 0;
|
|
|
|
sendMessage(msg);
|
2009-03-05 02:32:45 -05:00
|
|
|
}
|
2009-11-21 17:45:39 -05:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public void setSelectedFolder(int position)
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
android.os.Message msg = new android.os.Message();
|
|
|
|
msg.what = MSG_SET_SELECTED_FOLDER;
|
|
|
|
msg.arg1 = position;
|
|
|
|
sendMessage(msg);
|
2009-03-05 02:32:45 -05:00
|
|
|
}
|
2009-11-21 17:45:39 -05:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public void dataChanged()
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
sendEmptyMessage(MSG_DATA_CHANGED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
private MessagingListener mListener = new MessagingListener()
|
|
|
|
{
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void listFoldersStarted(Account account)
|
|
|
|
{
|
|
|
|
if (!account.equals(mAccount))
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mHandler.progress(true);
|
2009-03-05 02:32:45 -05:00
|
|
|
}
|
2009-11-21 17:45:39 -05:00
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void listFoldersFailed(Account account, String message)
|
|
|
|
{
|
|
|
|
if (!account.equals(mAccount))
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mHandler.progress(false);
|
2009-03-05 02:32:45 -05:00
|
|
|
}
|
2009-11-21 17:45:39 -05:00
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void listFoldersFinished(Account account)
|
|
|
|
{
|
|
|
|
if (!account.equals(mAccount))
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mHandler.progress(false);
|
2009-03-05 02:32:45 -05:00
|
|
|
}
|
2009-11-21 17:45:39 -05:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void listFolders(Account account, Folder[] folders)
|
|
|
|
{
|
|
|
|
if (!account.equals(mAccount))
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Account.FolderMode aMode = Account.FolderMode.ALL;
|
2009-11-24 19:40:29 -05:00
|
|
|
if (showDisplayableOnly)
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
aMode = account.getFolderDisplayMode();
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
aMode = account.getFolderTargetMode();
|
|
|
|
}
|
|
|
|
Preferences prefs = Preferences.getPreferences(getApplication().getApplicationContext());
|
|
|
|
ArrayList<String> localFolders = new ArrayList<String>();
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
for (Folder folder : folders)
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
String name = folder.getName();
|
|
|
|
|
|
|
|
// Inbox needs to be compared case-insensitively
|
2009-12-14 21:50:53 -05:00
|
|
|
if (hideCurrentFolder && (name.equals(mFolder) || (K9.INBOX.equalsIgnoreCase(mFolder) && K9.INBOX.equalsIgnoreCase(name))))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
continue;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
folder.refresh(prefs);
|
|
|
|
Folder.FolderClass fMode = folder.getDisplayClass();
|
|
|
|
|
|
|
|
if ((aMode == Account.FolderMode.FIRST_CLASS && fMode != Folder.FolderClass.FIRST_CLASS)
|
|
|
|
|| (aMode == Account.FolderMode.FIRST_AND_SECOND_CLASS &&
|
|
|
|
fMode != Folder.FolderClass.FIRST_CLASS &&
|
|
|
|
fMode != Folder.FolderClass.SECOND_CLASS)
|
2009-11-24 19:40:29 -05:00
|
|
|
|| (aMode == Account.FolderMode.NOT_SECOND_CLASS && fMode == Folder.FolderClass.SECOND_CLASS))
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
continue;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
catch (MessagingException me)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Couldn't get prefs to check for displayability of folder " + folder.getName(), me);
|
2009-11-21 17:45:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
localFolders.add(folder.getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
if (showOptionNone)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
localFolders.add(K9.FOLDER_NONE);
|
2009-11-21 17:45:39 -05:00
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
Collections.sort(localFolders, new Comparator<String>()
|
|
|
|
{
|
|
|
|
public int compare(String aName, String bName)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.FOLDER_NONE.equalsIgnoreCase(aName))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.FOLDER_NONE.equalsIgnoreCase(bName))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
return 1;
|
|
|
|
}
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.INBOX.equalsIgnoreCase(aName))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.INBOX.equalsIgnoreCase(bName))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aName.compareToIgnoreCase(bName);
|
|
|
|
}
|
|
|
|
});
|
2010-08-08 16:27:39 -04:00
|
|
|
mAdapter.setNotifyOnChange(false);
|
|
|
|
mAdapter.clear();
|
2009-11-21 17:45:39 -05:00
|
|
|
int selectedFolder = -1;
|
|
|
|
int position = 0;
|
2009-11-24 19:40:29 -05:00
|
|
|
for (String name : localFolders)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.INBOX.equalsIgnoreCase(name))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2010-08-08 16:27:39 -04:00
|
|
|
mAdapter.add(getString(R.string.special_mailbox_name_inbox));
|
2009-11-21 17:45:39 -05:00
|
|
|
heldInbox = name;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-08-08 16:27:39 -04:00
|
|
|
mAdapter.add(name);
|
2009-11-21 17:45:39 -05:00
|
|
|
}
|
|
|
|
|
2010-07-04 19:29:57 -04:00
|
|
|
if (mSelectFolder != null)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Never select EXTRA_CUR_FOLDER (mFolder) if EXTRA_SEL_FOLDER
|
|
|
|
* (mSelectedFolder) was provided.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (name.equals(mSelectFolder))
|
|
|
|
{
|
|
|
|
selectedFolder = position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (name.equals(mFolder) ||
|
2010-07-06 06:29:26 -04:00
|
|
|
(K9.INBOX.equalsIgnoreCase(mFolder) && K9.INBOX.equalsIgnoreCase(name)))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
selectedFolder = position;
|
|
|
|
}
|
|
|
|
position++;
|
|
|
|
}
|
2010-07-04 19:29:57 -04:00
|
|
|
|
|
|
|
mHandler.dataChanged();
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
if (selectedFolder != -1)
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
mHandler.setSelectedFolder(selectedFolder);
|
|
|
|
}
|
2009-04-08 14:14:52 -04:00
|
|
|
}
|
2009-11-21 17:45:39 -05:00
|
|
|
};
|
2009-03-05 02:32:45 -05:00
|
|
|
}
|