blob: 3e06866be9e612d1d8271d48c86d0ab377fe5b94 [file] [log] [blame]
Todd Poynor752faf22013-06-12 13:25:59 -07001/*
2 * Copyright (C) 2013 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#include "BatteryPropertiesRegistrar.h"
18#include <batteryservice/BatteryService.h>
19#include <batteryservice/IBatteryPropertiesListener.h>
20#include <batteryservice/IBatteryPropertiesRegistrar.h>
21#include <binder/IServiceManager.h>
22#include <utils/Errors.h>
23#include <utils/Mutex.h>
24#include <utils/String16.h>
25
26namespace android {
27
28BatteryPropertiesRegistrar::BatteryPropertiesRegistrar(BatteryMonitor* monitor) {
29 mBatteryMonitor = monitor;
30}
31
32void BatteryPropertiesRegistrar::publish() {
33 defaultServiceManager()->addService(String16("batterypropreg"), this);
34}
35
36void BatteryPropertiesRegistrar::notifyListeners(struct BatteryProperties props) {
37 Mutex::Autolock _l(mRegistrationLock);
38 for (size_t i = 0; i < mListeners.size(); i++) {
39 mListeners[i]->batteryPropertiesChanged(props);
40 }
41}
42
43void BatteryPropertiesRegistrar::registerListener(const sp<IBatteryPropertiesListener>& listener) {
44 {
45 Mutex::Autolock _l(mRegistrationLock);
46 // check whether this is a duplicate
47 for (size_t i = 0; i < mListeners.size(); i++) {
48 if (mListeners[i]->asBinder() == listener->asBinder()) {
49 return;
50 }
51 }
52
53 mListeners.add(listener);
54 listener->asBinder()->linkToDeath(this);
55 }
56 mBatteryMonitor->update();
57}
58
59void BatteryPropertiesRegistrar::unregisterListener(const sp<IBatteryPropertiesListener>& listener) {
60 Mutex::Autolock _l(mRegistrationLock);
61 for (size_t i = 0; i < mListeners.size(); i++) {
62 if (mListeners[i]->asBinder() == listener->asBinder()) {
63 mListeners[i]->asBinder()->unlinkToDeath(this);
64 mListeners.removeAt(i);
65 break;
66 }
67 }
68}
69
Todd Poynorc133b712013-08-14 17:39:13 -070070status_t BatteryPropertiesRegistrar::getProperty(int id, struct BatteryProperty *val) {
71 return mBatteryMonitor->getProperty(id, val);
72}
73
Todd Poynor752faf22013-06-12 13:25:59 -070074void BatteryPropertiesRegistrar::binderDied(const wp<IBinder>& who) {
75 Mutex::Autolock _l(mRegistrationLock);
76
77 for (size_t i = 0; i < mListeners.size(); i++) {
78 if (mListeners[i]->asBinder() == who) {
79 mListeners.removeAt(i);
80 break;
81 }
82 }
83}
84
85} // namespace android