14333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau/*
24333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau * Copyright (C) 2009 The Android Open Source Project
34333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau *
44333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau * Licensed under the Apache License, Version 2.0 (the "License"); you may not
54333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau * use this file except in compliance with the License. You may obtain a copy of
64333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau * the License at
74333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau *
84333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau * http://www.apache.org/licenses/LICENSE-2.0
94333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau *
104333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau * Unless required by applicable law or agreed to in writing, software
114333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
124333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
134333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau * License for the specific language governing permissions and limitations under
144333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau * the License.
154333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau */
164333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau
174333bd145e2b964414cd343814f50684694e234cYu Shan Emily Laupackage com.android.lowstoragetest;
184333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau
194333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lauimport android.app.Activity;
204333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lauimport android.content.Context;
214333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau
224333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lauimport android.os.Bundle;
234333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lauimport android.os.Environment;
244333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lauimport android.os.StatFs;
254333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lauimport android.util.Log;
264333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lauimport android.view.KeyEvent;
274333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lauimport android.view.View;
284333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lauimport android.view.View.OnClickListener;
294333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lauimport java.io.File;
304333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lauimport java.io.FileOutputStream;
314333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lauimport android.widget.TextView;
324333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lauimport android.widget.Button;
334333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau
344333bd145e2b964414cd343814f50684694e234cYu Shan Emily Laupublic class LowStorageTest extends Activity {
354333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    static final String TAG = "DiskFullTest";
364333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    static final long WAIT_FOR_FINISH = 5 * 60 * 60;
374333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    static final int NO_OF_BLOCKS_TO_FILL = 1000;
384333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    static final int BYTE_SIZE = 1024;
394333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    static final int WAIT_FOR_SYSTEM_UPDATE = 10000;
404333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau
414333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    private int mBlockSize = 0;
424333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    private final Object fillUpDone = new Object();
434333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau
444333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    /** Called when the activity is first created. */
454333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    @Override
464333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    public void onCreate(Bundle icicle) {
474333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        super.onCreate(icicle);
484333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        setContentView(R.layout.main);
494333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau
504333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        // Update the current data info
514333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        File path = Environment.getDataDirectory();
524333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        StatFs stat = new StatFs(path.getPath());
534333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        int totalBlocks = stat.getBlockCount();
544333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        mBlockSize = (int) (stat.getBlockSize());
554333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        TextView startSizeTextView = (TextView) findViewById(R.id.totalsize);
564333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        startSizeTextView.setText(Long.toString((totalBlocks * mBlockSize) / BYTE_SIZE));
574333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        Button button = (Button) findViewById(R.id.button_run);
584333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        button.setOnClickListener(mStartListener);
594333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    }
604333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau
614333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    View.OnClickListener mStartListener = new OnClickListener() {
624333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        public void onClick(View v) {
634333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            fillDataAndUpdateInfo();
644333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        }
654333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    };
664333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau
674333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    public void fillDataAndUpdateInfo() {
684333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        updateInfo(this);
694333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    }
704333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau
714333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    // Fill up 100% of the data partition
724333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    public void fillupdisk(Context context) {
734333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        final Context contextfill = context;
744333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        new Thread() {
754333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            @Override
764333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            public void run() {
774333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                try {
784333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    // Fill up all the memory
794333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    File path = Environment.getDataDirectory();
804333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    StatFs stat = new StatFs(path.getPath());
814333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    int totalBlocks = stat.getBlockCount();
824333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    int noOfBlockToFill = stat.getAvailableBlocks();
834333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    FileOutputStream fs =
844333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                            contextfill.openFileOutput("testdata", Context.MODE_APPEND);
854333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    for (int i = 0; i < (noOfBlockToFill / NO_OF_BLOCKS_TO_FILL); i++) {
864333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                        byte buf[] = new byte[mBlockSize * NO_OF_BLOCKS_TO_FILL];
874333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                        fs.write(buf);
884333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                        fs.flush();
894333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    }
904333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau
914333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    // Fill up the last few block
924333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    byte buf[] = new byte[(noOfBlockToFill % NO_OF_BLOCKS_TO_FILL) * mBlockSize];
934333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    fs.write(buf);
944333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    fs.flush();
954333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    fs.close();
964333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau
974333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    // Finished, update the info
984333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    synchronized (fillUpDone) {
994333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                        fillUpDone.notify();
1004333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    }
1014333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                } catch (Exception e) {
1024333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                    Log.v(TAG, e.toString());
1034333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                }
1044333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            }
1054333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        }.start();
1064333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    }
1074333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau
1084333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    public void updateInfo(Context context) {
1094333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        fillupdisk(this);
1104333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        synchronized (fillUpDone) {
1114333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            try {
1124333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                fillUpDone.wait(WAIT_FOR_FINISH);
1134333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            } catch (Exception e) {
1144333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau                Log.v(TAG, "wait was interrupted.");
1154333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            }
1164333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        }
1174333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        try {
1184333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            // The stat didn't relect the correct data right away
1194333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            // put some extra time to make sure if get the right size.
1204333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            Thread.sleep(WAIT_FOR_SYSTEM_UPDATE);
1214333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            File path = Environment.getDataDirectory();
1224333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            StatFs stat = new StatFs(path.getPath());
1234333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            long availableBlocks = stat.getAvailableBlocks();
1244333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            TextView freeSizeTextView = (TextView) findViewById(R.id.freesize);
1254333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            freeSizeTextView.setText(Long.toString((availableBlocks * mBlockSize) / BYTE_SIZE));
1264333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            TextView statusTextView = (TextView) findViewById(R.id.status);
1274333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            statusTextView.setText("Finished. You can start the test now.");
1284333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        } catch (Exception e) {
1294333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau            Log.v(TAG, e.toString());
1304333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau        }
1314333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau    }
1324333bd145e2b964414cd343814f50684694e234cYu Shan Emily Lau}
133