Added "free memory" check before downloading [Android N / API 24+] (#10505)

Added "free space" check before downloading eliminating bugs related to out-of-memory on Android N / API 24+
This commit is contained in:
CloudyRowly 2024-03-21 19:18:55 +11:00 committed by GitHub
parent 5bdb6f18d6
commit 2e318b8b03
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 82 additions and 4 deletions

View file

@ -2,6 +2,8 @@ package us.shandian.giga.util;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;
import androidx.annotation.ColorInt;
@ -26,10 +28,8 @@ import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.util.Locale;
import java.util.Random;
import okio.ByteString;
import us.shandian.giga.get.DownloadMission;
public class Utility {
@ -40,6 +40,20 @@ public class Utility {
UNKNOWN
}
/**
* Get amount of free system's memory.
* @return free memory (bytes)
*/
public static long getSystemFreeMemory() {
try {
final StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath());
return statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong();
} catch (final Exception e) {
// do nothing
}
return -1;
}
public static String formatBytes(long bytes) {
Locale locale = Locale.getDefault();
if (bytes < 1024) {