Make ConnectionFactory injectable
This commit is contained in:
parent
47a298af2f
commit
c3b3384df6
|
@ -1,6 +1,7 @@
|
|||
package org.mercury_im.messenger.android.di.component;
|
||||
|
||||
import org.mercury_im.messenger.android.MercuryImApplication;
|
||||
import org.mercury_im.messenger.core.di.module.XmppTcpConnectionFactoryModule;
|
||||
import org.mercury_im.messenger.data.di.RepositoryModule;
|
||||
import org.mercury_im.messenger.android.di.module.AndroidPersistenceModule;
|
||||
import org.mercury_im.messenger.android.di.module.AppModule;
|
||||
|
@ -35,7 +36,8 @@ import dagger.Component;
|
|||
AppModule.class,
|
||||
AndroidPersistenceModule.class,
|
||||
RepositoryModule.class,
|
||||
ViewModelModule.class
|
||||
ViewModelModule.class,
|
||||
XmppTcpConnectionFactoryModule.class
|
||||
})
|
||||
public interface AppComponent {
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package org.mercury_im.messenger.core.di.module;
|
||||
|
||||
import org.mercury_im.messenger.core.xmpp.XmppConnectionFactory;
|
||||
import org.mercury_im.messenger.core.xmpp.XmppTcpConnectionFactory;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
@Module
|
||||
public class XmppTcpConnectionFactoryModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
static XmppConnectionFactory provideConnectionFactory() {
|
||||
return new XmppTcpConnectionFactory();
|
||||
}
|
||||
}
|
|
@ -37,7 +37,7 @@ import io.reactivex.subjects.BehaviorSubject;
|
|||
public class MercuryConnectionManager {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger("ConnectionManager");
|
||||
private static final XmppConnectionFactory connectionFactory = new XmppConnectionFactory();
|
||||
private final XmppConnectionFactory connectionFactory;
|
||||
|
||||
private final AccountRepository accountRepository;
|
||||
private final RosterStoreBinder rosterStoreBinder;
|
||||
|
@ -60,13 +60,15 @@ public class MercuryConnectionManager {
|
|||
@Inject
|
||||
public MercuryConnectionManager(Repositories repositories,
|
||||
RosterStoreBinder rosterStoreBinder,
|
||||
MercuryEntityCapsStore entityCapsStore) {
|
||||
MercuryEntityCapsStore entityCapsStore,
|
||||
XmppConnectionFactory connectionFactory) {
|
||||
this.accountRepository = repositories.getAccountRepository();
|
||||
this.rosterStoreBinder = rosterStoreBinder;
|
||||
this.entityCapsStore = entityCapsStore;
|
||||
this.peerRepository = repositories.getPeerRepository();
|
||||
this.directChatRepository = repositories.getDirectChatRepository();
|
||||
this.messageRepository = repositories.getMessageRepository();
|
||||
this.connectionFactory = connectionFactory;
|
||||
|
||||
EntityCapsManager.setPersistentCache(entityCapsStore);
|
||||
start();
|
||||
|
|
|
@ -1,31 +1,10 @@
|
|||
package org.mercury_im.messenger.core.xmpp;
|
||||
|
||||
import org.jivesoftware.smack.AbstractXMPPConnection;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
||||
import org.jivesoftware.smack.util.TLSUtils;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
import org.mercury_im.messenger.entity.Account;
|
||||
|
||||
public class XmppConnectionFactory {
|
||||
public interface XmppConnectionFactory {
|
||||
|
||||
private static final int CONNECTION_TIMEOUT = 30 * 1000;
|
||||
AbstractXMPPConnection createConnection(Account account);
|
||||
|
||||
public AbstractXMPPConnection createConnection(Account account) {
|
||||
try {
|
||||
XMPPTCPConnectionConfiguration.Builder configBuilder =
|
||||
XMPPTCPConnectionConfiguration.builder()
|
||||
.setConnectTimeout(CONNECTION_TIMEOUT)
|
||||
.setXmppAddressAndPassword(account.getAddress(), account.getPassword());
|
||||
if (account.getHost() != null) {
|
||||
configBuilder.setHost(account.getHost());
|
||||
}
|
||||
if (account.getPort() != 0) {
|
||||
configBuilder.setPort(account.getPort());
|
||||
}
|
||||
return new XMPPTCPConnection(configBuilder.build());
|
||||
} catch (XmppStringprepException e) {
|
||||
throw new AssertionError("Account has invalid address: " + account.getAddress(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package org.mercury_im.messenger.core.xmpp;
|
||||
|
||||
import org.jivesoftware.smack.AbstractXMPPConnection;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
||||
import org.jivesoftware.smack.util.TLSUtils;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
import org.mercury_im.messenger.entity.Account;
|
||||
|
||||
public class XmppTcpConnectionFactory implements XmppConnectionFactory {
|
||||
|
||||
private static final int CONNECTION_TIMEOUT = 30 * 1000;
|
||||
|
||||
public AbstractXMPPConnection createConnection(Account account) {
|
||||
try {
|
||||
XMPPTCPConnectionConfiguration.Builder configBuilder =
|
||||
XMPPTCPConnectionConfiguration.builder()
|
||||
.setConnectTimeout(CONNECTION_TIMEOUT)
|
||||
.setXmppAddressAndPassword(account.getAddress(), account.getPassword());
|
||||
if (account.getHost() != null) {
|
||||
configBuilder.setHost(account.getHost());
|
||||
}
|
||||
if (account.getPort() != 0) {
|
||||
configBuilder.setPort(account.getPort());
|
||||
}
|
||||
return new XMPPTCPConnection(configBuilder.build());
|
||||
} catch (XmppStringprepException e) {
|
||||
throw new AssertionError("Account has invalid address: " + account.getAddress(), e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue