-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathRetroActivityFuture.java
More file actions
352 lines (303 loc) · 12.1 KB
/
RetroActivityFuture.java
File metadata and controls
352 lines (303 loc) · 12.1 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
package com.retroarch.browser.retroactivity;
import android.util.Log;
import android.view.PointerIcon;
import android.view.View;
import android.view.WindowManager;
import android.content.Intent;
import android.content.Context;
import android.hardware.input.InputManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.retroarch.browser.preferences.util.ConfigFile;
import com.retroarch.browser.preferences.util.UserPreferences;
import com.retroarch.browser.mainmenu.MainMenuActivity;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import android.content.pm.PackageManager;
import android.Manifest;
public final class RetroActivityFuture extends RetroActivityCamera {
// Tracks activity lifecycle state for MainMenuActivity resume detection
public static volatile boolean isRunning = false;
// If set to true then RetroArch will completely exit when it loses focus
private boolean quitfocus = false;
// Top-level window decor view
private View mDecorView;
// Constants used for Handler messages
private static final int HANDLER_WHAT_TOGGLE_IMMERSIVE = 1;
private static final int HANDLER_WHAT_TOGGLE_POINTER_CAPTURE = 2;
private static final int HANDLER_WHAT_TOGGLE_POINTER_NVIDIA = 3;
private static final int HANDLER_WHAT_TOGGLE_POINTER_ICON = 4;
private static final int HANDLER_ARG_TRUE = 1;
private static final int HANDLER_ARG_FALSE = 0;
private static final int HANDLER_MESSAGE_DELAY_DEFAULT_MS = 300;
// Handler used for UI events
private final Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
boolean state = (msg.arg1 == HANDLER_ARG_TRUE) ? true : false;
if (msg.what == HANDLER_WHAT_TOGGLE_IMMERSIVE) {
attemptToggleImmersiveMode(state);
} else if (msg.what == HANDLER_WHAT_TOGGLE_POINTER_CAPTURE) {
attemptTogglePointerCapture(state);
} else if (msg.what == HANDLER_WHAT_TOGGLE_POINTER_NVIDIA) {
attemptToggleNvidiaCursorVisibility(state);
} else if (msg.what == HANDLER_WHAT_TOGGLE_POINTER_ICON) {
attemptTogglePointerIcon(state);
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isRunning = true;
mDecorView = getWindow().getDecorView();
// Check if we need permissions before proceeding
if (!checkPermissions()) {
// Redirect to MainMenuActivity to get permissions
Intent mainMenuIntent = new Intent(this, MainMenuActivity.class);
mainMenuIntent.putExtras(getIntent());
startActivity(mainMenuIntent);
finish();
return;
}
// Handle core sideloading if external core path provided
handleCoreSideloading();
// If QUITFOCUS parameter is provided then enable that Retroarch quits when focus is lost
quitfocus = getIntent().hasExtra("QUITFOCUS");
}
private boolean checkPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
List<String> permissionsNeeded = new ArrayList<String>();
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
return permissionsNeeded.isEmpty();
}
return true;
}
private void handleCoreSideloading() {
String corePath = getIntent().getStringExtra("LIBRETRO");
if (corePath == null || corePath.isEmpty()) {
return;
}
File coreFile = new File(corePath);
if (!coreFile.exists()) {
Log.w("RetroActivityFuture", "Core file does not exist: " + corePath);
return;
}
// Get the cores directory
String coresDir = getApplicationInfo().dataDir + "/cores/";
File coresDirFile = new File(coresDir);
// Create cores directory if it doesn't exist
if (!coresDirFile.exists()) {
if (!coresDirFile.mkdirs()) {
Log.e("RetroActivityFuture", "Failed to create cores directory: " + coresDir);
return;
}
}
// Check if core is outside RetroArch cores folder - if so, sideload it
if (!corePath.startsWith(coresDir)) {
String coreFileName = coreFile.getName();
File destinationFile = new File(coresDir, coreFileName);
try {
copyFile(coreFile, destinationFile);
Log.i("RetroActivityFuture", "Sideloaded core from " + corePath + " to " + destinationFile.getAbsolutePath());
// Update intent to point to the new location
getIntent().putExtra("LIBRETRO", destinationFile.getAbsolutePath());
} catch (IOException e) {
Log.e("RetroActivityFuture", "Failed to sideload core: " + e.getMessage());
}
}
}
private void copyFile(File source, File destination) throws IOException {
FileInputStream inStream = null;
FileOutputStream outStream = null;
try {
inStream = new FileInputStream(source);
outStream = new FileOutputStream(destination);
byte[] buffer = new byte[8192];
int length;
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
// Ignore
}
}
if (outStream != null) {
try {
outStream.close();
} catch (IOException e) {
// Ignore
}
}
}
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Extract game parameters from new intent
String newRom = intent.getStringExtra("ROM");
String newCore = intent.getStringExtra("LIBRETRO");
// Get current intent parameters for comparison
Intent currentIntent = getIntent();
String currentRom = currentIntent != null ? currentIntent.getStringExtra("ROM") : null;
String currentCore = currentIntent != null ? currentIntent.getStringExtra("LIBRETRO") : null;
// Check if we're trying to launch different content
if ((newRom != null && !newRom.equals(currentRom)) ||
(newCore != null && !newCore.equals(currentCore))) {
// Different game content - exit cleanly and let launcher restart us
finish();
System.exit(0);
} else {
// Same content, just update intent
setIntent(intent);
}
}
@Override
public void onResume() {
super.onResume();
setSustainedPerformanceMode(sustainedPerformanceMode);
// Check for Android UI specific parameters
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
String refresh = getIntent().getStringExtra("REFRESH");
// If REFRESH parameter is provided then try to set refreshrate accordingly
if (refresh != null) {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.preferredRefreshRate = Integer.parseInt(refresh);
getWindow().setAttributes(params);
}
}
// Checks if Android versions is above 9.0 (28) and enable the screen to write over notch if the user desires
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ConfigFile configFile = new ConfigFile(UserPreferences.getDefaultConfigPath(this));
try {
if (configFile.getBoolean("video_notch_write_over_enable")) {
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
} catch (Exception e) {
Log.w("Key doesn't exist yet.", e.getMessage());
}
}
}
@Override
public void onStop() {
super.onStop();
// If QUITFOCUS parameter was set then completely exit RetroArch when focus is lost
if (quitfocus) System.exit(0);
}
@Override
public void onDestroy() {
super.onDestroy();
isRunning = false;
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mHandlerSendUiMessage(HANDLER_WHAT_TOGGLE_IMMERSIVE, hasFocus);
try {
ConfigFile configFile = new ConfigFile(UserPreferences.getDefaultConfigPath(this));
if (configFile.getBoolean("input_auto_mouse_grab")) {
inputGrabMouse(hasFocus);
}
} catch (Exception e) {
Log.w("[onWindowFocusChanged] exception thrown:", e.getMessage());
}
}
private void mHandlerSendUiMessage(int what, boolean state) {
int arg1 = (state ? HANDLER_ARG_TRUE : HANDLER_ARG_FALSE);
int arg2 = -1;
Message message = mHandler.obtainMessage(what, arg1, arg2);
mHandler.sendMessageDelayed(message, HANDLER_MESSAGE_DELAY_DEFAULT_MS);
}
public void inputGrabMouse(boolean state) {
mHandlerSendUiMessage(HANDLER_WHAT_TOGGLE_POINTER_CAPTURE, state);
mHandlerSendUiMessage(HANDLER_WHAT_TOGGLE_POINTER_NVIDIA, state);
mHandlerSendUiMessage(HANDLER_WHAT_TOGGLE_POINTER_ICON, state);
}
private void attemptToggleImmersiveMode(boolean state) {
// Attempt to toggle "Immersive Mode" for Android 4.4 (19) and up
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
if (state) {
mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_IMMERSIVE);
} else {
mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
} catch (Exception e) {
Log.w("[attemptToggleImmersiveMode] exception thrown:", e.getMessage());
}
}
}
private void attemptTogglePointerCapture(boolean state) {
// Attempt requestPointerCapture for Android 8.0 (26) and up
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {
if (state) {
mDecorView.requestPointerCapture();
} else {
mDecorView.releasePointerCapture();
}
} catch (Exception e) {
Log.w("[attemptTogglePointerCapture] exception thrown:", e.getMessage());
}
}
}
private void attemptToggleNvidiaCursorVisibility(boolean state) {
// Attempt setCursorVisibility for Android 4.1 (16) and up
// only works if NVIDIA Android extensions for NVIDIA Shield are available
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
try {
boolean cursorVisibility = !state;
Method mInputManager_setCursorVisibility = InputManager.class.getMethod("setCursorVisibility", boolean.class);
InputManager inputManager = (InputManager) getSystemService(Context.INPUT_SERVICE);
mInputManager_setCursorVisibility.invoke(inputManager, cursorVisibility);
} catch (NoSuchMethodException e) {
// Extensions were not available so do nothing
} catch (Exception e) {
Log.w("[attemptToggleNvidiaCursorVisibility] exception thrown:", e.getMessage());
}
}
}
private void attemptTogglePointerIcon(boolean state) {
// Attempt setPointerIcon for Android 7.x (24, 25) only
// For Android 8.0+, requestPointerCapture is used
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
try {
if (state) {
PointerIcon nullPointerIcon = PointerIcon.getSystemIcon(this, PointerIcon.TYPE_NULL);
mDecorView.setPointerIcon(nullPointerIcon);
} else {
// Restore the pointer icon to it's default value
mDecorView.setPointerIcon(null);
}
} catch (Exception e) {
Log.w("[attemptTogglePointerIcon] exception thrown:", e.getMessage());
}
}
}
}