Switch to HexEncoding from ByteStringUtils

Migration in preparation for deletion of ByteStringUtils.

Bug: 124232146
Test: build only
Merged-In: I07983ca596443a3e00616b63355c6504376f3e7c
Change-Id: I07983ca596443a3e00616b63355c6504376f3e7c
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
index a5b7849..4d287eaa 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
@@ -80,7 +80,6 @@
 import android.text.TextUtils;
 import android.util.ArrayMap;
 import android.util.ArraySet;
-import android.util.ByteStringUtils;
 import android.util.Slog;
 import android.util.SparseArray;
 import android.util.SparseBooleanArray;
@@ -95,6 +94,8 @@
 
 import com.google.android.collect.Sets;
 
+import libcore.util.HexEncoding;
+
 import java.io.File;
 import java.io.FileDescriptor;
 import java.io.FileNotFoundException;
@@ -109,7 +110,6 @@
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.regex.Pattern;
@@ -2410,7 +2410,7 @@
             rand.nextBytes(keyBytes);
 
             // Convert to string for storage in settings table.
-            final String userKey = ByteStringUtils.toHexString(keyBytes);
+            final String userKey = HexEncoding.encodeToString(keyBytes, true /* upperCase */);
 
             // Store the key in the ssaid table.
             final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
@@ -2440,13 +2440,16 @@
                 }
             }
             final String userKey = userKeySetting.getValue();
+            if (userKey == null || userKey.length() % 2 != 0) {
+                throw new IllegalStateException("User key invalid");
+            }
 
             // Convert the user's key back to a byte array.
-            final byte[] keyBytes = ByteStringUtils.fromHexToByteArray(userKey);
+            final byte[] keyBytes = HexEncoding.decode(userKey);
 
             // Validate that the key is of expected length.
             // Keys are currently 32 bytes, but were once 16 bytes during Android O development.
-            if (keyBytes == null || (keyBytes.length != 16 && keyBytes.length != 32)) {
+            if (keyBytes.length != 16 && keyBytes.length != 32) {
                 throw new IllegalStateException("User key invalid");
             }
 
@@ -2468,8 +2471,8 @@
             }
 
             // Convert result to a string for storage in settings table. Only want first 64 bits.
-            final String ssaid = ByteStringUtils.toHexString(m.doFinal()).substring(0, 16)
-                    .toLowerCase(Locale.US);
+            final String ssaid = HexEncoding.encodeToString(m.doFinal(), false /* upperCase */)
+                    .substring(0, 16);
 
             // Save the ssaid in the ssaid table.
             final String uid = Integer.toString(callingPkg.applicationInfo.uid);