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.ActivityManagerNative; 25 import android.app.IActivityController; 26 import android.app.IActivityManager; 27 import android.app.Service; 28 import android.content.BroadcastReceiver; 29 import android.content.Context; 30 import android.content.Intent; 31 import android.content.IntentFilter; 32 import android.os.AsyncTask; 33 import android.os.Bundle; 34 import android.os.IBinder; 35 import android.os.IPowerManager; 36 import android.os.Process; 37 import android.os.RemoteException; 38 import android.os.ServiceManager; 39 import android.util.Log; 40 import android.view.View; 41 import android.widget.Button; 42 43 public class CacheAbuser extends Activity { 44 Button mStartInternalAbuse; 45 Button mStartSlowInternalAbuse; 46 Button mStartExternalAbuse; 47 Button mStartSlowExternalAbuse; 48 Button mStopAbuse; 49 50 AsyncTask<Void, Void, Void> mInternalAbuseTask; 51 AsyncTask<Void, Void, Void> mExternalAbuseTask; 52 53 static class AbuseTask extends AsyncTask<Void, Void, Void> { 54 final File mBaseDir; 55 final boolean mQuick; 56 final byte[] mBuffer; 57 58 AbuseTask(File cacheDir, boolean quick) { 59 File dir = new File(cacheDir, quick ? "quick" : "slow"); 60 mBaseDir = new File(dir, Long.toString(System.currentTimeMillis())); 61 mQuick = quick; 62 mBuffer = quick ? new byte[1024*1024] : new byte[1024]; 63 } 64 65 @Override 66 protected Void doInBackground(Void... params) { 67 long num = 0; 68 while (!isCancelled()) { 69 long dir1num = num/100; 70 long dir2num = num%100; 71 File dir = new File(mBaseDir, Long.toString(dir1num)); 72 File file = new File(dir, Long.toString(dir2num)); 73 FileOutputStream fos = null; 74 try { 75 dir.mkdirs(); 76 fos = new FileOutputStream(file, false); 77 fos.write(mBuffer); 78 } catch (IOException e) { 79 Log.w("CacheAbuser", "Write failed to " + file + ": " + e); 80 try { 81 wait(5*1000); 82 } catch (InterruptedException e1) { 83 } 84 } finally { 85 try { 86 if (fos != null) { 87 fos.close(); 88 } 89 } catch (IOException e) { 90 } 91 } 92 num++; 93 } 94 return null; 95 } 96 } 97 98 @Override 99 public void onCreate(Bundle icicle) { 100 super.onCreate(icicle); 101 102 setContentView(R.layout.cache_abuser); 103 104 mStartInternalAbuse = (Button) findViewById(R.id.start_internal_abuse); 105 mStartInternalAbuse.setOnClickListener(new View.OnClickListener() { 106 public void onClick(View v) { 107 if (mInternalAbuseTask == null) { 108 mInternalAbuseTask = new AbuseTask(getCacheDir(), true); 109 mInternalAbuseTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 110 updateButtonState(); 111 } 112 } 113 }); 114 115 mStartSlowInternalAbuse = (Button) findViewById(R.id.start_slow_internal_abuse); 116 mStartSlowInternalAbuse.setOnClickListener(new View.OnClickListener() { 117 public void onClick(View v) { 118 if (mInternalAbuseTask == null) { 119 mInternalAbuseTask = new AbuseTask(getCacheDir(), false); 120 mInternalAbuseTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 121 updateButtonState(); 122 } 123 } 124 }); 125 126 mStartExternalAbuse = (Button) findViewById(R.id.start_external_abuse); 127 mStartExternalAbuse.setOnClickListener(new View.OnClickListener() { 128 public void onClick(View v) { 129 if (mExternalAbuseTask == null) { 130 mExternalAbuseTask = new AbuseTask(getExternalCacheDir(), true); 131 mExternalAbuseTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 132 updateButtonState(); 133 } 134 } 135 }); 136 137 mStartSlowExternalAbuse = (Button) findViewById(R.id.start_slow_external_abuse); 138 mStartSlowExternalAbuse.setOnClickListener(new View.OnClickListener() { 139 public void onClick(View v) { 140 if (mExternalAbuseTask == null) { 141 mExternalAbuseTask = new AbuseTask(getExternalCacheDir(), false); 142 mExternalAbuseTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 143 updateButtonState(); 144 } 145 } 146 }); 147 148 mStopAbuse = (Button) findViewById(R.id.stop_abuse); 149 mStopAbuse.setOnClickListener(new View.OnClickListener() { 150 public void onClick(View v) { 151 stopAbuse(); 152 } 153 }); 154 155 updateButtonState(); 156 } 157 158 @Override 159 public void onStart() { 160 super.onStart(); 161 updateButtonState(); 162 } 163 164 @Override 165 public void onStop() { 166 super.onStop(); 167 stopAbuse(); 168 } 169 170 void stopAbuse() { 171 if (mInternalAbuseTask != null) { 172 mInternalAbuseTask.cancel(false); 173 mInternalAbuseTask = null; 174 } 175 if (mExternalAbuseTask != null) { 176 mExternalAbuseTask.cancel(false); 177 mExternalAbuseTask = null; 178 } 179 updateButtonState(); 180 } 181 182 void updateButtonState() { 183 mStartInternalAbuse.setEnabled(mInternalAbuseTask == null); 184 mStartSlowInternalAbuse.setEnabled(mInternalAbuseTask == null); 185 mStartExternalAbuse.setEnabled(mExternalAbuseTask == null); 186 mStartSlowExternalAbuse.setEnabled(mExternalAbuseTask == null); 187 mStopAbuse.setEnabled(mInternalAbuseTask != null 188 || mExternalAbuseTask != null); 189 } 190 } 191