Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 25f7ef12 authored by Darrell Shi's avatar Darrell Shi Committed by Automerger Merge Worker
Browse files

Merge "Implement low light dream library." into tm-qpr-dev am: bde7caf8

parents 06996043 bde7caf8
Loading
Loading
Loading
Loading
+47 −0
Original line number Diff line number Diff line
// 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"],
}
+18 −0
Original line number Diff line number Diff line
<?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" />
+20 −0
Original line number Diff line number Diff line
<?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>
+117 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}
+61 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}
Loading