SensorManager.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (C) 2010 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. #define LOG_TAG "Sensors"
  17. #include <stdint.h>
  18. #include <sys/types.h>
  19. #include <utils/Errors.h>
  20. #include <utils/RefBase.h>
  21. #include <utils/Singleton.h>
  22. #include <binder/IBinder.h>
  23. #include <binder/IServiceManager.h>
  24. #include <gui/ISensorServer.h>
  25. #include <gui/ISensorEventConnection.h>
  26. #include <gui/Sensor.h>
  27. #include <gui/SensorManager.h>
  28. #include <gui/SensorEventQueue.h>
  29. // ----------------------------------------------------------------------------
  30. namespace android {
  31. // ----------------------------------------------------------------------------
  32. android::Mutex android::SensorManager::sLock;
  33. std::map<String16, SensorManager*> android::SensorManager::sPackageInstances;
  34. SensorManager& SensorManager::getInstanceForPackage(const String16& packageName) {
  35. Mutex::Autolock _l(sLock);
  36. SensorManager* sensorManager;
  37. std::map<String16, SensorManager*>::iterator iterator =
  38. sPackageInstances.find(packageName);
  39. if (iterator != sPackageInstances.end()) {
  40. sensorManager = iterator->second;
  41. } else {
  42. String16 opPackageName = packageName;
  43. // It is possible that the calling code has no access to the package name.
  44. // In this case we will get the packages for the calling UID and pick the
  45. // first one for attributing the app op. This will work correctly for
  46. // runtime permissions as for legacy apps we will toggle the app op for
  47. // all packages in the UID. The caveat is that the operation may be attributed
  48. // to the wrong package and stats based on app ops may be slightly off.
  49. if (opPackageName.size() <= 0) {
  50. sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
  51. if (binder != 0) {
  52. const uid_t uid = IPCThreadState::self()->getCallingUid();
  53. Vector<String16> packages;
  54. interface_cast<IPermissionController>(binder)->getPackagesForUid(uid, packages);
  55. if (!packages.isEmpty()) {
  56. opPackageName = packages[0];
  57. } else {
  58. ALOGE("No packages for calling UID");
  59. }
  60. } else {
  61. ALOGE("Cannot get permission service");
  62. }
  63. }
  64. sensorManager = new SensorManager(opPackageName);
  65. // If we had no package name, we looked it up from the UID and the sensor
  66. // manager instance we created should also be mapped to the empty package
  67. // name, to avoid looking up the packages for a UID and get the same result.
  68. if (packageName.size() <= 0) {
  69. sPackageInstances.insert(std::make_pair(String16(), sensorManager));
  70. }
  71. // Stash the per package sensor manager.
  72. sPackageInstances.insert(std::make_pair(opPackageName, sensorManager));
  73. }
  74. return *sensorManager;
  75. }
  76. SensorManager::SensorManager(const String16& opPackageName)
  77. : mSensorList(0), mOpPackageName(opPackageName)
  78. {
  79. // okay we're not locked here, but it's not needed during construction
  80. assertStateLocked();
  81. }
  82. SensorManager::~SensorManager()
  83. {
  84. free(mSensorList);
  85. }
  86. void SensorManager::sensorManagerDied()
  87. {
  88. Mutex::Autolock _l(mLock);
  89. mSensorServer.clear();
  90. free(mSensorList);
  91. mSensorList = NULL;
  92. mSensors.clear();
  93. }
  94. status_t SensorManager::assertStateLocked() const {
  95. bool initSensorManager = false;
  96. if (mSensorServer == NULL) {
  97. initSensorManager = true;
  98. } else {
  99. // Ping binder to check if sensorservice is alive.
  100. status_t err = IInterface::asBinder(mSensorServer)->pingBinder();
  101. if (err != NO_ERROR) {
  102. initSensorManager = true;
  103. }
  104. }
  105. if (initSensorManager) {
  106. // try for 300 seconds (60*5(getService() tries for 5 seconds)) before giving up ...
  107. const String16 name("sensorservice");
  108. for (int i = 0; i < 60; i++) {
  109. status_t err = getService(name, &mSensorServer);
  110. if (err == NAME_NOT_FOUND) {
  111. sleep(1);
  112. continue;
  113. }
  114. if (err != NO_ERROR) {
  115. return err;
  116. }
  117. break;
  118. }
  119. class DeathObserver : public IBinder::DeathRecipient {
  120. SensorManager& mSensorManger;
  121. virtual void binderDied(const wp<IBinder>& who) {
  122. ALOGW("sensorservice died [%p]", who.unsafe_get());
  123. mSensorManger.sensorManagerDied();
  124. }
  125. public:
  126. DeathObserver(SensorManager& mgr) : mSensorManger(mgr) { }
  127. };
  128. LOG_ALWAYS_FATAL_IF(mSensorServer.get() == NULL, "getService(SensorService) NULL");
  129. mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this));
  130. IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver);
  131. mSensors = mSensorServer->getSensorList(mOpPackageName);
  132. size_t count = mSensors.size();
  133. mSensorList =
  134. static_cast<Sensor const**>(malloc(count * sizeof(Sensor*)));
  135. LOG_ALWAYS_FATAL_IF(mSensorList == NULL, "mSensorList NULL");
  136. for (size_t i=0 ; i<count ; i++) {
  137. mSensorList[i] = mSensors.array() + i;
  138. }
  139. }
  140. return NO_ERROR;
  141. }
  142. ssize_t SensorManager::getSensorList(Sensor const* const** list) const
  143. {
  144. Mutex::Autolock _l(mLock);
  145. status_t err = assertStateLocked();
  146. if (err < 0) {
  147. return static_cast<ssize_t>(err);
  148. }
  149. *list = mSensorList;
  150. return static_cast<ssize_t>(mSensors.size());
  151. }
  152. Sensor const* SensorManager::getDefaultSensor(int type)
  153. {
  154. Mutex::Autolock _l(mLock);
  155. if (assertStateLocked() == NO_ERROR) {
  156. bool wakeUpSensor = false;
  157. // For the following sensor types, return a wake-up sensor. These types are by default
  158. // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return
  159. // a non_wake-up version.
  160. if (type == SENSOR_TYPE_PROXIMITY || type == SENSOR_TYPE_SIGNIFICANT_MOTION ||
  161. type == SENSOR_TYPE_TILT_DETECTOR || type == SENSOR_TYPE_WAKE_GESTURE ||
  162. type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE) {
  163. wakeUpSensor = true;
  164. }
  165. // For now we just return the first sensor of that type we find.
  166. // in the future it will make sense to let the SensorService make
  167. // that decision.
  168. for (size_t i=0 ; i<mSensors.size() ; i++) {
  169. if (mSensorList[i]->getType() == type &&
  170. mSensorList[i]->isWakeUpSensor() == wakeUpSensor) {
  171. return mSensorList[i];
  172. }
  173. }
  174. }
  175. return NULL;
  176. }
  177. sp<SensorEventQueue> SensorManager::createEventQueue(String8 packageName, int mode) {
  178. sp<SensorEventQueue> queue;
  179. Mutex::Autolock _l(mLock);
  180. while (assertStateLocked() == NO_ERROR) {
  181. sp<ISensorEventConnection> connection =
  182. mSensorServer->createSensorEventConnection(packageName, mode, mOpPackageName);
  183. if (connection == NULL) {
  184. // SensorService just died or the app doesn't have required permissions.
  185. ALOGE("createEventQueue: connection is NULL.");
  186. return NULL;
  187. }
  188. queue = new SensorEventQueue(connection);
  189. break;
  190. }
  191. return queue;
  192. }
  193. bool SensorManager::isDataInjectionEnabled() {
  194. Mutex::Autolock _l(mLock);
  195. if (assertStateLocked() == NO_ERROR) {
  196. return mSensorServer->isDataInjectionEnabled();
  197. }
  198. return false;
  199. }
  200. // ----------------------------------------------------------------------------
  201. }; // namespace android