Initially sync 7 days, default sync 1 day

pull/147/head
M66B 6 years ago
parent f8b9640ca1
commit afe3832364

File diff suppressed because it is too large Load Diff

@ -23,6 +23,7 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.os.Bundle; 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)); 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.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.setText(TextUtils.join(" ", folder.keywords));
tvKeywords.setVisibility(debug && folder.keywords.length > 0 ? View.VISIBLE : View.GONE); tvKeywords.setVisibility(debug && folder.keywords.length > 0 ? View.VISIBLE : View.GONE);

@ -46,7 +46,7 @@ import io.requery.android.database.sqlite.RequerySQLiteOpenHelperFactory;
// https://developer.android.com/topic/libraries/architecture/room.html // https://developer.android.com/topic/libraries/architecture/room.html
@Database( @Database(
version = 21, version = 22,
entities = { entities = {
EntityIdentity.class, EntityIdentity.class,
EntityAccount.class, EntityAccount.class,
@ -292,6 +292,14 @@ public abstract class DB extends RoomDatabase {
db.execSQL("ALTER TABLE `identity` ADD COLUMN `bcc` TEXT"); 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(); .build();
} }

@ -175,6 +175,9 @@ public interface DaoFolder {
@Query("UPDATE folder SET name = :name WHERE account = :account AND name = :old") @Query("UPDATE folder SET name = :name WHERE account = :account AND name = :old")
int renameFolder(long account, String old, String name); 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") @Query("UPDATE folder SET tbd = 1 WHERE id = :id")
int setFolderTbd(long id); int setFolderTbd(long id);

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

@ -92,17 +92,22 @@ public class EntityOperation {
queue(db, message, name, jargs); queue(db, message, name, jargs);
} }
static void sync(DB db, long folder) { static void sync(DB db, long fid) {
if (db.operation().getOperationCount(folder, EntityOperation.SYNC) == 0) { 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(); EntityOperation operation = new EntityOperation();
operation.folder = folder; operation.folder = folder.id;
operation.message = null; operation.message = null;
operation.name = SYNC; operation.name = SYNC;
operation.args = new JSONArray().toString(); operation.args = jargs.toString();
operation.created = new Date().getTime(); operation.created = new Date().getTime();
operation.id = db.operation().insertOperation(operation); operation.id = db.operation().insertOperation(operation);
db.folder().setFolderSyncState(folder, "requested"); db.folder().setFolderSyncState(fid, "requested");
Log.i(Helper.TAG, "Queued sync folder=" + folder); Log.i(Helper.TAG, "Queued sync folder=" + folder);
} }

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

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

@ -1478,7 +1478,7 @@ public class ServiceSynchronize extends LifecycleService {
if (EntityFolder.OUTBOX.equals(folder.type)) if (EntityFolder.OUTBOX.equals(folder.type))
db.folder().setFolderError(folder.id, null); db.folder().setFolderError(folder.id, null);
else else
synchronizeMessages(account, folder, ifolder, state); synchronizeMessages(account, folder, ifolder, jargs, state);
else else
throw new MessagingException("Unknown operation name=" + op.name); throw new MessagingException("Unknown operation name=" + op.name);
@ -1934,8 +1934,8 @@ public class ServiceSynchronize extends LifecycleService {
folder.level = level; folder.level = level;
folder.synchronize = false; folder.synchronize = false;
folder.poll = ("imap.gmail.com".equals(account.host)); folder.poll = ("imap.gmail.com".equals(account.host));
folder.sync_days = EntityFolder.DEFAULT_USER_SYNC; folder.sync_days = EntityFolder.DEFAULT_SYNC;
folder.keep_days = EntityFolder.DEFAULT_USER_SYNC; folder.keep_days = EntityFolder.DEFAULT_KEEP;
db.folder().insertFolder(folder); db.folder().insertFolder(folder);
Log.i(Helper.TAG, folder.name + " added"); Log.i(Helper.TAG, folder.name + " added");
} else { } 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); DB db = DB.getInstance(this);
try { try {
// Refresh parameters int sync_days = jargs.getInt(0);
folder = db.folder().getFolder(folder.id); 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"); db.folder().setFolderSyncState(folder.id, "syncing");
// Get reference times // Get reference times
Calendar cal_sync = Calendar.getInstance(); 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.HOUR_OF_DAY, 0);
cal_sync.set(Calendar.MINUTE, 0); cal_sync.set(Calendar.MINUTE, 0);
cal_sync.set(Calendar.SECOND, 0); cal_sync.set(Calendar.SECOND, 0);
cal_sync.set(Calendar.MILLISECOND, 0); cal_sync.set(Calendar.MILLISECOND, 0);
Calendar cal_keep = Calendar.getInstance(); 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.HOUR_OF_DAY, 12);
cal_keep.set(Calendar.MINUTE, 0); cal_keep.set(Calendar.MINUTE, 0);
cal_keep.set(Calendar.SECOND, 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); db.folder().setFolderError(folder.id, null);
} finally { } finally {
Log.v(Helper.TAG, folder.name + " end sync state=" + state); Log.v(Helper.TAG, folder.name + " end sync state=" + state);

@ -188,7 +188,7 @@
<string name="title_poll_folder">Check periodically instead of continuous synchronize</string> <string name="title_poll_folder">Check periodically instead of continuous synchronize</string>
<string name="title_notify_folder">Notify new messages</string> <string name="title_notify_folder">Notify new messages</string>
<string name="title_sync_days">Synchronize messages (days)</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_days">Keep messages (days)</string>
<string name="title_keep_all">Keep all messages</string> <string name="title_keep_all">Keep all messages</string>
<string name="title_folder_exists">Folder %1$s exists</string> <string name="title_folder_exists">Folder %1$s exists</string>

Loading…
Cancel
Save