GameActivity.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package org.love2d.android;
  2. import org.libsdl.app.SDLActivity;
  3. import java.util.List;
  4. import java.io.BufferedInputStream;
  5. import java.io.BufferedOutputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.net.HttpURLConnection;
  11. import java.net.URL;
  12. import android.app.DownloadManager;
  13. import android.app.ProgressDialog;
  14. import android.content.Context;
  15. import android.content.DialogInterface;
  16. import android.content.Intent;
  17. import android.net.Uri;
  18. import android.os.AsyncTask;
  19. import android.os.Build;
  20. import android.os.Bundle;
  21. import android.os.Environment;
  22. import android.os.Handler;
  23. import android.os.PowerManager;
  24. import android.os.ResultReceiver;
  25. import android.os.Vibrator;
  26. import android.util.Log;
  27. import android.util.DisplayMetrics;
  28. import android.widget.Toast;
  29. public class GameActivity extends SDLActivity {
  30. private static DisplayMetrics metrics = new DisplayMetrics();
  31. private static String gamePath = "";
  32. private static Context context;
  33. private static Vibrator vibrator;
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. Log.d("GameActivity", "started");
  37. context = this.getApplicationContext();
  38. vibrator = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
  39. Uri game = this.getIntent().getData();
  40. if (game != null) {
  41. if (game.getScheme().equals ("file")) {
  42. // If we were given the path of a main.lua then use its
  43. // directory. Otherwise use full path.
  44. List<String> path_segments = game.getPathSegments();
  45. if (path_segments.get(path_segments.size() - 1).equals("main.lua")) {
  46. gamePath = game.getPath().substring(0, game.getPath().length() - "main.lua".length());
  47. } else {
  48. gamePath = game.getPath();
  49. }
  50. } else {
  51. copyGameToCache (game);
  52. }
  53. Log.d("GameActivity", "Opening game from: " + getGamePath());
  54. }
  55. super.onCreate(savedInstanceState);
  56. getWindowManager().getDefaultDisplay().getMetrics(metrics);
  57. }
  58. @Override
  59. protected void onDestroy() {
  60. Log.d("GameActivity", "Cancelling vibration");
  61. vibrator.cancel();
  62. super.onDestroy();
  63. }
  64. @Override
  65. protected void onPause() {
  66. Log.d("GameActivity", "Cancelling vibration");
  67. vibrator.cancel();
  68. super.onPause();
  69. }
  70. public static String getGamePath() {
  71. Log.d ("GameActivity", "called getGamePath(), game path = " + gamePath);
  72. return gamePath;
  73. }
  74. public static DisplayMetrics getMetrics() {
  75. return metrics;
  76. }
  77. public static void vibrate (double seconds) {
  78. vibrator.vibrate((long) (seconds * 1000.));
  79. }
  80. public static void openURL (String url) {
  81. Log.d ("GameActivity", "opening url = " + url);
  82. Intent i = new Intent(Intent.ACTION_VIEW);
  83. i.setData(Uri.parse(url));
  84. i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  85. context.startActivity(i);
  86. }
  87. void copyGameToCache (Uri sourceuri)
  88. {
  89. String destinationFilename = this.getCacheDir().getPath()+"/downloaded.love";
  90. gamePath = destinationFilename;
  91. BufferedOutputStream bos = null;
  92. try {
  93. bos = new BufferedOutputStream(new FileOutputStream(destinationFilename, false));
  94. } catch (IOException e) {
  95. Log.d ("GameActivity", "Could not open destination file:" + e.getMessage());
  96. }
  97. int chunk_read = 0;
  98. int bytes_written = 0;
  99. BufferedInputStream bis = null;
  100. if (sourceuri.getScheme().equals("content")) {
  101. try {
  102. bis = new BufferedInputStream(getContentResolver().openInputStream(sourceuri));
  103. } catch (IOException e) {
  104. Log.d ("GameActivity", "Could not open game file:" + e.getMessage());
  105. }
  106. } else {
  107. Log.d ("GameActivity", "Unsupported scheme: " + sourceuri.getScheme());
  108. }
  109. if (bis != null) {
  110. // actual copying
  111. try {
  112. byte[] buf = new byte[1024];
  113. chunk_read = bis.read(buf);
  114. do {
  115. bos.write(buf, 0, chunk_read);
  116. bytes_written += chunk_read;
  117. chunk_read = bis.read(buf);
  118. } while(chunk_read != -1);
  119. } catch (IOException e) {
  120. Log.d ("GameActivity", "Copying failed:" + e.getMessage());
  121. }
  122. }
  123. // close streams
  124. try {
  125. if (bis != null) bis.close();
  126. if (bos != null) bos.close();
  127. } catch (IOException e) {
  128. Log.d ("GameActivity", "Copying failed: " + e.getMessage());
  129. }
  130. Log.d("GameActivity", "Copied " + bytes_written + " bytes");
  131. }
  132. }