diff --git a/MessengerProj/jni/mrwrapper.c b/MessengerProj/jni/mrwrapper.c index 6cd5ffddc..a7d9180c3 100644 --- a/MessengerProj/jni/mrwrapper.c +++ b/MessengerProj/jni/mrwrapper.c @@ -277,6 +277,24 @@ JNIEXPORT void Java_com_b44t_messenger_MrMailbox_fetch(JNIEnv *env, jclass cls) } +JNIEXPORT void Java_com_b44t_messenger_MrMailbox_performSmtpJobs(JNIEnv *env, jclass cls) +{ + mrmailbox_perform_smtp_jobs(get_mrmailbox_t(env, cls)); +} + + +JNIEXPORT void Java_com_b44t_messenger_MrMailbox_performSmtpIdle(JNIEnv *env, jclass cls) +{ + mrmailbox_perform_smtp_idle(get_mrmailbox_t(env, cls)); +} + + +JNIEXPORT void Java_com_b44t_messenger_MrMailbox_interruptSmtpIdle(JNIEnv *env, jclass cls) +{ + mrmailbox_interrupt_smtp_idle(get_mrmailbox_t(env, cls)); +} + + /* MrMailbox - handle contacts */ JNIEXPORT jintArray Java_com_b44t_messenger_MrMailbox_getContacts(JNIEnv *env, jclass cls, jint flags, jstring query) diff --git a/MessengerProj/src/main/java/com/b44t/messenger/ApplicationLoader.java b/MessengerProj/src/main/java/com/b44t/messenger/ApplicationLoader.java index cf5036292..0438e5204 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/ApplicationLoader.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/ApplicationLoader.java @@ -149,6 +149,9 @@ public class ApplicationLoader extends Application { 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 + 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 + convertVideoWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "convertVideoWakeLock" /*any name*/); convertVideoWakeLock.setReferenceCounted(false); @@ -214,7 +217,7 @@ public class ApplicationLoader extends Application { MrMailbox.open(dbfile.getAbsolutePath()); if( MrMailbox.isConfigured()!=0 && ApplicationLoader.getPermanentPush() ) { //ApplicationLoader.imapForeground = true; - ApplicationLoader.startImapThread(); + ApplicationLoader.startThreads(); } // create other default objects @@ -286,73 +289,100 @@ public class ApplicationLoader extends Application { } } - private static final Object imapThreadCritical = new Object(); + private static final Object threadsCritical = new Object(); private static boolean imapThreadStartedVal; private static final Object imapThreadStartedCond = new Object(); - public static Thread imapThread = null; private static PowerManager.WakeLock imapWakeLock = null; + + private static boolean smtpThreadStartedVal; + private static final Object smtpThreadStartedCond = new Object(); + public static Thread smtpThread = null; + private static PowerManager.WakeLock smtpWakeLock = null; + //public static boolean imapForeground = false; - public static void startImapThread() + public static void startThreads() { - synchronized(imapThreadCritical) { + synchronized(threadsCritical) { - if( imapThread != null && imapThread.isAlive() ) { - Log.i("DeltaChat", "IMAP-Thread is already active, doing nothing."); - return; - } + if (imapThread == null || !imapThread.isAlive()) { - synchronized (imapThreadStartedCond) { - imapThreadStartedVal = false; - } - - imapThread = new Thread(new Runnable() { - @Override - public void run() { - - // raise the starting condition - // after acquiring a wakelock so that the process is not terminated. - // as imapWakeLock is not reference counted that would result in a wakelock-gap is not needed here. - imapWakeLock.acquire(); - synchronized (imapThreadStartedCond) { - imapThreadStartedVal = true; - imapThreadStartedCond.notifyAll(); - } - - Log.i("DeltaChat", "###################### IMAP-Thread started. ######################"); - - - while( true ) { - imapWakeLock.acquire(); - MrMailbox.performJobs(); - MrMailbox.fetch(); - imapWakeLock.release(); - - //if( imapForeground ) { - MrMailbox.idle(); - /*} - else { - break; - }*/ - } - - - //Log.i("DeltaChat", "IMAP-Thread stopped."); + synchronized (imapThreadStartedCond) { + imapThreadStartedVal = false; } - }, "imapThread"); - imapThread.start(); + imapThread = new Thread(new Runnable() { + @Override + public void run() { + // raise the starting condition + // after acquiring a wakelock so that the process is not terminated. + // as imapWakeLock is not reference counted that would result in a wakelock-gap is not needed here. + imapWakeLock.acquire(); + synchronized (imapThreadStartedCond) { + imapThreadStartedVal = true; + imapThreadStartedCond.notifyAll(); + } + + Log.i("DeltaChat", "###################### IMAP-Thread started. ######################"); + + + while (true) { + imapWakeLock.acquire(); + MrMailbox.performJobs(); + MrMailbox.fetch(); + imapWakeLock.release(); + MrMailbox.idle(); + } + } + }, "imapThread"); + imapThread.start(); + } + + if (smtpThread == null || !smtpThread.isAlive()) { + + synchronized (smtpThreadStartedCond) { + smtpThreadStartedVal = false; + } + + smtpThread = new Thread(new Runnable() { + @Override + public void run() { + smtpWakeLock.acquire(); + synchronized (smtpThreadStartedCond) { + smtpThreadStartedVal = true; + smtpThreadStartedCond.notifyAll(); + } + + Log.i("DeltaChat", "###################### SMTP-Thread started. ######################"); + + + while (true) { + smtpWakeLock.acquire(); + MrMailbox.performSmtpJobs(); + smtpWakeLock.release(); + MrMailbox.performSmtpIdle(); + } + } + }, "smtpThread"); + smtpThread.start(); + } } } - public static void waitForImapThreadRunning() + public static void waitForThreadsRunning() { try { - synchronized( imapThreadStartedCond ) { // `synchronized` locks the object - while( !imapThreadStartedVal ) { // the doc says, spurious wakeups are possible, wait() should always be used in a loop - imapThreadStartedCond.wait(); // `wait()` unlocks the object, waits and locks again + synchronized( imapThreadStartedCond ) { + while( !imapThreadStartedVal ) { + imapThreadStartedCond.wait(); + } + } + + synchronized( smtpThreadStartedCond ) { + while( !smtpThreadStartedVal ) { + smtpThreadStartedCond.wait(); } } } diff --git a/MessengerProj/src/main/java/com/b44t/messenger/Components/ForegroundDetector.java b/MessengerProj/src/main/java/com/b44t/messenger/Components/ForegroundDetector.java index 39e11ee98..092cb85dd 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/Components/ForegroundDetector.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/Components/ForegroundDetector.java @@ -70,7 +70,7 @@ public class ForegroundDetector implements Application.ActivityLifecycleCallback } //ApplicationLoader.imapForeground = true; - ApplicationLoader.startImapThread(); // we call this without checking getPermanentPush() to have a simple guarantee that push is always active when the app is in foregroud (startIdleThread makes sure the thread is not started twice) + ApplicationLoader.startThreads(); // we call this without checking getPermanentPush() to have a simple guarantee that push is always active when the app is in foregroud (startIdleThread makes sure the thread is not started twice) } public boolean isWasInBackground(boolean reset) { diff --git a/MessengerProj/src/main/java/com/b44t/messenger/MrMailbox.java b/MessengerProj/src/main/java/com/b44t/messenger/MrMailbox.java index 42bd9568b..83b471ec9 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/MrMailbox.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/MrMailbox.java @@ -61,6 +61,10 @@ public class MrMailbox { public native static void idle(); public native static void interruptIdle(); + public native static void performSmtpJobs(); + public native static void performSmtpIdle(); + public native static void interruptSmtpIdle(); + public native static void setConfig(String key, String value); public native static void setConfigInt(String key, int value); public native static String getConfig(String key, String def); diff --git a/MessengerProj/src/main/java/com/b44t/messenger/SettingsAccountFragment.java b/MessengerProj/src/main/java/com/b44t/messenger/SettingsAccountFragment.java index ac377b86b..0d285820e 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/SettingsAccountFragment.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/SettingsAccountFragment.java @@ -355,7 +355,7 @@ public class SettingsAccountFragment extends BaseFragment implements Notificatio } // try to connect, this results in an MR_EVENT_CONFIGURE_ENDED resp. NotificationCenter.configureEnded event - ApplicationLoader.startImapThread(); + ApplicationLoader.startThreads(); MrMailbox.configure(); } diff --git a/MessengerProj/src/main/java/com/b44t/messenger/TimerReceiver.java b/MessengerProj/src/main/java/com/b44t/messenger/TimerReceiver.java index 6a7006edb..a39995af7 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/TimerReceiver.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/TimerReceiver.java @@ -31,8 +31,6 @@ import android.content.Intent; import android.os.Build; import android.util.Log; -import com.coremedia.iso.boxes.apple.AppleItemListBox; - public class TimerReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { @@ -41,8 +39,8 @@ public class TimerReceiver extends BroadcastReceiver { scheduleNextAlarm(); - ApplicationLoader.startImapThread(); - ApplicationLoader.waitForImapThreadRunning(); + ApplicationLoader.startThreads(); + ApplicationLoader.waitForThreadsRunning(); } public static void scheduleNextAlarm() diff --git a/MessengerProj/src/main/java/com/b44t/messenger/WelcomeActivity.java b/MessengerProj/src/main/java/com/b44t/messenger/WelcomeActivity.java index b1050d4e2..d1e643fd7 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/WelcomeActivity.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/WelcomeActivity.java @@ -383,7 +383,7 @@ public class WelcomeActivity extends Activity implements NotificationCenter.Noti if( (int)args[0]==1 ) { //ApplicationLoader.imapForeground = true; - ApplicationLoader.startImapThread(); // import does not automatically connect + ApplicationLoader.startThreads(); // import does not automatically connect KeepAliveService kas = KeepAliveService.getInstance(); if( kas != null ) { kas.updateForegroundNotification();