Weiyin Jiang | 35d5af1 | 2015-01-28 16:14:02 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "AWakeLock" |
| 19 | #include <utils/Log.h> |
| 20 | |
Weiyin Jiang | 35d5af1 | 2015-01-28 16:14:02 +0800 | [diff] [blame] | 21 | #include "AWakeLock.h" |
| 22 | |
| 23 | #include <binder/IPCThreadState.h> |
| 24 | #include <binder/IServiceManager.h> |
Dongwon Kang | 3dced07 | 2017-10-09 08:55:22 -0700 | [diff] [blame] | 25 | #include <media/stagefright/foundation/ADebug.h> |
Weiyin Jiang | 35d5af1 | 2015-01-28 16:14:02 +0800 | [diff] [blame] | 26 | #include <powermanager/PowerManager.h> |
| 27 | |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | AWakeLock::AWakeLock() : |
| 32 | mPowerManager(NULL), |
| 33 | mWakeLockToken(NULL), |
| 34 | mWakeLockCount(0), |
| 35 | mDeathRecipient(new PMDeathRecipient(this)) {} |
| 36 | |
| 37 | AWakeLock::~AWakeLock() { |
| 38 | if (mPowerManager != NULL) { |
Brian Carlstrom | c179438 | 2015-02-09 22:30:05 -0800 | [diff] [blame] | 39 | sp<IBinder> binder = IInterface::asBinder(mPowerManager); |
Weiyin Jiang | 35d5af1 | 2015-01-28 16:14:02 +0800 | [diff] [blame] | 40 | binder->unlinkToDeath(mDeathRecipient); |
| 41 | } |
| 42 | clearPowerManager(); |
| 43 | } |
| 44 | |
| 45 | bool AWakeLock::acquire() { |
| 46 | if (mWakeLockCount == 0) { |
| 47 | CHECK(mWakeLockToken == NULL); |
| 48 | if (mPowerManager == NULL) { |
| 49 | // use checkService() to avoid blocking if power service is not up yet |
| 50 | sp<IBinder> binder = |
| 51 | defaultServiceManager()->checkService(String16("power")); |
| 52 | if (binder == NULL) { |
| 53 | ALOGW("could not get the power manager service"); |
| 54 | } else { |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 55 | mPowerManager = interface_cast<os::IPowerManager>(binder); |
Weiyin Jiang | 35d5af1 | 2015-01-28 16:14:02 +0800 | [diff] [blame] | 56 | binder->linkToDeath(mDeathRecipient); |
| 57 | } |
| 58 | } |
| 59 | if (mPowerManager != NULL) { |
| 60 | sp<IBinder> binder = new BBinder(); |
| 61 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 62 | binder::Status status = mPowerManager->acquireWakeLock( |
| 63 | binder, POWERMANAGER_PARTIAL_WAKE_LOCK, |
| 64 | String16("AWakeLock"), String16("media"), |
| 65 | {} /* workSource */, {} /* historyTag */); |
Weiyin Jiang | 35d5af1 | 2015-01-28 16:14:02 +0800 | [diff] [blame] | 66 | IPCThreadState::self()->restoreCallingIdentity(token); |
Chris Ye | 6597d73 | 2020-02-28 22:38:25 -0800 | [diff] [blame] | 67 | if (status.isOk()) { |
Weiyin Jiang | 35d5af1 | 2015-01-28 16:14:02 +0800 | [diff] [blame] | 68 | mWakeLockToken = binder; |
| 69 | mWakeLockCount++; |
| 70 | return true; |
| 71 | } |
| 72 | } |
| 73 | } else { |
| 74 | mWakeLockCount++; |
| 75 | return true; |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | void AWakeLock::release(bool force) { |
| 81 | if (mWakeLockCount == 0) { |
| 82 | return; |
| 83 | } |
| 84 | if (force) { |
| 85 | // Force wakelock release below by setting reference count to 1. |
| 86 | mWakeLockCount = 1; |
| 87 | } |
| 88 | if (--mWakeLockCount == 0) { |
| 89 | CHECK(mWakeLockToken != NULL); |
| 90 | if (mPowerManager != NULL) { |
| 91 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 92 | mPowerManager->releaseWakeLock(mWakeLockToken, 0 /* flags */); |
| 93 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 94 | } |
| 95 | mWakeLockToken.clear(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void AWakeLock::clearPowerManager() { |
| 100 | release(true); |
| 101 | mPowerManager.clear(); |
| 102 | } |
| 103 | |
| 104 | void AWakeLock::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) { |
| 105 | if (mWakeLock != NULL) { |
| 106 | mWakeLock->clearPowerManager(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | } // namespace android |