Skip to content

Commit

Permalink
style: reformat file
Browse files Browse the repository at this point in the history
  • Loading branch information
juliansteenbakker committed Jan 5, 2023
1 parent 18c6e6b commit 8481963
Showing 1 changed file with 125 additions and 89 deletions.
214 changes: 125 additions & 89 deletions android/src/main/kotlin/dev/steenbakker/nordicdfu/NordicDfuPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import no.nordicsemi.android.dfu.*
import no.nordicsemi.android.dfu.DfuBaseService.NOTIFICATION_ID
import no.nordicsemi.android.dfu.DfuProgressListenerAdapter
import no.nordicsemi.android.dfu.DfuServiceController
import no.nordicsemi.android.dfu.DfuServiceInitiator
import no.nordicsemi.android.dfu.DfuServiceListenerHelper
import java.util.*

class NordicDfuPlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamHandler {
class NordicDfuPlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamHandler {

private var mContext: Context? = null

Expand All @@ -36,8 +39,6 @@ class NordicDfuPlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamHa

eventChannel = EventChannel(binding.binaryMessenger, "dev.steenbakker.nordic_dfu/event")
eventChannel!!.setStreamHandler(this)


}

override fun onDetachedFromEngine(binding: FlutterPluginBinding) {
Expand Down Expand Up @@ -72,10 +73,12 @@ class NordicDfuPlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamHa
var filePath = call.argument<String>("filePath")
var fileInAsset = call.argument<Boolean>("fileInAsset")
val forceDfu = call.argument<Boolean>("forceDfu")
val enableUnsafeExperimentalButtonlessServiceInSecureDfu = call.argument<Boolean>("enableUnsafeExperimentalButtonlessServiceInSecureDfu")
val enableUnsafeExperimentalButtonlessServiceInSecureDfu =
call.argument<Boolean>("enableUnsafeExperimentalButtonlessServiceInSecureDfu")
val disableNotification = call.argument<Boolean>("disableNotification")
val keepBond = call.argument<Boolean>("keepBond")
val packetReceiptNotificationsEnabled = call.argument<Boolean>("packetReceiptNotificationsEnabled")
val packetReceiptNotificationsEnabled =
call.argument<Boolean>("packetReceiptNotificationsEnabled")
val restoreBond = call.argument<Boolean>("restoreBond")
val startAsForegroundService = call.argument<Boolean>("startAsForegroundService")
val numberOfPackets = call.argument<Int>("numberOfPackets")
Expand All @@ -91,8 +94,8 @@ class NordicDfuPlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamHa
if (fileInAsset) {
val loader = FlutterInjector.instance().flutterLoader()
filePath = loader.getLookupKeyForAsset(filePath)
val tempFileName = (PathUtils.getExternalAppCachePath(mContext!!)
+ UUID.randomUUID().toString())
val tempFileName =
(PathUtils.getExternalAppCachePath(mContext!!) + UUID.randomUUID().toString())
// copy asset file to temp path
if (!ResourceUtils.copyFileFromAssets(filePath, tempFileName, mContext!!)) {
result.error("File Error", "File not found!", "$filePath")
Expand All @@ -104,9 +107,21 @@ class NordicDfuPlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamHa
}
pendingResult = result
startDfu(
address, name, filePath, forceDfu, enableUnsafeExperimentalButtonlessServiceInSecureDfu,
disableNotification, keepBond, packetReceiptNotificationsEnabled, restoreBond, startAsForegroundService,
result, numberOfPackets, dataDelay, numberOfRetries)
address,
name,
filePath,
forceDfu,
enableUnsafeExperimentalButtonlessServiceInSecureDfu,
disableNotification,
keepBond,
packetReceiptNotificationsEnabled,
restoreBond,
startAsForegroundService,
result,
numberOfPackets,
dataDelay,
numberOfRetries
)
}

private fun abortDfu() {
Expand All @@ -128,15 +143,17 @@ class NordicDfuPlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamHa
startAsForegroundService: Boolean?,
result: MethodChannel.Result,
numberOfPackets: Int?,
dataDelay: Long?,
numberOfRetries: Int?) {
dataDelay: Long?,
numberOfRetries: Int?
) {

val starter = DfuServiceInitiator(address)
.setZip(filePath)
val starter = DfuServiceInitiator(address).setZip(filePath)

if (name != null) starter.setDeviceName(name)
if (enableUnsafeExperimentalButtonlessServiceInSecureDfu != null) {
starter.setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(enableUnsafeExperimentalButtonlessServiceInSecureDfu)
starter.setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(
enableUnsafeExperimentalButtonlessServiceInSecureDfu
)
}
if (forceDfu != null) starter.setForceDfu(forceDfu)
if (disableNotification != null) starter.setDisableNotification(disableNotification)
Expand Down Expand Up @@ -171,95 +188,114 @@ class NordicDfuPlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamHa
private fun cancelNotification() {
// let's wait a bit until we cancel the notification. When canceled immediately it will be recreated by service again.
Handler(Looper.getMainLooper()).postDelayed({
val manager = mContext!!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val manager =
mContext!!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.cancel(NOTIFICATION_ID)
}, 200)
}

private val mDfuProgressListener: DfuProgressListenerAdapter = object : DfuProgressListenerAdapter() {
override fun onDeviceConnected(deviceAddress: String) {
super.onDeviceConnected(deviceAddress)
sink?.success(mapOf("onDeviceConnected" to deviceAddress))
}
private val mDfuProgressListener: DfuProgressListenerAdapter =
object : DfuProgressListenerAdapter() {
override fun onDeviceConnected(deviceAddress: String) {
super.onDeviceConnected(deviceAddress)
sink?.success(mapOf("onDeviceConnected" to deviceAddress))
}

override fun onError(deviceAddress: String, error: Int, errorType: Int, message: String) {
super.onError(deviceAddress, error, errorType, message)
cancelNotification()
val parameters = mutableMapOf<String, Any>()
parameters["deviceAddress"] = deviceAddress
parameters["error"] = error
parameters["errorType"] = errorType
parameters["message"] = message
sink?.success(mapOf("onError" to parameters))
if (pendingResult != null) {
pendingResult!!.error("$error", "DFU FAILED: $message", "Address: $deviceAddress, Error Type: $errorType")
pendingResult = null
override fun onError(
deviceAddress: String, error: Int, errorType: Int, message: String
) {
super.onError(deviceAddress, error, errorType, message)
cancelNotification()
val parameters = mutableMapOf<String, Any>()
parameters["deviceAddress"] = deviceAddress
parameters["error"] = error
parameters["errorType"] = errorType
parameters["message"] = message
sink?.success(mapOf("onError" to parameters))
if (pendingResult != null) {
pendingResult!!.error(
"$error",
"DFU FAILED: $message",
"Address: $deviceAddress, Error Type: $errorType"
)
pendingResult = null
}
}
}

override fun onDeviceConnecting(deviceAddress: String) {
super.onDeviceConnecting(deviceAddress)
sink?.success(mapOf("onDeviceConnecting" to deviceAddress))
}
override fun onDeviceConnecting(deviceAddress: String) {
super.onDeviceConnecting(deviceAddress)
sink?.success(mapOf("onDeviceConnecting" to deviceAddress))
}

override fun onDeviceDisconnected(deviceAddress: String) {
super.onDeviceDisconnected(deviceAddress)
sink?.success(mapOf("onDeviceDisconnected" to deviceAddress))
}
override fun onDeviceDisconnected(deviceAddress: String) {
super.onDeviceDisconnected(deviceAddress)
sink?.success(mapOf("onDeviceDisconnected" to deviceAddress))
}

override fun onDeviceDisconnecting(deviceAddress: String) {
super.onDeviceDisconnecting(deviceAddress)
sink?.success(mapOf("onDeviceDisconnecting" to deviceAddress))
}
override fun onDeviceDisconnecting(deviceAddress: String) {
super.onDeviceDisconnecting(deviceAddress)
sink?.success(mapOf("onDeviceDisconnecting" to deviceAddress))
}

override fun onDfuAborted(deviceAddress: String) {
super.onDfuAborted(deviceAddress)
cancelNotification()
sink?.success(mapOf("onDfuAborted" to deviceAddress))
pendingResult?.error("DFU_ABORTED", "DFU ABORTED by user", "device address: $deviceAddress")
pendingResult = null
}
override fun onDfuAborted(deviceAddress: String) {
super.onDfuAborted(deviceAddress)
cancelNotification()
sink?.success(mapOf("onDfuAborted" to deviceAddress))
pendingResult?.error(
"DFU_ABORTED", "DFU ABORTED by user", "device address: $deviceAddress"
)
pendingResult = null
}

override fun onDfuCompleted(deviceAddress: String) {
super.onDfuCompleted(deviceAddress)
cancelNotification()
sink?.success(mapOf("onDfuCompleted" to deviceAddress))
pendingResult?.success(deviceAddress)
pendingResult = null
}
override fun onDfuCompleted(deviceAddress: String) {
super.onDfuCompleted(deviceAddress)
cancelNotification()
sink?.success(mapOf("onDfuCompleted" to deviceAddress))
pendingResult?.success(deviceAddress)
pendingResult = null
}

override fun onDfuProcessStarted(deviceAddress: String) {
super.onDfuProcessStarted(deviceAddress)
sink?.success(mapOf("onDfuProcessStarted" to deviceAddress))
}
override fun onDfuProcessStarted(deviceAddress: String) {
super.onDfuProcessStarted(deviceAddress)
sink?.success(mapOf("onDfuProcessStarted" to deviceAddress))
}

override fun onDfuProcessStarting(deviceAddress: String) {
super.onDfuProcessStarting(deviceAddress)
sink?.success(mapOf("onDfuProcessStarting" to deviceAddress))
}
override fun onDfuProcessStarting(deviceAddress: String) {
super.onDfuProcessStarting(deviceAddress)
sink?.success(mapOf("onDfuProcessStarting" to deviceAddress))
}

override fun onEnablingDfuMode(deviceAddress: String) {
super.onEnablingDfuMode(deviceAddress)
sink?.success(mapOf("onEnablingDfuMode" to deviceAddress))
}
override fun onEnablingDfuMode(deviceAddress: String) {
super.onEnablingDfuMode(deviceAddress)
sink?.success(mapOf("onEnablingDfuMode" to deviceAddress))
}

override fun onFirmwareValidating(deviceAddress: String) {
super.onFirmwareValidating(deviceAddress)
sink?.success(mapOf("onFirmwareValidating" to deviceAddress))
}
override fun onFirmwareValidating(deviceAddress: String) {
super.onFirmwareValidating(deviceAddress)
sink?.success(mapOf("onFirmwareValidating" to deviceAddress))
}

override fun onProgressChanged(deviceAddress: String, percent: Int, speed: Float, avgSpeed: Float, currentPart: Int, partsTotal: Int) {
super.onProgressChanged(deviceAddress, percent, speed, avgSpeed, currentPart, partsTotal)
val parameters = mutableMapOf<String, Any>()
parameters["deviceAddress"] = deviceAddress
parameters["percent"] = percent
parameters["speed"] = speed
parameters["avgSpeed"] = avgSpeed
parameters["currentPart"] = currentPart
parameters["partsTotal"] = partsTotal

sink?.success(mapOf("onProgressChanged" to parameters))
override fun onProgressChanged(
deviceAddress: String,
percent: Int,
speed: Float,
avgSpeed: Float,
currentPart: Int,
partsTotal: Int
) {
super.onProgressChanged(
deviceAddress, percent, speed, avgSpeed, currentPart, partsTotal
)
val parameters = mutableMapOf<String, Any>()
parameters["deviceAddress"] = deviceAddress
parameters["percent"] = percent
parameters["speed"] = speed
parameters["avgSpeed"] = avgSpeed
parameters["currentPart"] = currentPart
parameters["partsTotal"] = partsTotal

sink?.success(mapOf("onProgressChanged" to parameters))
}
}
}

}

0 comments on commit 8481963

Please sign in to comment.