aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/invalid/lena/scrcpy/SettingsActivity.java
blob: fce776b17e994bbb59a9d517e98ef91894b830d9 (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
116
117
package invalid.lena.scrcpy;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.WindowInsets;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

// Codec choice + streaming knobs. All persisted to SharedPreferences
// via Settings on click; the next session bringup reads them and
// applies to the scrcpy server cmdline.
public final class SettingsActivity extends Activity {

    @Override
    protected void onCreate(Bundle saved) {
        super.onCreate(saved);
        setContentView(R.layout.settings);
        Ui.padForInsets(findViewById(R.id.root), WindowInsets.Type.systemBars());
        showVersion();

        RadioGroup videoGroup   = findViewById(R.id.video_codec);
        RadioGroup audioGroup   = findViewById(R.id.audio_codec);
        RadioGroup maxSizeGroup = findViewById(R.id.max_size);
        RadioGroup bitRateGroup = findViewById(R.id.video_bit_rate);

        switch (Settings.videoCodec(this)) {
            case "h265": ((RadioButton) findViewById(R.id.video_h265)).setChecked(true); break;
            case "av1":  ((RadioButton) findViewById(R.id.video_av1)).setChecked(true);  break;
            default:     ((RadioButton) findViewById(R.id.video_h264)).setChecked(true);
        }
        switch (Settings.audioCodec(this)) {
            case "opus": ((RadioButton) findViewById(R.id.audio_opus)).setChecked(true); break;
            default:     ((RadioButton) findViewById(R.id.audio_raw)).setChecked(true);
        }
        switch (Settings.maxSize(this)) {
            case 480:  ((RadioButton) findViewById(R.id.max_size_480)).setChecked(true); break;
            case 720:  ((RadioButton) findViewById(R.id.max_size_720)).setChecked(true); break;
            case 1080: ((RadioButton) findViewById(R.id.max_size_1080)).setChecked(true); break;
            case 1440: ((RadioButton) findViewById(R.id.max_size_1440)).setChecked(true); break;
            case 2160: ((RadioButton) findViewById(R.id.max_size_2160)).setChecked(true); break;
            default:   ((RadioButton) findViewById(R.id.max_size_0)).setChecked(true);
        }
        switch (Settings.videoBitRate(this)) {
            case 1_000_000:  ((RadioButton) findViewById(R.id.bit_rate_1m)).setChecked(true);  break;
            case 2_000_000:  ((RadioButton) findViewById(R.id.bit_rate_2m)).setChecked(true);  break;
            case 8_000_000:  ((RadioButton) findViewById(R.id.bit_rate_8m)).setChecked(true);  break;
            case 16_000_000: ((RadioButton) findViewById(R.id.bit_rate_16m)).setChecked(true); break;
            default:         ((RadioButton) findViewById(R.id.bit_rate_4m)).setChecked(true);
        }
        videoGroup.setOnCheckedChangeListener((g, id) -> {
            String v = "h264";
            if (id == R.id.video_h265) v = "h265";
            else if (id == R.id.video_av1) v = "av1";
            Settings.setVideoCodec(this, v);
            Log.i("settings: video_codec=%s", v);
        });

        audioGroup.setOnCheckedChangeListener((g, id) -> {
            String a = id == R.id.audio_opus ? "opus" : "raw";
            Settings.setAudioCodec(this, a);
            Log.i("settings: audio_codec=%s", a);
        });

        maxSizeGroup.setOnCheckedChangeListener((g, id) -> {
            int v = 0;
            if      (id == R.id.max_size_480)  v = 480;
            else if (id == R.id.max_size_720)  v = 720;
            else if (id == R.id.max_size_1080) v = 1080;
            else if (id == R.id.max_size_1440) v = 1440;
            else if (id == R.id.max_size_2160) v = 2160;
            Settings.setMaxSize(this, v);
            Log.i("settings: max_size=%d", v);
        });

        bitRateGroup.setOnCheckedChangeListener((g, id) -> {
            // Every button maps explicitly. Falling back to
            // DEFAULT_VIDEO_BIT_RATE for the unmatched one silently tied
            // whichever button that was to the default's current value,
            // so changing the default changed what that button stored.
            int v;
            if      (id == R.id.bit_rate_1m)  v = 1_000_000;
            else if (id == R.id.bit_rate_2m)  v = 2_000_000;
            else if (id == R.id.bit_rate_4m)  v = 4_000_000;
            else if (id == R.id.bit_rate_8m)  v = 8_000_000;
            else if (id == R.id.bit_rate_16m) v = 16_000_000;
            else return;
            Settings.setVideoBitRate(this, v);
            Log.i("settings: video_bit_rate=%d", v);
        });

        findViewById(R.id.licenses).setOnClickListener(v ->
                startActivity(new android.content.Intent(this, Licenses.class)));

        CheckBox clipboardBox = findViewById(R.id.clipboard_sync);
        clipboardBox.setChecked(Settings.clipboardSync(this));
        clipboardBox.setOnCheckedChangeListener((b, checked) -> {
            Settings.setClipboardSync(this, checked);
            Log.i("settings: clipboard=%b", checked);
        });

    }

    @SuppressWarnings("deprecation")
    private void showVersion() {
        try {
            PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
            ((TextView) findViewById(R.id.version)).setText(getString(
                    R.string.version_format, info.versionName, info.getLongVersionCode()));
        } catch (PackageManager.NameNotFoundException e) {
            throw new IllegalStateException("installed package is missing", e);
        }
    }
}