-
-
Notifications
You must be signed in to change notification settings - Fork 215
/
MainActivity.java
840 lines (759 loc) · 42.3 KB
/
MainActivity.java
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
/*
* DroidVNC-NG main activity.
*
* Author: Christian Beier <[email protected]
*
* Copyright (C) 2020 Kitchen Armor.
*
* You can redistribute and/or modify this program under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place Suite 330, Boston, MA 02111-1307, USA.
*/
package net.christianbeier.droidvnc_ng;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
import android.provider.Settings;
import android.text.Editable;
import android.text.InputType;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.SingleLineTransformationMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.util.Pair;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.slider.Slider;
import com.google.android.material.switchmaterial.SwitchMaterial;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String PREFS_KEY_REVERSE_VNC_LAST_HOST = "reverse_vnc_last_host" ;
private static final String PREFS_KEY_REPEATER_VNC_LAST_HOST = "repeater_vnc_last_host" ;
private static final String PREFS_KEY_REPEATER_VNC_LAST_ID = "repeater_vnc_last_id" ;
private Button mButtonToggle;
private TextView mAddress;
private boolean mIsMainServiceRunning;
private BroadcastReceiver mMainServiceBroadcastReceiver;
private AlertDialog mOutgoingConnectionWaitDialog;
private String mLastMainServiceRequestId;
private String mLastReverseHost;
private int mLastReversePort;
private String mLastRepeaterHost;
private int mLastRepeaterPort;
private String mLastRepeaterId;
private Defaults mDefaults;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
mDefaults = new Defaults(this);
mButtonToggle = findViewById(R.id.toggle);
mButtonToggle.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, MainService.class);
intent.putExtra(MainService.EXTRA_PORT, prefs.getInt(Constants.PREFS_KEY_SETTINGS_PORT, mDefaults.getPort()));
intent.putExtra(MainService.EXTRA_PASSWORD, prefs.getString(Constants.PREFS_KEY_SETTINGS_PASSWORD, mDefaults.getPassword()));
intent.putExtra(MainService.EXTRA_FILE_TRANSFER, prefs.getBoolean(Constants.PREFS_KEY_SETTINGS_FILE_TRANSFER, mDefaults.getFileTransfer()));
intent.putExtra(MainService.EXTRA_VIEW_ONLY, prefs.getBoolean(Constants.PREFS_KEY_SETTINGS_VIEW_ONLY, mDefaults.getViewOnly()));
intent.putExtra(MainService.EXTRA_SHOW_POINTERS, prefs.getBoolean(Constants.PREFS_KEY_SETTINGS_SHOW_POINTERS, mDefaults.getShowPointers()));
intent.putExtra(MainService.EXTRA_SCALING, prefs.getFloat(Constants.PREFS_KEY_SETTINGS_SCALING, mDefaults.getScaling()));
intent.putExtra(MainService.EXTRA_ACCESS_KEY, prefs.getString(Constants.PREFS_KEY_SETTINGS_ACCESS_KEY, mDefaults.getAccessKey()));
if(mIsMainServiceRunning) {
intent.setAction(MainService.ACTION_STOP);
}
else {
intent.setAction(MainService.ACTION_START);
}
mButtonToggle.setEnabled(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
});
mAddress = findViewById(R.id.address);
Button reverseVNC = findViewById(R.id.reverse_vnc);
reverseVNC.setOnClickListener(view -> {
final EditText inputText = new EditText(this);
inputText.setInputType(InputType.TYPE_CLASS_TEXT);
inputText.setHint(getString(R.string.main_activity_reverse_vnc_hint));
String lastHost = prefs.getString(PREFS_KEY_REVERSE_VNC_LAST_HOST, null);
if(lastHost != null) {
inputText.setText(lastHost);
// select all to make new input quicker
inputText.setSelectAllOnFocus(true);
}
inputText.requestFocus();
inputText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
EditText reconnectTriesInputText = new EditText(this);
reconnectTriesInputText.setInputType(InputType.TYPE_CLASS_NUMBER);
reconnectTriesInputText.setHint(getString(R.string.main_activity_reconnect_tries_hint));
reconnectTriesInputText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout inputLayout = new LinearLayout(this);
inputLayout.setPadding(
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, Resources.getSystem().getDisplayMetrics()),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, Resources.getSystem().getDisplayMetrics()),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, Resources.getSystem().getDisplayMetrics()),
0
);
inputLayout.setOrientation(LinearLayout.VERTICAL);
inputLayout.addView(inputText);
inputLayout.addView(reconnectTriesInputText);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(R.string.main_activity_reverse_vnc_button)
.setView(inputLayout)
.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {
// parse host and port parts
String[] parts = inputText.getText().toString().split("\\:");
String host = parts[0];
int port = mDefaults.getPortReverse();
if (parts.length > 1) {
try {
port = Integer.parseInt(parts[1]);
} catch(NumberFormatException unused) {
// stays at default reverse port
}
}
Log.d(TAG, "reverse vnc " + host + ":" + port);
mLastMainServiceRequestId = UUID.randomUUID().toString();
mLastReverseHost = host;
mLastReversePort = port;
Intent request = new Intent(MainActivity.this, MainService.class);
request.putExtra(MainService.EXTRA_ACCESS_KEY, prefs.getString(Constants.PREFS_KEY_SETTINGS_ACCESS_KEY, mDefaults.getAccessKey()));
request.setAction(MainService.ACTION_CONNECT_REVERSE);
request.putExtra(MainService.EXTRA_HOST, host);
request.putExtra(MainService.EXTRA_PORT, port);
request.putExtra(MainService.EXTRA_REQUEST_ID, mLastMainServiceRequestId);
try {
request.putExtra(MainService.EXTRA_RECONNECT_TRIES, Integer.parseInt(reconnectTriesInputText.getText().toString()));
} catch (NumberFormatException ignored) {
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(request);
} else {
startService(request);
}
// show a progress dialog
ProgressBar progressBar = new ProgressBar(this);
progressBar.setPadding(0,0,0, (int) (30 * getResources().getDisplayMetrics().density));
mOutgoingConnectionWaitDialog = new AlertDialog.Builder(this)
.setCancelable(false)
.setTitle(R.string.main_activity_reverse_vnc_button)
.setMessage(getString(R.string.main_activity_connecting_to, host + ":" + port))
.setView(progressBar)
.show();
})
.setNegativeButton(android.R.string.cancel, (dialogInterface, i) -> dialogInterface.cancel())
.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
});
Button repeaterVNC = findViewById(R.id.repeater_vnc);
repeaterVNC.setOnClickListener(view -> {
final EditText hostInputText = new EditText(this);
hostInputText.setInputType(InputType.TYPE_CLASS_TEXT);
hostInputText.setHint(getString(R.string.main_activity_repeater_vnc_hint));
String lastHost = prefs.getString(PREFS_KEY_REPEATER_VNC_LAST_HOST, "");
hostInputText.setText(lastHost); //host:port
hostInputText.requestFocus();
hostInputText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
final EditText idInputText = new EditText(this);
idInputText.setInputType(InputType.TYPE_CLASS_NUMBER);
idInputText.setHint(getString(R.string.main_activity_repeater_vnc_hint_id));
String lastID = prefs.getString(PREFS_KEY_REPEATER_VNC_LAST_ID, "");
idInputText.setText(lastID); //host:port
idInputText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
EditText reconnectTriesInputText = new EditText(this);
reconnectTriesInputText.setInputType(InputType.TYPE_CLASS_NUMBER);
reconnectTriesInputText.setHint(getString(R.string.main_activity_reconnect_tries_hint));
reconnectTriesInputText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout inputLayout = new LinearLayout(this);
inputLayout.setPadding(
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, Resources.getSystem().getDisplayMetrics()),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, Resources.getSystem().getDisplayMetrics()),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, Resources.getSystem().getDisplayMetrics()),
0
);
inputLayout.setOrientation(LinearLayout.VERTICAL);
inputLayout.addView(hostInputText);
inputLayout.addView(idInputText);
inputLayout.addView(reconnectTriesInputText);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(R.string.main_activity_repeater_vnc_button)
.setView(inputLayout)
.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {
// parse host and port parts
String[] parts = hostInputText.getText().toString().split("\\:");
String host = parts[0];
int port = mDefaults.getPortRepeater();
if (parts.length > 1) {
try {
port = Integer.parseInt(parts[1]);
} catch(NumberFormatException unused) {
// stays at default repeater port
}
}
// parse ID
String repeaterId = idInputText.getText().toString();
// sanity-check
if (host.isEmpty() || repeaterId.isEmpty()) {
Toast.makeText(MainActivity.this, getString(R.string.main_activity_repeater_vnc_input_missing), Toast.LENGTH_LONG).show();
return;
}
// done
Log.d(TAG, "repeater vnc " + host + ":" + port + ":" + repeaterId);
mLastMainServiceRequestId = UUID.randomUUID().toString();
mLastRepeaterHost = host;
mLastRepeaterPort = port;
mLastRepeaterId = repeaterId;
Intent request = new Intent(MainActivity.this, MainService.class);
request.putExtra(MainService.EXTRA_ACCESS_KEY, prefs.getString(Constants.PREFS_KEY_SETTINGS_ACCESS_KEY, mDefaults.getAccessKey()));
request.setAction(MainService.ACTION_CONNECT_REPEATER);
request.putExtra(MainService.EXTRA_HOST, host);
request.putExtra(MainService.EXTRA_PORT, port);
request.putExtra(MainService.EXTRA_REPEATER_ID, repeaterId);
request.putExtra(MainService.EXTRA_REQUEST_ID, mLastMainServiceRequestId);
try {
request.putExtra(MainService.EXTRA_RECONNECT_TRIES, Integer.parseInt(reconnectTriesInputText.getText().toString()));
} catch (NumberFormatException ignored) {
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(request);
} else {
startService(request);
}
// show a progress dialog
ProgressBar progressBar = new ProgressBar(this);
progressBar.setPadding(0,0,0, (int) (30 * getResources().getDisplayMetrics().density));
mOutgoingConnectionWaitDialog = new AlertDialog.Builder(this)
.setCancelable(false)
.setTitle(R.string.main_activity_repeater_vnc_button)
.setMessage(getString(R.string.main_activity_connecting_to, host + ":" + port + " - " + repeaterId))
.setView(progressBar)
.show();
})
.setNegativeButton(android.R.string.cancel, (dialogInterface, i) -> dialogInterface.cancel())
.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
});
final EditText port = findViewById(R.id.settings_port);
if(prefs.getInt(Constants.PREFS_KEY_SETTINGS_PORT, mDefaults.getPort()) < 0) {
port.setHint(R.string.main_activity_settings_port_not_listening);
} else {
port.setText(String.valueOf(prefs.getInt(Constants.PREFS_KEY_SETTINGS_PORT, mDefaults.getPort())));
}
port.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
try {
SharedPreferences.Editor ed = prefs.edit();
ed.putInt(Constants.PREFS_KEY_SETTINGS_PORT, Integer.parseInt(charSequence.toString()));
ed.apply();
} catch(NumberFormatException e) {
// nop
}
}
@Override
public void afterTextChanged(Editable editable) {
if(port.getText().length() == 0) {
// hint that not listening
port.setHint(R.string.main_activity_settings_port_not_listening);
// and set default
SharedPreferences.Editor ed = prefs.edit();
ed.putInt(Constants.PREFS_KEY_SETTINGS_PORT, -1);
ed.apply();
}
}
});
port.setOnFocusChangeListener((v, hasFocus) -> {
// move cursor to end of text
port.setSelection(port.getText().length());
});
final EditText password = findViewById(R.id.settings_password);
password.setText(prefs.getString(Constants.PREFS_KEY_SETTINGS_PASSWORD, mDefaults.getPassword()));
password.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// only save new value if it differs from the default and was not saved before
if(!(prefs.getString(Constants.PREFS_KEY_SETTINGS_PASSWORD, null) == null && charSequence.toString().equals(mDefaults.getPassword()))) {
SharedPreferences.Editor ed = prefs.edit();
ed.putString(Constants.PREFS_KEY_SETTINGS_PASSWORD, charSequence.toString());
ed.apply();
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
// show/hide password on focus change. NB that this triggers onTextChanged above, so we have
// to take special precautions there.
password.setOnFocusChangeListener((v, hasFocus) -> {
if (hasFocus) {
password.setTransformationMethod(new SingleLineTransformationMethod());
} else {
password.setTransformationMethod(new PasswordTransformationMethod());
}
// move cursor to end of text
password.setSelection(password.getText().length());
});
final EditText accessKey = findViewById(R.id.settings_access_key);
accessKey.setText(prefs.getString(Constants.PREFS_KEY_SETTINGS_ACCESS_KEY, mDefaults.getAccessKey()));
accessKey.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// only save new value if it differs from the default and was not saved before
if(!(prefs.getString(Constants.PREFS_KEY_SETTINGS_ACCESS_KEY, null) == null && charSequence.toString().equals(mDefaults.getAccessKey()))) {
SharedPreferences.Editor ed = prefs.edit();
ed.putString(Constants.PREFS_KEY_SETTINGS_ACCESS_KEY, charSequence.toString());
ed.apply();
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
// show/hide access key on focus change. NB that this triggers onTextChanged above, so we have
// to take special precautions there.
accessKey.setOnFocusChangeListener((v, hasFocus) -> {
if (hasFocus) {
accessKey.setTransformationMethod(new SingleLineTransformationMethod());
} else {
accessKey.setTransformationMethod(new PasswordTransformationMethod());
// if value just saved was empty, reset preference and UI back to default
String savedAccessKey = prefs.getString(Constants.PREFS_KEY_SETTINGS_ACCESS_KEY, null);
if(savedAccessKey != null && savedAccessKey.isEmpty()) {
SharedPreferences.Editor ed = prefs.edit();
ed.putString(Constants.PREFS_KEY_SETTINGS_ACCESS_KEY, mDefaults.getAccessKey());
ed.apply();
accessKey.setText(mDefaults.getAccessKey());
}
}
// move cursor to end of text
accessKey.setSelection(accessKey.getText().length());
});
final EditText startOnBootDelay = findViewById(R.id.settings_start_on_boot_delay);
startOnBootDelay.setText(String.valueOf(prefs.getInt(Constants.PREFS_KEY_SETTINGS_START_ON_BOOT_DELAY, mDefaults.getStartOnBootDelay())));
startOnBootDelay.setEnabled(prefs.getBoolean(Constants.PREFS_KEY_SETTINGS_START_ON_BOOT, mDefaults.getStartOnBoot()));
startOnBootDelay.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
try {
SharedPreferences.Editor ed = prefs.edit();
ed.putInt(Constants.PREFS_KEY_SETTINGS_START_ON_BOOT_DELAY, Integer.parseInt(charSequence.toString()));
ed.apply();
} catch(NumberFormatException e) {
// nop
}
}
@Override
public void afterTextChanged(Editable editable) {
// if value just saved was empty, reset preference and UI back to default
String savedAccessKey = prefs.getString(Constants.PREFS_KEY_SETTINGS_ACCESS_KEY, null);
if(savedAccessKey != null && savedAccessKey.isEmpty()) {
SharedPreferences.Editor ed = prefs.edit();
ed.putString(Constants.PREFS_KEY_SETTINGS_ACCESS_KEY, mDefaults.getAccessKey());
ed.apply();
accessKey.setText(mDefaults.getAccessKey());
}
if(startOnBootDelay.getText().length() == 0) {
// reset to default
startOnBootDelay.setHint(String.valueOf(mDefaults.getStartOnBootDelay()));
// and remove preference
SharedPreferences.Editor ed = prefs.edit();
ed.remove(Constants.PREFS_KEY_SETTINGS_START_ON_BOOT_DELAY);
ed.apply();
}
}
});
// move cursor to end of text
startOnBootDelay.setOnFocusChangeListener((v, hasFocus) -> startOnBootDelay.setSelection(startOnBootDelay.getText().length()));
final SwitchMaterial startOnBoot = findViewById(R.id.settings_start_on_boot);
startOnBoot.setChecked(prefs.getBoolean(Constants.PREFS_KEY_SETTINGS_START_ON_BOOT, mDefaults.getStartOnBoot()));
startOnBoot.setOnCheckedChangeListener((compoundButton, b) -> {
SharedPreferences.Editor ed = prefs.edit();
ed.putBoolean(Constants.PREFS_KEY_SETTINGS_START_ON_BOOT, b);
ed.apply();
startOnBootDelay.setEnabled(b);
});
if(Build.VERSION.SDK_INT >= 33) {
// no use asking for permission on Android 13+, always denied.
// users can always read/write Documents and Downloads tough.
findViewById(R.id.settings_row_file_transfer).setVisibility(View.GONE);
} else {
final SwitchMaterial fileTransfer = findViewById(R.id.settings_file_transfer);
fileTransfer.setChecked(prefs.getBoolean(Constants.PREFS_KEY_SETTINGS_FILE_TRANSFER, mDefaults.getFileTransfer()));
fileTransfer.setOnCheckedChangeListener((compoundButton, b) -> {
SharedPreferences.Editor ed = prefs.edit();
ed.putBoolean(Constants.PREFS_KEY_SETTINGS_FILE_TRANSFER, b);
ed.apply();
});
}
Slider scaling = findViewById(R.id.settings_scaling);
scaling.setValue(
prefs.getFloat(Constants.PREFS_KEY_SETTINGS_SCALING,
(float) Math.ceil(mDefaults.getScaling() * 100 / scaling.getStepSize()) * scaling.getStepSize() / 100)
* 100);
scaling.setLabelFormatter(value -> Math.round(value) + " %");
scaling.addOnChangeListener((slider, value, fromUser) -> {
SharedPreferences.Editor ed = prefs.edit();
ed.putFloat(Constants.PREFS_KEY_SETTINGS_SCALING, value/100);
ed.apply();
});
final SwitchMaterial showPointers = findViewById(R.id.settings_show_pointers);
showPointers.setChecked(prefs.getBoolean(Constants.PREFS_KEY_SETTINGS_SHOW_POINTERS, mDefaults.getShowPointers()));
showPointers.setOnCheckedChangeListener((compoundButton, b) -> {
SharedPreferences.Editor ed = prefs.edit();
ed.putBoolean(Constants.PREFS_KEY_SETTINGS_SHOW_POINTERS, b);
ed.apply();
});
showPointers.setEnabled(!prefs.getBoolean(Constants.PREFS_KEY_SETTINGS_VIEW_ONLY, mDefaults.getViewOnly()));
final SwitchMaterial viewOnly = findViewById(R.id.settings_view_only);
viewOnly.setChecked(prefs.getBoolean(Constants.PREFS_KEY_SETTINGS_VIEW_ONLY, mDefaults.getViewOnly()));
viewOnly.setOnCheckedChangeListener((compoundButton, b) -> {
SharedPreferences.Editor ed = prefs.edit();
ed.putBoolean(Constants.PREFS_KEY_SETTINGS_VIEW_ONLY, b);
ed.apply();
// pointers depend on this one
showPointers.setEnabled(!b);
});
TextView about = findViewById(R.id.about);
about.setText(getString(R.string.main_activity_about, BuildConfig.VERSION_NAME));
mMainServiceBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (MainService.ACTION_START.equals(intent.getAction())) {
if(intent.getBooleanExtra(MainService.EXTRA_REQUEST_SUCCESS, false)) {
// was a successful START requested by anyone (but sent by MainService, as the receiver is not exported!)
Log.d(TAG, "got MainService started success event");
onServerStarted();
} else {
// was a failed START requested by anyone (but sent by MainService, as the receiver is not exported!)
Log.d(TAG, "got MainService started fail event");
// if it was, by us, re-enable the button!
mButtonToggle.setEnabled(true);
// let focus stay on button
mButtonToggle.requestFocus();
}
}
if (MainService.ACTION_STOP.equals(intent.getAction())
&& (intent.getBooleanExtra(MainService.EXTRA_REQUEST_SUCCESS, true))) {
// was a successful STOP requested by anyone (but sent by MainService, as the receiver is not exported!)
// or a STOP without any extras
Log.d(TAG, "got MainService stopped event");
onServerStopped();
}
if (MainService.ACTION_CONNECT_REVERSE.equals(intent.getAction())
&& mLastMainServiceRequestId != null
&& mLastMainServiceRequestId.equals(intent.getStringExtra(MainService.EXTRA_REQUEST_ID))) {
// was a CONNECT_REVERSE requested by us
if (intent.getBooleanExtra(MainService.EXTRA_REQUEST_SUCCESS, false)) {
Toast.makeText(MainActivity.this,
getString(R.string.main_activity_reverse_vnc_success,
mLastReverseHost,
mLastReversePort),
Toast.LENGTH_LONG)
.show();
SharedPreferences.Editor ed = prefs.edit();
ed.putString(PREFS_KEY_REVERSE_VNC_LAST_HOST,
mLastReverseHost + ":" + mLastReversePort);
ed.apply();
} else
Toast.makeText(MainActivity.this,
getString(R.string.main_activity_reverse_vnc_fail,
mLastReverseHost,
mLastReversePort),
Toast.LENGTH_LONG)
.show();
// reset this
mLastMainServiceRequestId = null;
try {
mOutgoingConnectionWaitDialog.dismiss();
} catch(NullPointerException ignored) {
}
}
if (MainService.ACTION_CONNECT_REPEATER.equals(intent.getAction())
&& mLastMainServiceRequestId != null
&& mLastMainServiceRequestId.equals(intent.getStringExtra(MainService.EXTRA_REQUEST_ID))) {
// was a CONNECT_REPEATER requested by us
if (intent.getBooleanExtra(MainService.EXTRA_REQUEST_SUCCESS, false)) {
Toast.makeText(MainActivity.this,
getString(R.string.main_activity_repeater_vnc_success,
mLastRepeaterHost,
mLastRepeaterPort,
mLastRepeaterId),
Toast.LENGTH_LONG)
.show();
SharedPreferences.Editor ed = prefs.edit();
ed.putString(PREFS_KEY_REPEATER_VNC_LAST_HOST,
mLastRepeaterHost + ":" + mLastRepeaterPort);
ed.putString(PREFS_KEY_REPEATER_VNC_LAST_ID,
mLastRepeaterId);
ed.apply();
}
else
Toast.makeText(MainActivity.this,
getString(R.string.main_activity_repeater_vnc_fail,
mLastRepeaterHost,
mLastRepeaterPort,
mLastRepeaterId),
Toast.LENGTH_LONG)
.show();
// reset this
mLastMainServiceRequestId = null;
try {
mOutgoingConnectionWaitDialog.dismiss();
} catch(NullPointerException ignored) {
}
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(MainService.ACTION_START);
filter.addAction(MainService.ACTION_STOP);
filter.addAction(MainService.ACTION_CONNECT_REVERSE);
filter.addAction(MainService.ACTION_CONNECT_REPEATER);
// register the receiver as NOT_EXPORTED so it only receives broadcasts sent by MainService,
// not a malicious fake broadcaster like
// `adb shell am broadcast -a net.christianbeier.droidvnc_ng.ACTION_STOP --ez net.christianbeier.droidvnc_ng.EXTRA_REQUEST_SUCCESS true`
// for instance
ContextCompat.registerReceiver(this, mMainServiceBroadcastReceiver, filter, ContextCompat.RECEIVER_NOT_EXPORTED);
// setup UI initial state
if (MainService.isServerActive()) {
Log.d(TAG, "Found server to be started");
onServerStarted();
} else {
Log.d(TAG, "Found server to be stopped");
onServerStopped();
}
}
@SuppressLint("SetTextI18n")
@Override
protected void onResume() {
super.onResume();
/*
Update Input permission display.
*/
TextView inputStatus = findViewById(R.id.permission_status_input);
if(InputService.isConnected()) {
inputStatus.setText(R.string.main_activity_granted);
inputStatus.setTextColor(getColor(R.color.granted));
} else {
inputStatus.setText(R.string.main_activity_denied);
inputStatus.setTextColor(getColor(R.color.denied));
}
/*
Update File Access permission display. Only show on < Android 13.
*/
if(Build.VERSION.SDK_INT < 33) {
TextView fileAccessStatus = findViewById(R.id.permission_status_file_access);
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
fileAccessStatus.setText(R.string.main_activity_granted);
fileAccessStatus.setTextColor(getColor(R.color.granted));
} else {
fileAccessStatus.setText(R.string.main_activity_denied);
fileAccessStatus.setTextColor(getColor(R.color.denied));
}
} else {
findViewById(R.id.permission_row_file_access).setVisibility(View.GONE);
}
/*
Update Notification permission display. Only show on >= Android 13.
*/
if(Build.VERSION.SDK_INT >= 33) {
TextView notificationStatus = findViewById(R.id.permission_status_notification);
if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
notificationStatus.setText(R.string.main_activity_granted);
notificationStatus.setTextColor(getColor(R.color.granted));
} else {
notificationStatus.setText(R.string.main_activity_denied);
notificationStatus.setTextColor(getColor(R.color.denied));
}
notificationStatus.setOnClickListener(view -> {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + getPackageName()));
startActivity(intent);
});
} else {
findViewById(R.id.permission_row_notification).setVisibility(View.GONE);
}
/*
Update Screen Capturing permission display.
*/
TextView screenCapturingStatus = findViewById(R.id.permission_status_screen_capturing);
if(MediaProjectionService.isMediaProjectionEnabled()) {
screenCapturingStatus.setText(R.string.main_activity_granted);
screenCapturingStatus.setTextColor(getColor(R.color.granted));
}
if(!MediaProjectionService.isMediaProjectionEnabled()) {
screenCapturingStatus.setText(R.string.main_activity_denied);
screenCapturingStatus.setTextColor(getColor(R.color.denied));
}
if(!MediaProjectionService.isMediaProjectionEnabled() && InputService.isTakingScreenShots()) {
screenCapturingStatus.setText(R.string.main_activity_fallback);
screenCapturingStatus.setTextColor(getColor(R.color.fallback));
}
/*
Update start-on-boot permission display. Only show on >= Android 10.
*/
if (Build.VERSION.SDK_INT >= 30) {
TextView startOnBootStatus = findViewById(R.id.permission_status_start_on_boot);
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.PREFS_KEY_SETTINGS_START_ON_BOOT, mDefaults.getStartOnBoot())
&& InputService.isConnected()) {
startOnBootStatus.setText(R.string.main_activity_granted);
startOnBootStatus.setTextColor(getColor(R.color.granted));
} else {
startOnBootStatus.setText(R.string.main_activity_denied);
startOnBootStatus.setTextColor(getColor(R.color.denied));
// wire this up only for denied status
startOnBootStatus.setOnClickListener(view -> {
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.PREFS_KEY_SETTINGS_START_ON_BOOT, mDefaults.getStartOnBoot()))
{
Intent inputRequestIntent = new Intent(this, InputRequestActivity.class);
inputRequestIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
inputRequestIntent.putExtra(InputRequestActivity.EXTRA_DO_NOT_START_MAIN_SERVICE_ON_FINISH, true);
startActivity(inputRequestIntent);
}
});
}
} else {
findViewById(R.id.permission_row_start_on_boot).setVisibility(View.GONE);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
unregisterReceiver(mMainServiceBroadcastReceiver);
}
private void onServerStarted() {
mButtonToggle.post(() -> {
mButtonToggle.setText(R.string.stop);
mButtonToggle.setEnabled(true);
// let focus stay on button
mButtonToggle.requestFocus();
});
if(MainService.getPort() >= 0) {
HashMap<ClickableSpan, Pair<Integer,Integer>> spans = new HashMap<>();
// uhh there must be a nice functional way for this
ArrayList<String> hosts = MainService.getIPv4s();
StringBuilder sb = new StringBuilder();
sb.append(getString(R.string.main_activity_address)).append(" ");
for (int i = 0; i < hosts.size(); ++i) {
String host = hosts.get(i);
sb.append(host).append(":").append(MainService.getPort()).append(" (");
int start = sb.length();
sb.append(getString(R.string.main_activity_share_link));
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(@NonNull View widget) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,
"http://" + host + ":" + (MainService.getPort() - 100) + "/vnc.html?autoconnect=true&show_dot=true&&host=" + host + "&port=" + MainService.getPort());
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, null));
}
};
spans.put(clickableSpan, Pair.create(start, sb.length()));
sb.append(")");
if (i != hosts.size() - 1)
sb.append(" ").append(getString(R.string.or)).append(" ");
}
sb.append(".");
// done with string and span creation, put it all together
SpannableString spannableString = new SpannableString(sb);
spans.forEach((clickableSpan, startEnd) -> spannableString.setSpan(clickableSpan, startEnd.first, startEnd.second, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE));
mAddress.post(() -> {
mAddress.setText(spannableString);
mAddress.setMovementMethod(LinkMovementMethod.getInstance());
});
} else {
mAddress.post(() -> mAddress.setText(R.string.main_activity_not_listening));
}
// show outbound connection interface
findViewById(R.id.outbound_text).setVisibility(View.VISIBLE);
findViewById(R.id.outbound_buttons).setVisibility(View.VISIBLE);
// indicate that changing these settings does not have an effect when the server is running
findViewById(R.id.settings_port).setEnabled(false);
findViewById(R.id.settings_password).setEnabled(false);
findViewById(R.id.settings_access_key).setEnabled(false);
findViewById(R.id.settings_scaling).setEnabled(false);
findViewById(R.id.settings_view_only).setEnabled(false);
findViewById(R.id.settings_file_transfer).setEnabled(false);
findViewById(R.id.settings_show_pointers).setEnabled(false);
mIsMainServiceRunning = true;
}
private void onServerStopped() {
mButtonToggle.post(() -> {
mButtonToggle.setText(R.string.start);
mButtonToggle.setEnabled(true);
// let focus stay on button
mButtonToggle.requestFocus();
});
mAddress.post(() -> mAddress.setText(""));
// hide outbound connection interface
findViewById(R.id.outbound_text).setVisibility(View.GONE);
findViewById(R.id.outbound_buttons).setVisibility(View.GONE);
// indicate that changing these settings does have an effect when the server is stopped
findViewById(R.id.settings_port).setEnabled(true);
findViewById(R.id.settings_password).setEnabled(true);
findViewById(R.id.settings_access_key).setEnabled(true);
findViewById(R.id.settings_scaling).setEnabled(true);
findViewById(R.id.settings_view_only).setEnabled(true);
findViewById(R.id.settings_file_transfer).setEnabled(true);
if(!((SwitchMaterial)findViewById(R.id.settings_view_only)).isChecked()) {
// pointers depend on view-only being disabled
findViewById(R.id.settings_show_pointers).setEnabled(true);
}
mIsMainServiceRunning = false;
}
}