trying to add running of games via http link

This commit is contained in:
fysx
2014-02-14 01:41:47 +01:00
parent 60a58f9240
commit cb2c852f58
3 changed files with 116 additions and 1 deletions
+10
View File
@@ -8,6 +8,8 @@
<!-- Allow writing to external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<service android:name="DownloadService" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
@@ -35,6 +37,14 @@
<data android:scheme="content" />
<data android:mimeType="application/x-love-game" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:host="*" />
<data android:pathPattern=".*\\.love" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
@@ -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);
}
}
+43 -1
View File
@@ -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();
}
}
}
}
}