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.LoaderManager;
22import android.content.Context;
23import android.content.CursorLoader;
24import android.content.DialogInterface;
25import android.content.Loader;
26import android.database.Cursor;
27import android.net.Uri;
28import android.os.Bundle;
29
30import com.android.mail.R;
31import com.android.mail.providers.Folder;
32import com.android.mail.providers.UIProvider;
33import com.android.mail.ui.FolderSelectorAdapter.FolderRow;
34import com.android.mail.utils.Utils;
35
36import java.util.ArrayList;
37
38/**
39 * Displays a folder selection dialog for the conversation provided. It allows
40 * the user to switch a conversation from one folder to another.
41 */
42public class SingleFolderSelectionDialog extends FolderSelectionDialog {
43    public SingleFolderSelectionDialog() {}
44
45    private static final int FOLDER_LOADER_ID = 0;
46    private static final String FOLDER_QUERY_URI_TAG = "folderQueryUri";
47
48    @Override
49    public void onCreate(Bundle savedInstanceState) {
50        super.onCreate(savedInstanceState);
51
52        mTitleId = R.string.move_to_selection_dialog_title;
53
54        final Bundle args = new Bundle(1);
55        args.putParcelable(FOLDER_QUERY_URI_TAG, !Utils.isEmpty(mAccount.fullFolderListUri) ?
56                mAccount.fullFolderListUri : mAccount.folderListUri);
57        final Context loaderContext = getActivity().getApplicationContext();
58        getLoaderManager().initLoader(FOLDER_LOADER_ID, args,
59                new LoaderManager.LoaderCallbacks<Cursor>() {
60                    @Override
61                    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
62                        final Uri queryUri = args.getParcelable(FOLDER_QUERY_URI_TAG);
63                        return new CursorLoader(loaderContext, queryUri,
64                                UIProvider.FOLDERS_PROJECTION, null, null, null);
65                    }
66
67                    @Override
68                    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
69                        final Context context = getActivity();
70                        if (data == null || context == null) {
71                            return;
72                        }
73
74                        final AlertDialog dialog = (AlertDialog) getDialog();
75                        if (dialog == null) {
76                            // This could happen if the dialog is dismissed just before the
77                            // load finishes.
78                            return;
79                        }
80                        // The number of view types changes here, so we have to reset the ListView's
81                        // adapter.
82                        dialog.getListView().setAdapter(null);
83                        dialog.getListView().setDivider(null);
84
85                        mAdapter.clearSections();
86
87                        // Create a system folder adapter and an adapter for hierarchical
88                        // and user folders. If there are no folders added to either of them,
89                        // do not add as a section since a 0-count adapter will result in an
90                        // IndexOutOfBoundsException.
91                        SystemFolderSelectorAdapter sysFolderAdapter =
92                                new SystemFolderSelectorAdapter(context, data,
93                                    R.layout.single_folders_view, mCurrentFolder);
94                        if (sysFolderAdapter.getCount() > 0) {
95                            mAdapter.addSection(sysFolderAdapter);
96                        }
97
98                        // TODO(pwestbro): determine if we need to call filterFolders
99                        // if filterFolders is not necessary, remove the method decl with one arg.
100                        UserFolderHierarchicalFolderSelectorAdapter hierarchicalAdapter =
101                                new UserFolderHierarchicalFolderSelectorAdapter(context,
102                                    AddableFolderSelectorAdapter.filterFolders(data),
103                                    R.layout.single_folders_view, mCurrentFolder);
104                        if (hierarchicalAdapter.getCount() > 0) {
105                            mAdapter.addSection(hierarchicalAdapter);
106                        }
107
108                        dialog.getListView().setAdapter(mAdapter);
109                    }
110
111                    @Override
112                    public void onLoaderReset(Loader<Cursor> loader) {
113                        mAdapter.clearSections();
114                    }
115                });
116    }
117
118    @Override
119    protected void onListItemClick(int position) {
120        final Object item = mAdapter.getItem(position);
121        if (item instanceof FolderRow) {
122            final Folder folder = ((FolderRow) item).getFolder();
123            ArrayList<FolderOperation> ops = new ArrayList<FolderOperation>();
124            // Remove the current folder and add the new folder.
125            ops.add(new FolderOperation(mCurrentFolder, false));
126            ops.add(new FolderOperation(folder, true));
127            getConversationUpdater()
128                    .assignFolder(ops, mTarget, mBatch, true /* showUndo */, true /* isMoveTo */);
129            dismiss();
130        }
131    }
132
133    @Override
134    public void onClick(DialogInterface dialog, int which) {
135        // Do nothing.
136    }
137}
138