blob: f2edee4340d35b00cfeb4f43f1f672fdd7651e7d [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
Todd Poynor7b27f272013-09-06 15:14:24 -070026#include "healthd.h"
Todd Poynor752faf22013-06-12 13:25:59 -070027
Todd Poynor7b27f272013-09-06 15:14:24 -070028namespace android {
Todd Poynor752faf22013-06-12 13:25:59 -070029
30void BatteryPropertiesRegistrar::publish() {
31 defaultServiceManager()->addService(String16("batterypropreg"), this);
32}
33
34void BatteryPropertiesRegistrar::notifyListeners(struct BatteryProperties props) {
35 Mutex::Autolock _l(mRegistrationLock);
36 for (size_t i = 0; i < mListeners.size(); i++) {
37 mListeners[i]->batteryPropertiesChanged(props);
38 }
39}
40
41void BatteryPropertiesRegistrar::registerListener(const sp<IBatteryPropertiesListener>& listener) {
42 {
43 Mutex::Autolock _l(mRegistrationLock);
44 // check whether this is a duplicate
45 for (size_t i = 0; i < mListeners.size(); i++) {
46 if (mListeners[i]->asBinder() == listener->asBinder()) {
47 return;
48 }
49 }
50
51 mListeners.add(listener);
52 listener->asBinder()->linkToDeath(this);
53 }
Todd Poynor7b27f272013-09-06 15:14:24 -070054 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -070055}
56
57void BatteryPropertiesRegistrar::unregisterListener(const sp<IBatteryPropertiesListener>& listener) {
58 Mutex::Autolock _l(mRegistrationLock);
59 for (size_t i = 0; i < mListeners.size(); i++) {
60 if (mListeners[i]->asBinder() == listener->asBinder()) {
61 mListeners[i]->asBinder()->unlinkToDeath(this);
62 mListeners.removeAt(i);
63 break;
64 }
65 }
66}
67
Todd Poynorc133b712013-08-14 17:39:13 -070068status_t BatteryPropertiesRegistrar::getProperty(int id, struct BatteryProperty *val) {
Todd Poynor7b27f272013-09-06 15:14:24 -070069 return healthd_get_property(id, val);
Todd Poynorc133b712013-08-14 17:39:13 -070070}
71
Todd Poynor752faf22013-06-12 13:25:59 -070072void BatteryPropertiesRegistrar::binderDied(const wp<IBinder>& who) {
73 Mutex::Autolock _l(mRegistrationLock);
74
75 for (size_t i = 0; i < mListeners.size(); i++) {
76 if (mListeners[i]->asBinder() == who) {
77 mListeners.removeAt(i);
78 break;
79 }
80 }
81}
82
83} // namespace android