use of android.os.StatFs in project teaTime by ancfdy.
the class SdCardUtil method getAvailableSize.
/**
* Get available size of SD card.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static long getAvailableSize(String path) {
try {
File base = new File(path);
StatFs stat = new StatFs(base.getPath());
return stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
use of android.os.StatFs in project teaTime by ancfdy.
the class SdCardUtil method getSDCardInfo.
/**
* Get SD card info detail.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static SDCardInfo getSDCardInfo() {
SDCardInfo sd = new SDCardInfo();
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
sd.isExist = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
File sdcardDir = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(sdcardDir.getPath());
sd.totalBlocks = sf.getBlockCountLong();
sd.blockByteSize = sf.getBlockSizeLong();
sd.availableBlocks = sf.getAvailableBlocksLong();
sd.availableBytes = sf.getAvailableBytes();
sd.freeBlocks = sf.getFreeBlocksLong();
sd.freeBytes = sf.getFreeBytes();
sd.totalBytes = sf.getTotalBytes();
}
}
return sd;
}
use of android.os.StatFs in project teaTime by ancfdy.
the class SdCardUtil method getFreeBytes.
/**
* 获取指定路径所在空间的剩余可用容量字节数(byte)
* @param filePath
* @return 容量字节 SDCard可用空间,内部存储可用空间
*/
public static long getFreeBytes(String filePath) {
// 如果是sd卡的下的路径,则获取sd卡可用容量
if (filePath.startsWith(getSDCardPath())) {
filePath = getSDCardPath();
} else {
// 如果是内部存储的路径,则获取内存存储的可用容量
filePath = Environment.getDataDirectory().getAbsolutePath();
}
StatFs stat = new StatFs(filePath);
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
return stat.getBlockSize() * availableBlocks;
}
use of android.os.StatFs in project android_frameworks_base by crdroidandroid.
the class AppCacheTest method testAppCacheClear.
//@LargeTest
public void testAppCacheClear() {
String dataDir = "/data/data";
StatFs st = new StatFs(dataDir);
long blkSize = st.getBlockSize();
long totBlks = st.getBlockCount();
long availableBlks = st.getFreeBlocks();
long thresholdBlks = (totBlks * THRESHOLD) / 100L;
String testDirName = "testdir";
//create directory in cache
File testDir = new File(mContext.getCacheDir(), testDirName);
testDir.mkdirs();
byte[] buffer = getBuffer();
int i = 1;
if (localLOGV)
Log.i(TAG, "availableBlks=" + availableBlks + ", thresholdBlks=" + thresholdBlks);
long createdFileBlks = 0;
int imax = 300;
while ((availableBlks > thresholdBlks) && (i < imax)) {
File testFile = new File(testDir, "testFile" + i + ".txt");
if (localLOGV)
Log.i(TAG, "Creating " + i + "th test file " + testFile);
int jmax = i;
i++;
FileOutputStream fos;
try {
fos = new FileOutputStream(testFile);
} catch (FileNotFoundException e) {
Log.i(TAG, "Failed creating test file:" + testFile);
continue;
}
boolean err = false;
for (int j = 1; j <= jmax; j++) {
try {
fos.write(buffer);
} catch (IOException e) {
Log.i(TAG, "Failed to write to file:" + testFile);
err = true;
}
}
try {
fos.close();
} catch (IOException e) {
Log.i(TAG, "Failed closing file:" + testFile);
}
if (err) {
continue;
}
createdFileBlks += getFileNumBlocks(testFile.length(), blkSize);
st.restat(dataDir);
availableBlks = st.getFreeBlocks();
}
st.restat(dataDir);
long availableBytes = st.getFreeBlocks() * blkSize;
long shouldFree = (ACTUAL_THRESHOLD - THRESHOLD) * totBlks;
//wait for some time and confirm cache is deleted
try {
Log.i(TAG, "Sleeping for 2 minutes...");
Thread.sleep(2 * 60 * 1000);
} catch (InterruptedException e) {
fail("Exception when sleeping " + e);
}
boolean removedFlag = false;
long existingFileBlks = 0;
for (int k = 1; k < i; k++) {
File testFile = new File(testDir, "testFile" + k + ".txt");
if (!testFile.exists()) {
removedFlag = true;
if (localLOGV)
Log.i(TAG, testFile + " removed");
} else {
existingFileBlks += getFileNumBlocks(testFile.length(), blkSize);
}
}
if (localLOGV)
Log.i(TAG, "createdFileBlks=" + createdFileBlks + ", existingFileBlks=" + existingFileBlks);
long fileSize = createdFileBlks - existingFileBlks;
//verify fileSize number of bytes have been cleared from cache
if (localLOGV)
Log.i(TAG, "deletedFileBlks=" + fileSize + " shouldFreeBlks=" + shouldFree);
if ((fileSize > (shouldFree - blkSize) && (fileSize < (shouldFree + blkSize)))) {
Log.i(TAG, "passed");
}
assertTrue("Files should have been removed", removedFlag);
}
use of android.os.StatFs in project android_frameworks_base by crdroidandroid.
the class AppCacheTest method testFreeApplicationCacheSomeFiles.
public void testFreeApplicationCacheSomeFiles() throws Exception {
StatFs st = new StatFs("/data");
long blks1 = getFreeStorageBlks(st);
File cacheDir = mContext.getCacheDir();
assertNotNull(cacheDir);
createTestFiles1(cacheDir, "testtmpdir", 5);
long blks2 = getFreeStorageBlks(st);
Log.i(TAG, "blk1=" + blks1 + ", blks2=" + blks2);
long diff = (blks1 - blks2 - 2);
if (!invokePMFreeApplicationCache(diff * st.getBlockSize())) {
fail("Could not successfully invoke PackageManager free app cache API");
}
long blks3 = getFreeStorageBlks(st);
//blks3 should be greater than blks2 and less than blks1
if (!((blks3 <= blks1) && (blks3 >= blks2))) {
failStr("Expected " + (blks1 - blks2) + " number of blocks to be freed but freed only " + (blks1 - blks3));
}
}
Aggregations