-
Notifications
You must be signed in to change notification settings - Fork 0
/
android.pde
61 lines (55 loc) · 2.32 KB
/
android.pde
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
/*
* android.pde
*
* All android-specific utility methods.
* To run in Java mode, simply comment out
* this whole file and anywhere that calls
* these methods.
*
* Created on: January 12, 2020
* Author: Sean LaPlante
*/
int SCREEN_ORIENTATION_PORTRAIT = 1;
int FLAG_KEEP_SCREEN_ON = 128;
void androidSetup() {
/*
* Utility method used to set the window flag to keep the screen on while
* in our app and to set the game orientation to be fixed in PORTRAIT mode.
*
* The 'keep screen on' bit must be done this way to avoid CalledFromWrongThreadException
* since this flag must be added in the UI thread.
*
* This method uses Class.forName(), getMethod(), etc. so that we can seamlessly
* switch Processing between Android and Java mode without commenting out code.
*/
Class activity_cls = null;
Object activity_inst = null;
Method setRequestedOrientationMethod = null;
Method runOnUiThreadMethod = null;
try {
activity_cls = Class.forName("android.app.Activity");
runOnUiThreadMethod = activity_cls.getMethod("runOnUiThread", Runnable.class);
setRequestedOrientationMethod = activity_cls.getMethod("setRequestedOrientation", int.class);
// Fix orientation to PORTRAIT only
activity_inst = invokeInternalProcessingMethod("getActivity");
setRequestedOrientationMethod.invoke(activity_inst, SCREEN_ORIENTATION_PORTRAIT);
// Keep the screen on while in the game
runOnUiThreadMethod.invoke(activity_inst, new Runnable() {
@Override
public void run() {
try {
Class window_cls = Class.forName("android.view.Window");
Object window_inst = invokeInternalProcessingMethod("getWindow");
Method addFlagsMethod = window_cls.getMethod("addFlags", int.class);
addFlagsMethod.invoke(window_inst, FLAG_KEEP_SCREEN_ON);
} catch (Exception e) {
println("androidSetup failed to set FLAG_KEEP_SCREEN_ON: ", e);
e.printStackTrace();
}
}
});
} catch (Exception e) {
println("androidSetup failed: ", e);
e.printStackTrace();
}
}