implemented GNU social media preview

This commit is contained in:
Mariotaku Lee 2016-01-26 22:42:16 +08:00
parent 4831f3db38
commit d7e14a0d02
5 changed files with 140 additions and 25 deletions

View File

@ -0,0 +1,71 @@
package org.mariotaku.twidere.api.gnusocial.model;
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
/**
* Created by mariotaku on 16/1/26.
*/
@JsonObject
public class Attachment {
@JsonField(name = "width")
int width;
@JsonField(name = "height")
int height;
@JsonField(name = "url")
String url;
@JsonField(name = "thumb_url")
String thumbUrl;
@JsonField(name = "large_thumb_url")
String largeThumbUrl;
@JsonField(name = "mimetype")
String mimetype;
@JsonField(name = "id")
long id;
@JsonField(name = "oembed")
boolean oembed;
@JsonField(name = "size")
long size;
@JsonField(name = "version")
String version;
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public String getUrl() {
return url;
}
public String getThumbUrl() {
return thumbUrl;
}
public String getLargeThumbUrl() {
return largeThumbUrl;
}
public String getMimetype() {
return mimetype;
}
public long getId() {
return id;
}
public boolean isOembed() {
return oembed;
}
public long getSize() {
return size;
}
public String getVersion() {
return version;
}
}

View File

@ -22,6 +22,8 @@ package org.mariotaku.twidere.api.twitter.model;
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import org.mariotaku.twidere.api.gnusocial.model.Attachment;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

View File

@ -26,6 +26,7 @@ import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import com.bluelinelabs.logansquare.annotation.OnJsonParseComplete;
import org.mariotaku.twidere.api.gnusocial.model.Attachment;
import org.mariotaku.twidere.api.twitter.util.TwitterDateConverter;
import java.io.IOException;
@ -116,10 +117,12 @@ public class Status extends TwitterResponseObject implements Comparable<Status>,
@JsonField(name = "possibly_sensitive")
boolean possiblySensitive;
public static void setQuotedStatus(Status status, Status quoted) {
if (status == null) return;
status.quotedStatus = quoted;
}
@JsonField(name = "attachments")
Attachment[] attachments;
@JsonField(name = "external_url")
String externalUrl;
public User getUser() {
@ -294,7 +297,15 @@ public class Status extends TwitterResponseObject implements Comparable<Status>,
return contributors;
}
public Attachment[] getAttachments() {
return attachments;
}
public String getExternalUrl() {
return externalUrl;
}
@Override
public int compareTo(@NonNull final Status that) {
final long delta = id - that.getId();
if (delta < Integer.MIN_VALUE)
@ -421,6 +432,12 @@ public class Status extends TwitterResponseObject implements Comparable<Status>,
return lang;
}
public static void setQuotedStatus(Status status, Status quoted) {
if (status == null) return;
status.quotedStatus = quoted;
}
@JsonObject
public static class CurrentUserRetweet {
@JsonField(name = "id")

View File

@ -5,6 +5,7 @@ import android.os.Parcelable;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
@ -13,10 +14,12 @@ import com.hannesdorfmann.parcelableplease.annotation.ParcelableNoThanks;
import com.hannesdorfmann.parcelableplease.annotation.ParcelablePlease;
import com.hannesdorfmann.parcelableplease.annotation.ParcelableThisPlease;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.math.NumberUtils;
import org.mariotaku.twidere.api.gnusocial.model.Attachment;
import org.mariotaku.twidere.api.twitter.model.CardEntity;
import org.mariotaku.twidere.api.twitter.model.CardEntity.BindingValue;
import org.mariotaku.twidere.api.twitter.model.CardEntity.ImageValue;
@ -112,9 +115,9 @@ public class ParcelableMedia implements Parcelable {
type = update.type;
}
@Nullable
@NonNull
public static ParcelableMedia[] fromEntities(@Nullable final EntitySupport entities) {
if (entities == null) return null;
if (entities == null) return new ParcelableMedia[0];
final List<ParcelableMedia> list = new ArrayList<>();
final MediaEntity[] mediaEntities;
if (entities instanceof ExtendedEntitySupport) {
@ -144,7 +147,6 @@ public class ParcelableMedia implements Parcelable {
}
}
}
if (list.isEmpty()) return null;
return list.toArray(new ParcelableMedia[list.size()]);
}
@ -160,22 +162,43 @@ public class ParcelableMedia implements Parcelable {
}
@Nullable
public static ParcelableMedia[] fromStatus(final Status status) {
public static ParcelableMedia[] fromStatus(@NonNull final Status status) {
final ParcelableMedia[] fromEntities = fromEntities(status);
final ParcelableMedia[] fromAttachments = fromAttachments(status);
final ParcelableMedia[] fromCard = fromCard(status.getCard(), status.getUrlEntities());
if (fromEntities == null) {
return fromCard;
} else if (fromCard == null) {
return fromEntities;
}
final ParcelableMedia[] merged = new ParcelableMedia[fromCard.length + fromEntities.length];
TwidereArrayUtils.mergeArray(merged, fromEntities, fromCard);
final ParcelableMedia[] merged = new ParcelableMedia[fromCard.length +
fromAttachments.length + fromEntities.length];
TwidereArrayUtils.mergeArray(merged, fromEntities, fromAttachments, fromCard);
return merged;
}
@Nullable
@NonNull
private static ParcelableMedia[] fromAttachments(@NonNull Status status) {
final Attachment[] attachments = status.getAttachments();
if (attachments == null) return new ParcelableMedia[0];
final ParcelableMedia[] temp = new ParcelableMedia[attachments.length];
final String externalUrl = status.getExternalUrl();
int i = 0;
for (Attachment attachment : attachments) {
final String mimetype = attachment.getMimetype();
if (mimetype != null && mimetype.startsWith("image/")) {
ParcelableMedia media = new ParcelableMedia();
media.type = Type.TYPE_IMAGE;
media.width = attachment.getWidth();
media.height = attachment.getHeight();
media.url = TextUtils.isEmpty(externalUrl) ? attachment.getUrl() : externalUrl;
media.page_url = TextUtils.isEmpty(externalUrl) ? attachment.getUrl() : externalUrl;
media.media_url = attachment.getUrl();
media.preview_url = attachment.getLargeThumbUrl();
temp[i++] = media;
}
}
return ArrayUtils.subarray(temp, 0, i);
}
@NonNull
private static ParcelableMedia[] fromCard(@Nullable CardEntity card, @Nullable UrlEntity[] entities) {
if (card == null) return null;
if (card == null) return new ParcelableMedia[0];
final String name = card.getName();
if ("animated_gif".equals(name) || "player".equals(name)) {
final ParcelableMedia media = new ParcelableMedia();
@ -221,7 +244,7 @@ public class ParcelableMedia implements Parcelable {
} else if ("summary_large_image".equals(name)) {
final BindingValue photoImageFullSize = card.getBindingValue("photo_image_full_size");
if (!(photoImageFullSize instanceof ImageValue))
return null;
return new ParcelableMedia[0];
final ParcelableMedia media = new ParcelableMedia();
media.url = card.getUrl();
@ -245,7 +268,7 @@ public class ParcelableMedia implements Parcelable {
}
return new ParcelableMedia[]{media};
}
return null;
return new ParcelableMedia[0];
}
private static int getTypeInt(String type) {

View File

@ -24,6 +24,7 @@ import android.text.TextUtils;
import org.apache.commons.lang3.ArrayUtils;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
@ -86,17 +87,18 @@ public final class TwidereArrayUtils {
return fromList(list1);
}
public static void mergeArray(final Object[] dest, final Object[]... arrays) {
@SuppressWarnings("SuspiciousSystemArraycopy")
public static void mergeArray(final Object dest, final Object... arrays) {
if (arrays == null || arrays.length == 0) return;
if (arrays.length == 1) {
final Object[] array = arrays[0];
System.arraycopy(array, 0, dest, 0, array.length);
final Object array = arrays[0];
System.arraycopy(array, 0, dest, 0, Array.getLength(array));
return;
}
for (int i = 0, j = arrays.length - 1; i < j; i++) {
final Object[] array1 = arrays[i], array2 = arrays[i + 1];
System.arraycopy(array1, 0, dest, 0, array1.length);
System.arraycopy(array2, 0, dest, array1.length, array2.length);
final Object array1 = arrays[i], array2 = arrays[i + 1];
System.arraycopy(array1, 0, dest, 0, Array.getLength(array1));
System.arraycopy(array2, 0, dest, Array.getLength(array1), Array.getLength(array2));
}
}