forked from mboeddeker/disk_space
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced new method for checking storage space for a specified path…
… & upgraded flutter embedding/dependencies (#3) * switch to embedding v2 * migrate to AndroidX * maintain compatibility with embedding v1 * upgrade dependencies * added function to check available space for a specified directory * fix v1 embedding * added check if path exists
- Loading branch information
1 parent
3eec249
commit 0e8941f
Showing
14 changed files
with
198 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -5,3 +5,6 @@ | |
.pub/ | ||
|
||
build/ | ||
.idea/libraries/Dart_SDK.xml | ||
.idea/workspace.xml | ||
example/.flutter-plugins-dependencies |
This file contains 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
58 changes: 19 additions & 39 deletions
58
android/src/main/kotlin/de/appgewaltig/disk_space/DiskSpacePlugin.kt
This file contains 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,52 +1,32 @@ | ||
package de.appgewaltig.disk_space | ||
|
||
import android.os.Environment | ||
import android.os.StatFs | ||
import io.flutter.plugin.common.MethodCall | ||
import io.flutter.embedding.engine.plugins.FlutterPlugin | ||
import io.flutter.plugin.common.BinaryMessenger | ||
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 | ||
import io.flutter.plugin.common.PluginRegistry | ||
|
||
class DiskSpacePlugin: FlutterPlugin { | ||
|
||
class DiskSpacePlugin: MethodCallHandler { | ||
companion object { | ||
private var channel: MethodChannel? = null | ||
private var handler: MethodHandlerImpl = MethodHandlerImpl() | ||
|
||
@JvmStatic | ||
fun registerWith(registrar: Registrar) { | ||
val channel = MethodChannel(registrar.messenger(), "disk_space") | ||
channel.setMethodCallHandler(DiskSpacePlugin()) | ||
fun registerWith(registrar: PluginRegistry.Registrar) { | ||
registerChannel(registrar.messenger()) | ||
} | ||
} | ||
|
||
private fun getFreeDiskSpace(): Double { | ||
val stat = StatFs(Environment.getExternalStorageDirectory().path) | ||
|
||
var bytesAvailable: Long | ||
|
||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) | ||
bytesAvailable = stat.blockSizeLong * stat.availableBlocksLong | ||
else | ||
bytesAvailable = stat.blockSize.toLong() * stat.availableBlocks.toLong() | ||
return (bytesAvailable / (1024f * 1024f)).toDouble() | ||
private fun registerChannel(messenger: BinaryMessenger) { | ||
channel = MethodChannel(messenger, "disk_space") | ||
channel!!.setMethodCallHandler(handler) | ||
} | ||
} | ||
|
||
private fun getTotalDiskSpace(): Double { | ||
val stat = StatFs(Environment.getExternalStorageDirectory().path) | ||
|
||
var bytesAvailable: Long | ||
|
||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) | ||
bytesAvailable = stat.blockSizeLong * stat.blockCountLong | ||
else | ||
bytesAvailable = stat.blockSize.toLong() * stat.blockCount.toLong() | ||
return (bytesAvailable / (1024f * 1024f)).toDouble() | ||
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { | ||
registerChannel(binding.binaryMessenger) | ||
} | ||
|
||
|
||
override fun onMethodCall(call: MethodCall, result: Result) { | ||
when(call.method) { | ||
"getFreeDiskSpace" -> result.success(getFreeDiskSpace()) | ||
"getTotalDiskSpace" -> result.success(getTotalDiskSpace()) | ||
else -> result.notImplemented() | ||
} | ||
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { | ||
channel = null | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
android/src/main/kotlin/de/appgewaltig/disk_space/MethodHandlerImpl.kt
This file contains 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package de.appgewaltig.disk_space | ||
|
||
import android.os.Environment | ||
import android.os.StatFs | ||
import io.flutter.plugin.common.MethodCall | ||
import io.flutter.plugin.common.MethodChannel | ||
|
||
class MethodHandlerImpl : MethodChannel.MethodCallHandler { | ||
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { | ||
when(call.method) { | ||
"getFreeDiskSpace" -> result.success(getFreeDiskSpace()) | ||
"getTotalDiskSpace" -> result.success(getTotalDiskSpace()) | ||
"getFreeDiskSpaceForPath" -> result.success(getFreeDiskSpaceForPath(call.argument<String>("path")!!)) | ||
else -> result.notImplemented() | ||
} | ||
} | ||
|
||
private fun getFreeDiskSpace(): Double { | ||
val stat = StatFs(Environment.getExternalStorageDirectory().path) | ||
|
||
val bytesAvailable: Long | ||
bytesAvailable = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) | ||
stat.blockSizeLong * stat.availableBlocksLong | ||
else | ||
stat.blockSize.toLong() * stat.availableBlocks.toLong() | ||
return (bytesAvailable / (1024f * 1024f)).toDouble() | ||
} | ||
|
||
private fun getFreeDiskSpaceForPath(path: String): Double { | ||
val stat = StatFs(path) | ||
|
||
val bytesAvailable: Long | ||
bytesAvailable = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) | ||
stat.blockSizeLong * stat.availableBlocksLong | ||
else | ||
stat.blockSize.toLong() * stat.availableBlocks.toLong() | ||
return (bytesAvailable / (1024f * 1024f)).toDouble() | ||
} | ||
|
||
private fun getTotalDiskSpace(): Double { | ||
val stat = StatFs(Environment.getExternalStorageDirectory().path) | ||
|
||
val bytesAvailable: Long | ||
bytesAvailable = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) | ||
stat.blockSizeLong * stat.blockCountLong | ||
else | ||
stat.blockSize.toLong() * stat.blockCount.toLong() | ||
return (bytesAvailable / (1024f * 1024f)).toDouble() | ||
} | ||
} |
This file contains 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 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
13 changes: 13 additions & 0 deletions
13
example/android/app/src/main/kotlin/de/appgewaltig/disk_space_example/EmbeddingV1Activity.kt
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package de.appgewaltig.disk_space_example | ||
|
||
import android.os.Bundle | ||
import de.appgewaltig.disk_space.DiskSpacePlugin | ||
import io.flutter.app.FlutterActivity | ||
|
||
|
||
class EmbeddingV1Activity : FlutterActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
DiskSpacePlugin.registerWith(registrarFor("de.appgewaltig.disk_space.DiskSpacePlugin")) | ||
} | ||
} |
This file contains 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 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,4 @@ | ||
android.enableJetifier=true | ||
android.useAndroidX=true | ||
org.gradle.jvmargs=-Xmx1536M | ||
|
This file contains 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 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 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 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.