SingleFolderSelectionDialog.java revision 74098779f90c61487ff0b5bbb7a458f0a8818047
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                        // TODO(mindyp) : bring this back in UR8 when Email providers
86                        // will have divided folder sections.
87                        final String[] headers = context.getResources().getStringArray(
88                                R.array.moveto_folder_sections);
89                        // Currently, the number of adapters are assumed to match the
90                        // number of headers in the string array.
91                        mAdapter.addSection(new SystemFolderSelectorAdapter(context, data,
92                                R.layout.single_folders_view, headers[0], mCurrentFolder));
93
94                        // TODO(mindyp): we currently do not support frequently moved to
95                        // folders, at headers[1]; need to define what that means.*/
96                        // TODO(pwestbro): determine if we need to call filterFolders
97                        // if filterFolders is not necessary, remove the method decl with one arg.
98                        mAdapter.addSection(
99                                new UserFolderHierarchicalFolderSelectorAdapter(context,
100                                AddableFolderSelectorAdapter.filterFolders(data),
101                                R.layout.single_folders_view, headers[2], mCurrentFolder));
102
103                        dialog.getListView().setAdapter(mAdapter);
104                    }
105
106                    @Override
107                    public void onLoaderReset(Loader<Cursor> loader) {
108                        mAdapter.clearSections();
109                    }
110                });
111    }
112
113    @Override
114    protected void onListItemClick(int position) {
115        final Object item = mAdapter.getItem(position);
116        if (item instanceof FolderRow) {
117            final Folder folder = ((FolderRow) item).getFolder();
118            ArrayList<FolderOperation> ops = new ArrayList<FolderOperation>();
119            // Remove the current folder and add the new folder.
120            ops.add(new FolderOperation(mCurrentFolder, false));
121            ops.add(new FolderOperation(folder, true));
122            getConversationUpdater()
123                    .assignFolder(ops, mTarget, mBatch, true /* showUndo */, true /* isMoveTo */);
124            dismiss();
125        }
126    }
127
128    @Override
129    public void onClick(DialogInterface dialog, int which) {
130        // Do nothing.
131    }
132}
133