Search in sources :

Example 1 with StatFs

use of android.os.StatFs in project MyDiary by erttyy8821.

the class FileManager method getSDCardFreeSize.

/**
     * @return MB
     */
public static long getSDCardFreeSize() {
    File path = Environment.getExternalStorageDirectory();
    StatFs sf = new StatFs(path.getPath());
    long blockSize, freeBlocks;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
        blockSize = sf.getBlockSize();
        freeBlocks = sf.getAvailableBlocks();
    } else {
        blockSize = sf.getBlockSizeLong();
        freeBlocks = sf.getAvailableBlocksLong();
    }
    return (freeBlocks * blockSize) / 1024 / 1024;
}
Also used : StatFs(android.os.StatFs) File(java.io.File)

Example 2 with StatFs

use of android.os.StatFs in project android-app by eoecn.

the class CommonUtil method getRealSizeOnSdcard.

/**
	 * get the space is left over on sdcard
	 */
public static long getRealSizeOnSdcard() {
    File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    return availableBlocks * blockSize;
}
Also used : StatFs(android.os.StatFs) File(java.io.File)

Example 3 with StatFs

use of android.os.StatFs in project android_frameworks_base by ParanoidAndroid.

the class AppCacheTest method testFreeApplicationCacheAllFiles.

@LargeTest
public void testFreeApplicationCacheAllFiles() throws Exception {
    boolean TRACKING = true;
    StatFs st = new StatFs("/data");
    long blks1 = getFreeStorageBlks(st);
    long availableMem = getFreeStorageSize(st);
    File cacheDir = mContext.getCacheDir();
    assertNotNull(cacheDir);
    createTestFiles1(cacheDir, "testtmpdir", 5);
    long blks2 = getFreeStorageBlks(st);
    if (localLOGV || TRACKING)
        Log.i(TAG, "blk1=" + blks1 + ", blks2=" + blks2);
    //this should free up the test files that were created earlier
    if (!invokePMFreeApplicationCache(availableMem)) {
        fail("Could not successfully invoke PackageManager free app cache API");
    }
    long blks3 = getFreeStorageBlks(st);
    if (localLOGV || TRACKING)
        Log.i(TAG, "blks3=" + blks3);
    verifyTestFiles1(cacheDir, "testtmpdir", 5);
}
Also used : StatFs(android.os.StatFs) File(java.io.File) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 4 with StatFs

use of android.os.StatFs in project android_frameworks_base by ParanoidAndroid.

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);
}
Also used : StatFs(android.os.StatFs) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 5 with StatFs

use of android.os.StatFs in project android_frameworks_base by ParanoidAndroid.

the class AppCacheTest method testFreeStorage.

// TODO: flaky test, omit from LargeTest for now
//@LargeTest
public void testFreeStorage() throws Exception {
    boolean TRACKING = true;
    StatFs st = new StatFs("/data");
    long blks1 = getFreeStorageBlks(st);
    if (localLOGV || TRACKING)
        Log.i(TAG, "Available free blocks=" + blks1);
    long availableMem = getFreeStorageSize(st);
    File cacheDir = mContext.getCacheDir();
    assertNotNull(cacheDir);
    createTestFiles1(cacheDir, "testtmpdir", 5);
    long blks2 = getFreeStorageBlks(st);
    if (localLOGV || TRACKING)
        Log.i(TAG, "Available blocks after writing test files in application cache=" + blks2);
    // Create receiver and register it
    FreeStorageReceiver receiver = new FreeStorageReceiver();
    mContext.registerReceiver(receiver, new IntentFilter(FreeStorageReceiver.ACTION_FREE));
    PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, new Intent(FreeStorageReceiver.ACTION_FREE), 0);
    // Invoke PackageManager api
    if (!invokePMFreeStorage(availableMem, receiver, pi)) {
        fail("Could not invoke PackageManager free storage API");
    }
    long blks3 = getFreeStorageBlks(st);
    if (localLOGV || TRACKING)
        Log.i(TAG, "Available blocks after freeing cache" + blks3);
    assertEquals(receiver.getResultCode(), 1);
    mContext.unregisterReceiver(receiver);
    // Verify result  
    verifyTestFiles1(cacheDir, "testtmpdir", 5);
}
Also used : IntentFilter(android.content.IntentFilter) StatFs(android.os.StatFs) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) File(java.io.File)

Aggregations

StatFs (android.os.StatFs)196 File (java.io.File)120 IOException (java.io.IOException)30 FileOutputStream (java.io.FileOutputStream)17 TargetApi (android.annotation.TargetApi)15 LargeTest (android.test.suitebuilder.annotation.LargeTest)12 FileNotFoundException (java.io.FileNotFoundException)11 ContentResolver (android.content.ContentResolver)10 DropBoxManager (android.os.DropBoxManager)10 TextView (android.widget.TextView)10 DropBoxManagerService (com.android.server.DropBoxManagerService)10 Test (org.junit.Test)7 PendingIntent (android.app.PendingIntent)6 Intent (android.content.Intent)6 IntentFilter (android.content.IntentFilter)6 Context (android.content.Context)5 Suppress (android.test.suitebuilder.annotation.Suppress)5 Button (android.widget.Button)5 SuppressLint (android.annotation.SuppressLint)3 SDCardInfo (com.yzy.supercleanmaster.model.SDCardInfo)3