Add a CompareOrUpdateResult class. am: 2b70bacd6b am: accd4634e9 am: 903c23695e
Change-Id: Ic8b24b5e73d0f68a1313fe79a1aa546ce5f7adf5
diff --git a/staticlibs/Android.bp b/staticlibs/Android.bp
index 3f867fc..8f845bc 100644
--- a/staticlibs/Android.bp
+++ b/staticlibs/Android.bp
@@ -31,11 +31,16 @@
java_library {
name: "net-utils-framework-common",
- srcs: [":net-utils-framework-common-srcs"],
+ srcs: [
+ ":net-utils-framework-common-srcs",
+ // TODO: avoid including all framework annotations as they end up in library users jars
+ // and need jarjaring
+ ":framework-annotations",
+ ],
+ sdk_version: "system_current",
jarjar_rules: "jarjar-rules-shared.txt",
visibility: [
"//frameworks/base/packages/Tethering",
- "//frameworks/opt/net/wifi",
"//frameworks/opt/net/ike",
"//frameworks/opt/telephony",
"//packages/modules/NetworkStack",
@@ -46,11 +51,14 @@
java_library {
name: "net-utils-services-common",
- srcs: ["src_servicescommon/**/*.java"],
+ srcs: [
+ "src_servicescommon/**/*.java",
+ ":framework-annotations",
+ ],
+ sdk_version: "system_current",
visibility: [
- "//frameworks/base/services",
+ "//frameworks/base/services/net",
"//frameworks/base/packages/Tethering",
- "//frameworks/opt/net/wifi/service",
"//frameworks/opt/net/ike",
"//packages/modules/NetworkStack",
"//packages/modules/CaptivePortalLogin",
@@ -70,3 +78,31 @@
"//frameworks/opt/telephony",
],
}
+
+// Use a filegroup and not a library for wifi sources, as this needs corresponding jar-jar
+// rules on the wifi side.
+// Any class here *must* have a corresponding jarjar rule in the wifi build rules.
+filegroup {
+ name: "net-utils-framework-wifi-common-srcs",
+ srcs: [
+ "src_frameworkcommon/android/net/util/nsd/DnsSdTxtRecord.java",
+ "src_frameworkcommon/android/net/util/MacAddressUtils.java",
+ ],
+ visibility: [
+ "//frameworks/base",
+ ],
+}
+
+// Use a filegroup and not a library for wifi sources, as this needs corresponding jar-jar
+// rules on the wifi side.
+// Any class here *must* have a corresponding jarjar rule in the wifi build rules.
+filegroup {
+ name: "net-utils-wifi-service-common-srcs",
+ srcs: [
+ "src_frameworkcommon/android/net/util/NetUtils.java",
+ "src_servicescommon/android/net/NetworkFactory.java",
+ ],
+ visibility: [
+ "//frameworks/opt/net/wifi/service",
+ ],
+}
diff --git a/staticlibs/jarjar-rules-shared.txt b/staticlibs/jarjar-rules-shared.txt
index 4a2b653..b22771d 100644
--- a/staticlibs/jarjar-rules-shared.txt
+++ b/staticlibs/jarjar-rules-shared.txt
@@ -1 +1,3 @@
rule android.net.util.** com.android.net.module.util.@1
+rule android.annotation.** com.android.net.module.annotation.@1
+rule com.android.internal.annotations.** com.android.net.module.annotation.@1
\ No newline at end of file
diff --git a/staticlibs/src_frameworkcommon/android/net/util/MacAddressUtils.java b/staticlibs/src_frameworkcommon/android/net/util/MacAddressUtils.java
index 5345789..3ad452b 100644
--- a/staticlibs/src_frameworkcommon/android/net/util/MacAddressUtils.java
+++ b/staticlibs/src_frameworkcommon/android/net/util/MacAddressUtils.java
@@ -20,11 +20,9 @@
import android.annotation.Nullable;
import android.net.MacAddress;
-import com.android.internal.util.BitUtils;
-import com.android.internal.util.Preconditions;
-
import java.security.SecureRandom;
import java.util.Arrays;
+import java.util.Objects;
import java.util.Random;
/**
@@ -98,14 +96,15 @@
* Convert a byte address to long address.
*/
public static long longAddrFromByteAddr(byte[] addr) {
- Preconditions.checkNotNull(addr);
+ Objects.requireNonNull(addr);
if (!isMacAddress(addr)) {
throw new IllegalArgumentException(
Arrays.toString(addr) + " was not a valid MAC address");
}
long longAddr = 0;
for (byte b : addr) {
- longAddr = (longAddr << 8) + BitUtils.uint8(b);
+ final int uint8Byte = b & 0xff;
+ longAddr = (longAddr << 8) + uint8Byte;
}
return longAddr;
}
diff --git a/staticlibs/src_frameworkcommon/android/net/util/nsd/DnsSdTxtRecord.java b/staticlibs/src_frameworkcommon/android/net/util/nsd/DnsSdTxtRecord.java
new file mode 100644
index 0000000..81768f7
--- /dev/null
+++ b/staticlibs/src_frameworkcommon/android/net/util/nsd/DnsSdTxtRecord.java
@@ -0,0 +1,325 @@
+/* -*- Mode: Java; tab-width: 4 -*-
+ *
+ * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+
+ To do:
+ - implement remove()
+ - fix set() to replace existing values
+ */
+
+package android.net.util.nsd;
+
+import android.os.Parcelable;
+import android.os.Parcel;
+
+import java.util.Arrays;
+
+/**
+ * This class handles TXT record data for DNS based service discovery as specified at
+ * http://tools.ietf.org/html/draft-cheshire-dnsext-dns-sd-11
+ *
+ * DNS-SD specifies that a TXT record corresponding to an SRV record consist of
+ * a packed array of bytes, each preceded by a length byte. Each string
+ * is an attribute-value pair.
+ *
+ * The DnsSdTxtRecord object stores the entire TXT data as a single byte array, traversing it
+ * as need be to implement its various methods.
+ * @hide
+ *
+ */
+public class DnsSdTxtRecord implements Parcelable {
+ private static final byte mSeparator = '=';
+
+ private byte[] mData;
+
+ /** Constructs a new, empty TXT record. */
+ public DnsSdTxtRecord() {
+ mData = new byte[0];
+ }
+
+ /** Constructs a new TXT record from a byte array in the standard format. */
+ public DnsSdTxtRecord(byte[] data) {
+ mData = (byte[]) data.clone();
+ }
+
+ /** Copy constructor */
+ public DnsSdTxtRecord(DnsSdTxtRecord src) {
+ if (src != null && src.mData != null) {
+ mData = (byte[]) src.mData.clone();
+ }
+ }
+
+ /**
+ * Set a key/value pair. Setting an existing key will replace its value.
+ * @param key Must be ascii with no '='
+ * @param value matching value to key
+ */
+ public void set(String key, String value) {
+ byte[] keyBytes;
+ byte[] valBytes;
+ int valLen;
+
+ if (value != null) {
+ valBytes = value.getBytes();
+ valLen = valBytes.length;
+ } else {
+ valBytes = null;
+ valLen = 0;
+ }
+
+ try {
+ keyBytes = key.getBytes("US-ASCII");
+ }
+ catch (java.io.UnsupportedEncodingException e) {
+ throw new IllegalArgumentException("key should be US-ASCII");
+ }
+
+ for (int i = 0; i < keyBytes.length; i++) {
+ if (keyBytes[i] == '=') {
+ throw new IllegalArgumentException("= is not a valid character in key");
+ }
+ }
+
+ if (keyBytes.length + valLen >= 255) {
+ throw new IllegalArgumentException("Key and Value length cannot exceed 255 bytes");
+ }
+
+ int currentLoc = remove(key);
+ if (currentLoc == -1)
+ currentLoc = keyCount();
+
+ insert(keyBytes, valBytes, currentLoc);
+ }
+
+ /**
+ * Get a value for a key
+ *
+ * @param key
+ * @return The value associated with the key
+ */
+ public String get(String key) {
+ byte[] val = this.getValue(key);
+ return val != null ? new String(val) : null;
+ }
+
+ /** Remove a key/value pair. If found, returns the index or -1 if not found */
+ public int remove(String key) {
+ int avStart = 0;
+
+ for (int i=0; avStart < mData.length; i++) {
+ int avLen = mData[avStart];
+ if (key.length() <= avLen &&
+ (key.length() == avLen || mData[avStart + key.length() + 1] == mSeparator)) {
+ String s = new String(mData, avStart + 1, key.length());
+ if (0 == key.compareToIgnoreCase(s)) {
+ byte[] oldBytes = mData;
+ mData = new byte[oldBytes.length - avLen - 1];
+ System.arraycopy(oldBytes, 0, mData, 0, avStart);
+ System.arraycopy(oldBytes, avStart + avLen + 1, mData, avStart,
+ oldBytes.length - avStart - avLen - 1);
+ return i;
+ }
+ }
+ avStart += (0xFF & (avLen + 1));
+ }
+ return -1;
+ }
+
+ /** Return the count of keys */
+ public int keyCount() {
+ int count = 0, nextKey;
+ for (nextKey = 0; nextKey < mData.length; count++) {
+ nextKey += (0xFF & (mData[nextKey] + 1));
+ }
+ return count;
+ }
+
+ /** Return true if key is present, false if not. */
+ public boolean contains(String key) {
+ String s = null;
+ for (int i = 0; null != (s = this.getKey(i)); i++) {
+ if (0 == key.compareToIgnoreCase(s)) return true;
+ }
+ return false;
+ }
+
+ /* Gets the size in bytes */
+ public int size() {
+ return mData.length;
+ }
+
+ /* Gets the raw data in bytes */
+ public byte[] getRawData() {
+ return (byte[]) mData.clone();
+ }
+
+ private void insert(byte[] keyBytes, byte[] value, int index) {
+ byte[] oldBytes = mData;
+ int valLen = (value != null) ? value.length : 0;
+ int insertion = 0;
+ int newLen, avLen;
+
+ for (int i = 0; i < index && insertion < mData.length; i++) {
+ insertion += (0xFF & (mData[insertion] + 1));
+ }
+
+ avLen = keyBytes.length + valLen + (value != null ? 1 : 0);
+ newLen = avLen + oldBytes.length + 1;
+
+ mData = new byte[newLen];
+ System.arraycopy(oldBytes, 0, mData, 0, insertion);
+ int secondHalfLen = oldBytes.length - insertion;
+ System.arraycopy(oldBytes, insertion, mData, newLen - secondHalfLen, secondHalfLen);
+ mData[insertion] = (byte) avLen;
+ System.arraycopy(keyBytes, 0, mData, insertion + 1, keyBytes.length);
+ if (value != null) {
+ mData[insertion + 1 + keyBytes.length] = mSeparator;
+ System.arraycopy(value, 0, mData, insertion + keyBytes.length + 2, valLen);
+ }
+ }
+
+ /** Return a key in the TXT record by zero-based index. Returns null if index exceeds the total number of keys. */
+ private String getKey(int index) {
+ int avStart = 0;
+
+ for (int i=0; i < index && avStart < mData.length; i++) {
+ avStart += mData[avStart] + 1;
+ }
+
+ if (avStart < mData.length) {
+ int avLen = mData[avStart];
+ int aLen = 0;
+
+ for (aLen=0; aLen < avLen; aLen++) {
+ if (mData[avStart + aLen + 1] == mSeparator) break;
+ }
+ return new String(mData, avStart + 1, aLen);
+ }
+ return null;
+ }
+
+ /**
+ * Look up a key in the TXT record by zero-based index and return its value.
+ * Returns null if index exceeds the total number of keys.
+ * Returns null if the key is present with no value.
+ */
+ private byte[] getValue(int index) {
+ int avStart = 0;
+ byte[] value = null;
+
+ for (int i=0; i < index && avStart < mData.length; i++) {
+ avStart += mData[avStart] + 1;
+ }
+
+ if (avStart < mData.length) {
+ int avLen = mData[avStart];
+ int aLen = 0;
+
+ for (aLen=0; aLen < avLen; aLen++) {
+ if (mData[avStart + aLen + 1] == mSeparator) {
+ value = new byte[avLen - aLen - 1];
+ System.arraycopy(mData, avStart + aLen + 2, value, 0, avLen - aLen - 1);
+ break;
+ }
+ }
+ }
+ return value;
+ }
+
+ private String getValueAsString(int index) {
+ byte[] value = this.getValue(index);
+ return value != null ? new String(value) : null;
+ }
+
+ private byte[] getValue(String forKey) {
+ String s = null;
+ int i;
+
+ for (i = 0; null != (s = this.getKey(i)); i++) {
+ if (0 == forKey.compareToIgnoreCase(s)) {
+ return this.getValue(i);
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Return a string representation.
+ * Example : {key1=value1},{key2=value2}..
+ *
+ * For a key say like "key3" with null value
+ * {key1=value1},{key2=value2}{key3}
+ */
+ public String toString() {
+ String a, result = null;
+
+ for (int i = 0; null != (a = this.getKey(i)); i++) {
+ String av = "{" + a;
+ String val = this.getValueAsString(i);
+ if (val != null)
+ av += "=" + val + "}";
+ else
+ av += "}";
+ if (result == null)
+ result = av;
+ else
+ result = result + ", " + av;
+ }
+ return result != null ? result : "";
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o == this) {
+ return true;
+ }
+ if (!(o instanceof DnsSdTxtRecord)) {
+ return false;
+ }
+
+ DnsSdTxtRecord record = (DnsSdTxtRecord)o;
+ return Arrays.equals(record.mData, mData);
+ }
+
+ @Override
+ public int hashCode() {
+ return Arrays.hashCode(mData);
+ }
+
+ /** Implement the Parcelable interface */
+ public int describeContents() {
+ return 0;
+ }
+
+ /** Implement the Parcelable interface */
+ public void writeToParcel(Parcel dest, int flags) {
+ dest.writeByteArray(mData);
+ }
+
+ /** Implement the Parcelable interface */
+ public static final @android.annotation.NonNull Creator<DnsSdTxtRecord> CREATOR =
+ new Creator<DnsSdTxtRecord>() {
+ public DnsSdTxtRecord createFromParcel(Parcel in) {
+ DnsSdTxtRecord info = new DnsSdTxtRecord();
+ in.readByteArray(info.mData);
+ return info;
+ }
+
+ public DnsSdTxtRecord[] newArray(int size) {
+ return new DnsSdTxtRecord[size];
+ }
+ };
+}
diff --git a/staticlibs/src_servicescommon/android/net/NetworkFactory.java b/staticlibs/src_servicescommon/android/net/NetworkFactory.java
index 479a191..47054f5 100644
--- a/staticlibs/src_servicescommon/android/net/NetworkFactory.java
+++ b/staticlibs/src_servicescommon/android/net/NetworkFactory.java
@@ -18,22 +18,19 @@
import android.annotation.NonNull;
import android.content.Context;
-import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
-import android.util.SparseArray;
import com.android.internal.annotations.VisibleForTesting;
-import com.android.internal.util.IndentingPrintWriter;
-import com.android.internal.util.Protocol;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
-import java.util.concurrent.atomic.AtomicInteger;
+import java.util.LinkedHashMap;
+import java.util.Map;
/**
* A NetworkFactory is an entity that creates NetworkAgent objects.
@@ -51,24 +48,8 @@
* @hide
**/
public class NetworkFactory extends Handler {
- /* TODO: delete when all callers have migrated to NetworkProvider IDs. */
- public static class SerialNumber {
- // Guard used by no network factory.
- public static final int NONE = -1;
- // A hardcoded serial number for NetworkAgents representing VPNs. These agents are
- // not created by any factory, so they use this constant for clarity instead of NONE.
- public static final int VPN = -2;
- private static final AtomicInteger sNetworkFactorySerialNumber = new AtomicInteger(1);
- /** Returns a unique serial number for a factory. */
- public static final int nextSerialNumber() {
- return sNetworkFactorySerialNumber.getAndIncrement();
- }
- }
-
private static final boolean DBG = true;
private static final boolean VDBG = false;
-
- private static final int BASE = Protocol.BASE_NETWORK_FACTORY;
/**
* Pass a network request to the bearer. If the bearer believes it can
* satisfy the request it should connect to the network and create a
@@ -93,33 +74,33 @@
* msg.arg2 = the ID of the NetworkProvider currently responsible for the
* NetworkAgent handling this request, or NetworkProvider.ID_NONE if none.
*/
- public static final int CMD_REQUEST_NETWORK = BASE;
+ public static final int CMD_REQUEST_NETWORK = 1;
/**
* Cancel a network request
* msg.obj = NetworkRequest
*/
- public static final int CMD_CANCEL_REQUEST = BASE + 1;
+ public static final int CMD_CANCEL_REQUEST = 2;
/**
* Internally used to set our best-guess score.
* msg.arg1 = new score
*/
- private static final int CMD_SET_SCORE = BASE + 2;
+ private static final int CMD_SET_SCORE = 3;
/**
* Internally used to set our current filter for coarse bandwidth changes with
* technology changes.
* msg.obj = new filter
*/
- private static final int CMD_SET_FILTER = BASE + 3;
+ private static final int CMD_SET_FILTER = 4;
private final Context mContext;
private final ArrayList<Message> mPreConnectedQueue = new ArrayList<Message>();
private final String LOG_TAG;
- private final SparseArray<NetworkRequestInfo> mNetworkRequests =
- new SparseArray<NetworkRequestInfo>();
+ private final Map<NetworkRequest, NetworkRequestInfo> mNetworkRequests =
+ new LinkedHashMap<>();
private int mScore;
private NetworkCapabilities mCapabilityFilter;
@@ -127,7 +108,6 @@
private int mRefCount = 0;
private Messenger mMessenger = null;
private NetworkProvider mProvider = null;
- private int mProviderId;
public NetworkFactory(Looper looper, Context context, String logTag,
NetworkCapabilities filter) {
@@ -158,7 +138,8 @@
};
mMessenger = new Messenger(this);
- mProviderId = ConnectivityManager.from(mContext).registerNetworkProvider(mProvider);
+ ((ConnectivityManager) mContext.getSystemService(
+ Context.CONNECTIVITY_SERVICE)).registerNetworkProvider(mProvider);
}
public void unregister() {
@@ -168,7 +149,8 @@
}
if (DBG) log("Unregistering NetworkFactory");
- ConnectivityManager.from(mContext).unregisterNetworkProvider(mProvider);
+ ((ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
+ .unregisterNetworkProvider(mProvider);
mProvider = null;
}
@@ -240,14 +222,14 @@
*/
@VisibleForTesting
protected void handleAddRequest(NetworkRequest request, int score, int servingProviderId) {
- NetworkRequestInfo n = mNetworkRequests.get(request.requestId);
+ NetworkRequestInfo n = mNetworkRequests.get(request);
if (n == null) {
if (DBG) {
log("got request " + request + " with score " + score
+ " and providerId " + servingProviderId);
}
n = new NetworkRequestInfo(request, score, servingProviderId);
- mNetworkRequests.put(n.request.requestId, n);
+ mNetworkRequests.put(n.request, n);
} else {
if (VDBG) {
log("new score " + score + " for existing request " + request
@@ -263,9 +245,9 @@
@VisibleForTesting
protected void handleRemoveRequest(NetworkRequest request) {
- NetworkRequestInfo n = mNetworkRequests.get(request.requestId);
+ NetworkRequestInfo n = mNetworkRequests.get(request);
if (n != null) {
- mNetworkRequests.remove(request.requestId);
+ mNetworkRequests.remove(request);
if (n.requested) releaseNetworkFor(n.request);
}
}
@@ -309,8 +291,8 @@
log(" n.requests = " + n.requested);
log(" n.score = " + n.score);
log(" mScore = " + mScore);
- log(" n.providerId = " + n.providerId);
- log(" mProviderId = " + mProviderId);
+ log(" request.providerId = " + n.providerId);
+ log(" mProvider.id = " + mProvider.getProviderId());
}
if (shouldNeedNetworkFor(n)) {
if (VDBG) log(" needNetworkFor");
@@ -331,10 +313,10 @@
// If the score of this request is higher or equal to that of this factory and some
// other factory is responsible for it, then this factory should not track the request
// because it has no hope of satisfying it.
- && (n.score < mScore || n.providerId == mProviderId)
+ && (n.score < mScore || n.providerId == mProvider.getProviderId())
// If this factory can't satisfy the capability needs of this request, then it
// should not be tracked.
- && n.request.networkCapabilities.satisfiedByNetworkCapabilities(mCapabilityFilter)
+ && n.request.satisfiedBy(mCapabilityFilter)
// Finally if the concrete implementation of the factory rejects the request, then
// don't track it.
&& acceptRequest(n.request, n.score);
@@ -349,15 +331,13 @@
// assigned to the factory
// - This factory can't satisfy the capability needs of the request
// - The concrete implementation of the factory rejects the request
- && ((n.score > mScore && n.providerId != mProviderId)
- || !n.request.networkCapabilities.satisfiedByNetworkCapabilities(
- mCapabilityFilter)
+ && ((n.score > mScore && n.providerId != mProvider.getProviderId())
+ || !n.request.satisfiedBy(mCapabilityFilter)
|| !acceptRequest(n.request, n.score));
}
private void evalRequests() {
- for (int i = 0; i < mNetworkRequests.size(); i++) {
- NetworkRequestInfo n = mNetworkRequests.valueAt(i);
+ for (NetworkRequestInfo n : mNetworkRequests.values()) {
evalRequest(n);
}
}
@@ -384,7 +364,12 @@
protected void releaseRequestAsUnfulfillableByAnyFactory(NetworkRequest r) {
post(() -> {
if (DBG) log("releaseRequestAsUnfulfillableByAnyFactory: " + r);
- ConnectivityManager.from(mContext).declareNetworkRequestUnfulfillable(r);
+ final NetworkProvider provider = mProvider;
+ if (provider == null) {
+ Log.e(LOG_TAG, "Ignoring attempt to release unregistered request as unfulfillable");
+ return;
+ }
+ provider.declareNetworkRequestUnfulfillable(r);
});
}
@@ -416,11 +401,11 @@
/* TODO: delete when all callers have migrated to NetworkProvider IDs. */
public int getSerialNumber() {
- return mProviderId;
+ return mProvider.getProviderId();
}
- public int getProviderId() {
- return mProviderId;
+ public NetworkProvider getProvider() {
+ return mProvider;
}
protected void log(String s) {
@@ -428,19 +413,16 @@
}
public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
- final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ");
- pw.println(toString());
- pw.increaseIndent();
- for (int i = 0; i < mNetworkRequests.size(); i++) {
- pw.println(mNetworkRequests.valueAt(i));
+ writer.println(toString());
+ for (NetworkRequestInfo n : mNetworkRequests.values()) {
+ writer.println(" " + n);
}
- pw.decreaseIndent();
}
@Override
public String toString() {
- StringBuilder sb = new StringBuilder("{").append(LOG_TAG).append(" - mProviderId=")
- .append(mProviderId).append(", ScoreFilter=")
+ StringBuilder sb = new StringBuilder("{").append(LOG_TAG).append(" - providerId=")
+ .append(mProvider.getProviderId()).append(", ScoreFilter=")
.append(mScore).append(", Filter=").append(mCapabilityFilter).append(", requests=")
.append(mNetworkRequests.size()).append(", refCount=").append(mRefCount)
.append("}");