| 1 |
|
| 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 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 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 |
|
| 130 |
|
| 131 |
|
| 132 |
public boolean messageReceived(String[] parts) |
| 133 |
{ |
| 134 |
boolean result = false; |
| 135 |
boolean r; |
| 136 |
|
| 137 |
|
| 138 |
if (parts.length == 2 && parts[0].equals("permission-request")) { |
| 139 |
requestPermission(parts[1]); |
| 140 |
result = true; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 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 |
|
| 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 |
|
| 184 |
|
| 185 |
|
| 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 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 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 |
|
| 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 |
} |