-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHookState.java
More file actions
337 lines (291 loc) · 11.6 KB
/
HookState.java
File metadata and controls
337 lines (291 loc) · 11.6 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package fuck.iosstackingscreenshots.droidvendorssuck;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.net.Uri;
import android.util.Log;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import android.view.View;
import android.view.WindowManager;
final class HookState {
private static final String TAG = "iOSStackingShots";
private static final long REENTRY_GRACE_MS = 1500L;
private static final int MAX_STACK_BITMAPS = 3;
private static final int MAX_STACK_EDGE_PX = 640;
private static final Paint STACK_BITMAP_PAINT =
new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
private static volatile WeakReference<Object> screenshotWindowRef = new WeakReference<>(null);
private static volatile WeakReference<View> screenshotShelfViewRef = new WeakReference<>(null);
private static volatile WeakReference<View> continuityOverlayViewRef = new WeakReference<>(null);
private static volatile WeakReference<WindowManager> continuityOverlayWindowManagerRef =
new WeakReference<>(null);
private static volatile Bitmap lastPreviewBitmap;
private static volatile Uri lastSavedScreenshotUri;
private static final ArrayList<Bitmap> previewStack = new ArrayList<>();
private static final ArrayList<Uri> activeSavedScreenshotUris = new ArrayList<>();
private static final IdentityHashMap<Object, Integer> exportBatchIds = new IdentityHashMap<>();
private static volatile long reentryArmedAtMs;
private static volatile boolean reentryPreviewBound;
private static volatile int currentBatchId = 1;
private static volatile int currentBatchCaptureCount;
private static volatile int pendingDeletionBatchId = -1;
private static volatile long lastBatchActivityAtMs;
private static volatile long lastMarkupEditorLaunchAtMs;
private HookState() {
}
static void setScreenshotWindow(Object screenshotWindow) {
screenshotWindowRef = new WeakReference<>(screenshotWindow);
}
static void clearScreenshotWindow(Object screenshotWindow) {
Object current = screenshotWindowRef.get();
if (current == screenshotWindow) {
screenshotWindowRef = new WeakReference<>(null);
}
}
static Object getScreenshotWindow() {
return screenshotWindowRef.get();
}
static void setScreenshotShelfView(View view) {
screenshotShelfViewRef = new WeakReference<>(view);
}
static View getScreenshotShelfView() {
return screenshotShelfViewRef.get();
}
static void setContinuityOverlay(View view, WindowManager windowManager) {
continuityOverlayViewRef = new WeakReference<>(view);
continuityOverlayWindowManagerRef = new WeakReference<>(windowManager);
}
static View getContinuityOverlayView() {
return continuityOverlayViewRef.get();
}
static WindowManager getContinuityOverlayWindowManager() {
return continuityOverlayWindowManagerRef.get();
}
static void clearContinuityOverlay() {
continuityOverlayViewRef = new WeakReference<>(null);
continuityOverlayWindowManagerRef = new WeakReference<>(null);
}
static void setLastPreviewBitmap(Bitmap bitmap) {
lastPreviewBitmap = bitmap;
}
static Bitmap getLastPreviewBitmap() {
return lastPreviewBitmap;
}
static void setLastSavedScreenshotUri(Uri uri) {
lastSavedScreenshotUri = uri;
}
static Uri getLastSavedScreenshotUri() {
return lastSavedScreenshotUri;
}
static synchronized List<Uri> getActiveSavedScreenshotUris() {
return new ArrayList<>(activeSavedScreenshotUris);
}
static synchronized int getActiveSavedScreenshotCount() {
return activeSavedScreenshotUris.size();
}
static synchronized void beginFreshBatch() {
currentBatchId++;
currentBatchCaptureCount = 0;
pendingDeletionBatchId = -1;
activeSavedScreenshotUris.clear();
exportBatchIds.clear();
lastSavedScreenshotUri = null;
noteBatchActivity();
}
static synchronized void noteBatchCaptureRequested() {
currentBatchCaptureCount++;
noteBatchActivity();
}
static synchronized int getCurrentBatchCaptureCount() {
return currentBatchCaptureCount;
}
static synchronized int getCurrentBatchId() {
return currentBatchId;
}
static synchronized int getPendingExportCountForCurrentBatch() {
int pending = 0;
for (Integer batchId : exportBatchIds.values()) {
if (batchId != null && batchId == currentBatchId) {
pending++;
}
}
return pending;
}
static synchronized void registerExportFuture(Object futureDelegate, int batchId) {
if (futureDelegate == null) {
return;
}
exportBatchIds.put(futureDelegate, batchId);
noteBatchActivity();
}
static synchronized boolean recordSavedScreenshotUri(Object futureDelegate, Uri uri) {
if (uri == null) {
return false;
}
Integer batchId = futureDelegate == null ? null : exportBatchIds.remove(futureDelegate);
int resolvedBatchId = batchId != null ? batchId : currentBatchId;
lastSavedScreenshotUri = uri;
if (resolvedBatchId == pendingDeletionBatchId) {
return true;
}
if (resolvedBatchId != currentBatchId) {
return false;
}
if (!activeSavedScreenshotUris.contains(uri)) {
activeSavedScreenshotUris.add(uri);
}
noteBatchActivity();
return false;
}
static void noteBatchActivity() {
lastBatchActivityAtMs = android.os.SystemClock.uptimeMillis();
}
static boolean hasBatchSettled(long quietPeriodMs) {
long lastActivityAt = lastBatchActivityAtMs;
return lastActivityAt == 0L
|| android.os.SystemClock.uptimeMillis() - lastActivityAt >= quietPeriodMs;
}
static void markMarkupEditorLaunched() {
lastMarkupEditorLaunchAtMs = android.os.SystemClock.uptimeMillis();
}
static boolean wasMarkupEditorLaunchedRecently(long recentWindowMs) {
long launchedAt = lastMarkupEditorLaunchAtMs;
return launchedAt != 0L
&& android.os.SystemClock.uptimeMillis() - launchedAt <= recentWindowMs;
}
static synchronized List<Uri> markSavedBatchForDeletion() {
pendingDeletionBatchId = currentBatchId;
ArrayList<Uri> uris = new ArrayList<>(activeSavedScreenshotUris);
activeSavedScreenshotUris.clear();
return uris;
}
static synchronized void removeSavedScreenshotUri(Uri uri) {
activeSavedScreenshotUris.remove(uri);
if (uri != null && uri.equals(lastSavedScreenshotUri)) {
lastSavedScreenshotUri = null;
}
}
static synchronized void clearSavedScreenshotTracking() {
pendingDeletionBatchId = -1;
activeSavedScreenshotUris.clear();
exportBatchIds.clear();
lastSavedScreenshotUri = null;
}
static synchronized void pushStackBitmap(Bitmap bitmap) {
if (bitmap == null) {
Log.i(TAG, "pushStackBitmap: source bitmap was null");
return;
}
Log.i(TAG, "pushStackBitmap: source=" + describeBitmap(bitmap));
Bitmap stackBitmap = createStackBitmap(bitmap);
if (stackBitmap == null) {
Log.i(TAG, "pushStackBitmap: failed to create stack bitmap");
return;
}
Log.i(TAG, "pushStackBitmap: stored=" + describeBitmap(stackBitmap));
previewStack.add(0, stackBitmap);
while (previewStack.size() > MAX_STACK_BITMAPS) {
recycleBitmap(previewStack.remove(previewStack.size() - 1));
}
}
static synchronized List<Bitmap> getStackBitmaps() {
return new ArrayList<>(previewStack);
}
static synchronized void clearPreviewStack() {
for (Bitmap bitmap : previewStack) {
recycleBitmap(bitmap);
}
previewStack.clear();
lastPreviewBitmap = null;
clearSavedScreenshotTracking();
}
static void armReentryGrace() {
reentryArmedAtMs = android.os.SystemClock.uptimeMillis();
reentryPreviewBound = false;
}
static void clearReentryGrace() {
reentryArmedAtMs = 0L;
reentryPreviewBound = false;
}
static void markReentryPreviewBound() {
if (isReentryActive()) {
reentryPreviewBound = true;
}
}
static boolean hasReentryPreviewBound() {
return reentryPreviewBound;
}
static Object getContinuityOverlaySurface() {
View overlayView = continuityOverlayViewRef.get();
if (overlayView == null) {
return null;
}
Object viewRootImpl = ReflectionHelpers.callMethodIfExists(overlayView, "getViewRootImpl");
if (viewRootImpl == null) {
return null;
}
Object directSurface = ReflectionHelpers.callMethodIfExists(viewRootImpl, "getSurfaceControl");
if (directSurface != null) {
return directSurface;
}
return ReflectionHelpers.getObjectFieldIfExists(viewRootImpl, "mSurfaceControl");
}
static boolean isReentryGraceActive() {
return isReentryActive();
}
private static boolean isReentryActive() {
long armedAt = reentryArmedAtMs;
return armedAt != 0L && android.os.SystemClock.uptimeMillis() - armedAt <= REENTRY_GRACE_MS;
}
private static Bitmap createStackBitmap(Bitmap bitmap) {
if (bitmap == null || bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) {
return null;
}
int maxEdge = Math.max(bitmap.getWidth(), bitmap.getHeight());
float scale = maxEdge > MAX_STACK_EDGE_PX ? (float) MAX_STACK_EDGE_PX / maxEdge : 1.0f;
int width = Math.max(1, Math.round(bitmap.getWidth() * scale));
int height = Math.max(1, Math.round(bitmap.getHeight() * scale));
try {
Bitmap safeBitmap = bitmap;
if (bitmap.getConfig() == Bitmap.Config.HARDWARE) {
safeBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, false);
if (safeBitmap == null) {
Log.i(TAG, "createStackBitmap: hardware copy returned null");
return null;
}
}
if (safeBitmap.getWidth() == width && safeBitmap.getHeight() == height
&& safeBitmap.getConfig() == Bitmap.Config.ARGB_8888 && !safeBitmap.isMutable()) {
return safeBitmap;
}
Bitmap copy = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(copy);
canvas.drawBitmap(safeBitmap, null, new android.graphics.Rect(0, 0, width, height), STACK_BITMAP_PAINT);
return copy;
} catch (Throwable t) {
Log.i(TAG, "createStackBitmap: failed for " + describeBitmap(bitmap) + " error=" + t);
return null;
}
}
private static String describeBitmap(Bitmap bitmap) {
if (bitmap == null) {
return "null";
}
return bitmap.getWidth() + "x" + bitmap.getHeight()
+ " config=" + bitmap.getConfig()
+ " mutable=" + bitmap.isMutable()
+ " recycled=" + bitmap.isRecycled();
}
private static void recycleBitmap(Bitmap bitmap) {
if (bitmap == null || bitmap.isRecycled()) {
return;
}
try {
bitmap.recycle();
} catch (Throwable ignored) {
}
}
}