From 3861a7b6e481985b1bb37625dc8dd900fe6b3f3a Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 1 Jun 2023 16:31:54 +0200 Subject: [PATCH] Crash due to delayed service call (Peertube) --- .../timeline/FragmentMastodonTimeline.java | 2 +- .../services/RetrieveInfoService.java | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonTimeline.java b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonTimeline.java index cb069d66b..07e87bfda 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonTimeline.java +++ b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonTimeline.java @@ -917,7 +917,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. timelinesVM.getTimeline(timelineStatuses, timelineParams) .observe(getViewLifecycleOwner(), statuses -> { initialStatuses = statuses; - if (!retry_for_home_done && timelineType == Timeline.TimeLineEnum.HOME && (statuses == null || statuses.statuses.size() == 0) && timelineParams.maxId != null) { + if (!retry_for_home_done && timelineType == Timeline.TimeLineEnum.HOME && (statuses == null || statuses.statuses == null || statuses.statuses.size() == 0) && timelineParams.maxId != null) { retry_for_home_done = true; timelineParams.maxId = null; max_id = null; diff --git a/app/src/main/java/app/fedilab/android/peertube/services/RetrieveInfoService.java b/app/src/main/java/app/fedilab/android/peertube/services/RetrieveInfoService.java index 072d606af..8e67a10b3 100644 --- a/app/src/main/java/app/fedilab/android/peertube/services/RetrieveInfoService.java +++ b/app/src/main/java/app/fedilab/android/peertube/services/RetrieveInfoService.java @@ -24,7 +24,9 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Build; +import android.os.Handler; import android.os.IBinder; +import android.os.Looper; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; @@ -50,12 +52,6 @@ public class RetrieveInfoService extends Service implements NetworkStateReceiver networkStateReceiver = new NetworkStateReceiver(); networkStateReceiver.addListener(this); registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION)); - - - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, getString(R.string.notification_channel_name), @@ -83,8 +79,13 @@ public class RetrieveInfoService extends Service implements NetworkStateReceiver startForeground(1, notification); } - Thread thread = new Thread() { + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + + Thread thread = new Thread() { @Override public void run() { EmojiHelper.fillMapEmoji(getApplicationContext()); @@ -96,7 +97,9 @@ public class RetrieveInfoService extends Service implements NetworkStateReceiver peertubeInformation.setPlaylistPrivacies(new LinkedHashMap<>()); peertubeInformation.setTranslations(new LinkedHashMap<>()); peertubeInformation = new RetrofitPeertubeAPI(RetrieveInfoService.this).getPeertubeInformation(); - stopForeground(true); + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> stopForeground(true); + mainHandler.post(myRunnable); } }; thread.start();