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

Commit 54ac6934 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Adds hardware color theming dependencies to ThemeService." into main

parents ee993dc7 98082fef
Loading
Loading
Loading
Loading
+43 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.server.theming;

import android.annotation.NonNull;
import android.annotation.Nullable;

/**
 * An interface that abstracts the reading of system properties, primarily to facilitate testing.
 * <p>
 * This interface serves as a wrapper around {@link android.os.SystemProperties}, allowing
 * dependencies that rely on system properties to be unit-tested by injecting a mock
 * implementation.
 * <p>
 * In the context of theming, this is used to read device-specific properties like
 * {@code ro.boot.hardware.color}, which can define the default theme for a device out of the box.
 */
public interface SystemPropertiesReader {
    /**
     * Get the String value for the given {@code key}.
     *
     * @param key the key to lookup
     * @param def the default value in case the property is not set or empty
     * @return if the {@code key} isn't found, return {@code def} if it isn't null, or an empty
     * string otherwise
     */
    @NonNull
    String get(@NonNull String key, @Nullable String def);
}
+36 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.server.theming;

import android.annotation.UserIdInt;
import android.app.WallpaperColors;
import android.app.WallpaperManager;

/**
 * An interface that abstracts the reading of wallpaper colors, primarily to facilitate testing.
 * <p>
 * This interface serves as a wrapper around {@link android.app.WallpaperManager}, allowing
 * dependencies that rely on wallpaper colors to be unit-tested by injecting a mock
 * implementation.
 * <p>
 * In the context of theming, this is used to retrieve the colors from the current home or
 * lock screen wallpaper, which can then be used as a seed for generating dynamic color schemes.
 */
interface WallpaperColorsReader {
    WallpaperColors getWallpaperColors(@WallpaperManager.SetWallpaperFlags int which,
            @UserIdInt int userId);
}
 No newline at end of file
+57 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.server.theming;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class HardwareColorRule implements TestRule {
    public String color = "";
    public String[] options = {};
    public boolean isTesting = false;

    @Override
    public Statement apply(Statement base, Description description) {
        HardwareColors hardwareColors = description.getAnnotation(HardwareColors.class);
        if (hardwareColors != null) {
            color = hardwareColors.color();
            options = hardwareColors.options();
            isTesting = true;
        }
        return base;
    }

    @Override
    public String toString() {
        JSONObject obj = new JSONObject();

        try {
            obj.append("color", color);
            JSONArray optionsArray = new JSONArray();
            for (String option : options) {
                optionsArray.put(option);
            }
            obj.append("options", optionsArray);
            return obj.toString();
        } catch (Exception e) {
            return "{}";
        }
    }
}
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.server.theming;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/** Annotation for hardware colors. */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface HardwareColors {
    /** The hardware color name / key */
    String color();

    /**
     * The color options triplets array. eg:<pre>
     * options = {
     *     "RED_DEV|VIBRANT|#FF0000",
     *     "*|TONAL_SPOT|#00FF00"
     * }
     * </pre>
     */
    String[] options();
}