Skip to content

Commit db3c867

Browse files
author
Haoyu Bai
committed
Network data activity change intent for network interfaces.
The activity notification is received from netd, an intent DATA_ACTIVITY_CHANGE is then raised for other part of the system to consume. Change-Id: Idfcc4763c51c5b314c57f546c12557082f06bebf
1 parent 6b7358d commit db3c867

File tree

11 files changed

+106
-0
lines changed

11 files changed

+106
-0
lines changed

core/java/android/net/ConnectivityManager.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,28 @@ public class ConnectivityManager {
136136
*/
137137
public static final String EXTRA_INET_CONDITION = "inetCondition";
138138

139+
/**
140+
* Broadcast action to indicate the change of data activity status
141+
* (idle or active) on a network in a recent period.
142+
* The network becomes active when data transimission is started, or
143+
* idle if there is no data transimition for a period of time.
144+
* {@hide}
145+
*/
146+
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
147+
public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
148+
/**
149+
* The lookup key for an enum that indicates the network device type on which this data activity
150+
* change happens.
151+
* {@hide}
152+
*/
153+
public static final String EXTRA_DEVICE_TYPE = "deviceType";
154+
/**
155+
* The lookup key for a boolean that indicates the device is active or not. {@code true} means
156+
* it is actively sending or receiving data and {@code false} means it is idle.
157+
* {@hide}
158+
*/
159+
public static final String EXTRA_IS_ACTIVE = "isActive";
160+
139161
/**
140162
* Broadcast Action: The setting for background data usage has changed
141163
* values. Use {@link #getBackgroundDataSetting()} to get the current value.

core/java/android/net/EthernetDataTracker.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ public void interfaceRemoved(String iface) {
9999
public void limitReached(String limitName, String iface) {
100100
// Ignored.
101101
}
102+
103+
public void interfaceClassDataActivityChanged(String label, boolean active) {
104+
// Ignored.
105+
}
102106
}
103107

104108
private EthernetDataTracker() {

core/java/android/net/INetworkManagementEventObserver.aidl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,11 @@ interface INetworkManagementEventObserver {
6262
*/
6363
void limitReached(String limitName, String iface);
6464

65+
/**
66+
* Interface data activity status is changed.
67+
*
68+
* @param iface The interface.
69+
* @param active True if the interface is actively transmitting data, false if it is idle.
70+
*/
71+
void interfaceClassDataActivityChanged(String label, boolean active);
6572
}

core/res/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121

122122
<protected-broadcast android:name="android.net.conn.CONNECTIVITY_CHANGE" />
123123
<protected-broadcast android:name="android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE" />
124+
<protected-broadcast android:name="android.net.conn.DATA_ACTIVITY_CHANGE" />
124125

125126
<protected-broadcast android:name="android.nfc.action.LLCP_LINK_STATE_CHANGED" />
126127
<protected-broadcast android:name="com.android.nfc_extras.action.RF_FIELD_ON_DETECTED" />
@@ -508,6 +509,11 @@
508509
android:permissionGroup="android.permission-group.NETWORK"
509510
android:protectionLevel="signature|system" />
510511

512+
<!-- @hide -->
513+
<permission android:name="android.permission.RECEIVE_DATA_ACTIVITY_CHANGE"
514+
android:permissionGroup="android.permission-group.NETWORK"
515+
android:protectionLevel="signature|system" />
516+
511517
<!-- ================================== -->
512518
<!-- Permissions for accessing accounts -->
513519
<!-- ================================== -->

services/java/com/android/server/CommonTimeManagementService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public void interfaceRemoved(String iface) {
120120
reevaluateServiceState();
121121
}
122122
public void limitReached(String limitName, String iface) { }
123+
124+
public void interfaceClassDataActivityChanged(String label, boolean active) {}
123125
};
124126

125127
private BroadcastReceiver mConnectivityMangerObserver = new BroadcastReceiver() {

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.android.server;
1818

1919
import static android.Manifest.permission.MANAGE_NETWORK_POLICY;
20+
import static android.Manifest.permission.RECEIVE_DATA_ACTIVITY_CHANGE;
2021
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
2122
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
2223
import static android.net.ConnectivityManager.isNetworkTypeValid;
@@ -35,6 +36,7 @@
3536
import android.net.DummyDataStateTracker;
3637
import android.net.EthernetDataTracker;
3738
import android.net.IConnectivityManager;
39+
import android.net.INetworkManagementEventObserver;
3840
import android.net.INetworkPolicyListener;
3941
import android.net.INetworkPolicyManager;
4042
import android.net.INetworkStatsService;
@@ -546,6 +548,13 @@ public ConnectivityService(Context context, INetworkManagementService netd,
546548
mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
547549
mSettingsObserver.observe(mContext);
548550

551+
INetworkManagementEventObserver netdObserver = new NetdObserver();
552+
try {
553+
mNetd.registerObserver(netdObserver);
554+
} catch (RemoteException e) {
555+
loge("Error registering observer :" + e);
556+
}
557+
549558
loadGlobalProxy();
550559
}
551560
private NetworkStateTracker makeWimaxStateTracker() {
@@ -923,6 +932,19 @@ public boolean setRadio(int netType, boolean turnOn) {
923932
return tracker != null && tracker.setRadio(turnOn);
924933
}
925934

935+
private class NetdObserver extends INetworkManagementEventObserver.Stub {
936+
public void interfaceClassDataActivityChanged(String label, boolean active) {
937+
int deviceType = Integer.parseInt(label);
938+
sendDataActivityBroadcast(deviceType, active);
939+
}
940+
941+
public void interfaceStatusChanged(String iface, boolean up) {}
942+
public void interfaceLinkStateChanged(String iface, boolean up) {}
943+
public void interfaceAdded(String iface) {}
944+
public void interfaceRemoved(String iface) {}
945+
public void limitReached(String limitName, String iface) {}
946+
}
947+
926948
/**
927949
* Used to notice when the calling process dies so we can self-expire
928950
*
@@ -1759,6 +1781,13 @@ private void sendGeneralBroadcastDelayed(NetworkInfo info, String bcastType, int
17591781
sendStickyBroadcastDelayed(makeGeneralIntent(info, bcastType), delayMs);
17601782
}
17611783

1784+
private void sendDataActivityBroadcast(int deviceType, boolean active) {
1785+
Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE);
1786+
intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType);
1787+
intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active);
1788+
mContext.sendOrderedBroadcast(intent, RECEIVE_DATA_ACTIVITY_CHANGE);
1789+
}
1790+
17621791
/**
17631792
* Called when an attempt to fail over to another network has failed.
17641793
* @param info the {@link NetworkInfo} for the failed network

services/java/com/android/server/NetworkManagementService.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,20 @@ private void notifyLimitReached(String limitName, String iface) {
279279
mObservers.finishBroadcast();
280280
}
281281

282+
/**
283+
* Notify our observers of a change in the data activity state of the interface
284+
*/
285+
private void notifyInterfaceClassActivity(String label, boolean active) {
286+
final int length = mObservers.beginBroadcast();
287+
for (int i = 0; i < length; i++) {
288+
try {
289+
mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(label, active);
290+
} catch (RemoteException e) {
291+
}
292+
}
293+
mObservers.finishBroadcast();
294+
}
295+
282296
/**
283297
* Prepare native daemon once connected, enabling modules and pushing any
284298
* existing in-memory rules.
@@ -405,6 +419,19 @@ public boolean onEvent(int code, String raw, String[] cooked) {
405419
throw new IllegalStateException(
406420
String.format("Invalid event from daemon (%s)", raw));
407421
// break;
422+
case NetdResponseCode.InterfaceClassActivity:
423+
/*
424+
* An network interface class state changed (active/idle)
425+
* Format: "NNN IfaceClass <active/idle> <label>"
426+
*/
427+
if (cooked.length < 4 || !cooked[1].equals("IfaceClass")) {
428+
throw new IllegalStateException(
429+
String.format("Invalid event from daemon (%s)", raw));
430+
}
431+
boolean isActive = cooked[2].equals("active");
432+
notifyInterfaceClassActivity(cooked[3], isActive);
433+
return true;
434+
// break;
408435
default: break;
409436
}
410437
return false;

services/java/com/android/server/ThrottleService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ public void interfaceAdded(String iface) {
195195

196196
public void interfaceRemoved(String iface) {}
197197
public void limitReached(String limitName, String iface) {}
198+
public void interfaceClassDataActivityChanged(String label, boolean active) {}
198199
}
199200

200201

services/java/com/android/server/connectivity/Tethering.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ public void interfaceRemoved(String iface) {
319319

320320
public void limitReached(String limitName, String iface) {}
321321

322+
public void interfaceClassDataActivityChanged(String label, boolean active) {}
323+
322324
public int tether(String iface) {
323325
if (DBG) Log.d(TAG, "Tethering " + iface);
324326
TetherInterfaceSM sm = null;

services/java/com/android/server/connectivity/Vpn.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ public synchronized void interfaceRemoved(String interfaze) {
291291
public void limitReached(String limit, String interfaze) {
292292
}
293293

294+
public void interfaceClassDataActivityChanged(String label, boolean active) {
295+
}
296+
294297
private void enforceControlPermission() {
295298
// System user is allowed to control VPN.
296299
if (Binder.getCallingUid() == Process.SYSTEM_UID) {

0 commit comments

Comments
 (0)