mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
Merge pull request #1310 from deltachat/stop-threads
stop threads when switching between mupltiple accounts
This commit is contained in:
@@ -387,6 +387,12 @@ JNIEXPORT void Java_com_b44t_messenger_DcContext_performSmtpIdle(JNIEnv *env, jo
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT void Java_com_b44t_messenger_DcContext_interruptSmtpIdle(JNIEnv *env, jobject obj)
|
||||
{
|
||||
dc_interrupt_smtp_idle(get_dc_context(env, obj));
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT void Java_com_b44t_messenger_DcContext_maybeNetwork(JNIEnv *env, jobject obj)
|
||||
{
|
||||
dc_maybe_network(get_dc_context(env, obj));
|
||||
|
||||
@@ -106,6 +106,7 @@ public class DcContext {
|
||||
|
||||
public native void performSmtpJobs ();
|
||||
public native void performSmtpIdle ();
|
||||
public native void interruptSmtpIdle ();
|
||||
|
||||
public native void maybeNetwork ();
|
||||
public native void setConfig (String key, String value);
|
||||
|
||||
@@ -251,8 +251,6 @@ public class WelcomeActivity extends BaseActionBarActivity implements DcEventCen
|
||||
AccountManager accountManager = AccountManager.getInstance();
|
||||
if (accountManager.canRollbackAccountCreation(this)) {
|
||||
accountManager.rollbackAccountCreation(this);
|
||||
finish();
|
||||
startActivity(new Intent(getApplicationContext(), ConversationListActivity.class));
|
||||
} else {
|
||||
super.onBackPressed();
|
||||
}
|
||||
|
||||
@@ -16,8 +16,10 @@ import org.thoughtcrime.securesms.ConversationListActivity;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.WelcomeActivity;
|
||||
import org.thoughtcrime.securesms.util.Prefs;
|
||||
import org.thoughtcrime.securesms.util.task.ProgressDialogAsyncTask;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class AccountManager {
|
||||
@@ -93,6 +95,8 @@ public class AccountManager {
|
||||
// create an empty DcContext object - this will be set up then, starting with
|
||||
// getSelectedAccount()
|
||||
ApplicationContext appContext = (ApplicationContext)context.getApplicationContext();
|
||||
appContext.dcContext.stopThreads();
|
||||
appContext.dcContext.close();
|
||||
appContext.dcContext = new ApplicationDcContext(context);
|
||||
}
|
||||
|
||||
@@ -173,25 +177,15 @@ public class AccountManager {
|
||||
return !prevDbName.isEmpty();
|
||||
}
|
||||
|
||||
public void rollbackAccountCreation(Context context) {
|
||||
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
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", "");
|
||||
|
||||
sharedPreferences.edit().putString("prev_account_db_name", "").apply();
|
||||
sharedPreferences.edit().putString("curr_account_db_name", prevDbName).apply();
|
||||
|
||||
// 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);
|
||||
if (testContext.open(new File(context.getFilesDir(), inCreationDbName).getAbsolutePath()) != 0) {
|
||||
if (testContext.isConfigured() == 0) {
|
||||
testContext.close();
|
||||
deleteAccount(context, inCreationDbName);
|
||||
}
|
||||
Account prevAccount = maybeGetAccount(new File(activity.getFilesDir(), prevDbName));
|
||||
if (prevAccount!=null) {
|
||||
new SwitchAccountAsyncTask(activity, R.string.switch_account, prevAccount, inCreationDbName).execute();
|
||||
}
|
||||
|
||||
resetDcContext(context);
|
||||
}
|
||||
|
||||
|
||||
@@ -210,6 +204,60 @@ public class AccountManager {
|
||||
}
|
||||
|
||||
|
||||
// helper class for switching accounts gracefully
|
||||
|
||||
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;
|
||||
|
||||
public SwitchAccountAsyncTask(Activity activity, int title, @Nullable Account destAccount, @Nullable String deleteDbName) {
|
||||
super(activity, title, R.string.one_moment);
|
||||
this.activityWeakReference = new WeakReference<>(activity);
|
||||
this.destAccount = destAccount;
|
||||
this.deleteDbName = deleteDbName;
|
||||
}
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
Activity activity = activityWeakReference.get();
|
||||
if (activity!=null) {
|
||||
if (destAccount==null) {
|
||||
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);
|
||||
if (testContext.open(new File(activity.getFilesDir(), deleteDbName).getAbsolutePath()) != 0) {
|
||||
if (testContext.isConfigured() == 0) {
|
||||
testContext.close();
|
||||
AccountManager.getInstance().deleteAccount(activity, deleteDbName);
|
||||
PreferenceManager.getDefaultSharedPreferences(activity)
|
||||
.edit().putString("prev_account_db_name", "").apply();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
Activity activity = activityWeakReference.get();
|
||||
if (activity!=null) {
|
||||
activity.finishAffinity();
|
||||
if (destAccount==null) {
|
||||
activity.startActivity(new Intent(activity, WelcomeActivity.class));
|
||||
} else {
|
||||
activity.startActivity(new Intent(activity.getApplicationContext(), ConversationListActivity.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ui
|
||||
|
||||
public void handleSwitchAccount(Activity activity) {
|
||||
@@ -234,18 +282,13 @@ public class AccountManager {
|
||||
.setTitle(R.string.switch_account)
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.setSingleChoiceItems(menu.toArray(new String[menu.size()]), presel, (dialog, which) -> {
|
||||
dialog.dismiss();
|
||||
if (which==addAccount) {
|
||||
beginAccountCreation(activity);
|
||||
activity.finishAffinity();
|
||||
activity.startActivity(new Intent(activity, WelcomeActivity.class));
|
||||
new SwitchAccountAsyncTask(activity, R.string.add_account, null, null).execute();
|
||||
} else { // switch account
|
||||
Account account = accounts.get(which);
|
||||
if (account.isCurrent()) {
|
||||
dialog.dismiss();
|
||||
} else {
|
||||
switchAccount(activity, account);
|
||||
activity.finishAffinity();
|
||||
activity.startActivity(new Intent(activity.getApplicationContext(), ConversationListActivity.class));
|
||||
if (!account.isCurrent()) {
|
||||
new SwitchAccountAsyncTask(activity, R.string.switch_account, account, null).execute();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -296,6 +296,8 @@ public class ApplicationDcContext extends DcContext {
|
||||
|
||||
public final static int INTERRUPT_IDLE = 0x01; // interrupt idle if the thread is already running
|
||||
|
||||
public boolean run = true;
|
||||
|
||||
public void startThreads(int flags) {
|
||||
synchronized (threadsCritical) {
|
||||
|
||||
@@ -303,7 +305,7 @@ public class ApplicationDcContext extends DcContext {
|
||||
|
||||
imapThread = new Thread(() -> {
|
||||
Log.i(TAG, "###################### IMAP-Thread started. ######################");
|
||||
while (true) {
|
||||
while (run) {
|
||||
imapWakeLock.acquire();
|
||||
performImapJobs();
|
||||
performImapFetch();
|
||||
@@ -313,6 +315,7 @@ public class ApplicationDcContext extends DcContext {
|
||||
}
|
||||
performImapIdle();
|
||||
}
|
||||
Log.i(TAG, "!!!!!!!!!!!! IMAP-Thread stopped");
|
||||
}, "imapThread");
|
||||
imapThread.setPriority(Thread.NORM_PRIORITY);
|
||||
imapThread.start();
|
||||
@@ -327,7 +330,7 @@ public class ApplicationDcContext extends DcContext {
|
||||
|
||||
mvboxThread = new Thread(() -> {
|
||||
Log.i(TAG, "###################### MVBOX-Thread started. ######################");
|
||||
while (true) {
|
||||
while (run) {
|
||||
mvboxWakeLock.acquire();
|
||||
performMvboxJobs();
|
||||
performMvboxFetch();
|
||||
@@ -337,6 +340,7 @@ public class ApplicationDcContext extends DcContext {
|
||||
}
|
||||
performMvboxIdle();
|
||||
}
|
||||
Log.i(TAG, "!!!!!!!!!!!! MVBOX-Thread stopped");
|
||||
}, "mvboxThread");
|
||||
mvboxThread.setPriority(Thread.NORM_PRIORITY);
|
||||
mvboxThread.start();
|
||||
@@ -351,13 +355,14 @@ public class ApplicationDcContext extends DcContext {
|
||||
|
||||
sentboxThread = new Thread(() -> {
|
||||
Log.i(TAG, "###################### SENTBOX-Thread started. ######################");
|
||||
while (true) {
|
||||
while (run) {
|
||||
sentboxWakeLock.acquire();
|
||||
performSentboxJobs();
|
||||
performSentboxFetch();
|
||||
sentboxWakeLock.release();
|
||||
performSentboxIdle();
|
||||
}
|
||||
Log.i(TAG, "!!!!!!!!!!!! SENTBOX-Thread stopped");
|
||||
}, "sentboxThread");
|
||||
sentboxThread.setPriority(Thread.NORM_PRIORITY-1);
|
||||
sentboxThread.start();
|
||||
@@ -370,7 +375,7 @@ public class ApplicationDcContext extends DcContext {
|
||||
if (smtpThread == null || !smtpThread.isAlive()) {
|
||||
smtpThread = new Thread(() -> {
|
||||
Log.i(TAG, "###################### SMTP-Thread started. ######################");
|
||||
while (true) {
|
||||
while (run) {
|
||||
smtpWakeLock.acquire();
|
||||
performSmtpJobs();
|
||||
smtpWakeLock.release();
|
||||
@@ -379,6 +384,7 @@ public class ApplicationDcContext extends DcContext {
|
||||
}
|
||||
performSmtpIdle();
|
||||
}
|
||||
Log.i(TAG, "!!!!!!!!!!!! SMTP-Thread stopped");
|
||||
}, "smtpThread");
|
||||
smtpThread.setPriority(Thread.MAX_PRIORITY);
|
||||
smtpThread.start();
|
||||
@@ -397,6 +403,32 @@ public class ApplicationDcContext extends DcContext {
|
||||
}
|
||||
}
|
||||
|
||||
public void stopThreads() {
|
||||
run = false;
|
||||
synchronized (threadsCritical) {
|
||||
while (true) {
|
||||
|
||||
// in theory, interrupting once outside the loop should be sufficient,
|
||||
// but there are some corner cases, see https://github.com/deltachat/deltachat-core-rust/issues/925
|
||||
Log.i(TAG, "!!!!!!!!!!!! Stopping threads ...");
|
||||
if (imapThread!=null && imapThread.isAlive()) { interruptImapIdle(); }
|
||||
if (mvboxThread!=null && mvboxThread.isAlive()) { interruptMvboxIdle(); }
|
||||
if (sentboxThread!=null && sentboxThread.isAlive()) { interruptSentboxIdle(); }
|
||||
if (smtpThread!=null && smtpThread.isAlive()) { interruptSmtpIdle(); }
|
||||
|
||||
Util.sleep(300);
|
||||
|
||||
if ( (imapThread==null || !imapThread.isAlive())
|
||||
&& (mvboxThread==null || !mvboxThread.isAlive())
|
||||
&& (sentboxThread==null || !sentboxThread.isAlive())
|
||||
&& (smtpThread==null || !smtpThread.isAlive())) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Log.i(TAG, "!!!!!!!!!!!! threads stopped");
|
||||
}
|
||||
|
||||
|
||||
/***********************************************************************************************
|
||||
* Tools
|
||||
|
||||
Reference in New Issue
Block a user