From 6bfe16f044d608ecf7dc0bbbe999ec452d541b57 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 19 Feb 2021 15:59:53 +0100 Subject: [PATCH] replace away when screen off with away when locked fixes #3978 --- .../services/NotificationService.java | 10 +++---- .../services/XmppConnectionService.java | 30 +++++++------------ src/main/res/values/strings.xml | 4 +-- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index c7384b5fb..c478e4ccd 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -368,8 +368,8 @@ public class NotificationService { public void pushFailedDelivery(final Message message) { final Conversation conversation = (Conversation) message.getConversation(); - final boolean isScreenOn = mXmppConnectionService.isInteractive(); - if (this.mIsInForeground && isScreenOn && this.mOpenConversation == message.getConversation()) { + final boolean isScreenLocked = !mXmppConnectionService.isScreenLocked(); + if (this.mIsInForeground && isScreenLocked && this.mOpenConversation == message.getConversation()) { Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": suppressing failed delivery notification because conversation is open"); return; } @@ -537,8 +537,8 @@ public class NotificationService { Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": suppressing notification because turned off"); return; } - final boolean isScreenOn = mXmppConnectionService.isInteractive(); - if (this.mIsInForeground && isScreenOn && this.mOpenConversation == message.getConversation()) { + final boolean isScreenLocked = mXmppConnectionService.isScreenLocked(); + if (this.mIsInForeground && !isScreenLocked && this.mOpenConversation == message.getConversation()) { Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": suppressing notification because conversation is open"); return; } @@ -546,7 +546,7 @@ public class NotificationService { pushToStack(message); final Conversational conversation = message.getConversation(); final Account account = conversation.getAccount(); - final boolean doNotify = (!(this.mIsInForeground && this.mOpenConversation == null) || !isScreenOn) + final boolean doNotify = (!(this.mIsInForeground && this.mOpenConversation == null) || isScreenLocked) && !account.inGracePeriod() && !this.inMiniGracePeriod(account); updateNotification(doNotify, Collections.singletonList(conversation.getUuid())); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 65b082abe..4d100c0a5 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -4,6 +4,7 @@ import android.Manifest; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.AlarmManager; +import android.app.KeyguardManager; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; @@ -774,8 +775,9 @@ public class XmppConnectionService extends Service { break; case Intent.ACTION_SCREEN_ON: deactivateGracePeriod(); + case Intent.ACTION_USER_PRESENT: case Intent.ACTION_SCREEN_OFF: - if (awayWhenScreenOff()) { + if (awayWhenScreenLocked()) { refreshAllPresences(); } break; @@ -975,7 +977,7 @@ public class XmppConnectionService extends Service { return getBooleanPreference(SettingsActivity.TREAT_VIBRATE_AS_SILENT, R.bool.treat_vibrate_as_silent); } - private boolean awayWhenScreenOff() { + private boolean awayWhenScreenLocked() { return getBooleanPreference(SettingsActivity.AWAY_WHEN_SCREEN_IS_OFF, R.bool.away_when_screen_off); } @@ -986,29 +988,16 @@ public class XmppConnectionService extends Service { private Presence.Status getTargetPresence() { if (dndOnSilentMode() && isPhoneSilenced()) { return Presence.Status.DND; - } else if (awayWhenScreenOff() && !isInteractive()) { + } else if (awayWhenScreenLocked() && isScreenLocked()) { return Presence.Status.AWAY; } else { return Presence.Status.ONLINE; } } - @SuppressLint("NewApi") - @SuppressWarnings("deprecation") - public boolean isInteractive() { - try { - final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); - - final boolean isScreenOn; - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - isScreenOn = pm.isScreenOn(); - } else { - isScreenOn = pm.isInteractive(); - } - return isScreenOn; - } catch (RuntimeException e) { - return false; - } + public boolean isScreenLocked() { + final KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); + return keyguardManager != null && keyguardManager.inKeyguardRestrictedInputMode(); } private boolean isPhoneSilenced() { @@ -1272,10 +1261,11 @@ public class XmppConnectionService extends Service { } public void toggleScreenEventReceiver() { - if (awayWhenScreenOff() && !manuallyChangePresence()) { + if (awayWhenScreenLocked() && !manuallyChangePresence()) { final IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); + filter.addAction(Intent.ACTION_USER_PRESENT); registerReceiver(this.mInternalScreenEventReceiver, filter); } else { try { diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index d1d803688..ace5e4061 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -463,8 +463,8 @@ The server is not responsible for this domain Broken Availability - Away when screen is off - Show as Away when the screen is turned off + Away when device is locked + Show as Away when the device is locked Busy in silent mode Show as Busy when device is in silent mode Treat vibrate as silent mode