Merge "Workaround to let SettingsUtils handle empty settings values." into qt-dev
diff --git a/common/device-side/util-axt/src/com/android/compatibility/common/util/SettingsUtils.java b/common/device-side/util-axt/src/com/android/compatibility/common/util/SettingsUtils.java
index 12cc844..14f8285 100644
--- a/common/device-side/util-axt/src/com/android/compatibility/common/util/SettingsUtils.java
+++ b/common/device-side/util-axt/src/com/android/compatibility/common/util/SettingsUtils.java
@@ -21,6 +21,8 @@
 
 import android.content.Context;
 import android.provider.Settings;
+import android.text.TextUtils;
+import android.util.Log;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
@@ -30,9 +32,16 @@
  */
 public final class SettingsUtils {
 
+    private static final String TAG = SettingsUtils.class.getSimpleName();
+
     public static final String NAMESPACE_SECURE = "secure";
     public static final String NAMESPACE_GLOBAL = "global";
 
+    // TODO(b/123885378): we cannot pass an empty value when using 'cmd settings', so we need
+    // to remove the property instead. Once we use the Settings API directly, we can remove this
+    // constant and all if() statements that ues it
+    static final boolean TMP_HACK_REMOVE_EMPTY_PROPERTIES = true;
+
     /**
      * Uses a Shell command to set the given preference.
      */
@@ -41,6 +50,11 @@
             delete(namespace, key);
             return;
         }
+        if (TMP_HACK_REMOVE_EMPTY_PROPERTIES && TextUtils.isEmpty(value)) {
+            Log.w(TAG, "Value of " + namespace + ":" + key + " is empty; deleting it instead");
+            delete(namespace, key);
+            return;
+        }
         runShellCommand("settings put %s %s %s default", namespace, key, value);
     }
 
@@ -73,7 +87,13 @@
         observer.assertCalled();
 
         final String newValue = get(namespace, key);
-        assertWithMessage("invalid value for '%s' settings", key).that(newValue).isEqualTo(value);
+        if (TMP_HACK_REMOVE_EMPTY_PROPERTIES && TextUtils.isEmpty(value)) {
+            assertWithMessage("invalid value for '%s' settings", key).that(newValue)
+                .isNull();
+        } else {
+            assertWithMessage("invalid value for '%s' settings", key).that(newValue)
+                    .isEqualTo(value);
+        }
     }
 
     /**
diff --git a/common/device-side/util/src/com/android/compatibility/common/util/SettingsUtils.java b/common/device-side/util/src/com/android/compatibility/common/util/SettingsUtils.java
index 60e0069..c13de45 100644
--- a/common/device-side/util/src/com/android/compatibility/common/util/SettingsUtils.java
+++ b/common/device-side/util/src/com/android/compatibility/common/util/SettingsUtils.java
@@ -22,6 +22,8 @@
 
 import android.content.Context;
 import android.provider.Settings;
+import android.text.TextUtils;
+import android.util.Log;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
@@ -31,9 +33,16 @@
  */
 public final class SettingsUtils {
 
+    private static final String TAG = SettingsUtils.class.getSimpleName();
+
     public static final String NAMESPACE_SECURE = "secure";
     public static final String NAMESPACE_GLOBAL = "global";
 
+    // TODO(b/123885378): we cannot pass an empty value when using 'cmd settings', so we need
+    // to remove the property instead. Once we use the Settings API directly, we can remove this
+    // constant and all if() statements that ues it
+    static final boolean TMP_HACK_REMOVE_EMPTY_PROPERTIES = true;
+
     /**
      * Uses a Shell command to set the given preference.
      */
@@ -42,6 +51,11 @@
             delete(namespace, key);
             return;
         }
+        if (TMP_HACK_REMOVE_EMPTY_PROPERTIES && TextUtils.isEmpty(value)) {
+            Log.w(TAG, "Value of " + namespace + ":" + key + " is empty; deleting it instead");
+            delete(namespace, key);
+            return;
+        }
         runShellCommand("settings put %s %s %s default", namespace, key, value);
     }
 
@@ -74,7 +88,13 @@
         observer.assertCalled();
 
         final String newValue = get(namespace, key);
-        assertWithMessage("invalid value for '%s' settings", key).that(newValue).isEqualTo(value);
+        if (TMP_HACK_REMOVE_EMPTY_PROPERTIES && TextUtils.isEmpty(value)) {
+            assertWithMessage("invalid value for '%s' settings", key).that(newValue)
+                .isNull();
+        } else {
+            assertWithMessage("invalid value for '%s' settings", key).that(newValue)
+                    .isEqualTo(value);
+        }
     }
 
     /**