DownloadService.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Copyright (c) 2006-2024 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. package org.love2d.android;
  21. import java.util.List;
  22. import android.app.DownloadManager;
  23. import android.app.IntentService;
  24. import android.content.BroadcastReceiver;
  25. import android.content.Context;
  26. import android.content.Intent;
  27. import android.content.IntentFilter;
  28. import android.content.pm.PackageManager;
  29. import android.content.pm.ResolveInfo;
  30. import android.net.Uri;
  31. import android.os.Build;
  32. import android.os.Environment;
  33. import android.util.Log;
  34. public class DownloadService extends IntentService {
  35. public DownloadService() {
  36. super("DownloadService");
  37. }
  38. @Override
  39. public void onDestroy() {
  40. Log.d("DownloadService", "destroying");
  41. unregisterReceiver(downloadReceiver);
  42. }
  43. @Override
  44. protected void onHandleIntent(Intent intent) {
  45. Log.d("DownloadService", "service started");
  46. String url = intent.getStringExtra("url");
  47. Uri uri = Uri.parse(url);
  48. Log.d("DownloadService", "Downloading from url: " + url + " file = " + uri.getLastPathSegment());
  49. DownloadManager.Request request = new DownloadManager.Request(uri);
  50. request.setDescription("LÖVE Game Download");
  51. request.setTitle(uri.getLastPathSegment());
  52. request.setMimeType("application/x-love-game");
  53. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
  54. request.allowScanningByMediaScanner();
  55. }
  56. request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
  57. request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());
  58. // get download service and enqueue file
  59. Log.d("DownloadActivity", "creating manager");
  60. DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
  61. Log.d("DownloadActivity", "enqueuing download");
  62. manager.enqueue(request);
  63. Log.d("DownloadActivity", "download receiver = " + downloadReceiver);
  64. IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
  65. registerReceiver(downloadReceiver, intentFilter);
  66. }
  67. private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
  68. @Override
  69. public void onReceive(Context context, Intent intent) {
  70. Log.d("DownloadActivity", "downloadReceiver intent called");
  71. }
  72. };
  73. }