adapt AccountsManager to DcAccounts

This commit is contained in:
B. Petersen
2021-07-23 18:18:44 +02:00
parent 634c8d1e49
commit 0f7984442a
@@ -11,12 +11,14 @@ import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import com.b44t.messenger.DcAccounts;
import com.b44t.messenger.DcContext;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.ConversationListActivity;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.WelcomeActivity;
import org.thoughtcrime.securesms.notifications.NotificationCenter;
import org.thoughtcrime.securesms.util.Prefs;
import org.thoughtcrime.securesms.util.task.ProgressDialogAsyncTask;
@@ -29,82 +31,13 @@ public class AccountManager {
private static final String TAG = AccountManager.class.getSimpleName();
private static AccountManager self;
private final static String DEFAULT_DB_NAME = "messenger.db";
public class Account {
private String dbName;
private String displayname;
private String addr;
private boolean configured;
private boolean current;
public String getDescr() {
String ret = "";
if (!displayname.isEmpty() && !addr.isEmpty()) {
ret = String.format("%s (%s)", displayname, addr);
} else if (!addr.isEmpty()) {
ret = addr;
} else {
ret = dbName;
}
if (!configured) {
ret += " (not configured)";
}
return ret;
}
public boolean isCurrent() {
return current;
}
public String getDbName() {
return dbName;
}
};
private @Nullable Account maybeGetAccount(File file) {
try {
if (!file.isDirectory() && file.getName().startsWith("messenger") && file.getName().endsWith(".db")) {
DcContext testContext = new DcContext(null, file.getAbsolutePath());
if (testContext.isOk()) {
Account ret = new Account();
ret.dbName = file.getName();
ret.displayname = testContext.getConfig("displayname");
ret.addr = testContext.getConfig("addr");
ret.configured = testContext.isConfigured() != 0;
return ret;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private File getUniqueDbName(Context context) {
File dir = context.getFilesDir();
int index = 1;
while (true) {
File test = new File(dir, String.format("messenger-%d.db", index));
File testBlobdir = new File(dir, String.format("messenger-%d.db-blobs", index));
if (!test.exists() && !testBlobdir.exists()) {
return test;
}
index++;
}
}
private void resetDcContext(Context context) {
// create an empty DcContext object - this will be set up then, starting with
// getSelectedAccount()
/* TODO
ApplicationContext appContext = (ApplicationContext)context.getApplicationContext();
appContext.dcContext.notificationCenter.removeAllNotifiations();
appContext.dcContext.stopIo();
appContext.dcContext.unref();
appContext.dcContext = new ApplicationDcContext(context);
DcHelper.getNotificationCenter(context).removeAllNotifiations();
appContext.dcContext = appContext.dcAccounts.getSelectedAccount();
appContext.notificationCenter = new NotificationCenter(context);
appContext.eventCenter = new DcEventCenter(context);
DcHelper.setStockTranslations(context);
*/
}
@@ -117,30 +50,6 @@ public class AccountManager {
return self;
}
public ArrayList<Account> getAccounts(Context context) {
ArrayList<Account> result = new ArrayList<>();
String dbName = PreferenceManager.getDefaultSharedPreferences(context)
.getString("curr_account_db_name", DEFAULT_DB_NAME);
try {
File dir = context.getFilesDir();
File[] files = dir.listFiles();
for (File file : files) {
Account account = maybeGetAccount(file);
if (account!=null) {
if (account.dbName.equals(dbName)) {
account.current = true;
}
result.add(account);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public void migrateToDcAccounts(ApplicationContext context) {
try {
File[] files = context.getFilesDir().listFiles();
@@ -150,7 +59,7 @@ public class AccountManager {
int accountId = context.dcAccounts.migrateAccount(file.getAbsolutePath());
if (accountId != 0) {
String selName = PreferenceManager.getDefaultSharedPreferences(context)
.getString("curr_account_db_name", DEFAULT_DB_NAME);
.getString("curr_account_db_name", "messenger.db");
if (file.getName().equals(selName)) {
context.dcAccounts.selectAccount(accountId);
}
@@ -162,80 +71,32 @@ public class AccountManager {
}
}
public void switchAccount(Context context, Account account) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String prevDbName = sharedPreferences.getString("curr_account_db_name", DEFAULT_DB_NAME);
// we remember prev_account_db_name in case the account is not configured
// and the user want to rollback to the working account
sharedPreferences.edit().putString("prev_account_db_name", prevDbName).apply();
sharedPreferences.edit().putString("curr_account_db_name", account.dbName).apply();
public void switchAccount(Context context, int accountId) {
DcHelper.getAccounts(context).selectAccount(accountId);
resetDcContext(context);
}
public File getSelectedAccount(Context context) {
String dbName = PreferenceManager.getDefaultSharedPreferences(context)
.getString("curr_account_db_name", DEFAULT_DB_NAME);
return new File(context.getFilesDir(), dbName);
}
// add accounts
private void beginAccountCreation(Context context) {
// pause the current account and let the user create a new one.
// this function is not needed on the very first account creation.
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String prevDbName = sharedPreferences.getString("curr_account_db_name", DEFAULT_DB_NAME);
String inCreationDbName = getUniqueDbName(context).getName();
sharedPreferences.edit().putString("prev_account_db_name", prevDbName).apply();
sharedPreferences.edit().putString("curr_account_db_name", inCreationDbName).apply();
DcHelper.getAccounts(context).addAccount();
resetDcContext(context);
}
public boolean canRollbackAccountCreation(Context context) {
String prevDbName = PreferenceManager.getDefaultSharedPreferences(context)
.getString("prev_account_db_name", "");
return !prevDbName.isEmpty();
return DcHelper.getAccounts(context).getAll().length > 1;
}
public void rollbackAccountCreation(Activity activity) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(activity);
String prevDbName = sharedPreferences.getString("prev_account_db_name", "");
String inCreationDbName = sharedPreferences.getString("curr_account_db_name", "");
DcAccounts accounts = DcHelper.getAccounts(activity);
Account prevAccount = maybeGetAccount(new File(activity.getFilesDir(), prevDbName));
if (prevAccount!=null) {
new SwitchAccountAsyncTask(activity, R.string.switching_account, prevAccount, inCreationDbName, null).execute();
}
}
// delete account
private void deleteAccount(Context context, String dbName) {
try {
File blobdir = new File(context.getFilesDir(), dbName+"-blobs");
String [] blobfiles = blobdir.list();
for (String blobfile: blobfiles) {
new File(blobdir, blobfile).delete();
}
blobdir.delete();
File dbFile = new File(context.getFilesDir(), dbName);
dbFile.delete();
} catch(Exception e) {
e.printStackTrace();
DcContext selectedAccount = accounts.getSelectedAccount();
if (selectedAccount.isConfigured() == 0) {
accounts.removeAccount(selectedAccount.getAccountId());
}
File test = new File(context.getFilesDir(), dbName);
if (test.exists()) {
String err = String.format("Cannot delete account %s", test.getAbsolutePath());
Log.w(TAG, err);
Toast.makeText(context, err, Toast.LENGTH_LONG).show();
}
new SwitchAccountAsyncTask(activity, R.string.switching_account, accounts.getSelectedAccount().getAccountId(), null).execute();
}
@@ -243,39 +104,23 @@ public class AccountManager {
private static class SwitchAccountAsyncTask extends ProgressDialogAsyncTask<Void, Void, Void> {
private final WeakReference<Activity> activityWeakReference;
private final @Nullable Account destAccount; // null creates a new account
private final @Nullable String deleteDbName;
private final @Nullable int destAccountId; // 0 creates a new account
private final @Nullable String qrAccount;
public SwitchAccountAsyncTask(Activity activity, int title, @Nullable Account destAccount, @Nullable String deleteDbName, @Nullable String qrAccount) {
public SwitchAccountAsyncTask(Activity activity, int title, int destAccountId, @Nullable String qrAccount) {
super(activity, null, activity.getString(title));
this.activityWeakReference = new WeakReference<>(activity);
this.destAccount = destAccount;
this.deleteDbName = deleteDbName;
this.destAccountId = destAccountId;
this.qrAccount = qrAccount;
}
@Override
protected Void doInBackground(Void... voids) {
Activity activity = activityWeakReference.get();
if (activity!=null) {
if (destAccount==null) {
if (destAccountId==0) {
AccountManager.getInstance().beginAccountCreation(activity);
} else {
AccountManager.getInstance().switchAccount(activity, destAccount);
if (deleteDbName!=null) {
// used to delete the previous account, however, as a resilience check, make sure,
// we do not delete already configured accounts (just in case sth. changes the flow of activities)
DcContext testContext = new DcContext(null, new File(activity.getFilesDir(), deleteDbName).getAbsolutePath());
if (testContext.isOk()) {
if (testContext.isConfigured() == 0) {
testContext.unref();
AccountManager.getInstance().deleteAccount(activity, deleteDbName);
PreferenceManager.getDefaultSharedPreferences(activity)
.edit().putString("prev_account_db_name", "").apply();
}
}
}
AccountManager.getInstance().switchAccount(activity, destAccountId);
}
}
return null;
@@ -286,7 +131,7 @@ public class AccountManager {
Activity activity = activityWeakReference.get();
if (activity!=null) {
activity.finishAffinity();
if (destAccount==null) {
if (destAccountId==0) {
Intent intent = new Intent(activity, WelcomeActivity.class);
if (qrAccount!=null) {
intent.putExtra(WelcomeActivity.QR_ACCOUNT_EXTRA, qrAccount);
@@ -302,17 +147,19 @@ public class AccountManager {
// ui
public void showSwitchAccountMenu(Activity activity) {
ArrayList<AccountManager.Account> accounts = getAccounts(activity);
DcAccounts accounts = DcHelper.getAccounts(activity);
int[] accountIds = accounts.getAll();
int selectedAccountId = accounts.getSelectedAccount().getAccountId();
// build menu
int presel = 0;
ArrayList<String> menu = new ArrayList<>();
for (int i = 0; i < accounts.size(); i++) {
AccountManager.Account account = accounts.get(i);
if (account.isCurrent()) {
for (int i = 0; i < accountIds.length; i++) {
DcContext context = accounts.getAccount(accountIds[i]);
if (accountIds[i] == selectedAccountId) {
presel = i;
}
menu.add(account.getDescr());
menu.add(context.getNameNAddr());
}
int addAccount = menu.size();
@@ -325,15 +172,14 @@ public class AccountManager {
.setSingleChoiceItems(menu.toArray(new String[menu.size()]), presel, (dialog, which) -> {
dialog.dismiss();
if (which==addAccount) {
new SwitchAccountAsyncTask(activity, R.string.one_moment, null, null, null).execute();
new SwitchAccountAsyncTask(activity, R.string.one_moment, 0, null).execute();
} else { // switch account
Account account = accounts.get(which);
if (!account.isCurrent()) {
new SwitchAccountAsyncTask(activity, R.string.switching_account, account, null, null).execute();
if (accountIds[which] != selectedAccountId) {
new SwitchAccountAsyncTask(activity, R.string.switching_account, accountIds[which], null).execute();
}
}
});
if (accounts.size() > 1) {
if (accountIds.length > 1) {
builder.setNeutralButton(R.string.delete_account, (dialog, which) -> {
showDeleteAccountMenu(activity);
});
@@ -342,11 +188,13 @@ public class AccountManager {
}
private void showDeleteAccountMenu(Activity activity) {
ArrayList<AccountManager.Account> accounts = getAccounts(activity);
DcAccounts accounts = DcHelper.getAccounts(activity);
int[] accountIds = accounts.getAll();
int selectedAccountId = accounts.getSelectedAccount().getAccountId();
ArrayList<String> menu = new ArrayList<>();
for (AccountManager.Account account : accounts) {
menu.add(account.getDescr());
for (int accountId : accountIds) {
menu.add(accounts.getAccount(accountId).getNameNAddr());
}
int[] selection = {-1};
new AlertDialog.Builder(activity)
@@ -354,20 +202,20 @@ public class AccountManager {
.setSingleChoiceItems(menu.toArray(new String[menu.size()]), -1, (dialog, which) -> selection[0] = which)
.setNegativeButton(R.string.cancel, (dialog, which) -> showSwitchAccountMenu(activity))
.setPositiveButton(R.string.ok, (dialog, which) -> {
if (selection[0] >= 0 && selection[0] < accounts.size()) {
AccountManager.Account account = accounts.get(selection[0]);
if (account.isCurrent()) {
if (selection[0] >= 0 && selection[0] < accountIds.length) {
int accountId = accountIds[selection[0]];
if (accountId == selectedAccountId) {
new AlertDialog.Builder(activity)
.setMessage("To delete the currently active account, switch to another account first.")
.setPositiveButton(R.string.ok, null)
.show();
} else {
new AlertDialog.Builder(activity)
.setTitle(account.getDescr())
.setTitle(accounts.getAccount(accountId).getNameNAddr())
.setMessage(R.string.forget_login_confirmation_desktop)
.setNegativeButton(R.string.cancel, (dialog2, which2) -> showSwitchAccountMenu(activity))
.setPositiveButton(R.string.ok, (dialog2, which2) -> {
deleteAccount(activity, account.getDbName());
accounts.removeAccount(accountId);
showSwitchAccountMenu(activity);
})
.show();
@@ -380,6 +228,6 @@ public class AccountManager {
}
public void addAccountFromQr(Activity activity, String qr) {
new SwitchAccountAsyncTask(activity, R.string.one_moment, null, null, qr).execute();
new SwitchAccountAsyncTask(activity, R.string.one_moment, 0, qr).execute();
}
}