Skip to content

Commit

Permalink
migrated some enums into static final values
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed Jan 8, 2016
1 parent 3401ce5 commit 7d3b599
Show file tree
Hide file tree
Showing 29 changed files with 202 additions and 362 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.mariotaku.twidere.annotation;

import android.support.annotation.IntDef;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -28,34 +30,26 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface Preference {

boolean defaultBoolean() default false;

float defaultFloat() default 0;

int defaultInt() default 0;
boolean defaultBoolean() default false;

long defaultLong() default 0;
float defaultFloat() default 0;

int defaultResource() default 0;
int defaultInt() default 0;

String defaultString() default "";
long defaultLong() default 0;

boolean exportable() default true;
int defaultResource() default 0;

boolean hasDefault() default false;
String defaultString() default "";

Type type() default Type.NULL;
boolean exportable() default true;

public static enum Type {
BOOLEAN(1), INT(2), LONG(3), FLOAT(4), STRING(5), NULL(0), INVALID(-1);
private int type;
boolean hasDefault() default false;

Type(final int type) {
this.type = type;
}
@Type int type() default Type.NULL;

public int getType() {
return type;
}
}
@IntDef({Type.BOOLEAN, Type.INT, Type.LONG, Type.FLOAT, Type.STRING, Type.NULL, Type.INVALID})
@interface Type {
int BOOLEAN = 1, INT = 2, LONG = 3, FLOAT = 4, STRING = 5, NULL = 0, INVALID = -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,15 @@ public interface TwitterOAuth {
@Body(BodyType.FORM)
OAuthToken getAccessToken(@Form("x_auth_username") String xauthUsername,
@Form("x_auth_password") String xauthPassword,
@Form("x_auth_mode") XAuthMode xauthMode)throws TwitterException;
@Form("x_auth_mode") @XAuthMode String xauthMode) throws TwitterException;


@POST("/oauth/access_token")
@Body(BodyType.FORM)
OAuthToken getAccessToken(@Extra({"oauth_token", "oauth_token_secret"}) OAuthToken requestToken, @Form("oauth_verifier") String oauthVerifier)throws TwitterException;
OAuthToken getAccessToken(@Extra({"oauth_token", "oauth_token_secret"}) OAuthToken requestToken, @Form("oauth_verifier") String oauthVerifier) throws TwitterException;

enum XAuthMode {
CLIENT("client_auth");
@interface XAuthMode {
String CLIENT = "client_auth";

@Override
public String toString() {
return mode;
}

private final String mode;

XAuthMode(String mode) {
this.mode = mode;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,8 @@
*/
public final class SearchQuery extends SimpleValueMap {

enum Unit {
MILES("mi"), KILOMETERS("km");

private final String value;

Unit(String value) {
this.value = value;
}

public String getValue() {
return value;
}
@interface Unit {
String MILES = "mi", KILOMETERS = "km";
}


Expand Down Expand Up @@ -82,7 +72,8 @@ public SearchQuery(final String query) {
* @return the instance
* @since Twitter4J 2.1.0
*/
public SearchQuery geoCode(final GeoLocation location, final double radius, final Unit unit) {
public SearchQuery geoCode(final GeoLocation location, final double radius,
final @Unit String unit) {
setGeoCode(location, radius, unit);
return this;
}
Expand Down Expand Up @@ -191,8 +182,9 @@ public SearchQuery count(final int rpp) {
* @param radius radius
* @param unit {@link Unit#KILOMETERS} or {@link Unit#MILES}
*/
public void setGeoCode(@NonNull final GeoLocation location, final double radius, @NonNull final Unit unit) {
put("geocode", location.getLatitude() + "," + location.getLongitude() + "," + radius + unit.getValue());
public void setGeoCode(@NonNull final GeoLocation location, final double radius,
@NonNull final @Unit String unit) {
put("geocode", location.getLatitude() + "," + location.getLongitude() + "," + radius + unit);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
package org.mariotaku.twidere.api.twitter.model;

import android.support.annotation.NonNull;
import android.support.annotation.StringDef;

import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import com.bluelinelabs.logansquare.typeconverters.StringBasedTypeConverter;

import org.mariotaku.twidere.api.twitter.util.TwitterDateConverter;

Expand All @@ -49,8 +49,9 @@ public class UserList extends TwitterResponseObject implements Comparable<UserLi
@JsonField(name = "member_count")
long memberCount;

@JsonField(name = "mode", typeConverter = Mode.Converter.class)
Mode mode;
@Mode
@JsonField(name = "mode")
String mode;

@JsonField(name = "description")
String description;
Expand Down Expand Up @@ -91,7 +92,8 @@ public long getMemberCount() {
return memberCount;
}

public Mode getMode() {
@Mode
public String getMode() {
return mode;
}

Expand Down Expand Up @@ -142,40 +144,10 @@ public String toString() {
"} " + super.toString();
}

public enum Mode {
PUBLIC("public"), PRIVATE("private");

private final String mode;

Mode(String mode) {
this.mode = mode;
}

public static Mode parse(String str) {
switch (str) {
case "public":
return PUBLIC;
case "private":
return PRIVATE;
}
throw new UnsupportedOperationException();
}

public String getMode() {
return mode;
}

public static class Converter extends StringBasedTypeConverter<Mode> {

@Override
public Mode getFromString(String string) {
return Mode.parse(string);
}

@Override
public String convertToString(Mode object) {
return object.mode;
}
}
@StringDef({Mode.PRIVATE, Mode.PUBLIC})
public @interface Mode {
String PUBLIC = "public";
String PRIVATE = "private";

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void setName(String name) {
put("name", name);
}

public void setMode(UserList.Mode mode) {
put("mode", mode.getMode());
public void setMode(@UserList.Mode String mode) {
put("mode", mode);
}
}
29 changes: 28 additions & 1 deletion twidere/src/main/java/edu/tsinghua/hotmobi/HotMobiLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.mariotaku.twidere.BuildConfig;
import org.mariotaku.twidere.Constants;
import org.mariotaku.twidere.app.TwidereApplication;
import org.mariotaku.twidere.model.ParcelableMedia;
import org.mariotaku.twidere.model.ParcelableStatus;
import org.mariotaku.twidere.util.JsonSerializer;
import org.mariotaku.twidere.util.Utils;
import org.mariotaku.twidere.util.dagger.DependencyHolder;
Expand Down Expand Up @@ -60,6 +62,7 @@
import edu.tsinghua.hotmobi.model.ScrollRecord;
import edu.tsinghua.hotmobi.model.SessionEvent;
import edu.tsinghua.hotmobi.model.TweetEvent;
import edu.tsinghua.hotmobi.model.TweetType;
import edu.tsinghua.hotmobi.model.UploadLogEvent;

/**
Expand Down Expand Up @@ -204,6 +207,30 @@ public static boolean log(final String msg) {
return false;
}

public static
@TweetType
String getTweetType(ParcelableStatus status) {
if (status.media != null) {
boolean hasImage = false;
for (ParcelableMedia media : status.media) {
switch (media.type) {
case ParcelableMedia.Type.TYPE_ANIMATED_GIF:
case ParcelableMedia.Type.TYPE_CARD_ANIMATED_GIF:
case ParcelableMedia.Type.TYPE_VIDEO:
return TweetType.VIDEO;
case ParcelableMedia.Type.TYPE_IMAGE: {
hasImage = true;
break;
}
}
}
if (hasImage) {
return TweetType.PHOTO;
}
}
return TweetType.TEXT;
}

public <T> void log(long accountId, final T event, final PreProcessing<T> preProcessing) {
mExecutor.execute(new WriteLogTask<>(mApplication, accountId, event, preProcessing));
}
Expand All @@ -228,7 +255,7 @@ public <T> void logList(List<T> events, long accountId, String type, final PrePr
mExecutor.execute(new WriteLogTask<>(mApplication, accountId, type, events, preProcessing));
}

public static void logScreenEvent(Context context, ScreenEvent.Action action, long presentDuration) {
public static void logScreenEvent(Context context, @ScreenEvent.Action String action, long presentDuration) {
getInstance(context).log(ScreenEvent.create(context, action, presentDuration), null);
}

Expand Down
21 changes: 13 additions & 8 deletions twidere/src/main/java/edu/tsinghua/hotmobi/model/MediaEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.mariotaku.twidere.model.ParcelableMedia;
import org.mariotaku.twidere.model.ParcelableStatus;

import edu.tsinghua.hotmobi.HotMobiLogger;

/**
* Created by mariotaku on 15/8/7.
*/
Expand All @@ -43,11 +45,13 @@ public class MediaEvent extends BaseEvent implements Parcelable {
@JsonField(name = "user_id")
long userId;

@JsonField(name = "tweet_type", typeConverter = TweetType.Converter.class)
TweetType tweetType;
@JsonField(name = "tweet_type")
@TweetType
String tweetType;

@JsonField(name = "timeline_type", typeConverter = TimelineType.Converter.class)
TimelineType timelineType;
@JsonField(name = "timeline_type")
@TimelineType
String timelineType;

@JsonField(name = "preview_url")
String previewUrl;
Expand All @@ -58,7 +62,8 @@ public class MediaEvent extends BaseEvent implements Parcelable {
@JsonField(name = "preview_enabled")
boolean previewEnabled;

public static MediaEvent create(Context context, ParcelableStatus status, ParcelableMedia media, TimelineType timelineType, boolean previewEnabled) {
public static MediaEvent create(Context context, ParcelableStatus status, ParcelableMedia media,
@TimelineType String timelineType, boolean previewEnabled) {
final MediaEvent event = new MediaEvent();
event.markStart(context);
event.setId(status.id);
Expand All @@ -67,7 +72,7 @@ public static MediaEvent create(Context context, ParcelableStatus status, Parcel
event.setPreviewUrl(media.preview_url);
event.setPreviewEnabled(previewEnabled);
event.setTimelineType(timelineType);
event.setTweetType(TweetType.getTweetType(status));
event.setTweetType(HotMobiLogger.getTweetType(status));
return event;
}

Expand All @@ -91,11 +96,11 @@ public void setUserId(long userId) {
this.userId = userId;
}

public void setTweetType(TweetType tweetType) {
public void setTweetType(@TweetType String tweetType) {
this.tweetType = tweetType;
}

public void setTimelineType(TimelineType timelineType) {
public void setTimelineType(@TimelineType String timelineType) {
this.timelineType = timelineType;
}

Expand Down
Loading

0 comments on commit 7d3b599

Please sign in to comment.