1 /*
2 * Copyright 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
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "JWakeLock"
19 #include <utils/Log.h>
20
21 #include "JWakeLock.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 //TODO: use JAVA PowerManager, instead of binder
JWakeLock()32 JWakeLock::JWakeLock() :
33 mPowerManager(NULL),
34 mWakeLockToken(NULL),
35 mWakeLockCount(0),
36 mDeathRecipient(new PMDeathRecipient(this)) {}
37
~JWakeLock()38 JWakeLock::~JWakeLock() {
39 if (mPowerManager != NULL) {
40 sp<IBinder> binder = IInterface::asBinder(mPowerManager);
41 binder->unlinkToDeath(mDeathRecipient);
42 }
43 clearPowerManager();
44 }
45
acquire()46 bool JWakeLock::acquire() {
47 if (mWakeLockCount == 0) {
48 CHECK(mWakeLockToken == NULL);
49 if (mPowerManager == NULL) {
50 // use checkService() to avoid blocking if power service is not up yet
51 sp<IBinder> binder =
52 defaultServiceManager()->checkService(String16("power"));
53 if (binder == NULL) {
54 ALOGW("could not get the power manager service");
55 } else {
56 mPowerManager = interface_cast<IPowerManager>(binder);
57 binder->linkToDeath(mDeathRecipient);
58 }
59 }
60 if (mPowerManager != NULL) {
61 sp<IBinder> binder = new BBinder();
62 int64_t token = IPCThreadState::self()->clearCallingIdentity();
63 status_t status = mPowerManager->acquireWakeLock(
64 POWERMANAGER_PARTIAL_WAKE_LOCK,
65 binder, String16("JWakeLock"), String16("media"));
66 IPCThreadState::self()->restoreCallingIdentity(token);
67 if (status == NO_ERROR) {
68 mWakeLockToken = binder;
69 mWakeLockCount++;
70 return true;
71 }
72 }
73 } else {
74 mWakeLockCount++;
75 return true;
76 }
77 return false;
78 }
79
release(bool force)80 void JWakeLock::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
clearPowerManager()99 void JWakeLock::clearPowerManager() {
100 release(true);
101 mPowerManager.clear();
102 }
103
binderDied(const wp<IBinder> & who __unused)104 void JWakeLock::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) {
105 if (mWakeLock != NULL) {
106 mWakeLock->clearPowerManager();
107 }
108 }
109
110 } // namespace android
111