1 /*
2  * Copyright (C) 2024 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 TELEPHONY_SPAM_CALL_ADAPTER_MANAGER_H
17 #define TELEPHONY_SPAM_CALL_ADAPTER_MANAGER_H
18 
19 #include <string>
20 
21 #include "call_manager_info.h"
22 #include "singleton.h"
23 #include "cJSON.h"
24 #include "time_wait_helper.h"
25 #include "ability_connect_callback_stub.h"
26 #include <memory>
27 #include "ffrt.h"
28 
29 namespace OHOS {
30 namespace Telephony {
31 class SpamCallAdapter : public std::enable_shared_from_this<SpamCallAdapter> {
32 public:
33     SpamCallAdapter();
34     ~SpamCallAdapter();
35     bool DetectSpamCall(const std::string &phoneNumber, const int32_t &slotId);
36     void GetDetectResult(int32_t &errCode, std::string &result);
37     void SetDetectResult(int32_t &errCode, std::string &result);
38     void GetParseResult(bool &isBlock, NumberMarkInfo &info, int32_t &blockReason);
39     void SetParseResult(bool &isBlock, NumberMarkInfo &info, int32_t &blockReason);
40     std::string GetDetectPhoneNum();
41     void NotifyAll();
42     bool WaitForDetectResult();
43     void ParseDetectResult(const std::string &jsonData, bool &isBlock, NumberMarkInfo &info, int32_t &blockReason);
44 
45 private:
46     bool ConnectSpamCallAbility(const AAFwk::Want &want, const std::string &phoneNumber, const int32_t &slotId);
47     void DisconnectSpamCallAbility();
48     bool JsonGetNumberValue(cJSON *json, const std::string key, int32_t &out);
49     bool JsonGetStringValue(cJSON *json, const std::string key, std::string &out);
50     bool JsonGetBoolValue(cJSON *json, const std::string key);
51     int32_t errCode_ = -1;
52     std::string result_ = "";
53     std::string phoneNumber_ = "";
54     NumberMarkInfo info_ = {
55         .markType = MarkType::MARK_TYPE_NONE,
56         .markContent = "",
57         .markCount = -1,
58         .markSource = "",
59         .isCloud = false,
60     };
61     bool isBlock_ = false;
62     int32_t blockReason_ = 0;
63     std::unique_ptr<TimeWaitHelper> timeWaitHelper_ {nullptr};
64     ffrt::mutex mutex_;
65 };
66 } // namespace Telephony
67 } // namespace OHOS
68 #endif // TELEPHONY_SPAM_CALL_ADAPTER_MANAGER_H