Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find which apps are allowed under "Priority app notifications" within Do Not Disturb setting?

I am trying to programmatically find out for which apps the Do Not Disturb setting is bypassed exceptionally.

enter image description here

So far, I am using the following code to check whether the phone is set in Do not Disturb mode or not :

public static boolean isDnDModeEnabled(Context context)
{
    if(Build.VERSION.SDK_INT <23)
        return false;

    try {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        int filterValue = notificationManager.getCurrentInterruptionFilter();
        switch(filterValue)
        {
            case NotificationManager.INTERRUPTION_FILTER_ALL : Log.d("DND","Interruption filter all");
                break;
            case NotificationManager.INTERRUPTION_FILTER_ALARMS : Log.d("DND","Interruption filter alarms");
                break;
            case NotificationManager.INTERRUPTION_FILTER_PRIORITY : Log.d("DND","Interruption filter priority");
                break;
            case NotificationManager.INTERRUPTION_FILTER_UNKNOWN : Log.d("DND","Interruption filter unknown");
                break;
            case NotificationManager.INTERRUPTION_FILTER_NONE : Log.d("DND","Interruption filter none");
                break;

        }
        if(filterValue == NotificationManager.INTERRUPTION_FILTER_ALL)
            return false;
        else if(filterValue == NotificationManager.INTERRUPTION_FILTER_PRIORITY)
        {
            //Logic based on which apps are allowed as priority

            return true; //or false
        }
        else
            return true;
    }
    catch(Exception e)
    {
        return false;
    }
}

When I click on the Priority app notiications tab, I get a list of all installed apps for which I get to choose which apps to allow as priority exceptions.

My question is how to programmatically get the list of apps which are allowed as priority exceptions for Do Not Disturb mode, and thereby define the logic replacing the comment in the above code? Any solutions would be thoroughly appreciated.

like image 795
SoulRayder Avatar asked May 03 '17 17:05

SoulRayder


People also ask

Do not disturb priority only allows?

The Android settings vary by version and device, but you can usually get to the Do Not Disturb controls by swiping down from the top of the screen to the Quick Settings box. Tap the Do Not Disturb icon, and then tap More Settings. Select the Priority Only Allows option, and on the next screen tap Calls.

How do you add a app in Do Not Disturb list?

Do Not Disturb Mode Settings. You can access these settings by clicking Menu ▸ Settings, then navigating to Performance ▸ Do Not Disturb Mode. Clicking Don't block notifications from these apps will open the list of apps that are currently not being blocked; additional apps can be added to the list by clicking Add app.


1 Answers

You're looking for a way to determine which apps on the system have a notification importance of Notification.IMPORTANCE_MAX and, sorry to say but this is not possible by a non-system app. You need access to the INotificationService so you can call getImportance(packageName). See the Notification Manager source but it's guarded by a check that ensures you're a system app or the app whose package you passed so reflection is out...

Google allows an app to obtain its own notification importance through the NotificationManager with getImportance() (see docs) but you can't call it with an arbitrary package.

The other answer here references checking out the source from the system settings app and that's exactly what I did and, after a while of tracing through the code, I discovered how they determine which apps should show up in the "Overrides Do Not Disturb" menu by this code here which led me down the path of discovering how we could determine the IMPORTANCE_*

Sorry man, but the suggestions made by the other answerer are also not going to work because they are either incorrect (packages.xml doesn't have the info) or are going to require root which isn't reliable on all devices.

like image 198
Reuben Tanner Avatar answered Oct 13 '22 01:10

Reuben Tanner