1 /*
2  * Copyright (C) 2021-2022 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_CONTROLLER_H
17 #define OHOS_MULTI_SIM_CONTROLLER_H
18 
19 #include <list>
20 
21 #include "if_system_ability_manager.h"
22 #include "radio_protocol_controller.h"
23 #include "sim_constant.h"
24 #include "sim_file_manager.h"
25 #include "sim_rdb_helper.h"
26 #include "sim_state_manager.h"
27 #include "telephony_errors.h"
28 #include "want.h"
29 
30 namespace OHOS {
31 namespace Telephony {
32 class MultiSimController {
33 public:
34     MultiSimController(std::shared_ptr<Telephony::ITelRilManager> telRilManager,
35         std::vector<std::shared_ptr<Telephony::SimStateManager>> simStateManager,
36         std::vector<std::shared_ptr<Telephony::SimFileManager>> simFileManager);
37     virtual ~MultiSimController();
38 
39     void AddExtraManagers(std::shared_ptr<Telephony::SimStateManager> simStateManager,
40         std::shared_ptr<Telephony::SimFileManager> simFileManager);
41 
42     void Init();
43     bool InitData(int32_t slotId);
44     int32_t GetDefaultVoiceSlotId();
45     int32_t SetDefaultVoiceSlotId(int32_t slotId);
46     int32_t GetDefaultSmsSlotId();
47     int32_t SetDefaultSmsSlotId(int32_t slotId);
48     int32_t GetSimAccountInfo(int32_t slotId, bool denied, IccAccountInfo &info);
49     int32_t GetDefaultCellularDataSlotId();
50     int32_t SetDefaultCellularDataSlotId(int32_t slotId);
51     int32_t GetPrimarySlotId();
52     int32_t SetPrimarySlotId(int32_t slotId);
53     int32_t GetShowNumber(int32_t slotId, std::u16string &showNumber);
54     int32_t SetShowNumber(int32_t slotId, std::u16string Number, bool force = false);
55     int32_t SetShowNumberToDB(int32_t slotId, std::u16string Number);
56     int32_t GetShowName(int32_t slotId, std::u16string &showName);
57     int32_t SetShowName(int32_t slotId, std::u16string name, bool force = false);
58     int32_t GetSimTelephoneNumber(int32_t slotId, std::u16string &telephoneNumber);
59     bool IsSimActive(int32_t slotId);
60     int32_t SetActiveSim(int32_t slotId, int32_t enable, bool force = false);
61     bool SetActiveSimToRil(int32_t slotId, int32_t type, int32_t enable);
62     bool ForgetAllData();
63     bool ForgetAllData(int32_t slotId);
64     int32_t GetSlotId(int32_t simId);
65     int32_t GetSimId(int32_t slotId);
66     int32_t SaveImsSwitch(int32_t slotId, int32_t imsSwitchValue);
67     int32_t QueryImsSwitch(int32_t slotId, int32_t &imsSwitchValue);
68     bool GetListFromDataBase();
69     int32_t GetActiveSimAccountInfoList(bool denied, std::vector<IccAccountInfo> &iccAccountInfoList);
70     int32_t GetRadioProtocolTech(int32_t slotId);
71     void GetRadioProtocol(int32_t slotId);
72     bool InitShowName(int slotId);
73     void ReCheckPrimary();
74     bool IsDataShareError();
75     void ResetDataShareError();
76     int32_t UpdateOpKeyInfo();
77     bool IsSetActiveSimInProgress(int32_t slotId);
78     bool IsSetPrimarySlotIdInProgress();
79 
80 public:
81     int32_t unInitModemSlotId_ = INVALID_VALUE;
82     static constexpr const char *PHONE_NUMBER_PREF = "sim_number_";
83 
84 private:
85     bool IsValidData(int32_t slotId);
86     int32_t GetFirstActivedSlotId();
87     bool InitShowNumber(int slotId);
88     bool InitActive(int slotId);
89     bool InitIccId(int slotId);
90     int32_t UpdateDataByIccId(int slotId, const std::string &newIccId);
91     int32_t InsertData(int slotId, const std::string &newIccId);
92     void SortCache();
93     void SavePrimarySlotIdInfo(int32_t slotId);
94     void SaveDefaultCellularDataSlotIdInfo(int32_t slotId);
95     bool AnnouncePrimarySimIdChanged(int32_t simId);
96     bool AnnounceDefaultVoiceSimIdChanged(int32_t simId);
97     bool AnnounceDefaultSmsSimIdChanged(int32_t simId);
98     bool AnnounceDefaultCellularDataSimIdChanged(int32_t simId);
99     bool PublishSimFileEvent(const AAFwk::Want &want, int eventCode, const std::string &eventData);
100     bool RefreshActiveIccAccountInfoList();
101     std::string EncryptIccId(const std::string iccid);
102     void CheckIfNeedSwitchMainSlotId();
103     int32_t getDefaultMainSlotByIccId();
104     bool IsValidSlotId(int32_t slotId);
105     bool InitPrimary();
106     bool IsAllCardsReady();
107     bool IsAllCardsLoaded();
108     void SendMainCardBroadCast(int32_t slotId);
109     void SendDefaultCellularDataBroadCast(int32_t slotId);
110     void InitMainCardSlotId();
111     void PublishSetPrimaryEvent(bool setDone);
112     int32_t GetTargetDefaultSimId(int32_t slotId, int &simId);
113     size_t GetLocalCacheSize();
114     int32_t GetTargetSimId(int32_t slotId, int &simId);
115     int32_t GetTargetIccId(int32_t slotId, std::string &iccId);
116     bool IsAllModemInitDone();
117     int32_t IsSatelliteSupported();
118 
119 private:
120     const int32_t IMS_SWITCH_STATUS_UNKNOWN = -1;
121     int32_t maxCount_ = 0;
122     int32_t primarySimId_ = 0;
123     int32_t defaultSmsSimId_ = 0;
124     int32_t defaultCellularSimId_ = 0;
125     int32_t defaultVoiceSimId_ = 0;
126     int32_t lastPrimarySlotId_ = 0;
127     int32_t lastCellularDataSlotId_ = 0;
128     std::vector<std::shared_ptr<Telephony::SimStateManager>> simStateManager_;
129     std::vector<std::shared_ptr<Telephony::SimFileManager>> simFileManager_;
130     std::unique_ptr<SimRdbHelper> simDbHelper_ = nullptr;
131     IccAccountInfo iccAccountInfo_;
132     std::vector<IccAccountInfo> iccAccountInfoList_;
133     std::vector<SimRdbInfo> localCacheInfo_;
134     std::mutex mutex_;
135     std::shared_ptr<RadioProtocolController> radioProtocolController_ = nullptr;
136     std::vector<int> isSetActiveSimInProgress_;
137     bool isSetPrimarySlotIdInProgress_ = false;
138 };
139 } // namespace Telephony
140 } // namespace OHOS
141 #endif // OHOS_MULTI_SIM_CONTROLLER_H
142