blob: ce3a9a7b3f006a95ab95a2be884d80eb79c2dce2 [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;
yongga188ce672016-01-11 11:34:44 +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
yongga188ce672016-01-11 11:34:44 +080036import java.util.ArrayList;
Tony Mantler2a4be242013-12-11 15:30:53 -080037import java.util.Arrays;
Mark Wei8f98ac02012-10-01 17:05:08 -070038import java.util.Collection;
39
Tony Mantler2a4be242013-12-11 15:30:53 -080040public abstract class FolderSelectionDialog extends DialogFragment implements OnClickListener {
Mark Wei8f98ac02012-10-01 17:05:08 -070041 protected static final String LOG_TAG = LogTag.getLogTag();
Mark Wei9eb1c9a2012-10-01 12:54:50 -070042
Tony Mantler2a4be242013-12-11 15:30:53 -080043 private static final String ARG_FOLDER_TAG = "folder";
44 private static final String ARG_ACCOUNT_TAG = "account";
45 private static final String ARG_BATCH_TAG = "batch";
46 private static final String ARG_TARGET_TAG = "target";
Mark Wei8f98ac02012-10-01 17:05:08 -070047
Tony Mantler2a4be242013-12-11 15:30:53 -080048 protected SeparatedFolderListAdapter mAdapter;
49 protected Collection<Conversation> mTarget;
50 // True for CAB mode
51 protected boolean mBatch;
52 protected Account mAccount;
53 protected Folder mCurrentFolder;
54 protected int mTitleId;
Mark Wei8f98ac02012-10-01 17:05:08 -070055
Tony Mantler2a4be242013-12-11 15:30:53 -080056 public static FolderSelectionDialog getInstance(final Account account,
57 final Collection<Conversation> target, final boolean isBatch,
58 final Folder currentFolder, final boolean isMoveTo) {
Scott Kennedy0cb4aaa2013-05-13 14:22:26 -070059 /*
60 * TODO: This method should only be called with isMoveTo=true if this capability is not
61 * present on the account, so we should be able to remove the check here.
62 */
Tony Mantler2a4be242013-12-11 15:30:53 -080063 final FolderSelectionDialog f;
Rohan Shah00bbdef2013-02-07 14:18:05 -080064 if (isMoveTo || !account.supportsCapability(
65 UIProvider.AccountCapabilities.MULTIPLE_FOLDERS_PER_CONV)) {
Tony Mantler2a4be242013-12-11 15:30:53 -080066 f = new SingleFolderSelectionDialog();
Mark Wei8f98ac02012-10-01 17:05:08 -070067 } else {
Tony Mantler2a4be242013-12-11 15:30:53 -080068 f = new MultiFoldersSelectionDialog();
Mark Wei8f98ac02012-10-01 17:05:08 -070069 }
Tony Mantler2a4be242013-12-11 15:30:53 -080070 final Bundle args = new Bundle(4);
71 args.putParcelable(ARG_FOLDER_TAG, currentFolder);
72 args.putParcelable(ARG_ACCOUNT_TAG, account);
73 args.putBoolean(ARG_BATCH_TAG, isBatch);
74 args.putParcelableArray(ARG_TARGET_TAG, target.toArray(new Conversation[target.size()]));
75 f.setArguments(args);
76 return f;
Mark Wei9eb1c9a2012-10-01 12:54:50 -070077 }
78
Mark Wei8f98ac02012-10-01 17:05:08 -070079 protected abstract void onListItemClick(int position);
80
Tony Mantler2a4be242013-12-11 15:30:53 -080081 @Override
82 public void onCreate(final Bundle savedInstanceState) {
83 super.onCreate(savedInstanceState);
Tony Mantler47ce8042013-09-20 16:14:31 -070084 mAdapter = new SeparatedFolderListAdapter();
Tony Mantler2a4be242013-12-11 15:30:53 -080085
86 final Bundle args = getArguments();
87
88 mCurrentFolder = args.getParcelable(ARG_FOLDER_TAG);
89 mAccount = args.getParcelable(ARG_ACCOUNT_TAG);
90 mBatch = args.getBoolean(ARG_BATCH_TAG);
yongga188ce672016-01-11 11:34:44 +080091 mTarget = new ArrayList<>();
92 Parcelable[] temps = args.getParcelableArray(ARG_TARGET_TAG);
93 if(temps != null){
94 for(Parcelable temp:temps){
95 mTarget.add((Conversation) temp);
96 }
97 }
Mark Wei8f98ac02012-10-01 17:05:08 -070098 }
99
Tony Mantler2a4be242013-12-11 15:30:53 -0800100 @Override
101 public Dialog onCreateDialog(final Bundle savedInstanceState) {
102 final AlertDialog dialog = new AlertDialog.Builder(getActivity())
103 .setNegativeButton(R.string.cancel, this)
104 .setPositiveButton(R.string.ok, this)
105 .setAdapter(mAdapter, this)
106 .setTitle(mTitleId)
107 .create();
108 dialog.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
109 @Override
Mark Wei8f98ac02012-10-01 17:05:08 -0700110 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
111 onListItemClick(position);
112 }
113 });
Tony Mantler2a4be242013-12-11 15:30:53 -0800114 return dialog;
Mark Wei9eb1c9a2012-10-01 12:54:50 -0700115 }
116
Tony Mantler2a4be242013-12-11 15:30:53 -0800117 protected ConversationUpdater getConversationUpdater() {
118 if (!isResumed()) {
119 throw new IllegalStateException(
120 "Tried to update conversations while fragment is not running");
Mark Wei8f98ac02012-10-01 17:05:08 -0700121 }
Tony Mantler2a4be242013-12-11 15:30:53 -0800122 final ControllableActivity activity = (ControllableActivity)getActivity();
123 return activity.getConversationUpdater();
Mark Wei8f98ac02012-10-01 17:05:08 -0700124 }
Mark Wei9eb1c9a2012-10-01 12:54:50 -0700125}