• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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 
17 package com.android.systemui.statusbar.phone
18 
19 import android.annotation.ColorInt
20 import android.app.WallpaperManager
21 import android.graphics.Color
22 import android.os.Handler
23 import android.os.RemoteException
24 import android.view.IWindowManager
25 import com.android.systemui.Dumpable
26 import com.android.systemui.dagger.qualifiers.Background
27 import com.android.systemui.dagger.qualifiers.Main
28 import com.android.systemui.dump.DumpManager
29 import com.android.systemui.statusbar.phone.dagger.CentralSurfacesComponent
30 import com.android.systemui.statusbar.phone.dagger.CentralSurfacesComponent.CentralSurfacesScope
31 import java.io.PrintWriter
32 import java.util.concurrent.Executor
33 import javax.inject.Inject
34 
35 /** Responsible for providing information about the background of letterboxed apps. */
36 @CentralSurfacesScope
37 class LetterboxBackgroundProvider
38 @Inject
39 constructor(
40     private val windowManager: IWindowManager,
41     @Background private val backgroundExecutor: Executor,
42     private val dumpManager: DumpManager,
43     private val wallpaperManager: WallpaperManager,
44     @Main private val mainHandler: Handler,
45 ) : CentralSurfacesComponent.Startable, Dumpable {
46 
47     @ColorInt
48     var letterboxBackgroundColor: Int = Color.BLACK
49         private set
50 
51     var isLetterboxBackgroundMultiColored: Boolean = false
52         private set
53 
54     private val wallpaperColorsListener =
55         WallpaperManager.OnColorsChangedListener { _, _ ->
56             fetchBackgroundColorInfo()
57         }
58 
59     override fun start() {
60         dumpManager.registerDumpable(javaClass.simpleName, this)
61         fetchBackgroundColorInfo()
62         wallpaperManager.addOnColorsChangedListener(wallpaperColorsListener, mainHandler)
63     }
64 
65     private fun fetchBackgroundColorInfo() {
66         // Using a background executor, as binder calls to IWindowManager are blocking
67         backgroundExecutor.execute {
68             try {
69                 isLetterboxBackgroundMultiColored = windowManager.isLetterboxBackgroundMultiColored
70                 letterboxBackgroundColor = windowManager.letterboxBackgroundColorInArgb
71             } catch (e: RemoteException) {
72                 e.rethrowFromSystemServer()
73             }
74         }
75     }
76 
77     override fun stop() {
78         dumpManager.unregisterDumpable(javaClass.simpleName)
79         wallpaperManager.removeOnColorsChangedListener(wallpaperColorsListener)
80     }
81 
82     override fun dump(pw: PrintWriter, args: Array<out String>) {
83         pw.println(
84             """
85            letterboxBackgroundColor: ${Color.valueOf(letterboxBackgroundColor)}
86            isLetterboxBackgroundMultiColored: $isLetterboxBackgroundMultiColored
87        """.trimIndent())
88     }
89 }
90