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 AGNSS_NI_MANAGER_H
17 #define AGNSS_NI_MANAGER_H
18 #ifdef FEATURE_GNSS_SUPPORT
19 
20 #include <mutex>
21 #include <singleton.h>
22 #include <v2_0/ignss_interface.h>
23 
24 #include "common_utils.h"
25 #include "constant_definition.h"
26 #include "event_handler.h"
27 #include "gnss_common_event_subscriber.h"
28 #include "subability_common.h"
29 #include "system_ability_status_change_stub.h"
30 
31 namespace OHOS {
32 namespace Location {
33 using HDI::Location::Gnss::V2_0::IGnssInterface;
34 using HDI::Location::Gnss::V2_0::GnssNiNotificationRequest;
35 using HDI::Location::Gnss::V2_0::GnssNiRequestCategory;
36 using HDI::Location::Gnss::V2_0::GnssNiResponseCmd;
37 using HDI::Location::Gnss::V2_0::GNSS_NI_NOTIFICATION_REQUIRE_NOTIFY;
38 using HDI::Location::Gnss::V2_0::GNSS_NI_NOTIFICATION_REQUIRE_VERIFY;
39 using HDI::Location::Gnss::V2_0::GNSS_NI_NOTIFICATION_REQUIRE_PRIVACY_OVERRIDE;
40 using HDI::Location::Gnss::V2_0::GNSS_NI_RESPONSE_CMD_ACCEPT;
41 using HDI::Location::Gnss::V2_0::GNSS_NI_RESPONSE_CMD_REJECT;
42 using HDI::Location::Gnss::V2_0::GNSS_NI_RESPONSE_CMD_NO_RESPONSE;
43 using HDI::Location::Gnss::V2_0::GNSS_NI_ENCODING_FORMAT_NULL;
44 using HDI::Location::Gnss::V2_0::GNSS_NI_ENCODING_FORMAT_SUPL_GSM_DEFAULT;
45 using HDI::Location::Gnss::V2_0::GNSS_NI_ENCODING_FORMAT_SUPL_UCS2;
46 using HDI::Location::Gnss::V2_0::GNSS_NI_ENCODING_FORMAT_SUPL_UTF8;
47 
48 class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub {
49 public:
50     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
51     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
52 };
53 
54 class AGnssNiManager {
55 public:
56     AGnssNiManager();
57     ~AGnssNiManager();
58     void Run();
59     void SubscribeSaStatusChangeListerner();
60     void RegisterAgnssNiEvent();
61     void UnRegisterAgnssNiEvent();
62     void RegisterNiResponseEvent();
63     void UnRegisterNiResponseEvent();
64     void CheckWapSuplInit(const EventFwk::Want &want);
65     void CheckSmsSuplInit(const EventFwk::Want &want);
66     void OnCallStateChanged(const EventFwk::Want &want);
67     void HandleNiNotification(const GnssNiNotificationRequest &notif);
68     void SendUserResponse(GnssNiResponseCmd responseCmd);
69     static AGnssNiManager* GetInstance();
70 
71 private:
72     void AgnssNiSuplInit();
73     bool IsInEmergency();
74     std::string BuildStartCommand(const GnssNiNotificationRequest &notif);
75     void OpenNiDialog(const GnssNiNotificationRequest &notif);
76     void SendNiNotification(const GnssNiNotificationRequest &notif);
77     std::string DecodeNiString(std::string original, int coding);
78 
79     bool isInEmergencyCall_ = false;
80     short niNotificationId_ = 0;
81     int64_t emergencyCallEndTime_ = 0;
82     sptr<IGnssInterface> gnssInterface_ = nullptr;
83     std::shared_ptr<GnssCommonEventSubscriber> subscriber_ = nullptr;
84     std::shared_ptr<GnssCommonEventSubscriber> niResponseSubscriber_ = nullptr;
85     sptr<SystemAbilityStatusChangeListener> statusChangeListener_ = nullptr;
86     std::mutex mutex_;
87     std::mutex callStateMutex_;
88 };
89 } // namespace Location
90 } // namespace OHOS
91 
92 #endif // FEATURE_GNSS_SUPPORT
93 #endif // AGNSS_NI_MANAGER_H
94