From d42eb1d920ba74b77e7e41f0e7eb6d2488e0d34b Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Mon, 25 May 2020 16:59:13 +0200 Subject: [PATCH 1/9] get rid of open/close context --- jni/dc_wrapper.c | 77 ++----------------- src/com/b44t/messenger/DcContext.java | 28 +++---- .../securesms/connect/AccountManager.java | 12 +-- .../connect/ApplicationDcContext.java | 22 ++++-- 4 files changed, 43 insertions(+), 96 deletions(-) diff --git a/jni/dc_wrapper.c b/jni/dc_wrapper.c index 3afac1711..4afdf40df 100644 --- a/jni/dc_wrapper.c +++ b/jni/dc_wrapper.c @@ -173,14 +173,6 @@ static uint32_t* jintArray2uint32Pointer(JNIEnv* env, jintArray ja, int* ret_icn ******************************************************************************/ -typedef struct dc_jnicontext_t { - JavaVM* jvm; // JNIEnv cannot be shared between threads, so we share the JavaVM object - jclass cls; - jobject obj; - jmethodID methodId; -} dc_jnicontext_t; - - static dc_context_t* get_dc_context(JNIEnv *env, jobject obj) { static jfieldID fid = 0; @@ -195,42 +187,12 @@ static dc_context_t* get_dc_context(JNIEnv *env, jobject obj) } -static uintptr_t s_context_callback_(dc_context_t* context, int event, uintptr_t data1, uintptr_t data2) +JNIEXPORT jlong Java_com_b44t_messenger_DcContext_createContextCPtr(JNIEnv *env, jobject obj, jstring osname, jstring dbfile) { - jlong l; - JNIEnv* env; - dc_jnicontext_t* jnicontext = dc_get_userdata(context); - - if (jnicontext==NULL || jnicontext->jvm==NULL || jnicontext->cls==NULL || jnicontext->obj==NULL || jnicontext->methodId==NULL) { - return 0; /* may happen on startup */ - } - - (*jnicontext->jvm)->GetEnv(jnicontext->jvm, (void**)&env, JNI_VERSION_1_6); // as this function may be called from _any_ thread, we cannot use a static pointer to JNIEnv - if (env==NULL) { - return 0; /* may happen on startup */ - } - - l = (*env)->CallLongMethod(env, jnicontext->obj, jnicontext->methodId, (jint)event, (jlong)data1, (jlong)data2); - return (uintptr_t)l; -} - - -JNIEXPORT jlong Java_com_b44t_messenger_DcContext_createContextCPtr(JNIEnv *env, jobject obj, jstring osname) -{ - jclass cls = (*env)->GetObjectClass(env, obj); - - dc_jnicontext_t* jnicontext = calloc(1, sizeof(dc_jnicontext_t)); - if (cls==NULL || jnicontext==NULL) { - return 0; - } - - (*env)->GetJavaVM(env, &jnicontext->jvm); - jnicontext->cls = (*env)->NewGlobalRef(env, cls); - jnicontext->obj = (*env)->NewGlobalRef(env, obj); - jnicontext->methodId = (*env)->GetMethodID(env, jnicontext->cls, "handleEvent","(IJJ)J" /*signature as "(param)ret" with I=int, J=long*/); - CHAR_REF(osname); - jlong contextCPtr = (jlong)dc_context_new(s_context_callback_, jnicontext, osnamePtr); + CHAR_REF(dbfile) + jlong contextCPtr = (jlong)dc_context_new(osnamePtr, dbfilePtr, NULL); + CHAR_UNREF(dbfile) CHAR_UNREF(osname); return contextCPtr; } @@ -244,21 +206,6 @@ JNIEXPORT void Java_com_b44t_messenger_DcContact_unrefContextCPtr(JNIEnv *env, j /* DcContext - open/configure/connect/fetch */ -JNIEXPORT jint Java_com_b44t_messenger_DcContext_open(JNIEnv *env, jobject obj, jstring dbfile) -{ - CHAR_REF(dbfile); - jint ret = dc_open(get_dc_context(env, obj), dbfilePtr, NULL); - CHAR_UNREF(dbfile) - return ret; -} - - -JNIEXPORT void Java_com_b44t_messenger_DcContext_close(JNIEnv *env, jobject obj) -{ - dc_close(get_dc_context(env, obj)); -} - - JNIEXPORT void Java_com_b44t_messenger_DcContext_setStockTranslation(JNIEnv *env, jobject obj, jint stock_id, jstring translation) { CHAR_REF(translation); @@ -303,93 +250,79 @@ JNIEXPORT jint Java_com_b44t_messenger_DcContext_isConfigured(JNIEnv *env, jobje } +// TODO-ASYNC: delete these functions, also in the java-part JNIEXPORT void Java_com_b44t_messenger_DcContext_performImapJobs(JNIEnv *env, jobject obj) { - dc_perform_imap_jobs(get_dc_context(env, obj)); } JNIEXPORT void Java_com_b44t_messenger_DcContext_performImapIdle(JNIEnv *env, jobject obj) { - dc_perform_imap_idle(get_dc_context(env, obj)); } JNIEXPORT void Java_com_b44t_messenger_DcContext_performImapFetch(JNIEnv *env, jobject obj) { - dc_perform_imap_fetch(get_dc_context(env, obj)); } JNIEXPORT void Java_com_b44t_messenger_DcContext_interruptImapIdle(JNIEnv *env, jobject obj) { - dc_interrupt_imap_idle(get_dc_context(env, obj)); } JNIEXPORT void Java_com_b44t_messenger_DcContext_performSentboxJobs(JNIEnv *env, jobject obj) { - dc_perform_sentbox_jobs(get_dc_context(env, obj)); } JNIEXPORT void Java_com_b44t_messenger_DcContext_performSentboxFetch(JNIEnv *env, jobject obj) { - dc_perform_sentbox_fetch(get_dc_context(env, obj)); } JNIEXPORT void Java_com_b44t_messenger_DcContext_performSentboxIdle(JNIEnv *env, jobject obj) { - dc_perform_sentbox_idle(get_dc_context(env, obj)); } JNIEXPORT void Java_com_b44t_messenger_DcContext_interruptSentboxIdle(JNIEnv *env, jobject obj) { - dc_interrupt_sentbox_idle(get_dc_context(env, obj)); } JNIEXPORT void Java_com_b44t_messenger_DcContext_performMvboxJobs(JNIEnv *env, jobject obj) { - dc_perform_mvbox_jobs(get_dc_context(env, obj)); } JNIEXPORT void Java_com_b44t_messenger_DcContext_performMvboxFetch(JNIEnv *env, jobject obj) { - dc_perform_mvbox_fetch(get_dc_context(env, obj)); } JNIEXPORT void Java_com_b44t_messenger_DcContext_performMvboxIdle(JNIEnv *env, jobject obj) { - dc_perform_mvbox_idle(get_dc_context(env, obj)); } JNIEXPORT void Java_com_b44t_messenger_DcContext_interruptMvboxIdle(JNIEnv *env, jobject obj) { - dc_interrupt_mvbox_idle(get_dc_context(env, obj)); } JNIEXPORT void Java_com_b44t_messenger_DcContext_performSmtpJobs(JNIEnv *env, jobject obj) { - dc_perform_smtp_jobs(get_dc_context(env, obj)); } JNIEXPORT void Java_com_b44t_messenger_DcContext_performSmtpIdle(JNIEnv *env, jobject obj) { - dc_perform_smtp_idle(get_dc_context(env, obj)); } JNIEXPORT void Java_com_b44t_messenger_DcContext_interruptSmtpIdle(JNIEnv *env, jobject obj) { - dc_interrupt_smtp_idle(get_dc_context(env, obj)); } diff --git a/src/com/b44t/messenger/DcContext.java b/src/com/b44t/messenger/DcContext.java index 38a4f8fca..fecb50c38 100644 --- a/src/com/b44t/messenger/DcContext.java +++ b/src/com/b44t/messenger/DcContext.java @@ -72,20 +72,27 @@ public class DcContext { public final static int DC_EMPTY_MVBOX = 0x01; public final static int DC_EMPTY_INBOX = 0x02; - public DcContext(String osName) { - handleEvent(0,0,0); // call handleEvent() to make sure it is not optimized away and JNI won't find it - contextCPtr = createContextCPtr(osName); + public DcContext(String osName, String dbfile) { + contextCPtr = createContextCPtr(osName, dbfile); + } + + public boolean isOk() { + return contextCPtr != 0; } @Override protected void finalize() throws Throwable { super.finalize(); - unrefContextCPtr(); - contextCPtr = 0; + unref(); + } + + public void unref() { + if (contextCPtr != 0) { + unrefContextCPtr(); + contextCPtr = 0; + } } - public native int open (String dbfile); - public native void close (); public native void setStockTranslation (int stockId, String translation); public native String getBlobdir (); public native void configure (); @@ -189,11 +196,6 @@ public class DcContext { */ public native boolean setLocation (float latitude, float longitude, float accuracy); - // event handling - you should @Override this function in derived classes - public long handleEvent(int event, long data1, long data2) { - return 0; - } - // helper to get/return strings from/to handleEvent() public native static boolean data1IsString(int event); public native static boolean data2IsString(int event); @@ -201,7 +203,7 @@ public class DcContext { // working with raw c-data private long contextCPtr; // CAVE: the name is referenced in the JNI - private native long createContextCPtr(String osName); + private native long createContextCPtr(String osName, String dbfile); private native void unrefContextCPtr (); public native long createMsgCPtr (int viewtype); private native long getChatlistCPtr (int listflags, String query, int queryId); diff --git a/src/org/thoughtcrime/securesms/connect/AccountManager.java b/src/org/thoughtcrime/securesms/connect/AccountManager.java index bbabb281a..aa8e6ef09 100644 --- a/src/org/thoughtcrime/securesms/connect/AccountManager.java +++ b/src/org/thoughtcrime/securesms/connect/AccountManager.java @@ -65,8 +65,8 @@ public class AccountManager { private @Nullable Account maybeGetAccount(File file) { try { if (!file.isDirectory() && file.getName().endsWith(".db")) { - DcContext testContext = new DcContext(null); - if (testContext.open(file.getAbsolutePath()) != 0) { + DcContext testContext = new DcContext(null, file.getAbsolutePath()); + if (testContext.isOk()) { Account ret = new Account(); ret.dbName = file.getName(); ret.displayname = testContext.getConfig("displayname"); @@ -99,7 +99,7 @@ public class AccountManager { // getSelectedAccount() ApplicationContext appContext = (ApplicationContext)context.getApplicationContext(); appContext.dcContext.stopThreads(); - appContext.dcContext.close(); + appContext.dcContext.unref(); appContext.dcContext = new ApplicationDcContext(context); } @@ -243,10 +243,10 @@ public class AccountManager { 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) { + DcContext testContext = new DcContext(null, new File(activity.getFilesDir(), deleteDbName).getAbsolutePath()); + if (testContext.isOk()) { if (testContext.isConfigured() == 0) { - testContext.close(); + testContext.unref(); AccountManager.getInstance().deleteAccount(activity, deleteDbName); PreferenceManager.getDefaultSharedPreferences(activity) .edit().putString("prev_account_db_name", "").apply(); diff --git a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java index e52af1e4a..a410f732a 100644 --- a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java +++ b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java @@ -56,12 +56,9 @@ public class ApplicationDcContext extends DcContext { public NotificationCenter notificationCenter; public ApplicationDcContext(Context context) { - super("Android "+BuildConfig.VERSION_NAME); + super("Android "+BuildConfig.VERSION_NAME, AccountManager.getInstance().getSelectedAccount(context).getAbsolutePath()); this.context = context; - File dbfile = AccountManager.getInstance().getSelectedAccount(context); - open(dbfile.getAbsolutePath()); - // migration, can be removed after some versions (added 5/2020) // (this will convert only for one account, but that is fine, multi-account is experimental anyway) try { @@ -341,6 +338,10 @@ public class ApplicationDcContext extends DcContext { public boolean run = true; public void startThreads(int flags) { + + // TODO-ASYNC: remove this function, handle the flags differently + + /* synchronized (threadsCritical) { if (imapThread == null || !imapThread.isAlive()) { @@ -432,9 +433,14 @@ public class ApplicationDcContext extends DcContext { smtpThread.start(); } } + */ } public void waitForThreadsExecutedOnce() { + + // TODO-ASYNC + + /* while(true) { synchronized (incLoopsCritical) { if(inboxLoops>0 && mvboxLoops>0 && smtpLoops>0) { @@ -443,10 +449,15 @@ public class ApplicationDcContext extends DcContext { } Util.sleep(500); } + */ } public void stopThreads() { notificationCenter.removeAllNotifiations(); + + // TODO-ASYNC + + /* run = false; synchronized (threadsCritical) { while (true) { @@ -470,6 +481,7 @@ public class ApplicationDcContext extends DcContext { } } Log.i(TAG, "!!!!!!!!!!!! threads stopped"); + */ } @@ -557,7 +569,7 @@ public class ApplicationDcContext extends DcContext { }); } - @Override + // TODO-ASYNC: make this work again public long handleEvent(final int event, long data1, long data2) { switch (event) { case DC_EVENT_INFO: From 8fd3b374c285da477f306f6d3d0bd5c41d058c29 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Mon, 25 May 2020 18:07:12 +0200 Subject: [PATCH 2/9] use start/stop io --- jni/dc_wrapper.c | 70 +----- src/com/b44t/messenger/DcContext.java | 21 +- .../securesms/RegistrationActivity.java | 2 +- .../securesms/WelcomeActivity.java | 2 +- .../securesms/connect/AccountManager.java | 3 +- .../connect/ApplicationDcContext.java | 208 ++---------------- .../securesms/connect/FetchWorker.java | 11 +- .../securesms/connect/ForegroundDetector.java | 2 +- 8 files changed, 31 insertions(+), 288 deletions(-) diff --git a/jni/dc_wrapper.c b/jni/dc_wrapper.c index 4afdf40df..90515b764 100644 --- a/jni/dc_wrapper.c +++ b/jni/dc_wrapper.c @@ -250,79 +250,21 @@ JNIEXPORT jint Java_com_b44t_messenger_DcContext_isConfigured(JNIEnv *env, jobje } -// TODO-ASYNC: delete these functions, also in the java-part -JNIEXPORT void Java_com_b44t_messenger_DcContext_performImapJobs(JNIEnv *env, jobject obj) +JNIEXPORT void Java_com_b44t_messenger_DcContext_startIo(JNIEnv *env, jobject obj) { + dc_start_io(get_dc_context(env, obj)); } -JNIEXPORT void Java_com_b44t_messenger_DcContext_performImapIdle(JNIEnv *env, jobject obj) +JNIEXPORT void Java_com_b44t_messenger_DcContext_stopIo(JNIEnv *env, jobject obj) { + dc_stop_io(get_dc_context(env, obj)); } -JNIEXPORT void Java_com_b44t_messenger_DcContext_performImapFetch(JNIEnv *env, jobject obj) -{ -} - - -JNIEXPORT void Java_com_b44t_messenger_DcContext_interruptImapIdle(JNIEnv *env, jobject obj) -{ -} - - -JNIEXPORT void Java_com_b44t_messenger_DcContext_performSentboxJobs(JNIEnv *env, jobject obj) -{ -} - - -JNIEXPORT void Java_com_b44t_messenger_DcContext_performSentboxFetch(JNIEnv *env, jobject obj) -{ -} - - -JNIEXPORT void Java_com_b44t_messenger_DcContext_performSentboxIdle(JNIEnv *env, jobject obj) -{ -} - - -JNIEXPORT void Java_com_b44t_messenger_DcContext_interruptSentboxIdle(JNIEnv *env, jobject obj) -{ -} - - -JNIEXPORT void Java_com_b44t_messenger_DcContext_performMvboxJobs(JNIEnv *env, jobject obj) -{ -} - - -JNIEXPORT void Java_com_b44t_messenger_DcContext_performMvboxFetch(JNIEnv *env, jobject obj) -{ -} - - -JNIEXPORT void Java_com_b44t_messenger_DcContext_performMvboxIdle(JNIEnv *env, jobject obj) -{ -} - - -JNIEXPORT void Java_com_b44t_messenger_DcContext_interruptMvboxIdle(JNIEnv *env, jobject obj) -{ -} - - -JNIEXPORT void Java_com_b44t_messenger_DcContext_performSmtpJobs(JNIEnv *env, jobject obj) -{ -} - - -JNIEXPORT void Java_com_b44t_messenger_DcContext_performSmtpIdle(JNIEnv *env, jobject obj) -{ -} - - -JNIEXPORT void Java_com_b44t_messenger_DcContext_interruptSmtpIdle(JNIEnv *env, jobject obj) +JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_isIoRunning(JNIEnv *env, jobject obj) { + return dc_is_io_running(get_dc_context(env, obj)) != 0; } diff --git a/src/com/b44t/messenger/DcContext.java b/src/com/b44t/messenger/DcContext.java index fecb50c38..cacd009a7 100644 --- a/src/com/b44t/messenger/DcContext.java +++ b/src/com/b44t/messenger/DcContext.java @@ -99,24 +99,9 @@ public class DcContext { public native void stopOngoingProcess (); public native int isConfigured (); - public native void performImapJobs (); - public native void performImapFetch (); - public native void performImapIdle (); - public native void interruptImapIdle (); - - public native void performMvboxJobs (); - public native void performMvboxFetch (); - public native void performMvboxIdle (); - public native void interruptMvboxIdle (); - - public native void performSentboxJobs (); - public native void performSentboxFetch (); - public native void performSentboxIdle (); - public native void interruptSentboxIdle (); - - public native void performSmtpJobs (); - public native void performSmtpIdle (); - public native void interruptSmtpIdle (); + public native void startIo (); + public native void stopIo (); + public native boolean isIoRunning (); public native void maybeNetwork (); public native void setConfig (String key, String value); diff --git a/src/org/thoughtcrime/securesms/RegistrationActivity.java b/src/org/thoughtcrime/securesms/RegistrationActivity.java index 5881755b9..e7765b81d 100644 --- a/src/org/thoughtcrime/securesms/RegistrationActivity.java +++ b/src/org/thoughtcrime/securesms/RegistrationActivity.java @@ -512,7 +512,7 @@ public class RegistrationActivity extends BaseActionBarActivity implements DcEve // calling configure() results in // receiving multiple DC_EVENT_CONFIGURE_PROGRESS events DcHelper.getContext(this).captureNextError(); - DcHelper.getContext(this).configure(); + DcHelper.getContext(this).configure(); // TODO-ASYNC: make sure, io is stopped before and started afterwards } private void setConfig(@IdRes int viewId, String configTarget, boolean doTrim) { diff --git a/src/org/thoughtcrime/securesms/WelcomeActivity.java b/src/org/thoughtcrime/securesms/WelcomeActivity.java index 0e1268acc..17a563211 100644 --- a/src/org/thoughtcrime/securesms/WelcomeActivity.java +++ b/src/org/thoughtcrime/securesms/WelcomeActivity.java @@ -147,7 +147,7 @@ public class WelcomeActivity extends BaseActionBarActivity implements DcEventCen // calling configure() results in // receiving multiple DC_EVENT_CONFIGURE_PROGRESS events - dcContext.configure(); + dcContext.configure(); // TODO-ASYNC: make sure, io is stopped before and started afterwards } private void progressError() { diff --git a/src/org/thoughtcrime/securesms/connect/AccountManager.java b/src/org/thoughtcrime/securesms/connect/AccountManager.java index aa8e6ef09..eeb928606 100644 --- a/src/org/thoughtcrime/securesms/connect/AccountManager.java +++ b/src/org/thoughtcrime/securesms/connect/AccountManager.java @@ -98,7 +98,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.notificationCenter.removeAllNotifiations(); + appContext.dcContext.maybeStopIo(); appContext.dcContext.unref(); appContext.dcContext = new ApplicationDcContext(context); } diff --git a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java index a410f732a..4e686bdbe 100644 --- a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java +++ b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java @@ -100,27 +100,20 @@ public class ApplicationDcContext extends DcContext { } // /migration - try { - PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); - - imapWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "imapWakeLock"); - imapWakeLock.setReferenceCounted(false); // if the idle-thread is killed for any reasons, it is better not to rely on reference counting - - mvboxWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "mvboxWakeLock"); - mvboxWakeLock.setReferenceCounted(false); // if the idle-thread is killed for any reasons, it is better not to rely on reference counting - - sentboxWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sentboxWakeLock"); - sentboxWakeLock.setReferenceCounted(false); // if the idle-thread is killed for any reasons, it is better not to rely on reference counting - - smtpWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "smtpWakeLock"); - smtpWakeLock.setReferenceCounted(false); // if the idle-thread is killed for any reasons, it is better not to rely on reference counting - - } catch (Exception e) { - Log.e(TAG, "Cannot create wakeLocks"); - } - notificationCenter = new NotificationCenter(this); - startThreads(0); + maybeStartIo(); + } + + public void maybeStartIo() { + if (!isIoRunning()) { + startIo(); + } + } + + public void maybeStopIo() { + if (isIoRunning()) { + stopIo(); + } } public void setStockTranslations() { @@ -310,181 +303,6 @@ public class ApplicationDcContext extends DcContext { chat.getVisibility(), verified, chat.isSendingLocations(), chat.isMuted(), summary); } - - /*********************************************************************************************** - * Working Threads - **********************************************************************************************/ - - private final Object threadsCritical = new Object(); - private final Object incLoopsCritical= new Object(); - - public Thread imapThread = null; - private PowerManager.WakeLock imapWakeLock = null; - private int inboxLoops = 0; - - public Thread mvboxThread = null; - private PowerManager.WakeLock mvboxWakeLock = null; - private int mvboxLoops = 0; - - public Thread sentboxThread = null; - private PowerManager.WakeLock sentboxWakeLock = null; - - public Thread smtpThread = null; - private PowerManager.WakeLock smtpWakeLock = null; - private int smtpLoops = 0; - - public final static int INTERRUPT_IDLE = 0x01; // interrupt idle if the thread is already running - - public boolean run = true; - - public void startThreads(int flags) { - - // TODO-ASYNC: remove this function, handle the flags differently - - /* - synchronized (threadsCritical) { - - if (imapThread == null || !imapThread.isAlive()) { - - imapThread = new Thread(() -> { - Log.i(TAG, "###################### IMAP-Thread started. ######################"); - while (run) { - imapWakeLock.acquire(); - performImapJobs(); - performImapFetch(); - imapWakeLock.release(); - synchronized (incLoopsCritical) { - inboxLoops++; - } - performImapIdle(); - } - Log.i(TAG, "!!!!!!!!!!!! IMAP-Thread stopped"); - }, "imapThread"); - imapThread.setPriority(Thread.NORM_PRIORITY); - imapThread.start(); - } else { - if ((flags & INTERRUPT_IDLE) != 0) { - interruptImapIdle(); - } - } - - - if (mvboxThread == null || !mvboxThread.isAlive()) { - - mvboxThread = new Thread(() -> { - Log.i(TAG, "###################### MVBOX-Thread started. ######################"); - while (run) { - mvboxWakeLock.acquire(); - performMvboxJobs(); - performMvboxFetch(); - mvboxWakeLock.release(); - synchronized (incLoopsCritical) { - mvboxLoops++; - } - performMvboxIdle(); - } - Log.i(TAG, "!!!!!!!!!!!! MVBOX-Thread stopped"); - }, "mvboxThread"); - mvboxThread.setPriority(Thread.NORM_PRIORITY); - mvboxThread.start(); - } else { - if ((flags & INTERRUPT_IDLE) != 0) { - interruptMvboxIdle(); - } - } - - - if (sentboxThread == null || !sentboxThread.isAlive()) { - - sentboxThread = new Thread(() -> { - Log.i(TAG, "###################### SENTBOX-Thread started. ######################"); - while (run) { - sentboxWakeLock.acquire(); - performSentboxJobs(); - performSentboxFetch(); - sentboxWakeLock.release(); - performSentboxIdle(); - } - Log.i(TAG, "!!!!!!!!!!!! SENTBOX-Thread stopped"); - }, "sentboxThread"); - sentboxThread.setPriority(Thread.NORM_PRIORITY-1); - sentboxThread.start(); - } else { - if ((flags & INTERRUPT_IDLE) != 0) { - interruptSentboxIdle(); - } - } - - if (smtpThread == null || !smtpThread.isAlive()) { - smtpThread = new Thread(() -> { - Log.i(TAG, "###################### SMTP-Thread started. ######################"); - while (run) { - smtpWakeLock.acquire(); - performSmtpJobs(); - smtpWakeLock.release(); - synchronized (incLoopsCritical) { - smtpLoops++; - } - performSmtpIdle(); - } - Log.i(TAG, "!!!!!!!!!!!! SMTP-Thread stopped"); - }, "smtpThread"); - smtpThread.setPriority(Thread.MAX_PRIORITY); - smtpThread.start(); - } - } - */ - } - - public void waitForThreadsExecutedOnce() { - - // TODO-ASYNC - - /* - while(true) { - synchronized (incLoopsCritical) { - if(inboxLoops>0 && mvboxLoops>0 && smtpLoops>0) { - break; - } - } - Util.sleep(500); - } - */ - } - - public void stopThreads() { - notificationCenter.removeAllNotifiations(); - - // TODO-ASYNC - - /* - 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 **********************************************************************************************/ diff --git a/src/org/thoughtcrime/securesms/connect/FetchWorker.java b/src/org/thoughtcrime/securesms/connect/FetchWorker.java index 2fe3da400..d9eb546ec 100644 --- a/src/org/thoughtcrime/securesms/connect/FetchWorker.java +++ b/src/org/thoughtcrime/securesms/connect/FetchWorker.java @@ -19,17 +19,14 @@ public class FetchWorker extends Worker { @Override public @NonNull Result doWork() { - // MAYBE TODO: - // - when no threads are running: fetch-inbox, maybe fetch-mvbox, do smtp-jobs. - // fetch-sendbox is not needed as these messages shall not be notified. - // - when threads are running: interrupt-all-idle - Log.i("DeltaChat", "-------------------- FetchWorker.doWork() started --------------------"); ApplicationDcContext dcContext = DcHelper.getContext(context); - dcContext.startThreads(ApplicationDcContext.INTERRUPT_IDLE); - dcContext.waitForThreadsExecutedOnce(); + dcContext.maybeStartIo(); Log.i("DeltaChat", "-------------------- FetchWorker.doWork() done --------------------"); + // TODO-ASYNC: check, that the threads get some minimal amount of time and the app is not terminated + // directly after returning + return Result.success(); // when returning, the os may terminate the app again } } diff --git a/src/org/thoughtcrime/securesms/connect/ForegroundDetector.java b/src/org/thoughtcrime/securesms/connect/ForegroundDetector.java index 1876b9bee..efd343e98 100644 --- a/src/org/thoughtcrime/securesms/connect/ForegroundDetector.java +++ b/src/org/thoughtcrime/securesms/connect/ForegroundDetector.java @@ -37,7 +37,7 @@ public class ForegroundDetector implements Application.ActivityLifecycleCallback public void onActivityStarted(Activity activity) { refs++; - application.dcContext.startThreads(0); + application.dcContext.maybeStartIo(); } From cd8f8d1ede7b3ef98806dececca15c138001bfe1 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Mon, 25 May 2020 21:21:07 +0200 Subject: [PATCH 3/9] add new event and event-emitter classes --- jni/dc_wrapper.c | 88 ++++++++++++++++++++++ src/com/b44t/messenger/DcContext.java | 2 + src/com/b44t/messenger/DcEvent.java | 23 ++++++ src/com/b44t/messenger/DcEventEmitter.java | 21 ++++++ 4 files changed, 134 insertions(+) create mode 100644 src/com/b44t/messenger/DcEvent.java create mode 100644 src/com/b44t/messenger/DcEventEmitter.java diff --git a/jni/dc_wrapper.c b/jni/dc_wrapper.c index 90515b764..aaf150f7b 100644 --- a/jni/dc_wrapper.c +++ b/jni/dc_wrapper.c @@ -273,6 +273,11 @@ JNIEXPORT void Java_com_b44t_messenger_DcContext_maybeNetwork(JNIEnv *env, jobje dc_maybe_network(get_dc_context(env, obj)); } +JNIEXPORT jlong Java_com_b44t_messenger_DcContext_getEventEmitterCPtr(JNIEnv *env, jobject obj) +{ + return (jlong)dc_get_event_emitter(get_dc_context(env, obj)); +} + /* DcContext - handle contacts */ @@ -765,6 +770,89 @@ JNIEXPORT jlong Java_com_b44t_messenger_DcContext_getProviderFromEmailCPtr(JNIEn } +/******************************************************************************* + * DcEventEmitter + ******************************************************************************/ + + +static dc_event_emitter_t* get_dc_event_emitter(JNIEnv *env, jobject obj) +{ + static jfieldID fid = 0; + if (fid==0) { + jclass cls = (*env)->GetObjectClass(env, obj); + fid = (*env)->GetFieldID(env, cls, "eventEmitterCPtr", "J" /*Signature, J=long*/); + } + if (fid) { + return (dc_event_emitter_t*)(*env)->GetLongField(env, obj, fid); + } + return NULL; +} + + +JNIEXPORT void Java_com_b44t_messenger_DcEventEmitter_unrefEventEmitterCPtr(JNIEnv *env, jobject obj) +{ + dc_event_emitter_unref(get_dc_event_emitter(env, obj)); +} + + +JNIEXPORT jlong Java_com_b44t_messenger_DcEventEmitter_getNextEventCPtr(JNIEnv *env, jobject obj) +{ + return (jlong)dc_get_next_event(get_dc_event_emitter(env, obj)); +} + + +/******************************************************************************* + * DcEvent + ******************************************************************************/ + + +static dc_event_t* get_dc_event(JNIEnv *env, jobject obj) +{ + static jfieldID fid = 0; + if (fid==0) { + jclass cls = (*env)->GetObjectClass(env, obj); + fid = (*env)->GetFieldID(env, cls, "eventCPtr", "J" /*Signature, J=long*/); + } + if (fid) { + return (dc_event_t*)(*env)->GetLongField(env, obj, fid); + } + return NULL; +} + + +JNIEXPORT void Java_com_b44t_messenger_DcEvent_unrefEventCPtr(JNIEnv *env, jobject obj) +{ + dc_event_unref(get_dc_event(env, obj)); +} + + +JNIEXPORT jint Java_com_b44t_messenger_DcEvent_getId(JNIEnv *env, jobject obj) +{ + return dc_event_get_id(get_dc_event(env, obj)); +} + + +JNIEXPORT jint Java_com_b44t_messenger_DcEvent_getData1Int(JNIEnv *env, jobject obj) +{ + return dc_event_get_data1_int(get_dc_event(env, obj)); +} + + +JNIEXPORT jint Java_com_b44t_messenger_DcEvent_getData2Int(JNIEnv *env, jobject obj) +{ + return dc_event_get_data2_int(get_dc_event(env, obj)); +} + + +JNIEXPORT jstring Java_com_b44t_messenger_DcEvent_getData2Str(JNIEnv *env, jobject obj) +{ + char* temp = dc_event_get_data2_str(get_dc_event(env, obj)); + jstring ret = JSTRING_NEW(temp); + dc_str_unref(temp); + return ret; +} + + /******************************************************************************* * DcArray ******************************************************************************/ diff --git a/src/com/b44t/messenger/DcContext.java b/src/com/b44t/messenger/DcContext.java index cacd009a7..9e1b9b5e5 100644 --- a/src/com/b44t/messenger/DcContext.java +++ b/src/com/b44t/messenger/DcContext.java @@ -93,6 +93,7 @@ public class DcContext { } } + public DcEventEmitter getEventEmitter () { return new DcEventEmitter(getEventEmitterCPtr()); } public native void setStockTranslation (int stockId, String translation); public native String getBlobdir (); public native void configure (); @@ -190,6 +191,7 @@ public class DcContext { private long contextCPtr; // CAVE: the name is referenced in the JNI private native long createContextCPtr(String osName, String dbfile); private native void unrefContextCPtr (); + private native long getEventEmitterCPtr(); public native long createMsgCPtr (int viewtype); private native long getChatlistCPtr (int listflags, String query, int queryId); private native long getChatCPtr (int chat_id); diff --git a/src/com/b44t/messenger/DcEvent.java b/src/com/b44t/messenger/DcEvent.java new file mode 100644 index 000000000..965ad3aec --- /dev/null +++ b/src/com/b44t/messenger/DcEvent.java @@ -0,0 +1,23 @@ +package com.b44t.messenger; + +public class DcEvent { + + public DcEvent(long eventCPtr) { + this.eventCPtr = eventCPtr; + } + + @Override protected void finalize() throws Throwable { + super.finalize(); + unrefEventCPtr();; + eventCPtr = 0; + } + + public native int getId (); + public native int getData1Int (); + public native int getData2Int (); + public native String getData2Str (); + + // working with raw c-data + private long eventCPtr; // CAVE: the name is referenced in the JNI + private native void unrefEventCPtr(); +} diff --git a/src/com/b44t/messenger/DcEventEmitter.java b/src/com/b44t/messenger/DcEventEmitter.java new file mode 100644 index 000000000..217b04bde --- /dev/null +++ b/src/com/b44t/messenger/DcEventEmitter.java @@ -0,0 +1,21 @@ +package com.b44t.messenger; + +public class DcEventEmitter { + + public DcEventEmitter(long eventEmitterCPtr) { + this.eventEmitterCPtr = eventEmitterCPtr; + } + + @Override protected void finalize() throws Throwable { + super.finalize(); + unrefEventEmitterCPtr();; + eventEmitterCPtr = 0; + } + + public DcEvent getNextEvent () { return new DcEvent(getNextEventCPtr()); } + + // working with raw c-data + private long eventEmitterCPtr; // CAVE: the name is referenced in the JNI + private native long getNextEventCPtr (); + private native void unrefEventEmitterCPtr(); +} From 1d8397adc95629de413f5107449dc7131dd285f3 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Mon, 25 May 2020 21:53:36 +0200 Subject: [PATCH 4/9] use new event system --- jni/dc_wrapper.c | 19 +------- src/com/b44t/messenger/DcContext.java | 2 - .../connect/ApplicationDcContext.java | 43 ++++++++++++------- 3 files changed, 28 insertions(+), 36 deletions(-) diff --git a/jni/dc_wrapper.c b/jni/dc_wrapper.c index aaf150f7b..9a9f79ff0 100644 --- a/jni/dc_wrapper.c +++ b/jni/dc_wrapper.c @@ -1616,29 +1616,12 @@ JNIEXPORT jstring Java_com_b44t_messenger_DcProvider_getOverviewPage(JNIEnv *env return ret; } + /******************************************************************************* * Tools ******************************************************************************/ - -JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_data1IsString(JNIEnv *env, jclass cls, jint event) -{ - return DC_EVENT_DATA1_IS_STRING(event); -} - - JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_data2IsString(JNIEnv *env, jclass cls, jint event) { return DC_EVENT_DATA2_IS_STRING(event); } - - -JNIEXPORT jstring Java_com_b44t_messenger_DcContext_dataToString(JNIEnv *env, jclass cls, jlong data) -{ - /* the callback may return a long that represents a pointer to a C-String; this function creates a Java-string from such values. */ - if (data==0) { - return NULL; - } - const char* cstring = (const char*)data; - return JSTRING_NEW(cstring); -} \ No newline at end of file diff --git a/src/com/b44t/messenger/DcContext.java b/src/com/b44t/messenger/DcContext.java index 9e1b9b5e5..1046a4c38 100644 --- a/src/com/b44t/messenger/DcContext.java +++ b/src/com/b44t/messenger/DcContext.java @@ -183,9 +183,7 @@ public class DcContext { public native boolean setLocation (float latitude, float longitude, float accuracy); // helper to get/return strings from/to handleEvent() - public native static boolean data1IsString(int event); public native static boolean data2IsString(int event); - public native static String dataToString (long data); // working with raw c-data private long contextCPtr; // CAVE: the name is referenced in the JNI diff --git a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java index 4e686bdbe..17bc7e967 100644 --- a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java +++ b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java @@ -9,7 +9,6 @@ import android.net.NetworkInfo; import android.net.Uri; import android.os.Build; import android.os.Environment; -import android.os.PowerManager; import androidx.annotation.IntDef; import androidx.annotation.NonNull; import androidx.core.content.FileProvider; @@ -20,10 +19,11 @@ import android.webkit.MimeTypeMap; import android.widget.Toast; import com.b44t.messenger.DcChat; -import com.b44t.messenger.DcChatlist; import com.b44t.messenger.DcContact; import com.b44t.messenger.DcContext; +import com.b44t.messenger.DcEvent; import com.b44t.messenger.DcEventCenter; +import com.b44t.messenger.DcEventEmitter; import com.b44t.messenger.DcLot; import com.b44t.messenger.DcMsg; @@ -38,7 +38,6 @@ import org.thoughtcrime.securesms.util.Util; import java.io.File; import java.util.Date; import java.util.HashMap; -import java.util.Map; import java.util.Set; public class ApplicationDcContext extends DcContext { @@ -100,6 +99,18 @@ public class ApplicationDcContext extends DcContext { } // /migration + new Thread(() -> { + DcEventEmitter emitter = getEventEmitter(); + while (true) { + DcEvent event = emitter.getNextEvent(); + if (event==null) { + break; + } + handleEvent(event); + } + Log.i(TAG, "shutting down event handler"); + }, "eventThread").start(); + notificationCenter = new NotificationCenter(this); maybeStartIo(); } @@ -387,41 +398,41 @@ public class ApplicationDcContext extends DcContext { }); } - // TODO-ASYNC: make this work again - public long handleEvent(final int event, long data1, long data2) { - switch (event) { + public long handleEvent(DcEvent event) { + int id = event.getId(); + switch (id) { case DC_EVENT_INFO: - Log.i(TAG, dataToString(data2)); + Log.i(TAG, event.getData2Str()); break; case DC_EVENT_WARNING: - Log.w(TAG, dataToString(data2)); + Log.w(TAG, event.getData2Str()); break; case DC_EVENT_ERROR: - handleError(event, true, dataToString(data2)); + handleError(id, true, event.getData2Str()); break; case DC_EVENT_ERROR_NETWORK: - handleError(event, data1 != 0, dataToString(data2)); + handleError(id, event.getData1Int() != 0, event.getData2Str()); break; case DC_EVENT_ERROR_SELF_NOT_IN_GROUP: - handleError(event, true, dataToString(data2)); + handleError(id, true, event.getData2Str()); break; case DC_EVENT_INCOMING_MSG: - notificationCenter.addNotification((int) data1, (int) data2); + notificationCenter.addNotification(event.getData1Int(), event.getData2Int()); if (eventCenter != null) { - eventCenter.sendToObservers(event, data1, data2); // Other parts of the code are also interested in this event + eventCenter.sendToObservers(id, (long)event.getData1Int(), (long)event.getData2Int()); } break; default: { - final Object data1obj = data1IsString(event) ? dataToString(data1) : data1; - final Object data2obj = data2IsString(event) ? dataToString(data2) : data2; + final Object data1obj = (long)event.getData1Int(); + final Object data2obj = data2IsString(id) ? event.getData2Str() : (long)event.getData2Int(); if (eventCenter != null) { - eventCenter.sendToObservers(event, data1obj, data2obj); + eventCenter.sendToObservers(id, data1obj, data2obj); } } break; From 47f7ceb9864752fa3f4ca5ab1188f6dd1a80e0ef Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Tue, 26 May 2020 00:02:47 +0200 Subject: [PATCH 5/9] fix crash on context-unref due to typo this issue is older, however, by the explicit unref() call now more visible. but also in the past, this could have led to random crashes after account switching. --- jni/dc_wrapper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jni/dc_wrapper.c b/jni/dc_wrapper.c index 9a9f79ff0..0efe3380a 100644 --- a/jni/dc_wrapper.c +++ b/jni/dc_wrapper.c @@ -198,7 +198,7 @@ JNIEXPORT jlong Java_com_b44t_messenger_DcContext_createContextCPtr(JNIEnv *env, } -JNIEXPORT void Java_com_b44t_messenger_DcContact_unrefContextCPtr(JNIEnv *env, jobject obj) +JNIEXPORT void Java_com_b44t_messenger_DcContext_unrefContextCPtr(JNIEnv *env, jobject obj) { dc_context_unref(get_dc_context(env, obj)); } From a15a82c353604ad3d12f3788f16da3a2cd18ad4f Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Wed, 27 May 2020 21:47:07 +0200 Subject: [PATCH 6/9] stop io during configure(), call startIo()/stopIo() without is-running-check --- src/org/thoughtcrime/securesms/RegistrationActivity.java | 8 ++++++-- src/org/thoughtcrime/securesms/WelcomeActivity.java | 5 ++++- .../thoughtcrime/securesms/connect/AccountManager.java | 2 +- .../securesms/connect/ApplicationDcContext.java | 8 +------- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/org/thoughtcrime/securesms/RegistrationActivity.java b/src/org/thoughtcrime/securesms/RegistrationActivity.java index e7765b81d..c6971479b 100644 --- a/src/org/thoughtcrime/securesms/RegistrationActivity.java +++ b/src/org/thoughtcrime/securesms/RegistrationActivity.java @@ -511,8 +511,10 @@ public class RegistrationActivity extends BaseActionBarActivity implements DcEve // calling configure() results in // receiving multiple DC_EVENT_CONFIGURE_PROGRESS events - DcHelper.getContext(this).captureNextError(); - DcHelper.getContext(this).configure(); // TODO-ASYNC: make sure, io is stopped before and started afterwards + ApplicationDcContext dcContext = DcHelper.getContext(this); + dcContext.stopIo(); + dcContext.captureNextError(); + dcContext.configure(); } private void setConfig(@IdRes int viewId, String configTarget, boolean doTrim) { @@ -534,6 +536,7 @@ public class RegistrationActivity extends BaseActionBarActivity implements DcEve ApplicationDcContext dcContext = DcHelper.getContext(this); long progress = (Long)data1; if (progress==0/*error/aborted*/) { + dcContext.maybeStartIo(); // start-io is also needed on errors to make previous config work in case of changes dcContext.endCaptureNextError(); progressDialog.dismiss(); if (dcContext.hasCapturedError()) { @@ -555,6 +558,7 @@ public class RegistrationActivity extends BaseActionBarActivity implements DcEve progressDialog.setMessage(getResources().getString(R.string.one_moment)+String.format(" %d%%", percent)); } else if (progress==1000/*done*/) { + dcContext.maybeStartIo(); dcContext.endCaptureNextError(); progressDialog.dismiss(); Intent conversationList = new Intent(getApplicationContext(), ConversationListActivity.class); diff --git a/src/org/thoughtcrime/securesms/WelcomeActivity.java b/src/org/thoughtcrime/securesms/WelcomeActivity.java index 17a563211..589c324b7 100644 --- a/src/org/thoughtcrime/securesms/WelcomeActivity.java +++ b/src/org/thoughtcrime/securesms/WelcomeActivity.java @@ -147,7 +147,8 @@ public class WelcomeActivity extends BaseActionBarActivity implements DcEventCen // calling configure() results in // receiving multiple DC_EVENT_CONFIGURE_PROGRESS events - dcContext.configure(); // TODO-ASYNC: make sure, io is stopped before and started afterwards + dcContext.stopIo(); + dcContext.configure(); } private void progressError() { @@ -192,12 +193,14 @@ public class WelcomeActivity extends BaseActionBarActivity implements DcEventCen progressUpdate((int)progress); } else if (progress==1000/*done*/) { + dcContext.maybeStartIo(); progressSuccess(false); } } else if (manualConfigure && eventId==DcContext.DC_EVENT_CONFIGURE_PROGRESS) { long progress = (Long)data1; if (progress==1000/*done*/) { + dcContext.maybeStartIo(); finish(); // remove ourself from the activity stack (finishAffinity is available in API 16, we're targeting API 14) } } diff --git a/src/org/thoughtcrime/securesms/connect/AccountManager.java b/src/org/thoughtcrime/securesms/connect/AccountManager.java index eeb928606..c28860468 100644 --- a/src/org/thoughtcrime/securesms/connect/AccountManager.java +++ b/src/org/thoughtcrime/securesms/connect/AccountManager.java @@ -99,7 +99,7 @@ public class AccountManager { // getSelectedAccount() ApplicationContext appContext = (ApplicationContext)context.getApplicationContext(); appContext.dcContext.notificationCenter.removeAllNotifiations(); - appContext.dcContext.maybeStopIo(); + appContext.dcContext.stopIo(); appContext.dcContext.unref(); appContext.dcContext = new ApplicationDcContext(context); } diff --git a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java index 17bc7e967..f0096898c 100644 --- a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java +++ b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java @@ -116,17 +116,11 @@ public class ApplicationDcContext extends DcContext { } public void maybeStartIo() { - if (!isIoRunning()) { + if (isConfigured()!=0) { startIo(); } } - public void maybeStopIo() { - if (isIoRunning()) { - stopIo(); - } - } - public void setStockTranslations() { // the integers are defined in the core and used only here, an enum or sth. like that won't have a big benefit setStockTranslation(1, context.getString(R.string.chat_no_messages)); From 3bbe5ce671cc66f1c7e042e887221ad73bd420ed Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Wed, 27 May 2020 21:47:55 +0200 Subject: [PATCH 7/9] bump version --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 55564b7ae..68dbc8ba7 100644 --- a/build.gradle +++ b/build.gradle @@ -90,7 +90,7 @@ android { defaultConfig { versionCode 582 - versionName "1.8.1" + versionName "1.9.2" applicationId "com.b44t.messenger" multiDexEnabled true From 4423d47f442e76285db55dd945a6753f51eff11f Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Wed, 27 May 2020 23:30:39 +0200 Subject: [PATCH 8/9] give the background worker 60 seconds to finish its work --- .../securesms/connect/FetchWorker.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/org/thoughtcrime/securesms/connect/FetchWorker.java b/src/org/thoughtcrime/securesms/connect/FetchWorker.java index d9eb546ec..78b4850ba 100644 --- a/src/org/thoughtcrime/securesms/connect/FetchWorker.java +++ b/src/org/thoughtcrime/securesms/connect/FetchWorker.java @@ -7,6 +7,8 @@ import androidx.annotation.NonNull; import androidx.work.Worker; import androidx.work.WorkerParameters; +import org.thoughtcrime.securesms.util.Util; + public class FetchWorker extends Worker { private @NonNull Context context; @@ -17,16 +19,24 @@ public class FetchWorker extends Worker { this.context = context; } + // doWork() is called in a background thread; + // once we return, Worker is considered to have finished and will be destroyed, + // this does not necessarily mean, that the app is killed, we may or may not keep running, + // therefore we do not stopIo() here. @Override public @NonNull Result doWork() { Log.i("DeltaChat", "-------------------- FetchWorker.doWork() started --------------------"); ApplicationDcContext dcContext = DcHelper.getContext(context); dcContext.maybeStartIo(); + + // as we do not know when startIo() has done it's work or if is even doable in one step, + // we go the easy way and just wait for some amount of time. + // the core has to handle interrupts at any point anyway, + // and work also maybe continued when doWork() returns. + // however, we should not wait too long here to avoid getting bad battery ratings. + Util.sleep(60 * 1000); + Log.i("DeltaChat", "-------------------- FetchWorker.doWork() done --------------------"); - - // TODO-ASYNC: check, that the threads get some minimal amount of time and the app is not terminated - // directly after returning - - return Result.success(); // when returning, the os may terminate the app again + return Result.success(); } } From 729556acc0239ff059f7e4c4d6e82fcdfe0404ac Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Thu, 28 May 2020 13:52:28 +0200 Subject: [PATCH 9/9] make sure, getNextEvent() returns null when done, fix typos, thanks @cyBerta --- src/com/b44t/messenger/DcEvent.java | 2 +- src/com/b44t/messenger/DcEventEmitter.java | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/com/b44t/messenger/DcEvent.java b/src/com/b44t/messenger/DcEvent.java index 965ad3aec..34355be44 100644 --- a/src/com/b44t/messenger/DcEvent.java +++ b/src/com/b44t/messenger/DcEvent.java @@ -8,7 +8,7 @@ public class DcEvent { @Override protected void finalize() throws Throwable { super.finalize(); - unrefEventCPtr();; + unrefEventCPtr(); eventCPtr = 0; } diff --git a/src/com/b44t/messenger/DcEventEmitter.java b/src/com/b44t/messenger/DcEventEmitter.java index 217b04bde..bd7c8ad54 100644 --- a/src/com/b44t/messenger/DcEventEmitter.java +++ b/src/com/b44t/messenger/DcEventEmitter.java @@ -8,11 +8,14 @@ public class DcEventEmitter { @Override protected void finalize() throws Throwable { super.finalize(); - unrefEventEmitterCPtr();; + unrefEventEmitterCPtr(); eventEmitterCPtr = 0; } - public DcEvent getNextEvent () { return new DcEvent(getNextEventCPtr()); } + public DcEvent getNextEvent () { + long eventCPtr = getNextEventCPtr(); + return eventCPtr == 0 ? null : new DcEvent(eventCPtr); + } // working with raw c-data private long eventEmitterCPtr; // CAVE: the name is referenced in the JNI