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 OHOS_MULTI_SIM_MONITOR_H 17 #define OHOS_MULTI_SIM_MONITOR_H 18 19 #include <list> 20 #include <ffrt.h> 21 22 #include "common_event_subscriber.h" 23 #include "iservice_registry.h" 24 #include "multi_sim_controller.h" 25 #include "os_account_manager_wrapper.h" 26 #include "sim_constant.h" 27 #include "sim_file_manager.h" 28 #include "system_ability_definition.h" 29 #include "system_ability_status_change_stub.h" 30 #include "tel_event_handler.h" 31 #include "telephony_log_wrapper.h" 32 #include "telephony_state_registry_client.h" 33 34 namespace OHOS { 35 namespace Telephony { 36 using namespace OHOS::EventFwk; 37 using CommonEventSubscribeInfo = OHOS::EventFwk::CommonEventSubscribeInfo; 38 using CommonEventSubscriber = OHOS::EventFwk::CommonEventSubscriber; 39 class MultiSimMonitor : public TelEventHandler { 40 public: 41 explicit MultiSimMonitor(const std::shared_ptr<MultiSimController> &controller, 42 std::vector<std::shared_ptr<Telephony::SimStateManager>> simStateManager, 43 std::vector<std::weak_ptr<Telephony::SimFileManager>> simFileManager); 44 ~MultiSimMonitor(); 45 46 void Init(); 47 void AddExtraManagers(std::shared_ptr<Telephony::SimStateManager> simStateManager, 48 std::shared_ptr<Telephony::SimFileManager> simFileManager); 49 void RegisterCoreNotify(int32_t slotId, const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what); 50 int32_t RegisterSimAccountCallback(const int32_t tokenId, const sptr<SimAccountCallback> &callback); 51 int32_t UnregisterSimAccountCallback(const sptr<SimAccountCallback> &callback); 52 void NotifySimAccountChanged(); 53 void RegisterSimNotify(); 54 void RegisterSimNotify(int32_t slotId); 55 void UnRegisterSimNotify(); 56 int32_t ResetSimLoadAccount(int32_t slotId); 57 bool IsVSimSlotId(int32_t slotId); 58 59 public: 60 enum { 61 REGISTER_SIM_NOTIFY_EVENT = 0, 62 RESET_OPKEY_CONFIG = 1, 63 REGISTER_SIM_NOTIFY_RETRY_EVENT = 2, 64 INIT_DATA_RETRY_EVENT = 3, 65 RETRY_RESET_OPKEY_CONFIG = 4, 66 INIT_ESIM_DATA_RETRY_EVENT = 5, 67 }; 68 69 private: 70 struct SimAccountCallbackRecord { 71 int32_t tokenId = 0; 72 sptr<SimAccountCallback> simAccountCallback = nullptr; 73 }; 74 sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr}; 75 76 private: 77 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event); 78 void RefreshData(int32_t slotId); 79 void InitData(int32_t slotId); 80 void InitEsimData(); 81 bool IsValidSlotId(int32_t slotId); 82 std::list<SimAccountCallbackRecord> GetSimAccountCallbackRecords(); 83 void InitListener(); 84 void SubscribeDataShareReady(); 85 void SubscribeUserSwitch(); 86 void SubscribeBundleScanFinished(); 87 void UnSubscribeListeners(); 88 void CheckOpcNeedUpdata(const bool isDataShareError); 89 int32_t CheckUpdateOpcVersion(); 90 void UpdateAllOpkeyConfigs(); 91 void CheckDataShareError(); 92 void CheckSimNotifyRegister(); 93 void SetRemainCount(int remainCount); 94 void SetBlockLoadOperatorConfig(bool isBlockLoadOperatorConfig); 95 bool GetBlockLoadOperatorConfig(); 96 97 private: 98 class DataShareEventSubscriber : public CommonEventSubscriber { 99 public: DataShareEventSubscriber(const CommonEventSubscribeInfo & info,MultiSimMonitor & handler)100 explicit DataShareEventSubscriber( 101 const CommonEventSubscribeInfo &info, MultiSimMonitor &handler) 102 : CommonEventSubscriber(info), handler_(handler) {} 103 ~DataShareEventSubscriber() = default; 104 void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override; 105 MultiSimMonitor &handler_; 106 }; 107 108 class UserSwitchEventSubscriber : public CommonEventSubscriber { 109 public: UserSwitchEventSubscriber(const CommonEventSubscribeInfo & info,MultiSimMonitor & handler)110 explicit UserSwitchEventSubscriber( 111 const CommonEventSubscribeInfo &info, MultiSimMonitor &handler) 112 : CommonEventSubscriber(info), handler_(handler) {} 113 ~UserSwitchEventSubscriber() = default; 114 void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override; 115 MultiSimMonitor &handler_; 116 }; 117 118 class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { 119 public: SystemAbilityStatusChangeListener(MultiSimMonitor & handler)120 explicit SystemAbilityStatusChangeListener(MultiSimMonitor &handler) : handler_(handler) {}; 121 ~SystemAbilityStatusChangeListener() = default; 122 virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 123 virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 124 125 private: 126 MultiSimMonitor &handler_; 127 }; 128 129 private: 130 std::shared_ptr<MultiSimController> controller_ = nullptr; 131 std::vector<std::shared_ptr<Telephony::SimStateManager>> simStateManager_; 132 std::vector<std::weak_ptr<Telephony::SimFileManager>> simFileManager_; 133 std::vector<int> isSimAccountLoaded_; 134 bool isAllSimAccountLoaded_ = false; 135 std::vector<int> initDataRemainCount_; 136 int initEsimDataRemainCount_ = 0; 137 std::unique_ptr<ObserverHandler> observerHandler_ = nullptr; 138 std::list<SimAccountCallbackRecord> listSimAccountCallbackRecord_; 139 std::shared_ptr<DataShareEventSubscriber> dataShareSubscriber_ = nullptr; 140 std::shared_ptr<UserSwitchEventSubscriber> userSwitchSubscriber_ = nullptr; 141 sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr; 142 std::mutex mutexInner_; 143 std::mutex mutexForData_; 144 std::atomic<int32_t> remainCount_ = 15; 145 int32_t maxSlotCount_ = 0; 146 bool isDataShareReady_ = false; 147 bool isForgetAllDataDone_ = false; 148 ffrt::shared_mutex simStateMgrMutex_; 149 }; 150 } // namespace Telephony 151 } // namespace OHOS 152 #endif // OHOS_MULTI_SIM_MONITOR_H 153