aboutsummaryrefslogtreecommitdiff
path: root/app/src/debug/java/invalid/lena/scrcpy/Pattern.java
blob: 39a5d42b90401a7a66200b4a8ff74997fa2b8969 (plain) (blame)
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
package invalid.lena.scrcpy;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowInsets;
import android.view.WindowInsetsController;

// Deterministic moving target for the two-emulator E2E test. Debug-only,
// exported only in the debug manifest, and absent from release artifacts.
public final class Pattern extends Activity {

    private static final String TARGET_CLIPBOARD_TEXT = "scrcpy-e2e";
    private static final String SOURCE_CLIPBOARD_TEXT = "source-e2e";
    private static final String EXTRA_CLIPBOARD_TEXT = "clipboard_text";

    private ClipboardManager clipboard;
    private String clipboardText;
    private final ClipboardManager.OnPrimaryClipChangedListener clipboardListener =
            this::onClipboardChanged;

    @Override
    @SuppressWarnings("deprecation")
    protected void onCreate(Bundle state) {
        super.onCreate(state);
        String requested = getIntent().getStringExtra(EXTRA_CLIPBOARD_TEXT);
        clipboardText = SOURCE_CLIPBOARD_TEXT.equals(requested)
                ? SOURCE_CLIPBOARD_TEXT : TARGET_CLIPBOARD_TEXT;
        setContentView(new PatternView());
        clipboard = getSystemService(ClipboardManager.class);
        if (clipboard != null) clipboard.addPrimaryClipChangedListener(clipboardListener);
        getWindow().setDecorFitsSystemWindows(false);
        WindowInsetsController insets = getWindow().getInsetsController();
        if (insets != null) insets.hide(WindowInsets.Type.systemBars());
    }

    @Override
    protected void onDestroy() {
        if (clipboard != null) clipboard.removePrimaryClipChangedListener(clipboardListener);
        super.onDestroy();
    }

    private void onClipboardChanged() {
        ClipData data = clipboard.getPrimaryClip();
        if (data == null || data.getItemCount() == 0) return;
        CharSequence text = data.getItemAt(0).getText();
        if (SOURCE_CLIPBOARD_TEXT.contentEquals(text)) {
            Log.i("pattern: clipboard from source");
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP) {
            Log.i("pattern: key up code=%d", event.getKeyCode());
        }
        return super.dispatchKeyEvent(event);
    }

    private final class PatternView extends View {
        private final Paint paint = new Paint();
        private int step;

        PatternView() {
            super(Pattern.this);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getActionMasked() == MotionEvent.ACTION_UP) {
                Log.i("pattern: touch up x=%d y=%d", (int) event.getX(), (int) event.getY());
                performClick();
            }
            return true;
        }

        @Override
        public boolean performClick() {
            super.performClick();
            if (clipboard != null) {
                clipboard.setPrimaryClip(ClipData.newPlainText("e2e", clipboardText));
                Log.i("pattern: clipboard set");
            }
            return true;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            int w = getWidth();
            int h = getHeight();
            int mx = w / 2;
            int my = h / 2;
            paint.setColor(Color.rgb(224, 32, 32));
            canvas.drawRect(0, 0, mx, my, paint);
            paint.setColor(Color.rgb(32, 192, 48));
            canvas.drawRect(mx, 0, w, my, paint);
            paint.setColor(Color.rgb(32, 64, 224));
            canvas.drawRect(0, my, mx, h, paint);
            paint.setColor(Color.rgb(224, 192, 32));
            canvas.drawRect(mx, my, w, h, paint);

            paint.setColor(Color.WHITE);
            int x = step++ % Math.max(1, w);
            canvas.drawRect(x, 0, Math.min(w, x + 12), h, paint);
            postInvalidateDelayed(33);
        }
    }
}