Browse Source

Added daily cleanup job

main
M66B 6 years ago
parent
commit
e8da25a674
9 changed files with 159 additions and 39 deletions
  1. BIN
      .idea/caches/build_file_checksums.ser
  2. +5
    -0
      app/src/main/AndroidManifest.xml
  3. +20
    -1
      app/src/main/java/eu/faircode/email/BoundaryCallbackMessages.java
  4. +4
    -0
      app/src/main/java/eu/faircode/email/DaoLog.java
  5. +1
    -1
      app/src/main/java/eu/faircode/email/FragmentMessage.java
  6. +15
    -0
      app/src/main/java/eu/faircode/email/Helper.java
  7. +109
    -0
      app/src/main/java/eu/faircode/email/JobDaily.java
  8. +4
    -25
      app/src/main/java/eu/faircode/email/ServiceSynchronize.java
  9. +1
    -12
      app/src/main/java/eu/faircode/email/SimpleTask.java

BIN
.idea/caches/build_file_checksums.ser View File


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

@ -73,6 +73,11 @@
<service android:name=".ServiceSynchronize" />
<service
android:name=".JobDaily"
android:exported="true"
android:permission="android.permission.BIND_JOB_SERVICE" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}"


+ 20
- 1
app/src/main/java/eu/faircode/email/BoundaryCallbackMessages.java View File

@ -1,5 +1,24 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.os.Handler;
import android.util.Log;
@ -35,7 +54,7 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
private String search;
private Handler mainHandler;
private IBoundaryCallbackMessages intf;
private ExecutorService executor = Executors.newSingleThreadExecutor();
private ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
private boolean enabled = false;
private IMAPStore istore = null;


+ 4
- 0
app/src/main/java/eu/faircode/email/DaoLog.java View File

@ -40,4 +40,8 @@ public interface DaoLog {
@Insert
long insertLog(EntityLog log);
@Query("DELETE FROM log" +
" WHERE time < :before")
int deleteLogs(long before);
}

+ 1
- 1
app/src/main/java/eu/faircode/email/FragmentMessage.java View File

@ -145,7 +145,7 @@ public class FragmentMessage extends FragmentEx {
private boolean debug;
private DateFormat df = SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
private ExecutorService executor = Executors.newCachedThreadPool();
private ExecutorService executor = Executors.newCachedThreadPool(Helper.backgroundThreadFactory);
private static final long CACHE_IMAGE_DURATION = 3 * 24 * 3600 * 1000L;
static final String ACTION_DECRYPT_MESSAGE = BuildConfig.APPLICATION_ID + ".DECRYPT_MESSAGE";


+ 15
- 0
app/src/main/java/eu/faircode/email/Helper.java View File

@ -23,6 +23,7 @@ import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
@ -47,18 +48,32 @@ import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ThreadFactory;
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
public class Helper {
static final String TAG = "fairemail";
static final int JOB_DAILY = 1001;
static final int AUTH_TYPE_PASSWORD = 1;
static final int AUTH_TYPE_GMAIL = 2;
static final int ATTACHMENT_BUFFER_SIZE = 8192; // bytes
static ThreadFactory backgroundThreadFactory = new ThreadFactory() {
@Override
public Thread newThread(@NonNull Runnable runnable) {
Thread thread = new Thread(runnable);
thread.setPriority(THREAD_PRIORITY_BACKGROUND);
return thread;
}
};
static int resolveColor(Context context, int attr) {
int[] attrs = new int[]{attr};
TypedArray a = context.getTheme().obtainStyledAttributes(attrs);


+ 109
- 0
app/src/main/java/eu/faircode/email/JobDaily.java View File

@ -0,0 +1,109 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.util.Log;
import java.io.File;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class JobDaily extends JobService {
private ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
public static void schedule(Context context) {
Log.i(Helper.TAG, "Scheduling daily job");
JobInfo.Builder job = new JobInfo.Builder(Helper.JOB_DAILY, new ComponentName(context, JobDaily.class))
.setPeriodic(24 * 3600 * 1000L)
.setRequiresDeviceIdle(true);
JobScheduler scheduler = context.getSystemService(JobScheduler.class);
scheduler.cancel(Helper.JOB_DAILY);
if (scheduler.schedule(job.build()) == JobScheduler.RESULT_SUCCESS)
Log.i(Helper.TAG, "Scheduled daily job");
else
Log.e(Helper.TAG, "Failed to schedule daily job");
}
@Override
public boolean onStartJob(JobParameters args) {
Log.i(Helper.TAG, "Starting daily job");
final DB db = DB.getInstance(this);
executor.submit(new Runnable() {
@Override
public void run() {
Log.i(Helper.TAG, "Start daily job");
// Cleanup message files
Log.i(Helper.TAG, "Cleanup message files");
File[] messages = new File(getFilesDir(), "messages").listFiles();
if (messages != null)
for (File file : messages)
if (file.isFile()) {
long id = Long.parseLong(file.getName());
if (db.message().countMessage(id) == 0) {
Log.i(Helper.TAG, "Cleanup message id=" + id);
if (!file.delete())
Log.w(Helper.TAG, "Error deleting " + file);
}
}
// Cleanup attachment files
Log.i(Helper.TAG, "Cleanup attachment files");
File[] attachments = new File(getFilesDir(), "attachments").listFiles();
if (attachments != null)
for (File file : attachments)
if (file.isFile()) {
long id = Long.parseLong(file.getName());
if (db.attachment().countAttachment(id) == 0) {
Log.i(Helper.TAG, "Cleanup attachment id=" + id);
if (!file.delete())
Log.w(Helper.TAG, "Error deleting " + file);
}
}
Log.i(Helper.TAG, "Cleanup log");
long before = new Date().getTime() - 24 * 3600 * 1000L;
int logs = db.log().deleteLogs(before);
Log.i(Helper.TAG, "Deleted logs=" + logs);
Log.i(Helper.TAG, "End daily job");
}
});
return false;
}
@Override
public boolean onStopJob(JobParameters args) {
return false;
}
}

+ 4
- 25
app/src/main/java/eu/faircode/email/ServiceSynchronize.java View File

@ -386,7 +386,7 @@ public class ServiceSynchronize extends LifecycleService {
private void monitorAccount(final EntityAccount account, final ServiceState state) throws NoSuchProviderException {
final DB db = DB.getInstance(this);
final ExecutorService executor = Executors.newSingleThreadExecutor();
final ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
int backoff = CONNECT_BACKOFF_START;
while (state.running) {
@ -1270,28 +1270,6 @@ public class ServiceSynchronize extends LifecycleService {
Log.w(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex));
}
// Cleanup files
File[] messages = new File(getFilesDir(), "messages").listFiles();
if (messages != null)
for (File file : messages)
if (file.isFile()) {
long id = Long.parseLong(file.getName());
if (db.message().countMessage(id) == 0) {
Log.i(Helper.TAG, "Cleanup message id=" + id);
file.delete();
}
}
File[] attachments = new File(getFilesDir(), "attachments").listFiles();
if (attachments != null)
for (File file : attachments)
if (file.isFile()) {
long id = Long.parseLong(file.getName());
if (db.attachment().countAttachment(id) == 0) {
Log.i(Helper.TAG, "Cleanup attachment id=" + id);
file.delete();
}
}
Log.w(Helper.TAG, folder.name + " statistics added=" + added + " updated=" + updated + " unchanged=" + unchanged);
} finally {
Log.v(Helper.TAG, folder.name + " end sync");
@ -1439,8 +1417,8 @@ public class ServiceSynchronize extends LifecycleService {
private boolean running = false;
private Thread main;
private EntityFolder outbox = null;
private ExecutorService lifecycle = Executors.newSingleThreadExecutor();
private ExecutorService executor = Executors.newSingleThreadExecutor();
private ExecutorService lifecycle = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
private ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
@Override
public void onAvailable(Network network) {
@ -1648,6 +1626,7 @@ public class ServiceSynchronize extends LifecycleService {
public static void start(Context context) {
ContextCompat.startForegroundService(context, new Intent(context, ServiceSynchronize.class));
JobDaily.schedule(context);
}
public static void reload(Context context, String reason) {


+ 1
- 12
app/src/main/java/eu/faircode/email/SimpleTask.java View File

@ -22,12 +22,10 @@ package eu.faircode.email;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.util.Log;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
@ -37,8 +35,6 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleService;
import androidx.lifecycle.OnLifecycleEvent;
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
//
// This simple task is simple to use, but it is also simple to cause bugs that can easily lead to crashes
// Make sure to not access any member in any outer scope from onLoad
@ -51,14 +47,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
private Bundle args = null;
private Result stored = null;
private ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() {
@Override
public Thread newThread(@NonNull Runnable runnable) {
Thread thread = new Thread(runnable);
thread.setPriority(THREAD_PRIORITY_BACKGROUND);
return thread;
}
});
private ExecutorService executor = Executors.newCachedThreadPool(Helper.backgroundThreadFactory);
public void load(Context context, LifecycleOwner owner, Bundle args) {
run(context, owner, args);


Loading…
Cancel
Save