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

Commit ac6d8cf3 authored by George Lin's avatar George Lin Committed by Android (Google) Code Review
Browse files

Merge "Move PreviewFragment2 to WPP2 (2/3)" into udc-qpr-dev

parents 5c76e653 fb51e88a
Loading
Loading
Loading
Loading
+0 −68
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.customization.model.color

import android.content.Context
import android.content.pm.PackageManager
import android.content.res.Resources
import android.os.SystemProperties
import android.util.Log
import androidx.annotation.ColorInt

/** Utility to wrap Monet's color extraction */
object ColorUtils {
    private const val TAG = "ColorUtils"
    private const val MONET_FLAG = "flag_monet"
    private var sSysuiRes: Resources? = null
    private var sFlagId = 0

    /** Returns true if color extraction is enabled in systemui. */
    @JvmStatic
    fun isMonetEnabled(context: Context): Boolean {
        var monetEnabled = SystemProperties.getBoolean("persist.systemui.flag_monet", false)
        if (!monetEnabled) {
            if (sSysuiRes == null) {
                try {
                    val pm = context.packageManager
                    val sysUIInfo =
                        pm.getApplicationInfo(
                            "com.android.systemui",
                            PackageManager.GET_META_DATA or PackageManager.MATCH_SYSTEM_ONLY
                        )
                    if (sysUIInfo != null) {
                        sSysuiRes = pm.getResourcesForApplication(sysUIInfo)
                    }
                } catch (e: PackageManager.NameNotFoundException) {
                    Log.w(TAG, "Couldn't read color flag, skipping section", e)
                }
            }
            if (sFlagId == 0) {
                sFlagId =
                    if (sSysuiRes == null) 0
                    else sSysuiRes!!.getIdentifier(MONET_FLAG, "bool", "com.android.systemui")
            }
            if (sFlagId > 0) {
                monetEnabled = sSysuiRes!!.getBoolean(sFlagId)
            }
        }
        return monetEnabled
    }

    @JvmStatic
    fun toColorString(@ColorInt color: Int): String {
        return String.format("%06X", 0xFFFFFF and color)
    }
}
+0 −52
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.customization.model.color;

import android.app.WallpaperColors;
import android.content.Context;
import android.util.SparseIntArray;
import android.widget.RemoteViews.ColorResources;

import com.android.systemui.monet.ColorScheme;
import com.android.systemui.monet.TonalPalette;

/** A class to override colors in a {@link Context} with wallpaper colors. */
public class WallpaperColorResources {

    private final SparseIntArray mColorOverlay = new SparseIntArray();

    public WallpaperColorResources(WallpaperColors wallpaperColors) {
        ColorScheme wallpaperColorScheme = new ColorScheme(wallpaperColors, /* darkTheme= */ false);
        addOverlayColor(wallpaperColorScheme.getNeutral1(), android.R.color.system_neutral1_10);
        addOverlayColor(wallpaperColorScheme.getNeutral2(), android.R.color.system_neutral2_10);
        addOverlayColor(wallpaperColorScheme.getAccent1(), android.R.color.system_accent1_10);
        addOverlayColor(wallpaperColorScheme.getAccent2(), android.R.color.system_accent2_10);
        addOverlayColor(wallpaperColorScheme.getAccent3(), android.R.color.system_accent3_10);
    }

    /** Applies the wallpaper color resources to the {@code context}. */
    public void apply(Context context) {
        ColorResources.create(context, mColorOverlay).apply(context);
    }

    private void addOverlayColor(TonalPalette colorSchemehue, int firstResourceColorId) {
        int resourceColorId = firstResourceColorId;
        for (int color : colorSchemehue.getAllShades()) {
            mColorOverlay.put(resourceColorId, color);
            resourceColorId++;
        }
    }
}
+7 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelProvider
import com.android.customization.model.color.ColorCustomizationManager
import com.android.customization.model.color.ColorOptionsProvider
import com.android.customization.model.color.ColorOptionsProvider.COLOR_SOURCE_PRESET
import com.android.customization.model.grid.GridOptionsManager
import com.android.customization.model.grid.data.repository.GridRepositoryImpl
import com.android.customization.model.grid.domain.interactor.GridInteractor
@@ -602,6 +603,12 @@ internal constructor(
                .also { gridSnapshotRestorer = it }
    }

    override fun isCurrentSelectedColorPreset(context: Context): Boolean {
        val colorManager =
            ColorCustomizationManager.getInstance(context, OverlayManagerCompat(context))
        return COLOR_SOURCE_PRESET == colorManager.currentColorSource
    }

    companion object {
        @JvmStatic
        private val KEY_QUICK_AFFORDANCE_SNAPSHOT_RESTORER =