1/*
2 * Copyright (C) 2011 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.email.activity.setup;
18
19import android.app.ActionBar;
20import android.app.LoaderManager;
21import android.content.ContentUris;
22import android.content.ContentValues;
23import android.content.Context;
24import android.content.CursorLoader;
25import android.content.Intent;
26import android.content.Loader;
27import android.content.res.Resources;
28import android.database.Cursor;
29import android.net.Uri;
30import android.os.Bundle;
31import android.preference.CheckBoxPreference;
32import android.preference.ListPreference;
33import android.preference.Preference;
34import android.preference.Preference.OnPreferenceChangeListener;
35import android.preference.PreferenceActivity;
36import android.preference.PreferenceFragment;
37import android.support.annotation.NonNull;
38import android.text.TextUtils;
39import android.view.MenuItem;
40
41import com.android.email.R;
42import com.android.emailcommon.Logging;
43import com.android.emailcommon.provider.Account;
44import com.android.emailcommon.provider.EmailContent.AccountColumns;
45import com.android.emailcommon.provider.EmailContent.MailboxColumns;
46import com.android.emailcommon.provider.Mailbox;
47import com.android.emailcommon.provider.Policy;
48import com.android.emailcommon.utility.EmailAsyncTask;
49import com.android.emailcommon.utility.Utility;
50import com.android.mail.providers.Folder;
51import com.android.mail.providers.UIProvider;
52import com.android.mail.ui.MailAsyncTaskLoader;
53import com.android.mail.utils.LogUtils;
54import com.google.common.base.Preconditions;
55
56import java.util.ArrayList;
57import java.util.Arrays;
58import java.util.HashMap;
59import java.util.List;
60import java.util.Map;
61
62/**
63 * "Mailbox settings" activity.
64 *
65 * It's used to update per-mailbox sync settings.  It normally updates Mailbox settings, unless
66 * the target mailbox is Inbox, in which case it updates Account settings instead.
67 *
68 * All changes made by the user will not be immediately saved to the database, as changing the
69 * sync window may result in removal of messages.  Instead, we only save to the database in {@link
70 * #onDestroy()}, unless it's called for configuration changes.
71 */
72public class MailboxSettings extends PreferenceActivity {
73    private static final String EXTRA_FOLDERS_URI = "FOLDERS_URI";
74    private static final String EXTRA_INBOX_ID = "INBOX_ID";
75
76    private static final int FOLDERS_LOADER_ID = 0;
77    private Uri mFoldersUri;
78    private int mInboxId;
79    private final List<Folder> mFolders = new ArrayList<>();
80
81    /**
82     * Starts the activity
83     */
84    public static Intent getIntent(Context context, Uri foldersUri, Folder inbox) {
85        final Intent i = new Intent(context, MailboxSettings.class);
86        i.putExtra(EXTRA_FOLDERS_URI, foldersUri);
87        i.putExtra(EXTRA_INBOX_ID, inbox.id);
88        return i;
89    }
90
91    @Override
92    protected void onCreate(Bundle savedInstanceState) {
93        // This needs to happen before super.onCreate() since that calls onBuildHeaders()
94        mInboxId = getIntent().getIntExtra(EXTRA_INBOX_ID, -1);
95        mFoldersUri = getIntent().getParcelableExtra(EXTRA_FOLDERS_URI);
96
97        if (mFoldersUri != null) {
98            getLoaderManager().initLoader(FOLDERS_LOADER_ID, null,
99                    new MailboxSettingsFolderLoaderCallbacks());
100        }
101
102        super.onCreate(savedInstanceState);
103
104        // Always show "app up" as we expect our parent to be an Email activity.
105        ActionBar actionBar = getActionBar();
106        if (actionBar != null) {
107            actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
108        }
109    }
110
111    @Override
112    public void onBuildHeaders(List<Header> target) {
113        if (mFolders.isEmpty()) {
114            final Header dummy = new Header();
115            dummy.titleRes = R.string.mailbox_name_display_inbox;
116            dummy.fragment = MailboxSettingsFragment.class.getName();
117            dummy.fragmentArguments = MailboxSettingsFragment.getArguments(mInboxId);
118            target.add(dummy);
119        } else {
120            for (final Folder f : mFolders) {
121                final Header h = new Header();
122                if (!TextUtils.isEmpty(f.hierarchicalDesc)) {
123                    h.title = f.hierarchicalDesc;
124                } else {
125                    h.title = f.name;
126                }
127                h.fragment = MailboxSettingsFragment.class.getName();
128                h.fragmentArguments = MailboxSettingsFragment.getArguments(f.id);
129                if (f.id == mInboxId) {
130                    target.add(0, h);
131                } else {
132                    target.add(h);
133                }
134            }
135        }
136    }
137
138    @Override
139    protected boolean isValidFragment(String fragmentName) {
140        // Activity is not exported
141        return true;
142    }
143
144    @Override
145    public boolean onOptionsItemSelected(MenuItem item) {
146        if (item.getItemId() == android.R.id.home) {
147            onBackPressed();
148            return true;
149        }
150        return super.onOptionsItemSelected(item);
151    }
152
153    /**
154     * Setup the entries and entry values for the sync lookback preference
155     * @param context the caller's context
156     * @param pref a ListPreference to be set up
157     * @param maxLookback The maximum lookback allowed, or 0 if no max.
158     * @param showWithDefault Whether to show the version with default, or without.
159     */
160    public static void setupLookbackPreferenceOptions(final Context context,
161            final ListPreference pref, final int maxLookback, final boolean showWithDefault) {
162        final Resources resources = context.getResources();
163        // Load the complete list of entries/values
164        CharSequence[] entries;
165        CharSequence[] values;
166        final int offset;
167        if (showWithDefault) {
168            entries = resources.getTextArray(
169                    R.array.account_settings_mail_window_entries_with_default);
170            values = resources.getTextArray(
171                    R.array.account_settings_mail_window_values_with_default);
172            offset = 1;
173        } else {
174            entries = resources.getTextArray(R.array.account_settings_mail_window_entries);
175            values = resources.getTextArray(R.array.account_settings_mail_window_values);
176            offset = 0;
177        }
178        // If we have a maximum lookback policy, enforce it
179        if (maxLookback > 0) {
180            final int size = maxLookback + offset;
181            entries = Arrays.copyOf(entries, size);
182            values = Arrays.copyOf(values, size);
183        }
184        // Set up the preference
185        pref.setEntries(entries);
186        pref.setEntryValues(values);
187        pref.setSummary(pref.getEntry());
188    }
189
190    private class MailboxSettingsFolderLoaderCallbacks
191            implements LoaderManager.LoaderCallbacks<Cursor> {
192
193        @Override
194        public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
195            return new CursorLoader(MailboxSettings.this, mFoldersUri,
196                    UIProvider.FOLDERS_PROJECTION, null, null, null);
197        }
198
199        @Override
200        public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
201            if (cursor == null) {
202                return;
203            }
204            mFolders.clear();
205
206            while(cursor.moveToNext()) {
207                final Folder folder = new Folder(cursor);
208                if (!folder.supportsCapability(UIProvider.FolderCapabilities.IS_VIRTUAL) &&
209                        !folder.isTrash() && !folder.isDraft() && !folder.isOutbox()) {
210                    mFolders.add(folder);
211                }
212            }
213
214            invalidateHeaders();
215        }
216
217        @Override
218        public void onLoaderReset(Loader<Cursor> cursorLoader) {
219            mFolders.clear();
220        }
221    }
222
223    public static class MailboxSettingsFragment extends PreferenceFragment {
224        private static final String EXTRA_MAILBOX_ID = "MailboxId";
225
226        private static final String BUNDLE_MAILBOX = "MailboxSettings.mailbox";
227        private static final String BUNDLE_MAX_LOOKBACK = "MailboxSettings.maxLookback";
228        private static final String BUNDLE_SYNC_ENABLED_VALUE = "MailboxSettings.syncEnabled";
229        private static final String BUNDLE_SYNC_WINDOW_VALUE = "MailboxSettings.syncWindow";
230
231        private static final String PREF_SYNC_ENABLED_KEY = "sync_enabled";
232        private static final String PREF_SYNC_WINDOW_KEY = "sync_window";
233
234        private Mailbox mMailbox;
235        /** The maximum lookback allowed for this mailbox, or 0 if no max. */
236        private int mMaxLookback;
237
238        private CheckBoxPreference mSyncEnabledPref;
239        private ListPreference mSyncLookbackPref;
240
241        private static Bundle getArguments(long mailboxId) {
242            final Bundle b = new Bundle(1);
243            b.putLong(EXTRA_MAILBOX_ID, mailboxId);
244            return b;
245        }
246
247        public MailboxSettingsFragment() {}
248
249        @Override
250        public void onActivityCreated(Bundle savedInstanceState) {
251            super.onActivityCreated(savedInstanceState);
252            final long mailboxId = getArguments().getLong(EXTRA_MAILBOX_ID, Mailbox.NO_MAILBOX);
253            if (mailboxId == Mailbox.NO_MAILBOX) {
254                getActivity().finish();
255            }
256
257            addPreferencesFromResource(R.xml.mailbox_preferences);
258
259            mSyncEnabledPref = (CheckBoxPreference) findPreference(PREF_SYNC_ENABLED_KEY);
260            mSyncLookbackPref = (ListPreference) findPreference(PREF_SYNC_WINDOW_KEY);
261
262            mSyncLookbackPref.setOnPreferenceChangeListener(mPreferenceChanged);
263
264            if (savedInstanceState != null) {
265                mMailbox = savedInstanceState.getParcelable(BUNDLE_MAILBOX);
266                mMaxLookback = savedInstanceState.getInt(BUNDLE_MAX_LOOKBACK);
267                mSyncEnabledPref
268                        .setChecked(savedInstanceState.getBoolean(BUNDLE_SYNC_ENABLED_VALUE));
269                mSyncLookbackPref.setValue(savedInstanceState.getString(BUNDLE_SYNC_WINDOW_VALUE));
270                onDataLoaded();
271            } else {
272                // Make them disabled until we load data
273                enablePreferences(false);
274                getLoaderManager().initLoader(0, getArguments(), new MailboxLoaderCallbacks());
275            }
276        }
277
278        private void enablePreferences(boolean enabled) {
279            mSyncEnabledPref.setEnabled(enabled);
280            mSyncLookbackPref.setEnabled(enabled);
281        }
282
283        @Override
284        public void onSaveInstanceState(@NonNull Bundle outState) {
285            super.onSaveInstanceState(outState);
286            outState.putParcelable(BUNDLE_MAILBOX, mMailbox);
287            outState.putInt(BUNDLE_MAX_LOOKBACK, mMaxLookback);
288            outState.putBoolean(BUNDLE_SYNC_ENABLED_VALUE, mSyncEnabledPref.isChecked());
289            outState.putString(BUNDLE_SYNC_WINDOW_VALUE, mSyncLookbackPref.getValue());
290        }
291
292        /**
293         * We save all the settings in onDestroy, *unless it's for configuration changes*.
294         */
295        @Override
296        public void onDestroy() {
297            super.onDestroy();
298            if (!getActivity().isChangingConfigurations()) {
299                saveToDatabase();
300            }
301        }
302
303        private static class MailboxLoader extends MailAsyncTaskLoader<Map<String, Object>> {
304            /** Projection for loading an account's policy key. */
305            private static final String[] POLICY_KEY_PROJECTION =
306                    { AccountColumns.POLICY_KEY };
307            private static final int POLICY_KEY_COLUMN = 0;
308
309            /** Projection for loading the max email lookback. */
310            private static final String[] MAX_EMAIL_LOOKBACK_PROJECTION =
311                    { Policy.MAX_EMAIL_LOOKBACK };
312            private static final int MAX_EMAIL_LOOKBACK_COLUMN = 0;
313
314            public static final String RESULT_KEY_MAILBOX = "mailbox";
315            public static final String RESULT_KEY_MAX_LOOKBACK = "maxLookback";
316
317            private final long mMailboxId;
318
319            private MailboxLoader(Context context, long mailboxId) {
320                super(context);
321                mMailboxId = mailboxId;
322            }
323
324            @Override
325            public Map<String, Object> loadInBackground() {
326                final Map<String, Object> result = new HashMap<>();
327
328                final Mailbox mailbox = Mailbox.restoreMailboxWithId(getContext(), mMailboxId);
329                result.put(RESULT_KEY_MAILBOX, mailbox);
330                result.put(RESULT_KEY_MAX_LOOKBACK, 0);
331
332                if (mailbox == null) {
333                    return result;
334                }
335
336                // Get the max lookback from our policy, if we have one.
337                final Long policyKey = Utility.getFirstRowLong(getContext(),
338                        ContentUris.withAppendedId(Account.CONTENT_URI, mailbox.mAccountKey),
339                        POLICY_KEY_PROJECTION, null, null, null, POLICY_KEY_COLUMN);
340                if (policyKey == null) {
341                    // No policy, nothing to look up.
342                    return result;
343                }
344
345                final int maxLookback = Utility.getFirstRowInt(getContext(),
346                        ContentUris.withAppendedId(Policy.CONTENT_URI, policyKey),
347                        MAX_EMAIL_LOOKBACK_PROJECTION, null, null, null,
348                        MAX_EMAIL_LOOKBACK_COLUMN, 0);
349                result.put(RESULT_KEY_MAX_LOOKBACK, maxLookback);
350
351                return result;
352            }
353
354            @Override
355            protected void onDiscardResult(Map<String, Object> result) {}
356        }
357
358        private class MailboxLoaderCallbacks
359                implements LoaderManager.LoaderCallbacks<Map<String, Object>> {
360            @Override
361            public Loader<Map<String, Object>> onCreateLoader(int id, Bundle args) {
362                final long mailboxId = args.getLong(EXTRA_MAILBOX_ID);
363                return new MailboxLoader(getActivity(), mailboxId);
364            }
365
366            @Override
367            public void onLoadFinished(Loader<Map<String, Object>> loader,
368                    Map<String, Object> data) {
369                final Mailbox mailbox = (Mailbox)
370                        (data == null ? null : data.get(MailboxLoader.RESULT_KEY_MAILBOX));
371                if (mailbox == null) {
372                    getActivity().finish();
373                    return;
374                }
375
376                mMailbox = mailbox;
377                mMaxLookback = (Integer) data.get(MailboxLoader.RESULT_KEY_MAX_LOOKBACK);
378
379                mSyncEnabledPref.setChecked(mMailbox.mSyncInterval != 0);
380                mSyncLookbackPref.setValue(String.valueOf(mMailbox.mSyncLookback));
381                onDataLoaded();
382                if (mMailbox.mType != Mailbox.TYPE_DRAFTS) {
383                    enablePreferences(true);
384                }
385            }
386
387            @Override
388            public void onLoaderReset(Loader<Map<String, Object>> loader) {}
389        }
390
391        /**
392         * Called when {@link #mMailbox} is loaded (either by the loader or from the saved state).
393         */
394        private void onDataLoaded() {
395            Preconditions.checkNotNull(mMailbox);
396
397            // Update the title with the mailbox name.
398            final ActionBar actionBar = getActivity().getActionBar();
399            final String mailboxName = mMailbox.mDisplayName;
400            if (actionBar != null) {
401                actionBar.setTitle(mailboxName);
402                actionBar.setSubtitle(getString(R.string.mailbox_settings_activity_title));
403            } else {
404                getActivity().setTitle(
405                        getString(R.string.mailbox_settings_activity_title_with_mailbox,
406                                mailboxName));
407            }
408
409            MailboxSettings.setupLookbackPreferenceOptions(getActivity(), mSyncLookbackPref,
410                    mMaxLookback, true);
411        }
412
413
414        private final OnPreferenceChangeListener mPreferenceChanged =
415                new OnPreferenceChangeListener() {
416            @Override
417            public boolean onPreferenceChange(Preference preference, Object newValue) {
418                mSyncLookbackPref.setValue((String) newValue);
419                mSyncLookbackPref.setSummary(mSyncLookbackPref.getEntry());
420                return false;
421            }
422        };
423
424        /**
425         * Save changes to the database.
426         *
427         * Note it's called from {@link #onDestroy()}, which is called on the UI thread where we're
428         * not allowed to touch the database, so it uses {@link EmailAsyncTask} to do the save on a
429         * bg thread. This unfortunately means there's a chance that the app gets killed before the
430         * save is finished.
431         */
432        private void saveToDatabase() {
433            if (mMailbox == null) {
434                // We haven't loaded yet, nothing to save.
435                return;
436            }
437            final int syncInterval = mSyncEnabledPref.isChecked() ? 1 : 0;
438            final int syncLookback = Integer.valueOf(mSyncLookbackPref.getValue());
439
440            final boolean syncIntervalChanged = syncInterval != mMailbox.mSyncInterval;
441            final boolean syncLookbackChanged = syncLookback != mMailbox.mSyncLookback;
442
443            // Only save if a preference has changed value.
444            if (!syncIntervalChanged && !syncLookbackChanged) {
445                return;
446            }
447
448            LogUtils.i(Logging.LOG_TAG, "Saving mailbox settings...");
449            enablePreferences(false);
450
451            final long id = mMailbox.mId;
452            final Context context = getActivity().getApplicationContext();
453
454            new EmailAsyncTask<Void, Void, Void> (null /* no cancel */) {
455                @Override
456                protected Void doInBackground(Void... params) {
457                    final ContentValues cv = new ContentValues(2);
458                    final Uri uri;
459                    if (syncIntervalChanged) {
460                        cv.put(MailboxColumns.SYNC_INTERVAL, syncInterval);
461                    }
462                    if (syncLookbackChanged) {
463                        cv.put(MailboxColumns.SYNC_LOOKBACK, syncLookback);
464                    }
465                    uri = ContentUris.withAppendedId(Mailbox.CONTENT_URI, id);
466                    context.getContentResolver().update(uri, cv, null, null);
467
468                    LogUtils.i(Logging.LOG_TAG, "Saved: " + uri);
469                    return null;
470                }
471            }.executeSerial((Void [])null);
472        }
473    }
474}
475