1/*
2 * Copyright (C) 2017 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.frameworks.coretests.binderproxycountingtestservice;
18
19import android.app.Service;
20import android.content.Intent;
21import android.os.Debug;
22import android.os.Handler;
23import android.os.HandlerThread;
24import android.os.IBinder;
25import android.util.Log;
26
27import com.android.frameworks.coretests.aidl.IBpcCallbackObserver;
28import com.android.frameworks.coretests.aidl.IBpcTestServiceCmdService;
29import com.android.internal.os.BinderInternal;
30
31public class BpcTestServiceCmdService extends Service {
32    private static final String TAG = BpcTestServiceCmdService.class.getSimpleName();
33
34    //ServiceThread mHandlerThread;
35    Handler mHandler;
36    HandlerThread mHandlerThread;
37
38    private IBpcTestServiceCmdService.Stub mBinder = new IBpcTestServiceCmdService.Stub() {
39        IBpcCallbackObserver mCallbackObserver;
40
41        @Override
42        public void forceGc() {
43            int gcCount = Integer.parseInt(Debug.getRuntimeStat("art.gc.gc-count"));
44            int i = 20;
45            while (gcCount == Integer.parseInt(Debug.getRuntimeStat("art.gc.gc-count")) && i > 0) {
46                System.gc();
47                System.runFinalization();
48                i--;
49            }
50        }
51
52        @Override
53        public int getBinderProxyCount(int uid) {
54            return BinderInternal.nGetBinderProxyCount(uid);
55        }
56
57        @Override
58        public void setBinderProxyWatermarks(int high, int low) {
59            BinderInternal.nSetBinderProxyCountWatermarks(high, low);
60        }
61
62        @Override
63        public void enableBinderProxyLimit(boolean enable) {
64            BinderInternal.nSetBinderProxyCountEnabled(enable);
65        }
66
67        @Override
68        public void setBinderProxyCountCallback(IBpcCallbackObserver observer) {
69            if (observer != null) {
70                BinderInternal.setBinderProxyCountCallback(
71                        new BinderInternal.BinderProxyLimitListener() {
72                            @Override
73                            public void onLimitReached(int uid) {
74                                try {
75                                    synchronized (observer) {
76                                        observer.onCallback(uid);
77                                    }
78                                } catch (Exception e) {
79                                    Log.e(TAG, e.toString());
80                                }
81                            }
82                        }, mHandler);
83            } else {
84                BinderInternal.clearBinderProxyCountCallback();
85            }
86        }
87    };
88
89    @Override
90    public IBinder onBind(Intent intent) {
91        return mBinder;
92    }
93
94    @Override
95    public void onCreate()
96    {
97        mHandlerThread = new HandlerThread("BinderProxyCountingServiceThread");
98        mHandlerThread.start();
99        mHandler = new Handler(mHandlerThread.getLooper());
100    }
101}