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 
AWakeLock()31 AWakeLock::AWakeLock() :
32     mPowerManager(NULL),
33     mWakeLockToken(NULL),
34     mWakeLockCount(0),
35     mDeathRecipient(new PMDeathRecipient(this)) {}
36 
~AWakeLock()37 AWakeLock::~AWakeLock() {
38     if (mPowerManager != NULL) {
39         sp<IBinder> binder = IInterface::asBinder(mPowerManager);
40         binder->unlinkToDeath(mDeathRecipient);
41     }
42     clearPowerManager();
43 }
44 
acquire()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 {
55                 mPowerManager = interface_cast<os::IPowerManager>(binder);
56                 binder->linkToDeath(mDeathRecipient);
57             }
58         }
59         if (mPowerManager != NULL) {
60             sp<IBinder> binder = new BBinder();
61             int64_t token = IPCThreadState::self()->clearCallingIdentity();
62             binder::Status status = mPowerManager->acquireWakeLock(
63                     binder, POWERMANAGER_PARTIAL_WAKE_LOCK,
64                     String16("AWakeLock"), String16("media"),
65                     {} /* workSource */, {} /* historyTag */, -1 /* displayId */);
66             IPCThreadState::self()->restoreCallingIdentity(token);
67             if (status.isOk()) {
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 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 
clearPowerManager()99 void AWakeLock::clearPowerManager() {
100     release(true);
101     mPowerManager.clear();
102 }
103 
binderDied(const wp<IBinder> & who __unused)104 void AWakeLock::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) {
105     if (mWakeLock != NULL) {
106         mWakeLock->clearPowerManager();
107     }
108 }
109 
110 }  // namespace android
111