Initially sync 7 days, default sync 1 day

This commit is contained in:
M66B 2018-12-11 12:18:26 +01:00
parent f8b9640ca1
commit afe3832364
10 changed files with 1285 additions and 27 deletions

File diff suppressed because it is too large Load Diff

View File

@ -23,6 +23,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
@ -184,6 +185,10 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
tvAfter.setText(String.format("%d/%d", folder.sync_days, folder.keep_days));
ivSync.setImageResource(folder.synchronize ? R.drawable.baseline_sync_24 : R.drawable.baseline_sync_disabled_24);
}
ivSync.setImageTintList(ColorStateList.valueOf(
Helper.resolveColor(context,
folder.synchronize && folder.initialize && !EntityFolder.OUTBOX.equals(folder.type)
? R.attr.colorUnread : android.R.attr.textColorSecondary)));
tvKeywords.setText(TextUtils.join(" ", folder.keywords));
tvKeywords.setVisibility(debug && folder.keywords.length > 0 ? View.VISIBLE : View.GONE);

View File

@ -46,7 +46,7 @@ import io.requery.android.database.sqlite.RequerySQLiteOpenHelperFactory;
// https://developer.android.com/topic/libraries/architecture/room.html
@Database(
version = 21,
version = 22,
entities = {
EntityIdentity.class,
EntityAccount.class,
@ -292,6 +292,14 @@ public abstract class DB extends RoomDatabase {
db.execSQL("ALTER TABLE `identity` ADD COLUMN `bcc` TEXT");
}
})
.addMigrations(new Migration(21, 22) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `folder` ADD COLUMN `initialize` INTEGER NOT NULL DEFAULT 1");
db.execSQL("UPDATE `folder` SET sync_days = 1");
}
})
.build();
}

View File

@ -175,6 +175,9 @@ public interface DaoFolder {
@Query("UPDATE folder SET name = :name WHERE account = :account AND name = :old")
int renameFolder(long account, String old, String name);
@Query("UPDATE folder SET initialize = 0 WHERE id = :id")
int setFolderInitialized(long id);
@Query("UPDATE folder SET tbd = 1 WHERE id = :id")
int setFolderTbd(long id);

View File

@ -74,6 +74,8 @@ public class EntityFolder implements Serializable {
public Integer sync_days;
@NonNull
public Integer keep_days;
@NonNull
public Boolean initialize = true;
public String display;
@NonNull
public Boolean hide = false;
@ -128,9 +130,9 @@ public class EntityFolder implements Serializable {
USER
);
static final int DEFAULT_INBOX_SYNC = 7; // days
static final int DEFAULT_SYSTEM_SYNC = 7; // days
static final int DEFAULT_USER_SYNC = 7; // days
static final int DEFAULT_INIT = 7; // days
static final int DEFAULT_SYNC = 1; // days
static final int DEFAULT_KEEP = 14; // days
static final List<String> SYSTEM_FOLDER_SYNC = Arrays.asList(
DRAFTS,

View File

@ -92,17 +92,22 @@ public class EntityOperation {
queue(db, message, name, jargs);
}
static void sync(DB db, long folder) {
if (db.operation().getOperationCount(folder, EntityOperation.SYNC) == 0) {
static void sync(DB db, long fid) {
if (db.operation().getOperationCount(fid, EntityOperation.SYNC) == 0) {
EntityFolder folder = db.folder().getFolder(fid);
JSONArray jargs = new JSONArray();
jargs.put(folder.initialize ? Math.min(EntityFolder.DEFAULT_INIT, folder.keep_days) : folder.sync_days);
jargs.put(folder.keep_days);
EntityOperation operation = new EntityOperation();
operation.folder = folder;
operation.folder = folder.id;
operation.message = null;
operation.name = SYNC;
operation.args = new JSONArray().toString();
operation.args = jargs.toString();
operation.created = new Date().getTime();
operation.id = db.operation().insertOperation(operation);
db.folder().setFolderSyncState(folder, "requested");
db.folder().setFolderSyncState(fid, "requested");
Log.i(Helper.TAG, "Queued sync folder=" + folder);
}

View File

@ -515,8 +515,8 @@ public class FragmentAccount extends FragmentEx {
folder.name = ifolder.getFullName();
folder.type = (type == null ? EntityFolder.USER : type);
folder.synchronize = (type != null && EntityFolder.SYSTEM_FOLDER_SYNC.contains(type));
folder.sync_days = (type == null ? EntityFolder.DEFAULT_USER_SYNC : EntityFolder.DEFAULT_SYSTEM_SYNC);
folder.keep_days = folder.sync_days;
folder.sync_days = EntityFolder.DEFAULT_SYNC;
folder.keep_days = EntityFolder.DEFAULT_KEEP;
}
result.folders.add(folder);
@ -767,8 +767,8 @@ public class FragmentAccount extends FragmentEx {
inbox.synchronize = true;
inbox.unified = true;
inbox.notify = true;
inbox.sync_days = EntityFolder.DEFAULT_INBOX_SYNC;
inbox.keep_days = inbox.sync_days;
inbox.sync_days = EntityFolder.DEFAULT_SYNC;
inbox.keep_days = EntityFolder.DEFAULT_KEEP;
folders.add(inbox);

View File

@ -161,8 +161,8 @@ public class FragmentFolder extends FragmentEx {
if (TextUtils.isEmpty(display) || display.equals(name))
display = null;
int sync_days = (TextUtils.isEmpty(sync) ? EntityFolder.DEFAULT_USER_SYNC : Integer.parseInt(sync));
int keep_days = (TextUtils.isEmpty(keep) ? sync_days : Integer.parseInt(keep));
int sync_days = (TextUtils.isEmpty(sync) ? EntityFolder.DEFAULT_SYNC : Integer.parseInt(sync));
int keep_days = (TextUtils.isEmpty(keep) ? EntityFolder.DEFAULT_KEEP : Integer.parseInt(keep));
if (keep_days < sync_days)
keep_days = sync_days;
@ -361,11 +361,11 @@ public class FragmentFolder extends FragmentEx {
cbSynchronize.setChecked(folder == null || folder.synchronize);
cbPoll.setChecked(folder == null ? false : folder.poll);
cbNotify.setChecked(folder == null ? false : folder.notify);
etSyncDays.setText(Integer.toString(folder == null ? EntityFolder.DEFAULT_USER_SYNC : folder.sync_days));
etSyncDays.setText(Integer.toString(folder == null ? EntityFolder.DEFAULT_SYNC : folder.sync_days));
if (folder != null && folder.keep_days == Integer.MAX_VALUE)
cbKeepAll.setChecked(true);
else
etKeepDays.setText(Integer.toString(folder == null ? EntityFolder.DEFAULT_USER_SYNC : folder.keep_days));
etKeepDays.setText(Integer.toString(folder == null ? EntityFolder.DEFAULT_KEEP : folder.keep_days));
}
// Consider previous save as cancelled

View File

@ -1478,7 +1478,7 @@ public class ServiceSynchronize extends LifecycleService {
if (EntityFolder.OUTBOX.equals(folder.type))
db.folder().setFolderError(folder.id, null);
else
synchronizeMessages(account, folder, ifolder, state);
synchronizeMessages(account, folder, ifolder, jargs, state);
else
throw new MessagingException("Unknown operation name=" + op.name);
@ -1934,8 +1934,8 @@ public class ServiceSynchronize extends LifecycleService {
folder.level = level;
folder.synchronize = false;
folder.poll = ("imap.gmail.com".equals(account.host));
folder.sync_days = EntityFolder.DEFAULT_USER_SYNC;
folder.keep_days = EntityFolder.DEFAULT_USER_SYNC;
folder.sync_days = EntityFolder.DEFAULT_SYNC;
folder.keep_days = EntityFolder.DEFAULT_KEEP;
db.folder().insertFolder(folder);
Log.i(Helper.TAG, folder.name + " added");
} else {
@ -1960,26 +1960,26 @@ public class ServiceSynchronize extends LifecycleService {
}
}
private void synchronizeMessages(EntityAccount account, EntityFolder folder, IMAPFolder ifolder, ServiceState state) throws MessagingException, IOException {
private void synchronizeMessages(EntityAccount account, EntityFolder folder, IMAPFolder ifolder, JSONArray jargs, ServiceState state) throws JSONException, MessagingException, IOException {
DB db = DB.getInstance(this);
try {
// Refresh parameters
folder = db.folder().getFolder(folder.id);
int sync_days = jargs.getInt(0);
int keep_days = jargs.getInt(1);
Log.v(Helper.TAG, folder.name + " start sync after=" + folder.sync_days + "/" + folder.keep_days);
Log.v(Helper.TAG, folder.name + " start sync after=" + sync_days + "/" + keep_days);
db.folder().setFolderSyncState(folder.id, "syncing");
// Get reference times
Calendar cal_sync = Calendar.getInstance();
cal_sync.add(Calendar.DAY_OF_MONTH, -folder.sync_days);
cal_sync.add(Calendar.DAY_OF_MONTH, -sync_days);
cal_sync.set(Calendar.HOUR_OF_DAY, 0);
cal_sync.set(Calendar.MINUTE, 0);
cal_sync.set(Calendar.SECOND, 0);
cal_sync.set(Calendar.MILLISECOND, 0);
Calendar cal_keep = Calendar.getInstance();
cal_keep.add(Calendar.DAY_OF_MONTH, -folder.keep_days);
cal_keep.add(Calendar.DAY_OF_MONTH, -keep_days);
cal_keep.set(Calendar.HOUR_OF_DAY, 12);
cal_keep.set(Calendar.MINUTE, 0);
cal_keep.set(Calendar.SECOND, 0);
@ -2145,6 +2145,9 @@ public class ServiceSynchronize extends LifecycleService {
}
}
if (state.running)
db.folder().setFolderInitialized(folder.id);
db.folder().setFolderError(folder.id, null);
} finally {
Log.v(Helper.TAG, folder.name + " end sync state=" + state);

View File

@ -188,7 +188,7 @@
<string name="title_poll_folder">Check periodically instead of continuous synchronize</string>
<string name="title_notify_folder">Notify new messages</string>
<string name="title_sync_days">Synchronize messages (days)</string>
<string name="title_sync_days_remark">If you have daily internet connectivity, set this to one day to reduce battery and data usage after the initial synchronization</string>
<string name="title_sync_days_remark">Increasing this value will increase battery and data usage</string>
<string name="title_keep_days">Keep messages (days)</string>
<string name="title_keep_all">Keep all messages</string>
<string name="title_folder_exists">Folder %1$s exists</string>