-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenshotHooks.java
More file actions
1509 lines (1408 loc) · 65.5 KB
/
ScreenshotHooks.java
File metadata and controls
1509 lines (1408 loc) · 65.5 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package fuck.iosstackingscreenshots.droidvendorssuck;
import android.animation.AnimatorSet;
import android.content.ClipData;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.net.Uri;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.PixelCopy;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
import java.lang.ref.WeakReference;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
final class ScreenshotHooks {
private static final String TAG = "iOSStackingShots";
private static final long CONTINUITY_OVERLAY_MS = 1200L;
private static final long CONTINUITY_HANDOFF_MS = 96L;
private static final int SCREENSHOT_TIMEOUT_MS = 15000;
private static final Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());
private static final Paint CARD_BITMAP_PAINT =
new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
private static final Paint CARD_CONTENT_BACKGROUND_PAINT = new Paint(Paint.ANTI_ALIAS_FLAG);
private static final String STACK_CARD_TAG_PREFIX = "IOSStackingShotsCard";
private static final int IOS_FRAME_COLOR = Color.parseColor("#FFFFFF");
private static final int IOS_FRAME_STROKE_COLOR = Color.parseColor("#D6D9DE");
private static final int IOS_CARD_BACKGROUND_COLOR = Color.BLACK;
private static final float STACK_CARD_X_OFFSET_DP = 2.0f;
private static final float STACK_CARD_Y_OFFSET_DP = 1.0f;
private static final float CARD_MAX_WIDTH_DP = 88.0f;
private static final float CARD_MAX_HEIGHT_DP = 160.0f;
private static final float CARD_FRAME_INSET_DP = 2.0f;
private static final long STACK_UI_SETTLE_DELAY_MS = 16L;
private static final long PREVIEW_CHOOSER_HOLD_MS = 500L;
private static final long EDITOR_REFRESH_WINDOW_MS = 8000L;
private static final Executor DIRECT_EXECUTOR = new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
};
private static final ArrayList<Drawable> overlayStackCards = new ArrayList<>();
private static volatile boolean installed;
private static volatile WeakReference<View> activePreviewTouchViewRef = new WeakReference<>(null);
private static volatile long activePreviewTouchDownMs;
private static volatile boolean activePreviewLongPressTriggered;
private static Runnable activePreviewLongPressRunnable;
private static volatile WeakReference<View> lastPreviewClickTargetRef = new WeakReference<>(null);
private static volatile long lastPreviewClickHeldMs;
private static volatile long lastPreviewClickRecordedAtMs;
private static Runnable continuityOverlayRemoval;
static {
CARD_CONTENT_BACKGROUND_PAINT.setColor(IOS_CARD_BACKGROUND_COLOR);
}
private ScreenshotHooks() {
}
static synchronized void install(ClassLoader classLoader) {
if (installed) {
return;
}
installed = true;
log("installing hooks in com.android.systemui:screenshot");
hookScreenshotShelfViewProxy(classLoader);
hookScreenshotController(classLoader);
hookScreenshotCallbacks(classLoader);
hookScreenshotShelfBinder(classLoader);
hookPreviewTouchRouting(classLoader);
hookPreviewActionModel(classLoader);
hookScreenshotWindow(classLoader);
hookImageExporter(classLoader);
hookImageCapture(classLoader);
}
private static void hookScreenshotShelfViewProxy(ClassLoader classLoader) {
Class<?> proxyClass = XposedHelpers.findClassIfExists(
"com.android.systemui.screenshot.ScreenshotShelfViewProxy", classLoader);
if (proxyClass == null) {
log("ScreenshotShelfViewProxy not found");
return;
}
XposedBridge.hookAllConstructors(proxyClass, new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) {
View shelfView = (View) ReflectionHelpers.getObjectFieldIfExists(param.thisObject, "view");
if (shelfView == null) {
log("ScreenshotShelfViewProxy constructed but view field was null");
return;
}
HookState.setScreenshotShelfView(shelfView);
log("ScreenshotShelfViewProxy constructed; tinting preview border");
tintPreviewBorder(shelfView);
hideShelfChrome(shelfView);
installPreviewTapToast(shelfView);
}
});
XposedHelpers.findAndHookMethod(proxyClass, "createScreenshotDropInAnimation", Rect.class, boolean.class,
new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) {
param.args[1] = Boolean.FALSE;
if (!HookState.isReentryGraceActive()) {
return;
}
forcePreviewVisible(param.thisObject);
log("Skipping drop-in animation during screenshot reentry");
param.setResult(new AnimatorSet());
}
});
XposedBridge.hookAllMethods(proxyClass, "setScreenshot", new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) {
Object screenshotData = param.args[0];
Bitmap previousBitmap = HookState.getLastPreviewBitmap();
if (previousBitmap != null && HookState.isReentryGraceActive()) {
HookState.pushStackBitmap(previousBitmap);
}
Object bitmap = ReflectionHelpers.getObjectFieldIfExists(screenshotData, "bitmap");
if (bitmap instanceof Bitmap) {
HookState.setLastPreviewBitmap((Bitmap) bitmap);
log("Cached screenshot preview bitmap");
}
}
@Override
protected void afterHookedMethod(MethodHookParam param) {
Object screenshotData = param.args[0];
Object bitmap = ReflectionHelpers.getObjectFieldIfExists(screenshotData, "bitmap");
HookState.markReentryPreviewBound();
View shelfView = HookState.getScreenshotShelfView();
if (shelfView != null) {
if (bitmap instanceof Bitmap) {
forceCurrentPreviewBitmap(shelfView, (Bitmap) bitmap);
}
hideShelfChrome(shelfView);
scheduleStackUiUpdate(shelfView);
}
removeContinuityOverlay(false);
}
});
XposedBridge.hookAllMethods(proxyClass, "requestDismissal", new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) {
String eventName = String.valueOf(param.args[0]);
if ("SCREENSHOT_EXPLICIT_DISMISSAL".equals(eventName)) {
Float velocity = param.args.length > 1 && param.args[1] instanceof Float
? (Float) param.args[1] : null;
if (velocity != null && velocity.floatValue() < 0.0f) {
log("Deleting active screenshot batch after left-swipe dismissal");
deleteSavedScreenshotBatch(HookState.markSavedBatchForDeletion());
}
}
if ("SCREENSHOT_DISMISSED_OTHER".equals(eventName)) {
log("Ignoring stock dismissal event " + eventName);
param.setResult(null);
}
}
});
XposedBridge.hookAllMethods(proxyClass, "reset", new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) {
if (HookState.isReentryGraceActive()) {
log("ScreenshotShelfViewProxy.reset() called during reentry grace");
}
}
});
}
private static void hookScreenshotController(ClassLoader classLoader) {
Class<?> controllerClass = XposedHelpers.findClassIfExists(
"com.android.systemui.screenshot.ScreenshotController", classLoader);
if (controllerClass == null) {
log("ScreenshotController not found");
return;
}
XposedBridge.hookAllConstructors(controllerClass, new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) {
Object screenshotWindow = ReflectionHelpers.getObjectFieldIfExists(param.thisObject, "window");
if (screenshotWindow != null) {
HookState.setScreenshotWindow(screenshotWindow);
log("cached ScreenshotWindow from ScreenshotController");
} else {
log("ScreenshotController constructed but window field was null");
}
Object timeoutHandler = ReflectionHelpers.getObjectFieldIfExists(param.thisObject, "screenshotHandler");
if (timeoutHandler != null) {
XposedHelpers.setIntField(timeoutHandler, "mDefaultTimeout", SCREENSHOT_TIMEOUT_MS);
log("Set stock screenshot timeout to " + SCREENSHOT_TIMEOUT_MS + "ms");
}
}
});
XposedBridge.hookAllMethods(controllerClass, "handleScreenshot", new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) {
HookState.noteBatchActivity();
View shelfView = HookState.getScreenshotShelfView();
if (shelfView != null && shelfView.isAttachedToWindow()) {
removeContinuityOverlay(false);
HookState.armReentryGrace();
log("Armed screenshot reentry without continuity overlay");
} else {
HookState.clearReentryGrace();
HookState.beginFreshBatch();
HookState.clearPreviewStack();
}
HookState.noteBatchCaptureRequested();
}
});
}
private static void hookScreenshotCallbacks(ClassLoader classLoader) {
Class<?> callbackClass = XposedHelpers.findClassIfExists(
"com.android.systemui.screenshot.ScreenshotController$reloadAssets$1", classLoader);
if (callbackClass == null) {
log("ScreenshotController callback class not found");
return;
}
XposedBridge.hookAllMethods(callbackClass, "onTouchOutside", new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) {
}
});
}
private static void hookScreenshotShelfBinder(ClassLoader classLoader) {
Class<?> binderClass = XposedHelpers.findClassIfExists(
"com.android.systemui.screenshot.ui.binder.ScreenshotShelfViewBinder", classLoader);
if (binderClass == null) {
log("ScreenshotShelfViewBinder not found");
return;
}
XposedBridge.hookAllMethods(binderClass, "access$updateActions", new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) {
Object shelfView = param.args.length > 3 ? param.args[3] : null;
if (shelfView instanceof View) {
hideShelfChrome((View) shelfView);
}
log("Suppressing stock screenshot actions row");
param.setResult(null);
}
});
}
private static void hookScreenshotWindow(ClassLoader classLoader) {
Class<?> windowClass = XposedHelpers.findClassIfExists(
"com.android.systemui.screenshot.ScreenshotWindow", classLoader);
if (windowClass == null) {
log("ScreenshotWindow not found");
return;
}
XposedBridge.hookAllMethods(windowClass, "removeWindow", new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) {
if (HookState.getContinuityOverlayView() != null && HookState.isReentryGraceActive()) {
log("Allowing ScreenshotWindow.removeWindow while continuity overlay is active");
return;
}
HookState.clearReentryGrace();
}
@Override
protected void afterHookedMethod(MethodHookParam param) {
log("ScreenshotWindow.removeWindow called; preserving cached window for reuse");
if (!HookState.isReentryGraceActive()) {
boolean preserveBatchForEditor = HookState.wasMarkupEditorLaunchedRecently(
EDITOR_REFRESH_WINDOW_MS);
if (!preserveBatchForEditor) {
HookState.clearPreviewStack();
}
View shelfView = HookState.getScreenshotShelfView();
if (shelfView != null) {
clearStackUi(shelfView);
}
}
}
});
}
private static void hookImageCapture(ClassLoader classLoader) {
Class<?> imageCaptureClass = XposedHelpers.findClassIfExists(
"com.android.systemui.screenshot.ImageCaptureImpl", classLoader);
if (imageCaptureClass == null) {
log("ImageCaptureImpl not found");
return;
}
XposedHelpers.findAndHookMethod(imageCaptureClass, "captureDisplay", int.class, Rect.class,
new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
log("ImageCaptureImpl.captureDisplay called");
Object screenshotSurface = resolveScreenshotSurface();
if (screenshotSurface == null) {
log("No screenshot surface available; falling back to original capture");
return;
}
Integer displayId = (Integer) param.args[0];
Rect crop = (Rect) param.args[1];
Object result = invokeCaptureWithExcludedLayers(param.thisObject, displayId, crop,
screenshotSurface);
if (result != null) {
param.setResult(result);
}
}
});
}
private static void hookImageExporter(ClassLoader classLoader) {
Class<?> imageExporterClass = XposedHelpers.findClassIfExists(
"com.android.systemui.screenshot.ImageExporter", classLoader);
if (imageExporterClass == null) {
log("ImageExporter not found");
return;
}
XposedBridge.hookAllMethods(imageExporterClass, "export", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) {
final Object safeFuture = param.getResult();
if (safeFuture == null) {
return;
}
final Object delegate = ReflectionHelpers.getObjectFieldIfExists(safeFuture, "delegate");
if (delegate == null) {
return;
}
final int batchId = HookState.getCurrentBatchId();
HookState.registerExportFuture(delegate, batchId);
try {
XposedHelpers.callMethod(delegate, "addListener", new Runnable() {
@Override
public void run() {
onImageExportCompleted(delegate);
}
}, DIRECT_EXECUTOR);
} catch (Throwable t) {
log("Failed to attach ImageExporter listener: " + t);
}
}
});
}
private static void onImageExportCompleted(Object futureDelegate) {
try {
Object result = XposedHelpers.callMethod(futureDelegate, "get");
Object uriObject = ReflectionHelpers.getObjectFieldIfExists(result, "uri");
if (!(uriObject instanceof Uri)) {
return;
}
Uri uri = (Uri) uriObject;
if (HookState.recordSavedScreenshotUri(futureDelegate, uri)) {
deleteSavedScreenshotUri(uri);
} else {
log("Tracked saved screenshot uri " + uri);
refreshMarkupEditorBatchIfVisible();
}
} catch (Throwable t) {
log("Failed to read ImageExporter result: " + t);
}
}
private static void refreshMarkupEditorBatchIfVisible() {
if (!HookState.wasMarkupEditorLaunchedRecently(EDITOR_REFRESH_WINDOW_MS)) {
return;
}
Context context = getMarkupEditorLaunchContext();
if (context == null) {
return;
}
Uri screenshotUri = HookState.getLastSavedScreenshotUri();
if (screenshotUri == null) {
return;
}
try {
dispatchMarkupEditorLaunch(context, screenshotUri);
HookState.markMarkupEditorLaunched();
log("Refreshed markup editor batch after late export");
} catch (Throwable t) {
log("Failed to refresh markup editor batch: " + t);
}
}
private static void tintPreviewBorder(View shelfView) {
ImageView preview = findImageView(shelfView, "screenshot_preview");
if (preview != null) {
int inset = dp(preview, CARD_FRAME_INSET_DP);
preview.setBackground(createCardFrameDrawable(preview));
preview.setPadding(inset, inset, inset, inset);
preview.setClipToOutline(false);
preview.setScaleType(ImageView.ScaleType.FIT_XY);
}
int borderId = shelfView.getResources()
.getIdentifier("screenshot_preview_border", "id", "com.android.systemui");
if (borderId != 0) {
View border = shelfView.findViewById(borderId);
if (border != null) {
border.setVisibility(View.GONE);
border.setAlpha(0.0f);
border.setBackground(null);
}
}
log("screenshot chrome styled with white frame and black card fill");
}
private static void installPreviewTapToast(View shelfView) {
final ImageView preview = findImageView(shelfView, "screenshot_preview");
if (preview == null) {
return;
}
preview.setLongClickable(false);
preview.setHapticFeedbackEnabled(false);
}
private static void showPreviewTapToast(View view) {
Toast.makeText(view.getContext(), "Tap ignored. Hold for actions.", Toast.LENGTH_SHORT).show();
log("Preview tap consumed; waiting for deliberate hold before stock actions");
}
private static void launchMarkupEditor(View view) {
launchMarkupEditorNow(view);
}
private static void launchMarkupEditorNow(View view) {
Uri screenshotUri = HookState.getLastSavedScreenshotUri();
if (screenshotUri == null) {
showPreviewTapToast(view);
return;
}
try {
Context context = view.getContext();
dispatchMarkupEditorLaunch(context, screenshotUri);
HookState.markMarkupEditorLaunched();
dismissScreenshotShelf();
log("Launched markup editor for " + screenshotUri);
} catch (Throwable t) {
log("Failed to launch markup editor: " + t);
Toast.makeText(view.getContext(), "Failed to open editor", Toast.LENGTH_SHORT).show();
}
}
private static void dispatchMarkupEditorLaunch(Context context, Uri screenshotUri) {
Intent intent = buildMarkupEditorIntent(screenshotUri);
grantMarkupEditorUriPermissions(context,
intent.getParcelableArrayListExtra(MarkupEditorActivity.EXTRA_SCREENSHOT_BATCH_URIS));
context.startActivity(intent);
}
private static Intent buildMarkupEditorIntent(Uri screenshotUri) {
ArrayList<Uri> editorBatch = buildEditorBatch(screenshotUri);
Intent intent = new Intent();
intent.setComponent(new ComponentName(
"fuck.iosstackingscreenshots.droidvendorssuck",
MarkupEditorLaunchActivity.class.getName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
| Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setData(screenshotUri);
intent.setClipData(buildEditorClipData(editorBatch));
intent.putExtra(MarkupEditorActivity.EXTRA_SCREENSHOT_URI, screenshotUri);
intent.putParcelableArrayListExtra(MarkupEditorActivity.EXTRA_SCREENSHOT_BATCH_URIS, editorBatch);
intent.putExtra(MarkupEditorActivity.EXTRA_SCREENSHOT_INDEX, 0);
return intent;
}
private static void grantMarkupEditorUriPermissions(Context context, ArrayList<Uri> batchUris) {
if (context == null || batchUris == null) {
return;
}
for (Uri uri : batchUris) {
if (uri == null) {
continue;
}
try {
context.grantUriPermission(
"fuck.iosstackingscreenshots.droidvendorssuck",
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} catch (Throwable t) {
log("Failed to grant markup editor read permission for " + uri + ": " + t);
}
}
}
private static ArrayList<Uri> buildEditorBatch(Uri selectedUri) {
ArrayList<Uri> batch = new ArrayList<>(HookState.getActiveSavedScreenshotUris());
if (batch.isEmpty()) {
batch.add(selectedUri);
return batch;
}
if (!batch.contains(selectedUri)) {
batch.add(selectedUri);
}
Collections.reverse(batch);
return batch;
}
private static ClipData buildEditorClipData(List<Uri> batchUris) {
if (batchUris == null || batchUris.isEmpty()) {
return null;
}
ClipData clipData = new ClipData(
"screenshot-batch",
new String[]{"image/*"},
new ClipData.Item(batchUris.get(0)));
for (int i = 1; i < batchUris.size(); i++) {
clipData.addItem(new ClipData.Item(batchUris.get(i)));
}
return clipData;
}
private static void dismissScreenshotShelf() {
Object screenshotWindow = HookState.getScreenshotWindow();
if (screenshotWindow == null) {
return;
}
try {
XposedHelpers.callMethod(screenshotWindow, "removeWindow");
log("Dismissed screenshot shelf after handing off to markup editor");
} catch (Throwable t) {
log("Failed to dismiss screenshot shelf after editor launch: " + t);
}
}
private static Context getMarkupEditorLaunchContext() {
View shelfView = HookState.getScreenshotShelfView();
if (shelfView != null) {
return shelfView.getContext();
}
Object screenshotWindow = HookState.getScreenshotWindow();
if (screenshotWindow == null) {
return null;
}
Object phoneWindow = ReflectionHelpers.getObjectFieldIfExists(screenshotWindow, "window");
if (phoneWindow == null) {
return null;
}
Object context = ReflectionHelpers.callMethodIfExists(phoneWindow, "getContext");
return context instanceof Context ? (Context) context : null;
}
private static void hookPreviewTouchRouting(ClassLoader classLoader) {
XposedHelpers.findAndHookMethod(View.class, "dispatchTouchEvent", android.view.MotionEvent.class,
new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) {
if (!(param.thisObject instanceof View) || !(param.args[0] instanceof android.view.MotionEvent)) {
return;
}
View targetView = (View) param.thisObject;
if (!isScreenshotPreviewClickView(targetView)) {
return;
}
android.view.MotionEvent event = (android.view.MotionEvent) param.args[0];
View activeView = activePreviewTouchViewRef.get();
if (event.getActionMasked() == android.view.MotionEvent.ACTION_DOWN
&& !isPointInsideView(targetView, event.getRawX(), event.getRawY())) {
clearActivePreviewTouch();
return;
}
int action = event.getActionMasked();
switch (action) {
case android.view.MotionEvent.ACTION_DOWN:
activePreviewTouchViewRef = new WeakReference<>(targetView);
activePreviewTouchDownMs = event.getEventTime();
activePreviewLongPressTriggered = false;
schedulePreviewLongPress(targetView);
param.setResult(Boolean.TRUE);
return;
case android.view.MotionEvent.ACTION_MOVE:
if (activeView == null || activeView != targetView) {
clearActivePreviewTouch();
}
param.setResult(Boolean.TRUE);
return;
case android.view.MotionEvent.ACTION_UP:
if (activeView == null) {
clearActivePreviewTouch();
param.setResult(Boolean.TRUE);
return;
}
long heldMs = Math.max(0L, event.getEventTime() - activePreviewTouchDownMs);
boolean longPressTriggered = activePreviewLongPressTriggered;
clearActivePreviewTouch();
if (!longPressTriggered) {
log("Preview tap intercepted at " + heldMs + "ms");
launchPreviewTapAction(activeView);
}
param.setResult(Boolean.TRUE);
return;
case android.view.MotionEvent.ACTION_CANCEL:
clearActivePreviewTouch();
param.setResult(Boolean.TRUE);
return;
default:
if (activeView != null) {
param.setResult(Boolean.TRUE);
}
return;
}
}
});
}
private static long consumeRecentPreviewHoldMs(View clickedView) {
View recordedView = lastPreviewClickTargetRef.get();
long recordedAtMs = lastPreviewClickRecordedAtMs;
lastPreviewClickTargetRef = new WeakReference<>(null);
lastPreviewClickRecordedAtMs = 0L;
long heldMs = lastPreviewClickHeldMs;
lastPreviewClickHeldMs = 0L;
if (recordedView != clickedView) {
return -1L;
}
if (recordedAtMs == 0L || android.os.SystemClock.uptimeMillis() - recordedAtMs > 1000L) {
return -1L;
}
return heldMs;
}
private static void hookPreviewActionModel(ClassLoader classLoader) {
final Class<?> previewActionClass = XposedHelpers.findClassIfExists(
"com.android.systemui.screenshot.ui.viewmodel.PreviewAction", classLoader);
if (previewActionClass == null) {
log("PreviewAction not found");
return;
}
XposedBridge.hookAllConstructors(previewActionClass, new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) {
final Object previewAction = param.thisObject;
final Object originalFunction = XposedHelpers.getObjectField(previewAction, "onClick");
if (originalFunction == null) {
return;
}
Class<?>[] interfaces = originalFunction.getClass().getInterfaces();
if (interfaces.length == 0) {
return;
}
Object wrappedFunction = Proxy.newProxyInstance(
originalFunction.getClass().getClassLoader(),
interfaces,
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!"invoke".equals(method.getName())) {
return method.invoke(originalFunction, args);
}
long heldMs = consumeRecentPreviewHoldMs(lastPreviewClickTargetRef.get());
if (heldMs >= PREVIEW_CHOOSER_HOLD_MS) {
log("PreviewAction allowed stock invoke for hold=" + heldMs + "ms");
return method.invoke(originalFunction, args);
}
View shelfView = HookState.getScreenshotShelfView();
if (shelfView != null) {
log("PreviewAction intercepted short tap hold=" + heldMs + "ms");
launchPreviewTapAction(shelfView);
return null;
}
return method.invoke(originalFunction, args);
}
});
XposedHelpers.setObjectField(previewAction, "onClick", wrappedFunction);
}
});
}
private static boolean isScreenshotPreviewClickView(View view) {
if (view.getId() == View.NO_ID) {
return false;
}
try {
if (!"com.android.systemui".equals(view.getResources().getResourcePackageName(view.getId()))) {
return false;
}
String entryName = view.getResources().getResourceEntryName(view.getId());
if (!"screenshot_preview".equals(entryName)
&& !"screenshot_scrolling_scrim".equals(entryName)) {
return false;
}
} catch (Throwable t) {
return false;
}
return true;
}
private static void clearActivePreviewTouch() {
if (activePreviewLongPressRunnable != null) {
MAIN_HANDLER.removeCallbacks(activePreviewLongPressRunnable);
activePreviewLongPressRunnable = null;
}
activePreviewTouchViewRef = new WeakReference<>(null);
activePreviewTouchDownMs = 0L;
activePreviewLongPressTriggered = false;
}
private static void schedulePreviewLongPress(final View targetView) {
if (activePreviewLongPressRunnable != null) {
MAIN_HANDLER.removeCallbacks(activePreviewLongPressRunnable);
}
activePreviewLongPressRunnable = new Runnable() {
@Override
public void run() {
View activeView = activePreviewTouchViewRef.get();
if (activeView == null || activeView != targetView) {
return;
}
activePreviewLongPressTriggered = true;
long heldMs = Math.max(0L, android.os.SystemClock.uptimeMillis() - activePreviewTouchDownMs);
recordPreviewHold(activeView, heldMs);
log("Preview hold reached chooser threshold at " + heldMs + "ms");
try {
activeView.performClick();
} catch (Throwable t) {
log("Failed to forward preview hold to stock action: " + t);
}
}
};
MAIN_HANDLER.postDelayed(activePreviewLongPressRunnable, PREVIEW_CHOOSER_HOLD_MS);
}
private static void recordPreviewHold(View view, long heldMs) {
lastPreviewClickTargetRef = new WeakReference<>(view);
lastPreviewClickHeldMs = Math.max(0L, heldMs);
lastPreviewClickRecordedAtMs = android.os.SystemClock.uptimeMillis();
}
private static void launchPreviewTapAction(View view) {
launchMarkupEditor(view);
}
private static boolean isPointInsideView(View view, float rawX, float rawY) {
int[] location = new int[2];
view.getLocationOnScreen(location);
return rawX >= location[0]
&& rawX < location[0] + view.getWidth()
&& rawY >= location[1]
&& rawY < location[1] + view.getHeight();
}
private static void hideShelfChrome(View shelfView) {
hideBoundView(ReflectionHelpers.getObjectFieldIfExists(shelfView, "actionsContainerBackground"));
hideBoundView(ReflectionHelpers.getObjectFieldIfExists(shelfView, "actionsContainer"));
hideBoundView(ReflectionHelpers.getObjectFieldIfExists(shelfView, "dismissButton"));
hideView(shelfView, "actions_container_background");
hideView(shelfView, "actions_container");
hideView(shelfView, "screenshot_actions");
hideView(shelfView, "screenshot_dismiss_button");
}
private static void hideBoundView(Object candidate) {
if (candidate instanceof View) {
hideViewInstance((View) candidate);
}
}
private static void hideView(View root, String idName) {
int id = root.getResources().getIdentifier(idName, "id", "com.android.systemui");
if (id == 0) {
return;
}
View view = root.findViewById(id);
if (view == null) {
return;
}
hideViewInstance(view);
}
private static void hideViewInstance(View view) {
view.setVisibility(View.GONE);
view.setAlpha(0.0f);
view.setClickable(false);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
if (layoutParams != null) {
layoutParams.width = 0;
layoutParams.height = 0;
view.setLayoutParams(layoutParams);
}
}
private static void updateStackUi(View shelfView) {
ViewGroup shelfStatic = findViewGroup(shelfView, "screenshot_static");
ImageView preview = findImageView(shelfView, "screenshot_preview");
ImageView previewBlur = findImageView(shelfView, "screenshot_preview_blur");
if (shelfStatic == null || preview == null || previewBlur == null) {
return;
}
logViewGeometry("preview before update", preview);
logViewGeometry("previewBlur before update", previewBlur);
tintPreviewBorder(shelfView);
clearSyntheticStackUi(shelfStatic);
List<Bitmap> stackBitmaps = HookState.getStackBitmaps();
resetRearPreview(previewBlur);
if (stackBitmaps.isEmpty()) {
log("updateStackUi: empty stack");
return;
}
previewBlur.setVisibility(View.INVISIBLE);
preview.setAlpha(0.0f);
int visibleRearCards = Math.min(2, stackBitmaps.size());
for (int depth = visibleRearCards - 1; depth >= 0; depth--) {
addOverlayStackCard(shelfStatic, preview, stackBitmaps.get(depth), depth + 1);
}
Bitmap currentBitmap = HookState.getLastPreviewBitmap();
if (currentBitmap != null) {
addOverlayStackCard(shelfStatic, preview, currentBitmap, 0);
}
int totalCount = stackBitmaps.size() + (HookState.getLastPreviewBitmap() != null ? 1 : 0);
log("updateStackUi: stack=" + stackBitmaps.size() + " total=" + totalCount);
}
private static void clearStackUi(View shelfView) {
ViewGroup shelfStatic = findViewGroup(shelfView, "screenshot_static");
ImageView preview = findImageView(shelfView, "screenshot_preview");
ImageView previewBlur = findImageView(shelfView, "screenshot_preview_blur");
if (shelfStatic != null) {
clearSyntheticStackUi(shelfStatic);
}
if (preview != null) {
preview.setAlpha(1.0f);
preview.setTranslationX(0.0f);
preview.setTranslationY(0.0f);
}
if (previewBlur != null) {
resetRearPreview(previewBlur);
}
}
private static void clearSyntheticStackUi(ViewGroup shelfStatic) {
clearOverlayStackCards(shelfStatic);
for (int i = shelfStatic.getChildCount() - 1; i >= 0; i--) {
View child = shelfStatic.getChildAt(i);
Object tag = child.getTag();
if (tag instanceof String && ((String) tag).startsWith(STACK_CARD_TAG_PREFIX)) {
shelfStatic.removeViewAt(i);
}
}
}
private static void applyRearStackCard(ImageView stackCard, ImageView preview, Bitmap bitmap, int depth) {
Bitmap cardBitmap = createCardBitmap(preview, bitmap, CARD_FRAME_INSET_DP);
if (cardBitmap == null) {
return;
}
applyScreenshotBitmap(stackCard, cardBitmap);
syncPreviewLayout(preview, stackCard);
layoutStackCardToPreviewBounds(preview, stackCard);
applyStackCard(stackCard, preview, depth);
}
private static void applyStackCard(ImageView stackCard, ImageView preview, int depth) {
stackCard.setAdjustViewBounds(false);
stackCard.setClickable(false);
stackCard.setVisibility(View.VISIBLE);
stackCard.setAlpha(1.0f);
stackCard.setScaleX(1.0f);
stackCard.setScaleY(1.0f);
float offsetX = dp(stackCard, STACK_CARD_X_OFFSET_DP) * (depth + 1);
float offsetY = dp(stackCard, STACK_CARD_Y_OFFSET_DP) * (depth + 1);
stackCard.setTranslationX(preview.getTranslationX() - offsetX);
stackCard.setTranslationY(preview.getTranslationY() + offsetY);
stackCard.setElevation(Math.max(0.0f, preview.getElevation() - (depth + 1)));
}
private static void addOverlayStackCard(ViewGroup shelfStatic, ImageView preview, Bitmap bitmap, int depth) {
Bitmap cardBitmap = createCardBitmap(preview, bitmap, CARD_FRAME_INSET_DP);
if (cardBitmap == null) {
return;
}
BitmapDrawable drawable = new BitmapDrawable(shelfStatic.getResources(), cardBitmap);
int offsetX = dp(preview, STACK_CARD_X_OFFSET_DP) * depth;
int offsetY = dp(preview, STACK_CARD_Y_OFFSET_DP) * depth;
int left = Math.round(preview.getX()) + offsetX;
int top = Math.round(preview.getY()) - offsetY;
drawable.setBounds(left, top, left + cardBitmap.getWidth(), top + cardBitmap.getHeight());
shelfStatic.getOverlay().add(drawable);
overlayStackCards.add(drawable);
}
private static ImageView ensureSyntheticStackCard(ViewGroup shelfStatic, ImageView preview, int depth) {
String tag = STACK_CARD_TAG_PREFIX + depth;
for (int i = 0; i < shelfStatic.getChildCount(); i++) {
View child = shelfStatic.getChildAt(i);
if (tag.equals(child.getTag()) && child instanceof ImageView) {
return (ImageView) child;
}
}
ImageView stackCard = new ImageView(shelfStatic.getContext());
stackCard.setTag(tag);
ViewGroup.LayoutParams layoutParams = cloneLayoutParams(preview.getLayoutParams());
if (layoutParams != null) {
stackCard.setLayoutParams(layoutParams);
}
int previewIndex = shelfStatic.indexOfChild(preview);
int insertIndex = previewIndex >= 0 ? previewIndex : shelfStatic.getChildCount();
shelfStatic.addView(stackCard, insertIndex);
return stackCard;
}
private static void resetRearPreview(ImageView previewBlur) {
previewBlur.setImageDrawable(null);
previewBlur.setBackground(null);
previewBlur.setPadding(0, 0, 0, 0);
previewBlur.setTranslationX(0.0f);
previewBlur.setTranslationY(0.0f);
previewBlur.setScaleX(1.0f);
previewBlur.setScaleY(1.0f);
previewBlur.setAlpha(1.0f);
previewBlur.setVisibility(View.INVISIBLE);
}
private static void logViewGeometry(String label, View view) {
if (view == null) {
log(label + ": null");
return;
}
log(label
+ " left=" + view.getLeft()
+ " top=" + view.getTop()
+ " x=" + view.getX()
+ " y=" + view.getY()
+ " tx=" + view.getTranslationX()
+ " ty=" + view.getTranslationY()
+ " w=" + view.getWidth()
+ " h=" + view.getHeight()
+ " vis=" + view.getVisibility());
}
private static void scheduleStackUiUpdate(final View shelfView) {
shelfView.post(new Runnable() {
@Override
public void run() {
updateStackUi(shelfView);
shelfView.postDelayed(new Runnable() {
@Override
public void run() {
updateStackUi(shelfView);
}
}, STACK_UI_SETTLE_DELAY_MS);
}
});
}
private static void clearOverlayStackCards(ViewGroup shelfStatic) {
for (Drawable drawable : overlayStackCards) {
shelfStatic.getOverlay().remove(drawable);
}
overlayStackCards.clear();
}
private static Drawable createCardFrameDrawable(View view) {
int inset = dp(view, CARD_FRAME_INSET_DP);
float outerRadius = dp(view, 2.0f);
float innerRadius = Math.max(0.0f, outerRadius - inset);
GradientDrawable base = new GradientDrawable();
base.setShape(GradientDrawable.RECTANGLE);
base.setColor(IOS_FRAME_COLOR);
base.setCornerRadius(outerRadius);
GradientDrawable outer = new GradientDrawable();
outer.setShape(GradientDrawable.RECTANGLE);
outer.setColor(IOS_FRAME_COLOR);
outer.setCornerRadius(outerRadius);