From 5f6fa8c2b5cb7743c2df4c2c04ea91540a2b0a13 Mon Sep 17 00:00:00 2001 From: fysx Date: Thu, 20 Feb 2014 21:27:34 +0100 Subject: [PATCH] =?UTF-8?q?Proper=20downloading=20of=20games=20and=20start?= =?UTF-8?q?ing=20works=20when=20downloaded=20using=20the=20L=C3=96VE=20app?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AndroidManifest.xml | 25 +++-- src/org/love2d/android/DownloadActivity.java | 24 ++++ src/org/love2d/android/DownloadService.java | 112 ++++++++++++------- src/org/love2d/android/GameActivity.java | 56 +++------- 4 files changed, 122 insertions(+), 95 deletions(-) create mode 100644 src/org/love2d/android/DownloadActivity.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 954cbc51..bdf1a5f9 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -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" > - + android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > + - - - - - - - - @@ -53,6 +45,19 @@ + + + + + + + + + + + diff --git a/src/org/love2d/android/DownloadActivity.java b/src/org/love2d/android/DownloadActivity.java new file mode 100644 index 00000000..6a65c753 --- /dev/null +++ b/src/org/love2d/android/DownloadActivity.java @@ -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(); + } + } diff --git a/src/org/love2d/android/DownloadService.java b/src/org/love2d/android/DownloadService.java index bf6bfaea..b15ff2b3 100644 --- a/src/org/love2d/android/DownloadService.java +++ b/src/org/love2d/android/DownloadService.java @@ -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 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"); + + } + }; } diff --git a/src/org/love2d/android/GameActivity.java b/src/org/love2d/android/GameActivity.java index c33ad9f2..20a0286d 100644 --- a/src/org/love2d/android/GameActivity.java +++ b/src/org/love2d/android/GameActivity.java @@ -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(); - } - } - } - } }