DiskSpace.kt does the measuring and knows nothing about Capacitor 243,123 : its measure(dir) builds an android.os.StatFs for the directory and returns availableBytes to totalBytes. DiskSpacePlugin.kt is the class the bridge calls, the Kotlin counterpart of the Java ShelfPlugin in Plugin Registry (Android with Kotlin teaches the language):
@CapacitorPlugin(name = "DiskSpace")
class DiskSpacePlugin : Plugin() {
private val implementation = DiskSpace()
@PluginMethod
fun getSpace(call: PluginCall) {
val (free, total) = implementation.measure(context.filesDir)
call.resolve(JSObject().put("freeBytes", free).put("totalBytes", total))
}
@PluginMethod
fun canFit(call: PluginCall) {
// org.json parses 25000000 as an Int and 3000000000 as a Long: accept any Number
val bytes = (call.data.opt("bytes") as? Number)?.toLong()
if (bytes == null || bytes < 0) {
call.reject("canFit needs a non-negative number in 'bytes'", "BAD_SIZE")
return
}
val (free, _) = implementation.measure(context.filesDir)
call.resolve(JSObject().put("fits", bytes <= free).put("freeBytes", free))
}
}The base class Plugin supplies context, activity, bridge, notifyListeners() and the permission helpers. The annotation's name must equal the first argument of registerPlugin(), and it also takes permissions (Runtime Permissions). Each @PluginMethod takes one PluginCall and returns nothing; its default returnType is RETURN_PROMISE, and RETURN_CALLBACK suits methods that answer many times. context.filesDir is where Directory.Data points (Filesystem), so the answer describes the volume a download would use.