summaryrefslogtreecommitdiffstats
path: root/src/com/android/email/activity/setup/AccountSettingsFragment.java
blob: 26e476455681ff17ea3940452b12324f54a44df4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.email.activity.setup;

import android.accounts.AccountManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.LoaderManager;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Loader;
import android.content.res.Resources;
import android.database.Cursor;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.os.Vibrator;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceScreen;
import android.provider.CalendarContract;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;

import com.android.email.R;
import com.android.email.SecurityPolicy;
import com.android.email.provider.EmailProvider;
import com.android.email.provider.FolderPickerActivity;
import com.android.email.service.EmailServiceUtils;
import com.android.email.service.EmailServiceUtils.EmailServiceInfo;
import com.android.emailcommon.provider.Account;
import com.android.emailcommon.provider.EmailContent;
import com.android.emailcommon.provider.EmailContent.AccountColumns;
import com.android.emailcommon.provider.Mailbox;
import com.android.emailcommon.provider.Policy;
import com.android.emailcommon.service.EmailServiceProxy;
import com.android.mail.preferences.AccountPreferences;
import com.android.mail.preferences.FolderPreferences;
import com.android.mail.preferences.FolderPreferences.NotificationLight;
import com.android.mail.preferences.notifications.FolderNotificationLightPreference;
import com.android.mail.providers.Folder;
import com.android.mail.providers.UIProvider;
import com.android.mail.ui.MailAsyncTaskLoader;
import com.android.mail.ui.settings.MailAccountPrefsFragment;
import com.android.mail.ui.settings.PublicPreferenceActivity;
import com.android.mail.ui.settings.SettingsUtils;
import com.android.mail.utils.ContentProviderTask.UpdateTask;
import com.android.mail.utils.LogUtils;
import com.android.mail.utils.NotificationUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Fragment containing the main logic for account settings.  This also calls out to other
 * fragments for server settings.
 *
 * TODO: Can we defer calling addPreferencesFromResource() until after we load the account?  This
 *       could reduce flicker.
 */
public class AccountSettingsFragment extends MailAccountPrefsFragment
        implements Preference.OnPreferenceChangeListener {

    private static final String ARG_ACCOUNT_ID = "account_id";

    public static final String PREFERENCE_DESCRIPTION = "account_description";
    private static final String PREFERENCE_NAME = "account_name";
    private static final String PREFERENCE_SIGNATURE = "account_signature";
    private static final String PREFERENCE_QUICK_RESPONSES = "account_quick_responses";
    private static final String PREFERENCE_FREQUENCY = "account_check_frequency";
    private static final String PREFERENCE_SYNC_WINDOW = "account_sync_window";
    private static final String PREFERENCE_SYNC_SETTINGS = MailboxSettings.PREFERENCE_SYNC_SETTINGS;
    private static final String PREFERENCE_SYNC_EMAIL = "account_sync_email";
    private static final String PREFERENCE_SYNC_CONTACTS = "account_sync_contacts";
    private static final String PREFERENCE_SYNC_CALENDAR = "account_sync_calendar";
    private static final String PREFERENCE_BACKGROUND_ATTACHMENTS =
            "account_background_attachments";
    private static final String PREFERENCE_PER_FOLDER_NOTIFICATIONS =
            MailboxSettings.PREFERENCE_PER_FOLDER_NOTIFICATIONS;
    private static final String PREFERENCE_CATEGORY_DATA_USAGE = "data_usage";
    private static final String PREFERENCE_CATEGORY_NOTIFICATIONS = "account_notifications";
    private static final String PREFERENCE_CATEGORY_SERVER = "account_servers";
    private static final String PREFERENCE_CATEGORY_POLICIES = "account_policies";
    @SuppressWarnings("unused") // temporarily unused pending policy UI
    private static final String PREFERENCE_POLICIES_ENFORCED = "policies_enforced";
    @SuppressWarnings("unused") // temporarily unused pending policy UI
    private static final String PREFERENCE_POLICIES_UNSUPPORTED = "policies_unsupported";
    private static final String PREFERENCE_POLICIES_RETRY_ACCOUNT = "policies_retry_account";
    private static final String PREFERENCE_INCOMING = "incoming";
    private static final String PREFERENCE_OUTGOING = "outgoing";

    private static final String PREFERENCE_SYSTEM_FOLDERS = "system_folders";
    private static final String PREFERENCE_SYSTEM_FOLDERS_TRASH = "system_folders_trash";
    private static final String PREFERENCE_SYSTEM_FOLDERS_SENT = "system_folders_sent";

    private static final String SAVESTATE_SYNC_INTERVALS = "savestate_sync_intervals";
    private static final String SAVESTATE_SYNC_INTERVAL_STRINGS = "savestate_sync_interval_strings";

    // Request code to start different activities.
    private static final int RINGTONE_REQUEST_CODE = 0;

    // Message codes
    private static final int MSG_DELETE_ACCOUNT = 0;

    private EditTextPreference mAccountDescription;
    private EditTextPreference mAccountName;
    private EditTextPreference mAccountSignature;
    private ListPreference mCheckFrequency;
    private ListPreference mSyncWindow;
    private Preference mSyncSettings;
    private CheckBoxPreference mInboxVibrate;
    private Preference mInboxRingtone;
    private FolderNotificationLightPreference mInboxLights;
    private Preference mPerFolderNotification;

    private Context mContext;
    private Handler mHandler;

    private Account mAccount;
    private com.android.mail.providers.Account mUiAccount;
    private EmailServiceInfo mServiceInfo;
    private Folder mInboxFolder;

    private Ringtone mRingtone;

    private MenuItem mDeleteAccountItem;

    private final Callback mHandlerCallback = new Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == MSG_DELETE_ACCOUNT) {
                deleteAccount();
            }
            return false;
        }
    };

    /**
     * This may be null if the account exists but the inbox has not yet been created in the database
     * (waiting for initial sync)
     */
    private FolderPreferences mInboxFolderPreferences;

    // The email of the account being edited
    private String mAccountEmail;

    /**
     * If launching with an email address, use this method to build the arguments.
     */
    public static Bundle buildArguments(final String email) {
        final Bundle b = new Bundle(1);
        b.putString(ARG_ACCOUNT_EMAIL, email);
        return b;
    }

    /**
     * If launching with an account ID, use this method to build the arguments.
     */
    public static Bundle buildArguments(final long accountId) {
        final Bundle b = new Bundle(1);
        b.putLong(ARG_ACCOUNT_ID, accountId);
        return b;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }

    /**
     * Called to do initial creation of a fragment.  This is called after
     * {@link #onAttach(Activity)} and before {@link #onActivityCreated(Bundle)}.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mHandler = new Handler(mHandlerCallback);

        setHasOptionsMenu(true);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.account_settings_preferences);

        if (!getResources().getBoolean(R.bool.quickresponse_supported)) {
            final Preference quickResponsePref = findPreference(PREFERENCE_QUICK_RESPONSES);
            if (quickResponsePref != null) {
                getPreferenceScreen().removePreference(quickResponsePref);
            }
        }

        // Start loading the account data, if provided in the arguments
        // If not, activity must call startLoadingAccount() directly
        Bundle b = getArguments();
        if (b != null) {
            mAccountEmail = b.getString(ARG_ACCOUNT_EMAIL);
        }
        if (savedInstanceState != null) {
            // We won't know what the correct set of sync interval values and strings are until
            // our loader completes. The problem is, that if the sync frequency chooser is
            // displayed when the screen rotates, it reinitializes it to the defaults, and doesn't
            // correct it after the loader finishes again. See b/13624066
            // To work around this, we'll save the current set of sync interval values and strings,
            // in onSavedInstanceState, and restore them here.
            final CharSequence [] syncIntervalStrings =
                    savedInstanceState.getCharSequenceArray(SAVESTATE_SYNC_INTERVAL_STRINGS);
            final CharSequence [] syncIntervals =
                    savedInstanceState.getCharSequenceArray(SAVESTATE_SYNC_INTERVALS);
            mCheckFrequency = (ListPreference) findPreference(PREFERENCE_FREQUENCY);
            fillCheckFrecuency(syncIntervalStrings, syncIntervals);
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outstate) {
        super.onSaveInstanceState(outstate);
        if (mCheckFrequency != null) {
            outstate.putCharSequenceArray(SAVESTATE_SYNC_INTERVAL_STRINGS,
                    mCheckFrequency.getEntries());
            outstate.putCharSequenceArray(SAVESTATE_SYNC_INTERVALS,
                    mCheckFrequency.getEntryValues());
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final Bundle args = new Bundle(1);
        if (!TextUtils.isEmpty(mAccountEmail)) {
            args.putString(AccountLoaderCallbacks.ARG_ACCOUNT_EMAIL, mAccountEmail);
        } else {
            args.putLong(AccountLoaderCallbacks.ARG_ACCOUNT_ID,
                    getArguments().getLong(ARG_ACCOUNT_ID, -1));
        }
        getLoaderManager().initLoader(0, args, new AccountLoaderCallbacks(getActivity()));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case RINGTONE_REQUEST_CODE:
                if (resultCode == Activity.RESULT_OK && data != null) {
                    Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                    setRingtone(uri);
                }
                break;
        }
    }

    /**
     * Sets the current ringtone.
     */
    private void setRingtone(Uri ringtone) {
        if (ringtone != null) {
            mInboxFolderPreferences.setNotificationRingtoneUri(ringtone.toString());
            mRingtone = RingtoneManager.getRingtone(getActivity(), ringtone);
        } else {
            // Null means silent was selected.
            mInboxFolderPreferences.setNotificationRingtoneUri("");
            mRingtone = null;
        }

        setRingtoneSummary();
    }

    private void setRingtoneSummary() {
        final String summary = mRingtone != null ? mRingtone.getTitle(mContext)
                : mContext.getString(R.string.silent_ringtone);

        mInboxRingtone.setSummary(summary);
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
            @NonNull Preference preference) {
        final String key = preference.getKey();
        if (key.equals(PREFERENCE_SYNC_SETTINGS) ||
                key.equals(PREFERENCE_PER_FOLDER_NOTIFICATIONS)) {
            startActivity(MailboxSettings.getIntent(getActivity(), mUiAccount.fullFolderListUri,
                    mInboxFolder, key));
            return true;
        } else {
            return super.onPreferenceTreeClick(preferenceScreen, preference);
        }
    }

    /**
     * Listen to all preference changes in this class.
     * @param preference The changed Preference
     * @param newValue The new value of the Preference
     * @return True to update the state of the Preference with the new value
     */
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        // Can't use a switch here. Falling back to a giant conditional.
        final String key = preference.getKey();
        final ContentValues cv = new ContentValues(1);
        if (key.equals(PREFERENCE_DESCRIPTION)){
            String summary = newValue.toString().trim();
            if (TextUtils.isEmpty(summary)) {
                summary = mUiAccount.getEmailAddress();
            }
            mAccountDescription.setSummary(summary);
            mAccountDescription.setText(summary);
            cv.put(AccountColumns.DISPLAY_NAME, summary);
        } else if (key.equals(PREFERENCE_NAME)) {
            final String summary = newValue.toString().trim();
            if (!TextUtils.isEmpty(summary)) {
                mAccountName.setSummary(summary);
                mAccountName.setText(summary);
                cv.put(AccountColumns.SENDER_NAME, summary);
            }
        } else if (key.equals(PREFERENCE_SIGNATURE)) {
            // Clean up signature if it's only whitespace (which is easy to do on a
            // soft keyboard) but leave whitespace in place otherwise, to give the user
            // maximum flexibility, e.g. the ability to indent
            String signature = newValue.toString();
            if (signature.trim().isEmpty()) {
                signature = "";
            }
            mAccountSignature.setText(signature);
            SettingsUtils.updatePreferenceSummary(mAccountSignature, signature,
                    R.string.preferences_signature_summary_not_set);
            cv.put(AccountColumns.SIGNATURE, signature);
        } else if (key.equals(PREFERENCE_FREQUENCY)) {
            final String summary = newValue.toString();
            final int index = mCheckFrequency.findIndexOfValue(summary);
            mCheckFrequency.setSummary(mCheckFrequency.getEntries()[index]);
            mCheckFrequency.setValue(summary);
            if (mServiceInfo.syncContacts || mServiceInfo.syncCalendar) {
                // This account allows syncing of contacts and/or calendar, so we will always have
                // separate preferences to enable or disable syncing of email, contacts, and
                // calendar.
                // The "sync frequency" preference really just needs to control the frequency value
                // in our database.
                cv.put(AccountColumns.SYNC_INTERVAL, Integer.parseInt(summary));
            } else {
                // This account only syncs email (not contacts or calendar), which means that we
                // will hide the preference to turn syncing on and off. In this case, we want the
                // sync frequency preference to also control whether or not syncing is enabled at
                // all. If sync is turned off, we will display "sync never" regardless of what the
                // numeric value we have stored says.
                final android.accounts.Account androidAcct = new android.accounts.Account(
                        mAccount.mEmailAddress, mServiceInfo.accountType);
                if (Integer.parseInt(summary) == Account.CHECK_INTERVAL_NEVER) {
                    // Disable syncing from the account manager.
                    ContentResolver.setSyncAutomatically(androidAcct, EmailContent.AUTHORITY,
                            false);
                } else {
                    // Enable syncing from the account manager.
                    ContentResolver.setSyncAutomatically(androidAcct, EmailContent.AUTHORITY,
                            true);
                }
                cv.put(AccountColumns.SYNC_INTERVAL, Integer.parseInt(summary));
            }
        } else if (key.equals(PREFERENCE_SYNC_WINDOW)) {
            final String summary = newValue.toString();
            int index = mSyncWindow.findIndexOfValue(summary);
            mSyncWindow.setSummary(mSyncWindow.getEntries()[index]);
            mSyncWindow.setValue(summary);
            cv.put(AccountColumns.SYNC_LOOKBACK, Integer.parseInt(summary));
        } else if (key.equals(PREFERENCE_SYNC_EMAIL)) {
            final android.accounts.Account androidAcct = new android.accounts.Account(
                    mAccount.mEmailAddress, mServiceInfo.accountType);
            ContentResolver.setSyncAutomatically(androidAcct, EmailContent.AUTHORITY,
                    (Boolean) newValue);
            loadSettings();
        } else if (key.equals(PREFERENCE_SYNC_CONTACTS)) {
            final android.accounts.Account androidAcct = new android.accounts.Account(
                    mAccount.mEmailAddress, mServiceInfo.accountType);
            ContentResolver.setSyncAutomatically(androidAcct, ContactsContract.AUTHORITY,
                    (Boolean) newValue);
            loadSettings();
        } else if (key.equals(PREFERENCE_SYNC_CALENDAR)) {
            final android.accounts.Account androidAcct = new android.accounts.Account(
                    mAccount.mEmailAddress, mServiceInfo.accountType);
            ContentResolver.setSyncAutomatically(androidAcct, CalendarContract.AUTHORITY,
                    (Boolean) newValue);
            loadSettings();
        } else if (key.equals(PREFERENCE_BACKGROUND_ATTACHMENTS)) {
            int newFlags = mAccount.getFlags() & ~(Account.FLAGS_BACKGROUND_ATTACHMENTS);

            newFlags |= (Boolean) newValue ?
                    Account.FLAGS_BACKGROUND_ATTACHMENTS : 0;

            cv.put(AccountColumns.FLAGS, newFlags);
        } else if (FolderPreferences.PreferenceKeys.NOTIFICATIONS_ENABLED.equals(key)) {
            mInboxFolderPreferences.setNotificationsEnabled((Boolean) newValue);
            return true;
        } else if (FolderPreferences.PreferenceKeys.NOTIFICATION_VIBRATE.equals(key)) {
            final boolean vibrateSetting = (Boolean) newValue;
            mInboxVibrate.setChecked(vibrateSetting);
            mInboxFolderPreferences.setNotificationVibrateEnabled(vibrateSetting);
            return true;
        } else if (FolderPreferences.PreferenceKeys.NOTIFICATION_LIGHTS.equals(key)) {
            final String LightsSettings = (String) newValue;
            NotificationLight notificationLight = NotificationLight.fromStringPref(LightsSettings);
            updateNotificationLight(notificationLight);
            mInboxFolderPreferences.setNotificationLights(notificationLight);
            return true;
        } else if (FolderPreferences.PreferenceKeys.NOTIFICATION_RINGTONE.equals(key)) {
            return true;
        } else {
            // Default behavior, just indicate that the preferences were written
            LogUtils.d(LogUtils.TAG, "Unknown preference key %s", key);
            return true;
        }
        if (cv.size() > 0) {
            new UpdateTask().run(mContext.getContentResolver(), mAccount.getUri(), cv, null, null);
            EmailProvider.setServicesEnabledAsync(mContext);
        }
        return false;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();

        // Delete account item
        mDeleteAccountItem = menu.add(Menu.NONE, Menu.NONE, 0, R.string.delete_account);
        mDeleteAccountItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);

        inflater.inflate(R.menu.settings_fragment_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.equals(mDeleteAccountItem)) {
            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(mContext);
            alertBuilder.setTitle(R.string.delete_account);
            String msg = getString(R.string.delete_account_confirmation_msg, mAccountEmail);
            alertBuilder.setMessage(msg);
            alertBuilder.setCancelable(true);
            final DialogInterface.OnClickListener cb = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    mHandler.dispatchMessage(Message.obtain(mHandler, MSG_DELETE_ACCOUNT));
                }
            };
            alertBuilder.setPositiveButton(android.R.string.ok, cb);
            alertBuilder.setNegativeButton(android.R.string.cancel, null);
            alertBuilder.create().show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



    /**
     * Async task loader to load account in order to view/edit it
     */
    private static class AccountLoader extends MailAsyncTaskLoader<Map<String, Object>> {
        public static final String RESULT_KEY_ACCOUNT = "account";
        private static final String RESULT_KEY_UIACCOUNT_CURSOR = "uiAccountCursor";
        public static final String RESULT_KEY_UIACCOUNT = "uiAccount";
        public static final String RESULT_KEY_INBOX = "inbox";

        private final ForceLoadContentObserver mObserver;
        private final String mAccountEmail;
        private final long mAccountId;

        private AccountLoader(Context context, String accountEmail, long accountId) {
            super(context);
            mObserver = new ForceLoadContentObserver();
            mAccountEmail = accountEmail;
            mAccountId = accountId;
        }

        @Override
        public Map<String, Object> loadInBackground() {
            final Map<String, Object> map = new HashMap<>();

            final Account account;
            if (!TextUtils.isEmpty(mAccountEmail)) {
                account = Account.restoreAccountWithAddress(getContext(), mAccountEmail, mObserver);
            } else {
                account = Account.restoreAccountWithId(getContext(), mAccountId, mObserver);
            }
            if (account == null) {
                return map;
            }

            map.put(RESULT_KEY_ACCOUNT, account);

            // We don't monitor these for changes, but they probably won't change in any meaningful
            // way
            account.getOrCreateHostAuthRecv(getContext());
            account.getOrCreateHostAuthSend(getContext());

            if (account.mHostAuthRecv == null) {
                return map;
            }

            account.mPolicy =
                    Policy.restorePolicyWithId(getContext(), account.mPolicyKey, mObserver);

            final Cursor uiAccountCursor = getContext().getContentResolver().query(
                    EmailProvider.uiUri("uiaccount", account.getId()),
                    UIProvider.ACCOUNTS_PROJECTION,
                    null, null, null);

            if (uiAccountCursor != null) {
                map.put(RESULT_KEY_UIACCOUNT_CURSOR, uiAccountCursor);
                uiAccountCursor.registerContentObserver(mObserver);
            } else {
                return map;
            }

            if (!uiAccountCursor.moveToFirst()) {
                return map;
            }

            final com.android.mail.providers.Account uiAccount =
                    com.android.mail.providers.Account.builder().buildFrom(uiAccountCursor);

            map.put(RESULT_KEY_UIACCOUNT, uiAccount);

            final Cursor folderCursor = getContext().getContentResolver().query(
                    uiAccount.settings.defaultInbox, UIProvider.FOLDERS_PROJECTION, null, null,
                    null);

            final Folder inbox;
            try {
                if (folderCursor != null && folderCursor.moveToFirst()) {
                    inbox = new Folder(folderCursor);
                } else {
                    return map;
                }
            } finally {
                if (folderCursor != null) {
                    folderCursor.close();
                }
            }

            map.put(RESULT_KEY_INBOX, inbox);
            return map;
        }

        @Override
        protected void onDiscardResult(Map<String, Object> result) {
            final Account account = (Account) result.get(RESULT_KEY_ACCOUNT);
            if (account != null) {
                if (account.mPolicy != null) {
                    account.mPolicy.close(getContext());
                }
                account.close(getContext());
            }
            final Cursor uiAccountCursor = (Cursor) result.get(RESULT_KEY_UIACCOUNT_CURSOR);
            if (uiAccountCursor != null) {
                uiAccountCursor.close();
            }
        }
    }

    private class AccountLoaderCallbacks
            implements LoaderManager.LoaderCallbacks<Map<String, Object>> {
        public static final String ARG_ACCOUNT_EMAIL = "accountEmail";
        public static final String ARG_ACCOUNT_ID = "accountId";
        private final Context mContext;

        private AccountLoaderCallbacks(Context context) {
            mContext = context;
        }

        @Override
        public void onLoadFinished(Loader<Map<String, Object>> loader, Map<String, Object> data) {
            final Activity activity = getActivity();
            if (activity == null) {
                return;
            }
            if (data == null) {
                activity.finish();
                return;
            }

            mUiAccount = (com.android.mail.providers.Account)
                    data.get(AccountLoader.RESULT_KEY_UIACCOUNT);
            mAccount = (Account) data.get(AccountLoader.RESULT_KEY_ACCOUNT);

            if (mAccount != null && (mAccount.mFlags & Account.FLAGS_SECURITY_HOLD) != 0) {
                final Intent i = AccountSecurity.actionUpdateSecurityIntent(mContext,
                        mAccount.getId(), true);
                mContext.startActivity(i);
                activity.finish();
                return;
            }

            mInboxFolder = (Folder) data.get(AccountLoader.RESULT_KEY_INBOX);

            if (mUiAccount == null || mAccount == null) {
                activity.finish();
                return;
            }

            mServiceInfo =
                    EmailServiceUtils.getServiceInfo(mContext, mAccount.getProtocol(mContext));

            if (mInboxFolder == null) {
                mInboxFolderPreferences = null;
            } else {
                mInboxFolderPreferences = new FolderPreferences(mContext,
                        mUiAccount.getEmailAddress(), mInboxFolder, true);
            }
            loadSettings();
        }

        @Override
        public Loader<Map<String, Object>> onCreateLoader(int id, Bundle args) {
            return new AccountLoader(mContext, args.getString(ARG_ACCOUNT_EMAIL),
                    args.getLong(ARG_ACCOUNT_ID));
        }

        @Override
        public void onLoaderReset(Loader<Map<String, Object>> loader) {}
    }

    /**
     * From a Policy, create and return an ArrayList of Strings that describe (simply) those
     * policies that are supported by the OS.  At the moment, the strings are simple (e.g.
     * "password required"); we should probably add more information (# characters, etc.), though
     */
    @SuppressWarnings("unused") // temporarily unused pending policy UI
    private ArrayList<String> getSystemPoliciesList(Policy policy) {
        Resources res = mContext.getResources();
        ArrayList<String> policies = new ArrayList<>();
        if (policy.mPasswordMode != Policy.PASSWORD_MODE_NONE) {
            policies.add(res.getString(R.string.policy_require_password));
        }
        if (policy.mPasswordHistory > 0) {
            policies.add(res.getString(R.string.policy_password_history));
        }
        if (policy.mPasswordExpirationDays > 0) {
            policies.add(res.getString(R.string.policy_password_expiration));
        }
        if (policy.mMaxScreenLockTime > 0) {
            policies.add(res.getString(R.string.policy_screen_timeout));
        }
        if (policy.mDontAllowCamera) {
            policies.add(res.getString(R.string.policy_dont_allow_camera));
        }
        if (policy.mMaxEmailLookback != 0) {
            policies.add(res.getString(R.string.policy_email_age));
        }
        if (policy.mMaxCalendarLookback != 0) {
            policies.add(res.getString(R.string.policy_calendar_age));
        }
        return policies;
    }

    @SuppressWarnings("unused") // temporarily unused pending policy UI
    private void setPolicyListSummary(ArrayList<String> policies, String policiesToAdd,
            String preferenceName) {
        Policy.addPolicyStringToList(policiesToAdd, policies);
        if (policies.size() > 0) {
            Preference p = findPreference(preferenceName);
            StringBuilder sb = new StringBuilder();
            for (String desc: policies) {
                sb.append(desc);
                sb.append('\n');
            }
            p.setSummary(sb.toString());
        }
    }

    /**
     * Load account data into preference UI. This must be called on the main thread.
     */
    private void loadSettings() {
        final AccountPreferences accountPreferences =
                new AccountPreferences(mContext, mUiAccount.getEmailAddress());
        if (mInboxFolderPreferences != null) {
            NotificationUtils.moveNotificationSetting(
                    accountPreferences, mInboxFolderPreferences);
        }

        final String protocol = mAccount.getProtocol(mContext);
        if (mServiceInfo == null) {
            LogUtils.e(LogUtils.TAG,
                    "Could not find service info for account %d with protocol %s", mAccount.mId,
                    protocol);
            getActivity().onBackPressed();
            // TODO: put up some sort of dialog/toast here to tell the user something went wrong
            return;
        }
        final android.accounts.Account androidAcct = mUiAccount.getAccountManagerAccount();

        mAccountDescription = (EditTextPreference) findPreference(PREFERENCE_DESCRIPTION);
        mAccountDescription.setSummary(mAccount.getDisplayName());
        mAccountDescription.setText(mAccount.getDisplayName());
        mAccountDescription.setOnPreferenceChangeListener(this);

        mAccountName = (EditTextPreference) findPreference(PREFERENCE_NAME);
        String senderName = mUiAccount.getSenderName();
        // In rare cases, sendername will be null;  Change this to empty string to avoid NPE's
        if (senderName == null) {
            senderName = "";
        }
        mAccountName.setSummary(senderName);
        mAccountName.setText(senderName);
        mAccountName.setOnPreferenceChangeListener(this);

        final String accountSignature = mAccount.getSignature();
        mAccountSignature = (EditTextPreference) findPreference(PREFERENCE_SIGNATURE);
        mAccountSignature.setText(accountSignature);
        mAccountSignature.setOnPreferenceChangeListener(this);
        SettingsUtils.updatePreferenceSummary(mAccountSignature, accountSignature,
                R.string.preferences_signature_summary_not_set);

        mCheckFrequency = (ListPreference) findPreference(PREFERENCE_FREQUENCY);
        fillCheckFrecuency(mServiceInfo.syncIntervalStrings, mServiceInfo.syncIntervals);
        if (mServiceInfo.syncContacts || mServiceInfo.syncCalendar) {
            // This account allows syncing of contacts and/or calendar, so we will always have
            // separate preferences to enable or disable syncing of email, contacts, and calendar.
            // The "sync frequency" preference really just needs to control the frequency value
            // in our database.
            mCheckFrequency.setValue(String.valueOf(mAccount.getSyncInterval()));
        } else {
            // This account only syncs email (not contacts or calendar), which means that we will
            // hide the preference to turn syncing on and off. In this case, we want the sync
            // frequency preference to also control whether or not syncing is enabled at all. If
            // sync is turned off, we will display "sync never" regardless of what the numeric
            // value we have stored says.
            boolean synced = ContentResolver.getSyncAutomatically(androidAcct,
                    EmailContent.AUTHORITY);
            if (synced) {
                mCheckFrequency.setValue(String.valueOf(mAccount.getSyncInterval()));
            } else {
                mCheckFrequency.setValue(String.valueOf(Account.CHECK_INTERVAL_NEVER));
            }
        }
        mCheckFrequency.setSummary(mCheckFrequency.getEntry());
        mCheckFrequency.setOnPreferenceChangeListener(this);

        final Preference quickResponsePref = findPreference(PREFERENCE_QUICK_RESPONSES);
        if (quickResponsePref != null) {
            quickResponsePref.setOnPreferenceClickListener(
                    new Preference.OnPreferenceClickListener() {
                        @Override
                        public boolean onPreferenceClick(Preference preference) {
                            onEditQuickResponses(mUiAccount);
                            return true;
                        }
                    });
        }

        // Add check window preference
        final PreferenceCategory dataUsageCategory =
                (PreferenceCategory) findPreference(PREFERENCE_CATEGORY_DATA_USAGE);

        if (mServiceInfo.offerLookback) {
            if (mSyncWindow == null) {
                mSyncWindow = new ListPreference(mContext);
                mSyncWindow.setKey(PREFERENCE_SYNC_WINDOW);
                dataUsageCategory.addPreference(mSyncWindow);
            }
            mSyncWindow.setTitle(R.string.account_setup_options_mail_window_label);
            mSyncWindow.setValue(String.valueOf(mAccount.getSyncLookback()));
            final int maxLookback;
            if (mAccount.mPolicy != null) {
                maxLookback = mAccount.mPolicy.mMaxEmailLookback;
            } else {
                maxLookback = 0;
            }

            MailboxSettings.setupLookbackPreferenceOptions(mContext, mSyncWindow, maxLookback,
                    false);

            // Must correspond to the hole in the XML file that's reserved.
            mSyncWindow.setOrder(2);
            mSyncWindow.setOnPreferenceChangeListener(this);

            if (mSyncSettings == null) {
                mSyncSettings = new Preference(mContext);
                mSyncSettings.setKey(PREFERENCE_SYNC_SETTINGS);
                dataUsageCategory.addPreference(mSyncSettings);
            }

            mSyncSettings.setTitle(R.string.folder_sync_settings_pref_title);
            mSyncSettings.setOrder(3);
        }

        final PreferenceCategory folderPrefs =
                (PreferenceCategory) findPreference(PREFERENCE_SYSTEM_FOLDERS);
        if (folderPrefs != null) {
            if (mServiceInfo.requiresSetup) {
                Preference trashPreference = findPreference(PREFERENCE_SYSTEM_FOLDERS_TRASH);
                Intent i = new Intent(mContext, FolderPickerActivity.class);
                Uri uri = EmailContent.CONTENT_URI.buildUpon().appendQueryParameter(
                        "account", Long.toString(mAccount.getId())).build();
                i.setData(uri);
                i.putExtra(FolderPickerActivity.MAILBOX_TYPE_EXTRA, Mailbox.TYPE_TRASH);
                trashPreference.setIntent(i);

                Preference sentPreference = findPreference(PREFERENCE_SYSTEM_FOLDERS_SENT);
                i = new Intent(mContext, FolderPickerActivity.class);
                i.setData(uri);
                i.putExtra(FolderPickerActivity.MAILBOX_TYPE_EXTRA, Mailbox.TYPE_SENT);
                sentPreference.setIntent(i);
            } else {
                getPreferenceScreen().removePreference(folderPrefs);
            }
        }

        final CheckBoxPreference backgroundAttachments = (CheckBoxPreference)
                findPreference(PREFERENCE_BACKGROUND_ATTACHMENTS);
        if (backgroundAttachments != null) {
            if (!mServiceInfo.offerAttachmentPreload) {
                dataUsageCategory.removePreference(backgroundAttachments);
            } else {
                backgroundAttachments.setChecked(
                        0 != (mAccount.getFlags() & Account.FLAGS_BACKGROUND_ATTACHMENTS));
                backgroundAttachments.setOnPreferenceChangeListener(this);
            }
        }

        final PreferenceCategory notificationsCategory =
                (PreferenceCategory) findPreference(PREFERENCE_CATEGORY_NOTIFICATIONS);

        if (mInboxFolderPreferences != null) {
            if (mServiceInfo.offerLookback) {
                // This account supports per-folder notifications

                // Enable per-folder notifications preference
                if (mPerFolderNotification == null) {
                    mPerFolderNotification = new Preference(mContext);
                    mPerFolderNotification.setKey(PREFERENCE_PER_FOLDER_NOTIFICATIONS);
                    notificationsCategory.addPreference(mPerFolderNotification);
                }
                mPerFolderNotification.setTitle(R.string.folder_notify_settings_pref_title);

                // Remove Inbox per-account preferences
                final CheckBoxPreference inboxNotify = (CheckBoxPreference) findPreference(
                        FolderPreferences.PreferenceKeys.NOTIFICATIONS_ENABLED);
                if (inboxNotify != null) {
                    notificationsCategory.removePreference(inboxNotify);
                }
                mInboxRingtone = findPreference(
                        FolderPreferences.PreferenceKeys.NOTIFICATION_RINGTONE);
                if (mInboxRingtone != null) {
                    notificationsCategory.removePreference(mInboxRingtone);
                }
                mInboxVibrate = (CheckBoxPreference) findPreference(
                        FolderPreferences.PreferenceKeys.NOTIFICATION_VIBRATE);
                if (mInboxVibrate != null) {
                    notificationsCategory.removePreference(mInboxVibrate);
                }
                mInboxLights = (FolderNotificationLightPreference) findPreference(
                        FolderPreferences.PreferenceKeys.NOTIFICATION_LIGHTS);
                if (mInboxLights != null) {
                    notificationsCategory.removePreference(mInboxLights);
                }

                notificationsCategory.setEnabled(true);

            } else {
                final CheckBoxPreference inboxNotify = (CheckBoxPreference) findPreference(
                    FolderPreferences.PreferenceKeys.NOTIFICATIONS_ENABLED);
                inboxNotify.setChecked(mInboxFolderPreferences.areNotificationsEnabled());
                inboxNotify.setOnPreferenceChangeListener(this);

                mInboxRingtone = findPreference(
                        FolderPreferences.PreferenceKeys.NOTIFICATION_RINGTONE);
                final String ringtoneUri = mInboxFolderPreferences.getNotificationRingtoneUri();
                if (!TextUtils.isEmpty(ringtoneUri)) {
                    mRingtone = RingtoneManager.getRingtone(getActivity(), Uri.parse(ringtoneUri));
                }
                setRingtoneSummary();
                mInboxRingtone.setOnPreferenceChangeListener(this);
                mInboxRingtone.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                    @Override
                    public boolean onPreferenceClick(final Preference preference) {
                        showRingtonePicker();

                        return true;
                    }
                });

                notificationsCategory.setEnabled(true);

                // Set the vibrator value, or hide it on devices w/o a vibrator
                mInboxVibrate = (CheckBoxPreference) findPreference(
                        FolderPreferences.PreferenceKeys.NOTIFICATION_VIBRATE);
                if (mInboxVibrate != null) {
                    mInboxVibrate.setChecked(
                            mInboxFolderPreferences.isNotificationVibrateEnabled());
                    Vibrator vibrator = (Vibrator) mContext.getSystemService(
                            Context.VIBRATOR_SERVICE);
                    if (vibrator.hasVibrator()) {
                        // When the value is changed, update the setting.
                        mInboxVibrate.setOnPreferenceChangeListener(this);
                    } else {
                        // No vibrator present. Remove the preference altogether.
                        notificationsCategory.removePreference(mInboxVibrate);
                        mInboxVibrate = null;
                    }
                }

                boolean isArgbNotifColorSupported = getResources().getBoolean(
                        com.android.internal.R.bool.config_multiColorNotificationLed);
                mInboxLights = (FolderNotificationLightPreference) findPreference(
                        FolderPreferences.PreferenceKeys.NOTIFICATION_LIGHTS);
                if (mInboxLights != null) {
                    if (isArgbNotifColorSupported) {
                        updateNotificationLight(mInboxFolderPreferences.getNotificationLight());
                        mInboxLights.setOnPreferenceChangeListener(this);
                    } else {
                        notificationsCategory.removePreference(mInboxLights);
                    }
                }
            }
        } else {
            notificationsCategory.setEnabled(false);
        }

        final Preference retryAccount = findPreference(PREFERENCE_POLICIES_RETRY_ACCOUNT);
        final PreferenceCategory policiesCategory = (PreferenceCategory) findPreference(
                PREFERENCE_CATEGORY_POLICIES);
        if (policiesCategory != null) {
            // TODO: This code for showing policies isn't working. For KLP, just don't even bother
            // showing this data; we'll fix this later.
    /*
            if (policy != null) {
                if (policy.mProtocolPoliciesEnforced != null) {
                    ArrayList<String> policies = getSystemPoliciesList(policy);
                    setPolicyListSummary(policies, policy.mProtocolPoliciesEnforced,
                            PREFERENCE_POLICIES_ENFORCED);
                }
                if (policy.mProtocolPoliciesUnsupported != null) {
                    ArrayList<String> policies = new ArrayList<String>();
                    setPolicyListSummary(policies, policy.mProtocolPoliciesUnsupported,
                            PREFERENCE_POLICIES_UNSUPPORTED);
                } else {
                    // Don't show "retry" unless we have unsupported policies
                    policiesCategory.removePreference(retryAccount);
                }
            } else {
    */
            // Remove the category completely if there are no policies
            getPreferenceScreen().removePreference(policiesCategory);

            //}
        }

        if (retryAccount != null) {
            retryAccount.setOnPreferenceClickListener(
                    new Preference.OnPreferenceClickListener() {
                        @Override
                        public boolean onPreferenceClick(Preference preference) {
                            // Release the account
                            SecurityPolicy.setAccountHoldFlag(mContext, mAccount, false);
                            // Remove the preference
                            if (policiesCategory != null) {
                                policiesCategory.removePreference(retryAccount);
                            }
                            return true;
                        }
                    });
        }
        findPreference(PREFERENCE_INCOMING).setOnPreferenceClickListener(
                new Preference.OnPreferenceClickListener() {
                    @Override
                    public boolean onPreferenceClick(Preference preference) {
                        onIncomingSettings(mAccount);
                        return true;
                    }
                });

        // Hide the outgoing account setup link if it's not activated
        final Preference prefOutgoing = findPreference(PREFERENCE_OUTGOING);
        if (prefOutgoing != null) {
            if (mServiceInfo.usesSmtp && mAccount.mHostAuthSend != null) {
                prefOutgoing.setOnPreferenceClickListener(
                        new Preference.OnPreferenceClickListener() {
                            @Override
                            public boolean onPreferenceClick(Preference preference) {
                                onOutgoingSettings(mAccount);
                                return true;
                            }
                        });
            } else {
                if (mServiceInfo.usesSmtp) {
                    // We really ought to have an outgoing host auth but we don't.
                    // There's nothing we can do at this point, so just log the error.
                    LogUtils.e(LogUtils.TAG, "Account %d has a bad outbound hostauth",
                            mAccount.getId());
                }
                PreferenceCategory serverCategory = (PreferenceCategory) findPreference(
                        PREFERENCE_CATEGORY_SERVER);
                serverCategory.removePreference(prefOutgoing);
            }
        }

        final CheckBoxPreference syncContacts =
                (CheckBoxPreference) findPreference(PREFERENCE_SYNC_CONTACTS);
        final CheckBoxPreference syncCalendar =
                (CheckBoxPreference) findPreference(PREFERENCE_SYNC_CALENDAR);
        final CheckBoxPreference syncEmail =
                (CheckBoxPreference) findPreference(PREFERENCE_SYNC_EMAIL);
        if (syncContacts != null && syncCalendar != null && syncEmail != null) {
            if (mServiceInfo.syncContacts || mServiceInfo.syncCalendar) {
                if (mServiceInfo.syncContacts) {
                    syncContacts.setChecked(ContentResolver
                            .getSyncAutomatically(androidAcct, ContactsContract.AUTHORITY));
                    syncContacts.setOnPreferenceChangeListener(this);
                } else {
                    syncContacts.setChecked(false);
                    syncContacts.setEnabled(false);
                }
                if (mServiceInfo.syncCalendar) {
                    syncCalendar.setChecked(ContentResolver
                            .getSyncAutomatically(androidAcct, CalendarContract.AUTHORITY));
                    syncCalendar.setOnPreferenceChangeListener(this);
                } else {
                    syncCalendar.setChecked(false);
                    syncCalendar.setEnabled(false);
                }
                syncEmail.setChecked(ContentResolver
                        .getSyncAutomatically(androidAcct, EmailContent.AUTHORITY));
                syncEmail.setOnPreferenceChangeListener(this);
            } else {
                dataUsageCategory.removePreference(syncContacts);
                dataUsageCategory.removePreference(syncCalendar);
                dataUsageCategory.removePreference(syncEmail);
            }
        }
    }

    /**
     * Shows the system ringtone picker.
     */
    private void showRingtonePicker() {
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        final String ringtoneUri = mInboxFolderPreferences.getNotificationRingtoneUri();
        if (!TextUtils.isEmpty(ringtoneUri)) {
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(ringtoneUri));
        }
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI,
                Settings.System.DEFAULT_NOTIFICATION_URI);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
        startActivityForResult(intent, RINGTONE_REQUEST_CODE);
    }

    /**
     * Dispatch to edit quick responses.
     */
    public void onEditQuickResponses(com.android.mail.providers.Account account) {
        final Bundle args = AccountSettingsEditQuickResponsesFragment.createArgs(account);
        final PreferenceActivity activity = (PreferenceActivity) getActivity();
        activity.startPreferencePanel(AccountSettingsEditQuickResponsesFragment.class.getName(),
                args, R.string.account_settings_edit_quick_responses_label, null, null, 0);
    }

    /**
     * Dispatch to edit incoming settings.
     */
    public void onIncomingSettings(Account account) {
        final Intent intent =
                AccountServerSettingsActivity.getIntentForIncoming(getActivity(), account);
        getActivity().startActivity(intent);
    }

    /**
     * Dispatch to edit outgoing settings.
     */
    public void onOutgoingSettings(Account account) {
        final Intent intent =
                AccountServerSettingsActivity.getIntentForOutgoing(getActivity(), account);
        getActivity().startActivity(intent);
    }

    private void deleteAccount() {
        AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
            private ProgressDialog mDialog;

            @Override
            protected void onPreExecute() {
                // Display an alert dialog to advise the user that the operation is in progress
                mDialog = new ProgressDialog(mContext);
                mDialog.setMessage(mContext.getString(R.string.deleting_account_msg));
                mDialog.setCancelable(false);
                mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                mDialog.show();
            }

            @Override
            protected void onPostExecute(Boolean result) {
                if (!result) {
                    Toast.makeText(mContext, R.string.delete_account_failed,
                            Toast.LENGTH_SHORT).show();
                }
                mDialog.dismiss();
            }

            @Override
            protected Boolean doInBackground(Void... params) {
                try {
                    // Retrieve the necessary information
                    AccountManager accountManager = (AccountManager)mContext.getSystemService(
                            Context.ACCOUNT_SERVICE);
                    android.accounts.Account account = mUiAccount.getAccountManagerAccount();

                    // Remove the email account and its notifications
                    ContentResolver resolver = mContext.getContentResolver();
                    int ret = resolver.delete(mUiAccount.uri, null, null);
                    if (ret <= 0) {
                        LogUtils.w(LogUtils.TAG, "Failed to delete account %s", mAccountEmail);
                        return Boolean.FALSE;
                    }
                    NotificationUtils.clearAccountNotifications(mContext, account);

                    // And now we remove the system account that holds the email service
                    accountManager.removeAccount(account, getActivity(), null, null);

                    // Finish after account is deleted
                    getActivity().finish();
                } catch (Exception ex) {
                    LogUtils.w(LogUtils.TAG, ex, "Failed to delete account %s", mAccountEmail);
                    return Boolean.FALSE;
                }
                return Boolean.TRUE;
            }
        };
        task.execute();
    }

    private void updateNotificationLight(NotificationLight notificationLight) {
        if (notificationLight.mOn) {
            mInboxLights.setColor(notificationLight.mColor);
            mInboxLights.setOnOffValue(notificationLight.mTimeOn, notificationLight.mTimeOff);
        } else {
            int color = mUiAccount != null && mUiAccount.color != 0
                    ? mUiAccount.color
                    : FolderNotificationLightPreference.DEFAULT_COLOR;
            mInboxLights.setColor(color);
            mInboxLights.setOnOffValue(FolderNotificationLightPreference.DEFAULT_TIME,
                    FolderNotificationLightPreference.DEFAULT_TIME);
        }
        mInboxLights.setOn(notificationLight.mOn);
    }

    private void fillCheckFrecuency(CharSequence[] labels, CharSequence[] values) {
        if (mCheckFrequency == null) {
            return;
        }

        // Check push capability prior to include as an option
        if (mAccount != null) {
            boolean hasPushCapability = mAccount.hasCapability(EmailServiceProxy.CAPABILITY_PUSH);
            List<CharSequence> valuesList = new ArrayList<>(Arrays.asList(values));
            int checkIntervalPushPos = valuesList.indexOf(
                    String.valueOf(Account.CHECK_INTERVAL_PUSH));
            if (!hasPushCapability && checkIntervalPushPos != -1) {
                List<CharSequence> labelsList = new ArrayList<>(Arrays.asList(labels));
                labelsList.remove(checkIntervalPushPos);
                valuesList.remove(checkIntervalPushPos);
                labels = labelsList.toArray(new CharSequence[labelsList.size()]);
                values = valuesList.toArray(new CharSequence[valuesList.size()]);
            }
        }
        mCheckFrequency.setEntries(labels);
        mCheckFrequency.setEntryValues(values);
        mCheckFrequency.setDefaultValue(values);
    }
}