diff options
author | Tony Mantler <nicoya@google.com> | 2014-05-14 11:16:56 -0700 |
---|---|---|
committer | Tony Mantler <nicoya@google.com> | 2014-05-14 15:15:49 -0700 |
commit | 82a207132b34377d532f19882f5bfc70bc657da0 (patch) | |
tree | 1d3a92af895448003de3b0aec6f56ce5bdf77791 /emailcommon | |
parent | 97ddf6e5fa7cf188588cc0283db54cd6cf1356b0 (diff) | |
download | android_packages_apps_Email-82a207132b34377d532f19882f5bfc70bc657da0.tar.gz android_packages_apps_Email-82a207132b34377d532f19882f5bfc70bc657da0.tar.bz2 android_packages_apps_Email-82a207132b34377d532f19882f5bfc70bc657da0.zip |
Move account backup/restore to userdata in AccountManager
Change-Id: Iea9f2a1b1f2d87e07d63cbb1df5a0d6355ea4031
Diffstat (limited to 'emailcommon')
5 files changed, 205 insertions, 41 deletions
diff --git a/emailcommon/src/com/android/emailcommon/provider/Account.java b/emailcommon/src/com/android/emailcommon/provider/Account.java index b984035a3..615b47090 100755 --- a/emailcommon/src/com/android/emailcommon/provider/Account.java +++ b/emailcommon/src/com/android/emailcommon/provider/Account.java @@ -34,6 +34,10 @@ import android.os.Parcelable; import android.os.RemoteException; import com.android.emailcommon.utility.Utility; +import com.android.mail.utils.LogUtils; + +import org.json.JSONException; +import org.json.JSONObject; import java.util.ArrayList; @@ -132,12 +136,14 @@ public final class Account extends EmailContent implements Parcelable { @Deprecated private String mRingtoneUri; public String mProtocolVersion; - public int mNewMessageCount; public String mSecuritySyncKey; public String mSignature; public long mPolicyKey; public long mPingDuration; + private static final String JSON_TAG_HOST_AUTH_RECV = "hostAuthRecv"; + private static final String JSON_TAG_HOST_AUTH_SEND = "hostAuthSend"; + // Convenience for creating/working with an account public transient HostAuth mHostAuthRecv; public transient HostAuth mHostAuthSend; @@ -727,7 +733,6 @@ public final class Account extends EmailContent implements Parcelable { 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); values.put(AccountColumns.SIGNATURE, mSignature); values.put(AccountColumns.POLICY_KEY, mPolicyKey); @@ -735,6 +740,96 @@ public final class Account extends EmailContent implements Parcelable { return values; } + public String toJsonString(final Context context) { + ensureLoaded(context); + final JSONObject json = toJson(); + if (json != null) { + return json.toString(); + } + return null; + } + + protected JSONObject toJson() { + try { + final JSONObject json = new JSONObject(); + json.putOpt(AccountColumns.DISPLAY_NAME, mDisplayName); + json.put(AccountColumns.EMAIL_ADDRESS, mEmailAddress); + json.put(AccountColumns.SYNC_LOOKBACK, mSyncLookback); + json.put(AccountColumns.SYNC_INTERVAL, mSyncInterval); + final JSONObject recvJson = mHostAuthRecv.toJson(); + json.put(JSON_TAG_HOST_AUTH_RECV, recvJson); + if (mHostAuthSend != null) { + final JSONObject sendJson = mHostAuthSend.toJson(); + json.put(JSON_TAG_HOST_AUTH_SEND, sendJson); + } + json.put(AccountColumns.FLAGS, mFlags); + json.putOpt(AccountColumns.SENDER_NAME, mSenderName); + json.putOpt(AccountColumns.PROTOCOL_VERSION, mProtocolVersion); + json.putOpt(AccountColumns.SIGNATURE, mSignature); + json.put(AccountColumns.PING_DURATION, mPingDuration); + return json; + } catch (final JSONException e) { + LogUtils.d(LogUtils.TAG, e, "Exception while serializing Account"); + } + return null; + } + + public static Account fromJsonString(final String jsonString) { + try { + final JSONObject json = new JSONObject(jsonString); + return fromJson(json); + } catch (final JSONException e) { + LogUtils.d(LogUtils.TAG, e, "Could not parse json for account"); + } + return null; + } + + protected static Account fromJson(final JSONObject json) { + try { + final Account a = new Account(); + a.mDisplayName = json.optString(AccountColumns.DISPLAY_NAME); + a.mEmailAddress = json.getString(AccountColumns.EMAIL_ADDRESS); + // SYNC_KEY is not stored + a.mSyncLookback = json.getInt(AccountColumns.SYNC_LOOKBACK); + a.mSyncInterval = json.getInt(AccountColumns.SYNC_INTERVAL); + final JSONObject recvJson = json.getJSONObject(JSON_TAG_HOST_AUTH_RECV); + a.mHostAuthRecv = HostAuth.fromJson(recvJson); + final JSONObject sendJson = json.optJSONObject(JSON_TAG_HOST_AUTH_SEND); + if (sendJson != null) { + a.mHostAuthSend = HostAuth.fromJson(sendJson); + } + a.mFlags = json.getInt(AccountColumns.FLAGS); + a.mSenderName = json.optString(AccountColumns.SENDER_NAME); + a.mProtocolVersion = json.optString(AccountColumns.PROTOCOL_VERSION); + // SECURITY_SYNC_KEY is not stored + a.mSignature = json.optString(AccountColumns.SIGNATURE); + // POLICY_KEY is not stored + a.mPingDuration = json.optInt(AccountColumns.PING_DURATION, 0); + return a; + } catch (final JSONException e) { + LogUtils.d(LogUtils.TAG, e, "Exception while deserializing Account"); + } + return null; + } + + /** + * Ensure that all optionally-loaded fields are populated from the provider. + * @param context for provider loads + */ + public void ensureLoaded(final Context context) { + if (mHostAuthKeyRecv == 0 && mHostAuthRecv == null) { + throw new IllegalStateException("Trying to load incomplete Account object"); + } + getOrCreateHostAuthRecv(context).ensureLoaded(context); + + if (mHostAuthKeySend != 0) { + getOrCreateHostAuthSend(context); + if (mHostAuthSend != null) { + mHostAuthSend.ensureLoaded(context); + } + } + } + /** * Supports Parcelable */ @@ -778,7 +873,7 @@ public final class Account extends EmailContent implements Parcelable { dest.writeString(mSenderName); dest.writeString(mRingtoneUri); dest.writeString(mProtocolVersion); - dest.writeInt(mNewMessageCount); + dest.writeInt(0 /* mNewMessageCount */); dest.writeString(mSecuritySyncKey); dest.writeString(mSignature); dest.writeLong(mPolicyKey); @@ -816,7 +911,7 @@ public final class Account extends EmailContent implements Parcelable { mSenderName = in.readString(); mRingtoneUri = in.readString(); mProtocolVersion = in.readString(); - mNewMessageCount = in.readInt(); + /* mNewMessageCount = */ in.readInt(); mSecuritySyncKey = in.readString(); mSignature = in.readString(); mPolicyKey = in.readLong(); diff --git a/emailcommon/src/com/android/emailcommon/provider/Credential.java b/emailcommon/src/com/android/emailcommon/provider/Credential.java index d7961a29f..bf48693bb 100644 --- a/emailcommon/src/com/android/emailcommon/provider/Credential.java +++ b/emailcommon/src/com/android/emailcommon/provider/Credential.java @@ -9,10 +9,12 @@ import android.os.Parcelable; import android.provider.BaseColumns; import android.text.TextUtils; -import com.android.emailcommon.utility.Utility; import com.android.mail.utils.LogUtils; import com.google.common.base.Objects; +import org.json.JSONException; +import org.json.JSONObject; + public class Credential extends EmailContent implements Parcelable, BaseColumns { public static final String TABLE_NAME = "Credential"; @@ -143,9 +145,9 @@ public class Credential extends EmailContent implements Parcelable, BaseColumns return false; } Credential that = (Credential)o; - return Utility.areStringsEqual(mProviderId, that.mProviderId) - && Utility.areStringsEqual(mAccessToken, that.mAccessToken) - && Utility.areStringsEqual(mRefreshToken, that.mRefreshToken) + return TextUtils.equals(mProviderId, that.mProviderId) + && TextUtils.equals(mAccessToken, that.mAccessToken) + && TextUtils.equals(mRefreshToken, that.mRefreshToken) && mExpiration == that.mExpiration; } @@ -166,4 +168,32 @@ public class Credential extends EmailContent implements Parcelable, BaseColumns values.put(EXPIRATION_COLUMN, mExpiration); return values; } + + protected JSONObject toJson() { + try { + final JSONObject json = new JSONObject(); + json.put(PROVIDER_COLUMN, mProviderId); + json.putOpt(ACCESS_TOKEN_COLUMN, mAccessToken); + json.putOpt(REFRESH_TOKEN_COLUMN, mRefreshToken); + json.put(EXPIRATION_COLUMN, mExpiration); + return json; + } catch (final JSONException e) { + LogUtils.d(LogUtils.TAG, e, "Exception while serializing Credential"); + } + return null; + } + + protected static Credential fromJson(final JSONObject json) { + try { + final Credential c = new Credential(); + c.mProviderId = json.getString(PROVIDER_COLUMN); + c.mAccessToken = json.optString(ACCESS_TOKEN_COLUMN); + c.mRefreshToken = json.optString(REFRESH_TOKEN_COLUMN); + c.mExpiration = json.optInt(EXPIRATION_COLUMN, 0); + return c; + } catch (final JSONException e) { + LogUtils.d(LogUtils.TAG, e, "Exception while deserializing Credential"); + } + return null; + } } diff --git a/emailcommon/src/com/android/emailcommon/provider/EmailContent.java b/emailcommon/src/com/android/emailcommon/provider/EmailContent.java index 0df42913c..697db269e 100755 --- a/emailcommon/src/com/android/emailcommon/provider/EmailContent.java +++ b/emailcommon/src/com/android/emailcommon/provider/EmailContent.java @@ -92,10 +92,6 @@ public abstract class EmailContent { public static final String ID_SELECTION = BaseColumns._ID + " =?"; - public static final String FIELD_COLUMN_NAME = "field"; - public static final String ADD_COLUMN_NAME = "add"; - public static final String SET_COLUMN_NAME = "set"; - public static final int SYNC_STATUS_NONE = UIProvider.SyncStatus.NO_SYNC; public static final int SYNC_STATUS_USER = UIProvider.SyncStatus.USER_REFRESH; public static final int SYNC_STATUS_BACKGROUND = UIProvider.SyncStatus.BACKGROUND_SYNC; @@ -1695,6 +1691,7 @@ public abstract class EmailContent { // Protocol version (arbitrary string, used by EAS currently) public static final String PROTOCOL_VERSION = "protocolVersion"; // The number of new messages (reported by the sync/download engines + @Deprecated public static final String NEW_MESSAGE_COUNT = "newMessageCount"; // Legacy flags defining security (provisioning) requirements of this account; this // information is now found in the Policy table; POLICY_KEY (below) is the foreign key diff --git a/emailcommon/src/com/android/emailcommon/provider/HostAuth.java b/emailcommon/src/com/android/emailcommon/provider/HostAuth.java index 14effc20a..b904c66da 100644 --- a/emailcommon/src/com/android/emailcommon/provider/HostAuth.java +++ b/emailcommon/src/com/android/emailcommon/provider/HostAuth.java @@ -25,9 +25,11 @@ import android.os.Parcel; import android.os.Parcelable; import android.text.TextUtils; -import com.android.emailcommon.provider.EmailContent.HostAuthColumns; import com.android.emailcommon.utility.SSLUtils; -import com.android.emailcommon.utility.Utility; +import com.android.mail.utils.LogUtils; + +import org.json.JSONException; +import org.json.JSONObject; import java.net.URI; import java.net.URISyntaxException; @@ -69,6 +71,7 @@ public class HostAuth extends EmailContent implements Parcelable { public byte[] mServerCert = null; public long mCredentialKey; + private static final String JSON_TAG_CREDENTIAL = "credential"; public transient Credential mCredential; public static final int CONTENT_ID_COLUMN = 0; @@ -83,8 +86,8 @@ public class HostAuth extends EmailContent implements Parcelable { public static final int CONTENT_CREDENTIAL_KEY_COLUMN = 9; public static final String[] CONTENT_PROJECTION = new String[] { - RECORD_ID, HostAuthColumns.PROTOCOL, HostAuthColumns.ADDRESS, HostAuthColumns.PORT, - HostAuthColumns.FLAGS, HostAuthColumns.LOGIN, + HostAuthColumns._ID, HostAuthColumns.PROTOCOL, HostAuthColumns.ADDRESS, + HostAuthColumns.PORT, HostAuthColumns.FLAGS, HostAuthColumns.LOGIN, HostAuthColumns.PASSWORD, HostAuthColumns.DOMAIN, HostAuthColumns.CLIENT_CERT_ALIAS, HostAuthColumns.CREDENTIAL_KEY }; @@ -97,8 +100,8 @@ public class HostAuth extends EmailContent implements Parcelable { /** * Restore a HostAuth from the database, given its unique id - * @param context - * @param id + * @param context for provider loads + * @param id corresponds to rowid * @return the instantiated HostAuth */ public static HostAuth restoreHostAuthWithId(Context context, long id) { @@ -107,13 +110,6 @@ public class HostAuth extends EmailContent implements Parcelable { } /** - * Returns the scheme for the specified flags. - */ - public static String getSchemeString(String protocol, int flags) { - return getSchemeString(protocol, flags, null); - } - - /** * Returns the credential object for this HostAuth. This will load from the * database if the HosAuth has a valid credential key, or return null if not. */ @@ -131,7 +127,7 @@ public class HostAuth extends EmailContent implements Parcelable { * creating it if it does not yet exist. This should not be called on the * main thread. * - * @param context + * @param context for provider loads * @return the credential object for this HostAuth */ public Credential getOrCreateCredential(Context context) { @@ -157,7 +153,11 @@ public class HostAuth extends EmailContent implements Parcelable { * Builds a URI scheme name given the parameters for a {@code HostAuth}. If * a {@code clientAlias} is provided, this indicates that a secure * connection must be used. + * + * This is not used in live code, but is kept here for reference when creating providers.xml + * entries */ + @SuppressWarnings("unused") public static String getSchemeString(String protocol, int flags, String clientAlias) { String security = ""; switch (flags & USER_CONFIG_MASK) { @@ -244,6 +244,57 @@ public class HostAuth extends EmailContent implements Parcelable { return values; } + protected JSONObject toJson() { + try { + final JSONObject json = new JSONObject(); + json.put(HostAuthColumns.PROTOCOL, mProtocol); + json.put(HostAuthColumns.ADDRESS, mAddress); + json.put(HostAuthColumns.PORT, mPort); + json.put(HostAuthColumns.FLAGS, mFlags); + json.put(HostAuthColumns.LOGIN, mLogin); + json.putOpt(HostAuthColumns.PASSWORD, mPassword); + json.putOpt(HostAuthColumns.DOMAIN, mDomain); + json.putOpt(HostAuthColumns.CLIENT_CERT_ALIAS, mClientCertAlias); + if (mCredential != null) { + json.putOpt(JSON_TAG_CREDENTIAL, mCredential.toJson()); + } + return json; + } catch (final JSONException e) { + LogUtils.d(LogUtils.TAG, e, "Exception while serializing HostAuth"); + } + return null; + } + + protected static HostAuth fromJson(final JSONObject json) { + try { + final HostAuth h = new HostAuth(); + h.mProtocol = json.getString(HostAuthColumns.PROTOCOL); + h.mAddress = json.getString(HostAuthColumns.ADDRESS); + h.mPort = json.getInt(HostAuthColumns.PORT); + h.mFlags = json.getInt(HostAuthColumns.FLAGS); + h.mLogin = json.getString(HostAuthColumns.LOGIN); + h.mPassword = json.optString(HostAuthColumns.PASSWORD); + h.mDomain = json.optString(HostAuthColumns.DOMAIN); + h.mClientCertAlias = json.optString(HostAuthColumns.CLIENT_CERT_ALIAS); + final JSONObject credJson = json.optJSONObject(JSON_TAG_CREDENTIAL); + if (credJson != null) { + h.mCredential = Credential.fromJson(credJson); + } + return h; + } catch (final JSONException e) { + LogUtils.d(LogUtils.TAG, e, "Exception while deserializing HostAuth"); + } + return null; + } + + /** + * Ensure that all optionally-loaded fields are populated from the provider. + * @param context for provider loads + */ + public void ensureLoaded(final Context context) { + getCredential(context); + } + /** * Sets the user name and password from URI user info string */ @@ -432,12 +483,12 @@ public class HostAuth extends EmailContent implements Parcelable { return mPort == that.mPort && mId == that.mId && mFlags == that.mFlags - && Utility.areStringsEqual(mProtocol, that.mProtocol) - && Utility.areStringsEqual(mAddress, that.mAddress) - && Utility.areStringsEqual(mLogin, that.mLogin) - && Utility.areStringsEqual(mPassword, that.mPassword) - && Utility.areStringsEqual(mDomain, that.mDomain) - && Utility.areStringsEqual(mClientCertAlias, that.mClientCertAlias); + && TextUtils.equals(mProtocol, that.mProtocol) + && TextUtils.equals(mAddress, that.mAddress) + && TextUtils.equals(mLogin, that.mLogin) + && TextUtils.equals(mPassword, that.mPassword) + && TextUtils.equals(mDomain, that.mDomain) + && TextUtils.equals(mClientCertAlias, that.mClientCertAlias); // We don't care about the server certificate for equals } diff --git a/emailcommon/src/com/android/emailcommon/utility/Utility.java b/emailcommon/src/com/android/emailcommon/utility/Utility.java index 91552ec03..55ac448dc 100644 --- a/emailcommon/src/com/android/emailcommon/utility/Utility.java +++ b/emailcommon/src/com/android/emailcommon/utility/Utility.java @@ -780,15 +780,6 @@ public class Utility { return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); } - /** - * Test that the given strings are equal in a null-pointer safe fashion. - */ - // Use TextUtils.equals() - @Deprecated - public static boolean areStringsEqual(String s1, String s2) { - return (s1 != null && s1.equals(s2)) || (s1 == null && s2 == null); - } - public static void enableStrictMode(boolean enabled) { StrictMode.setThreadPolicy(enabled ? new StrictMode.ThreadPolicy.Builder().detectAll().build() |