Debian Codegraph @

tools/build-tool/data/android/integrated/app/src/main/java/io/castleengine/MainActivity.java

raw symbols SCIP
1 /* -*- tab-width: 4 -*- */
2 package io.castleengine;
3
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Locale;
7
8 import android.Manifest;
9 import android.annotation.SuppressLint;
10 import android.app.NativeActivity;
11 import android.app.Activity;
12 import android.content.Intent;
13 import android.os.Bundle;
14 import android.util.Log;
15 import android.content.pm.PackageManager;
16
17 import androidx.core.app.ActivityCompat;
18
19 public class MainActivity extends NativeActivity
20 {
21 private static final String CATEGORY = "MainActivity";
22
23 private ServiceMessaging messaging;
24 private final List<ServiceAbstract> services = new ArrayList<>();
25
26 @Override
27 protected void onCreate(Bundle savedInstanceState) {
28 super.onCreate(savedInstanceState);
29 ServiceAbstract.logInfo(CATEGORY, "Custom castleengine.MainActivity created");
30
31 services.add(messaging = new ServiceMessaging(this));
32 services.add(new ServiceMiscellaneous(this));
33
34 /* ANDROID-SERVICES-INITIALIZATION */
35
36 for (ServiceAbstract service : services) {
37 service.onCreate();
38 }
39
40 jniLanguage(Locale.getDefault().toString());
41 }
42
43 @Override
44 public void onDestroy() {
45 for (ServiceAbstract service : services) {
46 service.onDestroy();
47 }
48 super.onDestroy();
49 }
50
51 @SuppressLint("NewApi") @Override
52 public void onWindowFocusChanged(boolean hasFocus) {
53 super.onWindowFocusChanged(hasFocus);
54 for (ServiceAbstract service : services) {
55 service.onWindowFocusChanged(hasFocus);
56 }
57 }
58
59 @Override
60 protected void onStart() {
61 super.onStart();
62 ServiceAbstract.logInfo(CATEGORY, "onStart");
63 for (ServiceAbstract service : services) {
64 service.onStart();
65 }
66 }
67
68 @Override
69 protected void onStop() {
70 super.onStop();
71 ServiceAbstract.logInfo(CATEGORY, "onStop");
72 for (ServiceAbstract service : services) {
73 service.onStop();
74 }
75 }
76
77 @Override
78 protected void onActivityResult(int requestCode, int resultCode, Intent intent)
79 {
80 super.onActivityResult(requestCode, resultCode, intent);
81 for (ServiceAbstract service : services) {
82 service.onActivityResult(requestCode, resultCode, intent);
83 }
84 }
85
86 @Override
87 protected void onResume() {
88 super.onResume();
89 ServiceAbstract.logInfo(CATEGORY, "onResume");
90 for (ServiceAbstract service : services) {
91 service.onResume();
92 }
93 }
94
95 @Override
96 protected void onPause() {
97 super.onPause();
98 ServiceAbstract.logInfo(CATEGORY, "onPause");
99 for (ServiceAbstract service : services) {
100 service.onPause();
101 }
102 }
103
104 @Override
105 protected void onNewIntent(Intent intent) {
106 super.onNewIntent(intent);
107 ServiceAbstract.logInfo(CATEGORY, "onNewIntent");
108 for (ServiceAbstract service : services) {
109 service.onNewIntent(intent);
110 }
111 }
112
113 /**
114 * Send a message to native code.
115 * Services (except ServiceMessaging) may call this
116 * to send message to native code.
117 */
118 public void messageSend(String[] message, byte[] messageStream)
119 {
120 messaging.sendFromMainActivity(message, messageStream);
121 }
122
123 public void messageSend(String[] message)
124 {
125 messageSend(message, null);
126 }
127
128 /**
129 * Process a message from the native code.
130 * Called only by ServiceMessaging.
131 */
132 public boolean messageReceived(String[] parts)
133 {
134 boolean result = false;
135 boolean r;
136
137 // Check messages handled directly in this class
138 if (parts.length == 2 && parts[0].equals("permission-request")) {
139 requestPermission(parts[1]);
140 result = true;
141 }
142
143 // Call messageReceived of all services.
144 //
145 // Do not stop on 1st success, our analytics services depend on it.
146 // We pass all analytics-xxx messages to all analytics
147 // services (google analytics and game analytics).
148 // Only the ones initialized (by xxx-initialize) will actually
149 // do something with it.
150
151 for (ServiceAbstract service : services) {
152 r = service.messageReceived(parts);
153 result = result || r;
154 }
155
156 return result;
157 }
158
159 public void onPurchase(AvailableProduct product, String originalJson, String signature)
160 {
161 ServiceAbstract.logInfo(CATEGORY, "purchase " + product.id);
162 for (ServiceAbstract service : services) {
163 service.onPurchase(product, originalJson, signature);
164 }
165 }
166
167 /* JNI ------------------------------------------------------------------- */
168
169 public native String jniMessage(String messageToPascal, byte[] messageToPascalStream);
170 public native void jniLanguage(String javaToNative);
171
172 public static void safeLoadLibrary(String libName)
173 {
174 try {
175 System.loadLibrary(libName);
176 ServiceAbstract.logInfo(CATEGORY, "JNI: Successfully loaded lib" + libName + ".so");
177 } catch(UnsatisfiedLinkError e) {
178 ServiceAbstract.logError(CATEGORY, "JNI: Could not load lib" + libName + ".so, exception UnsatisfiedLinkError: " + e.getMessage());
179 }
180 }
181
182 static {
183 /* OpenAL may be loaded from here. It must be loaded before
184 our game's native code will try to open openal library,
185 so it's most reliable to place it here before loading game's library. */
186
187 ${ANDROID_ACTIVITY_LOAD_LIBRARIES}
188
189 safeLoadLibrary("${ANDROID_LIBRARY_NAME}");
190 }
191
192 private static final int MY_PERMISSIONS_REQUEST = 1;
193
194 /* Requires Android permission name.
195
196 @param permission Can be one of the Manifest.permission constants,
197 like Manifest.permission.WRITE_EXTERNAL_STORAGE,
198 see https://developer.android.com/reference/android/Manifest.permission.html .
199 You can also just type a string like "android.permission.WRITE_EXTERNAL_STORAGE"
200 (this is useful when permission is not specified in Java code,
201 so it's easier to just provide a string value of it).
202
203 See
204 https://developer.android.com/training/permissions/requesting#java
205 https://developer.android.com/training/permissions/usage-notes
206
207 In response, will always (but maybe asynchronously) answer with
208 "permission-granted" or "permission-cancelled".
209 */
210 public void requestPermission(String permission)
211 {
212 if (ActivityCompat.checkSelfPermission(this, permission)
213 != PackageManager.PERMISSION_GRANTED) {
214 ActivityCompat.requestPermissions(this,
215 new String[]{permission},
216 MY_PERMISSIONS_REQUEST);
217 } else {
218 messageSend(new String[]{"permission-granted", permission});
219 }
220 }
221
222 @Override
223 public void onRequestPermissionsResult(int requestCode,
224 String[] permissions, int[] grantResults)
225 {
226 switch (requestCode) {
227 case MY_PERMISSIONS_REQUEST: {
228 // If request is cancelled, the result arrays are empty.
229 if (grantResults.length > 0
230 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
231 messageSend(new String[]{"permission-granted", permissions[0]});
232 } else {
233 messageSend(new String[]{"permission-cancelled", permissions[0]});
234 }
235 return;
236 }
237 }
238 }
239 }