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 <string> 18 19 #include <base/macros.h> 20 #include <base/memory/weak_ptr.h> 21 #include <base/time/time.h> 22 #include <nativepower/wake_lock.h> 23 #include <powermanager/IPowerManager.h> 24 #include <utils/StrongPointer.h> 25 26 namespace android { 27 28 // Reasons that can be passed to PowerManagerClient::Suspend(). 29 enum class SuspendReason { 30 // These values must match the ones in android.os.PowerManager. 31 APPLICATION = 0, 32 DEVICE_ADMIN = 1, 33 TIMEOUT = 2, 34 LID_SWITCH = 3, 35 POWER_BUTTON = 4, 36 HDMI = 5, 37 SLEEP_BUTTON = 6, 38 }; 39 40 enum class SuspendFlags { 41 // Corresponds to GO_TO_SLEEP_FLAG_NO_DOZE in android.os.PowerManager. 42 NO_DOZE = 1 << 0, 43 }; 44 45 // Reasons that can be passed to PowerManagerClient::ShutDown(). 46 enum class ShutdownReason { 47 DEFAULT, 48 USER_REQUESTED, 49 }; 50 51 // Reasons that can be passed to PowerManagerClient::Reboot(). 52 enum class RebootReason { 53 DEFAULT, 54 RECOVERY, 55 }; 56 57 // Class used to communicate with the system power manager. 58 // 59 // android::BinderWrapper must be initialized before constructing this class. 60 class PowerManagerClient { 61 public: 62 PowerManagerClient(); 63 ~PowerManagerClient(); 64 65 // This should not be used directly; it's just exposed for WakeLock. 66 const sp<IPowerManager>& power_manager() { return power_manager_; } 67 68 // Initializes the object, returning true on success. Must be called before 69 // any other methods. 70 bool Init(); 71 72 // Creates and returns a wake lock identified by |tag| and |package|. The 73 // returned WakeLock object will block power management until it is destroyed. 74 // An empty pointer is returned on failure (e.g. due to issues communicating 75 // with the power manager). 76 std::unique_ptr<WakeLock> CreateWakeLock(const std::string& tag, 77 const std::string& package); 78 79 // Suspends the system immediately, returning true on success. 80 // 81 // |event_uptime| contains the time since the system was booted (e.g. 82 // base::SysInfo::Uptime()) of the event that triggered the suspend request. 83 // It is used to avoid acting on stale suspend requests that are sent before 84 // the currently-active suspend request completes. 85 // |reason| is currently only used by android.view.WindowManagerPolicy. 86 // |flags| is a bitfield of SuspendFlag values. 87 bool Suspend(base::TimeDelta event_uptime, SuspendReason reason, int flags); 88 89 // Shuts down or reboots the system, returning true on success. 90 bool ShutDown(ShutdownReason reason); 91 bool Reboot(RebootReason reason); 92 93 private: 94 // Called in response to |power_manager_|'s binder dying. 95 void OnPowerManagerDied(); 96 97 // Interface for communicating with the power manager. 98 sp<IPowerManager> power_manager_; 99 100 // Keep this member last. 101 base::WeakPtrFactory<PowerManagerClient> weak_ptr_factory_; 102 103 DISALLOW_COPY_AND_ASSIGN(PowerManagerClient); 104 }; 105 106 } // namespace android 107