diff options
| author | 2022-08-24 19:05:56 +0000 | |
|---|---|---|
| committer | 2022-09-01 21:02:24 +0000 | |
| commit | 54e3ae5836e088ef2d47c225268137ed2709b3a7 (patch) | |
| tree | 78ce696d51d215f4bf8b4669effb52f118c02593 | |
| parent | d2a46ddf38fd8eb51cb23fa8246c58db0897f5ba (diff) | |
Implement low light dream library.
This change implements a new library that allows clients to push ambient
light mode updates and set a low light dream as the system dream
component during low light.
Bug: 222529147
Test: atest LowLightDreamManagerTest
Test: on device verify that during low light the system dream component
is set to low light dream, and that the system dream component is
cleared when the ambient light is regular
Change-Id: Ib046f32a83c4bf333ba27eab077caef6cc306d6d
9 files changed, 470 insertions, 0 deletions
diff --git a/libs/dream/lowlight/Android.bp b/libs/dream/lowlight/Android.bp new file mode 100644 index 000000000000..5b5b0f07cabd --- /dev/null +++ b/libs/dream/lowlight/Android.bp @@ -0,0 +1,47 @@ +// Copyright (C) 2022 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 { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "frameworks_base_license" + // to get the below license kinds: + // SPDX-license-identifier-Apache-2.0 + default_applicable_licenses: ["frameworks_base_license"], +} + +filegroup { + name: "low_light_dream_lib-sources", + srcs: [ + "src/**/*.java", + ], + path: "src", +} + +android_library { + name: "LowLightDreamLib", + srcs: [ + ":low_light_dream_lib-sources", + ], + resource_dirs: [ + "res", + ], + static_libs: [ + "androidx.arch.core_core-runtime", + "dagger2", + "jsr330", + ], + manifest: "AndroidManifest.xml", + plugins: ["dagger2-compiler"], +} diff --git a/libs/dream/lowlight/AndroidManifest.xml b/libs/dream/lowlight/AndroidManifest.xml new file mode 100644 index 000000000000..a8d952699943 --- /dev/null +++ b/libs/dream/lowlight/AndroidManifest.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2022 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. +--> + +<manifest package="com.android.dream.lowlight" /> diff --git a/libs/dream/lowlight/res/values/config.xml b/libs/dream/lowlight/res/values/config.xml new file mode 100644 index 000000000000..70fe0738a6f4 --- /dev/null +++ b/libs/dream/lowlight/res/values/config.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright (C) 2022 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. + --> +<resources> + <!-- The dream component used when the device is low light environment. --> + <string translatable="false" name="config_lowLightDreamComponent"/> +</resources> diff --git a/libs/dream/lowlight/src/com/android/dream/lowlight/LowLightDreamManager.java b/libs/dream/lowlight/src/com/android/dream/lowlight/LowLightDreamManager.java new file mode 100644 index 000000000000..5ecec4ddd1ad --- /dev/null +++ b/libs/dream/lowlight/src/com/android/dream/lowlight/LowLightDreamManager.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2022 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.dream.lowlight; + +import static com.android.dream.lowlight.dagger.LowLightDreamModule.LOW_LIGHT_DREAM_COMPONENT; + +import android.annotation.IntDef; +import android.annotation.RequiresPermission; +import android.app.DreamManager; +import android.content.ComponentName; +import android.util.Log; + +import androidx.annotation.Nullable; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import javax.inject.Inject; +import javax.inject.Named; + +/** + * Maintains the ambient light mode of the environment the device is in, and sets a low light dream + * component, if present, as the system dream when the ambient light mode is low light. + * + * @hide + */ +public final class LowLightDreamManager { + private static final String TAG = "LowLightDreamManager"; + private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); + + /** + * @hide + */ + @Retention(RetentionPolicy.SOURCE) + @IntDef(prefix = { "AMBIENT_LIGHT_MODE_" }, value = { + AMBIENT_LIGHT_MODE_UNKNOWN, + AMBIENT_LIGHT_MODE_REGULAR, + AMBIENT_LIGHT_MODE_LOW_LIGHT + }) + public @interface AmbientLightMode {} + + /** + * Constant for ambient light mode being unknown. + * @hide + */ + public static final int AMBIENT_LIGHT_MODE_UNKNOWN = 0; + + /** + * Constant for ambient light mode being regular / bright. + * @hide + */ + public static final int AMBIENT_LIGHT_MODE_REGULAR = 1; + + /** + * Constant for ambient light mode being low light / dim. + * @hide + */ + public static final int AMBIENT_LIGHT_MODE_LOW_LIGHT = 2; + + private final DreamManager mDreamManager; + + @Nullable + private final ComponentName mLowLightDreamComponent; + + private int mAmbientLightMode = AMBIENT_LIGHT_MODE_UNKNOWN; + + @Inject + public LowLightDreamManager( + DreamManager dreamManager, + @Named(LOW_LIGHT_DREAM_COMPONENT) @Nullable ComponentName lowLightDreamComponent) { + mDreamManager = dreamManager; + mLowLightDreamComponent = lowLightDreamComponent; + } + + /** + * Sets the current ambient light mode. + * @hide + */ + @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) + public void setAmbientLightMode(@AmbientLightMode int ambientLightMode) { + if (mLowLightDreamComponent == null) { + if (DEBUG) { + Log.d(TAG, "ignore ambient light mode change because low light dream component " + + "is empty"); + } + return; + } + + if (mAmbientLightMode == ambientLightMode) { + return; + } + + if (DEBUG) { + Log.d(TAG, "ambient light mode changed from " + mAmbientLightMode + " to " + + ambientLightMode); + } + + mAmbientLightMode = ambientLightMode; + + mDreamManager.setSystemDreamComponent(mAmbientLightMode == AMBIENT_LIGHT_MODE_LOW_LIGHT + ? mLowLightDreamComponent : null); + } +} diff --git a/libs/dream/lowlight/src/com/android/dream/lowlight/dagger/LowLightDreamModule.java b/libs/dream/lowlight/src/com/android/dream/lowlight/dagger/LowLightDreamModule.java new file mode 100644 index 000000000000..c183a04cb2f9 --- /dev/null +++ b/libs/dream/lowlight/src/com/android/dream/lowlight/dagger/LowLightDreamModule.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2022 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.dream.lowlight.dagger; + +import android.app.DreamManager; +import android.content.ComponentName; +import android.content.Context; + +import androidx.annotation.Nullable; + +import com.android.dream.lowlight.R; + +import javax.inject.Named; + +import dagger.Module; +import dagger.Provides; + +/** + * Dagger module for low light dream. + * + * @hide + */ +@Module +public interface LowLightDreamModule { + String LOW_LIGHT_DREAM_COMPONENT = "low_light_dream_component"; + + /** + * Provides dream manager. + */ + @Provides + static DreamManager providesDreamManager(Context context) { + return context.getSystemService(DreamManager.class); + } + + /** + * Provides the component name of the low light dream, or null if not configured. + */ + @Provides + @Named(LOW_LIGHT_DREAM_COMPONENT) + @Nullable + static ComponentName providesLowLightDreamComponent(Context context) { + final String lowLightDreamComponent = context.getResources().getString( + R.string.config_lowLightDreamComponent); + return lowLightDreamComponent.isEmpty() ? null + : ComponentName.unflattenFromString(lowLightDreamComponent); + } +} diff --git a/libs/dream/lowlight/tests/Android.bp b/libs/dream/lowlight/tests/Android.bp new file mode 100644 index 000000000000..bd6f05eabac5 --- /dev/null +++ b/libs/dream/lowlight/tests/Android.bp @@ -0,0 +1,45 @@ +// Copyright (C) 2022 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 { + default_applicable_licenses: ["frameworks_base_license"], +} + +android_test { + name: "LowLightDreamTests", + srcs: [ + "**/*.java", + ], + static_libs: [ + "LowLightDreamLib", + "androidx.test.runner", + "androidx.test.rules", + "androidx.test.ext.junit", + "frameworks-base-testutils", + "junit", + "mockito-target-extended-minus-junit4", + "platform-test-annotations", + "testables", + "truth-prebuilt", + ], + libs: [ + "android.test.mock", + "android.test.base", + "android.test.runner", + ], + jni_libs: [ + "libdexmakerjvmtiagent", + "libstaticjvmtiagent", + ], +} diff --git a/libs/dream/lowlight/tests/AndroidManifest.xml b/libs/dream/lowlight/tests/AndroidManifest.xml new file mode 100644 index 000000000000..abb71fb53b49 --- /dev/null +++ b/libs/dream/lowlight/tests/AndroidManifest.xml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2022 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. +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:androidprv="http://schemas.android.com/apk/prv/res/android" + xmlns:tools="http://schemas.android.com/tools" + package="com.android.dream.lowlight.tests"> + + <application android:debuggable="true" android:largeHeap="true"> + <uses-library android:name="android.test.mock" /> + <uses-library android:name="android.test.runner" /> + </application> + + <instrumentation + android:name="androidx.test.runner.AndroidJUnitRunner" + android:label="Tests for LowLightDreamLib" + android:targetPackage="com.android.dream.lowlight.tests"> + </instrumentation> + +</manifest> diff --git a/libs/dream/lowlight/tests/AndroidTest.xml b/libs/dream/lowlight/tests/AndroidTest.xml new file mode 100644 index 000000000000..10800333add2 --- /dev/null +++ b/libs/dream/lowlight/tests/AndroidTest.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2022 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. +--> +<configuration description="Runs Tests for LowLightDreamLib"> + <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller"> + <option name="cleanup-apks" value="true" /> + <option name="install-arg" value="-t" /> + <option name="test-file-name" value="LowLightDreamTests.apk" /> + </target_preparer> + + <option name="test-suite-tag" value="apct" /> + <option name="test-suite-tag" value="framework-base-presubmit" /> + <option name="test-tag" value="LowLightDreamLibTests" /> + <test class="com.android.tradefed.testtype.AndroidJUnitTest" > + <option name="package" value="com.android.dream.lowlight.tests" /> + <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" /> + <option name="hidden-api-checks" value="false"/> + </test> +</configuration> diff --git a/libs/dream/lowlight/tests/src/com.android.dream.lowlight/LowLightDreamManagerTest.java b/libs/dream/lowlight/tests/src/com.android.dream.lowlight/LowLightDreamManagerTest.java new file mode 100644 index 000000000000..91a170f7ae14 --- /dev/null +++ b/libs/dream/lowlight/tests/src/com.android.dream.lowlight/LowLightDreamManagerTest.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2022 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.dream.lowlight; + +import static com.android.dream.lowlight.LowLightDreamManager.AMBIENT_LIGHT_MODE_LOW_LIGHT; +import static com.android.dream.lowlight.LowLightDreamManager.AMBIENT_LIGHT_MODE_REGULAR; +import static com.android.dream.lowlight.LowLightDreamManager.AMBIENT_LIGHT_MODE_UNKNOWN; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.clearInvocations; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +import android.app.DreamManager; +import android.content.ComponentName; +import android.testing.AndroidTestingRunner; + +import androidx.test.filters.SmallTest; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +@SmallTest +@RunWith(AndroidTestingRunner.class) +public class LowLightDreamManagerTest { + @Mock + private DreamManager mDreamManager; + + @Mock + private ComponentName mDreamComponent; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void setAmbientLightMode_lowLight_setSystemDream() { + final LowLightDreamManager lowLightDreamManager = new LowLightDreamManager(mDreamManager, + mDreamComponent); + + lowLightDreamManager.setAmbientLightMode(AMBIENT_LIGHT_MODE_LOW_LIGHT); + + verify(mDreamManager).setSystemDreamComponent(mDreamComponent); + } + + @Test + public void setAmbientLightMode_regularLight_clearSystemDream() { + final LowLightDreamManager lowLightDreamManager = new LowLightDreamManager(mDreamManager, + mDreamComponent); + + lowLightDreamManager.setAmbientLightMode(AMBIENT_LIGHT_MODE_REGULAR); + + verify(mDreamManager).setSystemDreamComponent(null); + } + + @Test + public void setAmbientLightMode_defaultUnknownMode_clearSystemDream() { + final LowLightDreamManager lowLightDreamManager = new LowLightDreamManager(mDreamManager, + mDreamComponent); + + // Set to low light first. + lowLightDreamManager.setAmbientLightMode(AMBIENT_LIGHT_MODE_LOW_LIGHT); + clearInvocations(mDreamManager); + + // Return to default unknown mode. + lowLightDreamManager.setAmbientLightMode(AMBIENT_LIGHT_MODE_UNKNOWN); + + verify(mDreamManager).setSystemDreamComponent(null); + } + + @Test + public void setAmbientLightMode_dreamComponentNotSet_doNothing() { + final LowLightDreamManager lowLightDreamManager = new LowLightDreamManager(mDreamManager, + null /*dream component*/); + + lowLightDreamManager.setAmbientLightMode(AMBIENT_LIGHT_MODE_LOW_LIGHT); + + verify(mDreamManager, never()).setSystemDreamComponent(any()); + } +} |