[go: nahoru, domu]

Use nullable to determine if window-extension components are available

Remove is present checks for window-extensions components.
Convert getters to return nullable values to align with API council
feedback.

Relnote: update extensions api.
Bug: 199123327
Test: n/a
Change-Id: Ica92bbdb8300f51b2d2906f38b40a9ed027b7810
diff --git a/window/window-extensions/api/current.txt b/window/window-extensions/api/current.txt
index 096d5d7..1534daf 100644
--- a/window/window-extensions/api/current.txt
+++ b/window/window-extensions/api/current.txt
@@ -3,8 +3,7 @@
 
   public interface WindowExtensions {
     method public default int getVendorApiLevel();
-    method public androidx.window.extensions.layout.WindowLayoutComponent getWindowLayoutComponent();
-    method public boolean isWindowLayoutComponentAvailable();
+    method public androidx.window.extensions.layout.WindowLayoutComponent? getWindowLayoutComponent();
   }
 
   public class WindowExtensionsProvider {
diff --git a/window/window-extensions/api/public_plus_experimental_current.txt b/window/window-extensions/api/public_plus_experimental_current.txt
index 392eca6..4a56a1c 100644
--- a/window/window-extensions/api/public_plus_experimental_current.txt
+++ b/window/window-extensions/api/public_plus_experimental_current.txt
@@ -5,11 +5,9 @@
   }
 
   public interface WindowExtensions {
-    method @androidx.window.extensions.ExperimentalWindowExtensionsApi public androidx.window.extensions.embedding.ActivityEmbeddingComponent getActivityEmbeddingComponent();
+    method @androidx.window.extensions.ExperimentalWindowExtensionsApi public androidx.window.extensions.embedding.ActivityEmbeddingComponent? getActivityEmbeddingComponent();
     method public default int getVendorApiLevel();
-    method public androidx.window.extensions.layout.WindowLayoutComponent getWindowLayoutComponent();
-    method @androidx.window.extensions.ExperimentalWindowExtensionsApi public boolean isEmbeddingComponentAvailable();
-    method public boolean isWindowLayoutComponentAvailable();
+    method public androidx.window.extensions.layout.WindowLayoutComponent? getWindowLayoutComponent();
   }
 
   public class WindowExtensionsProvider {
diff --git a/window/window-extensions/api/restricted_current.txt b/window/window-extensions/api/restricted_current.txt
index 096d5d7..1534daf 100644
--- a/window/window-extensions/api/restricted_current.txt
+++ b/window/window-extensions/api/restricted_current.txt
@@ -3,8 +3,7 @@
 
   public interface WindowExtensions {
     method public default int getVendorApiLevel();
-    method public androidx.window.extensions.layout.WindowLayoutComponent getWindowLayoutComponent();
-    method public boolean isWindowLayoutComponentAvailable();
+    method public androidx.window.extensions.layout.WindowLayoutComponent? getWindowLayoutComponent();
   }
 
   public class WindowExtensionsProvider {
diff --git a/window/window-extensions/src/main/java/androidx/window/extensions/WindowExtensions.java b/window/window-extensions/src/main/java/androidx/window/extensions/WindowExtensions.java
index 9e301e3..ef51631 100644
--- a/window/window-extensions/src/main/java/androidx/window/extensions/WindowExtensions.java
+++ b/window/window-extensions/src/main/java/androidx/window/extensions/WindowExtensions.java
@@ -16,7 +16,7 @@
 
 package androidx.window.extensions;
 
-import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
 import androidx.window.extensions.embedding.ActivityEmbeddingComponent;
 import androidx.window.extensions.layout.WindowLayoutComponent;
 
@@ -43,42 +43,22 @@
     }
 
     /**
-     * Returns {@code true} if {@link WindowLayoutComponent} is present on the device,
-     * {@code false} otherwise.
-     */
-    boolean isWindowLayoutComponentAvailable();
-
-    /**
      * Returns the OEM implementation of {@link WindowLayoutComponent} if it is supported on the
-     * device. The implementation must match the API level reported in
-     * {@link WindowExtensions}. Use {@link WindowExtensions#isWindowLayoutComponentAvailable()} to
-     * determine if {@link WindowLayoutComponent} is present.
+     * device, {@code null} otherwise. The implementation must match the API level reported in
+     * {@link WindowExtensions}.
      * @return the OEM implementation of {@link WindowLayoutComponent}
      * @throws UnsupportedOperationException if the device does not support
      */
-    @NonNull
+    @Nullable
     WindowLayoutComponent getWindowLayoutComponent();
 
-
-    /**
-     * Returns {@code true} if {@link ActivityEmbeddingComponent} is present on the device,
-     * {@code false} otherwise. If the component is not available the developer will receive a
-     * single callback with empty data or default values where possible.
-     */
-    @ExperimentalWindowExtensionsApi
-    boolean isEmbeddingComponentAvailable();
-
     /**
      * Returns the OEM implementation of {@link ActivityEmbeddingComponent} if it is supported on
-     * the device. The implementation must match the API level reported in
-     * {@link WindowExtensions}. An
-     * {@link UnsupportedOperationException} will be thrown if the device does not support
-     * Activity Embedding. Use
-     * {@link WindowExtensions#isEmbeddingComponentAvailable()} to determine if
-     * {@link ActivityEmbeddingComponent} is present.
+     * the device, {@code null} otherwise. The implementation must match the API level reported in
+     * {@link WindowExtensions}.
      * @return the OEM implementation of {@link ActivityEmbeddingComponent}
      */
-    @NonNull
+    @Nullable
     @ExperimentalWindowExtensionsApi
     ActivityEmbeddingComponent getActivityEmbeddingComponent();
 }
diff --git a/window/window/src/main/java/androidx/window/embedding/EmbeddingCompat.kt b/window/window/src/main/java/androidx/window/embedding/EmbeddingCompat.kt
index c424280..7ce0d6b 100644
--- a/window/window/src/main/java/androidx/window/embedding/EmbeddingCompat.kt
+++ b/window/window/src/main/java/androidx/window/embedding/EmbeddingCompat.kt
@@ -35,11 +35,7 @@
     private val adapter: EmbeddingAdapter
 ) : EmbeddingInterfaceCompat {
     constructor() : this(
-        if (isEmbeddingAvailable()) {
-            WindowExtensionsProvider.getWindowExtensions().getActivityEmbeddingComponent()
-        } else {
-            EmptyEmbeddingComponent()
-        },
+        embeddingComponent(),
         EmbeddingAdapter()
     )
 
@@ -74,7 +70,7 @@
 
         fun isEmbeddingAvailable(): Boolean {
             return try {
-                WindowExtensionsProvider.getWindowExtensions().isEmbeddingComponentAvailable
+                WindowExtensionsProvider.getWindowExtensions().activityEmbeddingComponent != null
             } catch (e: NoClassDefFoundError) {
                 if (DEBUG) {
                     Log.d(TAG, "Embedding extension version not found")
@@ -87,6 +83,15 @@
                 false
             }
         }
+
+        fun embeddingComponent(): ActivityEmbeddingComponent {
+            return if (isEmbeddingAvailable()) {
+                WindowExtensionsProvider.getWindowExtensions().getActivityEmbeddingComponent()
+                    ?: EmptyEmbeddingComponent()
+            } else {
+                EmptyEmbeddingComponent()
+            }
+        }
     }
 }