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 + " = ?", new String[] { account.name },
    101                         null);
    102                 final com.android.emailcommon.provider.Account ecAccount =
    103                         new com.android.emailcommon.provider.Account();
    104 
    105 
    106                 if (ecAccountCursor == null) {
    107                     LogUtils.e(LOG_TAG, "Null old account cursor for mailbox %s",
    108                             LogUtils.sanitizeName(LOG_TAG, account.name));
    109                     continue;
    110                 }
    111 
    112                 try {
    113                     if (ecAccountCursor.moveToFirst()) {
    114                         ecAccount.restore(ecAccountCursor);
    115                     } else {
    116                         LogUtils.e(LOG_TAG, "Couldn't load old account for mailbox %s",
    117                                 LogUtils.sanitizeName(LOG_TAG, account.name));
    118                         continue;
    119                     }
    120                 } finally {
    121                     ecAccountCursor.close();
    122                 }
    123 
    124                 // The only setting in AccountPreferences so far is a global notification toggle,
    125                 // but we only allow Inbox notifications, so it will remain unused
    126                 final Cursor folderCursor =
    127                         context.getContentResolver().query(account.settings.defaultInbox,
    128                                 UIProvider.FOLDERS_PROJECTION, null, null, null);
    129 
    130                 if (folderCursor == null) {
    131                     LogUtils.e(LOG_TAG, "Null folder cursor for mailbox %s",
    132                             LogUtils.sanitizeName(LOG_TAG,
    133                                     account.settings.defaultInbox.toString()));
    134                     continue;
    135                 }
    136 
    137                 Folder folder = null;
    138                 try {
    139                     if (folderCursor.moveToFirst()) {
    140                         folder = new Folder(folderCursor);
    141                     }
    142                 } finally {
    143                     folderCursor.close();
    144                 }
    145 
    146                 final FolderPreferences folderPreferences =
    147                         new FolderPreferences(context, account.getEmailAddress(), folder,
    148                                 true /* inbox */);
    149 
    150                 @SuppressWarnings("deprecation")
    151                 final boolean notify = (ecAccount.getFlags()
    152                         & com.android.emailcommon.provider.Account.FLAGS_NOTIFY_NEW_MAIL) != 0;
    153                 folderPreferences.setNotificationsEnabled(notify);
    154 
    155                 @SuppressWarnings("deprecation")
    156                 final String ringtoneUri = ecAccount.getRingtone();
    157                 folderPreferences.setNotificationRingtoneUri(ringtoneUri);
    158 
    159                 @SuppressWarnings("deprecation")
    160                 final boolean vibrate = (ecAccount.getFlags()
    161                         & com.android.emailcommon.provider.Account.FLAGS_VIBRATE) != 0;
    162                 folderPreferences.setNotificationVibrateEnabled(vibrate);
    163 
    164                 folderPreferences.commit();
    165             }
    166         }
    167 
    168         if (oldVersion < 2) {
    169             @SuppressWarnings("deprecation")
    170             final Set<String> whitelistedAddresses = preferences.getWhitelistedSenderAddresses();
    171             mailPrefs.setSenderWhitelist(whitelistedAddresses);
    172         }
    173 
    174         if (oldVersion < 3) {
    175             @SuppressWarnings("deprecation")
    176             // The default for the conversation list icon is the sender image.
    177             final boolean showSenderImages = !TextUtils.equals(
    178                     Preferences.CONV_LIST_ICON_NONE, preferences.getConversationListIcon());
    179             mailPrefs.setShowSenderImages(showSenderImages);
    180         }
    181     }
    182 }
    183