2016-12-03 16:45:13 +01:00
|
|
|
package org.mariotaku.twidere.model;
|
|
|
|
|
|
|
|
import android.accounts.Account;
|
2016-12-04 04:58:03 +01:00
|
|
|
import android.os.Parcel;
|
|
|
|
import android.os.Parcelable;
|
2016-12-03 16:45:13 +01:00
|
|
|
import android.support.annotation.ColorInt;
|
2016-12-04 04:58:03 +01:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
2016-12-07 14:20:25 +01:00
|
|
|
import com.bluelinelabs.logansquare.LoganSquare;
|
|
|
|
import com.bluelinelabs.logansquare.annotation.JsonField;
|
|
|
|
import com.bluelinelabs.logansquare.annotation.JsonObject;
|
|
|
|
import com.bluelinelabs.logansquare.annotation.OnJsonParseComplete;
|
|
|
|
import com.bluelinelabs.logansquare.annotation.OnPreJsonSerialize;
|
|
|
|
import com.bluelinelabs.logansquare.typeconverters.TypeConverter;
|
|
|
|
import com.fasterxml.jackson.core.JsonGenerator;
|
|
|
|
import com.fasterxml.jackson.core.JsonParser;
|
|
|
|
import com.fasterxml.jackson.core.JsonToken;
|
|
|
|
import com.hannesdorfmann.parcelableplease.annotation.ParcelableNoThanks;
|
2016-12-04 04:58:03 +01:00
|
|
|
import com.hannesdorfmann.parcelableplease.annotation.ParcelablePlease;
|
2016-12-03 16:45:13 +01:00
|
|
|
|
|
|
|
import org.mariotaku.twidere.annotation.AccountType;
|
2016-12-04 04:58:03 +01:00
|
|
|
import org.mariotaku.twidere.model.account.AccountExtras;
|
2016-12-03 16:45:13 +01:00
|
|
|
import org.mariotaku.twidere.model.account.cred.Credentials;
|
2016-12-07 14:20:25 +01:00
|
|
|
import org.mariotaku.twidere.model.util.UserKeyConverter;
|
|
|
|
import org.mariotaku.twidere.util.model.AccountDetailsUtils;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2016-12-03 16:45:13 +01:00
|
|
|
|
|
|
|
/**
|
2017-02-04 11:42:14 +01:00
|
|
|
* Object holding account info and credentials
|
2016-12-03 16:45:13 +01:00
|
|
|
* Created by mariotaku on 2016/12/3.
|
|
|
|
*/
|
2016-12-04 04:58:03 +01:00
|
|
|
@ParcelablePlease
|
2016-12-07 14:20:25 +01:00
|
|
|
@JsonObject
|
2016-12-05 04:39:22 +01:00
|
|
|
public class AccountDetails implements Parcelable, Comparable<AccountDetails> {
|
2016-12-03 16:45:13 +01:00
|
|
|
|
2016-12-07 14:20:25 +01:00
|
|
|
@JsonField(name = "account", typeConverter = AccountConverter.class)
|
2016-12-03 16:45:13 +01:00
|
|
|
public Account account;
|
2016-12-07 14:20:25 +01:00
|
|
|
|
|
|
|
@JsonField(name = "key", typeConverter = UserKeyConverter.class)
|
2016-12-03 16:45:13 +01:00
|
|
|
public UserKey key;
|
2016-12-07 14:20:25 +01:00
|
|
|
|
|
|
|
@AccountType
|
|
|
|
@JsonField(name = "type")
|
|
|
|
public String type;
|
|
|
|
|
|
|
|
@Credentials.Type
|
|
|
|
@JsonField(name = "credentials_type")
|
|
|
|
public String credentials_type;
|
|
|
|
|
|
|
|
@JsonField(name = "user")
|
2016-12-03 16:45:13 +01:00
|
|
|
public ParcelableUser user;
|
2016-12-07 14:20:25 +01:00
|
|
|
|
2016-12-03 16:45:13 +01:00
|
|
|
@ColorInt
|
2016-12-07 14:20:25 +01:00
|
|
|
@JsonField(name = "color")
|
2016-12-03 16:45:13 +01:00
|
|
|
public int color;
|
2016-12-07 14:20:25 +01:00
|
|
|
|
|
|
|
@JsonField(name = "position")
|
2016-12-03 16:45:13 +01:00
|
|
|
public int position;
|
2016-12-07 14:20:25 +01:00
|
|
|
|
|
|
|
@JsonField(name = "activated")
|
2016-12-03 16:45:13 +01:00
|
|
|
public boolean activated;
|
2016-12-07 14:20:25 +01:00
|
|
|
|
|
|
|
@JsonField(name = "dummy")
|
|
|
|
public boolean dummy;
|
|
|
|
|
|
|
|
@JsonField(name = "credentials")
|
|
|
|
@ParcelableNoThanks
|
|
|
|
String credentials_json;
|
|
|
|
public Credentials credentials;
|
|
|
|
|
|
|
|
@JsonField(name = "extras")
|
|
|
|
@ParcelableNoThanks
|
|
|
|
String extras_json;
|
2016-12-04 04:58:03 +01:00
|
|
|
public AccountExtras extras;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToParcel(Parcel dest, int flags) {
|
|
|
|
AccountDetailsParcelablePlease.writeToParcel(this, dest, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "AccountDetails{" +
|
|
|
|
"account=" + account +
|
|
|
|
", dummy=" + dummy +
|
|
|
|
", key=" + key +
|
|
|
|
", credentials=" + credentials +
|
|
|
|
", user=" + user +
|
|
|
|
", color=" + color +
|
|
|
|
", position=" + position +
|
|
|
|
", activated=" + activated +
|
|
|
|
", type='" + type + '\'' +
|
|
|
|
", credentials_type='" + credentials_type + '\'' +
|
|
|
|
", extras=" + extras +
|
|
|
|
'}';
|
|
|
|
}
|
|
|
|
|
2016-12-05 04:39:22 +01:00
|
|
|
@Override
|
|
|
|
public int compareTo(@NonNull AccountDetails that) {
|
|
|
|
return this.position - that.position;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
public static AccountDetails dummy() {
|
|
|
|
AccountDetails dummy = new AccountDetails();
|
|
|
|
dummy.dummy = true;
|
|
|
|
return dummy;
|
|
|
|
}
|
|
|
|
|
2016-12-07 14:20:25 +01:00
|
|
|
@OnPreJsonSerialize
|
|
|
|
void onPreJsonSerialize() throws IOException {
|
|
|
|
if (credentials != null) {
|
|
|
|
credentials_json = LoganSquare.serialize(credentials);
|
|
|
|
}
|
|
|
|
if (extras != null) {
|
|
|
|
extras_json = LoganSquare.serialize(extras);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@OnJsonParseComplete
|
|
|
|
void onJsonParseComplete() throws IOException {
|
|
|
|
if (credentials_json != null && credentials_type != null) {
|
|
|
|
credentials = AccountDetailsUtils.parseCredentials(credentials_json, credentials_type);
|
|
|
|
}
|
|
|
|
if (extras_json != null && type != null) {
|
|
|
|
extras = AccountDetailsUtils.parseAccountExtras(extras_json, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-04 04:58:03 +01:00
|
|
|
public static final Creator<AccountDetails> CREATOR = new Creator<AccountDetails>() {
|
|
|
|
public AccountDetails createFromParcel(Parcel source) {
|
|
|
|
AccountDetails target = new AccountDetails();
|
|
|
|
AccountDetailsParcelablePlease.readFromParcel(target, source);
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
public AccountDetails[] newArray(int size) {
|
|
|
|
return new AccountDetails[size];
|
|
|
|
}
|
|
|
|
};
|
2016-12-07 14:20:25 +01:00
|
|
|
|
|
|
|
static class AccountConverter implements TypeConverter<Account> {
|
|
|
|
@Override
|
|
|
|
public Account parse(JsonParser jsonParser) throws IOException {
|
|
|
|
if (jsonParser.getCurrentToken() == null) {
|
|
|
|
jsonParser.nextToken();
|
|
|
|
}
|
|
|
|
if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) {
|
|
|
|
jsonParser.skipChildren();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String name = null, type = null;
|
|
|
|
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
|
|
|
|
String fieldName = jsonParser.getCurrentName();
|
|
|
|
jsonParser.nextToken();
|
|
|
|
switch (fieldName) {
|
|
|
|
case "name": {
|
|
|
|
name = jsonParser.getValueAsString(null);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "type": {
|
|
|
|
type = jsonParser.getValueAsString(null);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jsonParser.skipChildren();
|
|
|
|
}
|
|
|
|
if (name != null && type != null) {
|
|
|
|
return new Account(name, type);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void serialize(Account object, String fieldName, boolean writeFieldNameForObject, JsonGenerator jsonGenerator) throws IOException {
|
|
|
|
if (writeFieldNameForObject) {
|
|
|
|
jsonGenerator.writeFieldName(fieldName);
|
|
|
|
}
|
|
|
|
if (object == null) {
|
|
|
|
jsonGenerator.writeNull();
|
|
|
|
} else {
|
|
|
|
jsonGenerator.writeStartObject();
|
|
|
|
jsonGenerator.writeStringField("name", object.name);
|
|
|
|
jsonGenerator.writeStringField("type", object.type);
|
|
|
|
jsonGenerator.writeEndObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-03 16:45:13 +01:00
|
|
|
}
|