1 /* 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef POWER_MODE_THREAD_H 17 #define POWER_MODE_THREAD_H 18 19 #include <map> 20 #include "ipc_skeleton.h" 21 #include <common_event_data.h> 22 #include <common_event_manager.h> 23 #include <common_event_publish_info.h> 24 #include <common_event_support.h> 25 26 #include "ipower_mode_callback.h" 27 28 #define FLAG_FALSE (-1) 29 #define LAST_MODE_FLAG 0 30 #define SETTINGS_PRIVIDER_VALUE_LCD_BRIGHTNESS 99 31 #define SETTINGS_PRIVIDER_VALUE_VIBRATION 1 32 #define SETTINGS_PRIVIDER_VALUE_ROTATION 1 33 34 namespace OHOS { 35 namespace PowerMgr { 36 class PowerModeModule { 37 public: 38 PowerModeModule(); 39 ~PowerModeModule() = default; 40 void InitPowerMode(); 41 void SetModeItem(PowerMode mode); 42 PowerMode GetModeItem(); 43 void EnableMode(PowerMode mode, bool isBoot = false); 44 void AddPowerModeCallback(const sptr<IPowerModeCallback>& callback); 45 void DelPowerModeCallback(const sptr<IPowerModeCallback>& callback); 46 47 private: 48 using IntentWant = OHOS::AAFwk::Want; 49 50 class CallbackManager : public IRemoteObject::DeathRecipient { 51 public: 52 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 53 void AddCallback(const sptr<IPowerModeCallback>& callback); 54 void RemoveCallback(const sptr<IPowerModeCallback>& callback); 55 void WaitingCallback(); 56 57 private: 58 void AddCallbackPidUid(const sptr<IRemoteObject>& callback); 59 void RemoveCallbackPidUid(const sptr<IRemoteObject>& callback); 60 std::pair<int32_t, int32_t> FindCallbackPidUid(const sptr<IRemoteObject>& callback); 61 std::mutex mutex_; 62 std::set<sptr<IRemoteObject>> callbacks_; 63 std::map<sptr<IRemoteObject>, std::pair<int32_t, int32_t>> cachedRegister_; 64 }; 65 66 PowerMode mode_; 67 uint32_t lastMode_; 68 bool observerRegisted_ = false; 69 70 void Prepare(); 71 void PublishPowerModeEvent(); 72 void UnregisterSaveModeObserver(); 73 void RegisterSaveModeObserver(); 74 void RegisterAutoAdjustBrightnessObserver(); 75 void RegisterAutoWindowRotationObserver(); 76 void RegisterVibrateStateObserver(); 77 void RegisterIntellVoiceObserver(); 78 79 sptr<CallbackManager> callbackMgr_; 80 void UpdateModepolicy(); 81 void RunAction(bool isBoot); 82 static void SetDisplayOffTime(bool isBoot); 83 static void SetSleepTime([[maybe_unused]] bool isBoot); 84 static void SetAutoAdjustBrightness(bool isBoot); 85 static void SetLcdBrightness(bool isBoot); 86 static void SetVibration(bool isBoot); 87 static void SetWindowRotation(bool isBoot); 88 static void SetIntellVoiceState(bool isBoot); 89 90 std::atomic<bool> started_; 91 std::mutex mutex_; 92 }; 93 } // namespace PowerMgr 94 } // namespace OHOS 95 #endif // POWER_MODE_THREAD_H 96