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
This commit is contained in:
Scott Kennedy 2013-08-26 12:21:13 -07:00
parent 076ab83074
commit 0a710bde68
3 changed files with 84 additions and 38 deletions

View File

@ -24,6 +24,7 @@ import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.OperationApplicationException; import android.content.OperationApplicationException;
import android.database.Cursor; import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.Uri; import android.net.Uri;
@ -57,6 +58,20 @@ public final class Account extends EmailContent implements AccountColumns, Parce
*/ */
public static final long NO_ACCOUNT = -1L; 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) // 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_MASK = 1<<2 | 1<<3;
public static final int FLAGS_DELETE_POLICY_SHIFT = 2; 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 int mFlags;
public String mCompatibilityUuid; public String mCompatibilityUuid;
public String mSenderName; public String mSenderName;
/** @deprecated Used only for migration */
@Deprecated
private String mRingtoneUri;
public String mProtocolVersion; public String mProtocolVersion;
public int mNewMessageCount; public int mNewMessageCount;
public String mSecuritySyncKey; 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_FLAGS_COLUMN = 8;
public static final int CONTENT_COMPATIBILITY_UUID_COLUMN = 9; public static final int CONTENT_COMPATIBILITY_UUID_COLUMN = 9;
public static final int CONTENT_SENDER_NAME_COLUMN = 10; public static final int CONTENT_SENDER_NAME_COLUMN = 10;
public static final int CONTENT_PROTOCOL_VERSION_COLUMN = 11; public static final int CONTENT_RINGTONE_URI_COLUMN = 11;
public static final int CONTENT_NEW_MESSAGE_COUNT_COLUMN = 12; public static final int CONTENT_PROTOCOL_VERSION_COLUMN = 12;
public static final int CONTENT_SECURITY_SYNC_KEY_COLUMN = 13; public static final int CONTENT_NEW_MESSAGE_COUNT_COLUMN = 13;
public static final int CONTENT_SIGNATURE_COLUMN = 14; public static final int CONTENT_SECURITY_SYNC_KEY_COLUMN = 14;
public static final int CONTENT_POLICY_KEY_COLUMN = 15; public static final int CONTENT_SIGNATURE_COLUMN = 15;
public static final int CONTENT_PING_DURATION_COLUMN = 16; 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[] { public static final String[] CONTENT_PROJECTION = new String[] {
RECORD_ID, AccountColumns.DISPLAY_NAME, 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.SYNC_INTERVAL, AccountColumns.HOST_AUTH_KEY_RECV,
AccountColumns.HOST_AUTH_KEY_SEND, AccountColumns.FLAGS, AccountColumns.HOST_AUTH_KEY_SEND, AccountColumns.FLAGS,
AccountColumns.COMPATIBILITY_UUID, AccountColumns.SENDER_NAME, AccountColumns.COMPATIBILITY_UUID, AccountColumns.SENDER_NAME,
AccountColumns.PROTOCOL_VERSION, AccountColumns.RINGTONE_URI, AccountColumns.PROTOCOL_VERSION,
AccountColumns.NEW_MESSAGE_COUNT, AccountColumns.SECURITY_SYNC_KEY, AccountColumns.NEW_MESSAGE_COUNT, AccountColumns.SECURITY_SYNC_KEY,
AccountColumns.SIGNATURE, AccountColumns.POLICY_KEY, AccountColumns.PING_DURATION AccountColumns.SIGNATURE, AccountColumns.POLICY_KEY, AccountColumns.PING_DURATION
}; };
@ -195,6 +214,7 @@ public final class Account extends EmailContent implements AccountColumns, Parce
mBaseUri = CONTENT_URI; mBaseUri = CONTENT_URI;
// other defaults (policy) // other defaults (policy)
mRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).toString();
mSyncInterval = -1; mSyncInterval = -1;
mSyncLookback = -1; mSyncLookback = -1;
mFlags = 0; mFlags = 0;
@ -246,6 +266,7 @@ public final class Account extends EmailContent implements AccountColumns, Parce
mFlags = cursor.getInt(CONTENT_FLAGS_COLUMN); mFlags = cursor.getInt(CONTENT_FLAGS_COLUMN);
mCompatibilityUuid = cursor.getString(CONTENT_COMPATIBILITY_UUID_COLUMN); mCompatibilityUuid = cursor.getString(CONTENT_COMPATIBILITY_UUID_COLUMN);
mSenderName = cursor.getString(CONTENT_SENDER_NAME_COLUMN); mSenderName = cursor.getString(CONTENT_SENDER_NAME_COLUMN);
mRingtoneUri = cursor.getString(CONTENT_RINGTONE_URI_COLUMN);
mProtocolVersion = cursor.getString(CONTENT_PROTOCOL_VERSION_COLUMN); mProtocolVersion = cursor.getString(CONTENT_PROTOCOL_VERSION_COLUMN);
mNewMessageCount = cursor.getInt(CONTENT_NEW_MESSAGE_COUNT_COLUMN); mNewMessageCount = cursor.getInt(CONTENT_NEW_MESSAGE_COUNT_COLUMN);
mSecuritySyncKey = cursor.getString(CONTENT_SECURITY_SYNC_KEY_COLUMN); mSecuritySyncKey = cursor.getString(CONTENT_SECURITY_SYNC_KEY_COLUMN);
@ -379,6 +400,15 @@ public final class Account extends EmailContent implements AccountColumns, Parce
mFlags = newFlags; 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. * Set the "delete policy" as a simple 0,1,2 value set.
* @param newPolicy the new delete policy * @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.FLAGS, mFlags);
values.put(AccountColumns.COMPATIBILITY_UUID, mCompatibilityUuid); values.put(AccountColumns.COMPATIBILITY_UUID, mCompatibilityUuid);
values.put(AccountColumns.SENDER_NAME, mSenderName); values.put(AccountColumns.SENDER_NAME, mSenderName);
values.put(AccountColumns.RINGTONE_URI, mRingtoneUri);
values.put(AccountColumns.PROTOCOL_VERSION, mProtocolVersion); values.put(AccountColumns.PROTOCOL_VERSION, mProtocolVersion);
values.put(AccountColumns.NEW_MESSAGE_COUNT, mNewMessageCount); values.put(AccountColumns.NEW_MESSAGE_COUNT, mNewMessageCount);
values.put(AccountColumns.SECURITY_SYNC_KEY, mSecuritySyncKey); values.put(AccountColumns.SECURITY_SYNC_KEY, mSecuritySyncKey);
@ -824,6 +855,7 @@ public final class Account extends EmailContent implements AccountColumns, Parce
dest.writeInt(mFlags); dest.writeInt(mFlags);
dest.writeString(mCompatibilityUuid); dest.writeString(mCompatibilityUuid);
dest.writeString(mSenderName); dest.writeString(mSenderName);
dest.writeString(mRingtoneUri);
dest.writeString(mProtocolVersion); dest.writeString(mProtocolVersion);
dest.writeInt(mNewMessageCount); dest.writeInt(mNewMessageCount);
dest.writeString(mSecuritySyncKey); dest.writeString(mSecuritySyncKey);
@ -861,6 +893,7 @@ public final class Account extends EmailContent implements AccountColumns, Parce
mFlags = in.readInt(); mFlags = in.readInt();
mCompatibilityUuid = in.readString(); mCompatibilityUuid = in.readString();
mSenderName = in.readString(); mSenderName = in.readString();
mRingtoneUri = in.readString();
mProtocolVersion = in.readString(); mProtocolVersion = in.readString();
mNewMessageCount = in.readInt(); mNewMessageCount = in.readInt();
mSecuritySyncKey = in.readString(); mSecuritySyncKey = in.readString();

View File

@ -1587,7 +1587,7 @@ public abstract class EmailContent {
/** /**
* Ringtone * 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 @Deprecated
public static final String RINGTONE_URI = "ringtoneUri"; public static final String RINGTONE_URI = "ringtoneUri";

View File

@ -16,21 +16,19 @@
package com.android.email.preferences; package com.android.email.preferences;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.preference.PreferenceManager;
import android.text.TextUtils; import android.text.TextUtils;
import com.android.email.Preferences; import com.android.email.Preferences;
import com.android.emailcommon.provider.EmailContent; import com.android.emailcommon.provider.EmailContent;
import com.android.emailcommon.provider.EmailContent.AccountColumns;
import com.android.mail.preferences.BasePreferenceMigrator; import com.android.mail.preferences.BasePreferenceMigrator;
import com.android.mail.preferences.FolderPreferences; import com.android.mail.preferences.FolderPreferences;
import com.android.mail.preferences.MailPrefs; import com.android.mail.preferences.MailPrefs;
import com.android.mail.providers.Account; import com.android.mail.providers.Account;
import com.android.mail.providers.Folder; import com.android.mail.providers.Folder;
import com.android.mail.providers.UIProvider; import com.android.mail.providers.UIProvider;
import com.android.mail.utils.LogTag;
import com.android.mail.utils.LogUtils; import com.android.mail.utils.LogUtils;
import java.util.ArrayList; import java.util.ArrayList;
@ -41,7 +39,7 @@ import java.util.Set;
* Migrates Email settings to UnifiedEmail * Migrates Email settings to UnifiedEmail
*/ */
public class EmailPreferenceMigrator extends BasePreferenceMigrator { public class EmailPreferenceMigrator extends BasePreferenceMigrator {
private static final String LOG_TAG = LogTag.getLogTag(); private static final String LOG_TAG = "EmailPrefMigrator";
@Override @Override
protected void migrate(final Context context, final int oldVersion, final int newVersion) { 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); 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, protected static void migrate(final Context context, final int oldVersion, final int newVersion,
final List<Account> accounts) { final List<Account> accounts) {
final Preferences preferences = Preferences.getPreferences(context); final Preferences preferences = Preferences.getPreferences(context);
@ -100,6 +93,34 @@ public class EmailPreferenceMigrator extends BasePreferenceMigrator {
// Move folder notification settings // Move folder notification settings
for (final Account account : accounts) { 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, // The only setting in AccountPreferences so far is a global notification toggle,
// but we only allow Inbox notifications, so it will remain unused // but we only allow Inbox notifications, so it will remain unused
final Cursor folderCursor = final Cursor folderCursor =
@ -108,7 +129,8 @@ public class EmailPreferenceMigrator extends BasePreferenceMigrator {
if (folderCursor == null) { if (folderCursor == null) {
LogUtils.e(LOG_TAG, "Null folder cursor for mailbox %s", LogUtils.e(LOG_TAG, "Null folder cursor for mailbox %s",
account.settings.defaultInbox); LogUtils.sanitizeName(LOG_TAG,
account.settings.defaultInbox.toString()));
continue; continue;
} }
@ -124,28 +146,19 @@ public class EmailPreferenceMigrator extends BasePreferenceMigrator {
final FolderPreferences folderPreferences = final FolderPreferences folderPreferences =
new FolderPreferences(context, account.name, folder, true /* inbox */); new FolderPreferences(context, account.name, folder, true /* inbox */);
final SharedPreferences sharedPreferences = @SuppressWarnings("deprecation")
PreferenceManager.getDefaultSharedPreferences(context); final boolean notify = (ecAccount.getFlags()
& com.android.emailcommon.provider.Account.FLAGS_NOTIFY_NEW_MAIL) != 0;
folderPreferences.setNotificationsEnabled(notify);
if (sharedPreferences.contains(PREFERENCE_NOTIFY)) { @SuppressWarnings("deprecation")
final boolean notify = sharedPreferences.getBoolean(PREFERENCE_NOTIFY, true); final String ringtoneUri = ecAccount.getRingtone();
folderPreferences.setNotificationsEnabled(notify); folderPreferences.setNotificationRingtoneUri(ringtoneUri);
}
if (sharedPreferences.contains(PREFERENCE_RINGTONE)) { @SuppressWarnings("deprecation")
final String ringtoneUri = final boolean vibrate = (ecAccount.getFlags()
sharedPreferences.getString(PREFERENCE_RINGTONE, null); & com.android.emailcommon.provider.Account.FLAGS_VIBRATE) != 0;
folderPreferences.setNotificationRingtoneUri(ringtoneUri); folderPreferences.setNotificationVibrateEnabled(vibrate);
}
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);
}
folderPreferences.commit(); folderPreferences.commit();
} }