2012-06-29 16:42:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Google Inc.
|
|
|
|
* Licensed to The Android Open Source Project.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email.provider;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2012-09-06 17:19:34 +00:00
|
|
|
import android.app.ProgressDialog;
|
2012-06-29 16:42:05 +00:00
|
|
|
import android.content.ContentUris;
|
|
|
|
import android.content.ContentValues;
|
2012-09-06 17:19:34 +00:00
|
|
|
import android.content.Context;
|
2012-06-29 16:42:05 +00:00
|
|
|
import android.content.Intent;
|
2012-09-06 17:19:34 +00:00
|
|
|
import android.database.ContentObserver;
|
|
|
|
import android.net.Uri;
|
2012-06-29 16:42:05 +00:00
|
|
|
import android.os.Bundle;
|
2012-09-06 17:19:34 +00:00
|
|
|
import android.os.Handler;
|
2012-06-29 16:42:05 +00:00
|
|
|
|
2012-09-06 17:19:34 +00:00
|
|
|
import com.android.email.R;
|
|
|
|
import com.android.emailcommon.provider.Account;
|
|
|
|
import com.android.emailcommon.provider.EmailContent;
|
2012-06-29 16:42:05 +00:00
|
|
|
import com.android.emailcommon.provider.Mailbox;
|
2012-09-06 17:19:34 +00:00
|
|
|
import com.android.emailcommon.provider.EmailContent.AccountColumns;
|
2012-06-29 16:42:05 +00:00
|
|
|
import com.android.emailcommon.provider.EmailContent.MailboxColumns;
|
|
|
|
import com.android.mail.providers.Folder;
|
2013-05-26 04:32:32 +00:00
|
|
|
import com.android.mail.utils.LogUtils;
|
2012-06-29 16:42:05 +00:00
|
|
|
|
|
|
|
public class FolderPickerActivity extends Activity implements FolderPickerCallback {
|
2012-09-06 17:19:34 +00:00
|
|
|
private static final String TAG = "FolderPickerActivity";
|
2012-09-10 02:07:53 +00:00
|
|
|
public static final String MAILBOX_TYPE_EXTRA = "mailbox_type";
|
|
|
|
|
2012-06-29 16:42:05 +00:00
|
|
|
private long mAccountId;
|
|
|
|
private int mMailboxType;
|
2012-09-06 17:19:34 +00:00
|
|
|
private AccountObserver mAccountObserver;
|
|
|
|
private String mAccountName;
|
2012-09-10 02:07:53 +00:00
|
|
|
private boolean mInSetup = true;
|
2012-06-29 16:42:05 +00:00
|
|
|
|
2013-05-26 04:32:32 +00:00
|
|
|
@Override
|
2012-06-29 16:42:05 +00:00
|
|
|
public void onCreate(Bundle bundle) {
|
|
|
|
super.onCreate(bundle);
|
|
|
|
Intent i = getIntent();
|
2012-09-06 17:19:34 +00:00
|
|
|
Uri uri = i.getData();
|
|
|
|
int headerId;
|
2013-09-20 21:30:57 +00:00
|
|
|
final com.android.mail.providers.Account uiAccount;
|
2012-09-06 17:19:34 +00:00
|
|
|
// If we've gotten a Uri, then this is a call from the UI in response to setupIntentUri
|
|
|
|
// in an account (meaning the account requires setup)
|
|
|
|
if (uri != null) {
|
|
|
|
String id = uri.getQueryParameter("account");
|
|
|
|
if (id == null) {
|
2013-05-26 04:32:32 +00:00
|
|
|
LogUtils.w(TAG, "No account # in Uri?");
|
2012-09-06 17:19:34 +00:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
mAccountId = Long.parseLong(id);
|
|
|
|
} catch (NumberFormatException e) {
|
2013-05-26 04:32:32 +00:00
|
|
|
LogUtils.w(TAG, "Invalid account # in Uri?");
|
2012-09-06 17:19:34 +00:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2012-09-10 02:07:53 +00:00
|
|
|
// We act a bit differently if we're coming to set up the trash after account creation
|
|
|
|
mInSetup = !i.hasExtra(MAILBOX_TYPE_EXTRA);
|
|
|
|
mMailboxType = i.getIntExtra(MAILBOX_TYPE_EXTRA, Mailbox.TYPE_TRASH);
|
2012-09-07 21:14:19 +00:00
|
|
|
long trashMailboxId = Mailbox.findMailboxOfType(this, mAccountId, Mailbox.TYPE_TRASH);
|
2012-09-10 02:07:53 +00:00
|
|
|
// If we already have a trash mailbox, we're done (if in setup; a race?)
|
|
|
|
if (trashMailboxId != Mailbox.NO_MAILBOX && mInSetup) {
|
2013-05-26 04:32:32 +00:00
|
|
|
LogUtils.w(TAG, "Trash folder already exists");
|
2012-09-07 21:14:19 +00:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2012-09-06 17:19:34 +00:00
|
|
|
Account account = Account.restoreAccountWithId(this, mAccountId);
|
|
|
|
if (account == null) {
|
2013-05-26 04:32:32 +00:00
|
|
|
LogUtils.w(TAG, "No account?");
|
2012-09-06 17:19:34 +00:00
|
|
|
finish();
|
|
|
|
} else {
|
|
|
|
mAccountName = account.mDisplayName;
|
|
|
|
// Two possibilities here; either we have our folder list, or we don't
|
|
|
|
if ((account.mFlags & Account.FLAGS_INITIAL_FOLDER_LIST_LOADED) != 0) {
|
|
|
|
// If we've got them, just start up the picker dialog
|
|
|
|
startPickerForAccount();
|
|
|
|
} else {
|
|
|
|
// Otherwise, wait for the folders to show up
|
|
|
|
waitForFolders();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// In this case, we're coming from Settings
|
|
|
|
uiAccount = i.getParcelableExtra(EmailProvider.PICKER_UI_ACCOUNT);
|
2014-03-01 00:44:39 +00:00
|
|
|
mAccountName = uiAccount.getDisplayName();
|
2012-09-06 17:19:34 +00:00
|
|
|
mAccountId = Long.parseLong(uiAccount.uri.getLastPathSegment());
|
|
|
|
mMailboxType = i.getIntExtra(EmailProvider.PICKER_MAILBOX_TYPE, -1);
|
|
|
|
headerId = i.getIntExtra(EmailProvider.PICKER_HEADER_ID, 0);
|
|
|
|
if (headerId == 0) {
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
startPicker(uiAccount.folderListUri, headerId);
|
2012-07-18 19:52:40 +00:00
|
|
|
}
|
2012-09-06 17:19:34 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 04:32:32 +00:00
|
|
|
@Override
|
2012-09-06 17:19:34 +00:00
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
// Clean up
|
|
|
|
if (mAccountObserver != null) {
|
|
|
|
getContentResolver().unregisterContentObserver(mAccountObserver);
|
|
|
|
mAccountObserver = null;
|
|
|
|
}
|
|
|
|
if (mWaitingForFoldersDialog != null) {
|
|
|
|
mWaitingForFoldersDialog.dismiss();
|
|
|
|
mWaitingForFoldersDialog = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class AccountObserver extends ContentObserver {
|
|
|
|
private final Context mContext;
|
|
|
|
|
|
|
|
public AccountObserver(Context context, Handler handler) {
|
|
|
|
super(handler);
|
|
|
|
mContext = context;
|
|
|
|
}
|
|
|
|
|
2013-05-26 04:32:32 +00:00
|
|
|
@Override
|
2012-09-06 17:19:34 +00:00
|
|
|
public void onChange(boolean selfChange) {
|
|
|
|
Account account = Account.restoreAccountWithId(mContext, mAccountId);
|
|
|
|
// All we care about is whether the folder list is now loaded
|
2012-09-07 21:14:19 +00:00
|
|
|
if ((account.mFlags & Account.FLAGS_INITIAL_FOLDER_LIST_LOADED) != 0 &&
|
|
|
|
(mAccountObserver != null)) {
|
|
|
|
mContext.getContentResolver().unregisterContentObserver(mAccountObserver);
|
2012-09-06 17:19:34 +00:00
|
|
|
mAccountObserver = null;
|
|
|
|
// Bring down the ProgressDialog and show the picker
|
|
|
|
if (mWaitingForFoldersDialog != null) {
|
|
|
|
mWaitingForFoldersDialog.dismiss();
|
|
|
|
mWaitingForFoldersDialog = null;
|
|
|
|
}
|
|
|
|
startPickerForAccount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ProgressDialog mWaitingForFoldersDialog;
|
|
|
|
|
|
|
|
private void waitForFolders() {
|
|
|
|
/// Show "Waiting for folders..." dialog
|
|
|
|
mWaitingForFoldersDialog = new ProgressDialog(this);
|
|
|
|
mWaitingForFoldersDialog.setIndeterminate(true);
|
|
|
|
mWaitingForFoldersDialog.setMessage(getString(R.string.account_waiting_for_folders_msg));
|
|
|
|
mWaitingForFoldersDialog.show();
|
|
|
|
|
|
|
|
// Listen for account changes
|
|
|
|
mAccountObserver = new AccountObserver(this, new Handler());
|
|
|
|
Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, mAccountId);
|
|
|
|
getContentResolver().registerContentObserver(uri, false, mAccountObserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void startPickerForAccount() {
|
|
|
|
int headerId = R.string.trash_folder_selection_title;
|
2013-09-20 21:30:57 +00:00
|
|
|
Uri uri = Uri.parse("content://" + EmailContent.AUTHORITY + "/uifullfolders/" + mAccountId);
|
2012-09-06 17:19:34 +00:00
|
|
|
startPicker(uri, headerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void startPicker(Uri uri, int headerId) {
|
|
|
|
String header = getString(headerId, mAccountName);
|
2012-09-10 02:07:53 +00:00
|
|
|
FolderPickerDialog dialog =
|
|
|
|
new FolderPickerDialog(this, uri, this, header, !mInSetup);
|
2012-09-06 17:19:34 +00:00
|
|
|
dialog.show();
|
2012-06-29 16:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void select(Folder folder) {
|
2013-07-11 20:24:59 +00:00
|
|
|
String folderId = folder.folderUri.fullUri.getLastPathSegment();
|
2012-06-29 16:42:05 +00:00
|
|
|
Long id = Long.parseLong(folderId);
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
|
|
|
// If we already have a mailbox of this type, change it back to generic mail type
|
|
|
|
Mailbox ofType = Mailbox.restoreMailboxOfType(this, mAccountId, mMailboxType);
|
|
|
|
if (ofType != null) {
|
|
|
|
values.put(MailboxColumns.TYPE, Mailbox.TYPE_MAIL);
|
|
|
|
getContentResolver().update(
|
|
|
|
ContentUris.withAppendedId(Mailbox.CONTENT_URI, ofType.mId), values,
|
|
|
|
null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change this mailbox to be of the desired type
|
|
|
|
Mailbox mailbox = Mailbox.restoreMailboxWithId(this, id);
|
|
|
|
if (mailbox != null) {
|
|
|
|
values.put(MailboxColumns.TYPE, mMailboxType);
|
|
|
|
getContentResolver().update(
|
|
|
|
ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailbox.mId), values,
|
|
|
|
null, null);
|
2012-09-06 17:19:34 +00:00
|
|
|
values.clear();
|
|
|
|
// Touch the account so that UI won't bring up this picker again
|
|
|
|
Account account = Account.restoreAccountWithId(this, mAccountId);
|
|
|
|
values.put(AccountColumns.FLAGS, account.mFlags);
|
|
|
|
getContentResolver().update(
|
|
|
|
ContentUris.withAppendedId(Account.CONTENT_URI, account.mId), values,
|
|
|
|
null, null);
|
2012-06-29 16:42:05 +00:00
|
|
|
}
|
|
|
|
finish();
|
|
|
|
}
|
2012-09-10 02:07:53 +00:00
|
|
|
|
2013-05-26 04:32:32 +00:00
|
|
|
@Override
|
2012-09-10 02:07:53 +00:00
|
|
|
public void cancel() {
|
|
|
|
finish();
|
|
|
|
}
|
2012-06-29 16:42:05 +00:00
|
|
|
}
|