Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android 13 permissions fix #236

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" android:minSdkVersion="33" />
</config-file>

<source-file src="src/android/PhotoViewer.java" target-dir="src/com/sarriaroman/PhotoViewer" />
Expand Down
23 changes: 18 additions & 5 deletions src/android/PhotoViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
Expand All @@ -22,6 +23,7 @@ public class PhotoViewer extends CordovaPlugin {

public static final String WRITE = Manifest.permission.WRITE_EXTERNAL_STORAGE;
public static final String READ = Manifest.permission.READ_EXTERNAL_STORAGE;
public static final String READ_IMAGES = Manifest.permission.READ_MEDIA_IMAGES;

public static final int REQ_CODE = 0;

Expand All @@ -33,19 +35,30 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
if (action.equals("show")) {
this.args = args;
this.callbackContext = callbackContext;

if (cordova.hasPermission(READ) && cordova.hasPermission(WRITE)) {
this.launchActivity();
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (cordova.hasPermission(READ_IMAGES)) {
this.launchActivity();
} else {
this.getPermission();
}
} else {
this.getPermission();
if (cordova.hasPermission(READ) && cordova.hasPermission(WRITE)) {
this.launchActivity();
} else {
this.getPermission();
}
}
return true;
}
return false;
}

protected void getPermission() {
cordova.requestPermissions(this, REQ_CODE, new String[]{WRITE, READ});
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
cordova.requestPermissions(this, REQ_CODE, new String[]{READ_IMAGES});
} else {
cordova.requestPermissions(this, REQ_CODE, new String[]{WRITE, READ});
}
}

//
Expand Down