mirror of
https://github.com/M66B/FairEmail.git
synced 2025-03-09 13:52:52 +00:00
Handle airplane mode changes
This commit is contained in:
parent
1c0f5382f5
commit
912c2281d2
2 changed files with 62 additions and 37 deletions
|
@ -21,8 +21,10 @@ package eu.faircode.email;
|
|||
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.Network;
|
||||
|
@ -71,7 +73,7 @@ public class ServiceSend extends ServiceBase {
|
|||
public void onCreate() {
|
||||
EntityLog.log(this, "Service send create");
|
||||
super.onCreate();
|
||||
startForeground(Helper.NOTIFICATION_SEND, getNotificationService(null, null).build());
|
||||
startForeground(Helper.NOTIFICATION_SEND, getNotificationService().build());
|
||||
|
||||
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
||||
wlOutbox = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":send");
|
||||
|
@ -81,8 +83,12 @@ public class ServiceSend extends ServiceBase {
|
|||
db.operation().liveUnsent().observe(this, new Observer<Integer>() {
|
||||
@Override
|
||||
public void onChanged(Integer unsent) {
|
||||
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
nm.notify(Helper.NOTIFICATION_SEND, getNotificationService(unsent, null).build());
|
||||
if (unsent != null && lastUnsent != unsent) {
|
||||
lastUnsent = unsent;
|
||||
|
||||
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
nm.notify(Helper.NOTIFICATION_SEND, getNotificationService().build());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -122,12 +128,19 @@ public class ServiceSend extends ServiceBase {
|
|||
NetworkRequest.Builder builder = new NetworkRequest.Builder();
|
||||
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
|
||||
cm.registerNetworkCallback(builder.build(), networkCallback);
|
||||
|
||||
IntentFilter iif = new IntentFilter();
|
||||
iif.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
||||
iif.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
|
||||
registerReceiver(connectionChangedReceiver, iif);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
EntityLog.log(this, "Service send destroy");
|
||||
|
||||
unregisterReceiver(connectionChangedReceiver);
|
||||
|
||||
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
cm.unregisterNetworkCallback(networkCallback);
|
||||
|
||||
|
@ -142,16 +155,11 @@ public class ServiceSend extends ServiceBase {
|
|||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
super.onStartCommand(intent, flags, startId);
|
||||
startForeground(Helper.NOTIFICATION_SEND, getNotificationService(null, null).build());
|
||||
startForeground(Helper.NOTIFICATION_SEND, getNotificationService().build());
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
NotificationCompat.Builder getNotificationService(Integer unsent, Boolean suitable) {
|
||||
if (unsent != null)
|
||||
lastUnsent = unsent;
|
||||
if (suitable != null)
|
||||
lastSuitable = suitable;
|
||||
|
||||
NotificationCompat.Builder getNotificationService() {
|
||||
// Build pending intent
|
||||
Intent intent = new Intent(this, ActivityView.class);
|
||||
intent.setAction("outbox");
|
||||
|
@ -180,8 +188,6 @@ public class ServiceSend extends ServiceBase {
|
|||
}
|
||||
|
||||
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
|
||||
private boolean suitable = false;
|
||||
|
||||
@Override
|
||||
public void onAvailable(Network network) {
|
||||
Log.i("Service send available=" + network);
|
||||
|
@ -199,27 +205,36 @@ public class ServiceSend extends ServiceBase {
|
|||
Log.i("Service send lost=" + network);
|
||||
checkConnectivity();
|
||||
}
|
||||
};
|
||||
|
||||
private void checkConnectivity() {
|
||||
boolean current = ConnectionHelper.getNetworkState(ServiceSend.this).isSuitable();
|
||||
if (suitable != current) {
|
||||
suitable = current;
|
||||
EntityLog.log(ServiceSend.this, "Service send suitable=" + suitable);
|
||||
|
||||
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
nm.notify(Helper.NOTIFICATION_SEND, getNotificationService(null, suitable).build());
|
||||
|
||||
if (suitable)
|
||||
executor.submit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
processOperations();
|
||||
}
|
||||
});
|
||||
}
|
||||
private BroadcastReceiver connectionChangedReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Log.i("Received intent=" + intent +
|
||||
" " + TextUtils.join(" ", Log.getExtras(intent.getExtras())));
|
||||
checkConnectivity();
|
||||
}
|
||||
};
|
||||
|
||||
private void checkConnectivity() {
|
||||
boolean suitable = ConnectionHelper.getNetworkState(ServiceSend.this).isSuitable();
|
||||
if (lastSuitable != suitable) {
|
||||
lastSuitable = suitable;
|
||||
EntityLog.log(ServiceSend.this, "Service send suitable=" + suitable);
|
||||
|
||||
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
nm.notify(Helper.NOTIFICATION_SEND, getNotificationService().build());
|
||||
|
||||
if (suitable)
|
||||
executor.submit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
processOperations();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void processOperations() {
|
||||
try {
|
||||
wlOutbox.acquire();
|
||||
|
|
|
@ -121,9 +121,12 @@ public class ServiceSynchronize extends ServiceBase {
|
|||
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
|
||||
// Removed because of Android VPN service
|
||||
// builder.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
|
||||
cm.registerNetworkCallback(builder.build(), onNetworkCallback);
|
||||
cm.registerNetworkCallback(builder.build(), networkCallback);
|
||||
|
||||
registerReceiver(onConnectionChanged, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
|
||||
IntentFilter iif = new IntentFilter();
|
||||
iif.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
||||
iif.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
|
||||
registerReceiver(connectionChangedReceiver, iif);
|
||||
registerReceiver(onScreenOff, new IntentFilter(Intent.ACTION_SCREEN_OFF));
|
||||
|
||||
DB db = DB.getInstance(this);
|
||||
|
@ -284,10 +287,10 @@ public class ServiceSynchronize extends ServiceBase {
|
|||
EntityLog.log(this, "Service destroy");
|
||||
|
||||
unregisterReceiver(onScreenOff);
|
||||
unregisterReceiver(onConnectionChanged);
|
||||
unregisterReceiver(connectionChangedReceiver);
|
||||
|
||||
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
cm.unregisterNetworkCallback(onNetworkCallback);
|
||||
cm.unregisterNetworkCallback(networkCallback);
|
||||
|
||||
setUnseen(null);
|
||||
|
||||
|
@ -1302,7 +1305,7 @@ public class ServiceSynchronize extends ServiceBase {
|
|||
}
|
||||
}
|
||||
|
||||
private ConnectivityManager.NetworkCallback onNetworkCallback = new ConnectivityManager.NetworkCallback() {
|
||||
private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
|
||||
private Boolean lastSuitable = null;
|
||||
|
||||
@Override
|
||||
|
@ -1419,12 +1422,19 @@ public class ServiceSynchronize extends ServiceBase {
|
|||
}
|
||||
};
|
||||
|
||||
private BroadcastReceiver onConnectionChanged = new BroadcastReceiver() {
|
||||
private BroadcastReceiver connectionChangedReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
EntityLog.log(ServiceSynchronize.this, "Connection intent=" + intent +
|
||||
EntityLog.log(ServiceSynchronize.this, "Received intent=" + intent +
|
||||
" " + TextUtils.join(" ", Log.getExtras(intent.getExtras())));
|
||||
onNetworkCallback.onCapabilitiesChanged(null, null);
|
||||
|
||||
if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
|
||||
boolean on = intent.getBooleanExtra("state", false);
|
||||
if (!on)
|
||||
lastLost = 0;
|
||||
}
|
||||
|
||||
networkCallback.onCapabilitiesChanged(null, null);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue