Skip to content

Commit

Permalink
Skip asking READ/WRITE_EXTERNAL_STORAGE permission for Android >= 13
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Oct 6, 2023
1 parent fef64f2 commit 17c9a97
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CommonUtils
11 changes: 9 additions & 2 deletions app/src/main/java/com/gianlu/aria2app/LoadingActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
Expand Down Expand Up @@ -201,7 +202,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

private void connectToInAppDownloader(@NonNull MultiProfile profile) {
AskPermission.ask(this, Manifest.permission.WRITE_EXTERNAL_STORAGE, new AskPermission.Listener() {
AskPermission.Listener listener = new AskPermission.Listener() {
@Override
public void permissionGranted(@NonNull String permission) {
connecting.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -241,7 +242,13 @@ public void askRationale(@NonNull AlertDialog.Builder builder) {
builder.setTitle(R.string.writeExternalStorageRequest_title)
.setMessage(R.string.writeExternalStorageRequest_message);
}
});
};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
listener.permissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE);
return;
}

AskPermission.ask(this, Manifest.permission.WRITE_EXTERNAL_STORAGE, listener);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
Expand Down Expand Up @@ -130,7 +131,7 @@ public void onException(@NonNull Exception ex) {
}

private void openDocument(@NonNull final String mime, @NonNull final String text, final int requestCode) {
AskPermission.ask(this, Manifest.permission.READ_EXTERNAL_STORAGE, new AskPermission.Listener() {
AskPermission.Listener listener = new AskPermission.Listener() {
@Override
public void permissionGranted(@NonNull String permission) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
Expand All @@ -150,7 +151,13 @@ public void askRationale(@NonNull AlertDialog.Builder builder) {
builder.setTitle(R.string.readExternalStorageRequest_title)
.setMessage(R.string.readExternalStorageRequest_base64Message);
}
});
};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
listener.permissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE);
return;
}

AskPermission.ask(this, Manifest.permission.READ_EXTERNAL_STORAGE, listener);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -109,7 +110,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
pick.setOnClickListener(v -> {
if (getActivity() == null) return;

AskPermission.ask(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE, new AskPermission.Listener() {
AskPermission.Listener listener = new AskPermission.Listener() {
@Override
public void permissionGranted(@NonNull String permission) {
showFilePicker();
Expand All @@ -125,7 +126,13 @@ public void askRationale(@NonNull AlertDialog.Builder builder) {
builder.setTitle(R.string.readExternalStorageRequest_title)
.setMessage(R.string.readExternalStorageRequest_base64Message);
}
});
};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
listener.permissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE);
return;
}

AskPermission.ask(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE, listener);
});

SuperTextView help = layout.findViewById(R.id.base64Fragment_help);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
Expand Down Expand Up @@ -129,7 +130,7 @@ void showPicker() {
final FragmentActivity activity = activityProvider.getActivity();
if (activity == null) return;

AskPermission.ask(activity, Manifest.permission.READ_EXTERNAL_STORAGE, new AskPermission.Listener() {
AskPermission.Listener listener = new AskPermission.Listener() {
@Override
public void permissionGranted(@NonNull String permission) {
if (activityProvider instanceof Fragment) {
Expand All @@ -152,7 +153,13 @@ public void askRationale(@NonNull AlertDialog.Builder builder) {
builder.setTitle(R.string.readExternalStorageRequest_title)
.setMessage(R.string.readExternalStorageRequest_certMessage);
}
});
};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
listener.permissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE);
return;
}

AskPermission.ask(activity, Manifest.permission.READ_EXTERNAL_STORAGE, listener);
}

void showCertificateDetails(@NonNull X509Certificate certificate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -453,24 +454,30 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup p
enableDirectDownload = layout.findViewById(R.id.editProfile_enableDirectDownload);
enableDirectDownload.setOnCheckedChangeListener((buttonView, isChecked) -> {
container.setVisibility(isChecked ? View.VISIBLE : View.GONE);
if (isChecked && getActivity() != null) {
AskPermission.ask(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, new AskPermission.Listener() {
@Override
public void permissionGranted(@NonNull String permission) {
}

@Override
public void permissionDenied(@NonNull String permission) {
showToast(Toaster.build().message(R.string.writePermissionDenied));
}

@Override
public void askRationale(@NonNull AlertDialog.Builder builder) {
builder.setTitle(R.string.writeExternalStorageRequest_title)
.setMessage(R.string.writeExternalStorageRequest_message);
}
});
if (!isChecked || getActivity() == null) return;

AskPermission.Listener listener = new AskPermission.Listener() {
@Override
public void permissionGranted(@NonNull String permission) {
}

@Override
public void permissionDenied(@NonNull String permission) {
showToast(Toaster.build().message(R.string.writePermissionDenied));
}

@Override
public void askRationale(@NonNull AlertDialog.Builder builder) {
builder.setTitle(R.string.writeExternalStorageRequest_title)
.setMessage(R.string.writeExternalStorageRequest_message);
}
};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
listener.permissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE);
return;
}

AskPermission.ask(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, listener);
});

encryptionContainer = layout.findViewById(R.id.editProfile_dd_encryption);
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/com/gianlu/aria2app/main/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
Expand Down Expand Up @@ -419,7 +420,7 @@ protected void onPostCreate() {
if ("magnet".equals(scheme) || "http".equals(scheme) || "https".equals(scheme) || "ftp".equals(scheme) || "sftp".equals(scheme)) {
processUrl(shareData);
} else {
AskPermission.ask(this, Manifest.permission.READ_EXTERNAL_STORAGE, new AskPermission.Listener() {
AskPermission.Listener listener = new AskPermission.Listener() {
@Override
public void permissionGranted(@NonNull String permission) {
processFileUri(shareData);
Expand All @@ -437,7 +438,13 @@ public void askRationale(@NonNull AlertDialog.Builder builder) {
builder.setTitle(R.string.readExternalStorageRequest_title)
.setMessage(R.string.readExternalStorageRequest_base64Message);
}
});
};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
listener.permissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE);
return;
}

AskPermission.ask(this, Manifest.permission.READ_EXTERNAL_STORAGE, listener);
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/java/com/gianlu/aria2app/options/OptionsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.Manifest;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
Expand Down Expand Up @@ -223,8 +224,7 @@ private void doExportOptions() {

private void export() {
if (getActivity() == null) return;

AskPermission.ask(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, new AskPermission.Listener() {
AskPermission.Listener listener = new AskPermission.Listener() {
@Override
public void permissionGranted(@NonNull String permission) {
doExportOptions();
Expand All @@ -240,7 +240,13 @@ public void askRationale(@NonNull AlertDialog.Builder builder) {
builder.setTitle(R.string.writeExternalStorageRequest_title)
.setMessage(R.string.exportOptionsGrantWrite);
}
});
};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
listener.permissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE);
return;
}

AskPermission.ask(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, listener);
}

@Override
Expand Down

0 comments on commit 17c9a97

Please sign in to comment.