diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-09-13 19:24:25 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-09-13 19:24:25 +0200 |
commit | 5f10156bf99ed59d9200a79a75b09e42b954518e (patch) | |
tree | c51e99a735dbdb046f530611fc0edc03e4a55517 | |
parent | 7a42c388ce9363de0a7cc668fc098aebc23d0ce6 (diff) |
listen for Android 6 DND events
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/services/XmppConnectionService.java | 48 |
1 files changed, 36 insertions, 12 deletions
diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index 01295c242..c42d8d755 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -4,8 +4,10 @@ import android.Manifest; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.AlarmManager; +import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; @@ -408,7 +410,8 @@ public class XmppConnectionService extends Service { private WakeLock wakeLock; private PowerManager pm; private LruCache<String, Bitmap> mBitmapCache; - private EventReceiver mEventReceiver = new EventReceiver(); + private BroadcastReceiver mInternalEventReceiver = new InternalEventReceiver(); + private BroadcastReceiver mInternalScreenEventReceiver = new InternalEventReceiver(); private static String generateFetchKey(Account account, final Avatar avatar) { return account.getJid().asBareJid() + "_" + avatar.owner + "_" + avatar.sha1sum; @@ -658,6 +661,7 @@ public class XmppConnectionService extends Service { updateConversation(c); }); case AudioManager.RINGER_MODE_CHANGED_ACTION: + case NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED: if (dndOnSilentMode()) { refreshAllPresences(); } @@ -945,16 +949,25 @@ public class XmppConnectionService extends Service { } private boolean isPhoneSilenced() { - AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); + final boolean notificationDnd; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + final NotificationManager notificationManager = getSystemService(NotificationManager.class); + final int filter = notificationManager == null ? NotificationManager.INTERRUPTION_FILTER_UNKNOWN : notificationManager.getCurrentInterruptionFilter(); + notificationDnd = filter >= NotificationManager.INTERRUPTION_FILTER_PRIORITY; + } else { + notificationDnd = false; + } + final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); + final int ringerMode = audioManager == null ? AudioManager.RINGER_MODE_NORMAL : audioManager.getRingerMode(); try { if (treatVibrateAsSilent()) { - return audioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL; + return notificationDnd || ringerMode != AudioManager.RINGER_MODE_NORMAL; } else { - return audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT; + return notificationDnd || ringerMode == AudioManager.RINGER_MODE_SILENT; } } catch (Throwable throwable) { Log.d(Config.LOGTAG, "platform bug in isPhoneSilenced (" + throwable.getMessage() + ")"); - return false; + return notificationDnd; } } @@ -1143,14 +1156,17 @@ public class XmppConnectionService extends Service { toggleScreenEventReceiver(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { scheduleNextIdlePing(); + IntentFilter intentFilter = new IntentFilter(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); + } + intentFilter.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED); + registerReceiver(this.mInternalEventReceiver, intentFilter); } //start export log service every day at given time ScheduleAutomaticExport(); // cancel scheduled exporter CancelAutomaticExport(false); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - registerReceiver(this.mEventReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); - } } public void startContactObserver() { @@ -1177,7 +1193,7 @@ public class XmppConnectionService extends Service { @Override public void onDestroy() { try { - unregisterReceiver(this.mEventReceiver); + unregisterReceiver(this.mInternalEventReceiver); } catch (IllegalArgumentException e) { //ignored } @@ -1194,12 +1210,13 @@ public class XmppConnectionService extends Service { public void toggleScreenEventReceiver() { if (awayWhenScreenOff() && !manuallyChangePresence()) { - final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); + final IntentFilter filter = new IntentFilter(); + filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); - registerReceiver(this.mEventReceiver, filter); + registerReceiver(this.mInternalScreenEventReceiver, filter); } else { try { - unregisterReceiver(this.mEventReceiver); + unregisterReceiver(this.mInternalScreenEventReceiver); } catch (IllegalArgumentException e) { //ignored } @@ -4481,4 +4498,11 @@ public class XmppConnectionService extends Service { return XmppConnectionService.this; } } + + private class InternalEventReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + onStartCommand(intent, 0, 0); + } + } } |