1 /* 2 * Copyright (C) 2024 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 17 package com.android.launcher3 18 19 import android.content.Context 20 import android.database.ContentObserver 21 import android.graphics.Paint 22 import android.net.Uri 23 import android.provider.Settings 24 import com.android.launcher3.util.Executors.ORDERED_BG_EXECUTOR 25 26 class PillColorProvider private constructor(c: Context) { 27 private val context = c.applicationContext 28 <lambda>null29 private val matchaUri by lazy { Settings.Secure.getUriFor(MATCHA_SETTING) } 30 var appTitlePillPaint = Paint() 31 private set 32 33 var appTitleTextPaint = Paint() 34 private set 35 36 private var isMatchaEnabledInternal = 0 37 38 var isMatchaEnabled = isMatchaEnabledInternal != 0 39 40 private val pillColorObserver = 41 object : ContentObserver(ORDERED_BG_EXECUTOR.handler) { onChangenull42 override fun onChange(selfChange: Boolean, uri: Uri?) { 43 if (uri == matchaUri) { 44 isMatchaEnabledInternal = 45 Settings.Secure.getInt(context.contentResolver, MATCHA_SETTING, 0) 46 isMatchaEnabled = isMatchaEnabledInternal != 0 47 } 48 } 49 } 50 registerObservernull51 fun registerObserver() { 52 context.contentResolver.registerContentObserver(matchaUri, false, pillColorObserver) 53 setup() 54 } 55 unregisterObservernull56 fun unregisterObserver() { 57 context.contentResolver.unregisterContentObserver(pillColorObserver) 58 } 59 setupnull60 fun setup() { 61 appTitlePillPaint.color = context.getColor(R.color.materialColorSurfaceContainer) 62 appTitleTextPaint.color = context.getColor(R.color.materialColorOnSurface) 63 isMatchaEnabledInternal = Settings.Secure.getInt(context.contentResolver, MATCHA_SETTING, 0) 64 isMatchaEnabled = isMatchaEnabledInternal != 0 65 } 66 67 companion object { 68 private var INSTANCE: PillColorProvider? = null 69 private const val MATCHA_SETTING = "matcha_enable" 70 71 // TODO: Replace with a Dagger injection that is a singleton. 72 @JvmStatic getInstancenull73 fun getInstance(context: Context): PillColorProvider { 74 if (INSTANCE == null) { 75 INSTANCE = PillColorProvider(context) 76 } 77 return INSTANCE!! 78 } 79 } 80 } 81