NsdService: simple cleanups

This patch replace some SparseArray<Integer> with SparseIntArray, and
simplify a value lookup function by using indexOfValue().

Test: TODO
Bug: 37013369, 33298084
Bug: 38503832

(cherry picked from commit d2552aee11)

Change-Id: Ifbe4f01bad8964e6f3a6e9633415959ab5feb0af
This commit is contained in:
Hugo Benichi
2017-04-11 14:42:47 +09:00
parent c0a215d1ad
commit ecea72cfca

View File

@@ -35,6 +35,7 @@ import android.provider.Settings;
import android.util.Base64; import android.util.Base64;
import android.util.Slog; import android.util.Slog;
import android.util.SparseArray; import android.util.SparseArray;
import android.util.SparseIntArray;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.PrintWriter; import java.io.PrintWriter;
@@ -240,8 +241,8 @@ public class NsdService extends INsdManager.Stub {
} }
private void removeRequestMap(int clientId, int globalId, ClientInfo clientInfo) { private void removeRequestMap(int clientId, int globalId, ClientInfo clientInfo) {
clientInfo.mClientIds.remove(clientId); clientInfo.mClientIds.delete(clientId);
clientInfo.mClientRequests.remove(clientId); clientInfo.mClientRequests.delete(clientId);
mIdToClientInfoMap.remove(globalId); mIdToClientInfoMap.remove(globalId);
} }
@@ -294,7 +295,7 @@ public class NsdService extends INsdManager.Stub {
clientInfo = mClients.get(msg.replyTo); clientInfo = mClients.get(msg.replyTo);
try { try {
id = clientInfo.mClientIds.get(msg.arg2).intValue(); id = clientInfo.mClientIds.get(msg.arg2);
} catch (NullPointerException e) { } catch (NullPointerException e) {
replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED, replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR); NsdManager.FAILURE_INTERNAL_ERROR);
@@ -332,7 +333,7 @@ public class NsdService extends INsdManager.Stub {
if (DBG) Slog.d(TAG, "unregister service"); if (DBG) Slog.d(TAG, "unregister service");
clientInfo = mClients.get(msg.replyTo); clientInfo = mClients.get(msg.replyTo);
try { try {
id = clientInfo.mClientIds.get(msg.arg2).intValue(); id = clientInfo.mClientIds.get(msg.arg2);
} catch (NullPointerException e) { } catch (NullPointerException e) {
replyToMessage(msg, NsdManager.UNREGISTER_SERVICE_FAILED, replyToMessage(msg, NsdManager.UNREGISTER_SERVICE_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR); NsdManager.FAILURE_INTERNAL_ERROR);
@@ -813,10 +814,10 @@ public class NsdService extends INsdManager.Stub {
private NsdServiceInfo mResolvedService; private NsdServiceInfo mResolvedService;
/* A map from client id to unique id sent to mDns */ /* A map from client id to unique id sent to mDns */
private final SparseArray<Integer> mClientIds = new SparseArray<>(); private final SparseIntArray mClientIds = new SparseIntArray();
/* A map from client id to the type of the request we had received */ /* A map from client id to the type of the request we had received */
private final SparseArray<Integer> mClientRequests = new SparseArray<>(); private final SparseIntArray mClientRequests = new SparseIntArray();
private ClientInfo(AsyncChannel c, Messenger m) { private ClientInfo(AsyncChannel c, Messenger m) {
mChannel = c; mChannel = c;
@@ -843,6 +844,7 @@ public class NsdService extends INsdManager.Stub {
// and send cancellations to the daemon. // and send cancellations to the daemon.
private void expungeAllRequests() { private void expungeAllRequests() {
int globalId, clientId, i; int globalId, clientId, i;
// TODO: to keep handler responsive, do not clean all requests for that client at once.
for (i = 0; i < mClientIds.size(); i++) { for (i = 0; i < mClientIds.size(); i++) {
clientId = mClientIds.keyAt(i); clientId = mClientIds.keyAt(i);
globalId = mClientIds.valueAt(i); globalId = mClientIds.valueAt(i);
@@ -870,15 +872,11 @@ public class NsdService extends INsdManager.Stub {
// mClientIds is a sparse array of listener id -> mDnsClient id. For a given mDnsClient id, // mClientIds is a sparse array of listener id -> mDnsClient id. For a given mDnsClient id,
// return the corresponding listener id. mDnsClient id is also called a global id. // return the corresponding listener id. mDnsClient id is also called a global id.
private int getClientId(final int globalId) { private int getClientId(final int globalId) {
// This doesn't use mClientIds.indexOfValue because indexOfValue uses == (not .equals) int idx = mClientIds.indexOfValue(globalId);
// while also coercing the int primitives to Integer objects. if (idx < 0) {
for (int i = 0, nSize = mClientIds.size(); i < nSize; i++) { return idx;
int mDnsId = mClientIds.valueAt(i);
if (globalId == mDnsId) {
return mClientIds.keyAt(i);
}
} }
return -1; return mClientIds.keyAt(idx);
} }
} }