• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 namespace android {
27 
BatteryPropertiesRegistrar(BatteryMonitor * monitor)28 BatteryPropertiesRegistrar::BatteryPropertiesRegistrar(BatteryMonitor* monitor) {
29     mBatteryMonitor = monitor;
30 }
31 
publish()32 void BatteryPropertiesRegistrar::publish() {
33     defaultServiceManager()->addService(String16("batterypropreg"), this);
34 }
35 
notifyListeners(struct BatteryProperties props)36 void 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 
registerListener(const sp<IBatteryPropertiesListener> & listener)43 void 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 
unregisterListener(const sp<IBatteryPropertiesListener> & listener)59 void 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 
binderDied(const wp<IBinder> & who)70 void BatteryPropertiesRegistrar::binderDied(const wp<IBinder>& who) {
71     Mutex::Autolock _l(mRegistrationLock);
72 
73     for (size_t i = 0; i < mListeners.size(); i++) {
74         if (mListeners[i]->asBinder() == who) {
75             mListeners.removeAt(i);
76             break;
77         }
78     }
79 }
80 
81 }  // namespace android
82