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