package com.yc.videocache;
import android.content.Context;
import android.os.Environment;
import java.io.File;
import static android.os.Environment.MEDIA_MOUNTED;
/**
* Provides application storage paths
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
* @since 1.0.0
*/
public final class StorageUtils {
private static final String INDIVIDUAL_DIR_NAME = "video-cache";
/**
* Returns individual application cache directory (for only video caching from Proxy). Cache directory will be
* created on SD card ("/Android/data/[app_package_name]/cache/video-cache") if card is mounted .
* Else - Android defines cache directory on device's file system.
*
* @param context Application context
* @return Cache {@link File directory}
*/
static File getIndividualCacheDirectory(Context context) {
File cacheDir = getCacheDirectory(context);
return new File(cacheDir, INDIVIDUAL_DIR_NAME);
}
/**
* Returns application cache directory. Cache directory will be created on SD card
* ("/Android/data/[app_package_name]/cache") (if card is mounted and app has appropriate permission) or
* on device's file system depending incoming parameters.
*
* @param context Application context
* @return Cache {@link File directory}.
* NOTE: Can be null in some unpredictable cases (if SD card is unmounted and
* {@link Context#getCacheDir() Context.getCacheDir()} returns null).
*/
private static File getCacheDirectory(Context context) {
File appCacheDir = null;
if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
appCacheDir = context.getExternalCacheDir();
}
if (appCacheDir == null) {
appCacheDir = context.getCacheDir();
}
if (appCacheDir == null) {
String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
appCacheDir = new File(cacheDirPath);
}
return appCacheDir;
}
/**
* 删除文件
* @param root file
* @return 是否删除成功
*/
public static boolean deleteFiles(File root) {
File[] files = root.listFiles();
if (files != null) {
for (File f : files) {
if (!f.isDirectory() && f.exists()) { // 判断是否存在
if (!f.delete()) {
return false;
}
}
}
}
return true;
}
/**
* 删除文件
* @param filePath file路径
* @return 是否删除成功
*/
public static boolean deleteFile(String filePath) {
File file = new File(filePath);
if (file.exists()) {
if (file.isFile()) {
return file.delete();
} else {
String[] filePaths = file.list();
if (filePaths != null) {
for (String path : filePaths) {
deleteFile(filePath + File.separator + path);
}
}
return file.delete();
}
}
return true;
}
}