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
17package com.android.strictmodetest;
18
19import android.app.Service;
20import android.content.Intent;
21import android.os.Binder;
22import android.os.Debug;
23import android.os.IBinder;
24import android.util.Log;
25
26import dalvik.system.BlockGuard;
27
28import java.io.FileDescriptor;
29import java.io.IOException;
30import java.io.InputStream;
31import java.io.OutputStream;
32import java.io.PrintWriter;
33
34public 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