1/*
2 * Copyright (C) 2022 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 GEO_CONVERT_SERVICE_H
17#define GEO_CONVERT_SERVICE_H
18#ifdef FEATURE_GEOCODE_SUPPORT
19
20#include <mutex>
21#include <singleton.h>
22#include <string>
23#include <vector>
24
25#include "event_runner.h"
26#include "event_handler.h"
27#include "ffrt.h"
28#include "if_system_ability_manager.h"
29#include "iremote_object.h"
30#include "message_parcel.h"
31#include "message_option.h"
32#include "system_ability.h"
33
34#include "common_utils.h"
35#include "constant_definition.h"
36#include "geo_coding_mock_info.h"
37#include "geo_convert_skeleton.h"
38#include "ability_connect_callback_interface.h"
39
40namespace OHOS {
41namespace Location {
42enum class ServiceConnectState {
43    STATE_DISCONNECT,
44    STATE_CONNECTTING,
45    STATE_CONNECTTED,
46};
47
48class GeoConvertHandler : public AppExecFwk::EventHandler {
49public:
50    explicit GeoConvertHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner);
51    ~GeoConvertHandler() override;
52    void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override;
53};
54
55class GeoServiceDeathRecipient : public IRemoteObject::DeathRecipient {
56public:
57    void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
58    GeoServiceDeathRecipient();
59    ~GeoServiceDeathRecipient() override;
60};
61
62class GeoConvertService : public SystemAbility, public GeoConvertServiceStub {
63DECLEAR_SYSTEM_ABILITY(GeoConvertService);
64
65public:
66    DISALLOW_COPY_AND_MOVE(GeoConvertService);
67    static GeoConvertService* GetInstance();
68
69    GeoConvertService();
70    ~GeoConvertService() override;
71    void OnStart() override;
72    void OnStop() override;
73    ServiceRunningState QueryServiceState() const
74    {
75        return state_;
76    }
77    int IsGeoConvertAvailable(MessageParcel &reply) override;
78    int GetAddressByCoordinate(MessageParcel &data, MessageParcel &reply) override;
79    int GetAddressByLocationName(MessageParcel &data, MessageParcel &reply) override;
80    bool EnableReverseGeocodingMock() override;
81    bool DisableReverseGeocodingMock() override;
82    LocationErrCode SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo) override;
83    int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
84    bool CancelIdleState() override;
85    void UnloadGeoConvertSystemAbility() override;
86
87    bool ConnectService();
88    void NotifyConnected(const sptr<IRemoteObject>& remoteObject);
89    void DisconnectAbilityConnect();
90    void NotifyDisConnected();
91    bool ResetServiceProxy();
92    bool SendGeocodeRequest(int code, MessageParcel& dataParcel, MessageParcel& replyParcel, MessageOption& option);
93    ServiceConnectState GetServiceConnectState();
94    void SetServiceConnectState(ServiceConnectState connectState);
95private:
96    bool Init();
97    static void SaDumpInfo(std::string& result);
98    int RemoteToService(uint32_t code, MessageParcel &data, MessageParcel &rep);
99    void ReportAddressMock(MessageParcel &data, MessageParcel &reply);
100    bool CheckIfGeoConvertConnecting();
101    bool GetService();
102    bool IsConnect();
103    bool IsConnecting();
104    void RegisterGeoServiceDeathRecipient();
105    void UnRegisterGeoServiceDeathRecipient();
106
107    bool mockEnabled_ = false;
108    bool registerToService_ = false;
109    ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START;
110    std::vector<std::shared_ptr<GeocodingMockInfo>> mockInfo_;
111    std::mutex mockInfoMutex_;
112    std::shared_ptr<GeoConvertHandler> geoConvertHandler_;
113
114    std::mutex mutex_;
115    sptr<IRemoteObject> serviceProxy_ = nullptr;
116    std::condition_variable connectCondition_;
117    sptr<AAFwk::IAbilityConnection> conn_;
118    sptr<IRemoteObject::DeathRecipient> geoServiceRecipient_ =
119        sptr<GeoServiceDeathRecipient>(new (std::nothrow) GeoServiceDeathRecipient());
120    std::mutex connectStateMutex_;
121    ServiceConnectState connectState_ = ServiceConnectState::STATE_DISCONNECT;
122};
123} // namespace OHOS
124} // namespace Location
125#endif // FEATURE_GEOCODE_SUPPORT
126#endif // GEO_CONVERT_SERVICE_H
127