• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
migrate(final Context context, final int oldVersion, final int newVersion)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(Account.builder().buildFrom(accountCursor));
61                 }
62             } finally {
63                 accountCursor.close();
64             }
65         }
66 
67         migrate(context, oldVersion, newVersion, accounts);
68     }
69 
70     @SuppressWarnings("deprecation")
migrate(final Context context, final int oldVersion, final int newVersion, final List<Account> accounts)71     protected static void migrate(final Context context, final int oldVersion, final int newVersion,
72             final List<Account> accounts) {
73         final Preferences preferences = Preferences.getPreferences(context);
74         final MailPrefs mailPrefs = MailPrefs.get(context);
75         if (oldVersion < 1) {
76             // Move global settings
77 
78             final boolean hasSwipeDelete = preferences.hasSwipeDelete();
79             if (hasSwipeDelete) {
80                 final boolean swipeDelete = preferences.getSwipeDelete();
81                 mailPrefs.setConversationListSwipeEnabled(swipeDelete);
82             }
83 
84             // Move reply-all setting
85             final boolean isReplyAllSet = preferences.hasReplyAll();
86             if (isReplyAllSet) {
87                 final boolean replyAll = preferences.getReplyAll();
88                 mailPrefs.setDefaultReplyAll(replyAll);
89             }
90 
91             // Move folder notification settings
92             for (final Account account : accounts) {
93                 // Get the emailcommon account
94                 final Cursor ecAccountCursor = context.getContentResolver().query(
95                         com.android.emailcommon.provider.Account.CONTENT_URI,
96                         com.android.emailcommon.provider.Account.CONTENT_PROJECTION,
97                         AccountColumns.EMAIL_ADDRESS + " = ?",
98                         new String[] { account.getEmailAddress() },
99                         null);
100                 final com.android.emailcommon.provider.Account ecAccount =
101                         new com.android.emailcommon.provider.Account();
102 
103 
104                 if (ecAccountCursor == null) {
105                     LogUtils.e(LOG_TAG, "Null old account cursor for mailbox %s",
106                             LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()));
107                     continue;
108                 }
109 
110                 try {
111                     if (ecAccountCursor.moveToFirst()) {
112                         ecAccount.restore(ecAccountCursor);
113                     } else {
114                         LogUtils.e(LOG_TAG, "Couldn't load old account for mailbox %s",
115                                 LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()));
116                         continue;
117                     }
118                 } finally {
119                     ecAccountCursor.close();
120                 }
121 
122                 // The only setting in AccountPreferences so far is a global notification toggle,
123                 // but we only allow Inbox notifications, so it will remain unused
124                 final Cursor folderCursor =
125                         context.getContentResolver().query(account.settings.defaultInbox,
126                                 UIProvider.FOLDERS_PROJECTION, null, null, null);
127 
128                 if (folderCursor == null) {
129                     LogUtils.e(LOG_TAG, "Null folder cursor for mailbox %s",
130                             LogUtils.sanitizeName(LOG_TAG,
131                                     account.settings.defaultInbox.toString()));
132                     continue;
133                 }
134 
135                 Folder folder = null;
136                 try {
137                     if (folderCursor.moveToFirst()) {
138                         folder = new Folder(folderCursor);
139                     } else {
140                         LogUtils.e(LOG_TAG, "Empty folder cursor for mailbox %s",
141                                 LogUtils.sanitizeName(LOG_TAG,
142                                         account.settings.defaultInbox.toString()));
143                         continue;
144                     }
145                 } finally {
146                     folderCursor.close();
147                 }
148 
149                 final FolderPreferences folderPreferences =
150                         new FolderPreferences(context, account.getEmailAddress(), folder,
151                                 true /* inbox */);
152 
153                 final boolean notify = (ecAccount.getFlags()
154                         & com.android.emailcommon.provider.Account.FLAGS_NOTIFY_NEW_MAIL) != 0;
155                 folderPreferences.setNotificationsEnabled(notify);
156 
157                 final String ringtoneUri = ecAccount.getRingtone();
158                 folderPreferences.setNotificationRingtoneUri(ringtoneUri);
159 
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             final Set<String> whitelistedAddresses = preferences.getWhitelistedSenderAddresses();
170             mailPrefs.setSenderWhitelist(whitelistedAddresses);
171         }
172 
173         if (oldVersion < 3) {
174             // The default for the conversation list icon is the sender image.
175             final boolean showSenderImages = !TextUtils.equals(
176                     Preferences.CONV_LIST_ICON_NONE, preferences.getConversationListIcon());
177             mailPrefs.setShowSenderImages(showSenderImages);
178         }
179 
180         if (oldVersion < 4) {
181             final boolean confirmDelete = preferences.getConfirmDelete();
182             mailPrefs.setConfirmDelete(confirmDelete);
183 
184             final boolean confirmSend = preferences.getConfirmSend();
185             mailPrefs.setConfirmSend(confirmSend);
186 
187             final int autoAdvance = preferences.getAutoAdvanceDirection();
188             switch(autoAdvance) {
189                 case Preferences.AUTO_ADVANCE_OLDER:
190                     mailPrefs.setAutoAdvanceMode(UIProvider.AutoAdvance.OLDER);
191                 case Preferences.AUTO_ADVANCE_NEWER:
192                     mailPrefs.setAutoAdvanceMode(UIProvider.AutoAdvance.NEWER);
193                 case Preferences.AUTO_ADVANCE_MESSAGE_LIST:
194                 default:
195                     mailPrefs.setAutoAdvanceMode(UIProvider.AutoAdvance.LIST);
196             }
197         }
198     }
199 }
200