listen for Android 6 DND events

This commit is contained in:
Daniel Gultsch 2018-09-06 23:17:37 +02:00
parent ac7f0032af
commit eadd0c5f7a

View file

@ -4,8 +4,10 @@ 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.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
@ -387,7 +389,8 @@ public class XmppConnectionService extends Service {
private WakeLock wakeLock; private WakeLock wakeLock;
private PowerManager pm; private PowerManager pm;
private LruCache<String, Bitmap> mBitmapCache; 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) { private static String generateFetchKey(Account account, final Avatar avatar) {
return account.getJid().asBareJid() + "_" + avatar.owner + "_" + avatar.sha1sum; return account.getJid().asBareJid() + "_" + avatar.owner + "_" + avatar.sha1sum;
@ -638,6 +641,7 @@ public class XmppConnectionService extends Service {
updateConversation(c); updateConversation(c);
}); });
case AudioManager.RINGER_MODE_CHANGED_ACTION: case AudioManager.RINGER_MODE_CHANGED_ACTION:
case NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED:
if (dndOnSilentMode()) { if (dndOnSilentMode()) {
refreshAllPresences(); refreshAllPresences();
} }
@ -862,16 +866,25 @@ public class XmppConnectionService extends Service {
} }
private boolean isPhoneSilenced() { 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 { try {
if (treatVibrateAsSilent()) { if (treatVibrateAsSilent()) {
return audioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL; return notificationDnd || ringerMode != AudioManager.RINGER_MODE_NORMAL;
} else { } else {
return audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT; return notificationDnd || ringerMode == AudioManager.RINGER_MODE_SILENT;
} }
} catch (Throwable throwable) { } catch (Throwable throwable) {
Log.d(Config.LOGTAG, "platform bug in isPhoneSilenced (" + throwable.getMessage() + ")"); Log.d(Config.LOGTAG, "platform bug in isPhoneSilenced (" + throwable.getMessage() + ")");
return false; return notificationDnd;
} }
} }
@ -1009,9 +1022,12 @@ public class XmppConnectionService extends Service {
toggleScreenEventReceiver(); toggleScreenEventReceiver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
scheduleNextIdlePing(); scheduleNextIdlePing();
} IntentFilter intentFilter = new IntentFilter();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
registerReceiver(this.mEventReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
}
intentFilter.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED);
registerReceiver(this.mInternalEventReceiver, intentFilter);
} }
} }
@ -1039,7 +1055,7 @@ public class XmppConnectionService extends Service {
@Override @Override
public void onDestroy() { public void onDestroy() {
try { try {
unregisterReceiver(this.mEventReceiver); unregisterReceiver(this.mInternalEventReceiver);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
//ignored //ignored
} }
@ -1054,12 +1070,13 @@ public class XmppConnectionService extends Service {
public void toggleScreenEventReceiver() { public void toggleScreenEventReceiver() {
if (awayWhenScreenOff() && !manuallyChangePresence()) { 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); filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(this.mEventReceiver, filter); registerReceiver(this.mInternalScreenEventReceiver, filter);
} else { } else {
try { try {
unregisterReceiver(this.mEventReceiver); unregisterReceiver(this.mInternalScreenEventReceiver);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
//ignored //ignored
} }
@ -4053,4 +4070,12 @@ public class XmppConnectionService extends Service {
return XmppConnectionService.this; return XmppConnectionService.this;
} }
} }
private class InternalEventReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
onStartCommand(intent,0,0);
}
}
} }