• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025-2025 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 ANTIFRAUD_SERVICE_H
17 #define ANTIFRAUD_SERVICE_H
18 
19 #include <mutex>
20 #include <queue>
21 #include "call_status_manager.h"
22 #include "anti_fraud_service_client.h"
23 #include "anti_fraud_service_client_type.h"
24 #include "anti_fraud_detect_res_listener.h"
25 #include "singleton.h"
26 #include "datashare_helper.h"
27 
28 namespace OHOS {
29 namespace Telephony {
30 const std::string ANTIFRAUD_SWITCH = "spamshield_call_live_detection";
31 const std::string USER_IMPROPLAN_SWITCH = "spamshield_call_live_report";
32 const std::string USER_SETTINGSDATA_URI =
33     "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_100?Proxy=true";
34 constexpr int32_t SELECT_RULE_LENGTH = 11;
35 
36 enum class AntiFraudState {
37     /**
38      * Indicates the default antifraud state
39      */
40     ANTIFRAUD_STATE_DEFAULT = 0,
41     /**
42      * Indicates the antifraud is started
43      */
44     ANTIFRAUD_STATE_STARTED,
45     /**
46      * Indicates the call is fraud risk call
47      */
48     ANTIFRAUD_STATE_RISK,
49     /**
50      * Indicates the antifraud is finished and the call is not fraud call
51      */
52     ANTIFRAUD_STATE_FINISHED,
53 };
54 
55 class AntiFraudService {
56     DECLARE_DELAYED_SINGLETON(AntiFraudService)
57 
58 public:
59     void SetCallStatusManager(std::shared_ptr<CallStatusManager> callStatusManager);
60     bool IsSwitchOn(const std::string switchName);
61     bool IsAntiFraudSwitchOn();
62     bool IsUserImprovementPlanSwitchOn();
63     void InitParams();
64     void RecordDetectResult(const OHOS::AntiFraudService::AntiFraudResult &antiFraudResult,
65         std::string resultPhoneNum, int32_t slotId, int32_t index);
66     int32_t StartAntiFraudService(const std::string &phoneNum, int32_t slotId, int32_t index);
67     int32_t StopAntiFraudService(int32_t slotId, int32_t index);
68     std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(
69         int32_t systemAbilityId, const char *uri);
70     void AddRuleToConfig(const std::string rulesName, void *config);
71     int AnonymizeText();
72 
73 private:
74     int32_t GetStoppedSlotId();
75     int32_t GetStoppedIndex();
76     void SetStoppedSlotId(int32_t slotId);
77     void SetStoppedIndex(int32_t index);
78     int32_t CheckAntiFraudService(const std::string &phoneNum, int32_t slotId, int32_t index);
79 
80 private:
81     class AntiFraudDetectResListenerImpl : public OHOS::AntiFraudService::AntiFraudDetectResListener {
82     public:
AntiFraudDetectResListenerImpl(std::string phoneNum,int slotId,int index)83         AntiFraudDetectResListenerImpl(std::string phoneNum, int slotId, int index) : AntiFraudDetectResListener(),
84             phoneNum_(phoneNum), slotId_(slotId), index_(index) {}
85         void HandleAntiFraudDetectRes(const OHOS::AntiFraudService::AntiFraudResult &antiFraudResult) override;
86     private:
87         std::string phoneNum_;
88         int32_t slotId_;
89         int32_t index_;
90     };
91 
92 private:
93     bool isResultFraud_ = false;
94     int fraudDetectErr_ = 0;
95     int fraudDetectVersion_ = 0;
96     int fraudDetectType_ = 0;
97     float fraudDetectProb_ = 0.0;
98     std::string fraudDetectText_ = "";
99 
100     int32_t antiFraudState_ = 0;
101     int32_t stoppedSlotId_ = -1;
102     int32_t stoppedIndex_ = -1;
103 
104     ffrt::mutex mutex_;
105 
106 private:
107     std::shared_ptr<CallStatusManager> callStatusManagerPtr_ = nullptr;
108 };
109 }
110 }
111 #endif // ANTIFRAUD_SERVICE_H