DownloadActivity.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright (c) 2006-2020 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 android.app.Activity;
  22. import android.app.AlertDialog;
  23. import android.content.DialogInterface;
  24. import android.content.Intent;
  25. import android.content.pm.PackageManager;
  26. import android.Manifest;
  27. import android.net.Uri;
  28. import android.os.Build;
  29. import android.os.Bundle;
  30. import android.util.Log;
  31. import androidx.core.app.ActivityCompat;
  32. public class DownloadActivity extends Activity {
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ||
  37. ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
  38. {
  39. runDownloader();
  40. finish();
  41. } else {
  42. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, GameActivity.EXTERNAL_STORAGE_REQUEST_CODE);
  43. }
  44. }
  45. @Override
  46. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  47. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  48. if (grantResults.length > 0) {
  49. Log.d("DownloadActivity", "Received a request permission result");
  50. if (requestCode == GameActivity.EXTERNAL_STORAGE_REQUEST_CODE) {
  51. if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  52. runDownloader();
  53. finish();
  54. } else {
  55. Log.d("DownloadActivity", "Did not get permission.");
  56. if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
  57. showExternalStoragePermissionMissingDialog();
  58. }
  59. }
  60. }
  61. else {
  62. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  63. }
  64. }
  65. }
  66. protected void showExternalStoragePermissionMissingDialog() {
  67. AlertDialog dialog = new AlertDialog.Builder(this)
  68. .setTitle("Storage Permission Missing")
  69. .setMessage("LÖVE for Android will not be able to download games without storage permission.")
  70. .setNeutralButton("Continue", new DialogInterface.OnClickListener() {
  71. public void onClick(DialogInterface dialog, int whichButton) {
  72. dialog.dismiss();
  73. finish();
  74. }
  75. })
  76. .create();
  77. dialog.show();
  78. }
  79. protected void runDownloader()
  80. {
  81. Intent intent = getIntent();
  82. if (intent != null) {
  83. Uri uri = intent.getData();
  84. Intent targetIntent = new Intent(this, DownloadService.class);
  85. targetIntent.putExtra("url", uri.toString());
  86. startService(targetIntent);
  87. }
  88. }
  89. }