reader-sdk-flutter-plugin icon indicating copy to clipboard operation
reader-sdk-flutter-plugin copied to clipboard

ReaderSdk.initialize breaks definition of platform brightness (aka light or dark theme)

Open TatsuUkraine opened this issue 2 years ago • 40 comments

Describe the issue

Starting from Flutter 3, after ReaderSdk.initialize is invoked, App fails into light theme even if Dark theme is enabled in the system, settings

To Reproduce

  1. Follow the instructions in doc
  2. Ensure that Material Theme has ThemeMode.system
  3. Start the app with Dark theme enabled in the system settings

Expected behavior

App uses Dark theme

Environment (please complete the following information):

  • platform: Android
  • OS and version: 12
  • dev environment: MacOS
  • Reader SDK version: >=1.5.6

Additional context

All works fine with Flutter 2.x. Issue appears only on Android 12 and only if app built with Flutter 3.x Removing ReaderSdk.initialize from onCreate method in Application fixes the issue

TatsuUkraine avatar Jul 12 '22 22:07 TatsuUkraine

@TatsuUkraine Actually It is plugin only we haven't code for dark mode yet. but we definitely ping you when we implement dark mode.

CharmisG avatar Jul 13 '22 10:07 CharmisG

@CharmisG all works fine if app built with flutter 2, issue appears only after we updated flutter to v3 and only on Android 12, on Android 11 all works fine even for app that is built with flutter 3

TatsuUkraine avatar Jul 13 '22 10:07 TatsuUkraine

@CharmisG also, the issue not in reader sdk intents, issue that after intialize method is invoked flutter portion starts receiving light theme through the platform channel always

TatsuUkraine avatar Jul 13 '22 10:07 TatsuUkraine

@TatsuUkraine It was a permission issue above android version 12 so need to give Bluetooth and location permission in plugin of the android folder so feel free to reopen this issue if you are still facing this issue. Thank you !!

CharmisG avatar Aug 03 '22 06:08 CharmisG

@CharmisG I don't have access to reopen the issue! I already tried a variant with requesting Bluetooth permissions before initialize it didn't help.

What specific location permissions should be granted before initialize method is invoked to fix this issue?

TatsuUkraine avatar Aug 03 '22 07:08 TatsuUkraine

@TatsuUkraine Permission to give in android manifest :

<user-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />

<user-permission android:name="android.permission.BLUTOOTH_ADMIN" android:maxSdkVersion="30" />

CharmisG avatar Aug 03 '22 07:08 CharmisG

@CharmisG it doesn't solve the issue, it solves problems with crash on app start, but not with Theme

TatsuUkraine avatar Aug 03 '22 07:08 TatsuUkraine

@TatsuUkraine

Please check below code and add permission in main android manifest and example -> android -> manifest

Screenshot 2022-08-03 at 2 15 13 PM

Screenshot 2022-08-03 at 2 18 37 PM

CharmisG avatar Aug 03 '22 08:08 CharmisG

@CharmisG still doesn't solve the issue

AndroidManifest.xml

    <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />

    <!-- Needed to communicate with already-paired Bluetooth devices. (Android 12 upwards)-->
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

MainActivity.kt

class MainActivity: FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            val sdkPermissions: Array<String> = arrayOf(Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_ADVERTISE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)

            if (sdkPermissions.all { checkSelfPermission(it) == PackageManager.PERMISSION_GRANTED }) {
                ReaderSdk.initialize(application)
                Toast.makeText(this, "Reader SDK initialized", Toast.LENGTH_SHORT).show()
            } else {
                ActivityCompat.requestPermissions(this, sdkPermissions, READER_SDK_PERMISSIONS_CODE)
            }
        } else {
            ReaderSdk.initialize(application)
            Toast.makeText(this, "Reader SDK initialized", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == READER_SDK_PERMISSIONS_CODE && grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
            ReaderSdk.initialize(application)
            Toast.makeText(this, "Reader SDK initialized", Toast.LENGTH_SHORT).show()
            return
        }

        Toast.makeText(this, "Reader SDK not initialized", Toast.LENGTH_SHORT).show()
    }

    companion object {
        private const val READER_SDK_PERMISSIONS_CODE = 100
    }
}

TatsuUkraine avatar Aug 03 '22 09:08 TatsuUkraine

p.s. example project in plugin repo doesn't work with flutter 3

e: /.pub-cache/hosted/pub.dartlang.org/barcode_scan-2.0.2/android/src/main/kotlin/de/mintware/barcode_scan/BarcodeScanPlugin.kt: (19, 1): Class 'BarcodeScanPlugin' is not abstract and does not implement abstract member public abstract fun onRequestPermissionsResult(p0: Int, p1: Array<(out) String!>, p2: IntArray): Boolean defined in io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener
e: /.pub-cache/hosted/pub.dartlang.org/barcode_scan-2.0.2/android/src/main/kotlin/de/mintware/barcode_scan/BarcodeScanPlugin.kt: (57, 9): Smart cast to 'Activity' is impossible, because 'registrar.activity()' is a complex expression
e: /.pub-cache/hosted/pub.dartlang.org/barcode_scan-2.0.2/android/src/main/kotlin/de/mintware/barcode_scan/BarcodeScanPlugin.kt: (67, 36): Type mismatch: inferred type is String? but String was expected
e: /.pub-cache/hosted/pub.dartlang.org/barcode_scan-2.0.2/android/src/main/kotlin/de/mintware/barcode_scan/BarcodeScanPlugin.kt: (76, 47): Type mismatch: inferred type is Activity? but Context was expected
e: /.pub-cache/hosted/pub.dartlang.org/barcode_scan-2.0.2/android/src/main/kotlin/de/mintware/barcode_scan/BarcodeScanPlugin.kt: (81, 43): Type mismatch: inferred type is Activity? but Activity was expected
e: /.pub-cache/hosted/pub.dartlang.org/barcode_scan-2.0.2/android/src/main/kotlin/de/mintware/barcode_scan/BarcodeScanPlugin.kt: (85, 5): 'onRequestPermissionsResult' overrides nothing

TatsuUkraine avatar Aug 03 '22 09:08 TatsuUkraine

if you building the app with flutter 2, there are NO issue with theme (I pointed out this in the issue description)

TatsuUkraine avatar Aug 03 '22 09:08 TatsuUkraine

@TatsuUkraine Sorry for the trouble but I'm also figuring out an issue from my side now I have kept all required permission for android 12 on flutter 3 so can you please check the latest code and let me know the status?

CharmisG avatar Aug 04 '22 04:08 CharmisG

@CharmisG if to follow getting started docs with the most recent changes it doesn't solve anything, app still crashes during the start. Also not quite sure if any of the recent changes can solve issue described here

TatsuUkraine avatar Aug 04 '22 08:08 TatsuUkraine

@TatsuUkraine Sorry for the delay but I'm working on it and I'll update you soon. Thank you !!

CharmisG avatar Aug 16 '22 06:08 CharmisG

@TatsuUkraine Sorry for the delay but we are using the permission_handler plugin and this plugin itself has this permission error that's why it's crashing so we are waiting for this(permission_handler) plugin update and we are continuously touched with this plugin and I'll let you know when we get any response and will update you!

Plugin link: https://pub.dev/packages/permission_handler

Thank you !!

CharmisG avatar Aug 25 '22 07:08 CharmisG

@CharmisG I'm not sure I understand what you mean. App doesn't crashes , theme doesn't work properly (please read ticket description)

TatsuUkraine avatar Aug 25 '22 07:08 TatsuUkraine

@CharmisG also, this plugin doesn't use permission handler plugin

TatsuUkraine avatar Aug 25 '22 07:08 TatsuUkraine

@TatsuUkraine Can you please share video? How your app is behaving in flutter 3

CharmisG avatar Aug 25 '22 07:08 CharmisG

@CharmisG not sure that I'm following what kind of video you need to see? flutter always chooses a light theme even when ThemeMode is system and the dark mode is enabled on the Android device

TatsuUkraine avatar Aug 25 '22 12:08 TatsuUkraine

@TatsuUkraine PR is merged please check in the latest code. Thank you foe help !!!

CharmisG avatar Sep 01 '22 07:09 CharmisG

@CharmisG it's not how it works! you need to publish the most recent version to pub.dev to make it available for everyone!

TatsuUkraine avatar Sep 01 '22 07:09 TatsuUkraine

also, these changes doesn't solve this issue, please repopen the PR! definition of light/dark theme still broken on android devices even with most up to date Android Reader SDK version

TatsuUkraine avatar Sep 01 '22 07:09 TatsuUkraine

@hukid

TatsuUkraine avatar Sep 01 '22 07:09 TatsuUkraine

@TatsuUkraine We'll publish the new plugin soon. Thanks a lot, @TatsuUkraine!

hukid avatar Sep 01 '22 17:09 hukid

@hukid I tried all this updates already, and issue described in this ticket is not solved by these changes

TatsuUkraine avatar Sep 01 '22 18:09 TatsuUkraine

@TatsuUkraine, re-opened this issue.

hukid avatar Sep 01 '22 18:09 hukid

@TatsuUkraine while we are investigating, if you see a fix, feel free to send the PR.

hukid avatar Sep 01 '22 18:09 hukid

@hukid I tried to find a reason, but no luck so far(( I started to think that it may be somewhere deep in Android Reader SDK, where I don't have access(

TatsuUkraine avatar Sep 01 '22 18:09 TatsuUkraine

I actually feel this is a bug in Flutter 3 given the issues doesn't exist in Flutter 2.

hukid avatar Sep 01 '22 21:09 hukid

@hukid could be, but I'm not quite sure since native layer starts sending wrong theme only after ReaderSdk.intialize method is invoked and on androids 12 and 13. On 11 all works fine

TatsuUkraine avatar Sep 01 '22 22:09 TatsuUkraine