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