From 7acd97f63fdd4fad9fbbdbf7e941dda03b04e44a Mon Sep 17 00:00:00 2001 From: Zachary Heidepriem Date: Fri, 6 Apr 2018 12:28:36 -0700 Subject: Reorganized rootcomponentgenerator package and added demo. -Moved rootcomponentgenerator/annotations to dialer/inject to reduce the number of deps engineers need to include -Move rootcomponentgenerator/processor into rootcomponentgenerator/ since the other package is now gone -Added inject/demo and tests TEST=unit, tap Test: unit, tap PiperOrigin-RevId: 191916595 Change-Id: Ic155808c7435bdce198970caa92309edb9fadac8 --- java/com/android/dialer/inject/ContextModule.java | 1 + .../android/dialer/inject/DialerRootComponent.java | 44 +++++++++++++++++ java/com/android/dialer/inject/DialerVariant.java | 44 +++++++++++++++++ .../android/dialer/inject/IncludeInDialerRoot.java | 47 ++++++++++++++++++ java/com/android/dialer/inject/InstallIn.java | 40 ++++++++++++++++ .../inject/RootComponentGeneratorMetadata.java | 31 ++++++++++++ .../dialer/inject/demo/DemoDaggerApplication.java | 55 ++++++++++++++++++++++ .../com/android/dialer/inject/demo/DemoModule.java | 35 ++++++++++++++ .../com/android/dialer/inject/demo/DemoObject.java | 32 +++++++++++++ .../dialer/inject/demo/DemoSubcomponent.java | 40 ++++++++++++++++ 10 files changed, 369 insertions(+) create mode 100644 java/com/android/dialer/inject/DialerRootComponent.java create mode 100644 java/com/android/dialer/inject/DialerVariant.java create mode 100644 java/com/android/dialer/inject/IncludeInDialerRoot.java create mode 100644 java/com/android/dialer/inject/InstallIn.java create mode 100644 java/com/android/dialer/inject/RootComponentGeneratorMetadata.java create mode 100644 java/com/android/dialer/inject/demo/DemoDaggerApplication.java create mode 100644 java/com/android/dialer/inject/demo/DemoModule.java create mode 100644 java/com/android/dialer/inject/demo/DemoObject.java create mode 100644 java/com/android/dialer/inject/demo/DemoSubcomponent.java (limited to 'java/com/android/dialer/inject') diff --git a/java/com/android/dialer/inject/ContextModule.java b/java/com/android/dialer/inject/ContextModule.java index e3da08edc..828f97830 100644 --- a/java/com/android/dialer/inject/ContextModule.java +++ b/java/com/android/dialer/inject/ContextModule.java @@ -24,6 +24,7 @@ import dagger.Provides; /** Provides the singleton application context object. */ @Module +@InstallIn(variants = {DialerVariant.DIALER_DEMO, DialerVariant.DIALER_TEST}) public final class ContextModule { @NonNull private final Context context; diff --git a/java/com/android/dialer/inject/DialerRootComponent.java b/java/com/android/dialer/inject/DialerRootComponent.java new file mode 100644 index 000000000..11dbf962e --- /dev/null +++ b/java/com/android/dialer/inject/DialerRootComponent.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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 com.android.dialer.inject; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotates the place with this annotation when a RootComponent is needed. + * + *

Usually users put this annotation on application class that is root of dependencies (the last + * thing to compile). The annotation processor will figure out what it needs to generate a variant + * root through dependencies. + * + *

Example: + * + *

+ * 
+ * @DialerRootComponent(variant = DialerVariant.DIALER_AOSP)
+ * public class RootDialerAosp {}
+ * 
+ * 
+ */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.SOURCE) +public @interface DialerRootComponent { + DialerVariant variant(); +} diff --git a/java/com/android/dialer/inject/DialerVariant.java b/java/com/android/dialer/inject/DialerVariant.java new file mode 100644 index 000000000..2e5794ffb --- /dev/null +++ b/java/com/android/dialer/inject/DialerVariant.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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 com.android.dialer.inject; + +/** Represents all dialer variants. */ +public enum DialerVariant { + // AOSP Dialer variants + DIALER_AOSP("DialerAosp"), + DIALER_AOSP_ESPRESSO("DialerAospEspresso"), + DIALER_ROBOLECTRIC("DialerRobolectric"), + + + + // TEST variant will be used in situations where we need create in-test application class which + // doesn't belong to any variants listed above + DIALER_TEST("DialerTest"), + // Just for sample code in inject/demo. + DIALER_DEMO("DialerDemo"); + + private final String variant; + + DialerVariant(String variant) { + this.variant = variant; + } + + @Override + public String toString() { + return variant; + } +} diff --git a/java/com/android/dialer/inject/IncludeInDialerRoot.java b/java/com/android/dialer/inject/IncludeInDialerRoot.java new file mode 100644 index 000000000..4e800559a --- /dev/null +++ b/java/com/android/dialer/inject/IncludeInDialerRoot.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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 com.android.dialer.inject; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; + +/** + * Annotates a type that should be included in Dialer Root Component. Typically, annotated types are + * HasComponent interfaces. + * + *

An example: + * + *

+ * 
+ * {@literal @}dagger.Subcomponent
+ * public abstract class SimulatorComponent {
+ *   public static SimulatorComponent get(Context context) {
+ *      return ((HasComponent)((HasRootComponent) context.getApplicationContext()).component())
+ *         .simulatorComponent();
+ *   }
+ *   {@literal @}IncludeInDialerRoot
+ *   public interface HasComponent {
+ *      SimulatorComponent simulatorComponent();
+ *  }
+ * }
+ * 
+ * 
+ */ +@Target(ElementType.TYPE) +public @interface IncludeInDialerRoot { + Class[] modules() default {}; +} diff --git a/java/com/android/dialer/inject/InstallIn.java b/java/com/android/dialer/inject/InstallIn.java new file mode 100644 index 000000000..a6f973b7b --- /dev/null +++ b/java/com/android/dialer/inject/InstallIn.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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 com.android.dialer.inject; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; + +/** + * Annotation for {@link dagger.Module dagger.Modules} which causes them to be installed in the + * specified variants. + * + *

It has a parameter for users to enter on which variants annotated module will be installed and + * also must be non-empty. Example: + * + *

+ * 
+ * @InstallIn(variants = {DialerVariant.DIALER_AOSP, DialerVariant.DIALER_TEST})
+ * public class Module1 {}
+ *
+ * 
+ * 
+ */ +@Target(ElementType.TYPE) +public @interface InstallIn { + DialerVariant[] variants(); +} diff --git a/java/com/android/dialer/inject/RootComponentGeneratorMetadata.java b/java/com/android/dialer/inject/RootComponentGeneratorMetadata.java new file mode 100644 index 000000000..51d134a95 --- /dev/null +++ b/java/com/android/dialer/inject/RootComponentGeneratorMetadata.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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 com.android.dialer.inject; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; + +/** + * Only used by rootcomponent generator to store metadata for locating annotated + * (@DialerComponent, @InstallIn) class. + */ +@Target(ElementType.TYPE) +public @interface RootComponentGeneratorMetadata { + String tag(); + + Class annotatedClass(); +} diff --git a/java/com/android/dialer/inject/demo/DemoDaggerApplication.java b/java/com/android/dialer/inject/demo/DemoDaggerApplication.java new file mode 100644 index 000000000..0c13dbb48 --- /dev/null +++ b/java/com/android/dialer/inject/demo/DemoDaggerApplication.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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 com.android.dialer.inject.demo; + +import android.app.Application; +import android.support.annotation.NonNull; +import com.android.dialer.inject.ContextModule; +import com.android.dialer.inject.DialerRootComponent; +import com.android.dialer.inject.DialerVariant; +import com.android.dialer.inject.HasRootComponent; + +/** Demo dialer dagger application. */ +@DialerRootComponent(variant = DialerVariant.DIALER_DEMO) +public final class DemoDaggerApplication extends Application implements HasRootComponent { + + private volatile Object rootComponent; + + /** Returns a cached instance of application's root component. */ + @Override + @NonNull + public final Object component() { + Object result = rootComponent; + if (result == null) { + synchronized (this) { + result = rootComponent; + if (result == null) { + rootComponent = + result = DaggerDialerDemo.builder().contextModule(new ContextModule(this)).build(); + } + } + } + return result; + } + + @Override + public void onCreate() { + super.onCreate(); + + DemoSubcomponent.get(this).demoObject(); + } +} diff --git a/java/com/android/dialer/inject/demo/DemoModule.java b/java/com/android/dialer/inject/demo/DemoModule.java new file mode 100644 index 000000000..40cd6fea1 --- /dev/null +++ b/java/com/android/dialer/inject/demo/DemoModule.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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 com.android.dialer.inject.demo; + +import com.android.dialer.inject.DialerVariant; +import com.android.dialer.inject.InstallIn; +import dagger.Module; +import dagger.Provides; + +/** Module for demo dagger application. */ +@Module +@InstallIn(variants = DialerVariant.DIALER_DEMO) +public final class DemoModule { + + private DemoModule() {} + + @Provides + static DemoObject provide() { + return new DemoObject("prod"); + } +} diff --git a/java/com/android/dialer/inject/demo/DemoObject.java b/java/com/android/dialer/inject/demo/DemoObject.java new file mode 100644 index 000000000..c6d48a14d --- /dev/null +++ b/java/com/android/dialer/inject/demo/DemoObject.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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 com.android.dialer.inject.demo; + +/** Object used to demonstrate dagger bindings. */ +class DemoObject { + + private final String value; + + DemoObject(String value) { + this.value = value; + } + + @Override + public String toString() { + return value; + } +} diff --git a/java/com/android/dialer/inject/demo/DemoSubcomponent.java b/java/com/android/dialer/inject/demo/DemoSubcomponent.java new file mode 100644 index 000000000..ff000aaa9 --- /dev/null +++ b/java/com/android/dialer/inject/demo/DemoSubcomponent.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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 com.android.dialer.inject.demo; + +import android.content.Context; +import com.android.dialer.inject.HasRootComponent; +import com.android.dialer.inject.IncludeInDialerRoot; +import dagger.Subcomponent; + +/** Subcomponent for the demo dagger application. */ +@Subcomponent +public abstract class DemoSubcomponent { + + abstract DemoObject demoObject(); + + public static DemoSubcomponent get(Context context) { + return ((HasComponent) ((HasRootComponent) context.getApplicationContext()).component()) + .demoSubcomponent(); + } + + /** Used to refer to the root application component. */ + @IncludeInDialerRoot + public interface HasComponent { + DemoSubcomponent demoSubcomponent(); + } +} -- cgit v1.2.3