BatteryService.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (C) 2012 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. #include <stdint.h>
  17. #include <math.h>
  18. #include <sys/types.h>
  19. #include <utils/Atomic.h>
  20. #include <utils/Errors.h>
  21. #include <utils/Singleton.h>
  22. #include <binder/BinderService.h>
  23. #include <binder/Parcel.h>
  24. #include "BatteryService.h"
  25. namespace android {
  26. // ---------------------------------------------------------------------------
  27. BatteryService::BatteryService() {
  28. const sp<IServiceManager> sm(defaultServiceManager());
  29. if (sm != NULL) {
  30. const String16 name("batterystats");
  31. mBatteryStatService = interface_cast<IBatteryStats>(sm->getService(name));
  32. }
  33. }
  34. bool BatteryService::addSensor(uid_t uid, int handle) {
  35. Mutex::Autolock _l(mActivationsLock);
  36. Info key(uid, handle);
  37. ssize_t index = mActivations.indexOf(key);
  38. if (index < 0) {
  39. index = mActivations.add(key);
  40. }
  41. Info& info(mActivations.editItemAt(index));
  42. info.count++;
  43. return info.count == 1;
  44. }
  45. bool BatteryService::removeSensor(uid_t uid, int handle) {
  46. Mutex::Autolock _l(mActivationsLock);
  47. ssize_t index = mActivations.indexOf(Info(uid, handle));
  48. if (index < 0) return false;
  49. Info& info(mActivations.editItemAt(index));
  50. info.count--;
  51. return info.count == 0;
  52. }
  53. void BatteryService::enableSensorImpl(uid_t uid, int handle) {
  54. if (mBatteryStatService != 0) {
  55. if (addSensor(uid, handle)) {
  56. int64_t identity = IPCThreadState::self()->clearCallingIdentity();
  57. mBatteryStatService->noteStartSensor(uid, handle);
  58. IPCThreadState::self()->restoreCallingIdentity(identity);
  59. }
  60. }
  61. }
  62. void BatteryService::disableSensorImpl(uid_t uid, int handle) {
  63. if (mBatteryStatService != 0) {
  64. if (removeSensor(uid, handle)) {
  65. int64_t identity = IPCThreadState::self()->clearCallingIdentity();
  66. mBatteryStatService->noteStopSensor(uid, handle);
  67. IPCThreadState::self()->restoreCallingIdentity(identity);
  68. }
  69. }
  70. }
  71. void BatteryService::cleanupImpl(uid_t uid) {
  72. if (mBatteryStatService != 0) {
  73. Mutex::Autolock _l(mActivationsLock);
  74. int64_t identity = IPCThreadState::self()->clearCallingIdentity();
  75. for (ssize_t i=0 ; i<mActivations.size() ; i++) {
  76. const Info& info(mActivations[i]);
  77. if (info.uid == uid) {
  78. mBatteryStatService->noteStopSensor(info.uid, info.handle);
  79. mActivations.removeAt(i);
  80. i--;
  81. }
  82. }
  83. IPCThreadState::self()->restoreCallingIdentity(identity);
  84. }
  85. }
  86. ANDROID_SINGLETON_STATIC_INSTANCE(BatteryService)
  87. // ---------------------------------------------------------------------------
  88. }; // namespace android