/* * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef POWERMGR_RUNNING_LOCK_MGR_H #define POWERMGR_RUNNING_LOCK_MGR_H #include #include #include #include "actions/irunning_lock_action.h" #include "running_lock_inner.h" #include "running_lock_proxy.h" #include "running_lock_token_stub.h" #include "running_lock_info.h" #include "ipower_runninglock_callback.h" #ifdef HAS_SENSORS_SENSOR_PART #include "sensor_agent.h" #endif namespace OHOS { namespace PowerMgr { class PowerMgrService; class PowerStateMachine; using RunningLockMap = std::map, std::shared_ptr>; class RunningLockMgr { public: enum { PROXIMITY_AWAY = 0, PROXIMITY_CLOSE }; explicit RunningLockMgr(const wptr& pms) : pms_(pms) {} ~RunningLockMgr(); std::shared_ptr CreateRunningLock(const sptr& remoteObj, const RunningLockParam& runningLockParam); bool ReleaseLock(const sptr remoteObj, const std::string& name = ""); bool UpdateWorkSource(const sptr& remoteObj, const std::map& workSources); bool Lock(const sptr& remoteObj); bool UnLock(const sptr remoteObj, const std::string& name = ""); void RegisterRunningLockCallback(const sptr& callback); void UnRegisterRunningLockCallback(const sptr& callback); void QueryRunningLockLists(std::map& runningLockLists); uint32_t GetRunningLockNum(RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT); uint32_t GetValidRunningLockNum(RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT); bool Init(); bool ExistValidRunningLock(); std::shared_ptr GetRunningLockInner(const sptr& remoteObj); std::shared_ptr GetRunningLockInnerByName(const std::string& name); const RunningLockMap& GetRunningLockMap() const { return runningLocks_; } static void NotifyRunningLockChanged(const RunningLockParam& lockInnerParam, const std::string &tag); bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid); void ProxyRunningLocks(bool isProxied, const std::vector>& processInfos); void LockInnerByProxy(const sptr& remoteObj, std::shared_ptr& lockInner); void UnlockInnerByProxy(const sptr& remoteObj, std::shared_ptr& lockInner); void ResetRunningLocks(); bool IsUsed(const sptr& remoteObj); static constexpr uint32_t CHECK_TIMEOUT_INTERVAL_MS = 60 * 1000; void SetProximity(uint32_t status); bool IsProximityClose(); void DumpInfo(std::string& result); void EnableMock(IRunningLockAction* mockAction); private: void AsyncWakeup(); void InitLocksTypeScreen(); void InitLocksTypeBackground(); void InitLocksTypeProximity(); void InitLocksTypeCoordination(); class LockCounter { public: LockCounter(RunningLockType type, std::function activate) : type_(type), activate_(activate), counter_(0) {} ~LockCounter() = default; int32_t Increase(const RunningLockParam& lockInnerParam); int32_t Decrease(const RunningLockParam& lockInnerParam); void Clear(); uint32_t GetCount() { return counter_; } RunningLockType GetType() { return type_; } private: const RunningLockType type_; std::shared_ptr action_; std::function activate_; uint32_t counter_; }; #ifdef HAS_SENSORS_SENSOR_PART class ProximityController { public: ProximityController(); ~ProximityController(); void Enable(); void Disable(); bool IsEnabled() { return enabled_; } bool IsSupported() { return support_; } bool IsClose(); void OnClose(); void OnAway(); uint32_t GetStatus() { return status_; } void Clear(); static void RecordSensorCallback(SensorEvent *event); private: static const int32_t PROXIMITY_CLOSE_SCALAR = 0; static const int32_t PROXIMITY_AWAY_SCALAR = 5; static const uint32_t SAMPLING_RATE = 100000000; bool support_ {false}; bool enabled_ {false}; bool isClose_ {false}; uint32_t status_ {0}; SensorUser user_; }; ProximityController proximityController_; #endif class RunningLockDeathRecipient : public IRemoteObject::DeathRecipient { public: RunningLockDeathRecipient() = default; virtual void OnRemoteDied(const wptr& remote); virtual ~RunningLockDeathRecipient() = default; }; bool InitLocks(); static bool IsSceneRunningLockType(RunningLockType type); static bool NeedNotify(RunningLockType type); static uint64_t TransformLockid(const sptr& remoteObj); bool IsValidType(RunningLockType type); void PreprocessBeforeAwake(); void ProximityLockOn(); RunningLockInfo FillAppRunningLockInfo(const RunningLockParam& info); void UpdateUnSceneLockLists(RunningLockParam& singleLockParam, bool fill); const wptr pms_; std::mutex mutex_; std::mutex screenLockListsMutex_; RunningLockMap runningLocks_; std::map> lockCounters_; std::shared_ptr runninglockProxy_; sptr runningLockDeathRecipient_; std::shared_ptr runningLockAction_; std::map unSceneLockLists_; }; } // namespace PowerMgr } // namespace OHOS #endif // POWERMGR_RUNNING_LOCK_MGR_H