blob: 920edb6541f742f9113a6c2c6cdb30877785e533 [file] [log] [blame]
Mark Wei9eb1c9a2012-10-01 12:54:50 -07001/*
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
Mark Wei8f98ac02012-10-01 17:05:08 -070020import android.app.AlertDialog;
Tony Mantler2a4be242013-12-11 15:30:53 -080021import android.app.Dialog;
22import android.app.DialogFragment;
Mark Wei8f98ac02012-10-01 17:05:08 -070023import android.content.DialogInterface.OnClickListener;
Tony Mantler2a4be242013-12-11 15:30:53 -080024import android.os.Bundle;
juwei7979c162018-05-25 12:01:24 +080025import android.os.Parcelable;
Mark Wei8f98ac02012-10-01 17:05:08 -070026import android.view.View;
27import android.widget.AdapterView;
Mark Wei9eb1c9a2012-10-01 12:54:50 -070028
Mark Wei8f98ac02012-10-01 17:05:08 -070029import 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;
Mark Wei9eb1c9a2012-10-01 12:54:50 -070035
Tony Mantler2a4be242013-12-11 15:30:53 -080036import java.util.Arrays;
Mark Wei8f98ac02012-10-01 17:05:08 -070037import java.util.Collection;
38
Tony Mantler2a4be242013-12-11 15:30:53 -080039public abstract class FolderSelectionDialog extends DialogFragment implements OnClickListener {
Mark Wei8f98ac02012-10-01 17:05:08 -070040 protected static final String LOG_TAG = LogTag.getLogTag();
Mark Wei9eb1c9a2012-10-01 12:54:50 -070041
Tony Mantler2a4be242013-12-11 15:30:53 -080042 private static final String ARG_FOLDER_TAG = "folder";
43 private static final String ARG_ACCOUNT_TAG = "account";
44 private static final String ARG_BATCH_TAG = "batch";
45 private static final String ARG_TARGET_TAG = "target";
Mark Wei8f98ac02012-10-01 17:05:08 -070046
Tony Mantler2a4be242013-12-11 15:30:53 -080047 protected SeparatedFolderListAdapter mAdapter;
48 protected Collection<Conversation> mTarget;
49 // True for CAB mode
50 protected boolean mBatch;
51 protected Account mAccount;
52 protected Folder mCurrentFolder;
53 protected int mTitleId;
Mark Wei8f98ac02012-10-01 17:05:08 -070054
Tony Mantler2a4be242013-12-11 15:30:53 -080055 public static FolderSelectionDialog getInstance(final Account account,
56 final Collection<Conversation> target, final boolean isBatch,
57 final Folder currentFolder, final boolean isMoveTo) {
Scott Kennedy0cb4aaa2013-05-13 14:22:26 -070058 /*
59 * TODO: This method should only be called with isMoveTo=true if this capability is not
60 * present on the account, so we should be able to remove the check here.
61 */
Tony Mantler2a4be242013-12-11 15:30:53 -080062 final FolderSelectionDialog f;
Rohan Shah00bbdef2013-02-07 14:18:05 -080063 if (isMoveTo || !account.supportsCapability(
64 UIProvider.AccountCapabilities.MULTIPLE_FOLDERS_PER_CONV)) {
Tony Mantler2a4be242013-12-11 15:30:53 -080065 f = new SingleFolderSelectionDialog();
Mark Wei8f98ac02012-10-01 17:05:08 -070066 } else {
Tony Mantler2a4be242013-12-11 15:30:53 -080067 f = new MultiFoldersSelectionDialog();
Mark Wei8f98ac02012-10-01 17:05:08 -070068 }
Tony Mantler2a4be242013-12-11 15:30:53 -080069 final Bundle args = new Bundle(4);
70 args.putParcelable(ARG_FOLDER_TAG, currentFolder);
71 args.putParcelable(ARG_ACCOUNT_TAG, account);
72 args.putBoolean(ARG_BATCH_TAG, isBatch);
73 args.putParcelableArray(ARG_TARGET_TAG, target.toArray(new Conversation[target.size()]));
74 f.setArguments(args);
75 return f;
Mark Wei9eb1c9a2012-10-01 12:54:50 -070076 }
77
Mark Wei8f98ac02012-10-01 17:05:08 -070078 protected abstract void onListItemClick(int position);
79
Tony Mantler2a4be242013-12-11 15:30:53 -080080 @Override
81 public void onCreate(final Bundle savedInstanceState) {
82 super.onCreate(savedInstanceState);
Tony Mantler47ce8042013-09-20 16:14:31 -070083 mAdapter = new SeparatedFolderListAdapter();
Tony Mantler2a4be242013-12-11 15:30:53 -080084
85 final Bundle args = getArguments();
86
87 mCurrentFolder = args.getParcelable(ARG_FOLDER_TAG);
88 mAccount = args.getParcelable(ARG_ACCOUNT_TAG);
89 mBatch = args.getBoolean(ARG_BATCH_TAG);
juwei7979c162018-05-25 12:01:24 +080090 Parcelable[] data = args.getParcelableArray(ARG_TARGET_TAG);
91 mTarget = Arrays.asList(Arrays.copyOf(data, data.length, Conversation[].class));
Mark Wei8f98ac02012-10-01 17:05:08 -070092 }
93
Tony Mantler2a4be242013-12-11 15:30:53 -080094 @Override
95 public Dialog onCreateDialog(final Bundle savedInstanceState) {
96 final AlertDialog dialog = new AlertDialog.Builder(getActivity())
97 .setNegativeButton(R.string.cancel, this)
98 .setPositiveButton(R.string.ok, this)
99 .setAdapter(mAdapter, this)
100 .setTitle(mTitleId)
101 .create();
102 dialog.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
103 @Override
Mark Wei8f98ac02012-10-01 17:05:08 -0700104 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
105 onListItemClick(position);
106 }
107 });
Tony Mantler2a4be242013-12-11 15:30:53 -0800108 return dialog;
Mark Wei9eb1c9a2012-10-01 12:54:50 -0700109 }
110
Tony Mantler2a4be242013-12-11 15:30:53 -0800111 protected ConversationUpdater getConversationUpdater() {
112 if (!isResumed()) {
113 throw new IllegalStateException(
114 "Tried to update conversations while fragment is not running");
Mark Wei8f98ac02012-10-01 17:05:08 -0700115 }
Tony Mantler2a4be242013-12-11 15:30:53 -0800116 final ControllableActivity activity = (ControllableActivity)getActivity();
117 return activity.getConversationUpdater();
Mark Wei8f98ac02012-10-01 17:05:08 -0700118 }
Mark Wei9eb1c9a2012-10-01 12:54:50 -0700119}