Skip to content
Merged
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
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
## 2.5.1
## 2.6.0
### Android
* Made HMS ML Kit dependencies optional using `compileOnly` and dynamic reflection to resolve `allowBackup` manifest merger conflicts (fixes #144).
* Prevented bundling of Huawei HMS libraries in the final APK by default, reducing build sizes for non-HMS projects.
* Removed HMS (Huawei Mobile Services) support entirely to ensure 16 KB page-size compatibility on Android 15+ (fixes #146).
* Replaced deprecated `getParcelable(key)` with type-safe `androidx.core.os.BundleCompat.getParcelable` for Android 13+ compatibility.
* Migrated the legacy `android` block to the modern `configure<LibraryExtension>` block and updated conditional Kotlin plugin application in `build.gradle.kts` to resolve build/deprecation warnings.
* Removed redundant and deprecated `sourceSets` block, as Kotlin source directories are resolved automatically by Gradle.

## 2.5.0
### General
Expand Down
23 changes: 4 additions & 19 deletions android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import com.android.build.api.dsl.LibraryExtension

group = "biz.cunning.cunning_document_scanner"
version = "1.0-SNAPSHOT"
Expand All @@ -9,18 +10,10 @@ plugins {
val agpMajor = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.substringBefore('.').toInt()

if (agpMajor < 9) {
apply(plugin = "org.jetbrains.kotlin.android")
pluginManager.apply("org.jetbrains.kotlin.android")
}

rootProject.allprojects {
repositories {
maven {
url = uri("https://developer.huawei.com/repo/")
}
}
}

android {
configure<LibraryExtension> {
namespace = "biz.cunning.cunning_document_scanner"
compileSdk = 34

Expand All @@ -32,23 +25,15 @@ android {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

sourceSets {
getByName("main") {
java.srcDir("src/main/kotlin")
}
}
}
plugins.withId("org.jetbrains.kotlin.android") {
extensions.configure<org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension> {
compilerOptions {
jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17)
jvmTarget.set(JvmTarget.JVM_17)
}
}
}

dependencies {
implementation("com.google.android.gms:play-services-mlkit-document-scanner:16.0.0")
compileOnly("com.huawei.hms:ml-computer-vision-documentskew:3.11.0.301")
compileOnly("com.huawei.hms:ml-computer-vision-documentskew-model:3.7.0.301")
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import androidx.core.app.ActivityCompat
import biz.cunning.cunning_document_scanner.fallback.DocumentScannerActivity
import biz.cunning.cunning_document_scanner.fallback.constants.DocumentScannerExtra
import biz.cunning.cunning_document_scanner.fallback.utils.FileUtil
import java.io.File
import com.google.mlkit.common.MlKitException
import com.google.mlkit.vision.documentscanner.GmsDocumentScannerOptions
import com.google.mlkit.vision.documentscanner.GmsDocumentScannerOptions.RESULT_FORMAT_JPEG
import com.google.mlkit.vision.documentscanner.GmsDocumentScannerOptions.RESULT_FORMAT_PDF
Expand Down Expand Up @@ -92,7 +90,7 @@ class CunningDocumentScannerPlugin : FlutterPlugin, MethodCallHandler, ActivityA
}
try {
ActivityCompat.startActivityForResult(activity, intent, START_GALLERY_PICKER, null)
} catch (e: ActivityNotFoundException) {
} catch (_: ActivityNotFoundException) {
pendingResult?.error("ERROR", "Failed to start gallery picker", null)
}
}
Expand Down Expand Up @@ -153,10 +151,14 @@ class CunningDocumentScannerPlugin : FlutterPlugin, MethodCallHandler, ActivityA
if (error != null) {
pendingResult?.error("ERROR", "error - $error", null)
} else {
// get an array with scanned document file paths
val scanningResult: GmsDocumentScanningResult =
data?.extras?.getParcelable("extra_scanning_result")
?: return@ActivityResultListener false
// get an array with scanned document file paths
val scanningResult = data?.extras?.let { extras ->
androidx.core.os.BundleCompat.getParcelable(
extras,
"extra_scanning_result",
GmsDocumentScanningResult::class.java
)
} ?: return@ActivityResultListener false

if (asPdf) {
val pdfUri = scanningResult.pdf?.uri?.toString()?.removePrefix("file://")
Expand Down Expand Up @@ -281,31 +283,7 @@ class CunningDocumentScannerPlugin : FlutterPlugin, MethodCallHandler, ActivityA
}
}

private fun isHmsAvailable(context: android.content.Context): Boolean {
return try {
context.packageManager.getPackageInfo("com.huawei.hwid", 0)
true
} catch (e: android.content.pm.PackageManager.NameNotFoundException) {
false
}
}

private fun startScan(noOfPages: Int, isGalleryImportAllowed: Boolean, scannerMode: Int, asPdf: Boolean) {
if (isHmsAvailable(activity)) {
val intent = createDocumentScanIntent(noOfPages)
try {
ActivityCompat.startActivityForResult(
this.activity,
intent,
START_DOCUMENT_FB_ACTIVITY,
null
)
} catch (e: ActivityNotFoundException) {
pendingResult?.error("ERROR", "FAILED TO START ACTIVITY", null)
}
return
}

val optionsBuilder = GmsDocumentScannerOptions.Builder()
.setGalleryImportAllowed(isGalleryImportAllowed)
.setPageLimit(noOfPages)
Expand All @@ -318,31 +296,34 @@ class CunningDocumentScannerPlugin : FlutterPlugin, MethodCallHandler, ActivityA
}

val options = optionsBuilder.build()
val scanner = GmsDocumentScanning.getClient(options)
scanner.getStartScanIntent(activity).addOnSuccessListener {
try {
// Use a custom request code for onActivityResult identification
activity.startIntentSenderForResult(it, START_DOCUMENT_ACTIVITY, null, 0, 0, 0)

} catch (e: IntentSender.SendIntentException) {
pendingResult?.error("ERROR", "Failed to start document scanner", null)
}
}.addOnFailureListener {
if (it is MlKitException) {
val intent = createDocumentScanIntent(noOfPages)
try {
val scanner = GmsDocumentScanning.getClient(options)
scanner.getStartScanIntent(activity).addOnSuccessListener {
try {
ActivityCompat.startActivityForResult(
this.activity,
intent,
START_DOCUMENT_FB_ACTIVITY,
null
)
} catch (e: ActivityNotFoundException) {
pendingResult?.error("ERROR", "FAILED TO START ACTIVITY", null)
// Use a custom request code for onActivityResult identification
activity.startIntentSenderForResult(it, START_DOCUMENT_ACTIVITY, null, 0, 0, 0)
} catch (_: IntentSender.SendIntentException) {
pendingResult?.error("ERROR", "Failed to start document scanner", null)
}
} else {
pendingResult?.error("ERROR", "Failed to start document scanner Intent", null)
}.addOnFailureListener {
openFallbackScanner(noOfPages)
}
} catch (_: Exception) {
openFallbackScanner(noOfPages)
}
}

private fun openFallbackScanner(noOfPages: Int) {
val intent = createDocumentScanIntent(noOfPages)
try {
ActivityCompat.startActivityForResult(
this.activity,
intent,
START_DOCUMENT_FB_ACTIVITY,
null
)
} catch (_: ActivityNotFoundException) {
pendingResult?.error("ERROR", "FAILED TO START ACTIVITY", null)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class DocumentScannerActivity : AppCompatActivity() {
return@CameraUtil
}

// get document corners asynchronously using HMS if available, or fallback to default corners
// get default corners for cropping
detectCorners(photo) { corners ->
document = Document(originalPhotoPath, photo.width, photo.height, corners)

Expand Down Expand Up @@ -246,41 +246,7 @@ class DocumentScannerActivity : AppCompatActivity() {
}
}

private fun isHmsAvailable(): Boolean {
return try {
packageManager.getPackageInfo("com.huawei.hwid", 0)
true
} catch (e: PackageManager.NameNotFoundException) {
false
}
}

private fun isHmsLibraryAvailable(): Boolean {
return try {
Class.forName("com.huawei.hms.mlsdk.dsc.MLDocumentSkewCorrectionAnalyzerFactory")
true
} catch (e: ClassNotFoundException) {
false
}
}

private fun detectCorners(photo: Bitmap, onComplete: (Quad) -> Unit) {
if (isHmsAvailable() && isHmsLibraryAvailable()) {
try {
val detectorClass = Class.forName("biz.cunning.cunning_document_scanner.fallback.HmsEdgeDetector")
val detector = detectorClass.getDeclaredConstructor().newInstance() as EdgeDetector
detector.detect(photo) { detectedQuad ->
if (detectedQuad != null) {
onComplete(detectedQuad)
} else {
onComplete(getDefaultCorners(photo))
}
}
return
} catch (e: Exception) {
android.util.Log.e("DocumentScannerActivity", "Error loading HmsEdgeDetector dynamically", e)
}
}
onComplete(getDefaultCorners(photo))
}

Expand Down

This file was deleted.

6 changes: 5 additions & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.6.1-bin.zip
networkTimeout=10000
retries=0
retryBackOffMs=500
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.5-all.zip
4 changes: 1 addition & 3 deletions example/android/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import java.util.Properties

pluginManagement {
val flutterSdkPath =
java.util.Properties().run {
Expand All @@ -19,7 +17,7 @@ pluginManagement {

plugins {
id("dev.flutter.flutter-plugin-loader") version "1.0.0"
id("com.android.application") version "8.13.1" apply false
id("com.android.application") version "9.2.1" apply false
id("org.jetbrains.kotlin.android") version "2.2.21" apply false
id("org.gradle.toolchains.foojay-resolver-convention") version "0.10.0"
}
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.5.0"
version: "2.5.1"
cupertino_icons:
dependency: "direct main"
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: cunning_document_scanner
description: A document scanner plugin for flutter. Scan and crop automatically on iOS and Android.
version: 2.5.1
version: 2.6.0
homepage: https://cunning.biz
repository: https://github.com/jachzen/cunning_document_scanner

Expand Down