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