FolderSelectionActivity.java revision 276c80bb3ca7cff5f2a0511e8a9d0452ae91d858
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.os.Bundle;
30import android.view.View;
31import android.view.View.OnClickListener;
32import android.widget.Button;
33
34import com.android.mail.R;
35import com.android.mail.providers.Account;
36import com.android.mail.providers.Folder;
37import com.android.mail.providers.Settings;
38import com.android.mail.providers.UIProvider;
39import com.android.mail.ui.ViewMode.ModeChangeListener;
40import com.android.mail.utils.LogUtils;
41import com.android.mail.utils.Utils;
42import com.android.mail.widget.WidgetProvider;
43import com.google.common.collect.Sets;
44
45import java.util.Set;
46
47/**
48 * This activity displays the list of available folders for the current account.
49 */
50public class FolderSelectionActivity extends Activity implements OnClickListener,
51        DialogInterface.OnClickListener, FolderChangeListener, ControllableActivity {
52    public static final String EXTRA_ACCOUNT_SHORTCUT = "account-shortcut";
53
54    private static final String LOG_TAG = new LogUtils().getLogTag();
55
56    private Account mAccount;
57    private Folder mSelectedFolder;
58    private boolean mConfigureShortcut;
59    private boolean mConfigureWidget;
60    private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
61
62    @Override
63    public void onCreate(Bundle icicle) {
64        super.onCreate(icicle);
65
66        setContentView(R.layout.folders_activity);
67
68        final Intent intent = getIntent();
69        final String action = intent.getAction();
70        mConfigureShortcut = Intent.ACTION_CREATE_SHORTCUT.equals(action);
71        mConfigureWidget = AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(action);
72        if (!mConfigureShortcut && !mConfigureWidget) {
73            LogUtils.wtf(LOG_TAG, "unexpected intent: %s", intent);
74        }
75        if (mConfigureShortcut || mConfigureWidget) {
76            ActionBar actionBar = getActionBar();
77            if (actionBar != null) {
78                actionBar.setIcon(R.mipmap.ic_launcher_shortcut_folder);
79            }
80        }
81
82        if (mConfigureWidget) {
83            mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
84                    AppWidgetManager.INVALID_APPWIDGET_ID);
85            if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
86                LogUtils.wtf(LOG_TAG, "invalid widgetId");
87            }
88        }
89
90        mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT_SHORTCUT);
91        Button firstButton = (Button) findViewById(R.id.first_button);
92        firstButton.setVisibility(View.VISIBLE);
93        // TODO(mindyp) disable the manage folders buttons until we have a mange folders screen.
94        firstButton.setEnabled(false);
95        firstButton.setOnClickListener(this);
96
97        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
98        Fragment fragment = FolderListFragment.newInstance(this, mAccount.folderListUri);
99        fragmentTransaction.replace(R.id.folder_list_pane, fragment);
100        fragmentTransaction.commitAllowingStateLoss();
101    }
102
103    @Override
104    protected void onResume() {
105        super.onResume();
106
107        // TODO: (mindyp) Make sure we're operating on the same account as
108        // before. If the user switched accounts, switch back.
109    }
110
111    @Override
112    public void onClick(View v) {
113        switch (v.getId()) {
114            case R.id.cancel:
115                doCancel();
116                break;
117        }
118    }
119
120    private void doCancel() {
121        setResult(RESULT_CANCELED);
122        finish();
123    }
124
125    @Override
126    public Dialog onCreateDialog(int id, Bundle bundle) {
127        Dialog dialog = null;
128        switch (id) {
129            case R.layout.folder_sync_for_widget_dialog:
130                dialog = new SyncForWidgetDialog(this, mAccount, mSelectedFolder, this);
131                break;
132        }
133        return dialog == null ? super.onCreateDialog(id, bundle) : dialog;
134    }
135
136    /**
137     * Create a widget for the specified account and folder
138     */
139    private void createWidget() {
140        WidgetProvider.updateWidget(this, mAppWidgetId, mAccount, mSelectedFolder);
141        Intent result = new Intent();
142        result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
143        setResult(RESULT_OK, result);
144        finish();
145    }
146
147    @Override
148    public void onClick(DialogInterface dialog, int which) {
149        if (which == DialogInterface.BUTTON_POSITIVE) {
150            // The only dialog that is
151            createWidget();
152        } else {
153            doCancel();
154        }
155    }
156
157    @Override
158    public void onFolderChanged(Folder folder) {
159        if (!folder.equals(mSelectedFolder)) {
160            mSelectedFolder = folder;
161            Intent resultIntent = new Intent();
162
163            if (mConfigureShortcut) {
164                /*
165                 * Create the shortcut Intent based on it with the additional
166                 * information that we have in this activity: name of the
167                 * account, calculate the human readable name of the folder and
168                 * use it as the shortcut name, etc...
169                 */
170                final Intent clickIntent = Utils.createViewFolderIntent(mSelectedFolder, mAccount);
171                resultIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, clickIntent);
172                resultIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
173                        Intent.ShortcutIconResource.fromContext(this,
174                                R.mipmap.ic_launcher_shortcut_folder));
175
176                CharSequence humanFolderName = mSelectedFolder.name;
177
178                resultIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, humanFolderName);
179
180                // Now ask the user what name they want for this shortcut. Pass
181                // the
182                // shortcut intent that we just created, the user can modify the
183                // folder in
184                // ShortcutNameActivity.
185                final Intent shortcutNameIntent = new Intent(this, ShortcutNameActivity.class);
186                shortcutNameIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY
187                        | Intent.FLAG_ACTIVITY_FORWARD_RESULT);
188                shortcutNameIntent.putExtra(ShortcutNameActivity.EXTRA_FOLDER_CLICK_INTENT,
189                        resultIntent);
190                shortcutNameIntent.putExtra(ShortcutNameActivity.EXTRA_SHORTCUT_NAME,
191                        humanFolderName);
192
193                startActivity(shortcutNameIntent);
194                finish();
195            } else if (mConfigureWidget) {
196                // Check to see if the widget is set to be synchronized
197                final Cursor settings = mAccount.getSettings();
198                final Set<String> synchronizedFoldersSet = Sets.newHashSet();
199
200                // Add all of the synchronized folders to the set
201                // TODO: (mindyp) deal with folders.
202                // synchronizedFoldersSet.addAll(settings.getFoldersIncluded());
203                // synchronizedFoldersSet.addAll(settings.getFoldersPartial());
204
205                if (!synchronizedFoldersSet.contains(mSelectedFolder.name)) {
206                    // Display a dialog offering to enable sync for this folder
207                    showDialog(R.layout.folder_sync_for_widget_dialog);
208                } else {
209                    createWidget();
210                }
211            }
212        }
213    }
214
215    @Override
216    public String getHelpContext() {
217        // TODO Auto-generated method stub
218        return null;
219    }
220
221    @Override
222    public Context getActivityContext() {
223        return this;
224    }
225
226    @Override
227    public void attachConversationList(ConversationListFragment conversationList) {
228    }
229
230    @Override
231    public ViewMode getViewMode() {
232        return null;
233    }
234
235    @Override
236    public void setViewModeListener(ModeChangeListener listener) {
237    }
238
239    @Override
240    public void unsetViewModeListener(ModeChangeListener listener) {
241    }
242
243    @Override
244    public ConversationListCallbacks getListHandler() {
245        return null;
246    }
247
248    @Override
249    public void attachFolderList(FolderListFragment folderListFragment) {
250    }
251
252    @Override
253    public void attachConversationView(ConversationViewFragment conversationViewFragment) {
254    }
255
256    @Override
257    public FolderChangeListener getFolderChangeListener() {
258        return this;
259    }
260
261    @Override
262    public Settings getSettings() {
263        return null;
264    }
265
266    @Override
267    public boolean onSearchRequested(String query) {
268        return false;
269    }
270
271    @Override
272    public boolean shouldShowFirstConversation() {
273        return false;
274    }
275}
276