Home | History | Annotate | Download | only in development
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of 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,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.development;
     18 
     19 import java.io.File;
     20 import java.io.FileOutputStream;
     21 import java.io.IOException;
     22 
     23 import android.app.Activity;
     24 import android.app.IActivityController;
     25 import android.app.IActivityManager;
     26 import android.app.Service;
     27 import android.content.BroadcastReceiver;
     28 import android.content.Context;
     29 import android.content.Intent;
     30 import android.content.IntentFilter;
     31 import android.os.AsyncTask;
     32 import android.os.Bundle;
     33 import android.os.IBinder;
     34 import android.os.IPowerManager;
     35 import android.os.Process;
     36 import android.os.RemoteException;
     37 import android.os.ServiceManager;
     38 import android.util.Log;
     39 import android.view.View;
     40 import android.widget.Button;
     41 
     42 public class CacheAbuser extends Activity {
     43     Button mStartInternalAbuse;
     44     Button mStartSlowInternalAbuse;
     45     Button mStartExternalAbuse;
     46     Button mStartSlowExternalAbuse;
     47     Button mStopAbuse;
     48 
     49     AsyncTask<Void, Void, Void> mInternalAbuseTask;
     50     AsyncTask<Void, Void, Void> mExternalAbuseTask;
     51 
     52     static class AbuseTask extends AsyncTask<Void, Void, Void> {
     53         final File mBaseDir;
     54         final boolean mQuick;
     55         final byte[] mBuffer;
     56 
     57         AbuseTask(File cacheDir, boolean quick) {
     58             File dir = new File(cacheDir, quick ? "quick" : "slow");
     59             mBaseDir = new File(dir, Long.toString(System.currentTimeMillis()));
     60             mQuick = quick;
     61             mBuffer = quick ? new byte[1024*1024] : new byte[1024];
     62         }
     63 
     64         @Override
     65         protected Void doInBackground(Void... params) {
     66             long num = 0;
     67             while (!isCancelled()) {
     68                 long dir1num = num/100;
     69                 long dir2num = num%100;
     70                 File dir = new File(mBaseDir, Long.toString(dir1num));
     71                 File file = new File(dir, Long.toString(dir2num));
     72                 FileOutputStream fos = null;
     73                 try {
     74                     dir.mkdirs();
     75                     fos = new FileOutputStream(file, false);
     76                     fos.write(mBuffer);
     77                 } catch (IOException e) {
     78                     Log.w("CacheAbuser", "Write failed to " + file + ": " + e);
     79                     try {
     80                         Thread.sleep(5*1000);
     81                     } catch (InterruptedException e1) {
     82                     }
     83                 } finally {
     84                     try {
     85                         if (fos != null) {
     86                             fos.close();
     87                         }
     88                     } catch (IOException e) {
     89                     }
     90                 }
     91                 num++;
     92             }
     93             return null;
     94         }
     95     }
     96 
     97     @Override
     98     public void onCreate(Bundle icicle) {
     99         super.onCreate(icicle);
    100 
    101         setContentView(R.layout.cache_abuser);
    102 
    103         mStartInternalAbuse = (Button) findViewById(R.id.start_internal_abuse);
    104         mStartInternalAbuse.setOnClickListener(new View.OnClickListener() {
    105             public void onClick(View v) {
    106                 if (mInternalAbuseTask == null) {
    107                     mInternalAbuseTask = new AbuseTask(getCacheDir(), true);
    108                     mInternalAbuseTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    109                     updateButtonState();
    110                 }
    111             }
    112         });
    113 
    114         mStartSlowInternalAbuse = (Button) findViewById(R.id.start_slow_internal_abuse);
    115         mStartSlowInternalAbuse.setOnClickListener(new View.OnClickListener() {
    116             public void onClick(View v) {
    117                 if (mInternalAbuseTask == null) {
    118                     mInternalAbuseTask = new AbuseTask(getCacheDir(), false);
    119                     mInternalAbuseTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    120                     updateButtonState();
    121                 }
    122             }
    123         });
    124 
    125         mStartExternalAbuse = (Button) findViewById(R.id.start_external_abuse);
    126         mStartExternalAbuse.setOnClickListener(new View.OnClickListener() {
    127             public void onClick(View v) {
    128                 if (mExternalAbuseTask == null) {
    129                     mExternalAbuseTask = new AbuseTask(getExternalCacheDir(), true);
    130                     mExternalAbuseTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    131                     updateButtonState();
    132                 }
    133             }
    134         });
    135 
    136         mStartSlowExternalAbuse = (Button) findViewById(R.id.start_slow_external_abuse);
    137         mStartSlowExternalAbuse.setOnClickListener(new View.OnClickListener() {
    138             public void onClick(View v) {
    139                 if (mExternalAbuseTask == null) {
    140                     mExternalAbuseTask = new AbuseTask(getExternalCacheDir(), false);
    141                     mExternalAbuseTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    142                     updateButtonState();
    143                 }
    144             }
    145         });
    146 
    147         mStopAbuse = (Button) findViewById(R.id.stop_abuse);
    148         mStopAbuse.setOnClickListener(new View.OnClickListener() {
    149             public void onClick(View v) {
    150                 stopAbuse();
    151             }
    152         });
    153 
    154         updateButtonState();
    155     }
    156 
    157     @Override
    158     public void onStart() {
    159         super.onStart();
    160         updateButtonState();
    161     }
    162 
    163     @Override
    164     public void onStop() {
    165         super.onStop();
    166         stopAbuse();
    167     }
    168 
    169     void stopAbuse() {
    170         if (mInternalAbuseTask != null) {
    171             mInternalAbuseTask.cancel(false);
    172             mInternalAbuseTask = null;
    173         }
    174         if (mExternalAbuseTask != null) {
    175             mExternalAbuseTask.cancel(false);
    176             mExternalAbuseTask = null;
    177         }
    178         updateButtonState();
    179     }
    180 
    181     void updateButtonState() {
    182         mStartInternalAbuse.setEnabled(mInternalAbuseTask == null);
    183         mStartSlowInternalAbuse.setEnabled(mInternalAbuseTask == null);
    184         mStartExternalAbuse.setEnabled(mExternalAbuseTask == null);
    185         mStartSlowExternalAbuse.setEnabled(mExternalAbuseTask == null);
    186         mStopAbuse.setEnabled(mInternalAbuseTask != null
    187                 || mExternalAbuseTask != null);
    188     }
    189 }
    190