Home | History | Annotate | Download | only in client
      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 #include <nativepower/wake_lock.h>
     18 
     19 #include <base/logging.h>
     20 #include <binderwrapper/binder_wrapper.h>
     21 #include <nativepower/power_manager_client.h>
     22 #include <powermanager/IPowerManager.h>
     23 #include <powermanager/PowerManager.h>
     24 
     25 namespace android {
     26 
     27 WakeLock::WakeLock(const std::string& tag,
     28                    const std::string& package,
     29                    PowerManagerClient* client)
     30     : acquired_lock_(false),
     31       tag_(tag),
     32       package_(package),
     33       client_(client) {
     34   DCHECK(client_);
     35 }
     36 
     37 WakeLock::~WakeLock() {
     38   sp<IPowerManager> power_manager = client_->power_manager();
     39   if (acquired_lock_ && power_manager.get()) {
     40     status_t status =
     41         power_manager->releaseWakeLock(lock_binder_, 0 /* flags */);
     42     if (status != OK) {
     43       LOG(ERROR) << "Wake lock release request for \"" << tag_ << "\" failed "
     44                  << "with status " << status;
     45     }
     46   }
     47 }
     48 
     49 bool WakeLock::Init() {
     50   sp<IPowerManager> power_manager = client_->power_manager();
     51   if (!power_manager.get()) {
     52     LOG(ERROR) << "Can't acquire wake lock for \"" << tag_ << "\"; no "
     53                << "connection to power manager";
     54     return false;
     55   }
     56 
     57   lock_binder_ = BinderWrapper::Get()->CreateLocalBinder();
     58   status_t status = power_manager->acquireWakeLock(
     59       POWERMANAGER_PARTIAL_WAKE_LOCK,
     60       lock_binder_, String16(tag_.c_str()), String16(package_.c_str()));
     61   if (status != OK) {
     62     LOG(ERROR) << "Wake lock acquire request for \"" << tag_ << "\" failed "
     63                << "with status " << status;
     64     return false;
     65   }
     66 
     67   acquired_lock_ = true;
     68   return true;
     69 }
     70 
     71 }  // namespace android
     72