mirror of https://github.com/M66B/FairEmail.git
Added on demand process
This commit is contained in:
parent
9d15ccc695
commit
c39b85c139
|
@ -306,7 +306,7 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
|
|||
// Outbox
|
||||
if (internet) {
|
||||
now = true;
|
||||
EntityOperation.sync(context, db, fid);
|
||||
EntityOperation.sync(context, fid);
|
||||
} else
|
||||
throw new IllegalArgumentException(context.getString(R.string.title_no_internet));
|
||||
} else {
|
||||
|
@ -314,12 +314,12 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
|
|||
if (account.ondemand || !enabled) {
|
||||
if (internet) {
|
||||
now = true;
|
||||
ServiceUI.sync(context, fid);
|
||||
EntityOperation.sync(context, fid);
|
||||
} else
|
||||
throw new IllegalArgumentException(context.getString(R.string.title_no_internet));
|
||||
} else {
|
||||
now = "connected".equals(account.state);
|
||||
EntityOperation.sync(context, db, fid);
|
||||
EntityOperation.sync(context, fid);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -39,13 +39,6 @@ public interface DaoFolder {
|
|||
" AND (account.synchronize AND NOT account.ondemand)")
|
||||
List<EntityFolder> getFoldersAutoSync();
|
||||
|
||||
@Query("SELECT folder.* FROM folder" +
|
||||
" JOIN account ON account.id = folder.account" +
|
||||
" WHERE folder.synchronize" +
|
||||
" AND account.id = :account" +
|
||||
" AND (account.synchronize AND account.ondemand)")
|
||||
List<EntityFolder> getFoldersOnDemandSync(long account);
|
||||
|
||||
@Query("SELECT folder.* FROM folder" +
|
||||
" JOIN account ON account.id = folder.account" +
|
||||
" WHERE account.synchronize" +
|
||||
|
|
|
@ -98,10 +98,14 @@ public class EntityOperation {
|
|||
queue(context, db, message, name, jargs);
|
||||
}
|
||||
|
||||
static void sync(Context context, DB db, long fid) {
|
||||
static void sync(Context context, long fid) {
|
||||
DB db = DB.getInstance(context);
|
||||
if (db.operation().getOperationCount(fid, EntityOperation.SYNC) == 0) {
|
||||
|
||||
EntityFolder folder = db.folder().getFolder(fid);
|
||||
EntityAccount account = null;
|
||||
if (folder.account != null)
|
||||
account = db.account().getAccount(folder.account);
|
||||
|
||||
EntityOperation operation = new EntityOperation();
|
||||
operation.folder = folder.id;
|
||||
|
@ -111,10 +115,16 @@ public class EntityOperation {
|
|||
operation.created = new Date().getTime();
|
||||
operation.id = db.operation().insertOperation(operation);
|
||||
|
||||
db.folder().setFolderSyncState(fid, "requested");
|
||||
if (account != null && account.ondemand) {
|
||||
db.folder().setFolderState(fid, "waiting");
|
||||
db.folder().setFolderSyncState(fid, "manual");
|
||||
} else
|
||||
db.folder().setFolderSyncState(fid, "requested");
|
||||
|
||||
if (folder.account == null) // Outbox
|
||||
if (account == null) // Outbox
|
||||
ServiceSend.start(context);
|
||||
else if (account.ondemand)
|
||||
ServiceUI.process(context, fid);
|
||||
|
||||
Log.i("Queued sync folder=" + folder);
|
||||
}
|
||||
|
@ -225,8 +235,6 @@ public class EntityOperation {
|
|||
} else if (DELETE.equals(name))
|
||||
db.message().setMessageUiHide(message.id, true);
|
||||
|
||||
else if (SEND.equals(name))
|
||||
ServiceSend.start(context);
|
||||
} catch (JSONException ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
|
@ -242,6 +250,14 @@ public class EntityOperation {
|
|||
Log.i("Queued op=" + operation.id + "/" + operation.name +
|
||||
" msg=" + operation.folder + "/" + operation.message +
|
||||
" args=" + operation.args);
|
||||
|
||||
if (SEND.equals(name))
|
||||
ServiceSend.start(context);
|
||||
else {
|
||||
EntityAccount account = db.account().getAccount(message.account);
|
||||
if (account.ondemand)
|
||||
ServiceUI.process(context, operation.folder);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -250,7 +250,7 @@ public class FragmentFolder extends FragmentBase {
|
|||
|
||||
db.message().deleteMessagesBefore(id, keep_time, true);
|
||||
|
||||
EntityOperation.sync(context, db, folder.id);
|
||||
EntityOperation.sync(context, folder.id);
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
|
|
|
@ -310,12 +310,12 @@ public class FragmentFolders extends FragmentBase {
|
|||
if (account.ondemand || !enabled)
|
||||
if (internet) {
|
||||
now = true;
|
||||
ServiceUI.sync(context, folder.id);
|
||||
EntityOperation.sync(context, folder.id);
|
||||
} else
|
||||
nointernet = true;
|
||||
else {
|
||||
now = "connected".equals(account.state);
|
||||
EntityOperation.sync(context, db, folder.id);
|
||||
EntityOperation.sync(context, folder.id);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -323,9 +323,7 @@ public class FragmentFolders extends FragmentBase {
|
|||
if (account.ondemand || !enabled) {
|
||||
if (internet) {
|
||||
now = true;
|
||||
List<EntityFolder> folders = db.folder().getFoldersOnDemandSync(aid);
|
||||
for (EntityFolder folder : folders)
|
||||
ServiceUI.sync(context, folder.id);
|
||||
ServiceUI.fsync(context, aid);
|
||||
} else
|
||||
nointernet = true;
|
||||
} else {
|
||||
|
|
|
@ -521,7 +521,7 @@ public class FragmentMessages extends FragmentBase {
|
|||
// Outbox
|
||||
if (internet) {
|
||||
now = true;
|
||||
EntityOperation.sync(context, db, folder.id);
|
||||
EntityOperation.sync(context, folder.id);
|
||||
} else
|
||||
nointernet = true;
|
||||
} else {
|
||||
|
@ -529,12 +529,12 @@ public class FragmentMessages extends FragmentBase {
|
|||
if (account.ondemand || !enabled) {
|
||||
if (internet) {
|
||||
now = true;
|
||||
ServiceUI.sync(context, folder.id);
|
||||
EntityOperation.sync(context, folder.id);
|
||||
} else
|
||||
nointernet = true;
|
||||
} else {
|
||||
now = "connected".equals(account.state);
|
||||
EntityOperation.sync(context, db, folder.id);
|
||||
EntityOperation.sync(context, folder.id);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -269,7 +269,7 @@ public class FragmentOptions extends FragmentBase implements SharedPreferences.O
|
|||
DB db = DB.getInstance(context);
|
||||
List<EntityFolder> folders = db.folder().getFoldersAutoSync();
|
||||
for (EntityFolder folder : folders)
|
||||
EntityOperation.sync(context, db, folder.id);
|
||||
EntityOperation.sync(context, folder.id);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -406,7 +406,7 @@ public class FragmentSetup extends FragmentBase {
|
|||
protected Void onExecute(Context context, Bundle args) {
|
||||
DB db = DB.getInstance(context);
|
||||
for (EntityFolder folder : db.folder().getFoldersAutoSync())
|
||||
EntityOperation.sync(context, db, folder.id);
|
||||
EntityOperation.sync(context, folder.id);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -235,9 +235,9 @@ public class ServiceSynchronize extends LifecycleService {
|
|||
Log.i(account.name + " run");
|
||||
|
||||
// Debug
|
||||
boolean debug = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("debug", false);
|
||||
debug = debug || BuildConfig.DEBUG;
|
||||
System.setProperty("mail.socket.debug", Boolean.toString(debug));
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
|
||||
boolean debug = (prefs.getBoolean("debug", false) || BuildConfig.BETA_RELEASE);
|
||||
//System.setProperty("mail.socket.debug", Boolean.toString(debug));
|
||||
|
||||
// Create session
|
||||
Properties props = MessageHelper.getSessionProperties(account.auth_type, account.realm, account.insecure);
|
||||
|
@ -581,7 +581,7 @@ public class ServiceSynchronize extends LifecycleService {
|
|||
idler.start();
|
||||
idlers.add(idler);
|
||||
|
||||
EntityOperation.sync(this, db, folder.id);
|
||||
EntityOperation.sync(this, folder.id);
|
||||
} else
|
||||
folders.put(folder, null);
|
||||
|
||||
|
@ -722,7 +722,7 @@ public class ServiceSynchronize extends LifecycleService {
|
|||
if (!folders.get(folder).isOpen())
|
||||
throw new FolderClosedException(folders.get(folder));
|
||||
} else
|
||||
EntityOperation.sync(this, db, folder.id);
|
||||
EntityOperation.sync(this, folder.id);
|
||||
|
||||
// Successfully connected: reset back off time
|
||||
backoff = CONNECT_BACKOFF_START;
|
||||
|
|
|
@ -9,8 +9,6 @@ import android.net.Uri;
|
|||
import android.os.PowerManager;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import com.sun.mail.imap.IMAPFolder;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
|
||||
|
@ -101,12 +99,16 @@ public class ServiceUI extends IntentService {
|
|||
break;
|
||||
|
||||
case "snooze":
|
||||
// AlarmManager.RTC_WAKEUP
|
||||
onSnooze(id);
|
||||
break;
|
||||
|
||||
case "synchronize":
|
||||
// AlarmManager.RTC_WAKEUP
|
||||
onSyncOndemand(id);
|
||||
case "process":
|
||||
onProcessOperation(id);
|
||||
break;
|
||||
|
||||
case "fsync":
|
||||
onFolderSync(id);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -239,10 +241,9 @@ public class ServiceUI extends IntentService {
|
|||
}
|
||||
}
|
||||
|
||||
private void onSyncOndemand(long fid) {
|
||||
Log.i("Synchronize on demand folder=" + fid);
|
||||
|
||||
private void onProcessOperation(long fid) {
|
||||
DB db = DB.getInstance(this);
|
||||
|
||||
EntityFolder folder = db.folder().getFolder(fid);
|
||||
if (folder == null)
|
||||
return;
|
||||
|
@ -252,10 +253,13 @@ public class ServiceUI extends IntentService {
|
|||
|
||||
Store istore = null;
|
||||
try {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
boolean debug = (prefs.getBoolean("debug", false) || BuildConfig.BETA_RELEASE);
|
||||
|
||||
// Create session
|
||||
Properties props = MessageHelper.getSessionProperties(account.auth_type, account.realm, account.insecure);
|
||||
final Session isession = Session.getInstance(props, null);
|
||||
isession.setDebug(true);
|
||||
isession.setDebug(debug);
|
||||
|
||||
// Connect account
|
||||
Log.i(account.name + " connecting");
|
||||
|
@ -267,9 +271,6 @@ public class ServiceUI extends IntentService {
|
|||
db.account().setAccountError(account.id, null);
|
||||
Log.i(account.name + " connected");
|
||||
|
||||
// Synchronize folders
|
||||
Core.onSynchronizeFolders(this, account, istore, new Core.State());
|
||||
|
||||
// Connect folder
|
||||
Log.i(folder.name + " connecting");
|
||||
db.folder().setFolderState(folder.id, "connecting");
|
||||
|
@ -282,9 +283,6 @@ public class ServiceUI extends IntentService {
|
|||
// Process operations
|
||||
Core.processOperations(this, account, folder, isession, istore, ifolder, new Core.State());
|
||||
|
||||
// Synchronize messages
|
||||
Core.onSynchronizeMessages(this, folder.getSyncArgs(), account, folder, (IMAPFolder) ifolder, new Core.State());
|
||||
|
||||
} catch (Throwable ex) {
|
||||
Log.w(ex);
|
||||
Core.reportError(this, account, folder, ex);
|
||||
|
@ -311,13 +309,66 @@ public class ServiceUI extends IntentService {
|
|||
}
|
||||
}
|
||||
|
||||
public static void sync(Context context, long folder) {
|
||||
DB db = DB.getInstance(context);
|
||||
db.folder().setFolderState(folder, "waiting");
|
||||
db.folder().setFolderSyncState(folder, "manual");
|
||||
private void onFolderSync(long aid) {
|
||||
DB db = DB.getInstance(this);
|
||||
EntityAccount account = db.account().getAccount(aid);
|
||||
if (account == null)
|
||||
return;
|
||||
|
||||
Store istore = null;
|
||||
try {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
boolean debug = (prefs.getBoolean("debug", false) || BuildConfig.BETA_RELEASE);
|
||||
|
||||
// Create session
|
||||
Properties props = MessageHelper.getSessionProperties(account.auth_type, account.realm, account.insecure);
|
||||
final Session isession = Session.getInstance(props, null);
|
||||
isession.setDebug(debug);
|
||||
|
||||
// Connect account
|
||||
Log.i(account.name + " connecting");
|
||||
db.account().setAccountState(account.id, "connecting");
|
||||
istore = isession.getStore(account.getProtocol());
|
||||
Helper.connect(this, istore, account);
|
||||
db.account().setAccountState(account.id, "connected");
|
||||
db.account().setAccountConnected(account.id, new Date().getTime());
|
||||
db.account().setAccountError(account.id, null);
|
||||
Log.i(account.name + " connected");
|
||||
|
||||
// Synchronize folders
|
||||
Core.onSynchronizeFolders(this, account, istore, new Core.State());
|
||||
|
||||
} catch (Throwable ex) {
|
||||
Log.w(ex);
|
||||
Core.reportError(this, account, null, ex);
|
||||
db.account().setAccountError(account.id, Helper.formatThrowable(ex));
|
||||
} finally {
|
||||
if (istore != null) {
|
||||
Log.i(account.name + " closing");
|
||||
db.account().setAccountState(account.id, "closing");
|
||||
|
||||
try {
|
||||
istore.close();
|
||||
} catch (MessagingException ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
|
||||
Log.i(account.name + " closed");
|
||||
}
|
||||
|
||||
db.account().setAccountState(account.id, null);
|
||||
}
|
||||
}
|
||||
|
||||
public static void process(Context context, long folder) {
|
||||
context.startService(
|
||||
new Intent(context, ServiceUI.class)
|
||||
.setAction("synchronize:" + folder));
|
||||
.setAction("process:" + folder));
|
||||
}
|
||||
|
||||
public static void fsync(Context context, long account) {
|
||||
context.startService(
|
||||
new Intent(context, ServiceUI.class)
|
||||
.setAction("fsync:" + account));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,8 +20,10 @@ package eu.faircode.email;
|
|||
*/
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import com.sun.mail.iap.Argument;
|
||||
import com.sun.mail.iap.Response;
|
||||
|
@ -163,9 +165,12 @@ public class ViewModelBrowse extends ViewModel {
|
|||
if (!internet)
|
||||
throw new IllegalArgumentException(state.context.getString(R.string.title_no_internet));
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(state.context);
|
||||
boolean debug = (prefs.getBoolean("debug", false) || BuildConfig.BETA_RELEASE);
|
||||
|
||||
Properties props = MessageHelper.getSessionProperties(account.auth_type, account.realm, account.insecure);
|
||||
Session isession = Session.getInstance(props, null);
|
||||
isession.setDebug(true);
|
||||
isession.setDebug(debug);
|
||||
|
||||
Log.i("Boundary connecting account=" + account.name);
|
||||
state.istore = (IMAPStore) isession.getStore(account.getProtocol());
|
||||
|
|
Loading…
Reference in New Issue