1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.customization.model.color 17 18 import android.content.Context 19 import android.content.pm.PackageManager 20 import android.content.res.Resources 21 import android.os.SystemProperties 22 import android.util.Log 23 import androidx.annotation.ColorInt 24 25 /** Utility to wrap Monet's color extraction */ 26 object ColorUtils { 27 private const val TAG = "ColorUtils" 28 private const val MONET_FLAG = "flag_monet" 29 private var sSysuiRes: Resources? = null 30 private var sFlagId = 0 31 32 /** Returns true if color extraction is enabled in systemui. */ 33 @JvmStatic isMonetEnablednull34 fun isMonetEnabled(context: Context): Boolean { 35 var monetEnabled = SystemProperties.getBoolean("persist.systemui.flag_monet", false) 36 if (!monetEnabled) { 37 if (sSysuiRes == null) { 38 try { 39 val pm = context.packageManager 40 val sysUIInfo = 41 pm.getApplicationInfo( 42 "com.android.systemui", 43 PackageManager.GET_META_DATA or PackageManager.MATCH_SYSTEM_ONLY 44 ) 45 if (sysUIInfo != null) { 46 sSysuiRes = pm.getResourcesForApplication(sysUIInfo) 47 } 48 } catch (e: PackageManager.NameNotFoundException) { 49 Log.w(TAG, "Couldn't read color flag, skipping section", e) 50 } 51 } 52 if (sFlagId == 0) { 53 sFlagId = 54 if (sSysuiRes == null) 0 55 else sSysuiRes!!.getIdentifier(MONET_FLAG, "bool", "com.android.systemui") 56 } 57 if (sFlagId > 0) { 58 monetEnabled = sSysuiRes!!.getBoolean(sFlagId) 59 } 60 } 61 return monetEnabled 62 } 63 64 @JvmStatic toColorStringnull65 fun toColorString(@ColorInt color: Int): String { 66 return String.format("%06X", 0xFFFFFF and color) 67 } 68 } 69