Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application space #10

Open
mbfakourii opened this issue Oct 2, 2024 · 6 comments
Open

Application space #10

mbfakourii opened this issue Oct 2, 2024 · 6 comments

Comments

@mbfakourii
Copy link

How to get the occupied space of the application?

@jayendranar02
Copy link

hi i am intrested to solve the perticular issue

@jayendranar02
Copy link

package flowmobile.storage_space

import android.Manifest
import android.content.pm.PackageManager
import android.os.Environment
import android.os.StatFs
import androidx.annotation.NonNull
import androidx.core.app.ActivityCompat
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** StorageSpacePlugin */
class StorageSpacePlugin: FlutterPlugin, MethodCallHandler {
private lateinit var channel: MethodChannel

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    channel = MethodChannel(flutterPluginBinding.binaryMessenger, "storage_space")
    channel.setMethodCallHandler(this)
}

companion object {
    @JvmStatic
    fun registerWith(registrar: Registrar) {
        val channel = MethodChannel(registrar.messenger(), "storage_space")
        channel.setMethodCallHandler(StorageSpacePlugin())
    }
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
    when (call.method) {
        "getFreeSpace" -> {
            if (checkPermission()) {
                val freeSpace = getFreeSpace()
                result.success(freeSpace)
            } else {
                result.error("PERMISSION_DENIED", "Storage permission is required", null)
            }
        }
        "getTotalSpace" -> {
            if (checkPermission()) {
                val totalSpace = getTotalSpace()
                result.success(totalSpace)
            } else {
                result.error("PERMISSION_DENIED", "Storage permission is required", null)
            }
        }
        else -> {
            result.notImplemented()
        }
    }
}

private fun getFreeSpace(): Long {
    val stat = StatFs(Environment.getDataDirectory().path)
    return stat.freeBytes
}

private fun getTotalSpace(): Long {
    val stat = StatFs(Environment.getDataDirectory().path)
    return stat.totalBytes
}

private fun checkPermission(): Boolean {
    // Here, you should implement your permission checking logic
    // This is just a placeholder. In a real application, you would typically
    // check this in the Activity context.
    return true // Change this to actual permission check logic
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    channel.setMethodCallHandler(null)
}

}

@jayendranar02
Copy link

EXPLAINATION:

Error Handling:
Added checks for permissions before accessing storage. If permissions are denied, it returns an error result with a specific error code and message.

Permissions:
Implemented a checkPermission method, which you will need to replace with actual permission checking logic relevant to your application (usually done in the Activity or Fragment context).

Method Calls:
Used a when expression for method calls to improve readability and maintainability.

@jayendranar02
Copy link

jayendranar02 commented Oct 5, 2024

Note on Permissions
For Android, you'll need to request permissions at runtime for accessing external storage. Make sure to declare the necessary permissions in your AndroidManifest.xml:

XML
uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/

You'll also need to handle runtime permissions in your Flutter app if targeting Android 6.0 (API level 23) or higher.

@jayendranar02
Copy link

Conclusion
This modified code ensures that the StorageSpacePlugin handles permission checks and returns error messages appropriately while maintaining the core functionality of retrieving free and total storage space. Adjust the permission check logic according to your app's requirements to ensure a smooth user experience.

@jayendranar02
Copy link

If find helpful please comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants