diff --git a/app/src/main/java/app/fedilab/android/helper/CrossActionHelper.java b/app/src/main/java/app/fedilab/android/helper/CrossActionHelper.java index e6559990..a49615a9 100644 --- a/app/src/main/java/app/fedilab/android/helper/CrossActionHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/CrossActionHelper.java @@ -27,9 +27,6 @@ import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelStoreOwner; import androidx.preference.PreferenceManager; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -255,10 +252,9 @@ public class CrossActionHelper { .connectTimeout(60, TimeUnit.SECONDS) .proxy(Helper.getProxy(context)) .build(); - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance + "/api/v2/") - .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(okHttpClient) .build(); return retrofit.create(MastodonSearchService.class); diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index 01049b3b..44ba2ac9 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -90,6 +90,12 @@ import com.bumptech.glide.Glide; import com.bumptech.glide.load.resource.bitmap.CenterCrop; import com.bumptech.glide.load.resource.bitmap.RoundedCorners; import com.bumptech.glide.request.RequestOptions; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; +import com.google.gson.stream.JsonWriter; import com.jaredrummler.cyanea.Cyanea; import org.conscrypt.Conscrypt; @@ -107,6 +113,7 @@ import java.net.PasswordAuthentication; import java.net.Proxy; import java.security.Security; import java.text.DateFormat; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Date; @@ -116,6 +123,7 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; import java.util.Random; +import java.util.TimeZone; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -1583,4 +1591,36 @@ public class Helper { void onAttachmentCopied(Attachment attachment); } + + public static Gson getDateBuilder() { + SimpleDateFormat[] formats = new SimpleDateFormat[]{ + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault()), + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault()), + }; + return new GsonBuilder() + .registerTypeAdapter(Date.class, new TypeAdapter() { + + @Override + public void write(JsonWriter out, Date value) { + } + + @Override + public Date read(JsonReader reader) throws IOException { + if (reader.peek() == JsonToken.NULL) { + reader.nextNull(); + return null; + } + String dateAsString = reader.nextString(); + for (SimpleDateFormat format : formats) { + try { + format.setTimeZone(TimeZone.getTimeZone("UTC")); + return format.parse(dateAsString); + } catch (ParseException ignored) { + } + } + return null; + } + }) + .create(); + } } diff --git a/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java b/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java index 29d5142c..09d2834b 100644 --- a/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java @@ -145,7 +145,7 @@ public class NotificationsHelper { Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance + "/api/v1/") - .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(okHttpClient) .build(); return retrofit.create(MastodonNotificationsService.class); diff --git a/app/src/main/java/app/fedilab/android/helper/PushNotifications.java b/app/src/main/java/app/fedilab/android/helper/PushNotifications.java index 79becf4d..efa9be2e 100644 --- a/app/src/main/java/app/fedilab/android/helper/PushNotifications.java +++ b/app/src/main/java/app/fedilab/android/helper/PushNotifications.java @@ -26,9 +26,6 @@ import android.os.Looper; import androidx.annotation.NonNull; import androidx.preference.PreferenceManager; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - import java.io.IOException; import java.util.Random; import java.util.concurrent.TimeUnit; @@ -137,10 +134,9 @@ public class PushNotifications { .connectTimeout(60, TimeUnit.SECONDS) .proxy(Helper.getProxy(context.getApplicationContext())) .build(); - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance + "/api/v1/") - .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(okHttpClient) .build(); return retrofit.create(MastodonNotificationsService.class); diff --git a/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java b/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java index 222c94ab..89ef579b 100644 --- a/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java @@ -1067,6 +1067,9 @@ public class SpannableHelper { if (status.translationContent != null) { status.span_translate = SpannableHelper.convert(context, status, status.translationContent); } + if (status.account == null) { + return status; + } status.account.span_display_name = SpannableHelper.convertA(context, status.account, status.account.display_name, true); if (status.poll != null) { for (Poll.PollItem pollItem : status.poll.options) { diff --git a/app/src/main/java/app/fedilab/android/services/PostMessageService.java b/app/src/main/java/app/fedilab/android/services/PostMessageService.java index c6896555..736e94db 100644 --- a/app/src/main/java/app/fedilab/android/services/PostMessageService.java +++ b/app/src/main/java/app/fedilab/android/services/PostMessageService.java @@ -31,9 +31,6 @@ import androidx.core.app.NotificationCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.preference.PreferenceManager; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -90,10 +87,9 @@ public class PostMessageService extends IntentService { } private static MastodonStatusesService init(Context context, @NonNull String instance) { - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance + "/api/v1/") - .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(getOkHttpClient(context)) .build(); return retrofit.create(MastodonStatusesService.class); diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AccountsVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AccountsVM.java index dca4fc22..5b8cf38e 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AccountsVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AccountsVM.java @@ -95,10 +95,10 @@ public class AccountsVM extends AndroidViewModel { } private MastodonAccountsService init(String instance) { - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); + Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance + "/api/v1/") - .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(okHttpClient) .build(); return retrofit.create(MastodonAccountsService.class); diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AdminVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AdminVM.java index 7723aa72..2375ee81 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AdminVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AdminVM.java @@ -60,10 +60,10 @@ public class AdminVM extends AndroidViewModel { } private MastodonAdminService init(@NonNull String instance) { - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); + Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance + "/api/v1/") - .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(okHttpClient) .build(); return retrofit.create(MastodonAdminService.class); diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AnnouncementsVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AnnouncementsVM.java index a1b3d537..afca9d26 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AnnouncementsVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AnnouncementsVM.java @@ -55,10 +55,10 @@ public class AnnouncementsVM extends AndroidViewModel { } private MastodonAnnouncementsService init(@NonNull String instance) { - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); + Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance + "/api/v1/") - .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(okHttpClient) .build(); return retrofit.create(MastodonAnnouncementsService.class); diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AppsVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AppsVM.java index d3ae5e86..26445ae7 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AppsVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AppsVM.java @@ -58,10 +58,10 @@ public class AppsVM extends AndroidViewModel { } private MastodonAppsService init(String instance) { - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); + Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance + "/api/v1/") - .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(okHttpClient) .build(); return retrofit.create(MastodonAppsService.class); diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/InstancesVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/InstancesVM.java index 288d3cdc..5857ce84 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/InstancesVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/InstancesVM.java @@ -61,10 +61,10 @@ public class InstancesVM extends AndroidViewModel { } private MastodonInstanceService init(@NonNull String instance) { - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); + Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance + "/api/v1/") - .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(okHttpClient) .build(); return retrofit.create(MastodonInstanceService.class); diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/NotificationsVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/NotificationsVM.java index 24cd28de..eccded41 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/NotificationsVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/NotificationsVM.java @@ -63,10 +63,10 @@ public class NotificationsVM extends AndroidViewModel { } private MastodonNotificationsService init(@NonNull String instance) { - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); + Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance + "/api/v1/") - .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(okHttpClient) .build(); return retrofit.create(MastodonNotificationsService.class); diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/OauthVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/OauthVM.java index 727f328c..37cba72d 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/OauthVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/OauthVM.java @@ -59,10 +59,10 @@ public class OauthVM extends AndroidViewModel { } private MastodonAppsService init(@NonNull String instance) { - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); + Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance + "/") - .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(okHttpClient) .build(); return retrofit.create(MastodonAppsService.class); diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/SearchVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/SearchVM.java index ab1a1077..b4d5f0e0 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/SearchVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/SearchVM.java @@ -58,10 +58,10 @@ public class SearchVM extends AndroidViewModel { } private MastodonSearchService init(@NonNull String instance) { - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); + Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance + "/api/v2/") - .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(okHttpClient) .build(); return retrofit.create(MastodonSearchService.class); diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/StatusesVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/StatusesVM.java index 0133b812..39118e99 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/StatusesVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/StatusesVM.java @@ -25,9 +25,6 @@ import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - import java.util.Date; import java.util.List; import java.util.concurrent.TimeUnit; @@ -90,10 +87,9 @@ public class StatusesVM extends AndroidViewModel { } private MastodonStatusesService init(@NonNull String instance) { - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance + "/api/v1/") - .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(getOkHttpClient()) .build(); return retrofit.create(MastodonStatusesService.class); diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java index d6c9dc38..4ed5ad2c 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java @@ -24,9 +24,6 @@ import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -84,10 +81,9 @@ public class TimelinesVM extends AndroidViewModel { } private MastodonTimelinesService initInstanceOnly(String instance) { - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance) - // .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(okHttpClient) .build(); return retrofit.create(MastodonTimelinesService.class); @@ -103,10 +99,9 @@ public class TimelinesVM extends AndroidViewModel { } private MastodonTimelinesService init(String instance) { - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance + "/api/v1/") - .addConverterFactory(GsonConverterFactory.create(gson)) + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) .client(okHttpClient) .build(); return retrofit.create(MastodonTimelinesService.class);