Merge "[IT4.4] Host network activity related logic into a new class"
This commit is contained in:
@@ -1115,11 +1115,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
userAllContext.registerReceiver(
|
userAllContext.registerReceiver(
|
||||||
mIntentReceiver, intentFilter, NETWORK_STACK, mHandler);
|
mIntentReceiver, intentFilter, NETWORK_STACK, mHandler);
|
||||||
|
|
||||||
try {
|
mNetworkActivityTracker = new LegacyNetworkActivityTracker(mContext, mNMS);
|
||||||
mNMS.registerObserver(mDataActivityObserver);
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
loge("Error registering observer :" + e);
|
|
||||||
}
|
|
||||||
|
|
||||||
mSettingsObserver = new SettingsObserver(mContext, mHandler);
|
mSettingsObserver = new SettingsObserver(mContext, mHandler);
|
||||||
registerSettingsCallbacks();
|
registerSettingsCallbacks();
|
||||||
@@ -1802,30 +1798,6 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private INetworkManagementEventObserver mDataActivityObserver = new BaseNetworkObserver() {
|
|
||||||
@Override
|
|
||||||
public void interfaceClassDataActivityChanged(int transportType, boolean active,
|
|
||||||
long tsNanos, int uid) {
|
|
||||||
sendDataActivityBroadcast(transportTypeToLegacyType(transportType), active, tsNanos);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// This is deprecated and only to support legacy use cases.
|
|
||||||
private int transportTypeToLegacyType(int type) {
|
|
||||||
switch (type) {
|
|
||||||
case NetworkCapabilities.TRANSPORT_CELLULAR:
|
|
||||||
return ConnectivityManager.TYPE_MOBILE;
|
|
||||||
case NetworkCapabilities.TRANSPORT_WIFI:
|
|
||||||
return ConnectivityManager.TYPE_WIFI;
|
|
||||||
case NetworkCapabilities.TRANSPORT_BLUETOOTH:
|
|
||||||
return ConnectivityManager.TYPE_BLUETOOTH;
|
|
||||||
case NetworkCapabilities.TRANSPORT_ETHERNET:
|
|
||||||
return ConnectivityManager.TYPE_ETHERNET;
|
|
||||||
default:
|
|
||||||
loge("Unexpected transport in transportTypeToLegacyType: " + type);
|
|
||||||
}
|
|
||||||
return ConnectivityManager.TYPE_NONE;
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* Ensures that the system cannot call a particular method.
|
* Ensures that the system cannot call a particular method.
|
||||||
*/
|
*/
|
||||||
@@ -2274,20 +2246,6 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
sendStickyBroadcast(makeGeneralIntent(info, bcastType));
|
sendStickyBroadcast(makeGeneralIntent(info, bcastType));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendDataActivityBroadcast(int deviceType, boolean active, long tsNanos) {
|
|
||||||
Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE);
|
|
||||||
intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType);
|
|
||||||
intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active);
|
|
||||||
intent.putExtra(ConnectivityManager.EXTRA_REALTIME_NS, tsNanos);
|
|
||||||
final long ident = Binder.clearCallingIdentity();
|
|
||||||
try {
|
|
||||||
mContext.sendOrderedBroadcastAsUser(intent, UserHandle.ALL,
|
|
||||||
RECEIVE_DATA_ACTIVITY_CHANGE, null, null, 0, null, null);
|
|
||||||
} finally {
|
|
||||||
Binder.restoreCallingIdentity(ident);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendStickyBroadcast(Intent intent) {
|
private void sendStickyBroadcast(Intent intent) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (!mSystemReady
|
if (!mSystemReady
|
||||||
@@ -2392,74 +2350,6 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Setup data activity tracking for the given network.
|
|
||||||
*
|
|
||||||
* Every {@code setupDataActivityTracking} should be paired with a
|
|
||||||
* {@link #removeDataActivityTracking} for cleanup.
|
|
||||||
*/
|
|
||||||
private void setupDataActivityTracking(NetworkAgentInfo networkAgent) {
|
|
||||||
final String iface = networkAgent.linkProperties.getInterfaceName();
|
|
||||||
|
|
||||||
final int timeout;
|
|
||||||
final int type;
|
|
||||||
|
|
||||||
if (networkAgent.networkCapabilities.hasTransport(
|
|
||||||
NetworkCapabilities.TRANSPORT_CELLULAR)) {
|
|
||||||
timeout = Settings.Global.getInt(mContext.getContentResolver(),
|
|
||||||
Settings.Global.DATA_ACTIVITY_TIMEOUT_MOBILE,
|
|
||||||
10);
|
|
||||||
type = NetworkCapabilities.TRANSPORT_CELLULAR;
|
|
||||||
} else if (networkAgent.networkCapabilities.hasTransport(
|
|
||||||
NetworkCapabilities.TRANSPORT_WIFI)) {
|
|
||||||
timeout = Settings.Global.getInt(mContext.getContentResolver(),
|
|
||||||
Settings.Global.DATA_ACTIVITY_TIMEOUT_WIFI,
|
|
||||||
15);
|
|
||||||
type = NetworkCapabilities.TRANSPORT_WIFI;
|
|
||||||
} else {
|
|
||||||
return; // do not track any other networks
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timeout > 0 && iface != null) {
|
|
||||||
try {
|
|
||||||
mNMS.addIdleTimer(iface, timeout, type);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// You shall not crash!
|
|
||||||
loge("Exception in setupDataActivityTracking " + e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove data activity tracking when network disconnects.
|
|
||||||
*/
|
|
||||||
private void removeDataActivityTracking(NetworkAgentInfo networkAgent) {
|
|
||||||
final String iface = networkAgent.linkProperties.getInterfaceName();
|
|
||||||
final NetworkCapabilities caps = networkAgent.networkCapabilities;
|
|
||||||
|
|
||||||
if (iface != null && (caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ||
|
|
||||||
caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))) {
|
|
||||||
try {
|
|
||||||
// the call fails silently if no idle timer setup for this interface
|
|
||||||
mNMS.removeIdleTimer(iface);
|
|
||||||
} catch (Exception e) {
|
|
||||||
loge("Exception in removeDataActivityTracking " + e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update data activity tracking when network state is updated.
|
|
||||||
*/
|
|
||||||
private void updateDataActivityTracking(NetworkAgentInfo newNetwork,
|
|
||||||
NetworkAgentInfo oldNetwork) {
|
|
||||||
if (newNetwork != null) {
|
|
||||||
setupDataActivityTracking(newNetwork);
|
|
||||||
}
|
|
||||||
if (oldNetwork != null) {
|
|
||||||
removeDataActivityTracking(oldNetwork);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* Reads the network specific MTU size from resources.
|
* Reads the network specific MTU size from resources.
|
||||||
* and set it on it's iface.
|
* and set it on it's iface.
|
||||||
@@ -3484,7 +3374,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
// the default network disconnecting. Find out why, fix the rematch code, and delete this.
|
// the default network disconnecting. Find out why, fix the rematch code, and delete this.
|
||||||
if (nai.isSatisfyingRequest(mDefaultRequest.requestId)) {
|
if (nai.isSatisfyingRequest(mDefaultRequest.requestId)) {
|
||||||
mDefaultNetworkNai = null;
|
mDefaultNetworkNai = null;
|
||||||
updateDataActivityTracking(null /* newNetwork */, nai);
|
mNetworkActivityTracker.updateDataActivityTracking(null /* newNetwork */, nai);
|
||||||
notifyLockdownVpn(nai);
|
notifyLockdownVpn(nai);
|
||||||
ensureNetworkTransitionWakelock(nai.toShortString());
|
ensureNetworkTransitionWakelock(nai.toShortString());
|
||||||
}
|
}
|
||||||
@@ -7314,7 +7204,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
if (oldDefaultNetwork != null) {
|
if (oldDefaultNetwork != null) {
|
||||||
mLingerMonitor.noteLingerDefaultNetwork(oldDefaultNetwork, newDefaultNetwork);
|
mLingerMonitor.noteLingerDefaultNetwork(oldDefaultNetwork, newDefaultNetwork);
|
||||||
}
|
}
|
||||||
updateDataActivityTracking(newDefaultNetwork, oldDefaultNetwork);
|
mNetworkActivityTracker.updateDataActivityTracking(
|
||||||
|
newDefaultNetwork, oldDefaultNetwork);
|
||||||
// Notify system services of the new default.
|
// Notify system services of the new default.
|
||||||
makeDefault(newDefaultNetwork);
|
makeDefault(newDefaultNetwork);
|
||||||
|
|
||||||
@@ -8668,4 +8559,145 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
|
|
||||||
notifyDataStallSuspected(p, network.getNetId());
|
notifyDataStallSuspected(p, network.getNetId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final LegacyNetworkActivityTracker mNetworkActivityTracker;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class used for updating network activity tracking with netd and notify network activity
|
||||||
|
* changes.
|
||||||
|
*/
|
||||||
|
private static final class LegacyNetworkActivityTracker {
|
||||||
|
private final Context mContext;
|
||||||
|
private final INetworkManagementService mNMS;
|
||||||
|
|
||||||
|
LegacyNetworkActivityTracker(@NonNull Context context,
|
||||||
|
@NonNull INetworkManagementService nms) {
|
||||||
|
mContext = context;
|
||||||
|
mNMS = nms;
|
||||||
|
try {
|
||||||
|
mNMS.registerObserver(mDataActivityObserver);
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
loge("Error registering observer :" + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Migrate away the dependency with INetworkManagementEventObserver.
|
||||||
|
private final INetworkManagementEventObserver mDataActivityObserver =
|
||||||
|
new BaseNetworkObserver() {
|
||||||
|
@Override
|
||||||
|
public void interfaceClassDataActivityChanged(int transportType, boolean active,
|
||||||
|
long tsNanos, int uid) {
|
||||||
|
sendDataActivityBroadcast(transportTypeToLegacyType(transportType), active,
|
||||||
|
tsNanos);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// This is deprecated and only to support legacy use cases.
|
||||||
|
private int transportTypeToLegacyType(int type) {
|
||||||
|
switch (type) {
|
||||||
|
case NetworkCapabilities.TRANSPORT_CELLULAR:
|
||||||
|
return ConnectivityManager.TYPE_MOBILE;
|
||||||
|
case NetworkCapabilities.TRANSPORT_WIFI:
|
||||||
|
return ConnectivityManager.TYPE_WIFI;
|
||||||
|
case NetworkCapabilities.TRANSPORT_BLUETOOTH:
|
||||||
|
return ConnectivityManager.TYPE_BLUETOOTH;
|
||||||
|
case NetworkCapabilities.TRANSPORT_ETHERNET:
|
||||||
|
return ConnectivityManager.TYPE_ETHERNET;
|
||||||
|
default:
|
||||||
|
loge("Unexpected transport in transportTypeToLegacyType: " + type);
|
||||||
|
}
|
||||||
|
return ConnectivityManager.TYPE_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendDataActivityBroadcast(int deviceType, boolean active, long tsNanos) {
|
||||||
|
final Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE);
|
||||||
|
intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType);
|
||||||
|
intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active);
|
||||||
|
intent.putExtra(ConnectivityManager.EXTRA_REALTIME_NS, tsNanos);
|
||||||
|
final long ident = Binder.clearCallingIdentity();
|
||||||
|
try {
|
||||||
|
mContext.sendOrderedBroadcastAsUser(intent, UserHandle.ALL,
|
||||||
|
RECEIVE_DATA_ACTIVITY_CHANGE,
|
||||||
|
null /* resultReceiver */,
|
||||||
|
null /* scheduler */,
|
||||||
|
0 /* initialCode */,
|
||||||
|
null /* initialData */,
|
||||||
|
null /* initialExtra */);
|
||||||
|
} finally {
|
||||||
|
Binder.restoreCallingIdentity(ident);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup data activity tracking for the given network.
|
||||||
|
*
|
||||||
|
* Every {@code setupDataActivityTracking} should be paired with a
|
||||||
|
* {@link #removeDataActivityTracking} for cleanup.
|
||||||
|
*/
|
||||||
|
private void setupDataActivityTracking(NetworkAgentInfo networkAgent) {
|
||||||
|
final String iface = networkAgent.linkProperties.getInterfaceName();
|
||||||
|
|
||||||
|
final int timeout;
|
||||||
|
final int type;
|
||||||
|
|
||||||
|
if (networkAgent.networkCapabilities.hasTransport(
|
||||||
|
NetworkCapabilities.TRANSPORT_CELLULAR)) {
|
||||||
|
timeout = Settings.Global.getInt(mContext.getContentResolver(),
|
||||||
|
Settings.Global.DATA_ACTIVITY_TIMEOUT_MOBILE,
|
||||||
|
10);
|
||||||
|
type = NetworkCapabilities.TRANSPORT_CELLULAR;
|
||||||
|
} else if (networkAgent.networkCapabilities.hasTransport(
|
||||||
|
NetworkCapabilities.TRANSPORT_WIFI)) {
|
||||||
|
timeout = Settings.Global.getInt(mContext.getContentResolver(),
|
||||||
|
Settings.Global.DATA_ACTIVITY_TIMEOUT_WIFI,
|
||||||
|
15);
|
||||||
|
type = NetworkCapabilities.TRANSPORT_WIFI;
|
||||||
|
} else {
|
||||||
|
return; // do not track any other networks
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timeout > 0 && iface != null) {
|
||||||
|
try {
|
||||||
|
// TODO: Access INetd directly instead of NMS
|
||||||
|
mNMS.addIdleTimer(iface, timeout, type);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// You shall not crash!
|
||||||
|
loge("Exception in setupDataActivityTracking " + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove data activity tracking when network disconnects.
|
||||||
|
*/
|
||||||
|
private void removeDataActivityTracking(NetworkAgentInfo networkAgent) {
|
||||||
|
final String iface = networkAgent.linkProperties.getInterfaceName();
|
||||||
|
final NetworkCapabilities caps = networkAgent.networkCapabilities;
|
||||||
|
|
||||||
|
if (iface != null && (caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
|
||||||
|
|| caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))) {
|
||||||
|
try {
|
||||||
|
// the call fails silently if no idle timer setup for this interface
|
||||||
|
// TODO: Access INetd directly instead of NMS
|
||||||
|
mNMS.removeIdleTimer(iface);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// You shall not crash!
|
||||||
|
loge("Exception in removeDataActivityTracking " + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update data activity tracking when network state is updated.
|
||||||
|
*/
|
||||||
|
public void updateDataActivityTracking(NetworkAgentInfo newNetwork,
|
||||||
|
NetworkAgentInfo oldNetwork) {
|
||||||
|
if (newNetwork != null) {
|
||||||
|
setupDataActivityTracking(newNetwork);
|
||||||
|
}
|
||||||
|
if (oldNetwork != null) {
|
||||||
|
removeDataActivityTracking(oldNetwork);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user