FileDocCategorySizeDatePackage
LowStorageTest.javaAPI DocAndroid 5.1 API4927Thu Mar 12 22:22:44 GMT 2015com.android.lowstoragetest

LowStorageTest

public class LowStorageTest extends android.app.Activity

Fields Summary
static final String
TAG
static final long
WAIT_FOR_FINISH
static final int
NO_OF_BLOCKS_TO_FILL
static final int
BYTE_SIZE
static final int
WAIT_FOR_SYSTEM_UPDATE
private int
mBlockSize
private final Object
fillUpDone
android.view.View.OnClickListener
mStartListener
Constructors Summary
Methods Summary
public voidfillDataAndUpdateInfo()


       
        updateInfo(this);
    
public voidfillupdisk(android.content.Context context)

        final Context contextfill = context;
        new Thread() {
            @Override
            public void run() {
                try {
                    // Fill up all the memory
                    File path = Environment.getDataDirectory();
                    StatFs stat = new StatFs(path.getPath());
                    int totalBlocks = stat.getBlockCount();
                    int noOfBlockToFill = stat.getAvailableBlocks();
                    FileOutputStream fs =
                            contextfill.openFileOutput("testdata", Context.MODE_APPEND);
                    for (int i = 0; i < (noOfBlockToFill / NO_OF_BLOCKS_TO_FILL); i++) {
                        byte buf[] = new byte[mBlockSize * NO_OF_BLOCKS_TO_FILL];
                        fs.write(buf);
                        fs.flush();
                    }

                    // Fill up the last few block
                    byte buf[] = new byte[(noOfBlockToFill % NO_OF_BLOCKS_TO_FILL) * mBlockSize];
                    fs.write(buf);
                    fs.flush();
                    fs.close();

                    // Finished, update the info
                    synchronized (fillUpDone) {
                        fillUpDone.notify();
                    }
                } catch (Exception e) {
                    Log.v(TAG, e.toString());
                }
            }
        }.start();
    
public voidonCreate(android.os.Bundle icicle)
Called when the activity is first created.


            
    
        
        super.onCreate(icicle);
        setContentView(R.layout.main);

        // Update the current data info
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        int totalBlocks = stat.getBlockCount();
        mBlockSize = (int) (stat.getBlockSize());
        TextView startSizeTextView = (TextView) findViewById(R.id.totalsize);
        startSizeTextView.setText(Long.toString((totalBlocks * mBlockSize) / BYTE_SIZE));
        Button button = (Button) findViewById(R.id.button_run);
        button.setOnClickListener(mStartListener);
    
public voidupdateInfo(android.content.Context context)

        fillupdisk(this);
        synchronized (fillUpDone) {
            try {
                fillUpDone.wait(WAIT_FOR_FINISH);
            } catch (Exception e) {
                Log.v(TAG, "wait was interrupted.");
            }
        }
        try {
            // The stat didn't relect the correct data right away
            // put some extra time to make sure if get the right size.
            Thread.sleep(WAIT_FOR_SYSTEM_UPDATE);
            File path = Environment.getDataDirectory();
            StatFs stat = new StatFs(path.getPath());
            long availableBlocks = stat.getAvailableBlocks();
            TextView freeSizeTextView = (TextView) findViewById(R.id.freesize);
            freeSizeTextView.setText(Long.toString((availableBlocks * mBlockSize) / BYTE_SIZE));
            TextView statusTextView = (TextView) findViewById(R.id.status);
            statusTextView.setText("Finished. You can start the test now.");
        } catch (Exception e) {
            Log.v(TAG, e.toString());
        }