adapt to new smtp-thread-API

This commit is contained in:
B. Petersen
2018-06-13 00:40:10 +02:00
parent 631959ba63
commit e7de3e059d
7 changed files with 108 additions and 58 deletions
+18
View File
@@ -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)
@@ -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();
}
}
}
@@ -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) {
@@ -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);
@@ -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();
}
@@ -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()
@@ -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();