FolderSelectionActivity.java revision 6902dcf8688d7d0691639f49365a78a3a78fe9e3
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.mail.ui;
18
19import android.app.ActionBar;
20import android.app.Activity;
21import android.app.Dialog;
22import android.app.Fragment;
23import android.app.FragmentTransaction;
24import android.appwidget.AppWidgetManager;
25import android.content.Context;
26import android.content.DialogInterface;
27import android.content.Intent;
28import android.database.Cursor;
29import android.net.Uri;
30import android.os.Bundle;
31import android.view.DragEvent;
32import android.view.View;
33import android.view.View.OnClickListener;
34import android.widget.Button;
35
36import com.android.mail.R;
37import com.android.mail.providers.Account;
38import com.android.mail.providers.Folder;
39import com.android.mail.providers.Settings;
40import com.android.mail.ui.FolderListFragment.FolderListSelectionListener;
41import com.android.mail.ui.ViewMode.ModeChangeListener;
42import com.android.mail.utils.LogUtils;
43import com.android.mail.utils.Utils;
44import com.android.mail.widget.WidgetProvider;
45import com.google.common.collect.Sets;
46
47import java.util.Set;
48
49/**
50 * This activity displays the list of available folders for the current account.
51 */
52public class FolderSelectionActivity extends Activity implements OnClickListener,
53        DialogInterface.OnClickListener, FolderChangeListener, ControllableActivity,
54        FolderListSelectionListener {
55    public static final String EXTRA_ACCOUNT_SHORTCUT = "account-shortcut";
56
57    private static final String LOG_TAG = new LogUtils().getLogTag();
58
59    private Account mAccount;
60    private Folder mSelectedFolder;
61    private boolean mConfigureShortcut;
62    private boolean mConfigureWidget;
63    private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
64
65    @Override
66    public void onCreate(Bundle icicle) {
67        super.onCreate(icicle);
68
69        setContentView(R.layout.folders_activity);
70
71        final Intent intent = getIntent();
72        final String action = intent.getAction();
73        mConfigureShortcut = Intent.ACTION_CREATE_SHORTCUT.equals(action);
74        mConfigureWidget = AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(action);
75        if (!mConfigureShortcut && !mConfigureWidget) {
76            LogUtils.wtf(LOG_TAG, "unexpected intent: %s", intent);
77        }
78        if (mConfigureShortcut || mConfigureWidget) {
79            ActionBar actionBar = getActionBar();
80            if (actionBar != null) {
81                actionBar.setIcon(R.mipmap.ic_launcher_shortcut_folder);
82            }
83        }
84
85        if (mConfigureWidget) {
86            mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
87                    AppWidgetManager.INVALID_APPWIDGET_ID);
88            if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
89                LogUtils.wtf(LOG_TAG, "invalid widgetId");
90            }
91        }
92
93        mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT_SHORTCUT);
94        Button firstButton = (Button) findViewById(R.id.first_button);
95        firstButton.setVisibility(View.VISIBLE);
96        // TODO(mindyp) disable the manage folders buttons until we have a mange folders screen.
97        firstButton.setEnabled(false);
98        firstButton.setOnClickListener(this);
99
100        createFolderListFragment(null, mAccount.folderListUri);
101    }
102
103    private void createFolderListFragment(Folder parent, Uri uri) {
104        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
105        Fragment fragment = FolderListFragment.newInstance(parent, uri);
106        fragmentTransaction.replace(R.id.content_pane, fragment);
107        fragmentTransaction.commitAllowingStateLoss();
108    }
109
110    @Override
111    protected void onResume() {
112        super.onResume();
113
114        // TODO: (mindyp) Make sure we're operating on the same account as
115        // before. If the user switched accounts, switch back.
116    }
117
118    @Override
119    public void onClick(View v) {
120        switch (v.getId()) {
121            case R.id.cancel:
122                doCancel();
123                break;
124        }
125    }
126
127    private void doCancel() {
128        setResult(RESULT_CANCELED);
129        finish();
130    }
131
132    @Override
133    public Dialog onCreateDialog(int id, Bundle bundle) {
134        Dialog dialog = null;
135        switch (id) {
136            case R.layout.folder_sync_for_widget_dialog:
137                dialog = new SyncForWidgetDialog(this, mAccount, mSelectedFolder, this);
138                break;
139        }
140        return dialog == null ? super.onCreateDialog(id, bundle) : dialog;
141    }
142
143    /**
144     * Create a widget for the specified account and folder
145     */
146    protected void createWidget(int id, Account account, Folder selectedFolder) {
147        WidgetProvider.updateWidget(this, id, account, selectedFolder);
148        Intent result = new Intent();
149        result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
150        setResult(RESULT_OK, result);
151        finish();
152    }
153
154    @Override
155    public void onClick(DialogInterface dialog, int which) {
156        if (which == DialogInterface.BUTTON_POSITIVE) {
157            // The only dialog that is
158            createWidget(mAppWidgetId, mAccount, mSelectedFolder);
159        } else {
160            doCancel();
161        }
162    }
163
164    @Override
165    public void onFolderChanged(Folder folder) {
166        if (!folder.equals(mSelectedFolder)) {
167            mSelectedFolder = folder;
168            Intent resultIntent = new Intent();
169
170            if (mConfigureShortcut) {
171                /*
172                 * Create the shortcut Intent based on it with the additional
173                 * information that we have in this activity: name of the
174                 * account, calculate the human readable name of the folder and
175                 * use it as the shortcut name, etc...
176                 */
177                final Intent clickIntent = Utils.createViewFolderIntent(mSelectedFolder, mAccount,
178                        true);
179                resultIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, clickIntent);
180                resultIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
181                        Intent.ShortcutIconResource.fromContext(this,
182                                R.mipmap.ic_launcher_shortcut_folder));
183
184                CharSequence humanFolderName = mSelectedFolder.name;
185
186                resultIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, humanFolderName);
187
188                // Now ask the user what name they want for this shortcut. Pass
189                // the
190                // shortcut intent that we just created, the user can modify the
191                // folder in
192                // ShortcutNameActivity.
193                final Intent shortcutNameIntent = new Intent(this, ShortcutNameActivity.class);
194                shortcutNameIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY
195                        | Intent.FLAG_ACTIVITY_FORWARD_RESULT);
196                shortcutNameIntent.putExtra(ShortcutNameActivity.EXTRA_FOLDER_CLICK_INTENT,
197                        resultIntent);
198                shortcutNameIntent.putExtra(ShortcutNameActivity.EXTRA_SHORTCUT_NAME,
199                        humanFolderName);
200
201                startActivity(shortcutNameIntent);
202                finish();
203            } else if (mConfigureWidget) {
204                // Check to see if the widget is set to be synchronized
205                final Set<String> synchronizedFoldersSet = Sets.newHashSet();
206
207                // Add all of the synchronized folders to the set
208                // TODO: (mindyp) deal with folders.
209                // synchronizedFoldersSet.addAll(settings.getFoldersIncluded());
210                // synchronizedFoldersSet.addAll(settings.getFoldersPartial());
211
212                if (!synchronizedFoldersSet.contains(mSelectedFolder.name)) {
213                    // Display a dialog offering to enable sync for this folder
214                    showDialog(R.layout.folder_sync_for_widget_dialog);
215                } else {
216                    createWidget(mAppWidgetId, mAccount, mSelectedFolder);
217                }
218            }
219        }
220    }
221
222    @Override
223    public String getHelpContext() {
224        // TODO Auto-generated method stub
225        return null;
226    }
227
228    @Override
229    public Context getActivityContext() {
230        return this;
231    }
232
233    @Override
234    public ViewMode getViewMode() {
235        return null;
236    }
237
238    @Override
239    public void setViewModeListener(ModeChangeListener listener) {
240    }
241
242    @Override
243    public void unsetViewModeListener(ModeChangeListener listener) {
244    }
245
246    @Override
247    public ConversationListCallbacks getListHandler() {
248        return null;
249    }
250
251    @Override
252    public FolderChangeListener getFolderChangeListener() {
253        return this;
254    }
255
256    @Override
257    public Settings getSettings() {
258        return null;
259    }
260
261    @Override
262    public boolean onSearchRequested(String query) {
263        return false;
264    }
265
266    @Override
267    public boolean shouldShowFirstConversation() {
268        return false;
269    }
270
271    @Override
272    public ConversationSelectionSet getSelectedSet() {
273        return null;
274    }
275
276    @Override
277    public void onFolderSelected(Folder folder, boolean viewingChildren) {
278        if (!viewingChildren && folder.hasChildren) {
279            // Replace this fragment with a new FolderListFragment
280            // showing this folder's children if we are not already looking
281            // at the child view for this folder.
282            createFolderListFragment(folder, folder.childFoldersListUri);
283            return;
284        }
285        onFolderChanged(folder);
286    }
287
288    @Override
289    public FolderListSelectionListener getFolderListSelectionListener() {
290        return this;
291    }
292
293    @Override
294    public DragListener getDragListener() {
295        return null;
296    }
297
298    @Override
299    public boolean supportsDrag(DragEvent event, Folder folder) {
300        return false;
301    }
302
303    @Override
304    public void handleDrop(DragEvent event, Folder folder) {
305        // Do nothing.
306    }
307
308    @Override
309    public void onUndoCancel() {
310        // Do nothing.
311    }
312
313    @Override
314    public void onUndoAvailable(UndoOperation undoOp) {
315        // Do nothing.
316    }
317}
318