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_ICC_DIALLING_NUMBERS_MANAGER_H
17 #define OHOS_ICC_DIALLING_NUMBERS_MANAGER_H
18 
19 #include <cstring>
20 #include <iostream>
21 #include <string>
22 
23 #include "dialling_numbers_info.h"
24 #include "i_tel_ril_manager.h"
25 #include "icc_dialling_numbers_cache.h"
26 #include "sim_file_manager.h"
27 #include "tel_event_handler.h"
28 
29 namespace OHOS {
30 namespace Telephony {
31 enum DiallingNumbersMessageType {
32     MSG_SIM_DIALLING_NUMBERS_GET_DONE  = 1,
33     MSG_SIM_DIALLING_NUMBERS_UPDATE_DONE,
34     MSG_SIM_DIALLING_NUMBERS_WRITE_DONE,
35     MSG_SIM_DIALLING_NUMBERS_DELETE_DONE,
36 };
37 class IccDiallingNumbersManager : public TelEventHandler {
38 public:
39     explicit IccDiallingNumbersManager(
40         std::weak_ptr<SimFileManager> simFileManager, std::shared_ptr<SimStateManager> simState);
41     ~IccDiallingNumbersManager();
42     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event);
43     int32_t QueryIccDiallingNumbers(int type, std::vector<std::shared_ptr<DiallingNumbersInfo>> &result);
44     int32_t AddIccDiallingNumbers(int type, const std::shared_ptr<DiallingNumbersInfo> &diallingNumber);
45     int32_t DelIccDiallingNumbers(int type, const std::shared_ptr<DiallingNumbersInfo> &diallingNumber);
46     int32_t UpdateIccDiallingNumbers(int type, const std::shared_ptr<DiallingNumbersInfo> &diallingNumber);
47     void Init();
48     static std::shared_ptr<IccDiallingNumbersManager> CreateInstance(
49         std::weak_ptr<SimFileManager> simFile, std::shared_ptr<SimStateManager> simState);
50     enum class HandleRunningState {
51         STATE_NOT_START,
52         STATE_RUNNING
53     };
54 
55 protected:
56     std::shared_ptr<IccDiallingNumbersCache> diallingNumbersCache_ = nullptr;
57     std::shared_ptr<AppExecFwk::EventRunner> eventLoopDiallingNumbers_ = nullptr;
58     HandleRunningState stateDiallingNumbers_ = HandleRunningState::STATE_NOT_START;
59 
60 private:
61     std::weak_ptr<SimFileManager> simFileManager_;
62     std::shared_ptr<Telephony::SimStateManager> simStateManager_ = nullptr;
63     std::vector<std::shared_ptr<DiallingNumbersInfo>> diallingNumbersList_;
64     std::mutex mtx_;
65     bool hasEventDone_ = false;
66     bool hasQueryEventDone_ = true;
67     std::condition_variable processWait_;
68     void ProcessLoadDone(const AppExecFwk::InnerEvent::Pointer &event);
69     void ProcessUpdateDone(const AppExecFwk::InnerEvent::Pointer &event);
70     void ProcessWriteDone(const AppExecFwk::InnerEvent::Pointer &event);
71     void ProcessDeleteDone(const AppExecFwk::InnerEvent::Pointer &event);
72     AppExecFwk::InnerEvent::Pointer BuildCallerInfo(int eventId);
73 
74     void ClearRecords();
75     int GetFileIdForType(int fileType);
76     void FillResults(const std::shared_ptr<std::vector<std::shared_ptr<DiallingNumbersInfo>>> &listInfo);
77     bool IsValidType(int type);
78     bool HasSimCard();
79     bool IsValidParam(int type, const std::shared_ptr<DiallingNumbersInfo> &info);
80 };
81 } // namespace Telephony
82 } // namespace OHOS
83 #endif // OHOS_ICC_DIALLING_NUMBERS_MANAGER_H
84