blob: 7bee0022460ddc04f41f548d2fe5cbdf2130bd82 [file] [log] [blame]
Weiyin Jiang35d5af12015-01-28 16:14:02 +08001/*
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 Jiang35d5af12015-01-28 16:14:02 +080021#include "AWakeLock.h"
22
23#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
Dongwon Kang3dced072017-10-09 08:55:22 -070025#include <media/stagefright/foundation/ADebug.h>
Weiyin Jiang35d5af12015-01-28 16:14:02 +080026#include <powermanager/PowerManager.h>
27
28
29namespace android {
30
31AWakeLock::AWakeLock() :
32 mPowerManager(NULL),
33 mWakeLockToken(NULL),
34 mWakeLockCount(0),
35 mDeathRecipient(new PMDeathRecipient(this)) {}
36
37AWakeLock::~AWakeLock() {
38 if (mPowerManager != NULL) {
Brian Carlstromc1794382015-02-09 22:30:05 -080039 sp<IBinder> binder = IInterface::asBinder(mPowerManager);
Weiyin Jiang35d5af12015-01-28 16:14:02 +080040 binder->unlinkToDeath(mDeathRecipient);
41 }
42 clearPowerManager();
43}
44
45bool 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 Ye6597d732020-02-28 22:38:25 -080055 mPowerManager = interface_cast<os::IPowerManager>(binder);
Weiyin Jiang35d5af12015-01-28 16:14:02 +080056 binder->linkToDeath(mDeathRecipient);
57 }
58 }
59 if (mPowerManager != NULL) {
60 sp<IBinder> binder = new BBinder();
61 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Chris Ye6597d732020-02-28 22:38:25 -080062 binder::Status status = mPowerManager->acquireWakeLock(
63 binder, POWERMANAGER_PARTIAL_WAKE_LOCK,
64 String16("AWakeLock"), String16("media"),
65 {} /* workSource */, {} /* historyTag */);
Weiyin Jiang35d5af12015-01-28 16:14:02 +080066 IPCThreadState::self()->restoreCallingIdentity(token);
Chris Ye6597d732020-02-28 22:38:25 -080067 if (status.isOk()) {
Weiyin Jiang35d5af12015-01-28 16:14:02 +080068 mWakeLockToken = binder;
69 mWakeLockCount++;
70 return true;
71 }
72 }
73 } else {
74 mWakeLockCount++;
75 return true;
76 }
77 return false;
78}
79
80void 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
99void AWakeLock::clearPowerManager() {
100 release(true);
101 mPowerManager.clear();
102}
103
104void AWakeLock::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) {
105 if (mWakeLock != NULL) {
106 mWakeLock->clearPowerManager();
107 }
108}
109
110} // namespace android