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
84                        mAdapter.clearSections();
85
86                        // Create a system folder adapter and an adapter for hierarchical
87                        // and user folders. If there are no folders added to either of them,
88                        // do not add as a section since a 0-count adapter will result in an
89                        // IndexOutOfBoundsException.
90                        SystemFolderSelectorAdapter sysFolderAdapter =
91                                new SystemFolderSelectorAdapter(context, data,
92                                    R.layout.single_folders_view, mCurrentFolder);
93                        if (sysFolderAdapter.getCount() > 0) {
94                            mAdapter.addSection(sysFolderAdapter);
95                        }
96
97                        // TODO(pwestbro): determine if we need to call filterFolders
98                        // if filterFolders is not necessary, remove the method decl with one arg.
99                        UserFolderHierarchicalFolderSelectorAdapter hierarchicalAdapter =
100                                new UserFolderHierarchicalFolderSelectorAdapter(context,
101                                    AddableFolderSelectorAdapter.filterFolders(data),
102                                    R.layout.single_folders_view, mCurrentFolder);
103                        if (hierarchicalAdapter.getCount() > 0) {
104                            mAdapter.addSection(hierarchicalAdapter);
105                        }
106
107                        dialog.getListView().setAdapter(mAdapter);
108                    }
109
110                    @Override
111                    public void onLoaderReset(Loader<Cursor> loader) {
112                        mAdapter.clearSections();
113                    }
114                });
115    }
116
117    @Override
118    protected void onListItemClick(int position) {
119        final Object item = mAdapter.getItem(position);
120        if (item instanceof FolderRow) {
121            final Folder folder = ((FolderRow) item).getFolder();
122            ArrayList<FolderOperation> ops = new ArrayList<FolderOperation>();
123            // Remove the current folder and add the new folder.
124            ops.add(new FolderOperation(mCurrentFolder, false));
125            ops.add(new FolderOperation(folder, true));
126            getConversationUpdater()
127                    .assignFolder(ops, mTarget, mBatch, true /* showUndo */, true /* isMoveTo */);
128            dismiss();
129        }
130    }
131
132    @Override
133    public void onClick(DialogInterface dialog, int which) {
134        // Do nothing.
135    }
136}
137