diff options
| author | 2019-09-24 15:28:23 -0700 | |
|---|---|---|
| committer | 2019-09-24 15:28:23 -0700 | |
| commit | 23d581fe2cf482a40e97f34d39c78429c35f3d84 (patch) | |
| tree | 44a2b3ffc8d805bb60e2dd9e7ae6bb3e3e7e59be | |
| parent | 061fda060c9fc21a8001b0eeb26c52d79144a8fd (diff) | |
| parent | 54bb8a1625342a585d95d64dcf06365cf37e4dd4 (diff) | |
Merge "Followup to CL 1103896"
am: 54bb8a1625
Change-Id: I1eb784a8266880260d75ca109c7d78f180333a0e
| -rw-r--r-- | core/java/android/net/MacAddress.java | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/core/java/android/net/MacAddress.java b/core/java/android/net/MacAddress.java index a809b28c9204..2cf2a6514e77 100644 --- a/core/java/android/net/MacAddress.java +++ b/core/java/android/net/MacAddress.java @@ -85,6 +85,9 @@ public final class MacAddress implements Parcelable { private static final long OUI_MASK = MacAddress.fromString("ff:ff:ff:0:0:0").mAddr; private static final long NIC_MASK = MacAddress.fromString("0:0:0:ff:ff:ff").mAddr; private static final MacAddress BASE_GOOGLE_MAC = MacAddress.fromString("da:a1:19:0:0:0"); + /** Default wifi MAC address used for a special purpose **/ + private static final MacAddress DEFAULT_MAC_ADDRESS = + MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS); // Internal representation of the MAC address as a single 8 byte long. // The encoding scheme sets the two most significant bytes to 0. The 6 bytes of the @@ -361,16 +364,7 @@ public final class MacAddress implements Parcelable { * @hide */ public static @NonNull MacAddress createRandomUnicastAddress() { - SecureRandom r = new SecureRandom(); - long addr = r.nextLong() & VALID_LONG_MASK; - addr |= LOCALLY_ASSIGNED_MASK; - addr &= ~MULTICAST_MASK; - MacAddress mac = new MacAddress(addr); - // WifiInfo.DEFAULT_MAC_ADDRESS is being used for another purpose, so do not use it here. - if (mac.toString().equals(WifiInfo.DEFAULT_MAC_ADDRESS)) { - return createRandomUnicastAddress(); - } - return mac; + return createRandomUnicastAddress(null, new SecureRandom()); } /** @@ -380,18 +374,23 @@ public final class MacAddress implements Parcelable { * The locally assigned bit is always set to 1. The multicast bit is always set to 0. * * @param base a base MacAddress whose OUI is used for generating the random address. + * If base == null then the OUI will also be randomized. * @param r a standard Java Random object used for generating the random address. * @return a random locally assigned MacAddress. * * @hide */ public static @NonNull MacAddress createRandomUnicastAddress(MacAddress base, Random r) { - long addr = (base.mAddr & OUI_MASK) | (NIC_MASK & r.nextLong()); + long addr; + if (base == null) { + addr = r.nextLong() & VALID_LONG_MASK; + } else { + addr = (base.mAddr & OUI_MASK) | (NIC_MASK & r.nextLong()); + } addr |= LOCALLY_ASSIGNED_MASK; addr &= ~MULTICAST_MASK; MacAddress mac = new MacAddress(addr); - // WifiInfo.DEFAULT_MAC_ADDRESS is being used for another purpose, so do not use it here. - if (mac.toString().equals(WifiInfo.DEFAULT_MAC_ADDRESS)) { + if (mac.equals(DEFAULT_MAC_ADDRESS)) { return createRandomUnicastAddress(base, r); } return mac; |