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_SIM_DIALLING_NUMBERS_CACHE_H
17 #define OHOS_SIM_DIALLING_NUMBERS_CACHE_H
18 
19 #include <algorithm>
20 #include <iostream>
21 #include <map>
22 #include <vector>
23 
24 #include "icc_dialling_numbers_handler.h"
25 #include "icc_file_controller.h"
26 #include "sim_file_manager.h"
27 #include "tel_event_handler.h"
28 #include "usim_dialling_numbers_service.h"
29 
30 namespace OHOS {
31 namespace Telephony {
32 enum {
33     MSG_SIM_OBTAIN_ADN_DETAILS_DONE,
34     MSG_SIM_CHANGE_DIALLING_NUMBERS_DONE,
35     MSG_SIM_OBTAIN_PBR_DETAILS_DONE,
36 };
37 
38 const int ADD_FLAG = -1;
39 class IccDiallingNumbersCache : public TelEventHandler {
40 public:
41     explicit IccDiallingNumbersCache(std::shared_ptr<SimFileManager> simFileManager);
42     ~IccDiallingNumbersCache();
43     void Init();
44     std::shared_ptr<std::vector<std::shared_ptr<DiallingNumbersInfo>>> LoadReadyDiallingNumbers(int fileId);
45     int ExtendedElementFile(int fileId);
46     void UpdateDiallingNumberToIcc(int fileId, std::shared_ptr<DiallingNumbersInfo> diallingNumberInfor,
47         int recordIndex, bool isDel, const AppExecFwk::InnerEvent::Pointer &caller);
48     void ObtainAllDiallingNumberFiles(int fileId, int extensionEf, const AppExecFwk::InnerEvent::Pointer &caller);
49     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event);
50     void ClearDiallingNumberCache();
51 
52 protected:
53     std::shared_ptr<SimFileManager> simFileManager_ = nullptr;
54     std::shared_ptr<IccDiallingNumbersHandler> diallingNumbersHandler_ = nullptr;
55     std::shared_ptr<UsimDiallingNumbersService> usimDiallingNumberSrv_ = nullptr;
56     std::map<int, std::shared_ptr<std::vector<std::shared_ptr<DiallingNumbersInfo>>>> diallingNumberFileList_;
57     std::map<int, int> extTypeMap_;
58     void SendUpdateResult(const AppExecFwk::InnerEvent::Pointer &caller, const std::shared_ptr<void> &object);
59     void SendBackResult(const AppExecFwk::InnerEvent::Pointer &caller,
60         const std::shared_ptr<std::vector<std::shared_ptr<DiallingNumbersInfo>>> &ar,
61         const std::shared_ptr<void> &object);
62 
63 private:
64     void SendExceptionResult(const AppExecFwk::InnerEvent::Pointer &caller, int errCode);
65     AppExecFwk::InnerEvent::Pointer BuildCallerInfo(
66         int eventId, int fileId, int index, std::shared_ptr<void> pobj, const AppExecFwk::InnerEvent::Pointer &caller);
67     AppExecFwk::InnerEvent::Pointer CreateUsimPointer(
68         int eventId, int fileId, const AppExecFwk::InnerEvent::Pointer &caller);
69     void ProcessObtainAdnDetailsDone(const AppExecFwk::InnerEvent::Pointer &event);
70     void ProcessChangeDiallingNumbersDone(const AppExecFwk::InnerEvent::Pointer &event);
71     void ProcessObtainPbrDetailsDone(const AppExecFwk::InnerEvent::Pointer &event);
72     bool IsDiallingNumberEqual(
73         const std::shared_ptr<DiallingNumbersInfo> &src, const std::shared_ptr<DiallingNumbersInfo> &dest);
74     bool StringEqual(const std::u16string &s1, const std::u16string &s2);
75     bool ArrayEqual(const std::vector<std::u16string> &mailsSrc, const std::vector<std::u16string> &mailsDest);
76     bool CheckValueAndOperation(const std::shared_ptr<std::vector<std::shared_ptr<DiallingNumbersInfo>>> &list,
77         const std::shared_ptr<DiallingNumbersInfo> &info, int &index, int fileId);
78     void SearchIndexByNameAndNumber(const std::shared_ptr<std::vector<std::shared_ptr<DiallingNumbersInfo>>> &list,
79         const std::shared_ptr<DiallingNumbersInfo> &info, int &index);
80     void InitFileTypeMap();
81 };
82 } // namespace Telephony
83 } // namespace OHOS
84 #endif // OHOS_SIM_DIALLING_NUMBERS_CACHE_H