mirror of
https://github.com/love2d/love-android.git
synced 2026-08-12 00:20:57 +02:00
Proper downloading of games and starting works when downloaded using the LÖVE app
This commit is contained in:
+15
-10
@@ -12,8 +12,8 @@
|
||||
android:allowBackup="true"
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="LÖVE for Android"
|
||||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
|
||||
<service android:name=".DownloadService" />
|
||||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
|
||||
<service android:name=".DownloadService" />
|
||||
<activity
|
||||
android:name="GameActivity"
|
||||
android:configChanges="orientation|screenSize"
|
||||
@@ -36,14 +36,6 @@
|
||||
<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" />
|
||||
@@ -53,6 +45,19 @@
|
||||
<data android:host="*" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity
|
||||
android:name="DownloadActivity"
|
||||
android:noHistory="true" >
|
||||
<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:scheme="https" />
|
||||
<data android:host="*" />
|
||||
<data android:pathPattern=".*\\.love" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
<!-- Android 2.3.3 -->
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.love2d.android;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
|
||||
public class DownloadActivity extends Activity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
Uri uri = this.getIntent().getData();
|
||||
|
||||
if (uri.getScheme().equals("http")) {
|
||||
String url = uri.toString();
|
||||
Intent intent = new Intent(this, DownloadService.class);
|
||||
intent.putExtra("url", url);
|
||||
startService(intent);
|
||||
};
|
||||
|
||||
finish();
|
||||
}
|
||||
}
|
||||
@@ -1,61 +1,89 @@
|
||||
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 java.util.List;
|
||||
|
||||
import android.app.DownloadManager;
|
||||
import android.app.IntentService;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.ResultReceiver;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
|
||||
public class DownloadService extends IntentService {
|
||||
public static final int UPDATE_PROGRESS = 8344;
|
||||
public DownloadService() {
|
||||
super("DownloadService");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
Log.d("DownloadService", "ending");
|
||||
unregisterReceiver(downloadReceiver);
|
||||
}
|
||||
|
||||
@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();
|
||||
Log.d ("DownloadService", "service started");
|
||||
|
||||
String url = intent.getStringExtra("url");
|
||||
Uri uri = Uri.parse(url);
|
||||
|
||||
// download the file
|
||||
InputStream input = new BufferedInputStream(url.openStream());
|
||||
OutputStream output = new FileOutputStream("/sdcard/lovegame/download.love");
|
||||
Log.d("DownloadActivity", "Downloading from url: " + url + "file = " + uri.getLastPathSegment());
|
||||
|
||||
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);
|
||||
}
|
||||
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
|
||||
request.setDescription("LÖVE Game Download");
|
||||
request.setTitle(uri.getLastPathSegment());
|
||||
request.setMimeType ("application/x-love-game");
|
||||
|
||||
// in order for this if to run, you must use the android 3.2 to compile your app
|
||||
if (Build.VERSION.SDK_INT >= 11) {
|
||||
request.allowScanningByMediaScanner();
|
||||
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
|
||||
}
|
||||
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());
|
||||
// get download service and enqueue file
|
||||
|
||||
output.flush();
|
||||
output.close();
|
||||
input.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Log.d("DownloadActivity", "creating manager");
|
||||
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
Log.d("DownloadActivity", "enqueuing download");
|
||||
manager.enqueue(request);
|
||||
|
||||
Bundle resultData = new Bundle();
|
||||
resultData.putInt("progress" ,100);
|
||||
receiver.send(UPDATE_PROGRESS, resultData);
|
||||
Log.d("DownloadActivity", "download receiver = " + downloadReceiver);
|
||||
IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
|
||||
registerReceiver(downloadReceiver, intentFilter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param context used to check the device version and DownloadManager information
|
||||
* @return true if the download manager is available
|
||||
*/
|
||||
public static boolean isDownloadManagerAvailable(Context context) {
|
||||
try {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
|
||||
return false;
|
||||
}
|
||||
Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||
intent.addCategory(Intent.CATEGORY_LAUNCHER);
|
||||
intent.setClassName("com.android.providers.downloads.ui", "com.android.providers.downloads.ui.DownloadList");
|
||||
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,
|
||||
PackageManager.MATCH_DEFAULT_ONLY);
|
||||
return list.size() > 0;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Log.d("DownloadActivity", "downloadReceiver intent called");
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,27 +6,36 @@ 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.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 = "";
|
||||
// declare the dialog as a member field of your activity
|
||||
private static ProgressDialog mProgressDialog;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
Log.d("GameActivity", "started");
|
||||
|
||||
Uri game = this.getIntent().getData();
|
||||
if (game != null) {
|
||||
if (game.getScheme().equals ("file")) {
|
||||
@@ -36,7 +45,7 @@ public class GameActivity extends SDLActivity {
|
||||
}
|
||||
Log.d("GameActivity", "Selected the file: " + getGamePath());
|
||||
}
|
||||
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
getWindowManager().getDefaultDisplay().getMetrics(metrics);
|
||||
}
|
||||
@@ -72,25 +81,6 @@ 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);
|
||||
mProgressDialog.setProgress (0);
|
||||
mProgressDialog.setMax (100);
|
||||
|
||||
// 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());
|
||||
}
|
||||
@@ -120,24 +110,4 @@ 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) {
|
||||
mProgressDialog.setIndeterminate(false);
|
||||
int progress = resultData.getInt("progress");
|
||||
mProgressDialog.setProgress(progress);
|
||||
if (progress == 100) {
|
||||
mProgressDialog.dismiss();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user