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