1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
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     /** Called when the activity is first created. */
45     @Override
onCreate(Bundle icicle)46     public void onCreate(Bundle icicle) {
47         super.onCreate(icicle);
48         setContentView(R.layout.main);
49 
50         // Update the current data info
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 
fillDataAndUpdateInfo()67     public void fillDataAndUpdateInfo() {
68         updateInfo(this);
69     }
70 
71     // Fill up 100% of the data partition
fillupdisk(Context context)72     public void fillupdisk(Context context) {
73         final Context contextfill = context;
74         new Thread() {
75             @Override
76             public void run() {
77                 try {
78                     // Fill up all the memory
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                     // Fill up the last few block
92                     byte buf[] = new byte[(noOfBlockToFill % NO_OF_BLOCKS_TO_FILL) * mBlockSize];
93                     fs.write(buf);
94                     fs.flush();
95                     fs.close();
96 
97                     // Finished, update the info
98                     synchronized (fillUpDone) {
99                         fillUpDone.notify();
100                     }
101                 } catch (Exception e) {
102                     Log.v(TAG, e.toString());
103                 }
104             }
105         }.start();
106     }
107 
updateInfo(Context context)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             // The stat didn't relect the correct data right away
119             // put some extra time to make sure if get the right size.
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 }
133