Merge "Fix AppFunctionRuntimeMedata.set/getEnabled" into main
diff --git a/core/java/android/app/appfunctions/AppFunctionRuntimeMetadata.java b/core/java/android/app/appfunctions/AppFunctionRuntimeMetadata.java
index 36daaab..83b5aa0 100644
--- a/core/java/android/app/appfunctions/AppFunctionRuntimeMetadata.java
+++ b/core/java/android/app/appfunctions/AppFunctionRuntimeMetadata.java
@@ -164,7 +164,13 @@
*/
@Nullable
public Boolean getEnabled() {
- return (Boolean) getProperty(PROPERTY_ENABLED);
+ // We can't use getPropertyBoolean here. getPropertyBoolean returns false instead of null
+ // if the value is missing.
+ boolean[] enabled = getPropertyBooleanArray(PROPERTY_ENABLED);
+ if (enabled == null || enabled.length == 0) {
+ return null;
+ }
+ return enabled[0];
}
/** Returns the qualified id linking to the static metadata of the app function. */
@@ -201,11 +207,16 @@
/**
* Sets an indicator specifying if the function is enabled or not. This would override the
* default enabled state in the static metadata ({@link
- * AppFunctionStaticMetadataHelper#STATIC_PROPERTY_ENABLED_BY_DEFAULT}).
+ * AppFunctionStaticMetadataHelper#STATIC_PROPERTY_ENABLED_BY_DEFAULT}). Sets this to
+ * null to clear the override.
*/
@NonNull
- public Builder setEnabled(boolean enabled) {
- setPropertyBoolean(PROPERTY_ENABLED, enabled);
+ public Builder setEnabled(@Nullable Boolean enabled) {
+ if (enabled == null) {
+ setPropertyBoolean(PROPERTY_ENABLED);
+ } else {
+ setPropertyBoolean(PROPERTY_ENABLED, enabled);
+ }
return this;
}
diff --git a/services/tests/appfunctions/src/android/app/appfunctions/AppFunctionRuntimeMetadataTest.kt b/services/tests/appfunctions/src/android/app/appfunctions/AppFunctionRuntimeMetadataTest.kt
index dbbb2fe..da3e94f 100644
--- a/services/tests/appfunctions/src/android/app/appfunctions/AppFunctionRuntimeMetadataTest.kt
+++ b/services/tests/appfunctions/src/android/app/appfunctions/AppFunctionRuntimeMetadataTest.kt
@@ -112,4 +112,28 @@
assertThat(runtimeMetadata.appFunctionStaticMetadataQualifiedId)
.isEqualTo("android\$apps-db/app_functions#com.pkg/funcId")
}
+
+ @Test
+ fun setEnabled_true() {
+ val runtimeMetadata =
+ AppFunctionRuntimeMetadata.Builder("com.pkg", "funcId").setEnabled(true).build()
+
+ assertThat(runtimeMetadata.enabled).isTrue()
+ }
+
+ @Test
+ fun setEnabled_false() {
+ val runtimeMetadata =
+ AppFunctionRuntimeMetadata.Builder("com.pkg", "funcId").setEnabled(false).build()
+
+ assertThat(runtimeMetadata.enabled).isFalse()
+ }
+
+ @Test
+ fun setEnabled_null() {
+ val runtimeMetadata =
+ AppFunctionRuntimeMetadata.Builder("com.pkg", "funcId").setEnabled(null).build()
+
+ assertThat(runtimeMetadata.enabled).isNull()
+ }
}