Search code examples
androidxiaomido-not-disturb

Why Do Not Disturb is not activated in Xiaomi devices?


I am setting the interruption filter using this code:

NotificationManager myNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
myNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY);

It works very well in every device, but, for some reason it doesn't in Xiaomi devices using Android 9. Do Not Disturb mode is not activated in those devices. It remains unchanged. If I ask the device what is the current interruption it answers with a value of 5. Which is an unknown and not documented value in Android Developer as stated here.

int iCurrentInterruption = oNotificationManager.getCurrentInterruptionFilter();

iCurrentInterruption has a value of 5. None of the following:

INTERRUPTION_FILTER_UNKNOWN = 0
INTERRUPTION_FILTER_ALL = 1
INTERRUPTION_FILTER_PRIORITY = 2
INTERRUPTION_FILTER_NONE = 3 
INTERRUPTION_FILTER_ALARMS = 4

Solution

  • I guess in Xiaomi devices the System takes longer to activate the interruption filter, so you cannot ask for the result just right away. So, I sleep for a second and ask after that. It also happens that sometimes it doesn't get activated at the first time so I have to call setInterruptionFilter a second time.

    Something like this:

    setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY)
    wait
    If INTERRUPTION_FILTER_PRIORITY==getCurrentInterruptionFilter() then return OK
    //Second try:
    setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY)
    wait
    If INTERRUPTION_FILTER_PRIORITY==getCurrentInterruptionFilter() then return Ok
    return error