| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package org.love2d.android;
- import org.libsdl.app.SDLActivity;
- import java.util.List;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import android.app.DownloadManager;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.PowerManager;
- import android.os.ResultReceiver;
- import android.os.Vibrator;
- import android.util.Log;
- import android.util.DisplayMetrics;
- import android.widget.Toast;
- public class GameActivity extends SDLActivity {
- private static DisplayMetrics metrics = new DisplayMetrics();
- private static String gamePath = "";
- private static Context context;
- private static Vibrator vibrator;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- Log.d("GameActivity", "started");
-
- context = this.getApplicationContext();
- vibrator = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
- Uri game = this.getIntent().getData();
- if (game != null) {
- if (game.getScheme().equals ("file")) {
- // If we were given the path of a main.lua then use its
- // directory. Otherwise use full path.
- List<String> path_segments = game.getPathSegments();
- if (path_segments.get(path_segments.size() - 1).equals("main.lua")) {
- gamePath = game.getPath().substring(0, game.getPath().length() - "main.lua".length());
- } else {
- gamePath = game.getPath();
- }
- } else {
- copyGameToCache (game);
- }
- Log.d("GameActivity", "Opening game from: " + getGamePath());
- }
- super.onCreate(savedInstanceState);
- getWindowManager().getDefaultDisplay().getMetrics(metrics);
- }
- @Override
- protected void onDestroy() {
- Log.d("GameActivity", "Cancelling vibration");
- vibrator.cancel();
- super.onDestroy();
- }
- @Override
- protected void onPause() {
- Log.d("GameActivity", "Cancelling vibration");
- vibrator.cancel();
- super.onPause();
- }
- public static String getGamePath() {
- Log.d ("GameActivity", "called getGamePath(), game path = " + gamePath);
- return gamePath;
- }
- public static DisplayMetrics getMetrics() {
- return metrics;
- }
- public static void vibrate (double seconds) {
- vibrator.vibrate((long) (seconds * 1000.));
- }
- public static void openURL (String url) {
- Log.d ("GameActivity", "opening url = " + url);
- Intent i = new Intent(Intent.ACTION_VIEW);
- i.setData(Uri.parse(url));
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(i);
- }
-
- void copyGameToCache (Uri sourceuri)
- {
- String destinationFilename = this.getCacheDir().getPath()+"/downloaded.love";
- gamePath = destinationFilename;
- BufferedOutputStream bos = null;
- try {
- bos = new BufferedOutputStream(new FileOutputStream(destinationFilename, false));
- } catch (IOException e) {
- Log.d ("GameActivity", "Could not open destination file:" + e.getMessage());
- }
- int chunk_read = 0;
- int bytes_written = 0;
- BufferedInputStream bis = null;
- if (sourceuri.getScheme().equals("content")) {
- try {
- bis = new BufferedInputStream(getContentResolver().openInputStream(sourceuri));
- } catch (IOException e) {
- Log.d ("GameActivity", "Could not open game file:" + e.getMessage());
- }
- } else {
- Log.d ("GameActivity", "Unsupported scheme: " + sourceuri.getScheme());
- }
- if (bis != null) {
- // actual copying
- try {
- byte[] buf = new byte[1024];
- chunk_read = bis.read(buf);
- do {
- bos.write(buf, 0, chunk_read);
- bytes_written += chunk_read;
- chunk_read = bis.read(buf);
- } while(chunk_read != -1);
- } catch (IOException e) {
- Log.d ("GameActivity", "Copying failed:" + e.getMessage());
- }
- }
- // close streams
- try {
- if (bis != null) bis.close();
- if (bos != null) bos.close();
- } catch (IOException e) {
- Log.d ("GameActivity", "Copying failed: " + e.getMessage());
- }
- Log.d("GameActivity", "Copied " + bytes_written + " bytes");
- }
- }
|