GameActivity.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. package org.love2d.android;
  2. import org.libsdl.app.SDLActivity;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.io.BufferedOutputStream;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import android.Manifest;
  11. import android.app.AlertDialog;
  12. import android.content.Context;
  13. import android.content.DialogInterface;
  14. import android.content.Intent;
  15. import android.media.AudioManager;
  16. import android.net.Uri;
  17. import android.os.Bundle;
  18. import android.os.Environment;
  19. import android.os.Vibrator;
  20. import android.util.Log;
  21. import android.util.DisplayMetrics;
  22. import android.view.*;
  23. import android.content.pm.PackageManager;
  24. import androidx.annotation.Keep;
  25. import androidx.core.app.ActivityCompat;
  26. public class GameActivity extends SDLActivity {
  27. private static DisplayMetrics metrics = new DisplayMetrics();
  28. private static String gamePath = "";
  29. private static Context context;
  30. private static Vibrator vibrator = null;
  31. protected final int[] externalStorageRequestDummy = new int[1];
  32. protected final int[] recordAudioRequestDummy = new int[1];
  33. public static final int EXTERNAL_STORAGE_REQUEST_CODE = 1;
  34. public static final int RECORD_AUDIO_REQUEST_CODE = 2;
  35. private static boolean immersiveActive = false;
  36. private static boolean mustCacheArchive = false;
  37. private boolean storagePermissionUnnecessary = false;
  38. public boolean embed = false;
  39. public int safeAreaTop = 0;
  40. public int safeAreaLeft = 0;
  41. public int safeAreaBottom = 0;
  42. public int safeAreaRight = 0;
  43. @Override
  44. protected String[] getLibraries() {
  45. return new String[]{
  46. "c++_shared",
  47. "mpg123",
  48. "openal",
  49. "hidapi",
  50. "love",
  51. };
  52. }
  53. @Override
  54. protected String getMainSharedObject() {
  55. String[] libs = getLibraries();
  56. String libname = "lib" + libs[libs.length - 1] + ".so";
  57. // Since Lollipop, you can simply pass "libname.so" to dlopen
  58. // and it will resolve correct paths and load correct library.
  59. // This is mandatory for extractNativeLibs=false support in
  60. // Marshmallow.
  61. if (android.os.Build.VERSION.SDK_INT >= 21) {
  62. return libname;
  63. } else {
  64. return getContext().getApplicationInfo().nativeLibraryDir + "/" + libname;
  65. }
  66. }
  67. @Override
  68. protected void onCreate(Bundle savedInstanceState) {
  69. Log.d("GameActivity", "started");
  70. context = this.getApplicationContext();
  71. int res = context.checkCallingOrSelfPermission(Manifest.permission.VIBRATE);
  72. if (res == PackageManager.PERMISSION_GRANTED) {
  73. vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
  74. } else {
  75. Log.d("GameActivity", "Vibration disabled: could not get vibration permission.");
  76. }
  77. // These 2 variables must be reset or it will use the existing value.
  78. gamePath = "";
  79. storagePermissionUnnecessary = false;
  80. embed = context.getResources().getBoolean(R.bool.embed);
  81. if (!embed) {
  82. handleIntent(this.getIntent());
  83. }
  84. super.onCreate(savedInstanceState);
  85. getWindowManager().getDefaultDisplay().getMetrics(metrics);
  86. if (android.os.Build.VERSION.SDK_INT >= 28) {
  87. getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
  88. }
  89. }
  90. @Override
  91. protected void onNewIntent(Intent intent) {
  92. Log.d("GameActivity", "onNewIntent() with " + intent);
  93. if (!embed) {
  94. handleIntent(intent);
  95. resetNative();
  96. startNative();
  97. }
  98. }
  99. protected void handleIntent(Intent intent) {
  100. Uri game = intent.getData();
  101. if (game != null) {
  102. String scheme = game.getScheme();
  103. String path = game.getPath();
  104. // If we have a game via the intent data we we try to figure out how we have to load it. We
  105. // support the following variations:
  106. // * a main.lua file: set gamePath to the directory containing main.lua
  107. // * otherwise: set gamePath to the file
  108. if (scheme.equals("file")) {
  109. Log.d("GameActivity", "Received file:// intent with path: " + path);
  110. // If we were given the path of a main.lua then use its
  111. // directory. Otherwise use full path.
  112. List<String> path_segments = game.getPathSegments();
  113. if (path_segments.get(path_segments.size() - 1).equals("main.lua")) {
  114. gamePath = path.substring(0, path.length() - "main.lua".length());
  115. } else {
  116. gamePath = path;
  117. }
  118. } else if (scheme.equals("content")) {
  119. Log.d("GameActivity", "Received content:// intent with path: " + path);
  120. try {
  121. String filename = "game.love";
  122. String[] pathSegments = path.split("/");
  123. if (pathSegments != null && pathSegments.length > 0) {
  124. filename = pathSegments[pathSegments.length - 1];
  125. }
  126. String destination_file = this.getCacheDir().getPath() + "/" + filename;
  127. InputStream data = getContentResolver().openInputStream(game);
  128. // copyAssetFile automatically closes the InputStream
  129. if (copyAssetFile(data, destination_file)) {
  130. gamePath = destination_file;
  131. storagePermissionUnnecessary = true;
  132. }
  133. } catch (Exception e) {
  134. Log.d("GameActivity", "could not read content uri " + game.toString() + ": " + e.getMessage());
  135. }
  136. } else {
  137. Log.e("GameActivity", "Unsupported scheme: '" + game.getScheme() + "'.");
  138. AlertDialog.Builder alert_dialog = new AlertDialog.Builder(this);
  139. alert_dialog.setMessage("Could not load LÖVE game '" + path
  140. + "' as it uses unsupported scheme '" + game.getScheme()
  141. + "'. Please contact the developer.");
  142. alert_dialog.setTitle("LÖVE for Android Error");
  143. alert_dialog.setPositiveButton("Exit",
  144. new DialogInterface.OnClickListener() {
  145. @Override
  146. public void onClick(DialogInterface dialog, int id) {
  147. finish();
  148. }
  149. });
  150. alert_dialog.setCancelable(false);
  151. alert_dialog.create().show();
  152. }
  153. } else {
  154. // No game specified via the intent data -> check whether we have a game.love in our assets.
  155. boolean game_love_in_assets = false;
  156. try {
  157. List<String> assets = Arrays.asList(getAssets().list(""));
  158. game_love_in_assets = assets.contains("game.love");
  159. } catch (Exception e) {
  160. Log.d("GameActivity", "could not list application assets:" + e.getMessage());
  161. }
  162. if (game_love_in_assets) {
  163. // If we have a game.love in our assets folder copy it to the cache folder
  164. // so that we can load it from native LÖVE code
  165. String destination_file = this.getCacheDir().getPath() + "/game.love";
  166. try {
  167. InputStream gameStream = getAssets().open("game.love");
  168. if (mustCacheArchive && copyAssetFile(gameStream, destination_file))
  169. gamePath = destination_file;
  170. else
  171. gamePath = "game.love";
  172. storagePermissionUnnecessary = true;
  173. } catch (IOException e) {
  174. Log.d("GameActivity", "Could not open game.love from assets: " + e.getMessage());
  175. gamePath = "";
  176. storagePermissionUnnecessary = false;
  177. }
  178. } else {
  179. gamePath = "";
  180. storagePermissionUnnecessary = false;
  181. }
  182. }
  183. Log.d("GameActivity", "new gamePath: " + gamePath);
  184. }
  185. protected void checkLovegameFolder() {
  186. // If no game.love was found fall back to the game in <external storage>/lovegame
  187. Log.d("GameActivity", "fallback to lovegame folder");
  188. if (hasExternalStoragePermission()) {
  189. File ext = Environment.getExternalStorageDirectory();
  190. if ((new File(ext, "/lovegame/main.lua")).exists()) {
  191. gamePath = ext.getPath() + "/lovegame/";
  192. }
  193. } else {
  194. Log.d("GameActivity", "Cannot load game from /sdcard/lovegame: permission not granted");
  195. }
  196. }
  197. @Override
  198. protected void onDestroy() {
  199. if (vibrator != null) {
  200. Log.d("GameActivity", "Cancelling vibration");
  201. vibrator.cancel();
  202. }
  203. super.onDestroy();
  204. }
  205. @Override
  206. protected void onPause() {
  207. if (vibrator != null) {
  208. Log.d("GameActivity", "Cancelling vibration");
  209. vibrator.cancel();
  210. }
  211. super.onPause();
  212. }
  213. @Override
  214. public void onResume() {
  215. super.onResume();
  216. if (immersiveActive) {
  217. int flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
  218. if (android.os.Build.VERSION.SDK_INT >= 16) {
  219. flags |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
  220. View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
  221. View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
  222. View.SYSTEM_UI_FLAG_FULLSCREEN;
  223. }
  224. if (android.os.Build.VERSION.SDK_INT >= 19) {
  225. flags |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
  226. }
  227. getWindow().getDecorView().setSystemUiVisibility(flags);
  228. }
  229. }
  230. public void setImmersiveMode(boolean immersive_mode) {
  231. if (android.os.Build.VERSION.SDK_INT < 11) {
  232. // The API getWindow().getDecorView().setSystemUiVisibility() was
  233. // added in Android 11 (a.k.a. Honeycomb, a.k.a. 3.0.x). If we run
  234. // on this we do nothing.
  235. return;
  236. }
  237. immersiveActive = immersive_mode;
  238. final Object lock = new Object();
  239. final boolean immersive_enabled = immersive_mode;
  240. synchronized (lock) {
  241. runOnUiThread(new Runnable() {
  242. @Override
  243. public void run() {
  244. synchronized (lock) {
  245. if (immersive_enabled) {
  246. int flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
  247. if (android.os.Build.VERSION.SDK_INT >= 16) {
  248. flags |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
  249. View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
  250. View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
  251. View.SYSTEM_UI_FLAG_FULLSCREEN;
  252. }
  253. if (android.os.Build.VERSION.SDK_INT >= 19) {
  254. flags |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
  255. }
  256. getWindow().getDecorView().setSystemUiVisibility(flags);
  257. } else if (android.os.Build.VERSION.SDK_INT >= 16) {
  258. getWindow().getDecorView().setSystemUiVisibility(
  259. View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
  260. View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  261. );
  262. }
  263. lock.notify();
  264. }
  265. }
  266. });
  267. }
  268. ;
  269. }
  270. public boolean getImmersiveMode() {
  271. return immersiveActive;
  272. }
  273. public static String getGamePath() {
  274. GameActivity self = (GameActivity) mSingleton; // use SDL provided one
  275. Log.d("GameActivity", "called getGamePath(), game path = " + gamePath);
  276. if (gamePath.length() > 0) {
  277. if(self.storagePermissionUnnecessary || self.hasExternalStoragePermission()) {
  278. return gamePath;
  279. } else {
  280. Log.d("GameActivity", "cannot open game " + gamePath + ": no external storage permission given!");
  281. }
  282. } else {
  283. self.checkLovegameFolder();
  284. if (gamePath.length() > 0)
  285. return gamePath;
  286. }
  287. return "";
  288. }
  289. public static DisplayMetrics getMetrics() {
  290. return metrics;
  291. }
  292. public static void vibrate(double seconds) {
  293. if (vibrator != null) {
  294. vibrator.vibrate((long) (seconds * 1000.));
  295. }
  296. }
  297. public static boolean openURL(String url) {
  298. Log.d("GameActivity", "opening url = " + url);
  299. try {
  300. Intent i = new Intent(Intent.ACTION_VIEW);
  301. i.setData(Uri.parse(url));
  302. i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
  303. context.startActivity(i);
  304. return true;
  305. } catch (RuntimeException e) {
  306. Log.d("GameActivity", "love.system.openURL", e);
  307. return false;
  308. }
  309. }
  310. /**
  311. * Copies a given file from the assets folder to the destination.
  312. *
  313. * @return true if successful
  314. */
  315. boolean copyAssetFile(InputStream source_stream, String destinationFileName) {
  316. boolean success = false;
  317. BufferedOutputStream destination_stream = null;
  318. try {
  319. destination_stream = new BufferedOutputStream(new FileOutputStream(destinationFileName, false));
  320. } catch (IOException e) {
  321. Log.d("GameActivity", "Could not open destination file: " + e.getMessage());
  322. }
  323. // perform the copying
  324. int chunk_read = 0;
  325. int bytes_written = 0;
  326. assert (source_stream != null && destination_stream != null);
  327. try {
  328. byte[] buf = new byte[1024];
  329. chunk_read = source_stream.read(buf);
  330. do {
  331. destination_stream.write(buf, 0, chunk_read);
  332. bytes_written += chunk_read;
  333. chunk_read = source_stream.read(buf);
  334. } while (chunk_read != -1);
  335. } catch (IOException e) {
  336. Log.d("GameActivity", "Copying failed:" + e.getMessage());
  337. }
  338. // close streams
  339. try {
  340. if (source_stream != null) source_stream.close();
  341. if (destination_stream != null) destination_stream.close();
  342. success = true;
  343. } catch (IOException e) {
  344. Log.d("GameActivity", "Copying failed: " + e.getMessage());
  345. }
  346. Log.d("GameActivity", "Successfully copied stream to " + destinationFileName + " (" + bytes_written + " bytes written).");
  347. return success;
  348. }
  349. @Keep
  350. public boolean hasBackgroundMusic() {
  351. AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  352. return audioManager.isMusicActive();
  353. }
  354. @Keep
  355. public void showRecordingAudioPermissionMissingDialog() {
  356. Log.d("GameActivity", "showRecordingAudioPermissionMissingDialog()");
  357. runOnUiThread(new Runnable() {
  358. @Override
  359. public void run() {
  360. AlertDialog dialog = new AlertDialog.Builder(mSingleton)
  361. .setTitle("Audio Recording Permission Missing")
  362. .setMessage("It appears that this game uses mic capabilities. The game may not work correctly without mic permission!")
  363. .setNeutralButton("Continue", new DialogInterface.OnClickListener() {
  364. public void onClick(DialogInterface di, int id) {
  365. synchronized (recordAudioRequestDummy) {
  366. recordAudioRequestDummy.notify();
  367. }
  368. }
  369. })
  370. .create();
  371. dialog.show();
  372. }
  373. });
  374. synchronized (recordAudioRequestDummy) {
  375. try {
  376. recordAudioRequestDummy.wait();
  377. } catch (InterruptedException e) {
  378. Log.d("GameActivity", "mic permission dialog", e);
  379. }
  380. }
  381. }
  382. public void showExternalStoragePermissionMissingDialog() {
  383. AlertDialog dialog = new AlertDialog.Builder(mSingleton)
  384. .setTitle("Storage Permission Missing")
  385. .setMessage("LÖVE for Android will not be able to run non-packaged games without storage permission.")
  386. .setNeutralButton("Continue", null)
  387. .create();
  388. dialog.show();
  389. }
  390. @Override
  391. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  392. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  393. if (grantResults.length > 0) {
  394. Log.d("GameActivity", "Received a request permission result");
  395. switch (requestCode) {
  396. case EXTERNAL_STORAGE_REQUEST_CODE: {
  397. if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  398. Log.d("GameActivity", "Permission granted");
  399. } else {
  400. Log.d("GameActivity", "Did not get permission.");
  401. if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
  402. showExternalStoragePermissionMissingDialog();
  403. }
  404. }
  405. Log.d("GameActivity", "Unlocking LÖVE thread");
  406. synchronized (externalStorageRequestDummy) {
  407. externalStorageRequestDummy[0] = grantResults[0];
  408. externalStorageRequestDummy.notify();
  409. }
  410. break;
  411. }
  412. case RECORD_AUDIO_REQUEST_CODE: {
  413. if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  414. Log.d("GameActivity", "Mic ermission granted");
  415. } else {
  416. Log.d("GameActivity", "Did not get mic permission.");
  417. }
  418. Log.d("GameActivity", "Unlocking LÖVE thread");
  419. synchronized (recordAudioRequestDummy) {
  420. recordAudioRequestDummy[0] = grantResults[0];
  421. recordAudioRequestDummy.notify();
  422. }
  423. break;
  424. }
  425. }
  426. }
  427. }
  428. @Keep
  429. public boolean hasExternalStoragePermission() {
  430. if (ActivityCompat.checkSelfPermission(this,
  431. Manifest.permission.READ_EXTERNAL_STORAGE)
  432. == PackageManager.PERMISSION_GRANTED) {
  433. return true;
  434. }
  435. Log.d("GameActivity", "Requesting permission and locking LÖVE thread until we have an answer.");
  436. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_REQUEST_CODE);
  437. synchronized (externalStorageRequestDummy) {
  438. try {
  439. externalStorageRequestDummy.wait();
  440. } catch (InterruptedException e) {
  441. Log.d("GameActivity", "requesting external storage permission", e);
  442. return false;
  443. }
  444. }
  445. return ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
  446. }
  447. @Keep
  448. public boolean hasRecordAudioPermission() {
  449. return ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
  450. }
  451. @Keep
  452. public void requestRecordAudioPermission() {
  453. if (ActivityCompat.checkSelfPermission(this,
  454. Manifest.permission.RECORD_AUDIO)
  455. == PackageManager.PERMISSION_GRANTED) {
  456. return;
  457. }
  458. Log.d("GameActivity", "Requesting mic permission and locking LÖVE thread until we have an answer.");
  459. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, RECORD_AUDIO_REQUEST_CODE);
  460. synchronized (recordAudioRequestDummy) {
  461. try {
  462. recordAudioRequestDummy.wait();
  463. } catch (InterruptedException e) {
  464. Log.d("GameActivity", "requesting mic permission", e);
  465. }
  466. }
  467. }
  468. @Keep
  469. public boolean initializeSafeArea() {
  470. if (android.os.Build.VERSION.SDK_INT >= 28) {
  471. DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
  472. if (cutout != null) {
  473. safeAreaTop = cutout.getSafeInsetTop();
  474. safeAreaLeft = cutout.getSafeInsetLeft();
  475. safeAreaBottom = cutout.getSafeInsetBottom();
  476. safeAreaRight = cutout.getSafeInsetRight();
  477. return true;
  478. }
  479. }
  480. return false;
  481. }
  482. }