| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.lowstoragetest; |
| 18 |
|
| 19 |
import android.app.Activity; |
| 20 |
import android.content.Context; |
| 21 |
|
| 22 |
import android.os.Bundle; |
| 23 |
import android.os.Environment; |
| 24 |
import android.os.StatFs; |
| 25 |
import android.util.Log; |
| 26 |
import android.view.KeyEvent; |
| 27 |
import android.view.View; |
| 28 |
import android.view.View.OnClickListener; |
| 29 |
import java.io.File; |
| 30 |
import java.io.FileOutputStream; |
| 31 |
import android.widget.TextView; |
| 32 |
import android.widget.Button; |
| 33 |
|
| 34 |
public class LowStorageTest extends Activity { |
| 35 |
static final String TAG = "DiskFullTest"; |
| 36 |
static final long WAIT_FOR_FINISH = 5 * 60 * 60; |
| 37 |
static final int NO_OF_BLOCKS_TO_FILL = 1000; |
| 38 |
static final int BYTE_SIZE = 1024; |
| 39 |
static final int WAIT_FOR_SYSTEM_UPDATE = 10000; |
| 40 |
|
| 41 |
private int mBlockSize = 0; |
| 42 |
private final Object fillUpDone = new Object(); |
| 43 |
|
| 44 |
|
| 45 |
@Override |
| 46 |
public void onCreate(Bundle icicle) { |
| 47 |
super.onCreate(icicle); |
| 48 |
setContentView(R.layout.main); |
| 49 |
|
| 50 |
|
| 51 |
File path = Environment.getDataDirectory(); |
| 52 |
StatFs stat = new StatFs(path.getPath()); |
| 53 |
int totalBlocks = stat.getBlockCount(); |
| 54 |
mBlockSize = (int) (stat.getBlockSize()); |
| 55 |
TextView startSizeTextView = findViewById(R.id.totalsize); |
| 56 |
startSizeTextView.setText(Long.toString((totalBlocks * mBlockSize) / BYTE_SIZE)); |
| 57 |
Button button = findViewById(R.id.button_run); |
| 58 |
button.setOnClickListener(mStartListener); |
| 59 |
} |
| 60 |
|
| 61 |
View.OnClickListener mStartListener = new OnClickListener() { |
| 62 |
public void onClick(View v) { |
| 63 |
fillDataAndUpdateInfo(); |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
public void fillDataAndUpdateInfo() { |
| 68 |
updateInfo(this); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
public void fillupdisk(Context context) { |
| 73 |
final Context contextfill = context; |
| 74 |
new Thread() { |
| 75 |
@Override |
| 76 |
public void run() { |
| 77 |
try { |
| 78 |
|
| 79 |
File path = Environment.getDataDirectory(); |
| 80 |
StatFs stat = new StatFs(path.getPath()); |
| 81 |
int totalBlocks = stat.getBlockCount(); |
| 82 |
int noOfBlockToFill = stat.getAvailableBlocks(); |
| 83 |
FileOutputStream fs = |
| 84 |
contextfill.openFileOutput("testdata", Context.MODE_APPEND); |
| 85 |
for (int i = 0; i < (noOfBlockToFill / NO_OF_BLOCKS_TO_FILL); i++) { |
| 86 |
byte buf[] = new byte[mBlockSize * NO_OF_BLOCKS_TO_FILL]; |
| 87 |
fs.write(buf); |
| 88 |
fs.flush(); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
byte buf[] = new byte[(noOfBlockToFill % NO_OF_BLOCKS_TO_FILL) * mBlockSize]; |
| 93 |
fs.write(buf); |
| 94 |
fs.flush(); |
| 95 |
fs.close(); |
| 96 |
|
| 97 |
|
| 98 |
synchronized (fillUpDone) { |
| 99 |
fillUpDone.notify(); |
| 100 |
} |
| 101 |
} catch (Exception e) { |
| 102 |
Log.v(TAG, e.toString()); |
| 103 |
} |
| 104 |
} |
| 105 |
}.start(); |
| 106 |
} |
| 107 |
|
| 108 |
public void updateInfo(Context context) { |
| 109 |
fillupdisk(this); |
| 110 |
synchronized (fillUpDone) { |
| 111 |
try { |
| 112 |
fillUpDone.wait(WAIT_FOR_FINISH); |
| 113 |
} catch (Exception e) { |
| 114 |
Log.v(TAG, "wait was interrupted."); |
| 115 |
} |
| 116 |
} |
| 117 |
try { |
| 118 |
|
| 119 |
|
| 120 |
Thread.sleep(WAIT_FOR_SYSTEM_UPDATE); |
| 121 |
File path = Environment.getDataDirectory(); |
| 122 |
StatFs stat = new StatFs(path.getPath()); |
| 123 |
long availableBlocks = stat.getAvailableBlocks(); |
| 124 |
TextView freeSizeTextView = findViewById(R.id.freesize); |
| 125 |
freeSizeTextView.setText(Long.toString((availableBlocks * mBlockSize) / BYTE_SIZE)); |
| 126 |
TextView statusTextView = findViewById(R.id.status); |
| 127 |
statusTextView.setText("Finished. You can start the test now."); |
| 128 |
} catch (Exception e) { |
| 129 |
Log.v(TAG, e.toString()); |
| 130 |
} |
| 131 |
} |
| 132 |
} |