Merge "Simplify UpstreamNetworkMonitor callback handling"

am: f9f5cf9d9b

Change-Id: I642567d81b6b8908b2ef7afea8d15628330481f9
This commit is contained in:
Erik Kline
2017-01-12 10:26:43 +00:00
committed by android-build-merger

View File

@@ -1076,15 +1076,11 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
} }
public void stop() { public void stop() {
if (mDefaultNetworkCallback != null) { releaseCallback(mDefaultNetworkCallback);
cm().unregisterNetworkCallback(mDefaultNetworkCallback); mDefaultNetworkCallback = null;
mDefaultNetworkCallback = null;
}
if (mDunTetheringCallback != null) { releaseCallback(mDunTetheringCallback);
cm().unregisterNetworkCallback(mDunTetheringCallback); mDunTetheringCallback = null;
mDunTetheringCallback = null;
}
mNetworkMap.clear(); mNetworkMap.clear();
} }
@@ -1093,88 +1089,87 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
return (network != null) ? mNetworkMap.get(network) : null; return (network != null) ? mNetworkMap.get(network) : null;
} }
public NetworkState processCallback(int arg1, Object obj) { private void handleAvailable(Network network) {
switch (arg1) { if (VDBG) {
case EVENT_ON_AVAILABLE: { Log.d(TAG, "EVENT_ON_AVAILABLE for " + network);
final Network network = (Network) obj;
if (VDBG) {
Log.d(TAG, "EVENT_ON_AVAILABLE for " + network);
}
if (!mNetworkMap.containsKey(network)) {
mNetworkMap.put(network,
new NetworkState(null, null, null, network, null, null));
}
final ConnectivityManager cm = cm();
if (mDefaultNetworkCallback != null) {
cm.requestNetworkCapabilities(mDefaultNetworkCallback);
cm.requestLinkProperties(mDefaultNetworkCallback);
}
// Requesting updates for mDunTetheringCallback is not
// necessary. Because it's a listen, it will already have
// heard all NetworkCapabilities and LinkProperties updates
// since UpstreamNetworkMonitor was started. Because we
// start UpstreamNetworkMonitor before chooseUpstreamType()
// is ever invoked (it can register a DUN request) this is
// mostly safe. However, if a DUN network is already up for
// some reason (unlikely, because DUN is restricted and,
// unless the DUN network is shared with another APN, only
// the system can request it and this is the only part of
// the system that requests it) we won't know its
// LinkProperties or NetworkCapabilities.
return mNetworkMap.get(network);
}
case EVENT_ON_CAPABILITIES: {
final NetworkState ns = (NetworkState) obj;
if (!mNetworkMap.containsKey(ns.network)) {
// Ignore updates for networks for which we have not yet
// received onAvailable() - which should never happen -
// or for which we have already received onLost().
return null;
}
if (VDBG) {
Log.d(TAG, String.format("EVENT_ON_CAPABILITIES for %s: %s",
ns.network, ns.networkCapabilities));
}
final NetworkState prev = mNetworkMap.get(ns.network);
mNetworkMap.put(ns.network,
new NetworkState(null, prev.linkProperties, ns.networkCapabilities,
ns.network, null, null));
return mNetworkMap.get(ns.network);
}
case EVENT_ON_LINKPROPERTIES: {
final NetworkState ns = (NetworkState) obj;
if (!mNetworkMap.containsKey(ns.network)) {
// Ignore updates for networks for which we have not yet
// received onAvailable() - which should never happen -
// or for which we have already received onLost().
return null;
}
if (VDBG) {
Log.d(TAG, String.format("EVENT_ON_LINKPROPERTIES for %s: %s",
ns.network, ns.linkProperties));
}
final NetworkState prev = mNetworkMap.get(ns.network);
mNetworkMap.put(ns.network,
new NetworkState(null, ns.linkProperties, prev.networkCapabilities,
ns.network, null, null));
return mNetworkMap.get(ns.network);
}
case EVENT_ON_LOST: {
final Network network = (Network) obj;
if (VDBG) {
Log.d(TAG, "EVENT_ON_LOST for " + network);
}
return mNetworkMap.remove(network);
}
default:
return null;
} }
if (!mNetworkMap.containsKey(network)) {
mNetworkMap.put(network,
new NetworkState(null, null, null, network, null, null));
}
final ConnectivityManager cm = cm();
if (mDefaultNetworkCallback != null) {
cm.requestNetworkCapabilities(mDefaultNetworkCallback);
cm.requestLinkProperties(mDefaultNetworkCallback);
}
// Requesting updates for mDunTetheringCallback is not
// necessary. Because it's a listen, it will already have
// heard all NetworkCapabilities and LinkProperties updates
// since UpstreamNetworkMonitor was started. Because we
// start UpstreamNetworkMonitor before chooseUpstreamType()
// is ever invoked (it can register a DUN request) this is
// mostly safe. However, if a DUN network is already up for
// some reason (unlikely, because DUN is restricted and,
// unless the DUN network is shared with another APN, only
// the system can request it and this is the only part of
// the system that requests it) we won't know its
// LinkProperties or NetworkCapabilities.
notifyTarget(EVENT_ON_AVAILABLE, network);
}
private void handleNetCap(Network network, NetworkCapabilities newNc) {
if (!mNetworkMap.containsKey(network)) {
// Ignore updates for networks for which we have not yet
// received onAvailable() - which should never happen -
// or for which we have already received onLost().
return;
}
if (VDBG) {
Log.d(TAG, String.format("EVENT_ON_CAPABILITIES for %s: %s",
network, newNc));
}
final NetworkState prev = mNetworkMap.get(network);
mNetworkMap.put(network,
new NetworkState(null, prev.linkProperties, newNc,
network, null, null));
notifyTarget(EVENT_ON_CAPABILITIES, network);
}
private void handleLinkProp(Network network, LinkProperties newLp) {
if (!mNetworkMap.containsKey(network)) {
// Ignore updates for networks for which we have not yet
// received onAvailable() - which should never happen -
// or for which we have already received onLost().
return;
}
if (VDBG) {
Log.d(TAG, String.format("EVENT_ON_LINKPROPERTIES for %s: %s",
network, newLp));
}
final NetworkState prev = mNetworkMap.get(network);
mNetworkMap.put(network,
new NetworkState(null, newLp, prev.networkCapabilities,
network, null, null));
notifyTarget(EVENT_ON_LINKPROPERTIES, network);
}
private void handleLost(Network network) {
if (!mNetworkMap.containsKey(network)) {
// Ignore updates for networks for which we have not yet
// received onAvailable() - which should never happen -
// or for which we have already received onLost().
return;
}
if (VDBG) {
Log.d(TAG, "EVENT_ON_LOST for " + network);
}
notifyTarget(EVENT_ON_LOST, mNetworkMap.remove(network));
} }
// Fetch (and cache) a ConnectivityManager only if and when we need one. // Fetch (and cache) a ConnectivityManager only if and when we need one.
@@ -1192,26 +1187,36 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
private class UpstreamNetworkCallback extends NetworkCallback { private class UpstreamNetworkCallback extends NetworkCallback {
@Override @Override
public void onAvailable(Network network) { public void onAvailable(Network network) {
mTarget.sendMessage(mWhat, EVENT_ON_AVAILABLE, 0, network); mTarget.getHandler().post(() -> handleAvailable(network));
} }
@Override @Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities newNc) { public void onCapabilitiesChanged(Network network, NetworkCapabilities newNc) {
mTarget.sendMessage(mWhat, EVENT_ON_CAPABILITIES, 0, mTarget.getHandler().post(() -> handleNetCap(network, newNc));
new NetworkState(null, null, newNc, network, null, null));
} }
@Override @Override
public void onLinkPropertiesChanged(Network network, LinkProperties newLp) { public void onLinkPropertiesChanged(Network network, LinkProperties newLp) {
mTarget.sendMessage(mWhat, EVENT_ON_LINKPROPERTIES, 0, mTarget.getHandler().post(() -> handleLinkProp(network, newLp));
new NetworkState(null, newLp, null, network, null, null));
} }
@Override @Override
public void onLost(Network network) { public void onLost(Network network) {
mTarget.sendMessage(mWhat, EVENT_ON_LOST, 0, network); mTarget.getHandler().post(() -> handleLost(network));
} }
} }
private void releaseCallback(NetworkCallback cb) {
if (cb != null) cm().unregisterNetworkCallback(cb);
}
private void notifyTarget(int which, Network network) {
notifyTarget(which, mNetworkMap.get(network));
}
private void notifyTarget(int which, NetworkState netstate) {
mTarget.sendMessage(mWhat, which, 0, netstate);
}
} }
// Needed because the canonical source of upstream truth is just the // Needed because the canonical source of upstream truth is just the
@@ -1728,9 +1733,7 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
mTryCell = !mTryCell; mTryCell = !mTryCell;
break; break;
case EVENT_UPSTREAM_CALLBACK: { case EVENT_UPSTREAM_CALLBACK: {
// First: always update local state about every network. final NetworkState ns = (NetworkState) message.obj;
final NetworkState ns = mUpstreamNetworkMonitor.processCallback(
message.arg1, message.obj);
if (ns == null || !pertainsToCurrentUpstream(ns)) { if (ns == null || !pertainsToCurrentUpstream(ns)) {
// TODO: In future, this is where upstream evaluation and selection // TODO: In future, this is where upstream evaluation and selection