Home | History | Annotate | Download | only in strictmodetest
      1 /*
      2  * Copyright (C) 2010 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.strictmodetest;
     18 
     19 import android.app.Service;
     20 import android.content.Intent;
     21 import android.os.Binder;
     22 import android.os.Debug;
     23 import android.os.IBinder;
     24 import android.util.Log;
     25 
     26 import dalvik.system.BlockGuard;
     27 
     28 import java.io.FileDescriptor;
     29 import java.io.IOException;
     30 import java.io.InputStream;
     31 import java.io.OutputStream;
     32 import java.io.PrintWriter;
     33 
     34 public class ServiceBase extends Service {
     35 
     36     private static final String TAG = "StrictService";
     37 
     38     @Override public void onCreate() {
     39         Log.v(TAG, "onCreate");
     40     }
     41 
     42     @Override public IBinder onBind(Intent intent) {
     43         Log.v(TAG, "onBind");
     44         return mBinder;
     45     }
     46 
     47     private static class IAmHereException extends Exception {
     48         public IAmHereException() {
     49             fillInStackTrace();
     50         }
     51     }
     52 
     53     protected final IService.Stub mBinder = new IService.Stub() {
     54         public int getThreadPolicy() {
     55             return BlockGuard.getThreadPolicy().getPolicyMask();
     56         }
     57 
     58         public void doDiskOneWay() {
     59             int policy = BlockGuard.getThreadPolicy().getPolicyMask();
     60             Log.d(TAG, "Doing a one-way disk write; policy is: " + policy);
     61             // Fake disk usage...
     62             BlockGuard.getThreadPolicy().onWriteToDisk();
     63         }
     64 
     65         public boolean doDiskWrite(int afterCalls) {
     66             if (afterCalls > 0) {
     67                 return doDiskWrite(afterCalls - 1);
     68             }
     69             int policy = BlockGuard.getThreadPolicy().getPolicyMask();
     70             Log.d(TAG, "Doing a diskWrite; policy is: " + policy, new IAmHereException());
     71             // Fake disk usage...
     72             BlockGuard.getThreadPolicy().onWriteToDisk();
     73             try {
     74                 // Fake disk delay...
     75                 Thread.sleep(100);
     76             } catch (InterruptedException e) {}
     77             return true;
     78         }
     79     };
     80 }
     81