Home » Android » android – What's "AutomaticZenRule" ? What is it used for?

android – What's "AutomaticZenRule" ? What is it used for?

Posted by: admin May 14, 2020 Leave a comment

Questions:

Background

I just noticed some functions of NotificationManager that handle a class that’s called AutomaticZenRule :

https://developer.android.com/reference/android/app/NotificationManager.html#addAutomaticZenRule(android.app.AutomaticZenRule)

and others…

The problem

Looking at the docs of AutomaticZenRule, it still doesn’t tell much about what it is, and what can it be used for:

Rule instance information for zen mode.

What I tried

Searching the Internet, I can see just in a Commonsware blog post, that they wonder what it is:

It is unclear what AutomaticZenRule is …

There is practically nothing more that I’ve found about it. Not “zen mode” and not “AutomaticZenRule”.

The questions

  1. What is “zen mode” ?

  2. What is “AutomaticZenRule” , and what can I do with it? How is it related to notifications?

  3. Is there anything special on Android N, that this API was added on this version?

  4. Is there a sample for using it?

How to&Answers:

From digging in into the other documents available, i was able to understand ZenMode to some extent(although it can be my own version and not the correct one).

What my understanding is as follows –

Zen Mode is the Do not Disturb mode which now in latest updates can be enabled automatically which depends on factors such as late time of the day, etc. AutomaticZenrule can be used by applications who want their notifications to not be masked or suppressed when in do not disturb mode.

For this your application should make request to policy access by sending the user to the activity that matches the system intent action ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS.

If user has granted access to notification policy for your app, then you will be able to set a priority notification even in do not disturb mode. AutomaticZenrule thus plays a vital role to state the system that the application’s notifications not be suppressed.

Although, i dont have a running sample code for it, i guess it should be on similar lines like the enabling device admin code or requesting a permission use case.

Thanks to you i got to read something new 🙂

Answer:

Zen Mode is just another name for Do Not Disturb (DND) mode. Android can activate DND mode based on rules. These rules can be provided either by the system, or by a third-party app.

In the following screenshot you can see two system-provided rules, together with a “Driving” rule provided by the third-party app “Pixel Ambient Services”:

DND rules

AutomaticZenRule is there to integrate your own rules into the Android system. To integrate your own rules, you have to follow these rough steps:

  1. Make sure that you have sufficient permissions to access the DND policy (android.permission.ACCESS_NOTIFICATION_POLICY). See NotificationManager.isNotificationPolicyAccessGranted() for details.
  2. Add an activity for your rule:

     <activity android:name="MyRuleConfigurationActivity">
        <meta-data android:name="android.service.zen.automatic.ruleType" android:value="My Rule" />
        <intent-filter>
          <action android:name="android.app.action.AUTOMATIC_ZEN_RULE"/>
        </intent-filter>
     </activity>
    
  3. Android will show your activity whenever the user wants to create or edit a rule of the specified rule type. In the latter case, Android will supply the ID of the existing rule in NotificationManager#EXTRA_AUTOMATIC_RULE_ID. To propagate changes in your activity back to android, you need to construct an AutomaticZenRuleinstance and call NotificationManager.addAutomaticZenRule / updateAutomaticZenRule.

  4. After that, you can tell Android that the conditions for your rule are currently satisfied / not satisfied by calling NotificationManager.setAutomaticZenRuleState.