From cb2c852f5827d11469c1a42d81d11e236b06e35d Mon Sep 17 00:00:00 2001 From: fysx Date: Fri, 14 Feb 2014 01:41:47 +0100 Subject: [PATCH] trying to add running of games via http link --- AndroidManifest.xml | 10 ++++ src/org/love2d/android/DownloadService.java | 63 +++++++++++++++++++++ src/org/love2d/android/GameActivity.java | 44 +++++++++++++- 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/org/love2d/android/DownloadService.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index f2e70cb1..856ef9c0 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -8,6 +8,8 @@ + + + + + + + + + + diff --git a/src/org/love2d/android/DownloadService.java b/src/org/love2d/android/DownloadService.java new file mode 100644 index 00000000..41dfeae9 --- /dev/null +++ b/src/org/love2d/android/DownloadService.java @@ -0,0 +1,63 @@ +package org.love2d.android; + +import java.io.BufferedInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URL; +import java.net.URLConnection; + +import android.app.IntentService; +import android.content.Intent; +import android.os.Bundle; +import android.os.ResultReceiver; +import android.util.Log; + +public class DownloadService extends IntentService { + public static final int UPDATE_PROGRESS = 8344; + public DownloadService() { + super("DownloadService"); + } + @Override + protected void onHandleIntent(Intent intent) { + Log.d ("GameActivity", "service started"); + String urlToDownload = intent.getStringExtra("url"); + ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver"); + try { + URL url = new URL(urlToDownload); + URLConnection connection = url.openConnection(); + connection.connect(); + // this will be useful so that you can show a typical 0-100% progress bar + int fileLength = connection.getContentLength(); + + // download the file + InputStream input = new BufferedInputStream(url.openStream()); + OutputStream output = new FileOutputStream("/sdcard/lovegame/download.love"); + + byte data[] = new byte[1024]; + long total = 0; + int count; + while ((count = input.read(data)) != -1) { + total += count; + // publishing the progress.... + Bundle resultData = new Bundle(); + resultData.putInt("progress" ,(int) (total * 100 / fileLength)); + receiver.send(UPDATE_PROGRESS, resultData); + output.write(data, 0, count); + + Log.d ("GameActivity", "Downloading..."); + } + + output.flush(); + output.close(); + input.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + Bundle resultData = new Bundle(); + resultData.putInt("progress" ,100); + receiver.send(UPDATE_PROGRESS, resultData); + } +} diff --git a/src/org/love2d/android/GameActivity.java b/src/org/love2d/android/GameActivity.java index caa1f61c..ceee2d76 100644 --- a/src/org/love2d/android/GameActivity.java +++ b/src/org/love2d/android/GameActivity.java @@ -7,15 +7,23 @@ import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; +import android.app.DownloadManager; +import android.app.ProgressDialog; +import android.content.Intent; import android.net.Uri; +import android.os.Build; import android.os.Bundle; +import android.os.Environment; +import android.os.Handler; +import android.os.ResultReceiver; import android.util.Log; import android.util.DisplayMetrics; public class GameActivity extends SDLActivity { private static DisplayMetrics metrics = new DisplayMetrics(); - private static String gamePath = ""; + // declare the dialog as a member field of your activity + private static ProgressDialog mProgressDialog; @Override protected void onCreate(Bundle savedInstanceState) { @@ -64,6 +72,23 @@ public class GameActivity extends SDLActivity { } catch (IOException e) { Log.d ("GameActivity", "Could not open game file:" + e.getMessage()); } + } else if (sourceuri.getScheme().equals("http")) { + String url = sourceuri.getScheme() + "://" + sourceuri.getHost() + sourceuri.getPath(); + Log.d("GameActivity", "url = " + url); + + // instantiate it within the onCreate method + mProgressDialog = new ProgressDialog(GameActivity.this); + mProgressDialog.setMessage("Downloading Game"); + mProgressDialog.setIndeterminate(true); + mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + mProgressDialog.setCancelable(true); + + // this is how you fire the downloader + mProgressDialog.show(); + Intent intent = new Intent(this, DownloadService.class); + intent.putExtra("url", url); + intent.putExtra("receiver", new DownloadReceiver(new Handler())); + startService(intent); } else { Log.d ("GameActivity", "Unsupported scheme: " + sourceuri.getScheme()); } @@ -94,4 +119,21 @@ public class GameActivity extends SDLActivity { Log.d("GameActivity", "Copied " + bytes_written + " bytes"); } + private class DownloadReceiver extends ResultReceiver{ + public DownloadReceiver(Handler handler) { + super(handler); + } + + @Override + protected void onReceiveResult(int resultCode, Bundle resultData) { + super.onReceiveResult(resultCode, resultData); + if (resultCode == DownloadService.UPDATE_PROGRESS) { + int progress = resultData.getInt("progress"); + mProgressDialog.setProgress(progress); + if (progress == 100) { + mProgressDialog.dismiss(); + } + } + } + } }