ActivityManager.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C) 2017 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 <mutex>
  17. #include <unistd.h>
  18. #include <binder/ActivityManager.h>
  19. #include <binder/Binder.h>
  20. #include <binder/IServiceManager.h>
  21. #include <utils/SystemClock.h>
  22. namespace android {
  23. ActivityManager::ActivityManager()
  24. {
  25. }
  26. sp<IActivityManager> ActivityManager::getService()
  27. {
  28. std::lock_guard<Mutex> scoped_lock(mLock);
  29. int64_t startTime = 0;
  30. sp<IActivityManager> service = mService;
  31. while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
  32. sp<IBinder> binder = defaultServiceManager()->checkService(String16("activity"));
  33. if (binder == nullptr) {
  34. // Wait for the activity service to come back...
  35. if (startTime == 0) {
  36. startTime = uptimeMillis();
  37. ALOGI("Waiting for activity service");
  38. } else if ((uptimeMillis() - startTime) > 1000000) {
  39. ALOGW("Waiting too long for activity service, giving up");
  40. service = nullptr;
  41. break;
  42. }
  43. usleep(25000);
  44. } else {
  45. service = interface_cast<IActivityManager>(binder);
  46. mService = service;
  47. }
  48. }
  49. return service;
  50. }
  51. int ActivityManager::openContentUri(const String16& stringUri)
  52. {
  53. sp<IActivityManager> service = getService();
  54. return service != nullptr ? service->openContentUri(stringUri) : -1;
  55. }
  56. void ActivityManager::registerUidObserver(const sp<IUidObserver>& observer,
  57. const int32_t event,
  58. const int32_t cutpoint,
  59. const String16& callingPackage)
  60. {
  61. sp<IActivityManager> service = getService();
  62. if (service != nullptr) {
  63. service->registerUidObserver(observer, event, cutpoint, callingPackage);
  64. }
  65. }
  66. void ActivityManager::unregisterUidObserver(const sp<IUidObserver>& observer)
  67. {
  68. sp<IActivityManager> service = getService();
  69. if (service != nullptr) {
  70. service->unregisterUidObserver(observer);
  71. }
  72. }
  73. bool ActivityManager::isUidActive(const uid_t uid, const String16& callingPackage)
  74. {
  75. sp<IActivityManager> service = getService();
  76. if (service != nullptr) {
  77. return service->isUidActive(uid, callingPackage);
  78. }
  79. return false;
  80. }
  81. int32_t ActivityManager::getUidProcessState(const uid_t uid, const String16& callingPackage)
  82. {
  83. sp<IActivityManager> service = getService();
  84. if (service != nullptr) {
  85. return service->getUidProcessState(uid, callingPackage);
  86. }
  87. return PROCESS_STATE_UNKNOWN;
  88. }
  89. status_t ActivityManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
  90. sp<IActivityManager> service = getService();
  91. if (service != nullptr) {
  92. return IInterface::asBinder(service)->linkToDeath(recipient);
  93. }
  94. return INVALID_OPERATION;
  95. }
  96. status_t ActivityManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
  97. sp<IActivityManager> service = getService();
  98. if (service != nullptr) {
  99. return IInterface::asBinder(service)->unlinkToDeath(recipient);
  100. }
  101. return INVALID_OPERATION;
  102. }
  103. }; // namespace android