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 NETWORK_ABILITY_H 17 #define NETWORK_ABILITY_H 18 19 #include <string> 20 #include <singleton.h> 21 22 #include "event_handler.h" 23 #include "event_runner.h" 24 #include "system_ability.h" 25 26 #include "common_utils.h" 27 #include "constant_definition.h" 28 #include "network_ability_skeleton.h" 29 #include "subability_common.h" 30 31 namespace OHOS { 32 namespace Location { 33 static constexpr int REQUEST_NETWORK_LOCATION = 1; 34 static constexpr int REMOVE_NETWORK_LOCATION = 2; 35 const std::string SERVICE_CONFIG_FILE = "/system/etc/location/location_service.conf"; 36 const std::string ABILITY_NAME = "LocationServiceAbility"; 37 class NetworkHandler : public AppExecFwk::EventHandler { 38 public: 39 explicit NetworkHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 40 ~NetworkHandler() override; 41 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; 42 }; 43 44 class NetworkAbility : public SystemAbility, public NetworkAbilityStub, public SubAbility, 45 DelayedSingleton<NetworkAbility> { 46 DECLEAR_SYSTEM_ABILITY(NetworkAbility); 47 48 public: 49 DISALLOW_COPY_AND_MOVE(NetworkAbility); 50 NetworkAbility(); 51 ~NetworkAbility() override; 52 void OnStart() override; 53 void OnStop() override; QueryServiceState()54 ServiceRunningState QueryServiceState() const 55 { 56 return state_; 57 } 58 LocationErrCode SendLocationRequest(WorkRecord &workrecord) override; 59 LocationErrCode SetEnable(bool state) override; 60 LocationErrCode SelfRequest(bool state) override; 61 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 62 void RequestRecord(WorkRecord &workRecord, bool isAdded) override; 63 LocationErrCode EnableMock() override; 64 LocationErrCode DisableMock() override; 65 LocationErrCode SetMocked(const int timeInterval, const std::vector<std::shared_ptr<Location>> &location) override; 66 void SendReportMockLocationEvent() override; 67 void ProcessReportLocationMock(); 68 bool ConnectNlpService(); 69 bool ReConnectNlpService(); 70 void NotifyConnected(const sptr<IRemoteObject>& remoteObject); 71 void NotifyDisConnected(); 72 bool IsMockEnabled(); 73 void SendMessage(uint32_t code, MessageParcel &data, MessageParcel &reply) override; 74 private: 75 bool Init(); 76 static void SaDumpInfo(std::string& result); 77 int32_t ReportMockedLocation(const std::shared_ptr<Location> location); 78 79 bool nlpServiceReady_ = false; 80 std::mutex mutex_; 81 sptr<IRemoteObject> nlpServiceProxy_; 82 std::condition_variable connectCondition_; 83 std::shared_ptr<NetworkHandler> networkHandler_; 84 size_t mockLocationIndex_ = 0; 85 bool registerToAbility_ = false; 86 ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START; 87 }; 88 } // namespace Location 89 } // namespace OHOS 90 #endif // NETWORK_ABILITY_H 91