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
21 #include "AWakeLock.h"
22
23 #include <binder/IPCThreadState.h>
24 #include <binder/IServiceManager.h>
25 #include <media/stagefright/foundation/ADebug.h>
26 #include <powermanager/PowerManager.h>
27
28
29 namespace android {
30
31 const int32_t INVALID_DISPLAY_ID = -1;
32
AWakeLock()33 AWakeLock::AWakeLock() :
34 mPowerManager(NULL),
35 mWakeLockToken(NULL),
36 mWakeLockCount(0),
37 mDeathRecipient(new PMDeathRecipient(this)){}
38
~AWakeLock()39 AWakeLock::~AWakeLock() {
40 if (mPowerManager != NULL) {
41 sp<IBinder> binder = IInterface::asBinder(mPowerManager);
42 binder->unlinkToDeath(mDeathRecipient);
43 }
44 clearPowerManager();
45 }
46
acquire()47 bool AWakeLock::acquire() {
48 if (mWakeLockCount == 0) {
49 CHECK(mWakeLockToken == NULL);
50 if (mPowerManager == NULL) {
51 // use checkService() to avoid blocking if power service is not up yet
52 sp<IBinder> binder =
53 defaultServiceManager()->checkService(String16("power"));
54 if (binder == NULL) {
55 ALOGW("could not get the power manager service");
56 } else {
57 mPowerManager = interface_cast<os::IPowerManager>(binder);
58 binder->linkToDeath(mDeathRecipient);
59 }
60 }
61 if (mPowerManager != NULL) {
62 sp<IBinder> binder = new BBinder();
63 int64_t token = IPCThreadState::self()->clearCallingIdentity();
64 binder::Status status = mPowerManager->acquireWakeLock(
65 binder,
66 /*flags= */ POWERMANAGER_PARTIAL_WAKE_LOCK,
67 /*tag=*/ String16("AWakeLock"),
68 /*packageName=*/ String16("media"),
69 /*ws=*/ {},
70 /*historyTag=*/ {},
71 /*displayId=*/ INVALID_DISPLAY_ID,
72 /*callback=*/NULL);
73 IPCThreadState::self()->restoreCallingIdentity(token);
74 if (status.isOk()) {
75 mWakeLockToken = binder;
76 mWakeLockCount++;
77 ALOGI("AwakeLock acquired");
78 return true;
79 }
80 }
81 } else {
82 mWakeLockCount++;
83 return true;
84 }
85 return false;
86 }
87
release(bool force)88 void AWakeLock::release(bool force) {
89 if (mWakeLockCount == 0) {
90 return;
91 }
92 if (force) {
93 // Force wakelock release below by setting reference count to 1.
94 mWakeLockCount = 1;
95 }
96 if (--mWakeLockCount == 0) {
97 CHECK(mWakeLockToken != NULL);
98 if (mPowerManager != NULL) {
99 int64_t token = IPCThreadState::self()->clearCallingIdentity();
100 mPowerManager->releaseWakeLock(mWakeLockToken, 0 /* flags */);
101 IPCThreadState::self()->restoreCallingIdentity(token);
102 }
103 mWakeLockToken.clear();
104 ALOGI("AwakeLock released");
105 }
106 }
107
clearPowerManager()108 void AWakeLock::clearPowerManager() {
109 release(true);
110 mPowerManager.clear();
111 }
112
binderDied(const wp<IBinder> & who __unused)113 void AWakeLock::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) {
114 if (mWakeLock != NULL) {
115 mWakeLock->clearPowerManager();
116 }
117 }
118
119 } // namespace android
120