Rebuild Fly App to design v2 with full DJI SDK integration

Rebuild the Flutter/DJI-MSDK-V4 Fly App to the v2 UI kit and wire the
full SDK surface behind it.

Native (Kotlin): split DjiSdkBridge into a method/event router delegating
to per-subsystem SubBridge helpers sharing a BridgeCtx — FlightController
(takeoff/land/RTH + rich telemetry), Camera (mode/record/photo/exposure),
Gimbal, Mission (Waypoint + ActiveTrack; QuickShots via ActiveTrack
QUICK_SHOT), Media (MediaManager list/thumbnail/download), and optional
DJI account login. Manifest gains scoped media permissions.

Flutter: ten screens under lib/ui/ (Flight HUD, capture modes, camera
settings, settings menu, map+waypoints, home, album, academy, profile,
routes/flight logs), driven by an expanded FlightModel. New PVIcon renders
the kit's SVG paths via flutter_svg; map uses flutter_map + latlong2.

Pin transitive androidx.core/browser down to SDK-35-compatible versions
so the newer plugins don't force AGP 8.9.1 onto the DJI toolchain.

Verified with `flutter build apk --debug` (compiles Dart + all Kotlin);
runtime behaviour is untested here — it needs a physical DJI-connected
device.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
tajniak81andClaude Opus 4.8 committed 2026-07-15 16:51:08 +02:00
1 parent 3903428964
commit a4a2709456
29 files changed
+3766 -696

No files matched your search

@@ -16,7 +16,11 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<!-- Scoped media access (API 33+) for downloaded drone photos/videos. -->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<!-- Biometric / face authentication (local_auth). -->
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
@@ -0,0 +1,62 @@
package com.dji.flutter.dji_msdk_sample
import dji.common.error.DJIError
import dji.common.useraccount.UserAccountState
import dji.common.util.CommonCallbacks
import dji.sdk.useraccount.UserAccountManager
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
/**
* Optional DJI user-account login (in addition to the PilotVault account).
* Unlocks NFZ-unlocking & flight-record sync where supported. Emits `djiAccount`
* state updates. Accessed via the static [UserAccountManager.getInstance].
*/
class AccountBridge(private val ctx: BridgeCtx) : SubBridge {
private val mgr get() = UserAccountManager.getInstance()
override fun handle(call: MethodCall, result: MethodChannel.Result): Boolean {
when (call.method) {
"djiLogin" -> {
mgr.logIntoDJIUserAccount(
ctx.appContext,
object : CommonCallbacks.CompletionCallbackWith<UserAccountState> {
override fun onSuccess(state: UserAccountState?) {
emitState(state)
ctx.mainHandler.post { result.success(state?.name) }
}
override fun onFailure(error: DJIError?) {
ctx.mainHandler.post {
result.error("DJI_ERROR", error?.description ?: "DJI login failed", null)
}
}
},
)
// Fetch the account name once logged in (best-effort).
mgr.getLoggedInDJIUserAccountName(object : CommonCallbacks.CompletionCallbackWith<String> {
override fun onSuccess(name: String?) {
ctx.emit(mapOf("type" to "djiAccount", "state" to mgr.userAccountState?.name, "user" to name))
}
override fun onFailure(error: DJIError?) {}
})
}
"djiLogout" -> {
mgr.logoutOfDJIUserAccount(ctx.completion(result))
ctx.emit(mapOf("type" to "djiAccount", "state" to "NOT_LOGGED_IN"))
}
"getDjiAccountState" -> {
emitState(mgr.userAccountState)
result.success(mgr.userAccountState?.name)
}
else -> return false
}
return true
}
private fun emitState(state: UserAccountState?) {
ctx.emit(mapOf("type" to "djiAccount", "state" to state?.name))
}
}
@@ -0,0 +1,171 @@
package com.dji.flutter.dji_msdk_sample
import dji.common.camera.ExposureSettings
import dji.common.camera.SettingsDefinitions
import dji.common.camera.SystemState
import dji.common.camera.WhiteBalance
import dji.sdk.base.BaseProduct
import dji.sdk.camera.Camera
import dji.sdk.products.Aircraft
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
/**
* Camera control: mode, photo/record, shoot-photo modes, and Pro exposure
* (ISO / shutter / aperture / EV / WB). Emits `camera` (system state) and
* `exposure` events. String↔enum mapping mirrors the v2 kit's scrubber labels;
* unsupported values fail cleanly rather than crashing.
*/
class CameraBridge(private val ctx: BridgeCtx) : SubBridge {
private fun cam(): Camera? = ctx.aircraft()?.camera
override fun handle(call: MethodCall, result: MethodChannel.Result): Boolean {
val camera = cam()
when (call.method) {
"setCameraMode" -> guard(camera, result) {
val mode = when (call.argument<String>("mode")) {
"photo" -> SettingsDefinitions.CameraMode.SHOOT_PHOTO
"video" -> SettingsDefinitions.CameraMode.RECORD_VIDEO
"mediaDownload" -> SettingsDefinitions.CameraMode.MEDIA_DOWNLOAD
"playback" -> SettingsDefinitions.CameraMode.PLAYBACK
else -> SettingsDefinitions.CameraMode.RECORD_VIDEO
}
it.setMode(mode, ctx.completion(result))
}
"startShootPhoto" -> guard(camera, result) { it.startShootPhoto(ctx.completion(result)) }
"stopShootPhoto" -> guard(camera, result) { it.stopShootPhoto(ctx.completion(result)) }
"startRecordVideo" -> guard(camera, result) { it.startRecordVideo(ctx.completion(result)) }
"stopRecordVideo" -> guard(camera, result) { it.stopRecordVideo(ctx.completion(result)) }
"setShootPhotoMode" -> guard(camera, result) {
val mode = enumOrNull<SettingsDefinitions.ShootPhotoMode>(call.argument<String>("mode"))
if (mode == null) ctx.fail(result, "Unsupported photo mode")
else it.setShootPhotoMode(mode, ctx.completion(result))
}
"setExposureProgram" -> guard(camera, result) {
val mode = if (call.argument<String>("program") == "pro")
SettingsDefinitions.ExposureMode.MANUAL
else SettingsDefinitions.ExposureMode.PROGRAM
it.setExposureMode(mode, ctx.completion(result))
}
"setISO" -> guard(camera, result) {
val v = enumOrNull<SettingsDefinitions.ISO>(isoName(call.argument<String>("value")))
if (v == null) ctx.fail(result, "Unsupported ISO") else it.setISO(v, ctx.completion(result))
}
"setShutterSpeed" -> guard(camera, result) {
val v = enumOrNull<SettingsDefinitions.ShutterSpeed>(shutterName(call.argument<String>("value")))
if (v == null) ctx.fail(result, "Unsupported shutter") else it.setShutterSpeed(v, ctx.completion(result))
}
"setAperture" -> guard(camera, result) {
val v = enumOrNull<SettingsDefinitions.Aperture>(apertureName(call.argument<String>("value")))
if (v == null) ctx.fail(result, "Unsupported aperture") else it.setAperture(v, ctx.completion(result))
}
"setEV" -> guard(camera, result) {
val v = enumOrNull<SettingsDefinitions.ExposureCompensation>(evName(call.argument<String>("value")))
if (v == null) ctx.fail(result, "Unsupported EV") else it.setExposureCompensation(v, ctx.completion(result))
}
"setWhiteBalance" -> guard(camera, result) {
val wb = whiteBalance(call.argument<String>("value"))
if (wb == null) ctx.fail(result, "Unsupported white balance")
else it.setWhiteBalance(wb, ctx.completion(result))
}
"setHistogramEnabled" -> {
// Histogram data streaming is model-dependent; accept the toggle so
// the UI stays responsive. Real histogram bins are a later add.
result.success(null)
}
else -> return false
}
return true
}
private inline fun guard(camera: Camera?, result: MethodChannel.Result, block: (Camera) -> Unit) {
if (camera == null) ctx.fail(result, "No camera connected") else block(camera)
}
override fun bind(product: BaseProduct?) {
if (product !is Aircraft) return
val camera = product.camera ?: return
camera.setSystemStateCallback { state: SystemState -> emitSystem(state) }
camera.setExposureSettingsCallback { settings: ExposureSettings -> emitExposure(settings) }
}
private fun emitSystem(state: SystemState) {
ctx.emit(
mapOf(
"type" to "camera",
"isRecording" to state.isRecording,
"recordingTimeSeconds" to state.currentVideoRecordingTimeInSeconds,
"mode" to state.mode?.name,
)
)
}
private fun emitExposure(s: ExposureSettings) {
ctx.emit(
mapOf(
"type" to "exposure",
"iso" to s.iso.toString(),
"shutter" to shutterLabel(s.shutterSpeed?.name),
"aperture" to apertureLabel(s.aperture?.name),
"ev" to evLabel(s.exposureCompensation?.name),
)
)
}
// ── string → enum-name mapping (kit scrubber labels) ────────────────────────
private fun isoName(v: String?): String? = when {
v == null -> null
v.equals("AUTO", true) -> "ISO_AUTO"
else -> "ISO_$v"
}
private fun shutterName(v: String?): String? =
if (v == null) null else "SHUTTER_SPEED_" + v.replace(".", "_DOT_").replace("/", "_")
private fun apertureName(v: String?): String? =
if (v == null) null else "F_" + v.replace("f/", "", true).replace(".", "_DOT_")
private fun evName(v: String?): String? {
if (v == null) return null
val clean = v.replace("+", "").trim()
if (clean == "0" || clean == "0.0") return "N_0_0"
val neg = clean.startsWith("-")
val digits = clean.removePrefix("-").replace(".", "_")
return (if (neg) "N_" else "P_") + digits
}
private fun whiteBalance(v: String?): WhiteBalance? {
if (v == null) return null
if (v.equals("AUTO", true)) {
return WhiteBalance(SettingsDefinitions.WhiteBalancePreset.AUTO)
}
val kelvin = v.replace("K", "", true).trim().toIntOrNull() ?: return null
// DJI custom colour temperature is expressed in units of 100 K.
return WhiteBalance(SettingsDefinitions.WhiteBalancePreset.CUSTOM, kelvin / 100)
}
// ── enum-name → label mapping (for the read-back model) ─────────────────────
private fun shutterLabel(name: String?): String? =
name?.removePrefix("SHUTTER_SPEED_")?.replace("_DOT_", ".")?.replace("_", "/")
private fun apertureLabel(name: String?): String? =
name?.removePrefix("F_")?.replace("_DOT_", ".")?.let { "f/$it" }
private fun evLabel(name: String?): String? {
if (name == null) return null
val neg = name.startsWith("N_")
val body = name.removePrefix("N_").removePrefix("P_").replace("_", ".")
if (body == "0.0") return "0.0"
return (if (neg) "-" else "+") + body
}
private inline fun <reified T : Enum<T>> enumOrNull(name: String?): T? {
if (name == null) return null
return try {
enumValueOf<T>(name)
} catch (_: IllegalArgumentException) {
null
}
}
}
@@ -6,7 +6,7 @@ import android.os.Looper
import dji.common.battery.BatteryState
import dji.common.error.DJIError
import dji.common.error.DJISDKError
import dji.common.flightcontroller.FlightControllerState
import dji.common.util.CommonCallbacks
import dji.sdk.base.BaseComponent
import dji.sdk.base.BaseProduct
import dji.sdk.products.Aircraft
@@ -20,8 +20,11 @@ import io.flutter.plugin.common.MethodChannel
/**
* Bridges the DJI Mobile SDK V4 to Flutter.
*
* - [METHOD_CHANNEL] handles imperative calls from Dart (register, connect, query).
* - [EVENT_CHANNEL] streams registration / connection / telemetry updates to Dart.
* - [METHOD_CHANNEL] handles imperative calls from Dart, routed to per-subsystem
* helpers ([FlightControllerBridge], [CameraBridge], [GimbalBridge],
* [MissionBridge], [MediaBridge], [AccountBridge]).
* - [EVENT_CHANNEL] streams every subsystem's async updates to Dart. Each event
* is a map carrying a `type` key.
*
* All SDK callbacks arrive on arbitrary threads, so every event is marshalled to
* the main thread before being pushed into the Flutter [EventChannel.EventSink].
@@ -42,6 +45,23 @@ class DjiSdkBridge(
private var eventSink: EventChannel.EventSink? = null
/** Shared context handed to every subsystem helper. */
private val ctx = BridgeCtx(
appContext = appContext,
mainHandler = mainHandler,
emit = ::emit,
product = { DJISDKManager.getInstance().product },
)
private val flight = FlightControllerBridge(ctx)
private val camera = CameraBridge(ctx)
private val gimbal = GimbalBridge(ctx)
private val mission = MissionBridge(ctx)
private val media = MediaBridge(ctx)
private val account = AccountBridge(ctx)
private val subBridges = listOf(flight, camera, gimbal, mission, media, account)
init {
methodChannel.setMethodCallHandler(this)
eventChannel.setStreamHandler(this)
@@ -50,28 +70,35 @@ class DjiSdkBridge(
// ── MethodChannel ──────────────────────────────────────────────────────────
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
// Core (SDK lifecycle) methods first, then delegate to subsystem helpers.
when (call.method) {
"getSdkVersion" ->
"getSdkVersion" -> {
result.success(DJISDKManager.getInstance().sdkVersion)
return
}
"registerApp" -> {
registerApp()
result.success(null)
return
}
"startConnection" ->
"startConnection" -> {
result.success(DJISDKManager.getInstance().startConnectionToProduct())
return
}
"stopConnection" -> {
DJISDKManager.getInstance().stopConnectionToProduct()
result.success(null)
return
}
"getProductInfo" ->
"getProductInfo" -> {
result.success(connectionMap(DJISDKManager.getInstance().product))
else -> result.notImplemented()
return
}
}
for (sub in subBridges) {
if (sub.handle(call, result)) return
}
result.notImplemented()
}
// ── EventChannel ───────────────────────────────────────────────────────────
@@ -100,7 +127,6 @@ class DjiSdkBridge(
override fun onRegister(error: DJIError?) {
if (error == DJISDKError.REGISTRATION_SUCCESS) {
emit(mapOf("type" to "registration", "state" to "success"))
// Begin scanning for an attached product (USB RC / Wi-Fi).
DJISDKManager.getInstance().startConnectionToProduct()
} else {
emit(
@@ -132,8 +158,6 @@ class DjiSdkBridge(
oldComponent: BaseComponent?,
newComponent: BaseComponent?,
) {
// A component (e.g. flight controller, battery) appeared/changed —
// (re)attach the telemetry callbacks.
bindComponentCallbacks(DJISDKManager.getInstance().product)
}
@@ -151,30 +175,20 @@ class DjiSdkBridge(
private fun connectionMap(product: BaseProduct?): Map<String, Any?> {
val connected = product != null && product.isConnected
val model = product?.model?.displayName
return mapOf("type" to "connection", "connected" to connected, "model" to model)
val firmware = product?.firmwarePackageVersion
return mapOf(
"type" to "connection",
"connected" to connected,
"model" to model,
"firmware" to firmware,
)
}
/** Attaches flight-controller and battery state listeners when on an aircraft. */
/** (Re)attaches every subsystem's state listeners when a component appears. */
private fun bindComponentCallbacks(product: BaseProduct?) {
if (product !is Aircraft) return
for (sub in subBridges) sub.bind(product)
product.flightController?.setStateCallback { state: FlightControllerState ->
val location = state.aircraftLocation
emit(
mapOf(
"type" to "telemetry",
"satelliteCount" to state.satelliteCount,
"isFlying" to state.isFlying,
"flightMode" to state.flightModeString,
"altitude" to location?.altitude,
"latitude" to location?.latitude,
"longitude" to location?.longitude,
"velocityX" to state.velocityX,
"velocityY" to state.velocityY,
"velocityZ" to state.velocityZ,
)
)
}
if (product !is Aircraft) return
@Suppress("DEPRECATION")
product.battery?.setStateCallback { batteryState: BatteryState ->
@@ -182,8 +196,49 @@ class DjiSdkBridge(
mapOf(
"type" to "battery",
"percent" to batteryState.chargeRemainingInPercent,
"voltage" to batteryState.voltage / 1000.0,
"temperature" to batteryState.temperature,
)
)
}
}
}
/**
* Shared services handed to each subsystem helper: the emit sink, main-thread
* handler, app context, and a live accessor for the connected product. Also
* provides the boilerplate completion callback that answers a Flutter [result].
*/
class BridgeCtx(
val appContext: Context,
val mainHandler: Handler,
val emit: (Map<String, Any?>) -> Unit,
val product: () -> BaseProduct?,
) {
fun aircraft(): Aircraft? = product() as? Aircraft
/** A one-shot SDK completion callback that resolves the Flutter [result]. */
fun completion(result: MethodChannel.Result): CommonCallbacks.CompletionCallback<DJIError> {
return object : CommonCallbacks.CompletionCallback<DJIError> {
override fun onResult(error: DJIError?) {
mainHandler.post {
if (error == null) result.success(null)
else result.error("DJI_ERROR", error.description, null)
}
}
}
}
fun fail(result: MethodChannel.Result, message: String) {
mainHandler.post { result.error("DJI_UNAVAILABLE", message, null) }
}
}
/** A subsystem method/event helper delegated to by [DjiSdkBridge]. */
interface SubBridge {
/** Returns true if it owns [call] (and has answered [result]). */
fun handle(call: MethodCall, result: MethodChannel.Result): Boolean
/** (Re)attach any component state listeners for the connected product. */
fun bind(product: BaseProduct?) {}
}
@@ -0,0 +1,130 @@
package com.dji.flutter.dji_msdk_sample
import dji.common.flightcontroller.FlightControllerState
import dji.sdk.base.BaseProduct
import dji.sdk.flightcontroller.FlightController
import dji.sdk.products.Aircraft
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.hypot
import kotlin.math.sin
import kotlin.math.sqrt
/**
* Flight-controller commands (takeoff / land / RTH), safety & control settings,
* and the enriched `telemetry` stream. Delegated to by [DjiSdkBridge].
*/
class FlightControllerBridge(private val ctx: BridgeCtx) : SubBridge {
private fun fc(): FlightController? = ctx.aircraft()?.flightController
override fun handle(call: MethodCall, result: MethodChannel.Result): Boolean {
val controller = fc()
when (call.method) {
"takeOff" -> guard(controller, result) { it.startTakeoff(ctx.completion(result)) }
"land" -> guard(controller, result) { it.startLanding(ctx.completion(result)) }
"confirmLanding" -> guard(controller, result) { it.confirmLanding(ctx.completion(result)) }
"cancelLanding" -> guard(controller, result) { it.cancelLanding(ctx.completion(result)) }
"startGoHome" -> guard(controller, result) { it.startGoHome(ctx.completion(result)) }
"cancelGoHome" -> guard(controller, result) { it.cancelGoHome(ctx.completion(result)) }
"setHomeToCurrent" -> guard(controller, result) {
it.setHomeLocationUsingAircraftCurrentLocation(ctx.completion(result))
}
"setMaxFlightHeight" -> guard(controller, result) {
it.setMaxFlightHeight((call.argument<Int>("value") ?: 120), ctx.completion(result))
}
"setMaxFlightRadius" -> guard(controller, result) {
it.setMaxFlightRadius((call.argument<Int>("value") ?: 500), ctx.completion(result))
}
"setMaxRadiusEnabled" -> guard(controller, result) {
it.setMaxFlightRadiusLimitationEnabled(
call.argument<Boolean>("value") ?: false, ctx.completion(result)
)
}
"setGoHomeHeight" -> guard(controller, result) {
it.setGoHomeHeightInMeters((call.argument<Int>("value") ?: 100), ctx.completion(result))
}
"setNoviceMode" -> guard(controller, result) {
it.setNoviceModeEnabled(call.argument<Boolean>("value") ?: false, ctx.completion(result))
}
"setObstacleAvoidance" -> {
val assistant = controller?.flightAssistant
if (assistant == null) {
ctx.fail(result, "Obstacle avoidance unavailable on this aircraft")
} else {
assistant.setCollisionAvoidanceEnabled(
call.argument<Boolean>("value") ?: true, ctx.completion(result)
)
}
}
else -> return false
}
return true
}
private inline fun guard(
controller: FlightController?,
result: MethodChannel.Result,
block: (FlightController) -> Unit,
) {
if (controller == null) ctx.fail(result, "No aircraft connected") else block(controller)
}
override fun bind(product: BaseProduct?) {
if (product !is Aircraft) return
product.flightController?.setStateCallback { state -> emitTelemetry(state) }
}
private fun emitTelemetry(state: FlightControllerState) {
val loc = state.aircraftLocation
val home = state.homeLocation
val vx = state.velocityX
val vy = state.velocityY
val vz = state.velocityZ
val horizontalSpeed = hypot(vx.toDouble(), vy.toDouble())
var homeDistance: Double? = null
if (loc != null && home != null) {
homeDistance = haversine(
loc.latitude, loc.longitude, home.latitude, home.longitude
)
}
ctx.emit(
mapOf(
"type" to "telemetry",
"satelliteCount" to state.satelliteCount,
"gpsSignalLevel" to (state.gpsSignalLevel?.value()),
"isFlying" to state.isFlying,
"areMotorsOn" to state.areMotorsOn(),
"flightMode" to state.flightModeString,
"altitude" to loc?.altitude,
"latitude" to loc?.latitude,
"longitude" to loc?.longitude,
"homeLatitude" to home?.latitude,
"homeLongitude" to home?.longitude,
"homeDistance" to homeDistance,
"horizontalSpeed" to horizontalSpeed,
"verticalSpeed" to -vz.toDouble(),
"heading" to state.attitude?.yaw,
"goHomeHeight" to state.goHomeHeight,
"velocityX" to vx,
"velocityY" to vy,
"velocityZ" to vz,
)
)
}
/** Great-circle distance in metres. */
private fun haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double {
val r = 6371000.0
val dLat = Math.toRadians(lat2 - lat1)
val dLon = Math.toRadians(lon2 - lon1)
val a = sin(dLat / 2) * sin(dLat / 2) +
cos(Math.toRadians(lat1)) * cos(Math.toRadians(lat2)) *
sin(dLon / 2) * sin(dLon / 2)
return r * 2 * atan2(sqrt(a), sqrt(1 - a))
}
}
@@ -0,0 +1,55 @@
package com.dji.flutter.dji_msdk_sample
import dji.common.gimbal.Rotation
import dji.common.gimbal.RotationMode
import dji.sdk.base.BaseProduct
import dji.sdk.gimbal.Gimbal
import dji.sdk.products.Aircraft
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
/**
* Gimbal pitch control (from the flight HUD's pitch slider) and gimbal-attitude
* telemetry (`gimbal` event).
*/
class GimbalBridge(private val ctx: BridgeCtx) : SubBridge {
private fun gimbal(): Gimbal? = ctx.aircraft()?.gimbal
override fun handle(call: MethodCall, result: MethodChannel.Result): Boolean {
val g = gimbal()
when (call.method) {
"rotateGimbalPitch" -> guard(g, result) {
val pitch = (call.argument<Double>("pitch") ?: 0.0).toFloat()
val rotation = Rotation.Builder()
.mode(RotationMode.ABSOLUTE_ANGLE)
.pitch(pitch)
.time(0.5)
.build()
it.rotate(rotation, ctx.completion(result))
}
"resetGimbal" -> guard(g, result) { it.reset(ctx.completion(result)) }
else -> return false
}
return true
}
private inline fun guard(g: Gimbal?, result: MethodChannel.Result, block: (Gimbal) -> Unit) {
if (g == null) ctx.fail(result, "No gimbal connected") else block(g)
}
override fun bind(product: BaseProduct?) {
if (product !is Aircraft) return
product.gimbal?.setStateCallback { state ->
val a = state.attitudeInDegrees
ctx.emit(
mapOf(
"type" to "gimbal",
"pitch" to a?.pitch,
"roll" to a?.roll,
"yaw" to a?.yaw,
)
)
}
}
}
@@ -0,0 +1,146 @@
package com.dji.flutter.dji_msdk_sample
import android.graphics.Bitmap
import dji.common.camera.SettingsDefinitions
import dji.common.error.DJIError
import dji.common.util.CommonCallbacks
import dji.sdk.media.DownloadListener
import dji.sdk.media.MediaFile
import dji.sdk.media.MediaManager
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import java.io.File
import java.io.FileOutputStream
/**
* On-drone media via the SDK [MediaManager]: list the SD card, fetch thumbnails,
* and download originals. Emits `mediaList` (the file catalogue) and
* `mediaDownload` (per-file progress). Thumbnails/downloads are written to the
* app's cache / files dirs and their paths handed back to Dart.
*/
class MediaBridge(private val ctx: BridgeCtx) : SubBridge {
// Snapshot of the last-refreshed file list; Dart addresses files by index.
private var files: List<MediaFile> = emptyList()
private fun manager(): MediaManager? = ctx.aircraft()?.camera?.mediaManager
override fun handle(call: MethodCall, result: MethodChannel.Result): Boolean {
when (call.method) {
"refreshMediaList" -> refresh(result)
"fetchThumbnail" -> fetchThumbnail(call.argument<Int>("index") ?: -1, result)
"downloadMedia" -> download(call.argument<Int>("index") ?: -1, result)
else -> return false
}
return true
}
private fun refresh(result: MethodChannel.Result) {
val camera = ctx.aircraft()?.camera ?: run { ctx.fail(result, "No camera connected"); return }
val mgr = camera.mediaManager ?: run { ctx.fail(result, "Media manager not supported"); return }
// The media manager only serves files while the camera is in download mode.
camera.setMode(
SettingsDefinitions.CameraMode.MEDIA_DOWNLOAD,
object : CommonCallbacks.CompletionCallback<DJIError> {
override fun onResult(modeErr: DJIError?) {
if (modeErr != null) {
ctx.mainHandler.post { result.error("DJI_ERROR", modeErr.description, null) }
return
}
mgr.refreshFileListOfStorageLocation(
SettingsDefinitions.StorageLocation.SDCARD,
object : CommonCallbacks.CompletionCallback<DJIError> {
override fun onResult(err: DJIError?) {
if (err != null) {
ctx.mainHandler.post { result.error("DJI_ERROR", err.description, null) }
return
}
files = mgr.sdCardFileListSnapshot ?: emptyList()
emitList()
ctx.mainHandler.post { result.success(files.size) }
}
},
)
}
},
)
}
private fun emitList() {
val list = files.mapIndexed { i, f ->
val type = f.mediaType
val isVideo = type == MediaFile.MediaType.MP4 || type == MediaFile.MediaType.MOV
mapOf(
"index" to i,
"fileName" to f.fileName,
"type" to if (isVideo) "video" else "photo",
"durationSeconds" to f.durationInSeconds.toInt(),
"sizeBytes" to f.fileSize,
"createdMs" to f.timeCreated,
)
}
ctx.emit(mapOf("type" to "mediaList", "files" to list))
}
private fun fetchThumbnail(index: Int, result: MethodChannel.Result) {
val file = files.getOrNull(index) ?: run { ctx.fail(result, "No such media"); return }
file.fetchThumbnail(object : CommonCallbacks.CompletionCallback<DJIError> {
override fun onResult(err: DJIError?) {
if (err != null) {
ctx.mainHandler.post { result.error("DJI_ERROR", err.description, null) }
return
}
val bmp: Bitmap? = file.thumbnail
if (bmp == null) {
ctx.mainHandler.post { result.success(null) }
return
}
val out = File(ctx.appContext.cacheDir, "thumb_$index.png")
try {
FileOutputStream(out).use { bmp.compress(Bitmap.CompressFormat.PNG, 90, it) }
ctx.mainHandler.post { result.success(out.absolutePath) }
} catch (e: Exception) {
ctx.mainHandler.post { result.error("IO_ERROR", e.message, null) }
}
}
})
}
private fun download(index: Int, result: MethodChannel.Result) {
val file = files.getOrNull(index) ?: run { ctx.fail(result, "No such media"); return }
val destDir = ctx.appContext.getExternalFilesDir(null) ?: ctx.appContext.filesDir
var answered = false
file.fetchFileData(destDir, file.fileName, object : DownloadListener<String> {
override fun onStart() {
ctx.emit(mapOf("type" to "mediaDownload", "index" to index, "state" to "downloading", "progress" to 0.0))
}
override fun onRateUpdate(total: Long, current: Long, persize: Long) {}
override fun onRealtimeDataUpdate(data: ByteArray?, position: Long, isBegin: Boolean) {}
override fun onProgress(total: Long, current: Long) {
val p = if (total > 0) current.toDouble() / total.toDouble() else 0.0
ctx.emit(mapOf("type" to "mediaDownload", "index" to index, "state" to "downloading", "progress" to p))
}
override fun onSuccess(path: String?) {
val full = if (path != null) File(destDir, file.fileName).absolutePath else null
ctx.emit(mapOf("type" to "mediaDownload", "index" to index, "state" to "done", "progress" to 1.0, "path" to full))
if (!answered) {
answered = true
ctx.mainHandler.post { result.success(full) }
}
}
override fun onFailure(error: DJIError?) {
ctx.emit(mapOf("type" to "mediaDownload", "index" to index, "state" to "failed"))
if (!answered) {
answered = true
ctx.mainHandler.post { result.error("DJI_ERROR", error?.description ?: "Download failed", null) }
}
}
})
}
}
@@ -0,0 +1,126 @@
package com.dji.flutter.dji_msdk_sample
import android.graphics.RectF
import dji.common.mission.activetrack.ActiveTrackMission
import dji.common.mission.activetrack.ActiveTrackMode
import dji.common.mission.activetrack.QuickShotMode
import dji.common.mission.waypoint.Waypoint
import dji.common.mission.waypoint.WaypointMission
import dji.common.mission.waypoint.WaypointMissionFinishedAction
import dji.sdk.mission.activetrack.ActiveTrackOperator
import dji.sdk.mission.waypoint.WaypointMissionOperator
import dji.sdk.sdkmanager.DJISDKManager
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
/**
* Intelligent-flight & waypoint missions.
*
* - Waypoint route (from the Map screen) → [WaypointMissionOperator].
* - ActiveTrack subject tracking → [ActiveTrackOperator].
* - DJI-Fly QuickShots (Dronie/Rocket/Circle/Helix/Boomerang/Asteroid) → an
* ActiveTrack mission in [ActiveTrackMode.QUICK_SHOT] with the matching
* [QuickShotMode]. Unsupported modes on a given aircraft fail cleanly.
*/
class MissionBridge(private val ctx: BridgeCtx) : SubBridge {
private fun waypointOp(): WaypointMissionOperator? =
DJISDKManager.getInstance().missionControl?.waypointMissionOperator
private fun trackOp(): ActiveTrackOperator? =
DJISDKManager.getInstance().missionControl?.activeTrackOperator
override fun handle(call: MethodCall, result: MethodChannel.Result): Boolean {
when (call.method) {
"uploadWaypointMission" -> uploadWaypoint(call, result)
"startWaypointMission" -> waypoint(result) { it.startMission(ctx.completion(result)) }
"stopWaypointMission" -> waypoint(result) { it.stopMission(ctx.completion(result)) }
"pauseWaypointMission" -> waypoint(result) { it.pauseMission(ctx.completion(result)) }
"resumeWaypointMission" -> waypoint(result) { it.resumeMission(ctx.completion(result)) }
"startActiveTrack" -> startActiveTrack(call, result)
"stopActiveTrack" -> track(result) { it.stopTracking(ctx.completion(result)) }
"startQuickShot" -> startQuickShot(call, result)
else -> return false
}
return true
}
private inline fun waypoint(result: MethodChannel.Result, block: (WaypointMissionOperator) -> Unit) {
val op = waypointOp() ?: run { ctx.fail(result, "Waypoint missions unavailable"); return }
block(op)
}
private inline fun track(result: MethodChannel.Result, block: (ActiveTrackOperator) -> Unit) {
val op = trackOp() ?: run { ctx.fail(result, "ActiveTrack unavailable"); return }
block(op)
}
private fun uploadWaypoint(call: MethodCall, result: MethodChannel.Result) {
val op = waypointOp() ?: run { ctx.fail(result, "Waypoint missions unavailable"); return }
val raw = call.argument<List<Map<String, Any>>>("points") ?: emptyList()
if (raw.size < 2) {
ctx.fail(result, "A route needs at least 2 waypoints")
return
}
val speed = (call.argument<Double>("speed") ?: 8.0).toFloat()
val finish = try {
WaypointMissionFinishedAction.valueOf(call.argument<String>("finishAction") ?: "NO_ACTION")
} catch (_: IllegalArgumentException) {
WaypointMissionFinishedAction.NO_ACTION
}
val waypoints = raw.map { p ->
val lat = (p["lat"] as Number).toDouble()
val lon = (p["lon"] as Number).toDouble()
val alt = (p["altitude"] as? Number)?.toFloat() ?: 50f
Waypoint(lat, lon, alt)
}
val mission = WaypointMission.Builder()
.autoFlightSpeed(speed)
.maxFlightSpeed(maxOf(speed, 10f))
.finishedAction(finish)
.waypointList(ArrayList(waypoints))
.waypointCount(waypoints.size)
.build()
val loadErr = op.loadMission(mission)
if (loadErr != null) {
ctx.fail(result, loadErr.description)
return
}
ctx.emit(mapOf("type" to "mission", "state" to "uploading"))
op.uploadMission(ctx.completion(result))
}
private fun startActiveTrack(call: MethodCall, result: MethodChannel.Result) {
val op = trackOp() ?: run { ctx.fail(result, "ActiveTrack unavailable"); return }
val x = (call.argument<Double>("x") ?: 0.4).toFloat()
val y = (call.argument<Double>("y") ?: 0.35).toFloat()
val w = (call.argument<Double>("w") ?: 0.2).toFloat()
val h = (call.argument<Double>("h") ?: 0.3).toFloat()
val mode = try {
ActiveTrackMode.valueOf(call.argument<String>("mode") ?: "TRACE")
} catch (_: IllegalArgumentException) {
ActiveTrackMode.TRACE
}
val mission = ActiveTrackMission(RectF(x, y, x + w, y + h), mode)
ctx.emit(mapOf("type" to "mission", "state" to "tracking"))
op.startTracking(mission, ctx.completion(result))
}
private fun startQuickShot(call: MethodCall, result: MethodChannel.Result) {
val op = trackOp() ?: run { ctx.fail(result, "QuickShots unavailable"); return }
val name = call.argument<String>("name")?.uppercase() ?: ""
val quick = try {
QuickShotMode.valueOf(name)
} catch (_: IllegalArgumentException) {
ctx.fail(result, "$name is not supported on this aircraft")
return
}
val mission = ActiveTrackMission(RectF(0.4f, 0.35f, 0.6f, 0.65f), ActiveTrackMode.QUICK_SHOT)
mission.quickShotMode = quick
ctx.emit(mapOf("type" to "mission", "state" to "quickshot"))
op.startTracking(mission, ctx.completion(result))
}
}