1 /* 2 * Copyright (C) 2012 Google Inc. 3 * Licensed to The Android Open Source Project. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.mail.ui; 19 20 import android.app.AlertDialog; 21 import android.content.Context; 22 import android.content.DialogInterface; 23 import android.content.DialogInterface.OnClickListener; 24 import android.content.DialogInterface.OnDismissListener; 25 import android.os.AsyncTask; 26 import android.view.View; 27 import android.widget.AdapterView; 28 29 import com.android.mail.R; 30 import com.android.mail.providers.Account; 31 import com.android.mail.providers.Conversation; 32 import com.android.mail.providers.Folder; 33 import com.android.mail.providers.UIProvider; 34 import com.android.mail.utils.LogTag; 35 import com.android.mail.utils.LogUtils; 36 37 import java.util.Collection; 38 39 public abstract class FolderSelectionDialog implements OnClickListener, OnDismissListener { 40 protected static final String LOG_TAG = LogTag.getLogTag(); 41 private static boolean sDialogShown; 42 43 protected AlertDialog mDialog; 44 protected final ConversationUpdater mUpdater; 45 protected final SeparatedFolderListAdapter mAdapter; 46 protected final Collection<Conversation> mTarget; 47 protected final boolean mBatch; 48 protected final QueryRunner mRunner; 49 protected final Account mAccount; 50 protected final AlertDialog.Builder mBuilder; 51 protected final Folder mCurrentFolder; 52 53 public static FolderSelectionDialog getInstance(final Context context, final Account account, 54 final ConversationUpdater updater, final Collection<Conversation> target, 55 final boolean isBatch, final Folder currentFolder, final boolean isMoveTo) { 56 if (sDialogShown) { 57 return null; 58 } 59 60 /* 61 * TODO: This method should only be called with isMoveTo=true if this capability is not 62 * present on the account, so we should be able to remove the check here. 63 */ 64 if (isMoveTo || !account.supportsCapability( 65 UIProvider.AccountCapabilities.MULTIPLE_FOLDERS_PER_CONV)) { 66 return new SingleFolderSelectionDialog( 67 context, account, updater, target, isBatch, currentFolder); 68 } else { 69 return new MultiFoldersSelectionDialog( 70 context, account, updater, target, isBatch, currentFolder); 71 } 72 } 73 74 public static void setDialogDismissed() { 75 LogUtils.d(LOG_TAG, "Folder Selection dialog dismissed"); 76 sDialogShown = false; 77 } 78 79 protected abstract void updateAdapterInBackground(Context context); 80 81 protected abstract void onListItemClick(int position); 82 83 protected FolderSelectionDialog(final Context context, final Account account, 84 final ConversationUpdater updater, final Collection<Conversation> target, 85 final boolean isBatch, final Folder currentFolder) { 86 mUpdater = updater; 87 mTarget = target; 88 mBatch = isBatch; 89 90 final AlertDialog.Builder builder = new AlertDialog.Builder(context); 91 builder.setNegativeButton(R.string.cancel, this); 92 mAccount = account; 93 mBuilder = builder; 94 mCurrentFolder = currentFolder; 95 mAdapter = new SeparatedFolderListAdapter(); 96 mRunner = new QueryRunner(context); 97 } 98 99 public void show() { 100 sDialogShown = true; 101 mRunner.execute(); 102 } 103 104 protected void showInternal() { 105 mDialog.show(); 106 mDialog.setOnDismissListener(this); 107 mDialog.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { 108 @Override 109 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 110 onListItemClick(position); 111 } 112 }); 113 } 114 115 @Override 116 public void onDismiss(DialogInterface dialog) { 117 FolderSelectionDialog.setDialogDismissed(); 118 } 119 120 /** 121 * Class to query the Folder list database in the background and update the 122 * adapter with an open cursor. 123 */ 124 private class QueryRunner extends AsyncTask<Void, Void, Void> { 125 private final Context mContext; 126 127 private QueryRunner(final Context context) { 128 mContext = context; 129 } 130 131 @Override 132 protected Void doInBackground(Void... v) { 133 updateAdapterInBackground(mContext); 134 return null; 135 } 136 137 @Override 138 protected void onPostExecute(Void v) { 139 mDialog = mBuilder.create(); 140 showInternal(); 141 } 142 } 143 } 144