From 7e485b8095ad04b737900e8c74de3127894f56c2 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Tue, 16 Oct 2018 11:47:58 -0700 Subject: [PATCH] Post startService() in onResume() as a possible fix to an Android P bug. Got confirmation that the crash we're seeing is a bug, and this might be a possible workaround. --- .../PassphraseRequiredActionBarActivity.java | 11 ++++++++--- .../thoughtcrime/securesms/WebRtcCallActivity.java | 8 +++++++- src/org/thoughtcrime/securesms/util/Util.java | 4 ++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/org/thoughtcrime/securesms/PassphraseRequiredActionBarActivity.java b/src/org/thoughtcrime/securesms/PassphraseRequiredActionBarActivity.java index 838536f2c2..0dbf3e7c85 100644 --- a/src/org/thoughtcrime/securesms/PassphraseRequiredActionBarActivity.java +++ b/src/org/thoughtcrime/securesms/PassphraseRequiredActionBarActivity.java @@ -17,6 +17,7 @@ import org.thoughtcrime.securesms.push.SignalServiceNetworkAccess; import org.thoughtcrime.securesms.service.KeyCachingService; import org.thoughtcrime.securesms.service.MessageRetrievalService; import org.thoughtcrime.securesms.util.TextSecurePreferences; +import org.thoughtcrime.securesms.util.Util; import java.util.Locale; @@ -60,10 +61,14 @@ public abstract class PassphraseRequiredActionBarActivity extends BaseActionBarA protected void onResume() { Log.i(TAG, "onResume()"); super.onResume(); - KeyCachingService.registerPassphraseActivityStarted(this); - if (!networkAccess.isCensored(this)) MessageRetrievalService.registerActivityStarted(this); - else ApplicationContext.getInstance(this).getJobManager().add(new PushNotificationReceiveJob(this)); + // Android P has a bug in foreground timings where starting a service in onResume() can still crash + Util.postToMain(() -> { + KeyCachingService.registerPassphraseActivityStarted(this); + + if (!networkAccess.isCensored(this)) MessageRetrievalService.registerActivityStarted(this); + else ApplicationContext.getInstance(this).getJobManager().add(new PushNotificationReceiveJob(this)); + }); isVisible = true; } diff --git a/src/org/thoughtcrime/securesms/WebRtcCallActivity.java b/src/org/thoughtcrime/securesms/WebRtcCallActivity.java index b3ad8a5a80..3112079317 100644 --- a/src/org/thoughtcrime/securesms/WebRtcCallActivity.java +++ b/src/org/thoughtcrime/securesms/WebRtcCallActivity.java @@ -48,6 +48,7 @@ import org.thoughtcrime.securesms.service.MessageRetrievalService; import org.thoughtcrime.securesms.service.WebRtcCallService; import org.thoughtcrime.securesms.util.ServiceUtil; import org.thoughtcrime.securesms.util.TextSecurePreferences; +import org.thoughtcrime.securesms.util.Util; import org.thoughtcrime.securesms.util.ViewUtil; import org.whispersystems.libsignal.IdentityKey; import org.whispersystems.libsignal.SignalProtocolAddress; @@ -88,7 +89,12 @@ public class WebRtcCallActivity extends Activity { public void onResume() { Log.i(TAG, "onResume()"); super.onResume(); - if (!networkAccess.isCensored(this)) MessageRetrievalService.registerActivityStarted(this); + + // Android P has a bug in foreground timings where starting a service in onResume() can still crash + Util.postToMain(() -> { + if (!networkAccess.isCensored(this)) MessageRetrievalService.registerActivityStarted(this); + }); + initializeScreenshotSecurity(); EventBus.getDefault().register(this); } diff --git a/src/org/thoughtcrime/securesms/util/Util.java b/src/org/thoughtcrime/securesms/util/Util.java index 9a93698038..8416e6b2d7 100644 --- a/src/org/thoughtcrime/securesms/util/Util.java +++ b/src/org/thoughtcrime/securesms/util/Util.java @@ -384,6 +384,10 @@ public class Util { } } + public static void postToMain(final @NonNull Runnable runnable) { + handler.post(runnable); + } + public static void runOnMain(final @NonNull Runnable runnable) { if (isMainThread()) runnable.run(); else handler.post(runnable);