-
-
Notifications
You must be signed in to change notification settings - Fork 474
Expand file tree
/
Copy pathWindows.kt
More file actions
209 lines (192 loc) · 7.04 KB
/
Copy pathWindows.kt
File metadata and controls
209 lines (192 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* Adapted from https://github.com/square/curtains/tree/v1.2.5
*
* Copyright 2021 Square Inc.
*
* 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 io.sentry.android.replay
import android.annotation.SuppressLint
import android.os.Build.VERSION.SDK_INT
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.View
import android.view.Window
import io.sentry.util.AutoClosableReentrantLock
import java.io.Closeable
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.LazyThreadSafetyMode.NONE
/**
* If this view is part of the view hierarchy from a [android.app.Activity], [android.app.Dialog] or
* [android.service.dreams.DreamService], then this returns the [android.view.Window] instance
* associated to it. Otherwise, this returns null.
*
* Note: this property is called [phoneWindow] because the only implementation of [Window] is the
* internal class android.view.PhoneWindow.
*/
internal val View.phoneWindow: Window?
get() {
return WindowSpy.pullWindow(rootView)
}
@SuppressLint("PrivateApi")
internal object WindowSpy {
/**
* DecorView moved out of PhoneWindow into its own class:
* https://android.googlesource.com/platform/frameworks/base/+/8804af2b63b0584034f7ec7d4dc701d06e6a8754
* In API 24:
* https://android.googlesource.com/platform/frameworks/base/+/android-7.0.0_r1/core/java/com/android/internal/policy/DecorView.java
*/
private val decorViewClass by
lazy(NONE) {
try {
Class.forName("com.android.internal.policy.DecorView")
} catch (ignored: Throwable) {
Log.d("WindowSpy", "Unexpected exception loading DecorView on API $SDK_INT", ignored)
null
}
}
/**
* See [decorViewClass] for the AOSP history of the DecorView class. Between the latest API 23
* release and the first API 24 release, DecorView first became a static class:
* https://android.googlesource.com/platform/frameworks/base/+/0daf2102a20d224edeb4ee45dd4ee91889ef3e0c
* Then it was extracted into a separate class.
*
* Hence we use "mWindow" on API 24+.
*/
private val windowField by
lazy(NONE) {
decorViewClass?.let { decorViewClass ->
try {
decorViewClass.getDeclaredField("mWindow").apply { isAccessible = true }
} catch (ignored: NoSuchFieldException) {
Log.d(
"WindowSpy",
"Unexpected exception retrieving $decorViewClass#mWindow on API $SDK_INT",
ignored,
)
null
}
}
}
fun pullWindow(maybeDecorView: View): Window? =
decorViewClass?.let { decorViewClass ->
if (decorViewClass.isInstance(maybeDecorView)) {
windowField?.let { windowField -> windowField[maybeDecorView] as Window }
} else {
null
}
}
}
/**
* Listener added to [Curtains.onRootViewsChangedListeners]. If you only care about either attached
* or detached, consider implementing [OnRootViewAddedListener] or [OnRootViewRemovedListener]
* instead.
*/
internal fun interface OnRootViewsChangedListener {
/**
* Called when [android.view.WindowManager.addView] and [android.view.WindowManager.removeView]
* are called.
*/
fun onRootViewsChanged(view: View, added: Boolean)
}
/** A utility that holds the list of root views that WindowManager updates. */
internal class RootViewsSpy private constructor() : Closeable {
private val isClosed = AtomicBoolean(false)
private val viewListLock = AutoClosableReentrantLock()
val listeners: CopyOnWriteArrayList<OnRootViewsChangedListener> =
object : CopyOnWriteArrayList<OnRootViewsChangedListener>() {
override fun add(element: OnRootViewsChangedListener?): Boolean {
viewListLock.acquire().use {
// notify listener about existing root views immediately
delegatingViewList.forEach { element?.onRootViewsChanged(it, true) }
}
return super.add(element)
}
}
private val delegatingViewList: ArrayList<View> =
object : ArrayList<View>() {
@Suppress("NewApi")
override fun addAll(elements: Collection<View>): Boolean {
listeners.forEach { listener ->
elements.forEach { element -> listener.onRootViewsChanged(element, true) }
}
return super.addAll(elements)
}
@Suppress("NewApi")
override fun add(element: View): Boolean {
listeners.forEach { it.onRootViewsChanged(element, true) }
return super.add(element)
}
@Suppress("NewApi")
override fun removeAt(index: Int): View {
val removedView = super.removeAt(index)
listeners.forEach { it.onRootViewsChanged(removedView, false) }
return removedView
}
}
override fun close() {
isClosed.set(true)
listeners.clear()
}
companion object {
fun install(): RootViewsSpy {
return RootViewsSpy().apply {
// had to do this on the main thread queue, otherwise if this is
// called from ContentProvider, it might be too early and the listener won't be installed
Handler(Looper.getMainLooper()).postAtFrontOfQueue {
if (isClosed.get()) {
return@postAtFrontOfQueue
}
WindowManagerSpy.swapWindowManagerGlobalMViews { mViews ->
viewListLock.acquire().use { delegatingViewList.apply { addAll(mViews) } }
}
}
}
}
}
}
internal object WindowManagerSpy {
private val windowManagerClass by
lazy(NONE) {
val className = "android.view.WindowManagerGlobal"
try {
Class.forName(className)
} catch (ignored: Throwable) {
Log.w("WindowManagerSpy", ignored)
null
}
}
private val windowManagerInstance by
lazy(NONE) { windowManagerClass?.getMethod("getInstance")?.invoke(null) }
private val mViewsField by
lazy(NONE) {
windowManagerClass?.let { windowManagerClass ->
windowManagerClass.getDeclaredField("mViews").apply { isAccessible = true }
}
}
// You can discourage me all you want I'll still do it.
@SuppressLint("PrivateApi", "ObsoleteSdkInt", "DiscouragedPrivateApi")
fun swapWindowManagerGlobalMViews(swap: (ArrayList<View>) -> ArrayList<View>) {
try {
windowManagerInstance?.let { windowManagerInstance ->
mViewsField?.let { mViewsField ->
@Suppress("UNCHECKED_CAST")
val mViews = mViewsField[windowManagerInstance] as ArrayList<View>
mViewsField[windowManagerInstance] = swap(mViews)
}
}
} catch (ignored: Throwable) {
Log.w("WindowManagerSpy", ignored)
}
}
}