replace away when screen off with away when locked

fixes #3978
This commit is contained in:
Daniel Gultsch 2021-02-19 15:59:53 +01:00
parent 53da64b7e2
commit 6bfe16f044
3 changed files with 17 additions and 27 deletions

View File

@ -368,8 +368,8 @@ public class NotificationService {
public void pushFailedDelivery(final Message message) { public void pushFailedDelivery(final Message message) {
final Conversation conversation = (Conversation) message.getConversation(); final Conversation conversation = (Conversation) message.getConversation();
final boolean isScreenOn = mXmppConnectionService.isInteractive(); final boolean isScreenLocked = !mXmppConnectionService.isScreenLocked();
if (this.mIsInForeground && isScreenOn && this.mOpenConversation == message.getConversation()) { 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"); Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": suppressing failed delivery notification because conversation is open");
return; return;
} }
@ -537,8 +537,8 @@ public class NotificationService {
Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": suppressing notification because turned off"); Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": suppressing notification because turned off");
return; return;
} }
final boolean isScreenOn = mXmppConnectionService.isInteractive(); final boolean isScreenLocked = mXmppConnectionService.isScreenLocked();
if (this.mIsInForeground && isScreenOn && this.mOpenConversation == message.getConversation()) { if (this.mIsInForeground && !isScreenLocked && this.mOpenConversation == message.getConversation()) {
Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": suppressing notification because conversation is open"); Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": suppressing notification because conversation is open");
return; return;
} }
@ -546,7 +546,7 @@ public class NotificationService {
pushToStack(message); pushToStack(message);
final Conversational conversation = message.getConversation(); final Conversational conversation = message.getConversation();
final Account account = conversation.getAccount(); 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() && !account.inGracePeriod()
&& !this.inMiniGracePeriod(account); && !this.inMiniGracePeriod(account);
updateNotification(doNotify, Collections.singletonList(conversation.getUuid())); updateNotification(doNotify, Collections.singletonList(conversation.getUuid()));

View File

@ -4,6 +4,7 @@ import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.KeyguardManager;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
@ -774,8 +775,9 @@ public class XmppConnectionService extends Service {
break; break;
case Intent.ACTION_SCREEN_ON: case Intent.ACTION_SCREEN_ON:
deactivateGracePeriod(); deactivateGracePeriod();
case Intent.ACTION_USER_PRESENT:
case Intent.ACTION_SCREEN_OFF: case Intent.ACTION_SCREEN_OFF:
if (awayWhenScreenOff()) { if (awayWhenScreenLocked()) {
refreshAllPresences(); refreshAllPresences();
} }
break; break;
@ -975,7 +977,7 @@ public class XmppConnectionService extends Service {
return getBooleanPreference(SettingsActivity.TREAT_VIBRATE_AS_SILENT, R.bool.treat_vibrate_as_silent); 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); 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() { private Presence.Status getTargetPresence() {
if (dndOnSilentMode() && isPhoneSilenced()) { if (dndOnSilentMode() && isPhoneSilenced()) {
return Presence.Status.DND; return Presence.Status.DND;
} else if (awayWhenScreenOff() && !isInteractive()) { } else if (awayWhenScreenLocked() && isScreenLocked()) {
return Presence.Status.AWAY; return Presence.Status.AWAY;
} else { } else {
return Presence.Status.ONLINE; return Presence.Status.ONLINE;
} }
} }
@SuppressLint("NewApi") public boolean isScreenLocked() {
@SuppressWarnings("deprecation") final KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
public boolean isInteractive() { return keyguardManager != null && keyguardManager.inKeyguardRestrictedInputMode();
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;
}
} }
private boolean isPhoneSilenced() { private boolean isPhoneSilenced() {
@ -1272,10 +1261,11 @@ public class XmppConnectionService extends Service {
} }
public void toggleScreenEventReceiver() { public void toggleScreenEventReceiver() {
if (awayWhenScreenOff() && !manuallyChangePresence()) { if (awayWhenScreenLocked() && !manuallyChangePresence()) {
final IntentFilter filter = new IntentFilter(); final IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
registerReceiver(this.mInternalScreenEventReceiver, filter); registerReceiver(this.mInternalScreenEventReceiver, filter);
} else { } else {
try { try {

View File

@ -463,8 +463,8 @@
<string name="account_status_host_unknown">The server is not responsible for this domain</string> <string name="account_status_host_unknown">The server is not responsible for this domain</string>
<string name="server_info_broken">Broken</string> <string name="server_info_broken">Broken</string>
<string name="pref_presence_settings">Availability</string> <string name="pref_presence_settings">Availability</string>
<string name="pref_away_when_screen_off">Away when screen is off</string> <string name="pref_away_when_screen_off">Away when device is locked</string>
<string name="pref_away_when_screen_off_summary">Show as Away when the screen is turned off</string> <string name="pref_away_when_screen_off_summary">Show as Away when the device is locked</string>
<string name="pref_dnd_on_silent_mode">Busy in silent mode</string> <string name="pref_dnd_on_silent_mode">Busy in silent mode</string>
<string name="pref_dnd_on_silent_mode_summary">Show as Busy when device is in silent mode</string> <string name="pref_dnd_on_silent_mode_summary">Show as Busy when device is in silent mode</string>
<string name="pref_treat_vibrate_as_silent">Treat vibrate as silent mode</string> <string name="pref_treat_vibrate_as_silent">Treat vibrate as silent mode</string>