Add space detect

pull/362/head
FongMi 2 years ago
parent a2b4955e42
commit c370746a30
  1. 13
      app/src/main/java/com/fongmi/android/tv/player/extractor/JianPian.java
  2. 39
      app/src/main/java/com/fongmi/android/tv/utils/FileUtil.java

@ -3,6 +3,7 @@ package com.fongmi.android.tv.player.extractor;
import android.net.Uri;
import com.fongmi.android.tv.player.Source;
import com.fongmi.android.tv.utils.FileUtil;
import com.github.catvod.utils.Path;
import com.p2p.P2PClass;
@ -27,10 +28,18 @@ public class JianPian implements Source.Extractor {
public String fetch(String url) throws Exception {
init();
stop();
check();
start(url);
return "http://127.0.0.1:" + p2p.port + "/" + URLEncoder.encode(Uri.parse(path).getLastPathSegment(), "GBK");
}
private void check() {
double cache = FileUtil.getDirectorySize(Path.jpa());
double avail = FileUtil.getAvailableStorageSpace(Path.jpa());
int usage = (int) (cache / avail * 100);
if (usage > 30) Path.clear(Path.jpa());
}
private void start(String url) {
try {
path = URLDecoder.decode(url).split("\\|")[0];
@ -48,14 +57,14 @@ public class JianPian implements Source.Extractor {
try {
if (p2p == null || path == null) return;
p2p.P2Pdoxpause(path.getBytes("GBK"));
path = null;
} catch (Exception e) {
e.printStackTrace();
} finally {
path = null;
}
}
@Override
public void exit() {
Path.clear(Path.jpa());
}
}

@ -3,6 +3,7 @@ package com.fongmi.android.tv.utils;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.StatFs;
import android.text.TextUtils;
import androidx.core.content.FileProvider;
@ -55,11 +56,37 @@ public class FileUtil {
public static void getCacheSize(Callback callback) {
App.execute(() -> {
String result = byteCountToDisplaySize(getFolderSize(Path.cache()));
App.post(() -> callback.success(result));
String usage = byteCountToDisplaySize(getDirectorySize(Path.cache()));
App.post(() -> callback.success(usage));
});
}
public static long getDirectorySize(File file) {
long size = 0;
if (file == null) return 0;
if (file.isDirectory()) for (File f : Path.list(file)) size += getDirectorySize(f);
else size = file.length();
return size;
}
public static long getTotalStorageSpace(File file) {
try {
StatFs stat = new StatFs(file.getAbsolutePath());
return stat.getBlockCountLong() * stat.getBlockSizeLong();
} catch (Exception e) {
return 0;
}
}
public static long getAvailableStorageSpace(File file) {
try {
StatFs stat = new StatFs(file.getAbsolutePath());
return stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
} catch (Exception e) {
return 0;
}
}
public static Uri getShareUri(String path) {
return getShareUri(new File(path.replace("file://", "")));
}
@ -73,14 +100,6 @@ public class FileUtil {
return TextUtils.isEmpty(mimeType) ? "*/*" : mimeType;
}
private static long getFolderSize(File file) {
long size = 0;
if (file == null) return 0;
if (file.isDirectory()) for (File f : Path.list(file)) size += getFolderSize(f);
else size = file.length();
return size;
}
private static String byteCountToDisplaySize(long size) {
if (size <= 0) return "0 KB";
String[] units = new String[]{"bytes", "KB", "MB", "GB", "TB"};

Loading…
Cancel
Save