From 0b49d7ae0dedd283dafa83427159827658f85726 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Wed, 25 Sep 2019 14:04:25 +1000 Subject: [PATCH] Fix crashing during start. --- .../thoughtcrime/securesms/ApplicationContext.java | 13 ++++++++----- .../thoughtcrime/securesms/jobs/PushDecryptJob.java | 7 +++++-- .../securesms/loki/AccountDetailsActivity.kt | 1 + 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ApplicationContext.java b/src/org/thoughtcrime/securesms/ApplicationContext.java index 007058f5fd..a12bfae920 100644 --- a/src/org/thoughtcrime/securesms/ApplicationContext.java +++ b/src/org/thoughtcrime/securesms/ApplicationContext.java @@ -81,6 +81,7 @@ import org.webrtc.PeerConnectionFactory; import org.webrtc.PeerConnectionFactory.InitializationOptions; import org.webrtc.voiceengine.WebRtcAudioManager; import org.webrtc.voiceengine.WebRtcAudioUtils; +import org.whispersystems.libsignal.IdentityKeyPair; import org.whispersystems.libsignal.logging.SignalProtocolLoggerProvider; import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; import org.whispersystems.signalservice.internal.push.SignalServiceProtos; @@ -160,7 +161,6 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc ProcessLifecycleOwner.get().getLifecycle().addObserver(this); // Loki - Set up P2P API if needed setUpP2PAPI(); - setUpStorageAPI(); // Loki - Set up beta analytics if (!BuildConfig.DEBUG) { Fabric.with(this, new Crashlytics()); @@ -183,6 +183,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc KeyCachingService.onAppForegrounded(this); // Loki - Start long polling if needed startLongPollingIfNeeded(); + setUpStorageAPIIfNeeded(); } @Override @@ -411,11 +412,13 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc } // region Loki - public void setUpStorageAPI() { + public void setUpStorageAPIIfNeeded() { String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this); - byte[] userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(this).getPrivateKey().serialize(); - LokiAPIDatabaseProtocol database = DatabaseFactory.getLokiAPIDatabase(this); - LokiStorageAPI.Companion.configure(userHexEncodedPublicKey, userPrivateKey, database); + if (userHexEncodedPublicKey != null && IdentityKeyUtil.hasIdentityKey(this)) { + byte[] userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(this).getPrivateKey().serialize(); + LokiAPIDatabaseProtocol database = DatabaseFactory.getLokiAPIDatabase(this); + LokiStorageAPI.Companion.configure(userHexEncodedPublicKey, userPrivateKey, database); + } } public void setUpP2PAPI() { diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index bd04a8b370..c29009195a 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -1078,7 +1078,8 @@ public class PushDecryptJob extends BaseJob implements InjectableType { TextSecurePreferences.setIsSecondaryDevice(context, true); // Propagate the updates to the file server - LokiStorageAPI.shared.updateOurDeviceMappings(); + LokiStorageAPI storageAPI = LokiStorageAPI.Companion.getShared(); + if (storageAPI != null) { storageAPI.updateOurDeviceMappings(); } // TODO: Trigger an event here? @@ -1104,10 +1105,12 @@ public class PushDecryptJob extends BaseJob implements InjectableType { private void acceptFriendRequestIfNeeded(@NonNull SignalServiceEnvelope envelope, @NonNull SignalServiceContent content) { LokiThreadDatabase lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context); if (envelope.isFriendRequest()) { + LokiStorageAPI storageAPI = LokiStorageAPI.Companion.getShared(); + if (storageAPI == null) { return; } // If we get a friend request then we need to check if the sender is a secondary device. // If it is then we need to check if we have its primary device as our friend // If so then we add them automatically as a friend - LokiStorageAPI.shared.getPrimaryDevice(content.getSender()).success(primaryDevicePubKey -> { + storageAPI.getPrimaryDevice(content.getSender()).success(primaryDevicePubKey -> { // Make sure we have a primary device if (primaryDevicePubKey == null) { return Unit.INSTANCE; } diff --git a/src/org/thoughtcrime/securesms/loki/AccountDetailsActivity.kt b/src/org/thoughtcrime/securesms/loki/AccountDetailsActivity.kt index 0d8e581611..c1a294ce22 100644 --- a/src/org/thoughtcrime/securesms/loki/AccountDetailsActivity.kt +++ b/src/org/thoughtcrime/securesms/loki/AccountDetailsActivity.kt @@ -36,6 +36,7 @@ class AccountDetailsActivity : BaseActionBarActivity() { val application = ApplicationContext.getInstance(this) application.setUpP2PAPI() application.startLongPollingIfNeeded() + application.setUpStorageAPIIfNeeded() startActivity(Intent(this, ConversationListActivity::class.java)) finish() }