Skip to content

Commit

Permalink
make ActivityRetainedSaveState an opt-in API.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 586393130
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Nov 29, 2023
1 parent 4dcebb5 commit cd70dea
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 3 deletions.
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions java/dagger/hilt/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions java/dagger/hilt/android/UnstableApi.java
Original file line number Diff line number Diff line change
@@ -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 {}
2 changes: 2 additions & 0 deletions java/dagger/hilt/android/internal/managers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +30,7 @@
@Module
@InstallIn(ActivityRetainedComponent.class)
abstract class SavedStateHandleModule {
@OptIn(markerClass = UnstableApi.class)
@ActivityRetainedSavedState
@ActivityRetainedScoped
@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
5 changes: 4 additions & 1 deletion java/dagger/hilt/android/lifecycle/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions java/dagger/hilt/android/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion javatests/artifacts/hilt-android/viewmodel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions javatests/dagger/hilt/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -589,13 +589,15 @@ 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",
"//java/dagger/hilt/android/testing:hilt_android_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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit cd70dea

Please sign in to comment.