Skip to content

Correctly handle supported image formats on Android APIs < 23 #622

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

Merged
merged 3 commits into from
Sep 25, 2023

Conversation

okarmazin
Copy link
Contributor

@okarmazin okarmazin commented Sep 22, 2023

The library declares minSdk 21, but BarcodeReader accesses ImageFormat constants that were added in API 23. This PR guards the access to these constants with a version check.

Also, the explicit API mode was enabled for the Kotlin compiler. This flag instructs the compiler to enforce visibility modifiers and return types be explicitly specified (if they would be public by default).

@okarmazin okarmazin changed the title Correctly handle supported image formats of Android APIs < 23 Correctly handle supported image formats on Android APIs < 23 Sep 22, 2023
@okarmazin okarmazin force-pushed the android-api23-fix branch 2 times, most recently from e19a7c8 to 5dd9c8a Compare September 23, 2023 06:37
@okarmazin okarmazin marked this pull request as ready for review September 23, 2023 06:52
@axxel
Copy link
Collaborator

axxel commented Sep 24, 2023

Just to make I understand the situation regarding the explicit mode: Everything in a kotlin class is public unless specified private, right? So if there is no private visibility modifier, the symbol is (explicitly) public. What is there to gain when littering the code with a bunch of public strings? Preventing an ignorant developer from accidentally adding public symbols to the API?

@okarmazin
Copy link
Contributor Author

okarmazin commented Sep 25, 2023

Yes, unfortunately Kotlin visibility is implicitly public by default, unless specified otherwise.

Explicit API mode helps prevent two errors:

  1. Makes it harder to accidentally publish implementation details in your public API. The requirement to specify visibility makes you think about your symbols and if they are actually supposed to be accessible by library consumers.

Note: Kotlin also has internal modifier that makes the element accessible within the same compilation unit (Gradle project) but not by its consumers. It's somewhat akin to Java package-only visibility, except enforced by the compiler.

  1. Prevents type inference from changing your return types by mistake. Explicit API mode requires that return types be specified explicitly instead of relying on type inference. It's the same as if you disallowed auto in public API in C++.

Take the following example from BarcodeReader:

public fun read(bitmap: Bitmap, cropRect: Rect = Rect(), rotation: Int = 0): Result? {
    return read(bitmap, options, cropRect, rotation)
}

I could also write the function in a single-expression format. Without Explicit API mode I could omit the return type:

public fun read(bitmap: Bitmap, cropRect: Rect = Rect(), rotation: Int = 0) = read(bitmap, options, cropRect, rotation)

Explicit API mode prevents that:

public fun read(bitmap: Bitmap, cropRect: Rect = Rect(), rotation: Int = 0): Result? = read(bitmap, options, cropRect, rotation)

@axxel
Copy link
Collaborator

axxel commented Sep 25, 2023

As you can possibly infer from my choice of words in my last comment, I'm not a fan of 'unnecessary' verbosity but I'm also not particularly emotional about the Kotlin code base in general and you clearly have more knowledge and experience here... Thanks for taking the time to engage in the discussion.

@axxel axxel merged commit a6a181f into zxing-cpp:master Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy