Home | History | Annotate | Download | only in preferences
      1 /*
      2  * Copyright (C) 2013 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 package com.android.email.preferences;
     17 
     18 import android.content.Context;
     19 import android.database.Cursor;
     20 import android.net.Uri;
     21 import android.text.TextUtils;
     22 
     23 import com.android.email.Preferences;
     24 import com.android.emailcommon.provider.EmailContent;
     25 import com.android.emailcommon.provider.EmailContent.AccountColumns;
     26 import com.android.mail.preferences.BasePreferenceMigrator;
     27 import com.android.mail.preferences.FolderPreferences;
     28 import com.android.mail.preferences.MailPrefs;
     29 import com.android.mail.providers.Account;
     30 import com.android.mail.providers.Folder;
     31 import com.android.mail.providers.UIProvider;
     32 import com.android.mail.utils.LogUtils;
     33 
     34 import java.util.ArrayList;
     35 import java.util.List;
     36 import java.util.Set;
     37 
     38 /**
     39  * Migrates Email settings to UnifiedEmail
     40  */
     41 public class EmailPreferenceMigrator extends BasePreferenceMigrator {
     42     private static final String LOG_TAG = "EmailPrefMigrator";
     43 
     44     @Override
     45     protected void migrate(final Context context, final int oldVersion, final int newVersion) {
     46         final List<Account> accounts = new ArrayList<Account>();
     47 
     48         final Cursor accountCursor = context.getContentResolver().query(Uri.parse(
     49                 EmailContent.CONTENT_URI + "/uiaccts"),
     50                 UIProvider.ACCOUNTS_PROJECTION_NO_CAPABILITIES, null, null, null);
     51 
     52         if (accountCursor == null) {
     53             LogUtils.wtf(LOG_TAG,
     54                     "Null cursor returned from query to %s when migrating accounts from %d to %d",
     55                     EmailContent.CONTENT_URI + "/uiaccts",
     56                     oldVersion, newVersion);
     57         } else {
     58             try {
     59                 while (accountCursor.moveToNext()) {
     60                     accounts.add(new Account(accountCursor));
     61                 }
     62             } finally {
     63                 accountCursor.close();
     64             }
     65         }
     66 
     67         migrate(context, oldVersion, newVersion, accounts);
     68     }
     69 
     70     protected static void migrate(final Context context, final int oldVersion, final int newVersion,
     71             final List<Account> accounts) {
     72         final Preferences preferences = Preferences.getPreferences(context);
     73         final MailPrefs mailPrefs = MailPrefs.get(context);
     74         if (oldVersion < 1) {
     75             // Move global settings
     76 
     77             @SuppressWarnings("deprecation")
     78             final boolean hasSwipeDelete = preferences.hasSwipeDelete();
     79             if (hasSwipeDelete) {
     80                 @SuppressWarnings("deprecation")
     81                 final boolean swipeDelete = preferences.getSwipeDelete();
     82                 mailPrefs.setConversationListSwipeEnabled(swipeDelete);
     83             }
     84 
     85             // Move reply-all setting
     86             @SuppressWarnings("deprecation")
     87             final boolean isReplyAllSet = preferences.hasReplyAll();
     88             if (isReplyAllSet) {
     89                 @SuppressWarnings("deprecation")
     90                 final boolean replyAll = preferences.getReplyAll();
     91                 mailPrefs.setDefaultReplyAll(replyAll);
     92             }
     93 
     94             // Move folder notification settings
     95             for (final Account account : accounts) {
     96                 // Get the emailcommon account
     97                 final Cursor ecAccountCursor = context.getContentResolver().query(
     98                         com.android.emailcommon.provider.Account.CONTENT_URI,
     99                         com.android.emailcommon.provider.Account.CONTENT_PROJECTION,
    100                         AccountColumns.EMAIL_ADDRESS + " = ?",
    101                         new String[] { account.getEmailAddress() },
    102                         null);
    103                 final com.android.emailcommon.provider.Account ecAccount =
    104                         new com.android.emailcommon.provider.Account();
    105 
    106 
    107                 if (ecAccountCursor == null) {
    108                     LogUtils.e(LOG_TAG, "Null old account cursor for mailbox %s",
    109                             LogUtils.sanitizeName(LOG_TAG, account.name));
    110                     continue;
    111                 }
    112 
    113                 try {
    114                     if (ecAccountCursor.moveToFirst()) {
    115                         ecAccount.restore(ecAccountCursor);
    116                     } else {
    117                         LogUtils.e(LOG_TAG, "Couldn't load old account for mailbox %s",
    118                                 LogUtils.sanitizeName(LOG_TAG, account.name));
    119                         continue;
    120                     }
    121                 } finally {
    122                     ecAccountCursor.close();
    123                 }
    124 
    125                 // The only setting in AccountPreferences so far is a global notification toggle,
    126                 // but we only allow Inbox notifications, so it will remain unused
    127                 final Cursor folderCursor =
    128                         context.getContentResolver().query(account.settings.defaultInbox,
    129                                 UIProvider.FOLDERS_PROJECTION, null, null, null);
    130 
    131                 if (folderCursor == null) {
    132                     LogUtils.e(LOG_TAG, "Null folder cursor for mailbox %s",
    133                             LogUtils.sanitizeName(LOG_TAG,
    134                                     account.settings.defaultInbox.toString()));
    135                     continue;
    136                 }
    137 
    138                 Folder folder = null;
    139                 try {
    140                     if (folderCursor.moveToFirst()) {
    141                         folder = new Folder(folderCursor);
    142                     }
    143                 } finally {
    144                     folderCursor.close();
    145                 }
    146 
    147                 final FolderPreferences folderPreferences =
    148                         new FolderPreferences(context, account.getEmailAddress(), folder,
    149                                 true /* inbox */);
    150 
    151                 @SuppressWarnings("deprecation")
    152                 final boolean notify = (ecAccount.getFlags()
    153                         & com.android.emailcommon.provider.Account.FLAGS_NOTIFY_NEW_MAIL) != 0;
    154                 folderPreferences.setNotificationsEnabled(notify);
    155 
    156                 @SuppressWarnings("deprecation")
    157                 final String ringtoneUri = ecAccount.getRingtone();
    158                 folderPreferences.setNotificationRingtoneUri(ringtoneUri);
    159 
    160                 @SuppressWarnings("deprecation")
    161                 final boolean vibrate = (ecAccount.getFlags()
    162                         & com.android.emailcommon.provider.Account.FLAGS_VIBRATE) != 0;
    163                 folderPreferences.setNotificationVibrateEnabled(vibrate);
    164 
    165                 folderPreferences.commit();
    166             }
    167         }
    168 
    169         if (oldVersion < 2) {
    170             @SuppressWarnings("deprecation")
    171             final Set<String> whitelistedAddresses = preferences.getWhitelistedSenderAddresses();
    172             mailPrefs.setSenderWhitelist(whitelistedAddresses);
    173         }
    174 
    175         if (oldVersion < 3) {
    176             @SuppressWarnings("deprecation")
    177             // The default for the conversation list icon is the sender image.
    178             final boolean showSenderImages = !TextUtils.equals(
    179                     Preferences.CONV_LIST_ICON_NONE, preferences.getConversationListIcon());
    180             mailPrefs.setShowSenderImages(showSenderImages);
    181         }
    182     }
    183 }
    184