-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNotificationSystemStatusUtil.java
84 lines (68 loc) · 3.78 KB
/
NotificationSystemStatusUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.gg.notifications;
import android.annotation.TargetApi;
import android.app.AppOpsManager;
import android.content.Context;
import android.os.Build;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Utility for determining whether the user has disabled all of Chrome's notifications using the
* system's per-application settings.
*
* Enabling developers to show notifications with their own content creates a significant product
* risk: one spammy notification too many and the user might disable notifications for all of
* Chrome, which is obviously very bad. While we have a strong focus on providing clear attribution
* and ways of revoking notifications for a particular website, measuring this is still important.
*/
public class NotificationSystemStatusUtil {
/** Status codes returned by {@link determineAppNotificationsEnabled}. **/
public static final int APP_NOTIFICATIONS_STATUS_UNDETERMINABLE = 0;
public static final int APP_NOTIFICATIONS_STATUS_EXCEPTION = 1;
public static final int APP_NOTIFICATIONS_STATUS_ENABLED = 2;
public static final int APP_NOTIFICATIONS_STATUS_DISABLED = 3;
/** Must be set to the maximum value of the above values, plus one. **/
public static final int APP_NOTIFICATIONS_STATUS_BOUNDARY = 4;
/** Method name on the AppOpsManager class to check for a setting's value. **/
private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
/** The POST_NOTIFICATION operation understood by the AppOpsManager. **/
private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
/**
* Determines whether notifications are enabled for the app represented by |context|.
* Notifications may be disabled because either the user, or a management tool, has explicitly
* disallowed the Chrome App to display notifications.
*
* This check requires Android KitKat or later. Earlier versions will return an INDETERMINABLE
* status. When an exception occurs, an EXCEPTION status will be returned instead.
*
* @param context The context to check of whether it can show notifications.
* @return One of the APP_NOTIFICATION_STATUS_* constants defined in this class.
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static int determineAppNotificationStatus(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return APP_NOTIFICATIONS_STATUS_UNDETERMINABLE;
}
final String packageName = context.getPackageName();
final int uid = context.getApplicationInfo().uid;
final AppOpsManager appOpsManager =
(AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
try {
Class appOpsManagerClass = Class.forName(AppOpsManager.class.getName());
@SuppressWarnings("unchecked")
final Method checkOpNoThrowMethod =
appOpsManagerClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE,
String.class);
final Field opPostNotificationField =
appOpsManagerClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (Integer) opPostNotificationField.get(Integer.class);
int status = (Integer) checkOpNoThrowMethod.invoke(appOpsManager, value, uid, packageName);
return status == AppOpsManager.MODE_ALLOWED
? APP_NOTIFICATIONS_STATUS_ENABLED : APP_NOTIFICATIONS_STATUS_DISABLED;
} catch (RuntimeException e) {
} catch (Exception e) {
// Silently fail here, since this is just collecting statistics. The histogram includes
// a count for thrown exceptions, if that proves to be significant we can revisit.
}
return APP_NOTIFICATIONS_STATUS_EXCEPTION;
}
}