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.app.Dialog;
22import android.app.DialogFragment;
23import android.content.DialogInterface.OnClickListener;
24import android.os.Bundle;
25import android.view.View;
26import android.widget.AdapterView;
27
28import com.android.mail.R;
29import com.android.mail.providers.Account;
30import com.android.mail.providers.Conversation;
31import com.android.mail.providers.Folder;
32import com.android.mail.providers.UIProvider;
33import com.android.mail.utils.LogTag;
34
35import java.util.Arrays;
36import java.util.Collection;
37
38public abstract class FolderSelectionDialog extends DialogFragment implements OnClickListener {
39    protected static final String LOG_TAG = LogTag.getLogTag();
40
41    private static final String ARG_FOLDER_TAG = "folder";
42    private static final String ARG_ACCOUNT_TAG = "account";
43    private static final String ARG_BATCH_TAG = "batch";
44    private static final String ARG_TARGET_TAG = "target";
45
46    protected SeparatedFolderListAdapter mAdapter;
47    protected Collection<Conversation> mTarget;
48    // True for CAB mode
49    protected boolean mBatch;
50    protected Account mAccount;
51    protected Folder mCurrentFolder;
52    protected int mTitleId;
53
54    public static FolderSelectionDialog getInstance(final Account account,
55            final Collection<Conversation> target, final boolean isBatch,
56            final Folder currentFolder, final boolean isMoveTo) {
57        /*
58         * TODO: This method should only be called with isMoveTo=true if this capability is not
59         * present on the account, so we should be able to remove the check here.
60         */
61        final FolderSelectionDialog f;
62        if (isMoveTo || !account.supportsCapability(
63                UIProvider.AccountCapabilities.MULTIPLE_FOLDERS_PER_CONV)) {
64            f = new SingleFolderSelectionDialog();
65        } else {
66            f = new MultiFoldersSelectionDialog();
67        }
68        final Bundle args = new Bundle(4);
69        args.putParcelable(ARG_FOLDER_TAG, currentFolder);
70        args.putParcelable(ARG_ACCOUNT_TAG, account);
71        args.putBoolean(ARG_BATCH_TAG, isBatch);
72        args.putParcelableArray(ARG_TARGET_TAG, target.toArray(new Conversation[target.size()]));
73        f.setArguments(args);
74        return f;
75    }
76
77    protected abstract void onListItemClick(int position);
78
79    @Override
80    public void onCreate(final Bundle savedInstanceState) {
81        super.onCreate(savedInstanceState);
82        mAdapter = new SeparatedFolderListAdapter();
83
84        final Bundle args = getArguments();
85
86        mCurrentFolder = args.getParcelable(ARG_FOLDER_TAG);
87        mAccount = args.getParcelable(ARG_ACCOUNT_TAG);
88        mBatch = args.getBoolean(ARG_BATCH_TAG);
89        mTarget = Arrays.asList((Conversation[])args.getParcelableArray(ARG_TARGET_TAG));
90    }
91
92    @Override
93    public Dialog onCreateDialog(final Bundle savedInstanceState) {
94        final AlertDialog dialog = new AlertDialog.Builder(getActivity())
95                .setNegativeButton(R.string.cancel, this)
96                .setPositiveButton(R.string.ok, this)
97                .setAdapter(mAdapter, this)
98                .setTitle(mTitleId)
99                .create();
100        dialog.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
101            @Override
102            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
103                onListItemClick(position);
104            }
105        });
106        return dialog;
107    }
108
109    protected ConversationUpdater getConversationUpdater() {
110        if (!isResumed()) {
111            throw new IllegalStateException(
112                    "Tried to update conversations while fragment is not running");
113        }
114        final ControllableActivity activity = (ControllableActivity)getActivity();
115        return activity.getConversationUpdater();
116    }
117}
118