1 /*
2  * Copyright (C) 2021 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 TELEPHONY_SIMSTATETRACKER_H
17 #define TELEPHONY_SIMSTATETRACKER_H
18 
19 #include <string>
20 
21 #include "common_event_subscriber.h"
22 #include "iservice_registry.h"
23 #include "operator_config_cache.h"
24 #include "operator_config_loader.h"
25 #include "system_ability_definition.h"
26 #include "system_ability_status_change_stub.h"
27 #include "telephony_log_wrapper.h"
28 
29 namespace OHOS {
30 namespace Telephony {
31 using namespace OHOS::EventFwk;
32 using CommonEventSubscribeInfo = OHOS::EventFwk::CommonEventSubscribeInfo;
33 using CommonEventSubscriber = OHOS::EventFwk::CommonEventSubscriber;
34 class SimStateTracker : public TelEventHandler {
35 public:
36     SimStateTracker(std::weak_ptr<SimFileManager> simFileManager,
37         std::shared_ptr<OperatorConfigCache> operatorConfigCache, int32_t slotId);
38     ~SimStateTracker();
39     void InitListener();
40     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event);
41     bool RegisterForIccLoaded();
42     bool RegisterOpkeyLoaded();
43     bool RegisterOperatorCacheDel();
44     bool UnRegisterForIccLoaded();
45     bool UnRegisterOpkeyLoaded();
46     bool UnregisterOperatorCacheDel();
47 
48     std::shared_ptr<OperatorConfigLoader> operatorConfigLoader_ = nullptr;
49 
50 private:
51     inline static const std::string OPERATOR_CONFIG_CHANGED = "operatorConfigChanged";
52     std::weak_ptr<SimFileManager> simFileManager_;
53     sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr;
54     std::shared_ptr<OperatorConfigCache> operatorConfigCache_ = nullptr;
55     int32_t slotId_;
56     OperatorConfig config_;
57     void ProcessSimRecordLoad(const AppExecFwk::InnerEvent::Pointer &event);
58     void ProcessSimOpkeyLoad(const AppExecFwk::InnerEvent::Pointer &event);
59     void ProcessOperatorCacheDel(const AppExecFwk::InnerEvent::Pointer &event);
60 
61 private:
62     class UserSwitchEventSubscriber : public CommonEventSubscriber {
63     public:
UserSwitchEventSubscriber(const CommonEventSubscribeInfo & info,int32_t slotId,std::shared_ptr<OperatorConfigLoader> configLoader)64         explicit UserSwitchEventSubscriber(
65             const CommonEventSubscribeInfo &info, int32_t slotId, std::shared_ptr<OperatorConfigLoader> configLoader)
66             : CommonEventSubscriber(info), slotId_(slotId), configLoader_(configLoader)
67         {}
68         ~UserSwitchEventSubscriber() = default;
69         void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override;
70 
71     private:
72         const int32_t slotId_;
73         std::shared_ptr<OperatorConfigLoader> configLoader_ = nullptr;
74     };
75     class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub {
76     public:
77         explicit SystemAbilityStatusChangeListener(int32_t slotId, std::shared_ptr<OperatorConfigLoader> configLoader);
78         ~SystemAbilityStatusChangeListener() = default;
79         virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
80         virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
81 
82     private:
83         const int32_t slotId_;
84         std::shared_ptr<OperatorConfigLoader> configLoader_ = nullptr;
85         std::shared_ptr<UserSwitchEventSubscriber> userSwitchSubscriber_ = nullptr;
86         bool isUserSwitchSubscribered = false;
87     };
88 };
89 } // namespace Telephony
90 } // namespace OHOS
91 #endif // TELEPHONY_SIMSTATETRACKER_H