-
Notifications
You must be signed in to change notification settings - Fork 22
Null Safety, Better Camera Permissions, Updated Flutter Embedding #20
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
Open
jeffmikels
wants to merge
5
commits into
g123k:master
Choose a base branch
from
jeffmikels:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
183dc7e
we do not need the camera permissions to operate the flashlight
jeffmikels aa28a7e
Updated README.md regarding crashes on old devices
jeffmikels d9b18d0
fixed kotlin error for onDetachedFromActivity
jeffmikels 5e1a08c
Merge branch 'master' of github.com:jeffmikels/flutter_torch_compat
jeffmikels 9707573
fixed kotlin error for onDetachedFromActivity
jeffmikels File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| org.gradle.jvmargs=-Xmx1536M | ||
|
|
||
| android.useAndroidX=true | ||
| android.enableJetifier=true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,3 @@ | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| package="fr.g123k.torch_compat"> | ||
|
|
||
| <uses-permission android:name="android.permission.CAMERA" /> | ||
| <uses-permission android:name="android.permission.FLASHLIGHT" /> | ||
| <uses-feature android:name="android.hardware.camera" /> | ||
| <uses-feature android:name="android.hardware.camera.autofocus" /> | ||
| <uses-feature android:name="android.hardware.camera.flash" /> | ||
|
|
||
| package="fr.g123k.torch_compat"> | ||
| </manifest> |
142 changes: 92 additions & 50 deletions
142
android/src/main/kotlin/fr/g123k/torch_compat/TorchCompatPlugin.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,67 +1,109 @@ | ||
| package fr.g123k.torch_compat | ||
|
|
||
| import android.app.Activity | ||
| import android.content.Context | ||
| import android.content.pm.PackageManager | ||
| import android.os.Build | ||
| import fr.g123k.torch_compat.impl.BaseTorch | ||
| import fr.g123k.torch_compat.impl.TorchCamera2Impl | ||
| import fr.g123k.torch_compat.impl.TorchCamera1Impl | ||
| import fr.g123k.torch_compat.utils.ActivityLifecycleCallbacks | ||
| import androidx.annotation.NonNull | ||
|
|
||
| import io.flutter.embedding.engine.plugins.FlutterPlugin | ||
| import io.flutter.plugin.common.MethodCall | ||
| import io.flutter.plugin.common.MethodChannel | ||
| import io.flutter.plugin.common.MethodChannel.MethodCallHandler | ||
| import io.flutter.plugin.common.MethodChannel.Result | ||
| import io.flutter.plugin.common.PluginRegistry.Registrar | ||
|
|
||
| @Suppress("JoinDeclarationAndAssignment") | ||
| class TorchCompatPlugin(activity: Activity) : MethodCallHandler { | ||
| import fr.g123k.torch_compat.impl.BaseTorch | ||
| import fr.g123k.torch_compat.impl.TorchCamera2Impl | ||
| import fr.g123k.torch_compat.impl.TorchCamera1Impl | ||
|
|
||
| import io.flutter.embedding.engine.plugins.activity.ActivityAware | ||
| import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding | ||
|
|
||
| private val hasLamp = activity.applicationContext.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) | ||
| private val torchImpl: BaseTorch | ||
|
|
||
| init { | ||
| torchImpl = when { | ||
| Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> TorchCamera2Impl(activity) | ||
| else -> TorchCamera1Impl(activity) | ||
| } | ||
| /** TorchCompatPlugin */ | ||
| class TorchCompatPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { | ||
| /// The MethodChannel that will the communication between Flutter and native Android | ||
| /// | ||
| /// This local reference serves to register the plugin with the Flutter Engine and unregister it | ||
| /// when the Flutter Engine is detached from the Activity | ||
| private lateinit var channel : MethodChannel | ||
|
|
||
| activity.application.registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks() { | ||
| override fun onActivityStopped(activity: Activity?) { | ||
| torchImpl.dispose() | ||
| } | ||
| }) | ||
| } | ||
| private lateinit var context: Context | ||
| private lateinit var activity: Activity | ||
|
|
||
| companion object { | ||
| @JvmStatic | ||
| fun registerWith(registrar: Registrar) { | ||
| val channel = MethodChannel(registrar.messenger(), "g123k/torch_compat") | ||
| channel.setMethodCallHandler(TorchCompatPlugin(registrar.activity())) | ||
| } | ||
| } | ||
| private var hasLamp: Boolean = false | ||
| private lateinit var torchImpl: BaseTorch | ||
|
|
||
| override fun onMethodCall(call: MethodCall, result: Result) { | ||
| if (call.method == "turnOn") { | ||
| if (!hasLamp) { | ||
| result.error("NOTORCH", "This device does not have a torch", null) | ||
| } else { | ||
| torchImpl.turnOn() | ||
| result.success(true) | ||
| } | ||
| } else if (call.method == "turnOff") { | ||
| if (!hasLamp) { | ||
| result.error("NOTORCH", "This device does not have a torch", null) | ||
| } else { | ||
| torchImpl.turnOff() | ||
| result.success(true) | ||
| } | ||
| } else if (call.method == "hasTorch") { | ||
| result.success(hasLamp) | ||
| } else if (call.method == "dispose") { | ||
| torchImpl.dispose() | ||
| result.success(true) | ||
| } else { | ||
| result.notImplemented() | ||
| } | ||
| private fun attach(binding: ActivityPluginBinding) { | ||
| activity = binding.activity | ||
| hasLamp = activity.applicationContext.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) | ||
| torchImpl = when { | ||
| Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> TorchCamera2Impl(activity) | ||
| else -> TorchCamera1Impl(activity) | ||
| } | ||
|
|
||
| // activity.application.registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks() { | ||
| // override fun onActivityStopped(activity: Activity?) { | ||
| // torchImpl.dispose() | ||
| // } | ||
| // }) | ||
| } | ||
|
|
||
| private fun detach() { | ||
| torchImpl.dispose() | ||
| } | ||
|
|
||
| override fun onDetachedFromActivity() { | ||
| detach() | ||
| } | ||
|
|
||
| override fun onDetachedFromActivityForConfigChanges() { | ||
| detach() | ||
| } | ||
|
|
||
| override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { | ||
| attach(binding) | ||
| } | ||
|
|
||
| override fun onAttachedToActivity(binding: ActivityPluginBinding) { | ||
| attach(binding) | ||
| } | ||
|
|
||
|
|
||
| override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { | ||
| channel = MethodChannel(flutterPluginBinding.binaryMessenger, "g123k/torch_compat") | ||
| channel.setMethodCallHandler(this) | ||
| } | ||
|
|
||
| override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { | ||
| if (call.method == "getPlatformVersion") { | ||
| result.success("Android ${android.os.Build.VERSION.RELEASE}") | ||
| } else if (call.method == "turnOn") { | ||
| if (!hasLamp) { | ||
| result.error("NOTORCH", "This device does not have a torch", null) | ||
| } else { | ||
| torchImpl.turnOn() | ||
| result.success(true) | ||
| } | ||
| } else if (call.method == "turnOff") { | ||
| if (!hasLamp) { | ||
| result.error("NOTORCH", "This device does not have a torch", null) | ||
| } else { | ||
| torchImpl.turnOff() | ||
| result.success(true) | ||
| } | ||
| } else if (call.method == "hasTorch") { | ||
| result.success(hasLamp) | ||
| } else if (call.method == "dispose") { | ||
| torchImpl.dispose() | ||
| result.success(true) | ||
| } else { | ||
| result.notImplemented() | ||
| } | ||
| } | ||
|
|
||
| override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { | ||
| channel.setMethodCallHandler(null) | ||
| torchImpl.dispose() | ||
| } | ||
| } | ||
29 changes: 0 additions & 29 deletions
29
android/src/main/kotlin/fr/g123k/torch_compat/utils/ActivityLifecycleCallbacks.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this still needed?