Merge "Add Injection use case"

This commit is contained in:
Zhi Dou
2023-06-26 13:57:30 +00:00
committed by Gerrit Code Review
13 changed files with 208 additions and 31 deletions

View File

@@ -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"],
}

View File

@@ -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. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.aconfig.demo.activity">
package="com.example.android.aconfig.demo">
<uses-permission android:name="android.permission.READ_DEVICE_CONFIG" />
<application android:label="Hello!">
<application
android:name="AconfigDemoApplication"
android:label="Hello!">
<activity android:name="AconfigDemoActivity"
android:exported="true">
<intent-filter>

View File

@@ -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"
}

View File

@@ -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();
}
}

View File

@@ -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();

View File

@@ -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());
}
}

View File

@@ -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();
}

View File

@@ -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();
}
}

View File

@@ -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);
}

View File

@@ -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",

View File

@@ -15,7 +15,7 @@
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.aconfig.demo.activity.tests">
package="com.example.android.aconfig.demo.tests">
<uses-permission android:name="android.permission.READ_DEVICE_CONFIG" />
<!-- We add an application tag here just so that we can indicate that
@@ -26,7 +26,7 @@
</application>
<instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
android:targetPackage="com.example.android.aconfig.demo.activity.tests"
android:targetPackage="com.example.android.aconfig.demo.tests"
android:label="AconfigDemoActivity sample tests">
</instrumentation>
</manifest>

View File

@@ -0,0 +1,26 @@
package com.example.android.aconfig.demo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.mock;
import com.example.android.aconfig.demo.flags.FeatureFlags;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public final class InjectedContentTests {
@Test
public void testInjectedContentFlagOn() throws Exception {
FeatureFlags fakeFeatureFlag = mock(FeatureFlags.class);
when(fakeFeatureFlag.appendInjectedContent()).thenReturn(true);
InjectedContent injectedContent = new InjectedContent(fakeFeatureFlag);
StringBuilder expected = new StringBuilder();
expected.append("The flag: appendInjectedContent is ON!!\n\n");
assertEquals("Get appendInjectedContent", expected.toString(), injectedContent.getContent());
}
}

View File

@@ -1,17 +1,19 @@
package com.example.android.aconfig.demo.activity;
package com.example.android.aconfig.demo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import com.example.android.aconfig.demo.flags.Flags;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public final class ContentProviderTests {
public final class StaticContentTests {
@Test
public void testFlag() {
assertFalse(Flags.appendContent());
assertFalse(Flags.appendStaticContent());
}
}