PalActivity.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package io.github.sdlpal;
  2. import org.libsdl.app.SDLActivity;
  3. import android.content.Intent;
  4. import android.os.*;
  5. import android.util.*;
  6. import android.media.*;
  7. import android.net.Uri;
  8. import java.io.*;
  9. public class PalActivity extends SDLActivity {
  10. private static final String TAG = "sdlpal-debug";
  11. private static MediaPlayer mediaPlayer;
  12. public static native void setAppPath(String basepath, String datapath, String cachepath);
  13. public static native void setScreenSize(int width, int height);
  14. public static boolean crashed = false;
  15. private static MediaPlayer JNI_mediaplayer_load(String filename){
  16. Log.v(TAG, "loading midi:" + filename);
  17. MediaPlayer mediaPlayer = new MediaPlayer();
  18. mediaPlayer.reset();
  19. try {
  20. mediaPlayer.setDataSource(mSingleton.getApplicationContext(), Uri.fromFile(new File(filename)));
  21. mediaPlayer.prepare();
  22. } catch(IOException e) {
  23. Log.e(TAG, filename + " not available for playing, check");
  24. }
  25. PalActivity.mediaPlayer = mediaPlayer;
  26. return mediaPlayer;
  27. }
  28. @Override
  29. public void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. String dataPath = getApplicationContext().getFilesDir().getPath();
  32. String cachePath = getApplicationContext().getCacheDir().getPath();
  33. String sdcardState = Environment.getExternalStorageState();
  34. if (sdcardState.equals(Environment.MEDIA_MOUNTED)){
  35. setAppPath(Environment.getExternalStorageDirectory().getPath() + "/sdlpal/", dataPath, cachePath);
  36. } else {
  37. setAppPath("/sdcard/sdlpal/", dataPath, cachePath);
  38. }
  39. DisplayMetrics metrics = new DisplayMetrics();
  40. getWindowManager().getDefaultDisplay().getMetrics(metrics);
  41. setScreenSize(metrics.widthPixels, metrics.heightPixels);
  42. File runningFile = new File(cachePath + "/running");
  43. crashed = runningFile.exists();
  44. if (SettingsActivity.loadConfigFile() || crashed) {
  45. runningFile.delete();
  46. Intent intent = new Intent(this, SettingsActivity.class);
  47. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  48. startActivity(intent);
  49. finish();
  50. }
  51. }
  52. @Override
  53. public void onDestroy() {
  54. super.onDestroy();
  55. }
  56. @Override
  57. protected void onPause() {
  58. if (!this.isFinishing() && mediaPlayer != null) {
  59. mediaPlayer.pause();
  60. }
  61. super.onPause();
  62. }
  63. @Override
  64. protected void onResume() {
  65. if (mediaPlayer != null) {
  66. mediaPlayer.start();
  67. }
  68. super.onResume();
  69. }
  70. }