mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-02-24 10:28:50 +01:00
Fix dangling lock if app killed before unlocking
This commit is contained in:
parent
886bcf10ae
commit
a4b7f2adfe
2 changed files with 28 additions and 2 deletions
|
@ -1,5 +1,6 @@
|
|||
package com.nkming.nc_photos.plugin
|
||||
|
||||
import android.util.Log
|
||||
import io.flutter.plugin.common.MethodCall
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
|
||||
|
@ -21,6 +22,24 @@ class LockChannelHandler : MethodChannel.MethodCallHandler {
|
|||
const val CHANNEL = "${K.LIB_ID}/lock"
|
||||
|
||||
private val locks = mutableMapOf<Int, Boolean>()
|
||||
|
||||
private const val TAG = "LockChannelHandler"
|
||||
}
|
||||
|
||||
/**
|
||||
* Dismiss this handler instance
|
||||
*
|
||||
* All dangling locks locked via this instance will automatically be
|
||||
* unlocked
|
||||
*/
|
||||
fun dismiss() {
|
||||
for (id in _lockedIds) {
|
||||
if (locks[id] == true) {
|
||||
Log.w(TAG, "[dismiss] Automatically unlocking id: $id")
|
||||
locks[id] = false
|
||||
}
|
||||
}
|
||||
_lockedIds.clear()
|
||||
}
|
||||
|
||||
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
|
||||
|
@ -48,6 +67,7 @@ class LockChannelHandler : MethodChannel.MethodCallHandler {
|
|||
private fun tryLock(lockId: Int, result: MethodChannel.Result) {
|
||||
if (locks[lockId] != true) {
|
||||
locks[lockId] = true
|
||||
_lockedIds.add(lockId)
|
||||
result.success(true)
|
||||
} else {
|
||||
result.success(false)
|
||||
|
@ -57,6 +77,7 @@ class LockChannelHandler : MethodChannel.MethodCallHandler {
|
|||
private fun unlock(lockId: Int, result: MethodChannel.Result) {
|
||||
if (locks[lockId] == true) {
|
||||
locks[lockId] = false
|
||||
_lockedIds.remove(lockId)
|
||||
result.success(null)
|
||||
} else {
|
||||
result.error(
|
||||
|
@ -66,4 +87,6 @@ class LockChannelHandler : MethodChannel.MethodCallHandler {
|
|||
)
|
||||
}
|
||||
}
|
||||
|
||||
private val _lockedIds = mutableListOf<Int>()
|
||||
}
|
||||
|
|
|
@ -9,11 +9,11 @@ class NcPhotosPlugin : FlutterPlugin {
|
|||
override fun onAttachedToEngine(
|
||||
@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding
|
||||
) {
|
||||
// this may get called more than once
|
||||
lockChannelHandler = LockChannelHandler()
|
||||
lockChannel = MethodChannel(
|
||||
flutterPluginBinding.binaryMessenger, LockChannelHandler.CHANNEL
|
||||
)
|
||||
lockChannel.setMethodCallHandler(LockChannelHandler())
|
||||
lockChannel.setMethodCallHandler(lockChannelHandler)
|
||||
|
||||
notificationChannel = MethodChannel(
|
||||
flutterPluginBinding.binaryMessenger,
|
||||
|
@ -41,6 +41,7 @@ class NcPhotosPlugin : FlutterPlugin {
|
|||
override fun onDetachedFromEngine(
|
||||
@NonNull binding: FlutterPlugin.FlutterPluginBinding
|
||||
) {
|
||||
lockChannelHandler.dismiss()
|
||||
lockChannel.setMethodCallHandler(null)
|
||||
notificationChannel.setMethodCallHandler(null)
|
||||
nativeEventChannel.setStreamHandler(null)
|
||||
|
@ -51,4 +52,6 @@ class NcPhotosPlugin : FlutterPlugin {
|
|||
private lateinit var notificationChannel: MethodChannel
|
||||
private lateinit var nativeEventChannel: EventChannel
|
||||
private lateinit var nativeEventMethodChannel: MethodChannel
|
||||
|
||||
private lateinit var lockChannelHandler: LockChannelHandler
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue