Mark Wei | 9eb1c9a | 2012-10-01 12:54:50 -0700 | [diff] [blame] | 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 | |
Mark Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 20 | import android.app.AlertDialog; |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 21 | import android.app.Dialog; |
| 22 | import android.app.DialogFragment; |
Mark Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 23 | import android.content.DialogInterface.OnClickListener; |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 24 | import android.os.Bundle; |
juwei | 7979c16 | 2018-05-25 12:01:24 +0800 | [diff] [blame] | 25 | import android.os.Parcelable; |
Mark Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 26 | import android.view.View; |
| 27 | import android.widget.AdapterView; |
Mark Wei | 9eb1c9a | 2012-10-01 12:54:50 -0700 | [diff] [blame] | 28 | |
Mark Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 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; |
Mark Wei | 9eb1c9a | 2012-10-01 12:54:50 -0700 | [diff] [blame] | 35 | |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 36 | import java.util.Arrays; |
Mark Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 37 | import java.util.Collection; |
| 38 | |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 39 | public abstract class FolderSelectionDialog extends DialogFragment implements OnClickListener { |
Mark Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 40 | protected static final String LOG_TAG = LogTag.getLogTag(); |
Mark Wei | 9eb1c9a | 2012-10-01 12:54:50 -0700 | [diff] [blame] | 41 | |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 42 | 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 Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 46 | |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 47 | 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 Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 54 | |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 55 | public static FolderSelectionDialog getInstance(final Account account, |
| 56 | final Collection<Conversation> target, final boolean isBatch, |
| 57 | final Folder currentFolder, final boolean isMoveTo) { |
Scott Kennedy | 0cb4aaa | 2013-05-13 14:22:26 -0700 | [diff] [blame] | 58 | /* |
| 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 Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 62 | final FolderSelectionDialog f; |
Rohan Shah | 00bbdef | 2013-02-07 14:18:05 -0800 | [diff] [blame] | 63 | if (isMoveTo || !account.supportsCapability( |
| 64 | UIProvider.AccountCapabilities.MULTIPLE_FOLDERS_PER_CONV)) { |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 65 | f = new SingleFolderSelectionDialog(); |
Mark Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 66 | } else { |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 67 | f = new MultiFoldersSelectionDialog(); |
Mark Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 68 | } |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 69 | 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 Wei | 9eb1c9a | 2012-10-01 12:54:50 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Mark Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 78 | protected abstract void onListItemClick(int position); |
| 79 | |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 80 | @Override |
| 81 | public void onCreate(final Bundle savedInstanceState) { |
| 82 | super.onCreate(savedInstanceState); |
Tony Mantler | 47ce804 | 2013-09-20 16:14:31 -0700 | [diff] [blame] | 83 | mAdapter = new SeparatedFolderListAdapter(); |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 84 | |
| 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); |
juwei | 7979c16 | 2018-05-25 12:01:24 +0800 | [diff] [blame] | 90 | Parcelable[] data = args.getParcelableArray(ARG_TARGET_TAG); |
| 91 | mTarget = Arrays.asList(Arrays.copyOf(data, data.length, Conversation[].class)); |
Mark Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 94 | @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 Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 104 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 105 | onListItemClick(position); |
| 106 | } |
| 107 | }); |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 108 | return dialog; |
Mark Wei | 9eb1c9a | 2012-10-01 12:54:50 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 111 | protected ConversationUpdater getConversationUpdater() { |
| 112 | if (!isResumed()) { |
| 113 | throw new IllegalStateException( |
| 114 | "Tried to update conversations while fragment is not running"); |
Mark Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 115 | } |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 116 | final ControllableActivity activity = (ControllableActivity)getActivity(); |
| 117 | return activity.getConversationUpdater(); |
Mark Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 118 | } |
Mark Wei | 9eb1c9a | 2012-10-01 12:54:50 -0700 | [diff] [blame] | 119 | } |