diff --git a/samples/AconfigDemo/Android.bp b/samples/AconfigDemo/Android.bp index 8bd0e75d3..a407f3493 100644 --- a/samples/AconfigDemo/Android.bp +++ b/samples/AconfigDemo/Android.bp @@ -5,11 +5,15 @@ package { android_app { name: "AconfigDemoActivity", manifest: "AndroidManifest.xml", - srcs: ["src/com/example/android/aconfig/demo/activity/AconfigDemoActivity.java"], + srcs: [ + "src/**/*.java" + ], certificate: "platform", sdk_version: "current", static_libs: [ - "ContentProvider", + "ContentLibs", + "dagger2", + "jsr330", ], optimize: { enabled: true, @@ -19,11 +23,13 @@ android_app { obfuscate: false, shrink_resources: true, }, + + plugins: ["dagger2-compiler"] } aconfig_declarations { name: "aconfig_demo_flags", - package: "com.example.android.aconfig.demo.activity", + package: "com.example.android.aconfig.demo.flags", srcs: ["aconfig_demo_flags.aconfig"], } @@ -34,12 +40,13 @@ java_aconfig_library { } java_library { - name: "ContentProvider", + name: "ContentLibs", srcs: [ - "src/com/example/android/aconfig/demo/activity/ContentProvider.java", + "lib/**/*.java", ], sdk_version: "current", static_libs: [ "aconfig_demo_flags_lib", ], + libs: ["jsr330"], } diff --git a/samples/AconfigDemo/AndroidManifest.xml b/samples/AconfigDemo/AndroidManifest.xml index b2320b3f7..302149189 100644 --- a/samples/AconfigDemo/AndroidManifest.xml +++ b/samples/AconfigDemo/AndroidManifest.xml @@ -20,9 +20,11 @@ own application, the package name must be changed from "com.example.*" to come from a domain that you own or have control over. --> + package="com.example.android.aconfig.demo"> - + diff --git a/samples/AconfigDemo/aconfig_demo_flags.aconfig b/samples/AconfigDemo/aconfig_demo_flags.aconfig index ecf699329..0ebf8c16d 100644 --- a/samples/AconfigDemo/aconfig_demo_flags.aconfig +++ b/samples/AconfigDemo/aconfig_demo_flags.aconfig @@ -1,7 +1,13 @@ -package: "com.example.android.aconfig.demo.activity" +package: "com.example.android.aconfig.demo.flags" flag { - name: "append_content" + name: "append_injected_content" namespace: "aconfig_demo_ns" - description: "This flag is buildtime flag" + description: "This flag controls injected content" +} + +flag { + name: "append_static_content" + namespace: "aconfig_demo_ns" + description: "This flag controls static content" } diff --git a/samples/AconfigDemo/lib/com/example/android/aconfig/demo/InjectedContent.java b/samples/AconfigDemo/lib/com/example/android/aconfig/demo/InjectedContent.java new file mode 100644 index 000000000..6da4c68b2 --- /dev/null +++ b/samples/AconfigDemo/lib/com/example/android/aconfig/demo/InjectedContent.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2023 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.example.android.aconfig.demo; + +import com.example.android.aconfig.demo.flags.FeatureFlags; +import javax.inject.Inject; + + +public class InjectedContent { + + private FeatureFlags featureFlags; + + @Inject + public InjectedContent(FeatureFlags featureFlags) { + this.featureFlags = featureFlags; + }; + + public String getContent() { + + StringBuilder sBuffer = new StringBuilder(); + + if (featureFlags.appendInjectedContent()) { + sBuffer.append("The flag: appendInjectedContent is ON!!\n\n"); + } else { + sBuffer.append("The flag: appendInjectedContent is OFF!!\n\n"); + } + + return sBuffer.toString(); + } +} + diff --git a/samples/AconfigDemo/src/com/example/android/aconfig/demo/activity/ContentProvider.java b/samples/AconfigDemo/lib/com/example/android/aconfig/demo/StaticContent.java similarity index 63% rename from samples/AconfigDemo/src/com/example/android/aconfig/demo/activity/ContentProvider.java rename to samples/AconfigDemo/lib/com/example/android/aconfig/demo/StaticContent.java index f6d2599a7..240808798 100644 --- a/samples/AconfigDemo/src/com/example/android/aconfig/demo/activity/ContentProvider.java +++ b/samples/AconfigDemo/lib/com/example/android/aconfig/demo/StaticContent.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007 The Android Open Source Project + * Copyright (C) 2023 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. @@ -14,23 +14,22 @@ * limitations under the License. */ -package com.example.android.aconfig.demo.activity; +package com.example.android.aconfig.demo; -/** - * A minimal "Hello, World!" application. - */ -public class ContentProvider { +import static com.example.android.aconfig.demo.flags.Flags.appendStaticContent; - public ContentProvider() {}; +public class StaticContent { + + public StaticContent() {}; public String getContent() { StringBuilder sBuffer = new StringBuilder(); - if (Flags.appendContent()) { - sBuffer.append("The flag is ON!!\n"); + if (appendStaticContent()) { + sBuffer.append("The flag: appendStaticContent is ON!!\n\n"); } else { - sBuffer.append("The flag is OFF!!\n"); + sBuffer.append("The flag: appendStaticContent is OFF!!\n\n"); } return sBuffer.toString(); diff --git a/samples/AconfigDemo/src/com/example/android/aconfig/demo/activity/AconfigDemoActivity.java b/samples/AconfigDemo/src/com/example/android/aconfig/demo/AconfigDemoActivity.java similarity index 77% rename from samples/AconfigDemo/src/com/example/android/aconfig/demo/activity/AconfigDemoActivity.java rename to samples/AconfigDemo/src/com/example/android/aconfig/demo/AconfigDemoActivity.java index 523380f8c..0f96370d2 100644 --- a/samples/AconfigDemo/src/com/example/android/aconfig/demo/activity/AconfigDemoActivity.java +++ b/samples/AconfigDemo/src/com/example/android/aconfig/demo/AconfigDemoActivity.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007 The Android Open Source Project + * Copyright (C) 2023 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. @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.example.android.aconfig.demo.activity; +package com.example.android.aconfig.demo; import android.app.Activity; import android.os.Bundle; @@ -22,24 +22,30 @@ import android.view.View; import android.widget.TextView; import android.view.WindowManager; +import javax.inject.Inject; + /** * A minimal "Hello, World!" application. */ public class AconfigDemoActivity extends Activity { + @Inject InjectedContent injectedContent; /** * Called with the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { + ((AconfigDemoApplication)getApplicationContext()).appComponent.inject(this); super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView simpleTextView = (TextView) findViewById(R.id.simpleTextView); simpleTextView.setText("Show Flags: \n\n"); - ContentProvider cp = new ContentProvider(); + StaticContent cp = new StaticContent(); simpleTextView.append(cp.getContent()); + + simpleTextView.append(injectedContent.getContent()); } } diff --git a/samples/AconfigDemo/src/com/example/android/aconfig/demo/AconfigDemoApplication.java b/samples/AconfigDemo/src/com/example/android/aconfig/demo/AconfigDemoApplication.java new file mode 100644 index 000000000..e4b89d7cb --- /dev/null +++ b/samples/AconfigDemo/src/com/example/android/aconfig/demo/AconfigDemoApplication.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2023 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.example.android.aconfig.demo; + +import android.app.Application; +import com.example.android.aconfig.demo.dagger.ApplicationComponent; +import com.example.android.aconfig.demo.dagger.DaggerApplicationComponent; +/** + * A minimal "Hello, World!" application. + */ +public class AconfigDemoApplication extends Application { + ApplicationComponent appComponent = DaggerApplicationComponent.create(); +} + diff --git a/samples/AconfigDemo/src/com/example/android/aconfig/demo/dagger/AconfigDemoFlagModule.java b/samples/AconfigDemo/src/com/example/android/aconfig/demo/dagger/AconfigDemoFlagModule.java new file mode 100644 index 000000000..d5809b7df --- /dev/null +++ b/samples/AconfigDemo/src/com/example/android/aconfig/demo/dagger/AconfigDemoFlagModule.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2023 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.example.android.aconfig.demo.dagger; + +import com.example.android.aconfig.demo.flags.FeatureFlags; +import com.example.android.aconfig.demo.flags.FeatureFlagsImpl; +import dagger.Module; +import dagger.Provides; + +@Module +public class AconfigDemoFlagModule { + @Provides + static FeatureFlags provideFeatureFlags() { + return new FeatureFlagsImpl(); + } +} diff --git a/samples/AconfigDemo/src/com/example/android/aconfig/demo/dagger/ApplicationComponent.java b/samples/AconfigDemo/src/com/example/android/aconfig/demo/dagger/ApplicationComponent.java new file mode 100644 index 000000000..70d54af3f --- /dev/null +++ b/samples/AconfigDemo/src/com/example/android/aconfig/demo/dagger/ApplicationComponent.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2023 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.example.android.aconfig.demo.dagger; + +import com.example.android.aconfig.demo.AconfigDemoActivity; +import dagger.Component; + +@Component(modules = {AconfigDemoFlagModule.class}) +public interface ApplicationComponent { + void inject(AconfigDemoActivity aconfigDemoActivity); +} diff --git a/samples/AconfigDemo/tests/Android.bp b/samples/AconfigDemo/tests/Android.bp index 12efabde1..e4bb718f5 100644 --- a/samples/AconfigDemo/tests/Android.bp +++ b/samples/AconfigDemo/tests/Android.bp @@ -3,12 +3,13 @@ package { } android_test { - name: "AconfigDemo_ContentProvide_Test", - srcs: ["src/com/example/android/aconfig/demo/activity/ContentProviderTests.java"], + name: "AconfigDemo_ContentLibs_Test", + srcs: ["src/**/*.java"], certificate: "platform", static_libs: [ - "ContentProvider", + "ContentLibs", "junit", + "mockito-target-minus-junit4", "androidx.test.runner", ], manifest: "AndroidManifest.xml", diff --git a/samples/AconfigDemo/tests/AndroidManifest.xml b/samples/AconfigDemo/tests/AndroidManifest.xml index fcc480e90..e9168e7d9 100644 --- a/samples/AconfigDemo/tests/AndroidManifest.xml +++ b/samples/AconfigDemo/tests/AndroidManifest.xml @@ -15,7 +15,7 @@ --> + package="com.example.android.aconfig.demo.tests">