From 348d2c8b4f7b3a35de7e6bd53efc47ed106c4848 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 8 Mar 2017 22:34:13 +0100 Subject: [PATCH] Start work on integrating Retrofit - GSON, Authorization --- app/build.gradle | 1 + .../com/keylesspalace/tusky/BaseActivity.java | 49 ++++++++++++++++ ...{MastodonService.java => MastodonAPI.java} | 7 ++- .../com/keylesspalace/tusky/entity/Media.java | 50 +++------------- .../tusky/entity/Relationship.java | 58 +++---------------- .../tusky/entity/StatusContext.java | 21 +------ 6 files changed, 74 insertions(+), 112 deletions(-) rename app/src/main/java/com/keylesspalace/tusky/{MastodonService.java => MastodonAPI.java} (97%) diff --git a/app/build.gradle b/app/build.gradle index 7c6d43e2..762e622d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -36,4 +36,5 @@ dependencies { testCompile 'junit:junit:4.12' compile 'com.mikhaellopez:circularfillableloaders:1.2.0' compile 'com.squareup.retrofit2:retrofit:2.2.0' + compile 'com.squareup.retrofit2:converter-gson:2.1.0' } diff --git a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java index bb3a4fb5..4725c25c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java @@ -15,7 +15,9 @@ package com.keylesspalace.tusky; +import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.graphics.Color; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; @@ -26,11 +28,22 @@ import android.support.v7.app.AppCompatActivity; import android.util.TypedValue; import android.view.Menu; +import java.io.IOException; + +import okhttp3.Interceptor; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; + /* There isn't presently a way to globally change the theme of a whole application at runtime, just * individual activities. So, each activity has to set its theme before any views are created. And * the most expedient way to accomplish this was to put it in a base class and just have every * activity extend from it. */ public class BaseActivity extends AppCompatActivity { + protected MastodonAPI mastodonAPI; + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -59,6 +72,42 @@ public class BaseActivity extends AppCompatActivity { overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right); } + protected String getAccessToken() { + SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE); + return preferences.getString("accessToken", null); + } + + protected String getBaseUrl() { + SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE); + return "https://" + preferences.getString("domain", null); + } + + protected void createMastodonAPI() { + OkHttpClient okHttpClient = new OkHttpClient.Builder() + .addInterceptor(new Interceptor() { + @Override + public Response intercept(Chain chain) throws IOException { + Request originalRequest = chain.request(); + + Request.Builder builder = originalRequest.newBuilder() + .header("Authorization", String.format("Bearer %s", getAccessToken())); + + Request newRequest = builder.build(); + + return chain.proceed(newRequest); + } + }) + .build(); + + Retrofit retrofit = new Retrofit.Builder() + .baseUrl(getBaseUrl()) + .client(okHttpClient) + .addConverterFactory(GsonConverterFactory.create()) + .build(); + + mastodonAPI = retrofit.create(MastodonAPI.class); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { TypedValue value = new TypedValue(); diff --git a/app/src/main/java/com/keylesspalace/tusky/MastodonService.java b/app/src/main/java/com/keylesspalace/tusky/MastodonAPI.java similarity index 97% rename from app/src/main/java/com/keylesspalace/tusky/MastodonService.java rename to app/src/main/java/com/keylesspalace/tusky/MastodonAPI.java index 1a74781f..06c98589 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MastodonService.java +++ b/app/src/main/java/com/keylesspalace/tusky/MastodonAPI.java @@ -7,6 +7,7 @@ import com.keylesspalace.tusky.entity.StatusContext; import java.util.List; import okhttp3.RequestBody; +import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.http.DELETE; import retrofit2.http.Field; @@ -18,7 +19,7 @@ import retrofit2.http.Part; import retrofit2.http.Path; import retrofit2.http.Query; -public interface MastodonService { +public interface MastodonAPI { @GET("api/v1/timelines/home") Call> homeTimeline(@Query("max_id") int maxId, @Query("since_id") int sinceId, @Query("limit") int limit); @GET("api/v1/timelines/public") @@ -29,7 +30,7 @@ public interface MastodonService { @GET("api/v1/notifications") Call> notifications(@Query("max_id") int maxId, @Query("since_id") int sinceId, @Query("limit") int limit); @POST("api/v1/notifications/clear") - Call clearNotifications(); + Call clearNotifications(); @GET("api/v1/notifications/{id}") Call notification(@Path("id") int notificationId); @@ -49,7 +50,7 @@ public interface MastodonService { @GET("api/v1/statuses/{id}/favourited_by") Call> statusFavouritedBy(@Path("id") int statusId, @Query("max_id") int maxId, @Query("since_id") int sinceId, @Query("limit") int limit); @DELETE("api/v1/statuses/{id}") - Call deleteStatus(@Path("id") int statusId); + Call deleteStatus(@Path("id") int statusId); @POST("api/v1/statuses/{id}/reblog") Call reblogStatus(@Path("id") int statusId); @POST("api/v1/statuses/{id}/unreblog") diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/Media.java b/app/src/main/java/com/keylesspalace/tusky/entity/Media.java index 38c40c09..ec6ef31c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/Media.java +++ b/app/src/main/java/com/keylesspalace/tusky/entity/Media.java @@ -1,49 +1,17 @@ package com.keylesspalace.tusky.entity; +import com.google.gson.annotations.SerializedName; + public class Media { - int id; - String type; - String url; - String preview_url; - String text_url; + public int id; - public int getId() { - return id; - } + public String type; - public void setId(int id) { - this.id = id; - } + public String url; - public String getType() { - return type; - } + @SerializedName("preview_url") + public String previewUrl; - public void setType(String type) { - this.type = type; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public String getPreview_url() { - return preview_url; - } - - public void setPreview_url(String preview_url) { - this.preview_url = preview_url; - } - - public String getText_url() { - return text_url; - } - - public void setText_url(String text_url) { - this.text_url = text_url; - } + @SerializedName("text_url") + public String textUrl; } diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/Relationship.java b/app/src/main/java/com/keylesspalace/tusky/entity/Relationship.java index ffffc945..6964b585 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/Relationship.java +++ b/app/src/main/java/com/keylesspalace/tusky/entity/Relationship.java @@ -1,58 +1,18 @@ package com.keylesspalace.tusky.entity; +import com.google.gson.annotations.SerializedName; + public class Relationship { - public boolean isFollowing() { - return following; - } + public int id; - public void setFollowing(boolean following) { - this.following = following; - } + public boolean following; - public boolean isFollowed_by() { - return followed_by; - } + @SerializedName("followed_by") + public boolean followedBy; - public void setFollowed_by(boolean followed_by) { - this.followed_by = followed_by; - } + public boolean blocking; - public boolean isBlocking() { - return blocking; - } + public boolean muting; - public void setBlocking(boolean blocking) { - this.blocking = blocking; - } - - public boolean isMuting() { - return muting; - } - - public void setMuting(boolean muting) { - this.muting = muting; - } - - public boolean isRequested() { - return requested; - } - - public void setRequested(boolean requested) { - this.requested = requested; - } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - int id; - boolean following; - boolean followed_by; - boolean blocking; - boolean muting; - boolean requested; + public boolean requested; } diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/StatusContext.java b/app/src/main/java/com/keylesspalace/tusky/entity/StatusContext.java index 15d2fded..4099df6b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/StatusContext.java +++ b/app/src/main/java/com/keylesspalace/tusky/entity/StatusContext.java @@ -5,23 +5,6 @@ import com.keylesspalace.tusky.Status; import java.util.List; public class StatusContext { - List ancestors; - - public List getAncestors() { - return ancestors; - } - - public void setAncestors(List ancestors) { - this.ancestors = ancestors; - } - - public List getDescendants() { - return descendants; - } - - public void setDescendants(List descendants) { - this.descendants = descendants; - } - - List descendants; + public List ancestors; + public List descendants; }