Home | History | Annotate | Download | only in daemon
      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 <base/format_macros.h>
     18 #include <base/logging.h>
     19 #include <base/strings/stringprintf.h>
     20 #include <binderwrapper/binder_wrapper.h>
     21 #include <nativepower/power_manager_stub.h>
     22 #include <utils/String8.h>
     23 
     24 #include "wake_lock_manager_stub.h"
     25 
     26 namespace android {
     27 
     28 PowerManagerStub::SuspendRequest::SuspendRequest(int64_t event_time_ms,
     29                                                  int reason,
     30                                                  int flags)
     31     : event_time_ms(event_time_ms),
     32       reason(reason),
     33       flags(flags) {}
     34 
     35 // static
     36 std::string PowerManagerStub::ConstructWakeLockString(
     37     const std::string& tag,
     38     const std::string& package,
     39     uid_t uid) {
     40   return WakeLockManagerStub::ConstructRequestString(tag, package, uid);
     41 }
     42 
     43 // static
     44 std::string PowerManagerStub::ConstructSuspendRequestString(
     45     int64_t event_time_ms,
     46     int reason,
     47     int flags) {
     48   return base::StringPrintf("%" PRId64 ",%d,%d", event_time_ms, reason, flags);
     49 }
     50 
     51 PowerManagerStub::PowerManagerStub()
     52     : wake_lock_manager_(new WakeLockManagerStub()) {}
     53 
     54 PowerManagerStub::~PowerManagerStub() = default;
     55 
     56 int PowerManagerStub::GetNumWakeLocks() const {
     57   return wake_lock_manager_->num_requests();
     58 }
     59 
     60 std::string PowerManagerStub::GetWakeLockString(
     61     const sp<IBinder>& binder) const {
     62   return wake_lock_manager_->GetRequestString(binder);
     63 }
     64 
     65 std::string PowerManagerStub::GetSuspendRequestString(size_t index) const {
     66   if (index >= suspend_requests_.size())
     67     return std::string();
     68 
     69   const SuspendRequest& request = suspend_requests_[index];
     70   return ConstructSuspendRequestString(request.event_time_ms, request.reason,
     71                                        request.flags);
     72 }
     73 
     74 status_t PowerManagerStub::acquireWakeLock(int flags,
     75                                            const sp<IBinder>& lock,
     76                                            const String16& tag,
     77                                            const String16& packageName,
     78                                            bool isOneWay) {
     79   CHECK(wake_lock_manager_->AddRequest(lock, String8(tag).string(),
     80                                        String8(packageName).string(),
     81                                        BinderWrapper::Get()->GetCallingUid()));
     82   return OK;
     83 }
     84 
     85 status_t PowerManagerStub::acquireWakeLockWithUid(int flags,
     86                                                   const sp<IBinder>& lock,
     87                                                   const String16& tag,
     88                                                   const String16& packageName,
     89                                                   int uid,
     90                                                   bool isOneWay) {
     91   CHECK(wake_lock_manager_->AddRequest(lock, String8(tag).string(),
     92                                        String8(packageName).string(),
     93                                        static_cast<uid_t>(uid)));
     94   return OK;
     95 }
     96 
     97 status_t PowerManagerStub::releaseWakeLock(const sp<IBinder>& lock,
     98                                            int flags,
     99                                            bool isOneWay) {
    100   CHECK(wake_lock_manager_->RemoveRequest(lock));
    101   return OK;
    102 }
    103 
    104 status_t PowerManagerStub::updateWakeLockUids(const sp<IBinder>& lock,
    105                                               int len,
    106                                               const int* uids,
    107                                               bool isOneWay) {
    108   return OK;
    109 }
    110 
    111 status_t PowerManagerStub::powerHint(int hintId, int data) {
    112   return OK;
    113 }
    114 
    115 status_t PowerManagerStub::goToSleep(int64_t event_time_ms,
    116                                      int reason,
    117                                      int flags) {
    118   suspend_requests_.emplace_back(event_time_ms, reason, flags);
    119   return OK;
    120 }
    121 
    122 status_t PowerManagerStub::reboot(bool confirm,
    123                                   const String16& reason,
    124                                   bool wait) {
    125   reboot_reasons_.push_back(String8(reason).string());
    126   return OK;
    127 }
    128 
    129 status_t PowerManagerStub::shutdown(bool confirm,
    130                                     const String16& reason,
    131                                     bool wait) {
    132   shutdown_reasons_.push_back(String8(reason).string());
    133   return OK;
    134 }
    135 
    136 status_t PowerManagerStub::crash(const String16& message) {
    137   return OK;
    138 }
    139 
    140 }  // namespace android
    141