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 SUBABILITY_COMMON_H
17 #define SUBABILITY_COMMON_H
18 
19 #include <vector>
20 
21 #include "iremote_broker.h"
22 
23 #include "constant_definition.h"
24 #include "i_locator_callback.h"
25 #include "location.h"
26 #include "hilog/log.h"
27 #include "work_record.h"
28 
29 namespace OHOS {
30 namespace Location {
31 class ISubAbility : public IRemoteBroker {
32 public:
33     virtual LocationErrCode SendLocationRequest(WorkRecord &workrecord) = 0;
34     virtual LocationErrCode SetEnable(bool state) = 0;
35     virtual LocationErrCode EnableMock() = 0;
36     virtual LocationErrCode DisableMock() = 0;
37     virtual LocationErrCode SetMocked(const int timeInterval,
38         const std::vector<std::shared_ptr<Location>> &location) = 0;
39 };
40 
41 class SubAbility {
42 public:
43     SubAbility();
44     virtual ~SubAbility();
45     void SetAbility(std::string name);
46     void LocationRequest(WorkRecord &workrecord);
47     void Enable(bool state, const sptr<IRemoteObject> ability);
48     void HandleRefrashRequirements();
49     int GetRequestNum();
50     bool EnableLocationMock();
51     bool DisableLocationMock();
52     bool SetMockedLocations(const int timeInterval, const std::vector<std::shared_ptr<Location>> &location);
53 
54     int GetTimeIntervalMock();
55     bool IsLocationMocked();
56     std::vector<std::shared_ptr<Location>> GetLocationMock();
57     void ClearLocationMock();
58     void ReportLocationInfo(const std::string& systemAbility, const std::shared_ptr<Location> location);
59     void StopAllLocationRequests();
60     void RestartAllLocationRequests();
61     std::string GetPackageNameByUuid(std::string uuid);
62 private:
63     void HandleLocalRequest(WorkRecord &record);
64     void HandleRemoveRecord(WorkRecord &newRecord);
65     void HandleAddRecord(WorkRecord &newRecord);
66     void CacheLocationMock(const std::vector<std::shared_ptr<Location>> &location);
67     virtual void RequestRecord(WorkRecord &workRecord, bool isAdded) = 0;
68     virtual void SendReportMockLocationEvent() = 0;
69 
70     OHOS::HiviewDFX::HiLogLabel label_;
71     int interval_ = 0;
72     std::string name_ = "";
73     std::u16string capability_ = u"";
74     std::unique_ptr<WorkRecord> lastRecord_;
75     std::unique_ptr<WorkRecord> newRecord_;
76     int mockTimeInterval_ = 0;
77     bool mockEnabled_ = false;
78     std::vector<std::shared_ptr<Location>> mockLoc_;
79     std::mutex mutex_;
80 };
81 } // namespace Location
82 } // namespace OHOS
83 #endif // SUBABILITY_COMMON_H
84