1787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian/*
2787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * Copyright (C) 2012 The Android Open Source Project
3787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian *
4787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * you may not use this file except in compliance with the License.
6787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * You may obtain a copy of the License at
7787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian *
8787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian *
10787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * Unless required by applicable law or agreed to in writing, software
11787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * See the License for the specific language governing permissions and
14787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian * limitations under the License.
15787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian */
16787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
17787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <stdint.h>
18787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <sys/types.h>
19787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
20787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian#include <utils/Singleton.h>
21787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
22787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopiannamespace android {
23787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian// ---------------------------------------------------------------------------
24787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
25787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianclass BatteryService : public Singleton<BatteryService> {
26787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    static const int TRANSACTION_noteStartSensor = IBinder::FIRST_CALL_TRANSACTION + 3;
27787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    static const int TRANSACTION_noteStopSensor = IBinder::FIRST_CALL_TRANSACTION + 4;
28787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    static const String16 DESCRIPTOR;
29787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
30787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    friend class Singleton<BatteryService>;
31787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    sp<IBinder> mBatteryStatService;
32787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
33787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    BatteryService();
34787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    status_t noteStartSensor(int uid, int handle);
35787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    status_t noteStopSensor(int uid, int handle);
36787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
37787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    void enableSensorImpl(uid_t uid, int handle);
38787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    void disableSensorImpl(uid_t uid, int handle);
39787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    void cleanupImpl(uid_t uid);
40787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
41787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    struct Info {
42787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        uid_t uid;
43787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        int handle;
44787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        int32_t count;
45787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        Info()  : uid(0), handle(0), count(0) { }
46787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        Info(uid_t uid, int handle) : uid(uid), handle(handle), count(0) { }
47787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        bool operator < (const Info& rhs) const {
48787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian            return (uid == rhs.uid) ? (handle < rhs.handle) :  (uid < rhs.uid);
49787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        }
50787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    };
51787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
52787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    Mutex mActivationsLock;
53787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    SortedVector<Info> mActivations;
54787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    bool addSensor(uid_t uid, int handle);
55787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    bool removeSensor(uid_t uid, int handle);
56787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
57787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopianpublic:
58787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    static void enableSensor(uid_t uid, int handle) {
59787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        BatteryService::getInstance().enableSensorImpl(uid, handle);
60787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    }
61787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    static void disableSensor(uid_t uid, int handle) {
62787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        BatteryService::getInstance().disableSensorImpl(uid, handle);
63787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    }
64787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    static void cleanup(uid_t uid) {
65787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian        BatteryService::getInstance().cleanupImpl(uid);
66787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian    }
67787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian};
68787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
69787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian// ---------------------------------------------------------------------------
70787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian}; // namespace android
71787ac1b388f144f5e6dd38f8b807866a5256dafcMathias Agopian
72