Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Poynor | 7b27f27 | 2013-09-06 15:14:24 -0700 | [diff] [blame^] | 26 | #include "healthd.h" |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 27 | |
Todd Poynor | 7b27f27 | 2013-09-06 15:14:24 -0700 | [diff] [blame^] | 28 | namespace android { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 29 | |
| 30 | void BatteryPropertiesRegistrar::publish() { |
| 31 | defaultServiceManager()->addService(String16("batterypropreg"), this); |
| 32 | } |
| 33 | |
| 34 | void 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 | |
| 41 | void 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 Poynor | 7b27f27 | 2013-09-06 15:14:24 -0700 | [diff] [blame^] | 54 | healthd_battery_update(); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void 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 Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 68 | status_t BatteryPropertiesRegistrar::getProperty(int id, struct BatteryProperty *val) { |
Todd Poynor | 7b27f27 | 2013-09-06 15:14:24 -0700 | [diff] [blame^] | 69 | return healthd_get_property(id, val); |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 72 | void 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 |