diff options
Diffstat (limited to 'services/core/java/com/android/server/net/NetworkPolicyManagerService.java')
-rw-r--r-- | services/core/java/com/android/server/net/NetworkPolicyManagerService.java | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/services/core/java/com/android/server/net/NetworkPolicyManagerService.java b/services/core/java/com/android/server/net/NetworkPolicyManagerService.java index f1d1c358b89..da623138348 100644 --- a/services/core/java/com/android/server/net/NetworkPolicyManagerService.java +++ b/services/core/java/com/android/server/net/NetworkPolicyManagerService.java @@ -203,6 +203,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { private static final int VERSION_LATEST = VERSION_SWITCH_UID; @VisibleForTesting + public static final int TYPE_NONE = 0; + @VisibleForTesting public static final int TYPE_WARNING = 0x1; @VisibleForTesting public static final int TYPE_LIMIT = 0x2; @@ -263,6 +265,9 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { private PowerManagerInternal mPowerManagerInternal; private IDeviceIdleController mDeviceIdleController; + private final ComponentName mNotificationComponent; + private int mNotificationSequenceNumber; + final Object mRulesLock = new Object(); volatile boolean mSystemReady; @@ -364,6 +369,11 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { mPolicyFile = new AtomicFile(new File(systemDir, "netpolicy.xml")); mAppOps = context.getSystemService(AppOpsManager.class); + + final String notificationComponent = context.getString( + R.string.config_networkPolicyNotificationComponent); + mNotificationComponent = notificationComponent != null + ? ComponentName.unflattenFromString(notificationComponent) : null; } public void bindConnectivityManager(IConnectivityManager connManager) { @@ -776,6 +786,11 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { final ArraySet<String> beforeNotifs = new ArraySet<String>(mActiveNotifs); mActiveNotifs.clear(); + // increment the sequence number so custom components know + // this update is new + mNotificationSequenceNumber++; + boolean hasNotifications = false; + // TODO: when switching to kernel notifications, compute next future // cycle boundary to recompute notifications. @@ -792,6 +807,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { final long totalBytes = getTotalBytes(policy.template, start, end); if (policy.isOverLimit(totalBytes)) { + hasNotifications = true; if (policy.lastLimitSnooze >= start) { enqueueNotification(policy, TYPE_LIMIT_SNOOZED, totalBytes); } else { @@ -805,10 +821,18 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { if (policy.isOverWarning(totalBytes) && policy.lastWarningSnooze < start && policy.limitBytes != LIMIT_DISABLED) { enqueueNotification(policy, TYPE_WARNING, totalBytes); + hasNotifications = true; } } } + // right now we don't care about restricted background notifications + // in the custom notification component, so trigger an update now + // if we didn't update anything this pass + if (!hasNotifications) { + sendNotificationToCustomComponent(null, TYPE_NONE, 0); + } + // ongoing notification when restricting background data if (mRestrictBackground) { enqueueRestrictedNotification(TAG_ALLOW_BACKGROUND); @@ -855,6 +879,12 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { * {@link NetworkPolicy#limitBytes}, potentially showing dialog to user. */ private void notifyOverLimitLocked(NetworkTemplate template) { + if (mNotificationComponent != null) { + // It is the job of the notification component to handle UI, + // so we do nothing here + return; + } + if (!mOverLimitNotified.contains(template)) { mContext.startActivity(buildNetworkOverLimitIntent(template)); mOverLimitNotified.add(template); @@ -873,11 +903,55 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { return TAG + ":" + policy.template.hashCode() + ":" + type; } + private boolean sendNotificationToCustomComponent( + NetworkPolicy policy, + int type, + long totalBytes) { + if (mNotificationComponent == null) { + return false; + } + + Intent intent = new Intent(); + intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND); + intent.setComponent(mNotificationComponent); + + int notificationType = NetworkPolicyManager.NOTIFICATION_TYPE_NONE; + switch (type) { + case TYPE_WARNING: + notificationType = NetworkPolicyManager.NOTIFICATION_TYPE_USAGE_WARNING; + break; + case TYPE_LIMIT: + notificationType = NetworkPolicyManager.NOTIFICATION_TYPE_USAGE_REACHED_LIMIT; + break; + case TYPE_LIMIT_SNOOZED: + notificationType = NetworkPolicyManager.NOTIFICATION_TYPE_USAGE_EXCEEDED_LIMIT; + break; + } + + intent.setAction(NetworkPolicyManager.ACTION_SHOW_NETWORK_POLICY_NOTIFICATION); + intent.putExtra(NetworkPolicyManager.EXTRA_NOTIFICATION_TYPE, notificationType); + intent.putExtra( + NetworkPolicyManager.EXTRA_NOTIFICATION_SEQUENCE_NUMBER, + mNotificationSequenceNumber); + + if (notificationType != NetworkPolicyManager.NOTIFICATION_TYPE_NONE) { + intent.putExtra(NetworkPolicyManager.EXTRA_NETWORK_POLICY, policy); + intent.putExtra(NetworkPolicyManager.EXTRA_BYTES_USED, totalBytes); + } + + mContext.sendBroadcast(intent); + return true; + } + /** * Show notification for combined {@link NetworkPolicy} and specific type, * like {@link #TYPE_LIMIT}. Okay to call multiple times. */ private void enqueueNotification(NetworkPolicy policy, int type, long totalBytes) { + if (sendNotificationToCustomComponent(policy, type, totalBytes)) { + return; + } + final String tag = buildNotificationTag(policy, type); final Notification.Builder builder = new Notification.Builder(mContext); builder.setOnlyAlertOnce(true); @@ -1737,6 +1811,19 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { } } + @Override + public void snoozeWarning(NetworkTemplate template) { + mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); + + final long token = Binder.clearCallingIdentity(); + try { + // TODO: this seems like a race condition? (along with snoozeLimit above) + performSnooze(template, TYPE_WARNING); + } finally { + Binder.restoreCallingIdentity(token); + } + } + void performSnooze(NetworkTemplate template, int type) { maybeRefreshTrustedTime(); final long currentTime = currentTimeMillis(); |