Skip to content

Commit 9f7cbf0

Browse files
committed
API to report if active network is metered.
Report to developers if active network is "metered" and define it as the user being sensitive to heavy data usage. Bug: 3001465 Change-Id: I855ca3cd3eb1de3c4814148d70ccf24957af898a
1 parent a94afeb commit 9f7cbf0

File tree

6 files changed

+52
-9
lines changed

6 files changed

+52
-9
lines changed

api/current.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11811,6 +11811,7 @@ package android.net {
1181111811
method public deprecated boolean getBackgroundDataSetting();
1181211812
method public android.net.NetworkInfo getNetworkInfo(int);
1181311813
method public int getNetworkPreference();
11814+
method public boolean isActiveNetworkMetered();
1181411815
method public static boolean isNetworkTypeValid(int);
1181511816
method public boolean requestRouteToHost(int, int);
1181611817
method public void setNetworkPreference(int);

core/java/android/net/ConnectivityManager.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,11 @@ public int getNetworkPreference() {
373373
}
374374

375375
/**
376-
* Gets you info about the current data network.
377-
* Call {@link NetworkInfo#isConnected()} on the returned {@link NetworkInfo}
378-
* to check if the device has a data connection.
379-
*/
376+
* Returns details about the currently active data network. When connected,
377+
* this network is the default route for outgoing connections. You should
378+
* always check {@link NetworkInfo#isConnected()} before initiating network
379+
* traffic. This may return {@code null} when no networks are available.
380+
*/
380381
public NetworkInfo getActiveNetworkInfo() {
381382
try {
382383
return mService.getActiveNetworkInfo();
@@ -856,4 +857,19 @@ public boolean isNetworkSupported(int networkType) {
856857
} catch (RemoteException e) {}
857858
return false;
858859
}
860+
861+
/**
862+
* Returns if the currently active data network is metered. A network is
863+
* classified as metered when the user is sensitive to heavy data usage on
864+
* that connection. You should check this before doing large data transfers,
865+
* and warn the user or delay the operation until another network is
866+
* available.
867+
*/
868+
public boolean isActiveNetworkMetered() {
869+
try {
870+
return mService.isActiveNetworkMetered();
871+
} catch (RemoteException e) {
872+
return false;
873+
}
874+
}
859875
}

core/java/android/net/IConnectivityManager.aidl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ interface IConnectivityManager
5151
NetworkState[] getAllNetworkState();
5252

5353
NetworkQuotaInfo getActiveNetworkQuotaInfo();
54+
boolean isActiveNetworkMetered();
5455

5556
boolean setRadios(boolean onOff);
5657

core/java/android/net/INetworkPolicyManager.aidl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ interface INetworkPolicyManager {
5050
boolean getRestrictBackground();
5151

5252
NetworkQuotaInfo getNetworkQuotaInfo(in NetworkState state);
53+
boolean isNetworkMetered(in NetworkState state);
5354

5455
}

services/java/com/android/server/ConnectivityService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,19 @@ public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
875875
return null;
876876
}
877877

878+
@Override
879+
public boolean isActiveNetworkMetered() {
880+
enforceAccessPermission();
881+
final NetworkState state = getNetworkStateUnchecked(mActiveDefaultNetwork);
882+
if (state != null) {
883+
try {
884+
return mPolicyManager.isNetworkMetered(state);
885+
} catch (RemoteException e) {
886+
}
887+
}
888+
return false;
889+
}
890+
878891
public boolean setRadios(boolean turnOn) {
879892
boolean result = true;
880893
enforceChangePermission();

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,22 @@ private NetworkQuotaInfo getNetworkQuotaInfoUnchecked(NetworkState state) {
13721372
return new NetworkQuotaInfo(totalBytes, softLimitBytes, hardLimitBytes);
13731373
}
13741374

1375+
@Override
1376+
public boolean isNetworkMetered(NetworkState state) {
1377+
final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state);
1378+
1379+
final NetworkPolicy policy;
1380+
synchronized (mRulesLock) {
1381+
policy = findPolicyForNetworkLocked(ident);
1382+
}
1383+
1384+
if (policy != null) {
1385+
return policy.metered;
1386+
} else {
1387+
return false;
1388+
}
1389+
}
1390+
13751391
@Override
13761392
protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
13771393
mContext.enforceCallingOrSelfPermission(DUMP, TAG);
@@ -1796,11 +1812,6 @@ public void addIdleHandler(IdleHandler handler) {
17961812
mHandler.getLooper().getQueue().addIdleHandler(handler);
17971813
}
17981814

1799-
public static boolean isAirplaneModeOn(Context context) {
1800-
return Settings.System.getInt(
1801-
context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0;
1802-
}
1803-
18041815
private static void collectKeys(SparseIntArray source, SparseBooleanArray target) {
18051816
final int size = source.size();
18061817
for (int i = 0; i < size; i++) {

0 commit comments

Comments
 (0)