From 912c2281d2c45478d52ef613acbc3618d38e6346 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 4 Oct 2019 17:17:42 +0200 Subject: [PATCH] Handle airplane mode changes --- .../java/eu/faircode/email/ServiceSend.java | 73 +++++++++++-------- .../eu/faircode/email/ServiceSynchronize.java | 26 +++++-- 2 files changed, 62 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ServiceSend.java b/app/src/main/java/eu/faircode/email/ServiceSend.java index cb6c4190d0..d4f8d2c3bd 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSend.java +++ b/app/src/main/java/eu/faircode/email/ServiceSend.java @@ -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() { @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(); diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index cb2a795b69..9e72b60efc 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -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); } };