Skip to content

Commit

Permalink
Merge pull request #8 from joutvhu/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
joutvhu authored Apr 26, 2023
2 parents 60b8ac0 + 124d749 commit 62de9b8
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.4.1

* Fix issue on Android 13

## 3.4.0

* Return message "Need to declare android.permission.REQUEST_INSTALL_PACKAGES to call this api" when open APK
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ OpenFile.open("/sdcard/example.txt");
## Support
### android
### Android
```
{
Expand Down Expand Up @@ -101,9 +101,17 @@ OpenFile.open("/sdcard/example.txt");
{".zip", "application/x-zip-compressed"},
{"", "*/*"}
}

```

You must specify additional permissions if you want to open the respective file types below

| Type of media | Permission to request | Android version |
|:-----------------:|:-------------------------------------------:|:---------------:|
| APK files | android.permission.REQUEST_INSTALL_PACKAGES | - |
| Images and photos | android.permission.READ_MEDIA_IMAGES | 13+ |
| Videos | android.permission.READ_MEDIA_VIDEO | 13+ |
| Audio files | android.permission.READ_MEDIA_AUDIO | 13+ |

when Conflict with other plugins about FileProvider, add code below in your /android/app/src/main/AndroidManifest.xml

```xml
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootProject.allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion 31
compileSdk 33

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand Down
3 changes: 2 additions & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.joutvhu.openfile">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />

<application>
<provider android:name="com.joutvhu.openfile.FileProvider"
Expand Down
46 changes: 40 additions & 6 deletions android/src/main/java/com/joutvhu/openfile/OpenFilePlusPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class OpenFilePlusPlugin implements FlutterPlugin, MethodCallHandler, Act
PluginRegistry.RequestPermissionsResultListener, PluginRegistry.ActivityResultListener {
private static final int REQUEST_CODE = 33432;
private static final int RESULT_CODE = 0x12;
private static final String TYPE_PREFIX_IMAGE = "image/";
private static final String TYPE_PREFIX_VIDEO = "video/";
private static final String TYPE_PREFIX_AUDIO = "audio/";
private static final String TYPE_STRING_APK = "application/vnd.android.package-archive";

/// The MethodChannel that will the communication between Flutter and native Android
Expand Down Expand Up @@ -94,19 +97,19 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (!isFileAvailable()) {
return;
}
if (!isMediaStorePath() && !Environment.isExternalStorageManager()) {
result(-3, "Permission denied: android.Manifest.permission.MANAGE_EXTERNAL_STORAGE");
return;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
if (!isMediaStorePath() && !Environment.isExternalStorageManager()) {
result(-3, "Permission denied: android.Manifest.permission.MANAGE_EXTERNAL_STORAGE");
return;
}
}
}
if (hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
if (checkPermissions()) {
if (TYPE_STRING_APK.equals(typeString)) {
openApkFile();
return;
}
startActivity();
} else {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE);
}
} else {
startActivity();
Expand All @@ -117,6 +120,37 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
}
}

private boolean checkPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (typeStartsWith(TYPE_PREFIX_IMAGE, typeString)) {
if (!hasPermission(Manifest.permission.READ_MEDIA_IMAGES)) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_MEDIA_IMAGES}, REQUEST_CODE);
return false;
}
} else if (typeStartsWith(TYPE_PREFIX_VIDEO, typeString)) {
if (!hasPermission(Manifest.permission.READ_MEDIA_VIDEO)) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_MEDIA_VIDEO}, REQUEST_CODE);
return false;
}
} else if (typeStartsWith(TYPE_PREFIX_AUDIO, typeString)) {
if (!hasPermission(Manifest.permission.READ_MEDIA_AUDIO)) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_MEDIA_AUDIO}, REQUEST_CODE);
return false;
}
}
} else {
if (!hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE);
return false;
}
}
return true;
}

private boolean typeStartsWith(String prefix, String fileType) {
return fileType != null && fileType.startsWith(prefix);
}

private boolean hasPermission(String permission) {
return ContextCompat.checkSelfPermission(activity, permission) == PermissionChecker.PERMISSION_GRANTED;
}
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ packages:
path: ".."
relative: true
source: path
version: "3.4.0"
version: "3.4.1"
path:
dependency: transitive
description:
Expand Down
8 changes: 7 additions & 1 deletion lib/src/common/parse_args.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
String parseArgs(List<String> args) {
final commandList = args.map((arg) => arg.replaceAll(' ', '\\ ')).toList();
final commandList = args.map((arg) =>
arg
.replaceAll(' ', '\\ ')
.replaceAll('(', '\\(')
.replaceAll(')', '\\)'))
.toList();

return commandList.join(' ');
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: open_file_plus
description: A plug-in that can call native APP to open files with string result in flutter, support iOS(UTI) / android(intent) / PC(ffi) / web(dart:html)
version: 3.4.0
version: 3.4.1

homepage: https://github.com/joutvhu/open_file_plus
repository: https://github.com/joutvhu/open_file_plus.git
Expand Down

0 comments on commit 62de9b8

Please sign in to comment.