Skip to content

Instantly share code, notes, and snippets.

@noln
Last active August 27, 2022 19:25
Show Gist options
  • Save noln/0ba4c085c8b292e56577 to your computer and use it in GitHub Desktop.
Save noln/0ba4c085c8b292e56577 to your computer and use it in GitHub Desktop.
A basic activity that listens to zen_mode state (Android's "do not disturb" volume/vibration muting feature) and dumps the current value to log. There are two ways included; the first is an observer and doesn't work in every case (I think there's a separate preference for priority messages), the second may is a manual check by hitting a button.
import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.provider.Settings.Global;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.util.Locale;
import static android.provider.Settings.System.CONTENT_URI;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "ZenModeObserver";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get a handle on the button for manually checking the zen_mode state
Button manualCheck = (Button) findViewById(R.id.manual_check);
manualCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
logZenModeState();
}
catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
}
});
/*
Create an instance of the ZenModeObserver below (it's actually just a global settings change observer)
This listens for settings state changes and calls the logZenModeState() method
*/
ZenModeObserver observer = new ZenModeObserver(new Handler());
this.getApplicationContext().getContentResolver().registerContentObserver(CONTENT_URI, true, observer);
}
// Log the current zen_mode state
private void logZenModeState() throws Settings.SettingNotFoundException {
int zenModeValue = Global.getInt(getContentResolver(), "zen_mode");
switch (zenModeValue) {
case 0:
Log.e(TAG, "DnD : OFF");
break;
case 1:
Log.e(TAG, "DnD : ON - Priority Only");
break;
case 2:
Log.e(TAG, "DnD : ON - Total Silence");
break;
case 3:
Log.e(TAG, "DnD : ON - Alarms Only");
break;
default:
Log.e(TAG, String.format(Locale.getDefault(), "DnD : Some other value in the future? Or obscure mfctr implementation? [%d]", zenModeValue));
}
}
private class ZenModeObserver extends ContentObserver {
ZenModeObserver(Handler handler) {
super(handler);
}
@Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
try {
logZenModeState();
}
catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
}
}
}
@noln
Copy link
Author

noln commented Aug 27, 2022

@kyleclegg not a clue, sorry.

If I were to look into DND detection these days, I'd dig into the ZenPolicy class docs, as it seems some of that is documented since API level 29; it didn't even exist when I first wrote this.

https://developer.android.com/reference/android/service/notification/ZenPolicy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment