/
ScummVMActivity.java
2205 lines (1935 loc) · 90.3 KB
/
ScummVMActivity.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
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 org.scummvm.scummvm;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.UriPermission;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.media.AudioManager;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.os.SystemClock;
import android.provider.DocumentsContract;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.PointerIcon;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.TreeSet;
import static android.content.res.Configuration.HARDKEYBOARDHIDDEN_NO;
import static android.content.res.Configuration.KEYBOARD_QWERTY;
public class ScummVMActivity extends Activity implements OnKeyboardVisibilityListener {
/* Establish whether the hover events are available */
private static boolean _hoverAvailable;
private ClipboardManager _clipboardManager;
private Version _currentScummVMVersion;
private File _configScummvmFile;
private File _logScummvmFile;
private File _actualScummVMDataDir;
private File _possibleExternalScummVMDir;
boolean _externalPathAvailableForReadAccess;
// SAF related
public final static int REQUEST_SAF = 50000;
// Use an Object to allow synchronization on it
private Object safSyncObject;
private int safRequestCode;
private int safResultCode;
private Uri safResultURI;
/**
* Ids to identify an external storage read (and write) request.
* They are app-defined int constants. The callback method gets the result of the request.
* Ie. returned in the Activity's onRequestPermissionsResult()
*/
private static final int MY_PERMISSIONS_REQUEST_READ_EXT_STORAGE = 100;
private static final int MY_PERMISSIONS_REQUEST_WRITE_EXT_STORAGE = 101;
private static final int MY_PERMISSION_ALL = 110;
private static final String[] MY_PERMISSIONS_STR_LIST = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
};
static {
try {
MouseHelper.checkHoverAvailable(); // this throws exception if we're on too old version
_hoverAvailable = true;
} catch (Throwable t) {
_hoverAvailable = false;
}
}
//
// --------------------------------------------------------------------------------------------------------------------------------------------
// Code for emulated in-app keyboard largely copied
// from https://github.com/pelya/commandergenius/tree/sdl_android/project
//
FrameLayout _videoLayout = null;
private EditableSurfaceView _main_surface = null;
private ImageView _toggleTouchModeKeyboardBtnIcon = null;
private ImageView _openMenuBtnIcon = null;
public View _screenKeyboard = null;
static boolean keyboardWithoutTextInputShown = false;
// boolean _isPaused = false;
private InputMethodManager _inputManager = null;
// Set to true in onDestroy
// This avoids that when C++ terminates we call finish() a second time
// This second finish causes termination when we are launched again
boolean _finishing = false;
private final int[][] TextInputKeyboardList =
{
{ 0, R.xml.qwerty },
{ 0, R.xml.qwerty_shift },
{ 0, R.xml.qwerty_alt },
{ 0, R.xml.qwerty_alt_shift }
};
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
final boolean hwKeyboard = isHWKeyboardConnected();
if (hwKeyboard) {
hideScreenKeyboard();
}
}
private boolean isHWKeyboardConnected() {
final Configuration config = getResources().getConfiguration();
return config.keyboard == KEYBOARD_QWERTY
&& config.hardKeyboardHidden == HARDKEYBOARDHIDDEN_NO;
}
public boolean isKeyboardOverlayShown() {
return keyboardWithoutTextInputShown;
}
public void showScreenKeyboardWithoutTextInputField(final int keyboard) {
if (_main_surface != null) {
_inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (!keyboardWithoutTextInputShown) {
keyboardWithoutTextInputShown = true;
runOnUiThread(new Runnable() {
public void run() {
//Log.d(ScummVM.LOG_TAG, "showScreenKeyboardWithoutTextInputField - captureMouse(false)");
_main_surface.captureMouse(false);
//_main_surface.showSystemMouseCursor(true);
if (keyboard == 0) {
// TODO do we need SHOW_FORCED HERE?
//_inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
//_inputManager.showSoftInput(_main_surface, InputMethodManager.SHOW_FORCED);
_inputManager.toggleSoftInputFromWindow(_main_surface.getWindowToken(), InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
_inputManager.showSoftInput(_main_surface, InputMethodManager.SHOW_IMPLICIT);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
} else {
if (_screenKeyboard != null) {
return;
}
class BuiltInKeyboardView extends CustomKeyboardView {
public boolean shift = false;
public boolean alt = false;
public final TreeSet<Integer> stickyKeys = new TreeSet<>();
public long mEventPressTime = -1;
public int mKeyRepeatedCount = 0;
public BuiltInKeyboardView(Context context, android.util.AttributeSet attrs) {
super(context, attrs);
}
public boolean onKeyDown(int key, final KeyEvent event) {
//Log.d(ScummVM.LOG_TAG, "BuiltInKeyboardView- 001 - onKeyDown()" );
return false;
}
public boolean onKeyUp(int key, final KeyEvent event) {
//Log.d(ScummVM.LOG_TAG, "BuiltInKeyboardView - 001 - onKeyUp()" );
return false;
}
public int getCompiledMetaState() {
int retCompiledMetaState = 0;
// search in the list of currently "ON" sticky keys to set the meta flags
for (int stickyActiveKeyCode : stickyKeys) {
switch (stickyActiveKeyCode) {
case KeyEvent.KEYCODE_SHIFT_LEFT:
retCompiledMetaState|= KeyEvent.META_SHIFT_LEFT_ON;
break;
case KeyEvent.KEYCODE_SHIFT_RIGHT:
retCompiledMetaState|= KeyEvent.META_SHIFT_RIGHT_ON;
break;
case KeyEvent.KEYCODE_CTRL_LEFT:
retCompiledMetaState|= KeyEvent.META_CTRL_LEFT_ON;
break;
case KeyEvent.KEYCODE_CTRL_RIGHT:
retCompiledMetaState|= KeyEvent.META_CTRL_RIGHT_ON;
break;
case KeyEvent.KEYCODE_ALT_LEFT:
retCompiledMetaState|= KeyEvent.META_ALT_LEFT_ON;
break;
case KeyEvent.KEYCODE_ALT_RIGHT:
retCompiledMetaState|= KeyEvent.META_ALT_RIGHT_ON;
break;
case KeyEvent.KEYCODE_META_LEFT:
retCompiledMetaState|= KeyEvent.META_META_LEFT_ON;
break;
case KeyEvent.KEYCODE_META_RIGHT:
retCompiledMetaState|= KeyEvent.META_META_RIGHT_ON;
break;
case KeyEvent.KEYCODE_CAPS_LOCK:
retCompiledMetaState|= KeyEvent.META_CAPS_LOCK_ON;
break;
case KeyEvent.KEYCODE_NUM_LOCK:
retCompiledMetaState|= KeyEvent.META_NUM_LOCK_ON;
break;
case KeyEvent.KEYCODE_SCROLL_LOCK:
retCompiledMetaState|= KeyEvent.META_SCROLL_LOCK_ON;
break;
case KeyEvent.KEYCODE_SYM:
// TODO Do we have or need a SYM key?
retCompiledMetaState|= KeyEvent.META_SYM_ON;
break;
default: break;
}
}
if (shift && !alt) {
retCompiledMetaState |= KeyEvent.META_SHIFT_LEFT_ON;
}
return retCompiledMetaState;
}
public void recheckStickyKeys() {
// setting sticky keys to their proper on or off state
// h
boolean atLeastOneStickyKeyWasChanged = false;
for (CustomKeyboard.CustomKey k: getKeyboard().getKeys()) {
if (stickyKeys.contains(k.codes[0]) && !k.on) {
k.on = true;
atLeastOneStickyKeyWasChanged = true;
} else if (!stickyKeys.contains(k.codes[0]) && k.sticky && k.on) {
k.on = false;
atLeastOneStickyKeyWasChanged = true;
}
}
if (atLeastOneStickyKeyWasChanged) {
invalidateAllKeys();
}
}
public void ChangeKeyboard() {
// Called when bringing up the keyboard
// or pressing one of the special keyboard keys that change the layout (eg "123...")
//
int idx = (shift ? 1 : 0) + (alt ? 2 : 0);
setKeyboard(new CustomKeyboard(ScummVMActivity.this, TextInputKeyboardList[idx][keyboard]));
setPreviewEnabled(false);
setProximityCorrectionEnabled(false);
// setKeyboard() already invalidates all keys,
// here we check for our memory of sticky keys state (and any that changed to on and were added to stickyKeys Set)
recheckStickyKeys();
//ScummVMActivity.this._scummvm.displayMessageOnOSD ("NEW KEYBOARD LAYOUT: QWERTY"
// + (alt ? " ALT " : "") + (shift? " SHIFT" : ""));
}
public void resetEventAndTimestamps() {
// clear event timestamps and repetition counts
mEventPressTime = -1;
mKeyRepeatedCount = -1;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// Reload keyboard to adapt to the new size
ChangeKeyboard();
}
}
final BuiltInKeyboardView builtinKeyboard = new BuiltInKeyboardView(ScummVMActivity.this, null);
builtinKeyboard.setAlpha(0.7f);
builtinKeyboard.ChangeKeyboard();
builtinKeyboard.setOnKeyboardActionListener(new CustomKeyboardView.OnKeyboardActionListener() {
public void onPress(int key) {
builtinKeyboard.resetEventAndTimestamps();
// Log.d(ScummVM.LOG_TAG, "SHOW KEYBOARD - 001 - onPress key: " + key );
if (key == KeyEvent.KEYCODE_BACK) {
return;
}
if (key <= 0) {
return;
}
for (CustomKeyboard.CustomKey k: builtinKeyboard.getKeyboard().getKeys()) {
if (k.sticky && key == k.codes[0]) {
return;
}
}
//
// downTime (long) - The time (in SystemClock.uptimeMillis()) at which this key code originally went down.
// ** Since this is a down event, this will be the same as getEventTime(). **
// Note that when chording keys, this value is the down time of the most recently pressed key, which may not be the same physical key of this event.
// eventTime (long) - The time (in SystemClock.uptimeMillis()) at which this event happened.
// TODO update repeat and event time? with
builtinKeyboard.mEventPressTime = SystemClock.uptimeMillis();
}
public void onRelease(int key) {
// Log.d(ScummVM.LOG_TAG, "SHOW KEYBOARD - 001 - onRelease key: " + key );
if (key == KeyEvent.KEYCODE_BACK) {
builtinKeyboard.setOnKeyboardActionListener(null);
builtinKeyboard.resetEventAndTimestamps();
showScreenKeyboardWithoutTextInputField(0); // Hide keyboard
return;
}
// CustomKeyboard.KEYCODE_SHIFT is a special button (negative value)
// which basically changes the keyboard to a another,"SHIFTED", layout (other keys)
// In this layout, if it's NOT also an "ALT" layout, keys are assumed to get the LEFT SHIFT modifier by default
if (key == CustomKeyboard.KEYCODE_SHIFT) {
builtinKeyboard.shift = !builtinKeyboard.shift;
builtinKeyboard.ChangeKeyboard();
builtinKeyboard.resetEventAndTimestamps();
return;
}
if (key == CustomKeyboard.KEYCODE_ALT) {
builtinKeyboard.alt = !builtinKeyboard.alt;
if (!builtinKeyboard.alt) {
builtinKeyboard.shift = false;
}
builtinKeyboard.ChangeKeyboard();
builtinKeyboard.resetEventAndTimestamps();
return;
}
if (key <= 0) {
builtinKeyboard.resetEventAndTimestamps();
return;
}
//
// TODO - Probably remove keys like caps lock, scroll lock, num lock, print etc...
//
for (CustomKeyboard.CustomKey k: builtinKeyboard.getKeyboard().getKeys()) {
if (k.sticky && key == k.codes[0]) {
if (builtinKeyboard.stickyKeys.contains(key)) {
// if it was "remembered" (in stickyKeys set) as ON
// (and it off by removing them from stickyKeys set)
builtinKeyboard.stickyKeys.remove(key);
} else {
// if it was "remembered" (in stickyKeys set) as OFF
// (turn it on by adding them to stickyKeys set)
builtinKeyboard.stickyKeys.add(key);
}
builtinKeyboard.recheckStickyKeys();
builtinKeyboard.resetEventAndTimestamps();
return;
}
}
int compiledMetaState = builtinKeyboard.getCompiledMetaState();
//boolean shifted = false;
if (key > 100000) {
key -= 100000;
//shifted = true;
compiledMetaState |= KeyEvent.META_SHIFT_LEFT_ON;
}
// The repeat argument (int) is:
// A repeat count for down events (> 0 if this is after the initial down)
// or event count for multiple events.
KeyEvent compiledKeyEvent = new KeyEvent(builtinKeyboard.mEventPressTime,
SystemClock.uptimeMillis(),
KeyEvent.ACTION_UP,
key,
builtinKeyboard.mKeyRepeatedCount,
compiledMetaState, 0, 0, KeyEvent.FLAG_SOFT_KEYBOARD);
_main_surface.dispatchKeyEvent(compiledKeyEvent);
builtinKeyboard.resetEventAndTimestamps();
// Excluding the CAPS LOCK NUM LOCK AND SCROLL LOCK keys,
// clear the state of all other sticky keys that are used in a key combo
// when we reach this part of the code
if (builtinKeyboard.stickyKeys.size() > 0) {
HashSet<Integer> stickiesToReleaseSet = new HashSet<>();
for (int tmpKeyCode : builtinKeyboard.stickyKeys) {
if (tmpKeyCode != KeyEvent.KEYCODE_CAPS_LOCK
&& tmpKeyCode != KeyEvent.KEYCODE_NUM_LOCK
&& tmpKeyCode != KeyEvent.KEYCODE_SCROLL_LOCK) {
stickiesToReleaseSet.add(tmpKeyCode);
}
}
if (stickiesToReleaseSet.size() > 0) {
builtinKeyboard.stickyKeys.removeAll(stickiesToReleaseSet);
builtinKeyboard.recheckStickyKeys();
}
}
}
public void onText(CharSequence p1) {}
// TODO - "Swipe" behavior does not seem to work currently. Should we support it?
public void swipeLeft() {
// Log.d(ScummVM.LOG_TAG, "SHOW KEYBOARD - 001 - swipeLeft");
}
public void swipeRight() {
// Log.d(ScummVM.LOG_TAG, "SHOW KEYBOARD - 001 - swipeRight" );
}
public void swipeDown() {
// Log.d(ScummVM.LOG_TAG, "SHOW KEYBOARD - 001 - swipeDown" );
}
public void swipeUp() {
// Log.d(ScummVM.LOG_TAG, "SHOW KEYBOARD - 001 - swipeUp ");
}
public void onKey(int key, int[] keysAround) {
// Log.d(ScummVM.LOG_TAG, "SHOW KEYBOARD - 001 - onKey key: " + key );
if (builtinKeyboard.mEventPressTime == -1) {
return;
}
if (builtinKeyboard.mKeyRepeatedCount < Integer.MAX_VALUE) {
++builtinKeyboard.mKeyRepeatedCount;
}
int compiledMetaState = builtinKeyboard.getCompiledMetaState();
// keys with keyCode greater than 100000, should be submitted with a LEFT_SHIFT_ modifier (and decreased by 100000 to get their proper code)
if (key > 100000) {
key -= 100000;
compiledMetaState |= KeyEvent.META_SHIFT_LEFT_ON;
}
// update the eventTime after the above check for first time "hit"
KeyEvent compiledKeyEvent = new KeyEvent(builtinKeyboard.mEventPressTime,
SystemClock.uptimeMillis(),
KeyEvent.ACTION_DOWN,
key,
builtinKeyboard.mKeyRepeatedCount,
compiledMetaState, 0, 0, KeyEvent.FLAG_SOFT_KEYBOARD);
_main_surface.dispatchKeyEvent(compiledKeyEvent);
}
});
_screenKeyboard = builtinKeyboard;
// TODO better to have specific dimensions in dp and not adjusted to parent
// it may resolve the issue of resizing the keyboard wrongly (smaller) when returning to the suspended Activity in low resolution
FrameLayout.LayoutParams sKeyboardLayout = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
_videoLayout.addView(_screenKeyboard, sKeyboardLayout);
_videoLayout.bringChildToFront(_screenKeyboard);
}
_scummvm.syncVirtkeyboardState(true);
}
});
} else {
keyboardWithoutTextInputShown = false;
runOnUiThread(new Runnable() {
public void run() {
if (_screenKeyboard != null ) {
_videoLayout.removeView(_screenKeyboard);
_screenKeyboard = null;
}
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
// TODO do we need this instead?
// _inputManager.hideSoftInputFromWindow(_main_surface.getWindowToken(), 0);
_inputManager.hideSoftInputFromWindow(_main_surface.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
DimSystemStatusBar.get().dim(_videoLayout);
//DimSystemStatusBar.get().dim(_main_surface);
//Log.d(ScummVM.LOG_TAG, "showScreenKeyboardWithoutTextInputField - captureMouse(true)");
_main_surface.captureMouse(true);
//_main_surface.showSystemMouseCursor(false);
_scummvm.syncVirtkeyboardState(false);
}
});
}
// _main_surface.nativeScreenKeyboardShown( keyboardWithoutTextInputShown ? 1 : 0 );
}
}
public void showScreenKeyboard() {
final boolean bGlobalsCompatibilityHacksTextInputEmulatesHwKeyboard = true;
final int dGlobalsTextInputKeyboard = 1;
if (isHWKeyboardConnected()) {
return;
}
if (_main_surface != null) {
if (bGlobalsCompatibilityHacksTextInputEmulatesHwKeyboard) {
showScreenKeyboardWithoutTextInputField(dGlobalsTextInputKeyboard);
//Log.d(ScummVM.LOG_TAG, "showScreenKeyboard - captureMouse(false)");
_main_surface.captureMouse(false);
//_main_surface.showSystemMouseCursor(true);
setupTouchModeBtn(_events.getTouchMode());
return;
}
//Log.d(ScummVM.LOG_TAG, "showScreenKeyboard: YOU SHOULD NOT SEE ME!!!");
// // TODO redundant ?
// if (_screenKeyboard != null) {
// return;
// }
//
}
}
public void hideScreenKeyboard() {
final int dGlobalsTextInputKeyboard = 1;
if (_main_surface != null) {
if (keyboardWithoutTextInputShown) {
showScreenKeyboardWithoutTextInputField(dGlobalsTextInputKeyboard);
//Log.d(ScummVM.LOG_TAG, "hideScreenKeyboard - captureMouse(true)");
_main_surface.captureMouse(true);
//_main_surface.showSystemMouseCursor(false);
setupTouchModeBtn(_events.getTouchMode());
}
}
}
public void toggleScreenKeyboard() {
if (isScreenKeyboardShown()) {
hideScreenKeyboard();
} else {
showScreenKeyboard();
}
}
public boolean isScreenKeyboardShown() {
return _screenKeyboard != null;
}
public View getScreenKeyboard() {
return _screenKeyboard;
}
// public View getMainSurfaceView() {
// return _main_surface;
// }
//
// END OF new screenKeyboardCode
// ---------------------------------------------------------------------------------------------------------------------------
//
protected void setupTouchModeBtn(final int touchMode) {
int resId;
if (isScreenKeyboardShown()) {
resId = R.drawable.ic_action_keyboard;
} else {
switch(touchMode) {
case ScummVMEventsBase.TOUCH_MODE_TOUCHPAD:
resId = R.drawable.ic_action_touchpad;
break;
case ScummVMEventsBase.TOUCH_MODE_MOUSE:
resId = R.drawable.ic_action_mouse;
break;
case ScummVMEventsBase.TOUCH_MODE_GAMEPAD:
resId = R.drawable.ic_action_gamepad;
break;
default:
throw new IllegalArgumentException("Invalid touchMode");
}
}
_toggleTouchModeKeyboardBtnIcon.setImageResource(resId);
}
public final View.OnClickListener touchModeKeyboardBtnOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
runOnUiThread(new Runnable() {
public void run() {
// On normal click, hide keyboard if it is shown
// Else, change touch mode
if (isScreenKeyboardShown()) {
hideScreenKeyboard();
} else {
int newTouchMode = _events.nextTouchMode();
setupTouchModeBtn(newTouchMode);
}
}
});
}
};
public final View.OnLongClickListener touchModeKeyboardBtnOnLongClickListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
runOnUiThread(new Runnable() {
public void run() {
// On long click, toggle screen keyboard (if there isn't any HW)
toggleScreenKeyboard();
}
});
return true;
}
};
public final View.OnClickListener menuBtnOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
runOnUiThread(new Runnable() {
public void run() {
_scummvm.pushEvent(ScummVMEventsBase.JE_MENU, 0, 0, 0, 0, 0, 0);
}
});
}
};
private class MyScummVM extends ScummVM {
public MyScummVM(SurfaceHolder holder, final MyScummVMDestroyedCallback destroyedCallback) {
super(ScummVMActivity.this.getAssets(), holder, destroyedCallback);
}
@Override
protected void getDPI(float[] values) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
values[0] = metrics.xdpi;
values[1] = metrics.ydpi;
}
@Override
protected void displayMessageOnOSD(final String msg) {
if (msg != null) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(ScummVMActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
protected void openUrl(String url) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
@Override
protected boolean hasTextInClipboard() {
final android.content.ClipData clip = _clipboardManager.getPrimaryClip();
return (clip != null
&& clip.getItemCount() > 0
&& clip.getItemAt(0).getText() != null);
}
@Override
protected String getTextFromClipboard() {
// based on: https://stackoverflow.com/q/37196571
final android.content.ClipData clip = _clipboardManager.getPrimaryClip();
if (clip == null
|| clip.getItemCount() == 0
|| clip.getItemCount() > 0 && clip.getItemAt(0).getText() == null) {
return null;
}
return clip.getItemAt(0).getText().toString();
}
@Override
protected boolean setTextInClipboard(String textStr) {
final android.content.ClipData clip = android.content.ClipData.newPlainText("ScummVM clip", textStr);
_clipboardManager.setPrimaryClip(clip);
return true;
}
@Override
protected boolean isConnectionLimited() {
// The WIFI Service must be looked up on the Application Context or memory will leak on devices < Android N (According to Android Studio warning)
WifiManager wifiMgr = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiMgr != null && wifiMgr.isWifiEnabled()) {
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
return (wifiInfo == null || wifiInfo.getNetworkId() == -1); //WiFi is on, but it's not connected to any network
}
return true;
}
@Override
protected void setWindowCaption(final String caption) {
runOnUiThread(new Runnable() {
public void run() {
setTitle(caption);
}
});
}
@Override
protected void showVirtualKeyboard(final boolean enable) {
runOnUiThread(new Runnable() {
public void run() {
//showKeyboard(enable);
if (enable) {
showScreenKeyboard();
} else {
hideScreenKeyboard();
}
}
});
}
@Override
protected void showKeyboardControl(final boolean enable) {
runOnUiThread(new Runnable() {
public void run() {
showToggleKeyboardBtnIcon(enable);
}
});
}
@Override
protected Bitmap getBitmapResource(int resource) {
int id;
switch(resource) {
case 0: // TOUCH_ARROWS_BITMAP
id = R.drawable.touch_arrows;
break;
default:
return null;
}
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;
return BitmapFactory.decodeResource(getResources(), id, opts);
}
@Override
protected void setTouchMode(final int touchMode) {
if (_events.getTouchMode() == touchMode) {
return;
}
runOnUiThread(new Runnable() {
public void run() {
_events.setTouchMode(touchMode);
setupTouchModeBtn(touchMode);
}
});
}
@Override
protected int getTouchMode() {
return _events.getTouchMode();
}
@Override
protected void setOrientation(final int orientation) {
runOnUiThread(new Runnable() {
public void run() {
setRequestedOrientation(orientation);
}
});
}
@Override
protected String getScummVMBasePath() {
return _actualScummVMDataDir.getPath();
}
@Override
protected String getScummVMConfigPath() {
return _configScummvmFile.getPath();
}
@Override
protected String getScummVMLogPath() {
if (_logScummvmFile != null) {
return _logScummvmFile.getPath();
} else return "";
}
@Override
protected String[] getSysArchives() {
Log.d(ScummVM.LOG_TAG, "Adding to Search Archive: " + _actualScummVMDataDir.getPath());
if (_externalPathAvailableForReadAccess && _possibleExternalScummVMDir != null) {
Log.d(ScummVM.LOG_TAG, "Adding to Search Archive: " + _possibleExternalScummVMDir.getPath());
return new String[]{_actualScummVMDataDir.getPath(), _possibleExternalScummVMDir.getPath()};
} else return new String[]{_actualScummVMDataDir.getPath()};
}
@Override
protected String[] getAllStorageLocations() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU
&& (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|| checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
) {
// In Tiramisu (API 33) and above, READ and WRITE external storage permissions have no effect,
// and they are automatically denied -- onRequestPermissionsResult() will be called without user's input
requestPermissions(MY_PERMISSIONS_STR_LIST, MY_PERMISSION_ALL);
} else {
return ExternalStorage.getAllStorageLocations(getApplicationContext()).toArray(new String[0]);
}
return new String[0]; // an array of zero length
}
@Override
protected String[] getAllStorageLocationsNoPermissionRequest() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M
|| checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
) {
return ExternalStorage.getAllStorageLocations(getApplicationContext()).toArray(new String[0]);
}
// TODO we might be able to return something even if READ_EXTERNAL_STORAGE was not granted
// but for now, just return nothing
return new String[0]; // an array of zero length
}
@Override
@RequiresApi(api = Build.VERSION_CODES.N)
protected SAFFSTree getNewSAFTree(boolean folder, boolean write, String initialURI, String prompt) {
Uri initialURI_ = Uri.parse(initialURI);
Uri uri = selectWithNativeUI(folder, write, initialURI_, prompt);
if (uri == null) {
return null;
}
return SAFFSTree.newTree(ScummVMActivity.this, uri);
}
@Override
@RequiresApi(api = Build.VERSION_CODES.N)
protected SAFFSTree[] getSAFTrees() {
return SAFFSTree.getTrees(ScummVMActivity.this);
}
@Override
@RequiresApi(api = Build.VERSION_CODES.N)
protected SAFFSTree findSAFTree(String name) {
return SAFFSTree.findTree(ScummVMActivity.this, name);
}
}
private MyScummVM _scummvm;
private ScummVMEventsBase _events;
private MouseHelper _mouseHelper;
private Thread _scummvm_thread;
@Override
public void onCreate(Bundle savedInstanceState) {
// Log.d(ScummVM.LOG_TAG, "onCreate");
super.onCreate(savedInstanceState);
setLogFile();
safSyncObject = new Object();
hideSystemUI();
_videoLayout = new FrameLayout(this);
SetLayerType.get().setLayerType(_videoLayout);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(_videoLayout);
_videoLayout.setFocusable(true);
_videoLayout.setFocusableInTouchMode(true);
_videoLayout.requestFocus();
_main_surface = new EditableSurfaceView(this);
SetLayerType.get().setLayerType(_main_surface);
_videoLayout.addView(_main_surface, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
LinearLayout buttonLayout = new LinearLayout(this);
buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
FrameLayout.LayoutParams buttonLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.RIGHT);
buttonLayoutParams.topMargin = 5;
buttonLayoutParams.rightMargin = 5;
_videoLayout.addView(buttonLayout, buttonLayoutParams);
_videoLayout.bringChildToFront(buttonLayout);
_toggleTouchModeKeyboardBtnIcon = new ImageView(this);
buttonLayout.addView(_toggleTouchModeKeyboardBtnIcon, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
buttonLayout.bringChildToFront(_toggleTouchModeKeyboardBtnIcon);
_openMenuBtnIcon = new ImageView(this);
_openMenuBtnIcon.setImageResource(R.drawable.ic_action_menu);
buttonLayout.addView(_openMenuBtnIcon, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
buttonLayout.bringChildToFront(_openMenuBtnIcon);
_main_surface.setFocusable(true);
_main_surface.setFocusableInTouchMode(true);
_main_surface.requestFocus();
//Log.d(ScummVM.LOG_TAG, "onCreate - captureMouse(true)");
//_main_surface.captureMouse(true, true);
//_main_surface.showSystemMouseCursor(false);
// TODO is this redundant since we call hideSystemUI() ?
DimSystemStatusBar.get().dim(_videoLayout);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
// TODO needed?
takeKeyEvents(true);
_clipboardManager = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
// REMOVED: Since getFilesDir() is guaranteed to exist, getFilesDir().mkdirs() might be related to crashes in Android version 9+ (Pie or above, API 28+)!
// REMOVED: Setting savePath to Environment.getExternalStorageDirectory() + "/ScummVM/Saves/"
// so that it will be in persistent external storage and not deleted on uninstall
// This has the issue for external storage being unavailable on some devices
// Is this persistence really important considering that Android does not really support it anymore
// Exceptions (storage locations that are persisted or methods to access files across apps) are:
// - shareable media files (images, audio, video)
// - files stored with Storage Access Framework (SAF) which requires user interaction with FilePicker)
// Original fallback was getDir()
// so app's internal space (which would be deleted on uninstall) was set as WORLD_READABLE which is no longer supported in newer versions of Android API
// In newer APIs we can set that path as Context.MODE_PRIVATE which is the default - but this makes the files inaccessible to other apps
SurfaceHolder main_surface_holder = _main_surface.getHolder();
// By default Android selects RGB_565 for backward compatibility, use the best one by querying the display
// It's deprecated on API level >= 17 and will always return RGBA_8888
// but on older versions it could return RGB_565 which could be more efficient for the GPU
main_surface_holder.setFormat(getDisplayPixelFormat());
_scummvm = new MyScummVM(main_surface_holder, new MyScummVMDestroyedCallback() {
@Override
public void handle(int exitResult) {
Log.d(ScummVM.LOG_TAG, "Via callback: ScummVM native terminated with code: " + exitResult);
// call onDestroy() only we we aren't already in it
if (!_finishing) finish();
}
});
float[] dpiValues = new float[] { 0.0f, 0.0f };
_scummvm.getDPI(dpiValues);
Log.d(ScummVM.LOG_TAG, "Current xdpi: " + dpiValues[0] + " and ydpi: " + dpiValues[1]);
// Currently in release builds version string does not contain the revision info
// but in debug builds (daily builds) this should be there (see base/internal_version_h)
_currentScummVMVersion = new Version(_scummvm.getInstallingScummVMVersionInfo());
Log.d(ScummVM.LOG_TAG, "Current ScummVM version launching is: " + _currentScummVMVersion.getDescription() + " (" + _currentScummVMVersion.get() + ")");
//
// seekAndInitScummvmConfiguration() returns false if something went wrong
// when initializing configuration (or when seeking and trying to use an existing ini file) for ScummVM
if (!seekAndInitScummvmConfiguration()) {
Log.e(ScummVM.LOG_TAG, "Error while trying to find and/or initialize ScummVM configuration file!");
// in fact in all the cases where we return false, we also called finish()
return;
}
// We should have a valid path to a configuration file here
// Start ScummVM
_scummvm.setArgs(new String[]{
"ScummVM"
});
Log.d(ScummVM.LOG_TAG, "Hover available: " + _hoverAvailable);