mirror of
https://github.com/M66B/FairEmail.git
synced 2025-02-28 16:55:56 +00:00
Refactoring
This commit is contained in:
parent
30ec86f9da
commit
037af96be0
2 changed files with 49 additions and 51 deletions
|
@ -75,6 +75,9 @@ public class ConnectionHelper {
|
||||||
private Boolean suitable = null;
|
private Boolean suitable = null;
|
||||||
private Boolean unmetered = null;
|
private Boolean unmetered = null;
|
||||||
private Boolean roaming = null;
|
private Boolean roaming = null;
|
||||||
|
private actionType action = actionType.NONE;
|
||||||
|
|
||||||
|
public enum actionType {NONE, AVAILABLE, CHANGED, LOST}
|
||||||
|
|
||||||
boolean isConnected() {
|
boolean isConnected() {
|
||||||
return (connected != null && connected);
|
return (connected != null && connected);
|
||||||
|
@ -163,6 +166,12 @@ public class ConnectionHelper {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static NetworkState getNetworkState(Context context, NetworkState.actionType action) {
|
||||||
|
NetworkState state = getNetworkState(context);
|
||||||
|
state.action = action;
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
private static Boolean isMetered(Context context) {
|
private static Boolean isMetered(Context context) {
|
||||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
if (cm == null)
|
if (cm == null)
|
||||||
|
|
|
@ -87,10 +87,10 @@ import me.leolin.shortcutbadger.ShortcutBadger;
|
||||||
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
|
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
|
||||||
|
|
||||||
public class ServiceSynchronize extends ServiceBase implements SharedPreferences.OnSharedPreferenceChangeListener {
|
public class ServiceSynchronize extends ServiceBase implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
private ConnectionHelper.NetworkState lastNetworkState = new ConnectionHelper.NetworkState();
|
private Boolean lastSuitable = null;
|
||||||
private long lastLost = 0;
|
private long lastLost = 0;
|
||||||
private int accounts = 0;
|
private int lastAccounts = 0;
|
||||||
private int operations = 0;
|
private int lastOperations = 0;
|
||||||
|
|
||||||
private static final int CONNECT_BACKOFF_START = 8; // seconds
|
private static final int CONNECT_BACKOFF_START = 8; // seconds
|
||||||
private static final int CONNECT_BACKOFF_MAX = 64; // seconds (totally 2 minutes)
|
private static final int CONNECT_BACKOFF_MAX = 64; // seconds (totally 2 minutes)
|
||||||
|
@ -228,15 +228,15 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int connected = 0;
|
int accounts = 0;
|
||||||
int pending = 0;
|
int operations = 0;
|
||||||
boolean runService = false;
|
boolean runService = false;
|
||||||
for (TupleAccountNetworkState current : accountNetworkStates) {
|
for (TupleAccountNetworkState current : accountNetworkStates) {
|
||||||
if (current.accountState.shouldRun(current.enabled))
|
if (current.accountState.shouldRun(current.enabled))
|
||||||
runService = true;
|
runService = true;
|
||||||
if ("connected".equals(current.accountState.state))
|
if ("connected".equals(current.accountState.state))
|
||||||
connected++;
|
accounts++;
|
||||||
pending += current.accountState.operations;
|
operations += current.accountState.operations;
|
||||||
|
|
||||||
int index = accountStates.indexOf(current);
|
int index = accountStates.indexOf(current);
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
|
@ -277,15 +277,15 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
}
|
}
|
||||||
|
|
||||||
if (runService) {
|
if (runService) {
|
||||||
if (accounts != connected || operations != pending) {
|
if (lastAccounts != accounts || lastOperations != operations) {
|
||||||
accounts = connected;
|
lastAccounts = accounts;
|
||||||
operations = pending;
|
lastOperations = operations;
|
||||||
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(accounts, operations).build());
|
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
running = false;
|
running = false;
|
||||||
stopSelf();
|
stopSelf(); // will result in quit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -293,7 +293,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
private void start(final TupleAccountNetworkState accountNetworkState, boolean sync) {
|
private void start(final TupleAccountNetworkState accountNetworkState, boolean sync) {
|
||||||
EntityLog.log(ServiceSynchronize.this, "Service start=" + accountNetworkState);
|
EntityLog.log(ServiceSynchronize.this, "Service start=" + accountNetworkState);
|
||||||
|
|
||||||
final Core.State astate = new Core.State(lastNetworkState);
|
final Core.State astate = new Core.State(accountNetworkState.networkState);
|
||||||
astate.runnable(new Runnable() {
|
astate.runnable(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
@ -321,10 +321,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
|
|
||||||
Map<String, String> crumb = new HashMap<>();
|
Map<String, String> crumb = new HashMap<>();
|
||||||
crumb.put("account", accountNetworkState.toString());
|
crumb.put("account", accountNetworkState.toString());
|
||||||
crumb.put("connected", Boolean.toString(lastNetworkState.isConnected()));
|
crumb.put("connected", Boolean.toString(accountNetworkState.networkState.isConnected()));
|
||||||
crumb.put("suitable", Boolean.toString(lastNetworkState.isSuitable()));
|
crumb.put("suitable", Boolean.toString(accountNetworkState.networkState.isSuitable()));
|
||||||
crumb.put("unmetered", Boolean.toString(lastNetworkState.isUnmetered()));
|
crumb.put("unmetered", Boolean.toString(accountNetworkState.networkState.isUnmetered()));
|
||||||
crumb.put("roaming", Boolean.toString(lastNetworkState.isRoaming()));
|
crumb.put("roaming", Boolean.toString(accountNetworkState.networkState.isRoaming()));
|
||||||
crumb.put("lastLost", new Date(lastLost).toString());
|
crumb.put("lastLost", new Date(lastLost).toString());
|
||||||
Log.breadcrumb("start", crumb);
|
Log.breadcrumb("start", crumb);
|
||||||
|
|
||||||
|
@ -346,10 +346,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
public void run() {
|
public void run() {
|
||||||
Map<String, String> crumb = new HashMap<>();
|
Map<String, String> crumb = new HashMap<>();
|
||||||
crumb.put("account", accountNetworkState.toString());
|
crumb.put("account", accountNetworkState.toString());
|
||||||
crumb.put("connected", Boolean.toString(lastNetworkState.isConnected()));
|
crumb.put("connected", Boolean.toString(accountNetworkState.networkState.isConnected()));
|
||||||
crumb.put("suitable", Boolean.toString(lastNetworkState.isSuitable()));
|
crumb.put("suitable", Boolean.toString(accountNetworkState.networkState.isSuitable()));
|
||||||
crumb.put("unmetered", Boolean.toString(lastNetworkState.isUnmetered()));
|
crumb.put("unmetered", Boolean.toString(accountNetworkState.networkState.isUnmetered()));
|
||||||
crumb.put("roaming", Boolean.toString(lastNetworkState.isRoaming()));
|
crumb.put("roaming", Boolean.toString(accountNetworkState.networkState.isRoaming()));
|
||||||
crumb.put("lastLost", new Date(lastLost).toString());
|
crumb.put("lastLost", new Date(lastLost).toString());
|
||||||
Log.breadcrumb("stop", crumb);
|
Log.breadcrumb("stop", crumb);
|
||||||
|
|
||||||
|
@ -616,11 +616,11 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
return START_STICKY;
|
return START_STICKY;
|
||||||
}
|
}
|
||||||
|
|
||||||
private NotificationCompat.Builder getNotificationService(Integer connected, Integer pending) {
|
private NotificationCompat.Builder getNotificationService(Integer accounts, Integer operations) {
|
||||||
if (connected != null)
|
if (accounts != null)
|
||||||
this.accounts = connected;
|
this.lastAccounts = accounts;
|
||||||
if (pending != null)
|
if (operations != null)
|
||||||
this.operations = pending;
|
this.lastOperations = operations;
|
||||||
|
|
||||||
// Build pending intent
|
// Build pending intent
|
||||||
Intent why = new Intent(this, ActivityView.class);
|
Intent why = new Intent(this, ActivityView.class);
|
||||||
|
@ -632,7 +632,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
new NotificationCompat.Builder(this, "service")
|
new NotificationCompat.Builder(this, "service")
|
||||||
.setSmallIcon(R.drawable.baseline_compare_arrows_white_24)
|
.setSmallIcon(R.drawable.baseline_compare_arrows_white_24)
|
||||||
.setContentTitle(getResources().getQuantityString(
|
.setContentTitle(getResources().getQuantityString(
|
||||||
R.plurals.title_notification_synchronizing, accounts, accounts))
|
R.plurals.title_notification_synchronizing, lastAccounts, lastAccounts))
|
||||||
.setContentIntent(piWhy)
|
.setContentIntent(piWhy)
|
||||||
.setAutoCancel(false)
|
.setAutoCancel(false)
|
||||||
.setShowWhen(false)
|
.setShowWhen(false)
|
||||||
|
@ -641,11 +641,11 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
|
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
|
||||||
.setLocalOnly(true);
|
.setLocalOnly(true);
|
||||||
|
|
||||||
if (operations > 0)
|
if (lastOperations > 0)
|
||||||
builder.setContentText(getResources().getQuantityString(
|
builder.setContentText(getResources().getQuantityString(
|
||||||
R.plurals.title_notification_operations, operations, operations));
|
R.plurals.title_notification_operations, lastOperations, lastOperations));
|
||||||
|
|
||||||
if (!lastNetworkState.isSuitable())
|
if (lastSuitable == null || !lastSuitable)
|
||||||
builder.setSubText(getString(R.string.title_notification_waiting));
|
builder.setSubText(getString(R.string.title_notification_waiting));
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
|
@ -1299,30 +1299,20 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
}
|
}
|
||||||
|
|
||||||
private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
|
private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
|
||||||
private Boolean lastSuitable = null;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAvailable(@NonNull Network network) {
|
public void onAvailable(@NonNull Network network) {
|
||||||
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
EntityLog.log(ServiceSynchronize.this, "Available network=" + network +
|
EntityLog.log(ServiceSynchronize.this, "Available network=" + network +
|
||||||
" capabilities " + cm.getNetworkCapabilities(network) +
|
" capabilities " + cm.getNetworkCapabilities(network));
|
||||||
" connected=" + lastNetworkState.isConnected() +
|
updateState(ConnectionHelper.NetworkState.actionType.AVAILABLE);
|
||||||
" suitable=" + lastNetworkState.isSuitable() +
|
|
||||||
" unmetered=" + lastNetworkState.isUnmetered() +
|
|
||||||
" roaming=" + lastNetworkState.isRoaming());
|
|
||||||
updateState();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities capabilities) {
|
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities capabilities) {
|
||||||
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
EntityLog.log(ServiceSynchronize.this, "Changed network=" + network +
|
EntityLog.log(ServiceSynchronize.this, "Changed network=" + network +
|
||||||
" capabilities " + cm.getNetworkCapabilities(network) +
|
" capabilities " + cm.getNetworkCapabilities(network));
|
||||||
" connected=" + lastNetworkState.isConnected() +
|
updateState(ConnectionHelper.NetworkState.actionType.CHANGED);
|
||||||
" suitable=" + lastNetworkState.isSuitable() +
|
|
||||||
" unmetered=" + lastNetworkState.isUnmetered() +
|
|
||||||
" roaming=" + lastNetworkState.isRoaming());
|
|
||||||
updateState();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1332,18 +1322,17 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
EntityLog.log(ServiceSynchronize.this, "Lost network=" + network + " active=" + active);
|
EntityLog.log(ServiceSynchronize.this, "Lost network=" + network + " active=" + active);
|
||||||
if (active == null)
|
if (active == null)
|
||||||
lastLost = new Date().getTime();
|
lastLost = new Date().getTime();
|
||||||
updateState();
|
updateState(ConnectionHelper.NetworkState.actionType.LOST);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateState() {
|
private void updateState(ConnectionHelper.NetworkState.actionType action) {
|
||||||
ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(ServiceSynchronize.this);
|
ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(ServiceSynchronize.this, action);
|
||||||
lastNetworkState.update(ns);
|
|
||||||
liveNetworkState.postValue(ns);
|
liveNetworkState.postValue(ns);
|
||||||
|
|
||||||
if (lastSuitable == null || lastSuitable != lastNetworkState.isSuitable()) {
|
if (lastSuitable == null || lastSuitable != ns.isSuitable()) {
|
||||||
lastSuitable = lastNetworkState.isSuitable();
|
lastSuitable = ns.isSuitable();
|
||||||
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(accounts, operations).build());
|
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue