session-android/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java

225 lines
11 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.dependencies;
import android.content.Context;
import org.greenrobot.eventbus.EventBus;
import org.thoughtcrime.securesms.BuildConfig;
2017-08-08 16:37:15 -07:00
import org.thoughtcrime.securesms.CreateProfileActivity;
import org.thoughtcrime.securesms.DeviceListFragment;
import org.thoughtcrime.securesms.crypto.storage.SignalProtocolStoreImpl;
2019-07-22 10:41:50 +10:00
import org.thoughtcrime.securesms.crypto.storage.TextSecureSessionStore;
2019-06-24 14:10:09 +10:00
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.events.ReminderUpdateEvent;
2019-06-24 14:10:09 +10:00
import org.thoughtcrime.securesms.gcm.FcmService;
import org.thoughtcrime.securesms.jobs.AttachmentDownloadJob;
2019-06-24 14:10:09 +10:00
import org.thoughtcrime.securesms.jobs.AttachmentUploadJob;
import org.thoughtcrime.securesms.jobs.AvatarDownloadJob;
import org.thoughtcrime.securesms.jobs.CleanPreKeysJob;
import org.thoughtcrime.securesms.jobs.CreateSignedPreKeyJob;
2019-01-24 03:04:28 -08:00
import org.thoughtcrime.securesms.jobs.FcmRefreshJob;
import org.thoughtcrime.securesms.jobs.MultiDeviceBlockedUpdateJob;
2019-06-24 14:10:09 +10:00
import org.thoughtcrime.securesms.jobs.MultiDeviceConfigurationUpdateJob;
import org.thoughtcrime.securesms.jobs.MultiDeviceContactUpdateJob;
import org.thoughtcrime.securesms.jobs.MultiDeviceGroupUpdateJob;
import org.thoughtcrime.securesms.jobs.MultiDeviceProfileKeyUpdateJob;
import org.thoughtcrime.securesms.jobs.MultiDeviceReadUpdateJob;
import org.thoughtcrime.securesms.jobs.MultiDeviceVerifiedUpdateJob;
2019-06-24 14:10:09 +10:00
import org.thoughtcrime.securesms.jobs.PushDecryptJob;
import org.thoughtcrime.securesms.jobs.PushGroupSendJob;
import org.thoughtcrime.securesms.jobs.PushGroupUpdateJob;
import org.thoughtcrime.securesms.jobs.PushMediaSendJob;
import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob;
import org.thoughtcrime.securesms.jobs.PushTextSendJob;
import org.thoughtcrime.securesms.jobs.RefreshAttributesJob;
import org.thoughtcrime.securesms.jobs.RefreshPreKeysJob;
2019-06-24 14:10:09 +10:00
import org.thoughtcrime.securesms.jobs.RefreshUnidentifiedDeliveryAbilityJob;
import org.thoughtcrime.securesms.jobs.RequestGroupInfoJob;
import org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob;
import org.thoughtcrime.securesms.jobs.RetrieveProfileJob;
2018-05-22 02:13:10 -07:00
import org.thoughtcrime.securesms.jobs.RotateCertificateJob;
2019-06-24 14:10:09 +10:00
import org.thoughtcrime.securesms.jobs.RotateProfileKeyJob;
import org.thoughtcrime.securesms.jobs.RotateSignedPreKeyJob;
2018-05-22 02:13:10 -07:00
import org.thoughtcrime.securesms.jobs.SendDeliveryReceiptJob;
2017-09-15 22:38:53 -07:00
import org.thoughtcrime.securesms.jobs.SendReadReceiptJob;
2019-06-24 14:10:09 +10:00
import org.thoughtcrime.securesms.jobs.TypingSendJob;
import org.thoughtcrime.securesms.logging.Log;
2018-03-01 09:51:37 -08:00
import org.thoughtcrime.securesms.preferences.AppProtectionPreferenceFragment;
import org.thoughtcrime.securesms.push.SecurityEventListener;
import org.thoughtcrime.securesms.push.SignalServiceNetworkAccess;
import org.thoughtcrime.securesms.service.IncomingMessageObserver;
import org.thoughtcrime.securesms.service.WebRtcCallService;
2019-06-04 11:45:29 +10:00
import org.thoughtcrime.securesms.util.RealtimeSleepTimer;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
import org.whispersystems.signalservice.api.util.CredentialsProvider;
import org.whispersystems.signalservice.api.util.SleepTimer;
import org.whispersystems.signalservice.api.util.UptimeSleepTimer;
import org.whispersystems.signalservice.api.websocket.ConnectivityListener;
import dagger.Module;
import dagger.Provides;
@Module(complete = false, injects = {CleanPreKeysJob.class,
CreateSignedPreKeyJob.class,
PushGroupSendJob.class,
PushTextSendJob.class,
PushMediaSendJob.class,
AttachmentDownloadJob.class,
RefreshPreKeysJob.class,
IncomingMessageObserver.class,
PushNotificationReceiveJob.class,
MultiDeviceContactUpdateJob.class,
MultiDeviceGroupUpdateJob.class,
MultiDeviceReadUpdateJob.class,
MultiDeviceBlockedUpdateJob.class,
DeviceListFragment.class,
RefreshAttributesJob.class,
2019-01-24 03:04:28 -08:00
FcmRefreshJob.class,
RequestGroupInfoJob.class,
PushGroupUpdateJob.class,
AvatarDownloadJob.class,
RotateSignedPreKeyJob.class,
WebRtcCallService.class,
RetrieveProfileJob.class,
2017-08-08 16:37:15 -07:00
MultiDeviceVerifiedUpdateJob.class,
CreateProfileActivity.class,
RetrieveProfileAvatarJob.class,
2017-09-15 22:38:53 -07:00
MultiDeviceProfileKeyUpdateJob.class,
SendReadReceiptJob.class,
AppProtectionPreferenceFragment.class,
2019-01-24 03:04:28 -08:00
FcmService.class,
2018-05-22 02:13:10 -07:00
RotateCertificateJob.class,
2018-10-11 16:45:22 -07:00
SendDeliveryReceiptJob.class,
RotateProfileKeyJob.class,
MultiDeviceConfigurationUpdateJob.class,
2018-10-29 15:14:31 -07:00
RefreshUnidentifiedDeliveryAbilityJob.class,
2018-12-07 18:31:39 -08:00
TypingSendJob.class,
AttachmentUploadJob.class,
PushDecryptJob.class})
public class SignalCommunicationModule {
private static final String TAG = SignalCommunicationModule.class.getSimpleName();
2017-09-15 22:38:53 -07:00
private final Context context;
private final SignalServiceNetworkAccess networkAccess;
private SignalServiceAccountManager accountManager;
private SignalServiceMessageSender messageSender;
private SignalServiceMessageReceiver messageReceiver;
public SignalCommunicationModule(Context context, SignalServiceNetworkAccess networkAccess) {
this.context = context;
this.networkAccess = networkAccess;
}
2017-09-15 22:38:53 -07:00
@Provides
synchronized SignalServiceAccountManager provideSignalAccountManager() {
if (this.accountManager == null) {
this.accountManager = new SignalServiceAccountManager(networkAccess.getConfiguration(context),
new DynamicCredentialsProvider(context),
BuildConfig.USER_AGENT);
}
return this.accountManager;
}
@Provides
2019-06-28 16:08:27 +10:00
public synchronized SignalServiceMessageSender provideSignalMessageSender() {
2017-09-15 22:38:53 -07:00
if (this.messageSender == null) {
this.messageSender = new SignalServiceMessageSender(networkAccess.getConfiguration(context),
new DynamicCredentialsProvider(context),
new SignalProtocolStoreImpl(context),
BuildConfig.USER_AGENT,
2018-05-22 02:13:10 -07:00
TextSecurePreferences.isMultiDevice(context),
Optional.fromNullable(IncomingMessageObserver.getPipe()),
2018-05-22 02:13:10 -07:00
Optional.fromNullable(IncomingMessageObserver.getUnidentifiedPipe()),
2019-06-11 16:27:00 +10:00
Optional.of(new SecurityEventListener(context)),
TextSecurePreferences.getLocalNumber(context),
2019-06-12 11:41:50 +10:00
DatabaseFactory.getLokiAPIDatabase(context),
2019-07-22 09:38:12 +10:00
DatabaseFactory.getLokiThreadDatabase(context),
2019-06-24 14:10:09 +10:00
DatabaseFactory.getLokiMessageFriendRequestDatabase(context),
2019-07-22 10:41:50 +10:00
DatabaseFactory.getLokiPreKeyBundleDatabase(context),
new TextSecureSessionStore(context));
} else {
2018-05-22 02:13:10 -07:00
this.messageSender.setMessagePipe(IncomingMessageObserver.getPipe(), IncomingMessageObserver.getUnidentifiedPipe());
this.messageSender.setIsMultiDevice(TextSecurePreferences.isMultiDevice(context));
2017-09-15 22:38:53 -07:00
}
2017-09-15 22:38:53 -07:00
return this.messageSender;
}
2017-09-15 22:38:53 -07:00
@Provides
synchronized SignalServiceMessageReceiver provideSignalMessageReceiver() {
if (this.messageReceiver == null) {
2019-01-24 03:04:28 -08:00
SleepTimer sleepTimer = TextSecurePreferences.isFcmDisabled(context) ? new RealtimeSleepTimer(context) : new UptimeSleepTimer();
2017-09-15 22:38:53 -07:00
this.messageReceiver = new SignalServiceMessageReceiver(networkAccess.getConfiguration(context),
new DynamicCredentialsProvider(context),
BuildConfig.USER_AGENT,
new PipeConnectivityListener(),
sleepTimer);
2017-09-15 22:38:53 -07:00
}
return this.messageReceiver;
}
@Provides
synchronized SignalServiceNetworkAccess provideSignalServiceNetworkAccess() {
return networkAccess;
}
private static class DynamicCredentialsProvider implements CredentialsProvider {
private final Context context;
private DynamicCredentialsProvider(Context context) {
this.context = context.getApplicationContext();
}
@Override
public String getUser() {
return TextSecurePreferences.getLocalNumber(context);
}
@Override
public String getPassword() {
return TextSecurePreferences.getPushServerPassword(context);
}
@Override
public String getSignalingKey() {
return TextSecurePreferences.getSignalingKey(context);
}
}
private class PipeConnectivityListener implements ConnectivityListener {
@Override
public void onConnected() {
2018-08-02 09:25:33 -04:00
Log.i(TAG, "onConnected()");
}
@Override
public void onConnecting() {
2018-08-02 09:25:33 -04:00
Log.i(TAG, "onConnecting()");
}
@Override
public void onDisconnected() {
Log.w(TAG, "onDisconnected()");
}
@Override
public void onAuthenticationFailure() {
Log.w(TAG, "onAuthenticationFailure()");
TextSecurePreferences.setUnauthorizedReceived(context, true);
EventBus.getDefault().post(new ReminderUpdateEvent());
}
}
}