@gachlab/capacitor-dnd-plugin 0.0.3 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,32 +2,34 @@ package com.gachlab.capacitor.dnd;
|
|
|
2
2
|
|
|
3
3
|
import static android.Manifest.permission.ACCESS_NOTIFICATION_POLICY;
|
|
4
4
|
|
|
5
|
-
import android.Manifest;
|
|
6
5
|
import android.app.NotificationManager;
|
|
7
6
|
import android.content.Context;
|
|
8
|
-
import android.content.Intent;
|
|
9
7
|
import android.os.Build;
|
|
10
8
|
import android.provider.Settings;
|
|
11
9
|
|
|
12
10
|
import androidx.annotation.RequiresApi;
|
|
13
|
-
import androidx.core.content.ContextCompat;
|
|
14
11
|
|
|
15
12
|
import com.getcapacitor.JSObject;
|
|
13
|
+
import com.getcapacitor.PermissionState;
|
|
16
14
|
import com.getcapacitor.Plugin;
|
|
17
15
|
import com.getcapacitor.PluginCall;
|
|
18
16
|
import com.getcapacitor.PluginMethod;
|
|
19
17
|
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
20
18
|
import com.getcapacitor.annotation.Permission;
|
|
19
|
+
import com.getcapacitor.annotation.PermissionCallback;
|
|
21
20
|
|
|
22
21
|
import java.util.Timer;
|
|
23
22
|
import java.util.TimerTask;
|
|
24
23
|
|
|
25
24
|
@RequiresApi(api = Build.VERSION_CODES.M)
|
|
26
|
-
@CapacitorPlugin(
|
|
25
|
+
@CapacitorPlugin(
|
|
26
|
+
name = "DoNotDisturb",
|
|
27
27
|
permissions = {
|
|
28
28
|
@Permission(alias = "notifications-policy", strings = {ACCESS_NOTIFICATION_POLICY})
|
|
29
29
|
})
|
|
30
30
|
public class DoNotDisturbPlugin extends Plugin {
|
|
31
|
+
private NotificationManager notificationManager = null;
|
|
32
|
+
|
|
31
33
|
@PluginMethod(returnType = PluginMethod.RETURN_CALLBACK)
|
|
32
34
|
public void monitor(PluginCall call) {
|
|
33
35
|
call.setKeepAlive(true);
|
|
@@ -50,23 +52,33 @@ public class DoNotDisturbPlugin extends Plugin {
|
|
|
50
52
|
}, 5000, 10000);
|
|
51
53
|
}
|
|
52
54
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
|
|
56
|
+
public void toggleDoNotDisturb(PluginCall call) {
|
|
57
|
+
this.notificationManager = (NotificationManager) getActivity().getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
|
|
55
58
|
// Check if the notification policy access has been granted for the app.
|
|
56
59
|
if (!notificationManager.isNotificationPolicyAccessGranted()) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
requestPermissionForAlias("notifications-policy", call, "doNotDisturbPermsCallback");
|
|
61
|
+
|
|
62
|
+
} else {
|
|
63
|
+
ToggleDoNotDisturb(call);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@PermissionCallback
|
|
68
|
+
private void doNotDisturbPermsCallback(PluginCall call) {
|
|
69
|
+
if (getPermissionState("notifications-policy") == PermissionState.GRANTED) {
|
|
70
|
+
ToggleDoNotDisturb(call);
|
|
71
|
+
} else {
|
|
72
|
+
call.reject("Permission is required to take a picture");
|
|
61
73
|
}
|
|
62
|
-
ToggleDoNotDisturb(notificationManager);
|
|
63
74
|
}
|
|
64
75
|
|
|
65
|
-
private void ToggleDoNotDisturb(
|
|
76
|
+
private void ToggleDoNotDisturb(PluginCall call) {
|
|
66
77
|
if (notificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_ALL) {
|
|
67
78
|
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
|
|
68
79
|
} else {
|
|
69
80
|
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
|
|
70
81
|
}
|
|
82
|
+
call.resolve();
|
|
71
83
|
}
|
|
72
84
|
}
|