1 /* 2 * Copyright (c) 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 IAM_CONTEXT_APPSTATE_OBSERVER_H 17 #define IAM_CONTEXT_APPSTATE_OBSERVER_H 18 19 #include <cstdint> 20 #include <map> 21 #include <mutex> 22 #include <string> 23 24 #include "application_state_observer_stub.h" 25 #include "app_mgr_interface.h" 26 #include "context_callback.h" 27 #include "if_system_ability_manager.h" 28 #include "iservice_registry.h" 29 30 namespace OHOS { 31 namespace UserIam { 32 namespace UserAuth { 33 using namespace OHOS::AppExecFwk; 34 class ContextAppStateObserverManager { 35 public: 36 ContextAppStateObserverManager() = default; 37 ~ContextAppStateObserverManager() = default; 38 static ContextAppStateObserverManager &GetInstance(); 39 40 void SubscribeAppState(const std::shared_ptr<ContextCallback> &callback, const uint64_t contextId); 41 void UnSubscribeAppState(); 42 void SetScreenLockState(bool screenLockState, int32_t userId); 43 void RemoveScreenLockState(int32_t userId); 44 bool GetScreenLockState(int32_t userId); 45 46 protected: 47 sptr<ApplicationStateObserverStub> appStateObserver_ = nullptr; 48 49 private: 50 sptr<IAppMgr> GetAppManagerInstance(); 51 std::mutex mutex_; 52 std::map<int32_t, bool> screenLockedMap_; 53 }; 54 55 class ContextAppStateObserver : public ApplicationStateObserverStub { 56 public: 57 ContextAppStateObserver(const uint64_t contextId, const std::string bundleName); 58 ~ContextAppStateObserver() override = default; 59 void OnAppStateChanged(const AppStateData &appStateData) override; 60 void OnForegroundApplicationChanged(const AppStateData &appStateData) override; 61 62 protected: 63 void ProcAppStateChanged(int32_t userId); 64 const uint64_t contextId_ = INVALID_CONTEXT_ID; 65 const std::string bundleName_; 66 }; 67 } // namespace UserAuth 68 } // namespace UserIam 69 } // namespace OHOS 70 #endif // IAM_CONTEXT_APPSTATE_OBSERVER_H