From cd70dea38f35c65e56d2a0ab25ed599ea1d0d5f9 Mon Sep 17 00:00:00 2001 From: Wanying Ding Date: Wed, 29 Nov 2023 10:54:57 -0800 Subject: [PATCH] make ActivityRetainedSaveState an opt-in API. RELNOTES=n/a PiperOrigin-RevId: 586393130 --- WORKSPACE | 1 + java/dagger/hilt/android/BUILD | 11 +++++++ java/dagger/hilt/android/UnstableApi.java | 29 +++++++++++++++++++ .../hilt/android/internal/managers/BUILD | 2 ++ .../managers/SavedStateHandleModule.java | 3 ++ .../lifecycle/ActivityRetainedSavedState.java | 5 ++++ java/dagger/hilt/android/lifecycle/BUILD | 5 +++- java/dagger/hilt/android/testing/BUILD | 1 + .../dagger/hilt/viewmodel/SimpleActivity.java | 12 +++++++- .../hilt-android/viewmodel/build.gradle | 2 +- javatests/dagger/hilt/android/BUILD | 2 ++ .../android/ViewModelSavedStateOwnerTest.java | 2 ++ 12 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 java/dagger/hilt/android/UnstableApi.java diff --git a/WORKSPACE b/WORKSPACE index 0eda5f5df63..a27ac38d2cc 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -176,6 +176,7 @@ KSP_VERSION = KOTLIN_VERSION + "-1.0.12" maven_install( artifacts = [ "androidx.annotation:annotation:1.1.0", + "androidx.annotation:annotation-experimental:1.2.0", "androidx.appcompat:appcompat:1.3.1", "androidx.activity:activity:1.5.1", "androidx.fragment:fragment:1.5.1", diff --git a/java/dagger/hilt/android/BUILD b/java/dagger/hilt/android/BUILD index d32617f67fe..983b5d18c7c 100644 --- a/java/dagger/hilt/android/BUILD +++ b/java/dagger/hilt/android/BUILD @@ -147,6 +147,14 @@ android_library( ], ) +android_library( + name = "unstable_api", + srcs = ["UnstableApi.java"], + deps = [ + "@maven//:androidx_annotation_annotation_experimental", + ], +) + java_library( name = "package_info", srcs = ["package-info.java"], @@ -164,6 +172,7 @@ android_library( ":entry_point_accessors", ":hilt_android_app", ":package_info", + ":unstable_api", "//java/dagger/hilt:artifact-core-lib", "//java/dagger/hilt/android/migration:custom_inject", "//java/dagger/hilt/android/migration:optional_inject", @@ -181,6 +190,7 @@ gen_maven_artifact( "//java/dagger/hilt/android:activity_retained_lifecycle", "//java/dagger/hilt/android:android_entry_point", "//java/dagger/hilt/android:hilt_android_app", + "//java/dagger/hilt/android:unstable_api", "//java/dagger/hilt/android:early_entry_point", "//java/dagger/hilt/android:package_info", "//java/dagger/hilt/android:view_model_lifecycle", @@ -220,6 +230,7 @@ gen_maven_artifact( artifact_target_maven_deps = [ "androidx.activity:activity", "androidx.annotation:annotation", + "androidx.annotation:annotation-experimental", "androidx.fragment:fragment", "androidx.lifecycle:lifecycle-common", "androidx.lifecycle:lifecycle-viewmodel", diff --git a/java/dagger/hilt/android/UnstableApi.java b/java/dagger/hilt/android/UnstableApi.java new file mode 100644 index 00000000000..dd328ecb31c --- /dev/null +++ b/java/dagger/hilt/android/UnstableApi.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2023 The Dagger Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dagger.hilt.android; + +import androidx.annotation.RequiresOptIn; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** Mark unstable Api usage. */ +@Retention(RetentionPolicy.CLASS) +@Target({ElementType.METHOD, ElementType.TYPE, ElementType.ANNOTATION_TYPE}) +@RequiresOptIn(level = RequiresOptIn.Level.ERROR) +public @interface UnstableApi {} diff --git a/java/dagger/hilt/android/internal/managers/BUILD b/java/dagger/hilt/android/internal/managers/BUILD index 386540335ba..950b51129ee 100644 --- a/java/dagger/hilt/android/internal/managers/BUILD +++ b/java/dagger/hilt/android/internal/managers/BUILD @@ -43,6 +43,7 @@ android_library( "//java/dagger/hilt:install_in", "//java/dagger/hilt/android:activity_retained_lifecycle", "//java/dagger/hilt/android:entry_point_accessors", + "//java/dagger/hilt/android:unstable_api", "//java/dagger/hilt/android/components", "//java/dagger/hilt/android/internal", "//java/dagger/hilt/android/internal/builders", @@ -53,6 +54,7 @@ android_library( "//java/dagger/hilt/internal:preconditions", "@maven//:androidx_activity_activity", "@maven//:androidx_annotation_annotation", + "@maven//:androidx_annotation_annotation_experimental", "@maven//:androidx_fragment_fragment", "@maven//:androidx_lifecycle_lifecycle_common", "@maven//:androidx_lifecycle_lifecycle_viewmodel", diff --git a/java/dagger/hilt/android/internal/managers/SavedStateHandleModule.java b/java/dagger/hilt/android/internal/managers/SavedStateHandleModule.java index ee2065f90d4..18ca508bcef 100644 --- a/java/dagger/hilt/android/internal/managers/SavedStateHandleModule.java +++ b/java/dagger/hilt/android/internal/managers/SavedStateHandleModule.java @@ -16,10 +16,12 @@ package dagger.hilt.android.internal.managers; +import androidx.annotation.OptIn; import androidx.lifecycle.SavedStateHandle; import dagger.Module; import dagger.Provides; import dagger.hilt.InstallIn; +import dagger.hilt.android.UnstableApi; import dagger.hilt.android.components.ActivityRetainedComponent; import dagger.hilt.android.lifecycle.ActivityRetainedSavedState; import dagger.hilt.android.scopes.ActivityRetainedScoped; @@ -28,6 +30,7 @@ @Module @InstallIn(ActivityRetainedComponent.class) abstract class SavedStateHandleModule { + @OptIn(markerClass = UnstableApi.class) @ActivityRetainedSavedState @ActivityRetainedScoped @Provides diff --git a/java/dagger/hilt/android/lifecycle/ActivityRetainedSavedState.java b/java/dagger/hilt/android/lifecycle/ActivityRetainedSavedState.java index 3d666a803c5..62984353882 100644 --- a/java/dagger/hilt/android/lifecycle/ActivityRetainedSavedState.java +++ b/java/dagger/hilt/android/lifecycle/ActivityRetainedSavedState.java @@ -16,11 +16,16 @@ package dagger.hilt.android.lifecycle; +import dagger.hilt.android.UnstableApi; import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import javax.inject.Qualifier; /** Qualifies a binding that belongs to ActivityRetainedComponent. */ @Qualifier @Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD}) +@UnstableApi +@Retention(RetentionPolicy.CLASS) public @interface ActivityRetainedSavedState {} diff --git a/java/dagger/hilt/android/lifecycle/BUILD b/java/dagger/hilt/android/lifecycle/BUILD index b13ed20bd94..4452c35a908 100644 --- a/java/dagger/hilt/android/lifecycle/BUILD +++ b/java/dagger/hilt/android/lifecycle/BUILD @@ -59,7 +59,10 @@ android_library( android_library( name = "activity_retained_saved_state", srcs = ["ActivityRetainedSavedState.java"], - deps = ["//third_party/java/jsr330_inject"], + deps = [ + "//java/dagger/hilt/android:unstable_api", + "//third_party/java/jsr330_inject", + ], ) compat_kt_android_library( diff --git a/java/dagger/hilt/android/testing/BUILD b/java/dagger/hilt/android/testing/BUILD index 6c3c6e56409..807dc19fd66 100644 --- a/java/dagger/hilt/android/testing/BUILD +++ b/java/dagger/hilt/android/testing/BUILD @@ -220,6 +220,7 @@ gen_maven_artifact( artifact_target_maven_deps = [ "androidx.activity:activity", "androidx.annotation:annotation", + "androidx.annotation:annotation-experimental", "androidx.fragment:fragment", "androidx.lifecycle:lifecycle-common", "androidx.lifecycle:lifecycle-viewmodel", diff --git a/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleActivity.java b/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleActivity.java index d1bab2f8236..ad6e7389919 100644 --- a/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleActivity.java +++ b/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleActivity.java @@ -18,20 +18,30 @@ import android.os.Bundle; import android.widget.TextView; +import androidx.annotation.OptIn; import androidx.appcompat.app.AppCompatActivity; +import androidx.lifecycle.SavedStateHandle; import androidx.lifecycle.ViewModelProvider; import dagger.hilt.android.AndroidEntryPoint; +import dagger.hilt.android.UnstableApi; +import dagger.hilt.android.lifecycle.ActivityRetainedSavedState; +import javax.inject.Inject; /** The main activity of the application. */ +@OptIn(markerClass = UnstableApi.class) @AndroidEntryPoint public class SimpleActivity extends AppCompatActivity { private static final String TAG = SimpleActivity.class.getSimpleName(); + @Inject + @ActivityRetainedSavedState + SavedStateHandle savedStateHandle; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SimpleViewModel viewModel = new ViewModelProvider(this).get(SimpleViewModel.class); - + savedStateHandle.set("some_key", "some_content"); setContentView(R.layout.activity_main); ((TextView) findViewById(R.id.greeting)) diff --git a/javatests/artifacts/hilt-android/viewmodel/build.gradle b/javatests/artifacts/hilt-android/viewmodel/build.gradle index 06578404369..e726eced711 100644 --- a/javatests/artifacts/hilt-android/viewmodel/build.gradle +++ b/javatests/artifacts/hilt-android/viewmodel/build.gradle @@ -16,7 +16,7 @@ buildscript { ext { - hilt_version = '2.48.1' + hilt_version = 'LOCAL-SNAPSHOT' // AGP is set below 7.2.0 on purpose to be able to obfuscate debug apk. agp_version = "7.1.2" kotlin_version = '1.9.0' diff --git a/javatests/dagger/hilt/android/BUILD b/javatests/dagger/hilt/android/BUILD index cd9d974b6e7..b3da0d2a622 100644 --- a/javatests/dagger/hilt/android/BUILD +++ b/javatests/dagger/hilt/android/BUILD @@ -589,6 +589,7 @@ android_local_test( "//java/dagger/hilt:install_in", "//java/dagger/hilt/android:android_entry_point", "//java/dagger/hilt/android:package_info", + "//java/dagger/hilt/android:unstable_api", "//java/dagger/hilt/android/lifecycle:activity_retained_saved_state", "//java/dagger/hilt/android/lifecycle:hilt_view_model", "//java/dagger/hilt/android/scopes", @@ -596,6 +597,7 @@ android_local_test( "//third_party/java/jsr330_inject", "//third_party/java/truth", "@maven//:androidx_activity_activity", + "@maven//:androidx_annotation_annotation_experimental", "@maven//:androidx_fragment_fragment", "@maven//:androidx_lifecycle_lifecycle_common", "@maven//:androidx_lifecycle_lifecycle_viewmodel", diff --git a/javatests/dagger/hilt/android/ViewModelSavedStateOwnerTest.java b/javatests/dagger/hilt/android/ViewModelSavedStateOwnerTest.java index f747329894f..3e522b9dd3f 100644 --- a/javatests/dagger/hilt/android/ViewModelSavedStateOwnerTest.java +++ b/javatests/dagger/hilt/android/ViewModelSavedStateOwnerTest.java @@ -24,6 +24,7 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.annotation.Nullable; +import androidx.annotation.OptIn; import androidx.lifecycle.SavedStateHandle; import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModelProvider; @@ -45,6 +46,7 @@ import org.robolectric.annotation.Config; /** Test that you can use the Hilt ViewModel factory with other owners. */ +@OptIn(markerClass = UnstableApi.class) @HiltAndroidTest @RunWith(AndroidJUnit4.class) // Robolectric requires Java9 to run API 29 and above, so use API 28 instead