From bd343eafa0d2497ddbed59fc1d5afd6697d1394e Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 22 Jan 2023 12:27:46 +0100 Subject: [PATCH] add async variant for createAccount --- .../android/database/dao/AccountDao.java | 3 +-- .../android/repository/AbstractRepository.java | 4 ++++ .../android/repository/AccountRepository.java | 11 ++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/im/conversations/android/database/dao/AccountDao.java b/src/main/java/im/conversations/android/database/dao/AccountDao.java index 4cfc8c8c5..1e6b76daf 100644 --- a/src/main/java/im/conversations/android/database/dao/AccountDao.java +++ b/src/main/java/im/conversations/android/database/dao/AccountDao.java @@ -2,7 +2,6 @@ package im.conversations.android.database.dao; import androidx.room.Dao; import androidx.room.Insert; -import androidx.room.OnConflictStrategy; import androidx.room.Query; import com.google.common.util.concurrent.ListenableFuture; import eu.siacs.conversations.xmpp.Jid; @@ -14,7 +13,7 @@ import java.util.List; @Dao public interface AccountDao { - @Insert(onConflict = OnConflictStrategy.REPLACE) + @Insert long insert(final AccountEntity account); @Query("SELECT id,address,randomSeed FROM account WHERE enabled = 1") diff --git a/src/main/java/im/conversations/android/repository/AbstractRepository.java b/src/main/java/im/conversations/android/repository/AbstractRepository.java index bee959b2b..15eddfada 100644 --- a/src/main/java/im/conversations/android/repository/AbstractRepository.java +++ b/src/main/java/im/conversations/android/repository/AbstractRepository.java @@ -2,9 +2,13 @@ package im.conversations.android.repository; import android.content.Context; import im.conversations.android.database.ConversationsDatabase; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; public abstract class AbstractRepository { + protected static final Executor IO_EXECUTOR = Executors.newSingleThreadExecutor(); + protected final Context context; protected final ConversationsDatabase database; diff --git a/src/main/java/im/conversations/android/repository/AccountRepository.java b/src/main/java/im/conversations/android/repository/AccountRepository.java index aebbcf551..b38e7786a 100644 --- a/src/main/java/im/conversations/android/repository/AccountRepository.java +++ b/src/main/java/im/conversations/android/repository/AccountRepository.java @@ -3,11 +3,14 @@ package im.conversations.android.repository; import android.content.Context; import androidx.annotation.NonNull; import com.google.common.base.Preconditions; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; import eu.siacs.conversations.xmpp.Jid; import im.conversations.android.IDs; import im.conversations.android.database.CredentialStore; import im.conversations.android.database.entity.AccountEntity; import im.conversations.android.database.model.Account; +import im.conversations.android.xmpp.ConnectionPool; public class AccountRepository extends AbstractRepository { @@ -15,7 +18,7 @@ public class AccountRepository extends AbstractRepository { super(context); } - public Account createAccount(@NonNull final Jid address, final String password) { + private Account createAccount(@NonNull final Jid address, final String password) { Preconditions.checkArgument( address.isBareJid(), "Account should be specified without resource"); Preconditions.checkArgument(password != null, "Missing password"); @@ -31,6 +34,12 @@ public class AccountRepository extends AbstractRepository { } catch (final Exception e) { throw new IllegalStateException("Could not store password", e); } + ConnectionPool.getInstance(context).reconfigure(account); return account; } + + public ListenableFuture createAccountAsync( + final @NonNull Jid address, final String password) { + return Futures.submit(() -> createAccount(address, password), IO_EXECUTOR); + } }