cph-cachet / flutter-plugins

A collection of Flutter plugins developed by CACHET
528 stars 628 forks source link

[app_usage-1.0.1] Missing FLAG_ACTIVITY_NEW_TASK to redirect user to app usage permission #221

Closed koenniem closed 3 years ago

koenniem commented 3 years ago

Device / Emulator and OS

Please complete the following information for each phone and/or emulator you're experiencing this bug on:

Describe the bug

When running the CAMS example app for the first time, permissions have to be given including the one for app usage. However, the user does not get redirected to the app usage permission screen, leaving the permission disabled and thus also the sensor disabled.

To Reproduce

Run CAMS example app with app usage enabled.

Expected behavior

Redirection to the app usage permission.

Actual behavior

No redirection. Error in log:

E/MethodChannel#app_usage(14679): Failed to handle method call E/MethodChannel#app_usage(14679): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? E/MethodChannel#app_usage(14679): at android.app.ContextImpl.startActivity(ContextImpl.java:1018) E/MethodChannel#app_usage(14679): at android.app.ContextImpl.startActivity(ContextImpl.java:994) E/MethodChannel#app_usage(14679): at android.content.ContextWrapper.startActivity(ContextWrapper.java:403) E/MethodChannel#app_usage(14679): at dk.cachet.app_usage.AppUsagePlugin.onMethodCall(AppUsagePlugin.kt:37) E/MethodChannel#app_usage(14679): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233) E/MethodChannel#app_usage(14679): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85) E/MethodChannel#app_usage(14679): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692) E/MethodChannel#app_usage(14679): at android.os.MessageQueue.nativePollOnce(Native Method) E/MethodChannel#app_usage(14679): at android.os.MessageQueue.next(MessageQueue.java:335) E/MethodChannel#app_usage(14679): at android.os.Looper.loop(Looper.java:183) E/MethodChannel#app_usage(14679): at android.app.ActivityThread.main(ActivityThread.java:7656) E/MethodChannel#app_usage(14679): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#app_usage(14679): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) E/MethodChannel#app_usage(14679): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Proposed solution

In AppUsagePlugin.kt:35 change

context.startActivity(Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS))

to

val intent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK context.startActivity(intent)

thomasnilsson commented 3 years ago

Fixed in version 1.0.2