Skip to content

Commit

Permalink
fix: ignore permission check no work on android (#1028)
Browse files Browse the repository at this point in the history
- Support set JDK version (Upgrade build.gradle)

Signed-off-by: CaiJingLong <[email protected]>
  • Loading branch information
CaiJingLong authored Nov 13, 2023
1 parent c3ba207 commit ef1a558
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/check-compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
push:
branches:
- main
- 2.x

jobs:
build-for-android:
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ that can be found in the LICENSE file. -->

# CHANGELOG

## 2.8.1

### Feature

Fix:

- Upgrade android/build.gradle to load current java version from some environment variables.
- Fix the `setIgnorePermissionCheck` method not working on Android.

## 2.8.0

### Feature
Expand Down
85 changes: 85 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.util.regex.Matcher
import java.util.regex.Pattern

group 'com.fluttercandies.photo_manager'
version '1.0-SNAPSHOT'

Expand All @@ -22,6 +25,80 @@ rootProject.allprojects {

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
/**
* Get java version from JAVA_HOME
* @param path JAVA_HOME path
* @return java version
*/
static JavaVersion getJavaFromHome(String path) {
def javaExe = path + "/bin/java"
Process process = Runtime.getRuntime().exec(new String[]{javaExe, "-version"})
process.waitFor()

def input = process.getErrorStream() ?: process.getInputStream()
def reader = new BufferedReader(new InputStreamReader(input))
def version = reader.readLine()
reader.close()

String regex = "\"(.+)\""
Pattern pattern = Pattern.compile(regex)

// 创建 Matcher 对象
Matcher matcher = pattern.matcher(version)

if (matcher.find()) {
String v = matcher.group(1)

if (v.startsWith("1.")) {
// just save the major and minor version
v = v.substring(0, 3)
}

return JavaVersion.toVersion(v)
}
}

JavaVersion getJavaVersion() {
// 0. Get java.version property from project's gradle.properties
// The version value is like 1.8, 11, 17, 21, etc.
// Also see: https://docs.gradle.org/current/javadoc/org/gradle/api/JavaVersion.html#toVersion-java.lang.Object-
JavaVersion res

def javaVersion = project.findProperty("java.version")
if (javaVersion != null) {
res = JavaVersion.toVersion(javaVersion)
println("Get java version from project's gradle.properties: $javaVersion")
}

String javaHome
// 1. read from JAVA_HOME environment variable
javaHome = System.getenv("JAVA_HOME")
if (res == null && javaHome != null) {
res = getJavaFromHome(javaHome)
println("Get java version from JAVA_HOME: $javaHome")
}

// 2. read gradle.properties
javaHome = project.findProperty("java.home")
if (res == null && javaHome != null) {
res = getJavaFromHome(javaHome)
println("Get java version from gradle.properties: $javaHome")
}

// 3. read from property with java.home
javaHome = System.getProperty("java.home")
if (res == null && javaHome != null) {
res = getJavaFromHome(javaHome)
println("Get java version from java.home: $javaHome")
}

if (res != null) {
return res
}

// last, use default version with current
return JavaVersion.current()
}

android {
if (project.android.hasProperty('namespace') ||
Expand All @@ -31,6 +108,14 @@ android {

compileSdkVersion 34

def javaVersion = getJavaVersion()
println("The photo_manager java version is ${javaVersion}")

compileOptions {
sourceCompatibility javaVersion
targetCompatibility javaVersion
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,6 @@ class PhotoManagerPlugin(
val type = call.argument<Int>("type")!!
permissionsUtils.presentLimited(type, resultHandler)
}

Methods.ignorePermissionCheck -> {
val ignore = call.argument<Boolean>("ignore")!!
ignorePermissionCheck = ignore
resultHandler.reply(ignore)
}
}

}
Expand Down Expand Up @@ -319,6 +313,12 @@ class PhotoManagerPlugin(
// The plugin will not hold instances cache on Android.
resultHandler.reply(1)
}

Methods.ignorePermissionCheck -> {
val ignore = call.argument<Boolean>("ignore")!!
ignorePermissionCheck = ignore
resultHandler.reply(ignore)
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions example/lib/page/developer/issues_page/issue_1025.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:flutter/cupertino.dart';
import 'package:photo_manager/photo_manager.dart';

import 'issue_index_page.dart';

class Issue1025Page extends StatefulWidget {
const Issue1025Page({Key? key}) : super(key: key);

@override
State<Issue1025Page> createState() => _Issue1025PageState();
}

class _Issue1025PageState extends State<Issue1025Page>
with IssueBase<Issue1025Page> {
String log = '';

@override
Widget build(BuildContext context) {
return buildScaffold(
[
buildButton('Test for ignore permission', _testForIgnorePermission),
Expanded(child: Text(log)),
],
);
}

@override
int get issueNumber => 1025;

Future<void> _testForIgnorePermission() async {
await PhotoManager.setIgnorePermissionCheck(true);
setState(() {
log = 'setIgnorePermissionCheck(true) success' '\n$log';
});
}
}
16 changes: 15 additions & 1 deletion example/lib/page/developer/issues_page/issue_index_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:photo_manager_example/widget/nav_column.dart';
import 'package:photo_manager_example/widget/theme_button.dart';
import 'package:url_launcher/url_launcher.dart';

import 'issue_1025.dart';
import 'issue_734.dart';
import 'issue_918.dart';
import 'issue_962.dart';
Expand All @@ -25,6 +26,7 @@ class IssuePage extends StatelessWidget {
Issue734Page(),
Issue918Page(),
Issue962(),
Issue1025Page(),
],
),
);
Expand All @@ -42,7 +44,19 @@ mixin IssueBase<T extends StatefulWidget> on State<T> {
icon: const Icon(Icons.info),
onPressed: () {
Clipboard.setData(ClipboardData(text: issueUrl));
showToast('The issue of $issueNumber was been copied.');
showToastWidget(Material(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
decoration: BoxDecoration(
color: Colors.black87,
borderRadius: BorderRadius.circular(8),
),
child: Text(
'The issue of $issueNumber was been copied.',
style: const TextStyle(color: Colors.white),
),
),
));
},
tooltip: 'Copy issue url to clipboard.',
);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: photo_manager
description: A Flutter plugin that provides assets abstraction management APIs on Android, iOS, and macOS.
repository: https://github.com/fluttercandies/flutter_photo_manager
version: 2.8.0
version: 2.8.1

environment:
sdk: ">=2.13.0 <3.0.0"
Expand Down

0 comments on commit ef1a558

Please sign in to comment.