Skip to content

Commit 338140f

Browse files
jsharkeyAndroid (Google) Code Review
authored andcommitted
Merge "Over-limit within handful of MTU's, update assets."
2 parents f005a2c + 50e7e51 commit 338140f

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

core/java/android/net/NetworkPolicy.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
4040
public long limitBytes;
4141
public long lastSnooze;
4242

43+
private static final long DEFAULT_MTU = 1500;
44+
4345
public NetworkPolicy(NetworkTemplate template, int cycleDay, long warningBytes, long limitBytes,
4446
long lastSnooze) {
4547
this.template = checkNotNull(template, "missing NetworkTemplate");
@@ -71,6 +73,17 @@ public int describeContents() {
7173
return 0;
7274
}
7375

76+
/**
77+
* Test if given measurement is near enough to {@link #limitBytes} to be
78+
* considered over-limit.
79+
*/
80+
public boolean isOverLimit(long totalBytes) {
81+
// over-estimate, since kernel will trigger limit once first packet
82+
// trips over limit.
83+
totalBytes += 2 * DEFAULT_MTU;
84+
return limitBytes != LIMIT_DISABLED && totalBytes >= limitBytes;
85+
}
86+
7487
/** {@inheritDoc} */
7588
public int compareTo(NetworkPolicy another) {
7689
if (another == null || another.limitBytes == LIMIT_DISABLED) {
1.03 KB
Loading
738 Bytes
Loading
1.38 KB
Loading

services/java/com/android/server/net/NetworkPolicyManagerService.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
import android.net.NetworkPolicy;
8686
import android.net.NetworkQuotaInfo;
8787
import android.net.NetworkState;
88-
import android.net.NetworkStats;
8988
import android.net.NetworkTemplate;
9089
import android.os.Binder;
9190
import android.os.Environment;
@@ -489,7 +488,7 @@ private void updateNotificationsLocked() {
489488
final long end = currentTime;
490489
final long totalBytes = getTotalBytes(policy.template, start, end);
491490

492-
if (policy.limitBytes != LIMIT_DISABLED && totalBytes >= policy.limitBytes) {
491+
if (policy.isOverLimit(totalBytes)) {
493492
if (policy.lastSnooze >= start) {
494493
enqueueNotification(policy, TYPE_LIMIT_SNOOZED, totalBytes);
495494
} else {
@@ -574,7 +573,7 @@ private void enqueueNotification(NetworkPolicy policy, int type, long totalBytes
574573
final CharSequence title = res.getText(R.string.data_usage_warning_title);
575574
final CharSequence body = res.getString(R.string.data_usage_warning_body);
576575

577-
builder.setSmallIcon(R.drawable.ic_menu_info_details);
576+
builder.setSmallIcon(R.drawable.stat_notify_error);
578577
builder.setTicker(title);
579578
builder.setContentTitle(title);
580579
builder.setContentText(body);
@@ -606,7 +605,7 @@ private void enqueueNotification(NetworkPolicy policy, int type, long totalBytes
606605
break;
607606
}
608607

609-
builder.setSmallIcon(com.android.internal.R.drawable.ic_menu_block);
608+
builder.setSmallIcon(R.drawable.stat_notify_disabled);
610609
builder.setTicker(title);
611610
builder.setContentTitle(title);
612611
builder.setContentText(body);
@@ -640,7 +639,7 @@ private void enqueueNotification(NetworkPolicy policy, int type, long totalBytes
640639
break;
641640
}
642641

643-
builder.setSmallIcon(R.drawable.ic_menu_info_details);
642+
builder.setSmallIcon(R.drawable.stat_notify_error);
644643
builder.setTicker(title);
645644
builder.setContentTitle(title);
646645
builder.setContentText(body);
@@ -677,7 +676,7 @@ private void enqueueRestrictedNotification(String tag) {
677676

678677
builder.setOnlyAlertOnce(true);
679678
builder.setOngoing(true);
680-
builder.setSmallIcon(R.drawable.ic_menu_info_details);
679+
builder.setSmallIcon(R.drawable.stat_notify_error);
681680
builder.setTicker(title);
682681
builder.setContentTitle(title);
683682
builder.setContentText(body);
@@ -750,8 +749,7 @@ private void updateNetworkEnabledLocked() {
750749
final long totalBytes = getTotalBytes(policy.template, start, end);
751750

752751
// disable data connection when over limit and not snoozed
753-
final boolean overLimit = policy.limitBytes != LIMIT_DISABLED
754-
&& totalBytes > policy.limitBytes && policy.lastSnooze < start;
752+
final boolean overLimit = policy.isOverLimit(totalBytes) && policy.lastSnooze < start;
755753
final boolean enabled = !overLimit;
756754

757755
setNetworkTemplateEnabled(policy.template, enabled);
@@ -1535,10 +1533,7 @@ private String getActiveSubscriberId() {
15351533

15361534
private long getTotalBytes(NetworkTemplate template, long start, long end) {
15371535
try {
1538-
final NetworkStats stats = mNetworkStats.getSummaryForNetwork(
1539-
template, start, end);
1540-
final NetworkStats.Entry entry = stats.getValues(0, null);
1541-
return entry.rxBytes + entry.txBytes;
1536+
return mNetworkStats.getSummaryForNetwork(template, start, end).getTotalBytes();
15421537
} catch (RemoteException e) {
15431538
// ignored; service lives in system_server
15441539
return 0;

0 commit comments

Comments
 (0)