2010-04-24 10:59:27 -04:00
|
|
|
package com.fsck.k9.activity;
|
|
|
|
|
2011-02-19 13:59:38 -05:00
|
|
|
import android.os.Parcel;
|
|
|
|
import android.os.Parcelable;
|
2011-02-03 01:32:29 -05:00
|
|
|
import android.util.Log;
|
|
|
|
import com.fsck.k9.K9;
|
|
|
|
import com.fsck.k9.helper.Utility;
|
|
|
|
import com.fsck.k9.mail.Flag;
|
|
|
|
import com.fsck.k9.mail.MessagingException;
|
|
|
|
|
|
|
|
import java.util.StringTokenizer;
|
2010-04-24 10:59:27 -04:00
|
|
|
|
2011-02-19 13:59:38 -05:00
|
|
|
public class MessageReference implements Parcelable {
|
2010-05-01 16:06:52 -04:00
|
|
|
public String accountUuid;
|
|
|
|
public String folderName;
|
|
|
|
public String uid;
|
2011-02-03 01:32:29 -05:00
|
|
|
public Flag flag = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize an empty MessageReference.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public MessageReference() {
|
2011-02-03 01:32:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Version identifier for use when serializing. This will allow us to introduce future versions
|
|
|
|
// if we have to rev MessageReference.
|
|
|
|
private static final String IDENTITY_VERSION_1 = "!";
|
|
|
|
private static final String IDENTITY_SEPARATOR = ":";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a MessageReference from a seraialized identity.
|
|
|
|
* @param identity Serialized identity.
|
|
|
|
* @throws MessagingException On missing or corrupted identity.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public MessageReference(final String identity) throws MessagingException {
|
2011-02-03 01:32:29 -05:00
|
|
|
// Can't be null and must be at least length one so we can check the version.
|
2011-02-06 17:09:48 -05:00
|
|
|
if (identity == null || identity.length() < 1) {
|
2011-02-03 01:32:29 -05:00
|
|
|
throw new MessagingException("Null or truncated MessageReference identity.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version check.
|
2011-02-06 17:09:48 -05:00
|
|
|
if (identity.charAt(0) == IDENTITY_VERSION_1.charAt(0)) {
|
2011-02-03 01:32:29 -05:00
|
|
|
// Split the identity, stripping away the first two characters representing the version and delimiter.
|
|
|
|
StringTokenizer tokens = new StringTokenizer(identity.substring(2), IDENTITY_SEPARATOR, false);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (tokens.countTokens() >= 3) {
|
2011-02-03 01:32:29 -05:00
|
|
|
accountUuid = Utility.base64Decode(tokens.nextToken());
|
|
|
|
folderName = Utility.base64Decode(tokens.nextToken());
|
|
|
|
uid = Utility.base64Decode(tokens.nextToken());
|
2011-02-04 10:41:39 -05:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (tokens.hasMoreTokens()) {
|
2011-02-04 10:41:39 -05:00
|
|
|
final String flagString = tokens.nextToken();
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
2011-02-04 10:41:39 -05:00
|
|
|
flag = Flag.valueOf(flagString);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (IllegalArgumentException ie) {
|
2011-02-04 10:41:39 -05:00
|
|
|
throw new MessagingException("Could not thaw message flag '" + flagString + "'", ie);
|
|
|
|
}
|
|
|
|
}
|
2011-02-03 01:32:29 -05:00
|
|
|
|
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Thawed " + toString());
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2011-02-04 10:41:39 -05:00
|
|
|
throw new MessagingException("Invalid MessageReference in " + identity + " identity.");
|
2011-02-03 01:32:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize this MessageReference for storing in a K9 identity. This is a colon-delimited base64 string.
|
|
|
|
*
|
|
|
|
* @return Serialized string.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public String toIdentityString() {
|
2011-02-03 01:32:29 -05:00
|
|
|
StringBuilder refString = new StringBuilder();
|
|
|
|
|
|
|
|
refString.append(IDENTITY_VERSION_1);
|
|
|
|
refString.append(IDENTITY_SEPARATOR);
|
|
|
|
refString.append(Utility.base64Encode(accountUuid));
|
|
|
|
refString.append(IDENTITY_SEPARATOR);
|
|
|
|
refString.append(Utility.base64Encode(folderName));
|
|
|
|
refString.append(IDENTITY_SEPARATOR);
|
|
|
|
refString.append(Utility.base64Encode(uid));
|
2011-02-06 17:09:48 -05:00
|
|
|
if (flag != null) {
|
2011-02-04 10:41:39 -05:00
|
|
|
refString.append(IDENTITY_SEPARATOR);
|
|
|
|
refString.append(flag.name());
|
|
|
|
}
|
2011-02-03 01:32:29 -05:00
|
|
|
|
|
|
|
return refString.toString();
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-05-03 09:46:55 -04:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (o instanceof MessageReference == false) {
|
2010-04-24 10:59:27 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MessageReference other = (MessageReference)o;
|
|
|
|
if ((accountUuid == other.accountUuid || (accountUuid != null && accountUuid.equals(other.accountUuid)))
|
|
|
|
&& (folderName == other.folderName || (folderName != null && folderName.equals(other.folderName)))
|
2011-02-06 17:09:48 -05:00
|
|
|
&& (uid == other.uid || (uid != null && uid.equals(other.uid)))) {
|
2010-04-24 10:59:27 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-03 09:46:55 -04:00
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public int hashCode() {
|
2010-05-03 09:46:55 -04:00
|
|
|
final int MULTIPLIER = 31;
|
|
|
|
|
|
|
|
int result = 1;
|
|
|
|
result = MULTIPLIER * result + ((accountUuid == null) ? 0 : accountUuid.hashCode());
|
|
|
|
result = MULTIPLIER * result + ((folderName == null) ? 0 : folderName.hashCode());
|
|
|
|
result = MULTIPLIER * result + ((uid == null) ? 0 : uid.hashCode());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public String toString() {
|
2011-02-03 01:32:29 -05:00
|
|
|
return "MessageReference{" +
|
2011-02-06 17:09:48 -05:00
|
|
|
"accountUuid='" + accountUuid + '\'' +
|
|
|
|
", folderName='" + folderName + '\'' +
|
|
|
|
", uid='" + uid + '\'' +
|
|
|
|
", flag=" + flag +
|
|
|
|
'}';
|
2010-04-24 10:59:27 -04:00
|
|
|
}
|
2011-02-19 13:59:38 -05:00
|
|
|
|
|
|
|
public static final Creator<MessageReference> CREATOR = new Creator<MessageReference>() {
|
|
|
|
@Override
|
|
|
|
public MessageReference createFromParcel(Parcel source) {
|
|
|
|
MessageReference ref = new MessageReference();
|
|
|
|
ref.uid = source.readString();
|
|
|
|
ref.accountUuid = source.readString();
|
|
|
|
ref.folderName = source.readString();
|
|
|
|
String flag = source.readString();
|
|
|
|
if (flag != null) ref.flag = Flag.valueOf(flag);
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MessageReference[] newArray(int size) {
|
|
|
|
return new MessageReference[size];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToParcel(Parcel dest, int flags) {
|
|
|
|
dest.writeString(uid);
|
|
|
|
dest.writeString(accountUuid);
|
|
|
|
dest.writeString(folderName);
|
|
|
|
dest.writeString(flag == null ? null : flag.name());
|
|
|
|
}
|
2010-04-24 10:59:27 -04:00
|
|
|
}
|