From 0a710bde6817354211525b0f3176a1e294d1cd79 Mon Sep 17 00:00:00 2001 From: Scott Kennedy Date: Mon, 26 Aug 2013 12:21:13 -0700 Subject: [PATCH] Fix notification preference migration These settings need to be migrated from the database, not the SharedPreferences file that likely shouldn't even exist. Everything added to Account.java was removed in Ie6ec389b5b5d2e7ab1b299d0877811ae716526e2 when it was believed to be unnecessary. Bug: 10211615 Change-Id: If6193758febda8a3272d82792492503549a44e32 --- .../android/emailcommon/provider/Account.java | 47 ++++++++++-- .../emailcommon/provider/EmailContent.java | 2 +- .../preferences/EmailPreferenceMigrator.java | 73 +++++++++++-------- 3 files changed, 84 insertions(+), 38 deletions(-) diff --git a/emailcommon/src/com/android/emailcommon/provider/Account.java b/emailcommon/src/com/android/emailcommon/provider/Account.java index c6959e407..be139a7d9 100755 --- a/emailcommon/src/com/android/emailcommon/provider/Account.java +++ b/emailcommon/src/com/android/emailcommon/provider/Account.java @@ -24,6 +24,7 @@ import android.content.ContentValues; import android.content.Context; import android.content.OperationApplicationException; import android.database.Cursor; +import android.media.RingtoneManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; @@ -57,6 +58,20 @@ public final class Account extends EmailContent implements AccountColumns, Parce */ public static final long NO_ACCOUNT = -1L; + /** + * Whether or not the user has asked for notifications of new mail in this account + * + * @deprecated Used only for migration + */ + @Deprecated + public final static int FLAGS_NOTIFY_NEW_MAIL = 1<<0; + /** + * Whether or not the user has asked for vibration notifications with all new mail + * + * @deprecated Used only for migration + */ + @Deprecated + public final static int FLAGS_VIBRATE = 1<<1; // Bit mask for the account's deletion policy (see DELETE_POLICY_x below) public static final int FLAGS_DELETE_POLICY_MASK = 1<<2 | 1<<3; public static final int FLAGS_DELETE_POLICY_SHIFT = 2; @@ -116,6 +131,9 @@ public final class Account extends EmailContent implements AccountColumns, Parce public int mFlags; public String mCompatibilityUuid; public String mSenderName; + /** @deprecated Used only for migration */ + @Deprecated + private String mRingtoneUri; public String mProtocolVersion; public int mNewMessageCount; public String mSecuritySyncKey; @@ -141,12 +159,13 @@ public final class Account extends EmailContent implements AccountColumns, Parce public static final int CONTENT_FLAGS_COLUMN = 8; public static final int CONTENT_COMPATIBILITY_UUID_COLUMN = 9; public static final int CONTENT_SENDER_NAME_COLUMN = 10; - public static final int CONTENT_PROTOCOL_VERSION_COLUMN = 11; - public static final int CONTENT_NEW_MESSAGE_COUNT_COLUMN = 12; - public static final int CONTENT_SECURITY_SYNC_KEY_COLUMN = 13; - public static final int CONTENT_SIGNATURE_COLUMN = 14; - public static final int CONTENT_POLICY_KEY_COLUMN = 15; - public static final int CONTENT_PING_DURATION_COLUMN = 16; + public static final int CONTENT_RINGTONE_URI_COLUMN = 11; + public static final int CONTENT_PROTOCOL_VERSION_COLUMN = 12; + public static final int CONTENT_NEW_MESSAGE_COUNT_COLUMN = 13; + public static final int CONTENT_SECURITY_SYNC_KEY_COLUMN = 14; + public static final int CONTENT_SIGNATURE_COLUMN = 15; + public static final int CONTENT_POLICY_KEY_COLUMN = 16; + public static final int CONTENT_PING_DURATION_COLUMN = 17; public static final String[] CONTENT_PROJECTION = new String[] { RECORD_ID, AccountColumns.DISPLAY_NAME, @@ -154,7 +173,7 @@ public final class Account extends EmailContent implements AccountColumns, Parce AccountColumns.SYNC_INTERVAL, AccountColumns.HOST_AUTH_KEY_RECV, AccountColumns.HOST_AUTH_KEY_SEND, AccountColumns.FLAGS, AccountColumns.COMPATIBILITY_UUID, AccountColumns.SENDER_NAME, - AccountColumns.PROTOCOL_VERSION, + AccountColumns.RINGTONE_URI, AccountColumns.PROTOCOL_VERSION, AccountColumns.NEW_MESSAGE_COUNT, AccountColumns.SECURITY_SYNC_KEY, AccountColumns.SIGNATURE, AccountColumns.POLICY_KEY, AccountColumns.PING_DURATION }; @@ -195,6 +214,7 @@ public final class Account extends EmailContent implements AccountColumns, Parce mBaseUri = CONTENT_URI; // other defaults (policy) + mRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).toString(); mSyncInterval = -1; mSyncLookback = -1; mFlags = 0; @@ -246,6 +266,7 @@ public final class Account extends EmailContent implements AccountColumns, Parce mFlags = cursor.getInt(CONTENT_FLAGS_COLUMN); mCompatibilityUuid = cursor.getString(CONTENT_COMPATIBILITY_UUID_COLUMN); mSenderName = cursor.getString(CONTENT_SENDER_NAME_COLUMN); + mRingtoneUri = cursor.getString(CONTENT_RINGTONE_URI_COLUMN); mProtocolVersion = cursor.getString(CONTENT_PROTOCOL_VERSION_COLUMN); mNewMessageCount = cursor.getInt(CONTENT_NEW_MESSAGE_COUNT_COLUMN); mSecuritySyncKey = cursor.getString(CONTENT_SECURITY_SYNC_KEY_COLUMN); @@ -379,6 +400,15 @@ public final class Account extends EmailContent implements AccountColumns, Parce mFlags = newFlags; } + /** + * @return the ringtone Uri for this account + * @deprecated Used only for migration + */ + @Deprecated + public String getRingtone() { + return mRingtoneUri; + } + /** * Set the "delete policy" as a simple 0,1,2 value set. * @param newPolicy the new delete policy @@ -774,6 +804,7 @@ public final class Account extends EmailContent implements AccountColumns, Parce values.put(AccountColumns.FLAGS, mFlags); values.put(AccountColumns.COMPATIBILITY_UUID, mCompatibilityUuid); values.put(AccountColumns.SENDER_NAME, mSenderName); + values.put(AccountColumns.RINGTONE_URI, mRingtoneUri); values.put(AccountColumns.PROTOCOL_VERSION, mProtocolVersion); values.put(AccountColumns.NEW_MESSAGE_COUNT, mNewMessageCount); values.put(AccountColumns.SECURITY_SYNC_KEY, mSecuritySyncKey); @@ -824,6 +855,7 @@ public final class Account extends EmailContent implements AccountColumns, Parce dest.writeInt(mFlags); dest.writeString(mCompatibilityUuid); dest.writeString(mSenderName); + dest.writeString(mRingtoneUri); dest.writeString(mProtocolVersion); dest.writeInt(mNewMessageCount); dest.writeString(mSecuritySyncKey); @@ -861,6 +893,7 @@ public final class Account extends EmailContent implements AccountColumns, Parce mFlags = in.readInt(); mCompatibilityUuid = in.readString(); mSenderName = in.readString(); + mRingtoneUri = in.readString(); mProtocolVersion = in.readString(); mNewMessageCount = in.readInt(); mSecuritySyncKey = in.readString(); diff --git a/emailcommon/src/com/android/emailcommon/provider/EmailContent.java b/emailcommon/src/com/android/emailcommon/provider/EmailContent.java index 8e6d513fe..0bedefc0c 100755 --- a/emailcommon/src/com/android/emailcommon/provider/EmailContent.java +++ b/emailcommon/src/com/android/emailcommon/provider/EmailContent.java @@ -1587,7 +1587,7 @@ public abstract class EmailContent { /** * Ringtone * - * @deprecated This is no longer used by anything except for creating the database. + * @deprecated Only used for creating the database (legacy reasons) and migration. */ @Deprecated public static final String RINGTONE_URI = "ringtoneUri"; diff --git a/src/com/android/email/preferences/EmailPreferenceMigrator.java b/src/com/android/email/preferences/EmailPreferenceMigrator.java index f1241b2a1..1321a69a1 100644 --- a/src/com/android/email/preferences/EmailPreferenceMigrator.java +++ b/src/com/android/email/preferences/EmailPreferenceMigrator.java @@ -16,21 +16,19 @@ package com.android.email.preferences; import android.content.Context; -import android.content.SharedPreferences; import android.database.Cursor; import android.net.Uri; -import android.preference.PreferenceManager; import android.text.TextUtils; import com.android.email.Preferences; import com.android.emailcommon.provider.EmailContent; +import com.android.emailcommon.provider.EmailContent.AccountColumns; import com.android.mail.preferences.BasePreferenceMigrator; import com.android.mail.preferences.FolderPreferences; import com.android.mail.preferences.MailPrefs; import com.android.mail.providers.Account; import com.android.mail.providers.Folder; import com.android.mail.providers.UIProvider; -import com.android.mail.utils.LogTag; import com.android.mail.utils.LogUtils; import java.util.ArrayList; @@ -41,7 +39,7 @@ import java.util.Set; * Migrates Email settings to UnifiedEmail */ public class EmailPreferenceMigrator extends BasePreferenceMigrator { - private static final String LOG_TAG = LogTag.getLogTag(); + private static final String LOG_TAG = "EmailPrefMigrator"; @Override protected void migrate(final Context context, final int oldVersion, final int newVersion) { @@ -69,11 +67,6 @@ public class EmailPreferenceMigrator extends BasePreferenceMigrator { migrate(context, oldVersion, newVersion, accounts); } - private static final String PREFERENCE_NOTIFY = "account_notify"; - private static final String PREFERENCE_VIBRATE = "account_settings_vibrate"; - private static final String PREFERENCE_VIBRATE_OLD = "account_settings_vibrate_when"; - private static final String PREFERENCE_RINGTONE = "account_ringtone"; - protected static void migrate(final Context context, final int oldVersion, final int newVersion, final List accounts) { final Preferences preferences = Preferences.getPreferences(context); @@ -100,6 +93,34 @@ public class EmailPreferenceMigrator extends BasePreferenceMigrator { // Move folder notification settings for (final Account account : accounts) { + // Get the emailcommon account + final Cursor ecAccountCursor = context.getContentResolver().query( + com.android.emailcommon.provider.Account.CONTENT_URI, + com.android.emailcommon.provider.Account.CONTENT_PROJECTION, + AccountColumns.EMAIL_ADDRESS + " = ?", new String[] { account.name }, + null); + final com.android.emailcommon.provider.Account ecAccount = + new com.android.emailcommon.provider.Account(); + + + if (ecAccountCursor == null) { + LogUtils.e(LOG_TAG, "Null old account cursor for mailbox %s", + LogUtils.sanitizeName(LOG_TAG, account.name)); + continue; + } + + try { + if (ecAccountCursor.moveToFirst()) { + ecAccount.restore(ecAccountCursor); + } else { + LogUtils.e(LOG_TAG, "Couldn't load old account for mailbox %s", + LogUtils.sanitizeName(LOG_TAG, account.name)); + continue; + } + } finally { + ecAccountCursor.close(); + } + // The only setting in AccountPreferences so far is a global notification toggle, // but we only allow Inbox notifications, so it will remain unused final Cursor folderCursor = @@ -108,7 +129,8 @@ public class EmailPreferenceMigrator extends BasePreferenceMigrator { if (folderCursor == null) { LogUtils.e(LOG_TAG, "Null folder cursor for mailbox %s", - account.settings.defaultInbox); + LogUtils.sanitizeName(LOG_TAG, + account.settings.defaultInbox.toString())); continue; } @@ -124,28 +146,19 @@ public class EmailPreferenceMigrator extends BasePreferenceMigrator { final FolderPreferences folderPreferences = new FolderPreferences(context, account.name, folder, true /* inbox */); - final SharedPreferences sharedPreferences = - PreferenceManager.getDefaultSharedPreferences(context); + @SuppressWarnings("deprecation") + final boolean notify = (ecAccount.getFlags() + & com.android.emailcommon.provider.Account.FLAGS_NOTIFY_NEW_MAIL) != 0; + folderPreferences.setNotificationsEnabled(notify); - if (sharedPreferences.contains(PREFERENCE_NOTIFY)) { - final boolean notify = sharedPreferences.getBoolean(PREFERENCE_NOTIFY, true); - folderPreferences.setNotificationsEnabled(notify); - } + @SuppressWarnings("deprecation") + final String ringtoneUri = ecAccount.getRingtone(); + folderPreferences.setNotificationRingtoneUri(ringtoneUri); - if (sharedPreferences.contains(PREFERENCE_RINGTONE)) { - final String ringtoneUri = - sharedPreferences.getString(PREFERENCE_RINGTONE, null); - folderPreferences.setNotificationRingtoneUri(ringtoneUri); - } - - if (sharedPreferences.contains(PREFERENCE_VIBRATE)) { - final boolean vibrate = sharedPreferences.getBoolean(PREFERENCE_VIBRATE, false); - folderPreferences.setNotificationVibrateEnabled(vibrate); - } else if (sharedPreferences.contains(PREFERENCE_VIBRATE_OLD)) { - final boolean vibrate = "always".equals( - sharedPreferences.getString(PREFERENCE_VIBRATE_OLD, "")); - folderPreferences.setNotificationVibrateEnabled(vibrate); - } + @SuppressWarnings("deprecation") + final boolean vibrate = (ecAccount.getFlags() + & com.android.emailcommon.provider.Account.FLAGS_VIBRATE) != 0; + folderPreferences.setNotificationVibrateEnabled(vibrate); folderPreferences.commit(); }