From 266064bcc7c6ed7318a284d59bb83d53edbf1921 Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Sun, 9 Sep 2012 19:07:53 -0700 Subject: [PATCH] Add "System Folders" settings * Add this category and preferences for setting the trash and sent items folders Bug: 7100092 Change-Id: I964e02e5e319b3f04b030d36ed7dca30ed046362 --- res/values/strings.xml | 8 ++++ res/xml/account_settings_preferences.xml | 22 +++++++++ .../setup/AccountSettingsFragment.java | 34 ++++++++++++++ .../email/provider/FolderPickerActivity.java | 18 ++++++-- .../email/provider/FolderPickerCallback.java | 1 + ...ionDialog.java => FolderPickerDialog.java} | 18 +++++--- .../provider/FolderPickerSelectorAdapter.java | 46 +++++++++++++++++++ 7 files changed, 137 insertions(+), 10 deletions(-) rename src/com/android/email/provider/{FolderSelectionDialog.java => FolderPickerDialog.java} (91%) create mode 100644 src/com/android/email/provider/FolderPickerSelectorAdapter.java diff --git a/res/values/strings.xml b/res/values/strings.xml index 59bfe9bb9..94d5cf30d 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -988,6 +988,14 @@ as %s. Security policies + System folders + Trash folder + Select your server\'s trash folder + Select your server\'s trash folder + Sent items folder + Select your server\'s sent items folder + Select your server\'s sent items folder + Edit quick response diff --git a/res/xml/account_settings_preferences.xml b/res/xml/account_settings_preferences.xml index 03cb7c61a..23726883a 100755 --- a/res/xml/account_settings_preferences.xml +++ b/res/xml/account_settings_preferences.xml @@ -120,6 +120,28 @@ + + + + + + + + diff --git a/src/com/android/email/activity/setup/AccountSettingsFragment.java b/src/com/android/email/activity/setup/AccountSettingsFragment.java index bc07cc82d..7b1b1ca30 100644 --- a/src/com/android/email/activity/setup/AccountSettingsFragment.java +++ b/src/com/android/email/activity/setup/AccountSettingsFragment.java @@ -18,10 +18,14 @@ package com.android.email.activity.setup; import android.app.Activity; import android.content.ContentResolver; +import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Resources; +import android.database.Cursor; +import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.Vibrator; @@ -38,15 +42,19 @@ import android.util.Log; import com.android.email.R; import com.android.email.SecurityPolicy; +import com.android.email.provider.FolderPickerActivity; import com.android.email.service.EmailServiceUtils; import com.android.email.service.EmailServiceUtils.EmailServiceInfo; import com.android.email2.ui.MailActivityEmail; import com.android.emailcommon.Logging; import com.android.emailcommon.provider.Account; import com.android.emailcommon.provider.EmailContent; +import com.android.emailcommon.provider.EmailContent.MailboxColumns; import com.android.emailcommon.provider.HostAuth; +import com.android.emailcommon.provider.Mailbox; import com.android.emailcommon.provider.Policy; import com.android.emailcommon.utility.Utility; +import com.android.mail.providers.UIProvider; import java.util.ArrayList; @@ -89,6 +97,10 @@ public class AccountSettingsFragment extends EmailPreferenceFragment private static final String PREFERENCE_SYNC_CALENDAR = "account_sync_calendar"; private static final String PREFERENCE_SYNC_EMAIL = "account_sync_email"; + private static final String PREFERENCE_SYSTEM_FOLDERS = "system_folders"; + private static final String PREFERENCE_SYSTEM_FOLDERS_TRASH = "system_folders_trash"; + private static final String PREFERENCE_SYSTEM_FOLDERS_SENT = "system_folders_sent"; + // These strings must match account_settings_vibrate_when_* strings in strings.xml private static final String PREFERENCE_VALUE_VIBRATE_WHEN_ALWAYS = "always"; private static final String PREFERENCE_VALUE_VIBRATE_WHEN_SILENT = "silent"; @@ -528,6 +540,28 @@ public class AccountSettingsFragment extends EmailPreferenceFragment dataUsageCategory.addPreference(mSyncWindow); } + PreferenceCategory folderPrefs = + (PreferenceCategory) findPreference(PREFERENCE_SYSTEM_FOLDERS); + if (info.requiresSetup) { + Preference trashPreference = + (Preference) findPreference(PREFERENCE_SYSTEM_FOLDERS_TRASH); + Intent i = new Intent(mContext, FolderPickerActivity.class); + Uri uri = EmailContent.CONTENT_URI.buildUpon().appendQueryParameter( + "account", Long.toString(mAccount.mId)).build(); + i.setData(uri); + i.putExtra(FolderPickerActivity.MAILBOX_TYPE_EXTRA, Mailbox.TYPE_TRASH); + trashPreference.setIntent(i); + + Preference sentPreference = + (Preference) findPreference(PREFERENCE_SYSTEM_FOLDERS_SENT); + i = new Intent(mContext, FolderPickerActivity.class); + i.setData(uri); + i.putExtra(FolderPickerActivity.MAILBOX_TYPE_EXTRA, Mailbox.TYPE_SENT); + sentPreference.setIntent(i); + } else { + getPreferenceScreen().removePreference(folderPrefs); + } + mAccountBackgroundAttachments = (CheckBoxPreference) findPreference(PREFERENCE_BACKGROUND_ATTACHMENTS); if (!info.offerAttachmentPreload) { diff --git a/src/com/android/email/provider/FolderPickerActivity.java b/src/com/android/email/provider/FolderPickerActivity.java index 42e2f37a2..cebdae67c 100644 --- a/src/com/android/email/provider/FolderPickerActivity.java +++ b/src/com/android/email/provider/FolderPickerActivity.java @@ -39,10 +39,13 @@ import com.android.mail.providers.Folder; public class FolderPickerActivity extends Activity implements FolderPickerCallback { private static final String TAG = "FolderPickerActivity"; + public static final String MAILBOX_TYPE_EXTRA = "mailbox_type"; + private long mAccountId; private int mMailboxType; private AccountObserver mAccountObserver; private String mAccountName; + private boolean mInSetup = true; public void onCreate(Bundle bundle) { super.onCreate(bundle); @@ -66,10 +69,12 @@ public class FolderPickerActivity extends Activity implements FolderPickerCallba finish(); return; } - mMailboxType = Mailbox.TYPE_TRASH; + // 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); long trashMailboxId = Mailbox.findMailboxOfType(this, mAccountId, Mailbox.TYPE_TRASH); - // If we already have a trash mailbox, we're done (race?) - if (trashMailboxId != Mailbox.NO_MAILBOX) { + // If we already have a trash mailbox, we're done (if in setup; a race?) + if (trashMailboxId != Mailbox.NO_MAILBOX && mInSetup) { Log.w(TAG, "Trash folder already exists"); finish(); return; @@ -165,7 +170,8 @@ public class FolderPickerActivity extends Activity implements FolderPickerCallba private void startPicker(Uri uri, int headerId) { String header = getString(headerId, mAccountName); - FolderSelectionDialog dialog = new FolderSelectionDialog(this, uri, this, header); + FolderPickerDialog dialog = + new FolderPickerDialog(this, uri, this, header, !mInSetup); dialog.show(); } @@ -201,4 +207,8 @@ public class FolderPickerActivity extends Activity implements FolderPickerCallba } finish(); } + + public void cancel() { + finish(); + } } diff --git a/src/com/android/email/provider/FolderPickerCallback.java b/src/com/android/email/provider/FolderPickerCallback.java index 52f82c66b..a1e127641 100644 --- a/src/com/android/email/provider/FolderPickerCallback.java +++ b/src/com/android/email/provider/FolderPickerCallback.java @@ -21,4 +21,5 @@ import com.android.mail.providers.Folder; public interface FolderPickerCallback { public void select(Folder folder); + public void cancel(); } diff --git a/src/com/android/email/provider/FolderSelectionDialog.java b/src/com/android/email/provider/FolderPickerDialog.java similarity index 91% rename from src/com/android/email/provider/FolderSelectionDialog.java rename to src/com/android/email/provider/FolderPickerDialog.java index 37be18699..bf9a4e2e6 100644 --- a/src/com/android/email/provider/FolderSelectionDialog.java +++ b/src/com/android/email/provider/FolderPickerDialog.java @@ -34,28 +34,29 @@ import com.android.mail.providers.Folder; import com.android.mail.providers.UIProvider; import com.android.mail.ui.FolderSelectorAdapter; import com.android.mail.ui.FolderSelectorAdapter.FolderRow; -import com.android.mail.ui.HierarchicalFolderSelectorAdapter; import com.android.mail.ui.SeparatedFolderListAdapter; import java.util.HashMap; import java.util.HashSet; import java.util.Map.Entry; -public class FolderSelectionDialog implements OnClickListener, OnMultiChoiceClickListener { +public class FolderPickerDialog implements OnClickListener, OnMultiChoiceClickListener, + OnCancelListener { private final AlertDialog mDialog; private final HashMap mCheckedState; private final SeparatedFolderListAdapter mAdapter; private final FolderPickerCallback mCallback; - public FolderSelectionDialog(final Context context, Uri uri, - FolderPickerCallback callback, String header) { + public FolderPickerDialog(final Context context, Uri uri, + FolderPickerCallback callback, String header, boolean cancelable) { mCallback = callback; // Mapping of a folder's uri to its checked state mCheckedState = new HashMap(); AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(header); builder.setPositiveButton(R.string.ok, this); - builder.setCancelable(false); + builder.setCancelable(cancelable); + builder.setOnCancelListener(this); // TODO: Do this on a background thread final Cursor foldersCursor = context.getContentResolver().query( uri, UIProvider.FOLDERS_PROJECTION, null, null, null); @@ -63,7 +64,7 @@ public class FolderSelectionDialog implements OnClickListener, OnMultiChoiceClic mAdapter = new SeparatedFolderListAdapter(context); String[] headers = context.getResources() .getStringArray(R.array.moveto_folder_sections); - mAdapter.addSection(new HierarchicalFolderSelectorAdapter(context, + mAdapter.addSection(new FolderPickerSelectorAdapter(context, foldersCursor, new HashSet(), true, headers[2])); builder.setAdapter(mAdapter, this); } finally { @@ -151,4 +152,9 @@ public class FolderSelectionDialog implements OnClickListener, OnMultiChoiceClic mCheckedState.put(row.getFolder(), isChecked); mDialog.getListView().setItemChecked(which, false); } + + @Override + public void onCancel(DialogInterface dialog) { + mCallback.cancel(); + } } diff --git a/src/com/android/email/provider/FolderPickerSelectorAdapter.java b/src/com/android/email/provider/FolderPickerSelectorAdapter.java new file mode 100644 index 000000000..aa91670b5 --- /dev/null +++ b/src/com/android/email/provider/FolderPickerSelectorAdapter.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * 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.content.Context; +import android.database.Cursor; + +import com.android.mail.providers.Folder; +import com.android.mail.providers.UIProvider.FolderCapabilities; +import com.android.mail.providers.UIProvider.FolderType; +import com.android.mail.ui.HierarchicalFolderSelectorAdapter; + +import java.util.Set; + +public class FolderPickerSelectorAdapter extends HierarchicalFolderSelectorAdapter { + + public FolderPickerSelectorAdapter(Context context, Cursor folders, + Set initiallySelected, boolean single, String header) { + super(context, folders, initiallySelected, single, header); + } + + /** + * Return whether the supplied folder meets the requirements to be displayed + * in the folder list. + */ + protected boolean meetsRequirements(Folder folder) { + // We only want to show the non-Trash folders that can accept moved messages + return folder.supportsCapability(FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES) || + folder.type == FolderType.TRASH; + } +}