Merge "Try to deduplicate observer iteration code" am: c8223d0874

am: c822d054a3

Change-Id: Icfa3b57630a6d8d31f68f7d11d09e7ee0b97cc73
This commit is contained in:
Erik Kline
2017-01-19 11:22:55 +00:00
committed by android-build-merger

View File

@@ -45,6 +45,7 @@ import static com.android.server.NetworkManagementService.NetdResponseCode.Tethe
import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsListResult; import static com.android.server.NetworkManagementService.NetdResponseCode.TetheringStatsListResult;
import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult; import static com.android.server.NetworkManagementService.NetdResponseCode.TtyListResult;
import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED; import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.app.ActivityManagerNative; import android.app.ActivityManagerNative;
import android.content.ContentResolver; import android.content.ContentResolver;
@@ -373,15 +374,17 @@ public class NetworkManagementService extends INetworkManagementService.Stub
mObservers.unregister(observer); mObservers.unregister(observer);
} }
/** @FunctionalInterface
* Notify our observers of an interface status change private interface NetworkManagementEventCallback {
*/ public void sendCallback(INetworkManagementEventObserver o) throws RemoteException;
private void notifyInterfaceStatusChanged(String iface, boolean up) { }
private void invokeForAllObservers(NetworkManagementEventCallback eventCallback) {
final int length = mObservers.beginBroadcast(); final int length = mObservers.beginBroadcast();
try { try {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
try { try {
mObservers.getBroadcastItem(i).interfaceStatusChanged(iface, up); eventCallback.sendCallback(mObservers.getBroadcastItem(i));
} catch (RemoteException | RuntimeException e) { } catch (RemoteException | RuntimeException e) {
} }
} }
@@ -390,39 +393,26 @@ public class NetworkManagementService extends INetworkManagementService.Stub
} }
} }
/**
* Notify our observers of an interface status change
*/
private void notifyInterfaceStatusChanged(String iface, boolean up) {
invokeForAllObservers(o -> o.interfaceStatusChanged(iface, up));
}
/** /**
* Notify our observers of an interface link state change * Notify our observers of an interface link state change
* (typically, an Ethernet cable has been plugged-in or unplugged). * (typically, an Ethernet cable has been plugged-in or unplugged).
*/ */
private void notifyInterfaceLinkStateChanged(String iface, boolean up) { private void notifyInterfaceLinkStateChanged(String iface, boolean up) {
final int length = mObservers.beginBroadcast(); invokeForAllObservers(o -> o.interfaceLinkStateChanged(iface, up));
try {
for (int i = 0; i < length; i++) {
try {
mObservers.getBroadcastItem(i).interfaceLinkStateChanged(iface, up);
} catch (RemoteException | RuntimeException e) {
}
}
} finally {
mObservers.finishBroadcast();
}
} }
/** /**
* Notify our observers of an interface addition. * Notify our observers of an interface addition.
*/ */
private void notifyInterfaceAdded(String iface) { private void notifyInterfaceAdded(String iface) {
final int length = mObservers.beginBroadcast(); invokeForAllObservers(o -> o.interfaceAdded(iface));
try {
for (int i = 0; i < length; i++) {
try {
mObservers.getBroadcastItem(i).interfaceAdded(iface);
} catch (RemoteException | RuntimeException e) {
}
}
} finally {
mObservers.finishBroadcast();
}
} }
/** /**
@@ -434,34 +424,14 @@ public class NetworkManagementService extends INetworkManagementService.Stub
mActiveAlerts.remove(iface); mActiveAlerts.remove(iface);
mActiveQuotas.remove(iface); mActiveQuotas.remove(iface);
final int length = mObservers.beginBroadcast(); invokeForAllObservers(o -> o.interfaceRemoved(iface));
try {
for (int i = 0; i < length; i++) {
try {
mObservers.getBroadcastItem(i).interfaceRemoved(iface);
} catch (RemoteException | RuntimeException e) {
}
}
} finally {
mObservers.finishBroadcast();
}
} }
/** /**
* Notify our observers of a limit reached. * Notify our observers of a limit reached.
*/ */
private void notifyLimitReached(String limitName, String iface) { private void notifyLimitReached(String limitName, String iface) {
final int length = mObservers.beginBroadcast(); invokeForAllObservers(o -> o.limitReached(limitName, iface));
try {
for (int i = 0; i < length; i++) {
try {
mObservers.getBroadcastItem(i).limitReached(limitName, iface);
} catch (RemoteException | RuntimeException e) {
}
}
} finally {
mObservers.finishBroadcast();
}
} }
/** /**
@@ -508,18 +478,9 @@ public class NetworkManagementService extends INetworkManagementService.Stub
// on the mobile network, that is not coming from the radio itself, and we // on the mobile network, that is not coming from the radio itself, and we
// have previously seen change reports from the radio. In that case only // have previously seen change reports from the radio. In that case only
// the radio is the authority for the current state. // the radio is the authority for the current state.
final int length = mObservers.beginBroadcast(); final boolean active = isActive;
try { invokeForAllObservers(o -> o.interfaceClassDataActivityChanged(
for (int i = 0; i < length; i++) { Integer.toString(type), active, tsNanos));
try {
mObservers.getBroadcastItem(i).interfaceClassDataActivityChanged(
Integer.toString(type), isActive, tsNanos);
} catch (RemoteException | RuntimeException e) {
}
}
} finally {
mObservers.finishBroadcast();
}
} }
boolean report = false; boolean report = false;
@@ -691,72 +652,31 @@ public class NetworkManagementService extends INetworkManagementService.Stub
* Notify our observers of a new or updated interface address. * Notify our observers of a new or updated interface address.
*/ */
private void notifyAddressUpdated(String iface, LinkAddress address) { private void notifyAddressUpdated(String iface, LinkAddress address) {
final int length = mObservers.beginBroadcast(); invokeForAllObservers(o -> o.addressUpdated(iface, address));
try {
for (int i = 0; i < length; i++) {
try {
mObservers.getBroadcastItem(i).addressUpdated(iface, address);
} catch (RemoteException | RuntimeException e) {
}
}
} finally {
mObservers.finishBroadcast();
}
} }
/** /**
* Notify our observers of a deleted interface address. * Notify our observers of a deleted interface address.
*/ */
private void notifyAddressRemoved(String iface, LinkAddress address) { private void notifyAddressRemoved(String iface, LinkAddress address) {
final int length = mObservers.beginBroadcast(); invokeForAllObservers(o -> o.addressRemoved(iface, address));
try {
for (int i = 0; i < length; i++) {
try {
mObservers.getBroadcastItem(i).addressRemoved(iface, address);
} catch (RemoteException | RuntimeException e) {
}
}
} finally {
mObservers.finishBroadcast();
}
} }
/** /**
* Notify our observers of DNS server information received. * Notify our observers of DNS server information received.
*/ */
private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) { private void notifyInterfaceDnsServerInfo(String iface, long lifetime, String[] addresses) {
final int length = mObservers.beginBroadcast(); invokeForAllObservers(o -> o.interfaceDnsServerInfo(iface, lifetime, addresses));
try {
for (int i = 0; i < length; i++) {
try {
mObservers.getBroadcastItem(i).interfaceDnsServerInfo(iface, lifetime,
addresses);
} catch (RemoteException | RuntimeException e) {
}
}
} finally {
mObservers.finishBroadcast();
}
} }
/** /**
* Notify our observers of a route change. * Notify our observers of a route change.
*/ */
private void notifyRouteChange(String action, RouteInfo route) { private void notifyRouteChange(String action, RouteInfo route) {
final int length = mObservers.beginBroadcast(); if (action.equals("updated")) {
try { invokeForAllObservers(o -> o.routeUpdated(route));
for (int i = 0; i < length; i++) { } else {
try { invokeForAllObservers(o -> o.routeRemoved(route));
if (action.equals("updated")) {
mObservers.getBroadcastItem(i).routeUpdated(route);
} else {
mObservers.getBroadcastItem(i).routeRemoved(route);
}
} catch (RemoteException | RuntimeException e) {
}
}
} finally {
mObservers.finishBroadcast();
} }
} }