Files
FairEmail/app/src/main/java/eu/faircode/email/JobDaily.java

157 lines
6.0 KiB
Java
Raw Normal View History

2018-09-04 07:02:54 +00:00
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.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-09-04 07:02:54 +00:00
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
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-09-04 07:02:54 +00:00
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;
2018-11-26 17:04:32 +01:00
import java.util.Calendar;
2018-09-04 07:02:54 +00:00
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);
private static final long CLEANUP_INTERVAL = 4 * 3600 * 1000L; // milliseconds
private static final long CACHE_IMAGE_DURATION = 3 * 24 * 3600 * 1000L;
2018-09-04 07:02:54 +00:00
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(CLEANUP_INTERVAL)
2018-09-04 07:02:54 +00:00
.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, "Scheduling daily job failed");
2018-09-04 07:02:54 +00:00
}
@Override
public boolean onStartJob(JobParameters args) {
2018-09-05 19:02:54 +00:00
EntityLog.log(this, "Daily cleanup");
2018-09-04 07:02:54 +00:00
executor.submit(new Runnable() {
@Override
public void run() {
2018-11-26 18:05:01 +01:00
cleanup(getApplicationContext());
2018-09-04 07:02:54 +00:00
}
});
return false;
}
2018-11-26 18:05:01 +01:00
static void cleanup(Context context) {
DB db = DB.getInstance(context);
try {
db.beginTransaction();
Log.i(Helper.TAG, "Start daily job");
// Cleanup folders
Log.i(Helper.TAG, "Cleanup folders");
for (EntityFolder folder : db.folder().getFolders()) {
Calendar cal_keep = Calendar.getInstance();
cal_keep.add(Calendar.DAY_OF_MONTH, -folder.keep_days);
cal_keep.set(Calendar.HOUR_OF_DAY, 12);
2018-11-26 18:05:01 +01:00
cal_keep.set(Calendar.MINUTE, 0);
cal_keep.set(Calendar.SECOND, 0);
cal_keep.set(Calendar.MILLISECOND, 0);
long keep_time = cal_keep.getTimeInMillis();
if (keep_time < 0)
keep_time = 0;
int messages = db.message().deleteMessagesBefore(folder.id, keep_time, false);
Log.i(Helper.TAG, "Cleanup folder=" + folder.account + ":" + folder.name +
" before=" + new Date(keep_time) + " deleted=" + messages);
}
// Cleanup message files
Log.i(Helper.TAG, "Cleanup message files");
File[] messages = new File(context.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(context.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);
}
}
// Cleanup cached images
Log.i(Helper.TAG, "Cleanup cached image files");
long now = new Date().getTime();
File[] images = new File(context.getCacheDir(), "images").listFiles();
if (images != null)
for (File file : images)
if (file.isFile() && file.lastModified() + CACHE_IMAGE_DURATION < now) {
Log.i(Helper.TAG, "Deleting cached image=" + file.getName());
if (!file.delete())
Log.w(Helper.TAG, "Error deleting " + file);
}
2018-11-26 18:05:01 +01:00
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);
db.setTransactionSuccessful();
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
} finally {
db.endTransaction();
Log.i(Helper.TAG, "End daily job");
}
}
2018-09-04 07:02:54 +00:00
@Override
public boolean onStopJob(JobParameters args) {
return false;
}
}