Browse Source

Use timers

main
M66B 6 years ago
parent
commit
b19f496ce0
3 changed files with 63 additions and 13 deletions
  1. +6
    -5
      FAQ.md
  2. +1
    -0
      app/src/main/AndroidManifest.xml
  3. +56
    -8
      app/src/main/java/eu/faircode/email/ServiceSynchronize.java

+ 6
- 5
FAQ.md View File

@ -8,11 +8,12 @@ At the bottom you can find how to ask other questions, request features and repo
<a name="FAQ1"></a>
**(1) Which permissions are needed and why?**
* Full network access (INTERNET): to send and receive email
* View network connections (ACCESS_NETWORK_STATE): to monitor internet connectivity changes
* Run at startup (RECEIVE_BOOT_COMPLETED): to start monitoring on device start
* In-app billing (BILLING): to allow in-app purchases
* Foreground service (FOREGROUND_SERVICE): to run a foreground service on Android 9 Pie and later, see also the next question
* have full network access (INTERNET): to send and receive email
* view network connections (ACCESS_NETWORK_STATE): to monitor internet connectivity changes
* run at startup (RECEIVE_BOOT_COMPLETED): to start monitoring on device start
* in-app billing (BILLING): to allow in-app purchases
* foreground service (FOREGROUND_SERVICE): to run a foreground service on Android 9 Pie and later, see also the next question
* prevent device from sleeping (WAKE_LOCK): to keep the device awake while synchronizing messages
* Optional: read your contacts (READ_CONTACTS): to autocomplete addresses and to show photos
* Optional: find accounts on the device (GET_ACCOUNTS): to use [OAuth](https://en.wikipedia.org/wiki/OAuth) instead of passwords


+ 1
- 0
app/src/main/AndroidManifest.xml View File

@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.vending.BILLING" />
<application


+ 56
- 8
app/src/main/java/eu/faircode/email/ServiceSynchronize.java View File

@ -42,6 +42,7 @@ import android.net.NetworkRequest;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
@ -77,6 +78,9 @@ import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.mail.Address;
import javax.mail.AuthenticationFailedException;
@ -796,14 +800,42 @@ public class ServiceSynchronize extends LifecycleService {
if (!capIdle) {
Log.i(Helper.TAG, folder.name + " start polling");
PowerManager pm = getSystemService(PowerManager.class);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, account.name + "/" + folder.name);
final Thread pthread = Thread.currentThread();
int rate = (folder.poll_interval == null ? 9 : folder.poll_interval);
ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1);
ScheduledFuture future = scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
Log.i(Helper.TAG, folder.name + " wakeup poll");
pthread.interrupt();
}
}, rate, rate, TimeUnit.MINUTES);
while (state.running) {
try {
Thread.sleep((folder.poll_interval == null ? 9 : folder.poll_interval) * 60 * 1000L);
synchronizeMessages(account, folder, ifolder, state);
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException ex) {
Log.w(Helper.TAG, folder.name + " poll " + ex.toString());
}
try {
wl.acquire();
synchronizeMessages(account, folder, ifolder, state);
} catch (Throwable ex) {
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
reportError(account.name, folder.name, ex);
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
} finally {
wl.release();
}
}
future.cancel(false);
}
} catch (Throwable ex) {
Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
@ -933,28 +965,44 @@ public class ServiceSynchronize extends LifecycleService {
lbm.registerReceiver(processFolder, f);
try {
PowerManager pm = getSystemService(PowerManager.class);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, account.name);
ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1);
ScheduledFuture future = scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
Log.i(Helper.TAG, account.name + " wakeup check");
state.thread.interrupt();
}
}, account.poll_interval, account.poll_interval, TimeUnit.MINUTES);
// Keep store alive
while (state.running) {
EntityLog.log(this, account.name + " wait=" + account.poll_interval);
try {
Thread.sleep(account.poll_interval * 60 * 1000L);
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException ex) {
Log.w(Helper.TAG, account.name + " wait " + ex.toString());
}
if (state.running) {
EntityLog.log(this, account.name + " checking store");
if (state.running) try {
wl.acquire();
if (!istore.isConnected())
throw new StoreClosedException(istore);
for (EntityFolder folder : folders.keySet()) {
EntityLog.log(this, account.name + " checking " + folder.name);
for (EntityFolder folder : folders.keySet())
if (!folders.get(folder).isOpen())
throw new FolderClosedException(folders.get(folder));
}
} finally {
wl.release();
}
}
future.cancel(false);
Log.i(Helper.TAG, account.name + " done running=" + state.running);
} finally {
lbm.unregisterReceiver(processFolder);


Loading…
Cancel
Save