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 18package com.android.mail.ui; 19 20import android.app.AlertDialog; 21import android.content.Context; 22import android.content.DialogInterface; 23import android.content.DialogInterface.OnClickListener; 24import android.content.DialogInterface.OnDismissListener; 25import android.os.AsyncTask; 26import android.view.View; 27import android.widget.AdapterView; 28 29import com.android.mail.R; 30import com.android.mail.providers.Account; 31import com.android.mail.providers.Conversation; 32import com.android.mail.providers.Folder; 33import com.android.mail.providers.UIProvider; 34import com.android.mail.utils.LogTag; 35import com.android.mail.utils.LogUtils; 36 37import java.util.Collection; 38 39public 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 // TODO: use a loader instead 80 @Deprecated 81 protected abstract void updateAdapterInBackground(Context context); 82 83 protected abstract void onListItemClick(int position); 84 85 protected FolderSelectionDialog(final Context context, final Account account, 86 final ConversationUpdater updater, final Collection<Conversation> target, 87 final boolean isBatch, final Folder currentFolder) { 88 mUpdater = updater; 89 mTarget = target; 90 mBatch = isBatch; 91 92 final AlertDialog.Builder builder = new AlertDialog.Builder(context); 93 builder.setNegativeButton(R.string.cancel, this); 94 mAccount = account; 95 mBuilder = builder; 96 mCurrentFolder = currentFolder; 97 mAdapter = new SeparatedFolderListAdapter(); 98 mRunner = new QueryRunner(context); 99 } 100 101 public void show() { 102 sDialogShown = true; 103 // TODO: use a loader instead 104 mRunner.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 105 } 106 107 protected void showInternal() { 108 mDialog.show(); 109 mDialog.setOnDismissListener(this); 110 mDialog.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { 111 @Override 112 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 113 onListItemClick(position); 114 } 115 }); 116 } 117 118 @Override 119 public void onDismiss(DialogInterface dialog) { 120 FolderSelectionDialog.setDialogDismissed(); 121 } 122 123 /** 124 * Class to query the Folder list database in the background and update the 125 * adapter with an open cursor. 126 */ 127 // TODO: use a loader instead 128 @Deprecated 129 private class QueryRunner extends AsyncTask<Void, Void, Void> { 130 private final Context mContext; 131 132 private QueryRunner(final Context context) { 133 mContext = context; 134 } 135 136 @Override 137 protected Void doInBackground(Void... v) { 138 updateAdapterInBackground(mContext); 139 return null; 140 } 141 142 @Override 143 protected void onPostExecute(Void v) { 144 mDialog = mBuilder.create(); 145 showInternal(); 146 } 147 } 148} 149