Make NetworkAgent stop subclassing Handler.

Subclassing Handler is not appropriate for a system API because
it is an implementation detail and allows users of this class to
post messages to the handler in ways that allow inappropriate
access to internals that aren't part of the API contract.

Also fix some lint errors.

Test: builds
Bug: 138306002
Change-Id: I79478ceff6bbcae879d1025098d177de0d15dbee
This commit is contained in:
Lorenzo Colitti
2020-01-12 20:27:32 +09:00
parent 8ae3471952
commit c58ecd5f33

View File

@@ -43,11 +43,12 @@ import java.util.concurrent.atomic.AtomicBoolean;
* *
* @hide * @hide
*/ */
public abstract class NetworkAgent extends Handler { public abstract class NetworkAgent {
// Guaranteed to be valid (not NETID_UNSET), otherwise registerNetworkAgent() would have thrown // Guaranteed to be valid (not NETID_UNSET), otherwise registerNetworkAgent() would have thrown
// an exception. // an exception.
public final int netId; public final int netId;
private final Handler mHandler;
private volatile AsyncChannel mAsyncChannel; private volatile AsyncChannel mAsyncChannel;
private final String LOG_TAG; private final String LOG_TAG;
private static final boolean DBG = true; private static final boolean DBG = true;
@@ -234,7 +235,7 @@ public abstract class NetworkAgent extends Handler {
public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni, public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
NetworkCapabilities nc, LinkProperties lp, int score, NetworkMisc misc, NetworkCapabilities nc, LinkProperties lp, int score, NetworkMisc misc,
int providerId) { int providerId) {
super(looper); mHandler = new NetworkAgentHandler(looper);
LOG_TAG = logTag; LOG_TAG = logTag;
mContext = context; mContext = context;
mProviderId = providerId; mProviderId = providerId;
@@ -245,8 +246,14 @@ public abstract class NetworkAgent extends Handler {
if (VDBG) log("Registering NetworkAgent"); if (VDBG) log("Registering NetworkAgent");
ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService( ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(
Context.CONNECTIVITY_SERVICE); Context.CONNECTIVITY_SERVICE);
netId = cm.registerNetworkAgent(new Messenger(this), new NetworkInfo(ni), netId = cm.registerNetworkAgent(new Messenger(mHandler), new NetworkInfo(ni),
new LinkProperties(lp), new NetworkCapabilities(nc), score, misc, providerId); new LinkProperties(lp), new NetworkCapabilities(nc), score, misc,
providerId);
}
private class NetworkAgentHandler extends Handler {
NetworkAgentHandler(Looper looper) {
super(looper);
} }
@Override @Override
@@ -296,14 +303,14 @@ public abstract class NetworkAgent extends Handler {
} }
if (currentTimeMs >= (mLastBwRefreshTime + BW_REFRESH_MIN_WIN_MS)) { if (currentTimeMs >= (mLastBwRefreshTime + BW_REFRESH_MIN_WIN_MS)) {
mPollLceScheduled = false; mPollLceScheduled = false;
if (mPollLcePending.getAndSet(true) == false) { if (!mPollLcePending.getAndSet(true)) {
pollLceData(); pollLceData();
} }
} else { } else {
// deliver the request at a later time rather than discard it completely. // deliver the request at a later time rather than discard it completely.
if (!mPollLceScheduled) { if (!mPollLceScheduled) {
long waitTime = mLastBwRefreshTime + BW_REFRESH_MIN_WIN_MS - long waitTime = mLastBwRefreshTime + BW_REFRESH_MIN_WIN_MS
currentTimeMs + 1; - currentTimeMs + 1;
mPollLceScheduled = sendEmptyMessageDelayed( mPollLceScheduled = sendEmptyMessageDelayed(
CMD_REQUEST_BANDWIDTH_UPDATE, waitTime); CMD_REQUEST_BANDWIDTH_UPDATE, waitTime);
} }
@@ -313,8 +320,9 @@ public abstract class NetworkAgent extends Handler {
case CMD_REPORT_NETWORK_STATUS: { case CMD_REPORT_NETWORK_STATUS: {
String redirectUrl = ((Bundle) msg.obj).getString(REDIRECT_URL_KEY); String redirectUrl = ((Bundle) msg.obj).getString(REDIRECT_URL_KEY);
if (VDBG) { if (VDBG) {
log("CMD_REPORT_NETWORK_STATUS(" + log("CMD_REPORT_NETWORK_STATUS("
(msg.arg1 == VALID_NETWORK ? "VALID, " : "INVALID, ") + redirectUrl); + (msg.arg1 == VALID_NETWORK ? "VALID, " : "INVALID, ")
+ redirectUrl);
} }
networkStatus(msg.arg1, redirectUrl); networkStatus(msg.arg1, redirectUrl);
break; break;
@@ -358,6 +366,7 @@ public abstract class NetworkAgent extends Handler {
} }
} }
} }
}
private void queueOrSendMessage(int what, Object obj) { private void queueOrSendMessage(int what, Object obj) {
queueOrSendMessage(what, 0, 0, obj); queueOrSendMessage(what, 0, 0, obj);