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;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.TextView;
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.*;
|
|
|
|
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;
|
|
|
|
Account mAccount;
|
|
|
|
String mUID;
|
|
|
|
ArrayAdapter<String> adapter;
|
|
|
|
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";
|
|
|
|
public static final String EXTRA_NEW_FOLDER = "com.fsck.k9.ChooseFolder_newfolder";
|
|
|
|
public static final String EXTRA_MESSAGE_UID = "com.fsck.k9.ChooseFolder_messageuid";
|
|
|
|
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().setTextFilterEnabled(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);
|
2009-11-21 17:45:39 -05:00
|
|
|
mUID = intent.getStringExtra(EXTRA_MESSAGE_UID);
|
|
|
|
mFolder = intent.getStringExtra(EXTRA_CUR_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 = "";
|
|
|
|
|
|
|
|
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
|
|
|
|
|
|
|
|
setListAdapter(adapter);
|
2009-03-05 02:32:45 -05:00
|
|
|
|
|
|
|
|
|
|
|
MessagingController.getInstance(getApplication()).listFolders(mAccount,
|
2009-11-21 17:45:39 -05:00
|
|
|
false, mListener);
|
|
|
|
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
this.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener()
|
|
|
|
{
|
|
|
|
public void onItemClick(AdapterView adapterview, View view, int i, long l)
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
Intent intent = new Intent();
|
|
|
|
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);
|
|
|
|
intent.putExtra(EXTRA_MESSAGE_UID, mUID);
|
|
|
|
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;
|
|
|
|
|
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:
|
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
break;
|
|
|
|
case MSG_SET_SELECTED_FOLDER:
|
|
|
|
// TODO: I want this to highlight the chosen folder, but this doesn't work.
|
2009-11-21 17:45:39 -05:00
|
|
|
// getListView().setSelection(msg.arg1);
|
|
|
|
// getListView().setItemChecked(msg.arg1, true);
|
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()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
adapter.setNotifyOnChange(false);
|
|
|
|
adapter.clear();
|
|
|
|
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
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
adapter.add(getString(R.string.special_mailbox_name_inbox));
|
|
|
|
heldInbox = name;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
adapter.add(name);
|
|
|
|
}
|
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
if ((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
|
|
|
selectedFolder = position;
|
|
|
|
}
|
|
|
|
position++;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
if (selectedFolder != -1)
|
|
|
|
{
|
2009-11-21 17:45:39 -05:00
|
|
|
mHandler.setSelectedFolder(selectedFolder);
|
|
|
|
}
|
|
|
|
mHandler.dataChanged();
|
|
|
|
|
2009-04-08 14:14:52 -04:00
|
|
|
}
|
2009-11-21 17:45:39 -05:00
|
|
|
};
|
2009-03-05 02:32:45 -05:00
|
|
|
}
|