• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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.quickstep
18 
19 import android.content.Context
20 import android.hardware.display.DisplayManager
21 import android.util.Log
22 import android.util.SparseArray
23 import android.view.Display
24 import androidx.core.util.valueIterator
25 import com.android.quickstep.DisplayModel.DisplayResource
26 import com.android.quickstep.SystemDecorationChangeObserver.Companion.INSTANCE
27 import com.android.quickstep.SystemDecorationChangeObserver.DisplayDecorationListener
28 import java.io.PrintWriter
29 
30 /** data model for managing resources with lifecycles that match that of the connected display */
31 abstract class DisplayModel<RESOURCE_TYPE : DisplayResource>(val context: Context) :
32     DisplayDecorationListener {
33 
34     companion object {
35         private const val TAG = "DisplayModel"
36         private const val DEBUG = false
37     }
38 
39     private val displayManager = context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
40     private var systemDecorationChangeObserver: SystemDecorationChangeObserver? = null
41     protected val displayResourceArray = SparseArray<RESOURCE_TYPE>()
42 
43     override fun onDisplayAddSystemDecorations(displayId: Int) {
44         if (DEBUG) Log.d(TAG, "onDisplayAdded: displayId=$displayId")
45         storeDisplayResource(displayId)
46     }
47 
48     override fun onDisplayRemoved(displayId: Int) {
49         if (DEBUG) Log.d(TAG, "onDisplayRemoved: displayId=$displayId")
50         deleteDisplayResource(displayId)
51     }
52 
53     override fun onDisplayRemoveSystemDecorations(displayId: Int) {
54         if (DEBUG) Log.d(TAG, "onDisplayRemoveSystemDecorations: displayId=$displayId")
55         deleteDisplayResource(displayId)
56     }
57 
58     protected abstract fun createDisplayResource(display: Display): RESOURCE_TYPE
59 
60     protected fun initializeDisplays() {
61         systemDecorationChangeObserver = INSTANCE[context]
62         systemDecorationChangeObserver?.registerDisplayDecorationListener(this)
63         displayManager.displays
64             .filter { getDisplayResource(it.displayId) == null }
65             .forEach { storeDisplayResource(it.displayId) }
66     }
67 
68     fun destroy() {
69         systemDecorationChangeObserver?.unregisterDisplayDecorationListener(this)
70         systemDecorationChangeObserver = null
71         displayResourceArray.valueIterator().forEach { displayResource ->
72             displayResource.cleanup()
73         }
74         displayResourceArray.clear()
75     }
76 
77     fun getDisplayResource(displayId: Int): RESOURCE_TYPE? {
78         if (DEBUG) Log.d(TAG, Log.getStackTraceString(Throwable("get: displayId=$displayId")))
79         return displayResourceArray[displayId]
80     }
81 
82     fun deleteDisplayResource(displayId: Int) {
83         if (DEBUG) Log.d(TAG, "delete: displayId=$displayId")
84         getDisplayResource(displayId)?.let {
85             it.cleanup()
86             displayResourceArray.remove(displayId)
87         }
88     }
89 
90     fun storeDisplayResource(displayId: Int) {
91         if (DEBUG) Log.d(TAG, "store: displayId=$displayId")
92         getDisplayResource(displayId)?.let {
93             return
94         }
95         val display = displayManager.getDisplay(displayId)
96         if (display == null) {
97             if (DEBUG)
98                 Log.w(
99                     TAG,
100                     "storeDisplayResource: could not create display for displayId=$displayId",
101                     Exception(),
102                 )
103             return
104         }
105         displayResourceArray[displayId] = createDisplayResource(display)
106     }
107 
108     fun dump(prefix: String, writer: PrintWriter) {
109         writer.println("${prefix}${this::class.simpleName}: display resources=[")
110 
111         displayResourceArray.valueIterator().forEach { displayResource ->
112             displayResource.dump("${prefix}\t", writer)
113         }
114         writer.println("${prefix}]")
115     }
116 
117     abstract class DisplayResource {
118         abstract fun cleanup()
119 
120         abstract fun dump(prefix: String, writer: PrintWriter)
121     }
122 }
123