mirror of
https://github.com/love2d/love-android.git
synced 2026-08-12 08:31:04 +02:00
Fix download manager crashing in newer devices.
Request storage permission in Mashmallow until Pie. Also added support of HTTPS URL.
This commit is contained in:
@@ -1,24 +1,78 @@
|
||||
package org.love2d.android;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.Manifest;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.core.app.ActivityCompat;
|
||||
|
||||
public class DownloadActivity extends Activity {
|
||||
public static final int EXTERNAL_STORAGE_REQUEST_CODE = 2;
|
||||
|
||||
@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);
|
||||
if (android.os.Build.VERSION.SDK_INT >= 29 ||
|
||||
ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
|
||||
{
|
||||
runDownloader();
|
||||
finish();
|
||||
} else {
|
||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_REQUEST_CODE);
|
||||
}
|
||||
}
|
||||
|
||||
finish();
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
|
||||
if (grantResults.length > 0) {
|
||||
Log.d("DownloadActivity", "Received a request permission result");
|
||||
|
||||
if (requestCode == EXTERNAL_STORAGE_REQUEST_CODE) {
|
||||
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
runDownloader();
|
||||
finish();
|
||||
} else {
|
||||
Log.d("DownloadActivity", "Did not get permission.");
|
||||
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
||||
showExternalStoragePermissionMissingDialog();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void showExternalStoragePermissionMissingDialog() {
|
||||
AlertDialog dialog = new AlertDialog.Builder(this)
|
||||
.setTitle("Storage Permission Missing")
|
||||
.setMessage("LÖVE for Android will not be able to download games without storage permission.")
|
||||
.setNeutralButton("Continue", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int whichButton) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
})
|
||||
.create();
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
protected void runDownloader()
|
||||
{
|
||||
Intent intent = getIntent();
|
||||
|
||||
if (intent != null) {
|
||||
Uri uri = intent.getData();
|
||||
Intent targetIntent = new Intent(this, DownloadService.class);
|
||||
targetIntent.putExtra("url", uri.toString());
|
||||
startService(targetIntent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,9 +33,9 @@ public class DownloadService extends IntentService {
|
||||
String url = intent.getStringExtra("url");
|
||||
Uri uri = Uri.parse(url);
|
||||
|
||||
Log.d("DownloadService", "Downloading from url: " + url + "file = " + uri.getLastPathSegment());
|
||||
Log.d("DownloadService", "Downloading from url: " + url + " file = " + uri.getLastPathSegment());
|
||||
|
||||
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
|
||||
DownloadManager.Request request = new DownloadManager.Request(uri);
|
||||
request.setDescription("LÖVE Game Download");
|
||||
request.setTitle(uri.getLastPathSegment());
|
||||
request.setMimeType("application/x-love-game");
|
||||
|
||||
Reference in New Issue
Block a user