From ef1a558a03a9063dc3d962d348b735fd84b584d3 Mon Sep 17 00:00:00 2001 From: Caijinglong Date: Mon, 13 Nov 2023 10:12:17 +0800 Subject: [PATCH] fix: ignore permission check no work on android (#1028) - Support set JDK version (Upgrade build.gradle) Signed-off-by: CaiJingLong --- .github/workflows/check-compatibility.yml | 1 + CHANGELOG.md | 9 ++ android/build.gradle | 85 +++++++++++++++++++ .../photo_manager/core/PhotoManagerPlugin.kt | 12 +-- .../developer/issues_page/issue_1025.dart | 36 ++++++++ .../issues_page/issue_index_page.dart | 16 +++- pubspec.yaml | 2 +- 7 files changed, 153 insertions(+), 8 deletions(-) create mode 100644 example/lib/page/developer/issues_page/issue_1025.dart diff --git a/.github/workflows/check-compatibility.yml b/.github/workflows/check-compatibility.yml index 4f8c22c4..8035bc0d 100644 --- a/.github/workflows/check-compatibility.yml +++ b/.github/workflows/check-compatibility.yml @@ -7,6 +7,7 @@ on: push: branches: - main + - 2.x jobs: build-for-android: diff --git a/CHANGELOG.md b/CHANGELOG.md index 66c95c46..5270b2d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/android/build.gradle b/android/build.gradle index bbe8fd67..2d255ce5 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,3 +1,6 @@ +import java.util.regex.Matcher +import java.util.regex.Pattern + group 'com.fluttercandies.photo_manager' version '1.0-SNAPSHOT' @@ -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') || @@ -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' } diff --git a/android/src/main/kotlin/com/fluttercandies/photo_manager/core/PhotoManagerPlugin.kt b/android/src/main/kotlin/com/fluttercandies/photo_manager/core/PhotoManagerPlugin.kt index 6d5d0d4c..33daa28c 100644 --- a/android/src/main/kotlin/com/fluttercandies/photo_manager/core/PhotoManagerPlugin.kt +++ b/android/src/main/kotlin/com/fluttercandies/photo_manager/core/PhotoManagerPlugin.kt @@ -256,12 +256,6 @@ class PhotoManagerPlugin( val type = call.argument("type")!! permissionsUtils.presentLimited(type, resultHandler) } - - Methods.ignorePermissionCheck -> { - val ignore = call.argument("ignore")!! - ignorePermissionCheck = ignore - resultHandler.reply(ignore) - } } } @@ -319,6 +313,12 @@ class PhotoManagerPlugin( // The plugin will not hold instances cache on Android. resultHandler.reply(1) } + + Methods.ignorePermissionCheck -> { + val ignore = call.argument("ignore")!! + ignorePermissionCheck = ignore + resultHandler.reply(ignore) + } } } diff --git a/example/lib/page/developer/issues_page/issue_1025.dart b/example/lib/page/developer/issues_page/issue_1025.dart new file mode 100644 index 00000000..c7381037 --- /dev/null +++ b/example/lib/page/developer/issues_page/issue_1025.dart @@ -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 createState() => _Issue1025PageState(); +} + +class _Issue1025PageState extends State + with IssueBase { + 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 _testForIgnorePermission() async { + await PhotoManager.setIgnorePermissionCheck(true); + setState(() { + log = 'setIgnorePermissionCheck(true) success' '\n$log'; + }); + } +} diff --git a/example/lib/page/developer/issues_page/issue_index_page.dart b/example/lib/page/developer/issues_page/issue_index_page.dart index af5f4a50..c746a908 100644 --- a/example/lib/page/developer/issues_page/issue_index_page.dart +++ b/example/lib/page/developer/issues_page/issue_index_page.dart @@ -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'; @@ -25,6 +26,7 @@ class IssuePage extends StatelessWidget { Issue734Page(), Issue918Page(), Issue962(), + Issue1025Page(), ], ), ); @@ -42,7 +44,19 @@ mixin IssueBase on State { 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.', ); diff --git a/pubspec.yaml b/pubspec.yaml index c5cea1ed..1d630312 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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"