Merge "[SP28] Add API for set data warning" am: f30b5cc765

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1469125

Change-Id: Ibdf919a762952350dc89bda59724b2f0cb291313
This commit is contained in:
Junyu Lai
2021-03-29 11:30:37 +00:00
committed by Automerger Merge Worker
4 changed files with 59 additions and 13 deletions

View File

@@ -23,6 +23,6 @@ package android.net.netstats.provider;
*/ */
oneway interface INetworkStatsProvider { oneway interface INetworkStatsProvider {
void onRequestStatsUpdate(int token); void onRequestStatsUpdate(int token);
void onSetLimit(String iface, long quotaBytes);
void onSetAlert(long quotaBytes); void onSetAlert(long quotaBytes);
void onSetWarningAndLimit(String iface, long warningBytes, long limitBytes);
} }

View File

@@ -26,6 +26,6 @@ import android.net.NetworkStats;
oneway interface INetworkStatsProviderCallback { oneway interface INetworkStatsProviderCallback {
void notifyStatsUpdated(int token, in NetworkStats ifaceStats, in NetworkStats uidStats); void notifyStatsUpdated(int token, in NetworkStats ifaceStats, in NetworkStats uidStats);
void notifyAlertReached(); void notifyAlertReached();
void notifyLimitReached(); void notifyWarningOrLimitReached();
void unregister(); void unregister();
} }

View File

@@ -29,7 +29,8 @@ import android.os.RemoteException;
@SystemApi @SystemApi
public abstract class NetworkStatsProvider { public abstract class NetworkStatsProvider {
/** /**
* A value used by {@link #onSetLimit} and {@link #onSetAlert} indicates there is no limit. * A value used by {@link #onSetLimit}, {@link #onSetAlert} and {@link #onSetWarningAndLimit}
* indicates there is no limit.
*/ */
public static final int QUOTA_UNLIMITED = -1; public static final int QUOTA_UNLIMITED = -1;
@@ -42,13 +43,13 @@ public abstract class NetworkStatsProvider {
} }
@Override @Override
public void onSetLimit(String iface, long quotaBytes) { public void onSetAlert(long quotaBytes) {
NetworkStatsProvider.this.onSetLimit(iface, quotaBytes); NetworkStatsProvider.this.onSetAlert(quotaBytes);
} }
@Override @Override
public void onSetAlert(long quotaBytes) { public void onSetWarningAndLimit(String iface, long warningBytes, long limitBytes) {
NetworkStatsProvider.this.onSetAlert(quotaBytes); NetworkStatsProvider.this.onSetWarningAndLimit(iface, warningBytes, limitBytes);
} }
}; };
@@ -145,11 +146,28 @@ public abstract class NetworkStatsProvider {
} }
/** /**
* Notify system that the quota set by {@code onSetLimit} has been reached. * Notify system that the warning set by {@link #onSetWarningAndLimit} has been reached.
*
* @hide
*/
// TODO: Expose as system API.
public void notifyWarningReached() {
try {
// Reuse the code path to notify warning reached with limit reached
// since framework handles them in the same way.
getProviderCallbackBinderOrThrow().notifyWarningOrLimitReached();
} catch (RemoteException e) {
e.rethrowAsRuntimeException();
}
}
/**
* Notify system that the quota set by {@link #onSetLimit} or limit set by
* {@link #onSetWarningAndLimit} has been reached.
*/ */
public void notifyLimitReached() { public void notifyLimitReached() {
try { try {
getProviderCallbackBinderOrThrow().notifyLimitReached(); getProviderCallbackBinderOrThrow().notifyWarningOrLimitReached();
} catch (RemoteException e) { } catch (RemoteException e) {
e.rethrowAsRuntimeException(); e.rethrowAsRuntimeException();
} }
@@ -180,8 +198,34 @@ public abstract class NetworkStatsProvider {
* @param quotaBytes the quota defined as the number of bytes, starting from zero and counting * @param quotaBytes the quota defined as the number of bytes, starting from zero and counting
* from now. A value of {@link #QUOTA_UNLIMITED} indicates there is no limit. * from now. A value of {@link #QUOTA_UNLIMITED} indicates there is no limit.
*/ */
// TODO: deprecate this once onSetWarningAndLimit is ready.
public abstract void onSetLimit(@NonNull String iface, long quotaBytes); public abstract void onSetLimit(@NonNull String iface, long quotaBytes);
/**
* Called by {@code NetworkStatsService} when setting the interface quotas for the specified
* upstream interface. If a provider implements {@link #onSetWarningAndLimit}, the system
* will not call {@link #onSetLimit}. When this method is called, the implementation
* should behave as follows:
* 1. If {@code warningBytes} is reached on {@code iface}, block all further traffic on
* {@code iface} and call {@link NetworkStatsProvider@notifyWarningReached()}.
* 2. If {@code limitBytes} is reached on {@code iface}, block all further traffic on
* {@code iface} and call {@link NetworkStatsProvider#notifyLimitReached()}.
*
* @param iface the interface requiring the operation.
* @param warningBytes the warning defined as the number of bytes, starting from zero and
* counting from now. A value of {@link #QUOTA_UNLIMITED} indicates
* there is no warning.
* @param limitBytes the limit defined as the number of bytes, starting from zero and counting
* from now. A value of {@link #QUOTA_UNLIMITED} indicates there is no limit.
*
* @hide
*/
// TODO: Expose as system API.
public void onSetWarningAndLimit(@NonNull String iface, long warningBytes, long limitBytes) {
// Backward compatibility for those who didn't override this function.
onSetLimit(iface, limitBytes);
}
/** /**
* Called by {@code NetworkStatsService} when setting the alert bytes. Custom implementations * Called by {@code NetworkStatsService} when setting the alert bytes. Custom implementations
* MUST call {@link NetworkStatsProvider#notifyAlertReached()} when {@code quotaBytes} bytes * MUST call {@link NetworkStatsProvider#notifyAlertReached()} when {@code quotaBytes} bytes

View File

@@ -1695,7 +1695,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
@Override @Override
public void setStatsProviderLimitAsync(@NonNull String iface, long quota) { public void setStatsProviderLimitAsync(@NonNull String iface, long quota) {
if (LOGV) Slog.v(TAG, "setStatsProviderLimitAsync(" + iface + "," + quota + ")"); if (LOGV) Slog.v(TAG, "setStatsProviderLimitAsync(" + iface + "," + quota + ")");
invokeForAllStatsProviderCallbacks((cb) -> cb.mProvider.onSetLimit(iface, quota)); // TODO: Set warning accordingly.
invokeForAllStatsProviderCallbacks((cb) -> cb.mProvider.onSetWarningAndLimit(iface,
NetworkStatsProvider.QUOTA_UNLIMITED, quota));
} }
} }
@@ -2090,10 +2092,10 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
@Override @Override
public void notifyLimitReached() { public void notifyWarningOrLimitReached() {
Log.d(TAG, mTag + ": onLimitReached"); Log.d(TAG, mTag + ": notifyWarningOrLimitReached");
LocalServices.getService(NetworkPolicyManagerInternal.class) LocalServices.getService(NetworkPolicyManagerInternal.class)
.onStatsProviderLimitReached(mTag); .onStatsProviderWarningOrLimitReached(mTag);
} }
@Override @Override