• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 LOCATOR_BACKGROUND_PROXY_H
17 #define LOCATOR_BACKGROUND_PROXY_H
18 
19 #include <map>
20 #include <singleton.h>
21 #include <string>
22 
23 #include "app_mgr_interface.h"
24 #include "app_state_data.h"
25 #include "application_state_observer_stub.h"
26 #include "common_event_subscriber.h"
27 #include "system_ability_status_change_stub.h"
28 
29 #include "i_locator_callback.h"
30 #include "request.h"
31 
32 namespace OHOS {
33 namespace Location {
34 class AppStateChangeCallback : public AppExecFwk::ApplicationStateObserverStub {
35 public:
36     AppStateChangeCallback();
37     ~AppStateChangeCallback() override;
38 
39     void OnForegroundApplicationChanged(const AppExecFwk::AppStateData& appStateData) override;
40 };
41 
42 class LocatorBackgroundProxy {
43 public:
44     static LocatorBackgroundProxy* GetInstance();
45     LocatorBackgroundProxy();
46     ~LocatorBackgroundProxy();
47     void UpdateListOnRequestChange(const std::shared_ptr<Request>& request);
48     void OnSuspend(const std::shared_ptr<Request>& request, bool active);
49     void OnSaStateChange(bool enable);
50     void OnDeleteRequestRecord(const std::shared_ptr<Request>& request);
51     bool IsCallbackInProxy(const sptr<ILocatorCallback>& callback) const;
52     bool IsAppBackground(std::string bundleName);
53     bool RegisterAppStateObserver();
54     bool UnregisterAppStateObserver();
55     bool IsAppInLocationContinuousTasks(pid_t uid, pid_t pid);
56     bool IsAppHasFormVisible(uint32_t tokenId, uint64_t tokenIdEx);
57     int32_t getCurrentUserId();
58     bool IsAppBackground(int uid, std::string bundleName);
59     void UpdateBackgroundAppStatues(int32_t uid, int32_t status);
60 private:
61     void StartLocator();
62     void StopLocator();
63     void StartLocatorThread();
64     void StopLocatorThread();
65     void OnUserSwitch(int32_t userId);
66     void OnUserRemove(int32_t userId);
67     void UpdateListOnSuspend(const std::shared_ptr<Request>& request, bool active);
68     void UpdateListOnUserSwitch(int32_t userId);
69     void InitArgsFromProp();
70     void SubscribeSaStatusChangeListerner();
71 
72     bool CheckPermission(const std::shared_ptr<Request>& request) const;
73     bool CheckMaxRequestNum(int32_t uid, const std::string& packageName) const;
74     int32_t GetUserId(int32_t uid) const;
75     const std::list<std::shared_ptr<Request>>& GetRequestsInProxy() const;
76 
77     class mLocatorCallback : public IRemoteStub<ILocatorCallback> {
78     public:
79         void OnLocationReport(const std::unique_ptr<Location>& location);
80         void OnLocatingStatusChange(const int status);
81         void OnErrorReport(const int errorCode);
82     };
83 
84     class UserSwitchSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
85     public:
86         explicit UserSwitchSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &info);
87         ~UserSwitchSubscriber() override = default;
88         static bool Subscribe();
89     private:
90         void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &event) override;
91     };
92 
93     class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub {
94     public:
95         explicit SystemAbilityStatusChangeListener(std::shared_ptr<UserSwitchSubscriber> &subscriber);
96         ~SystemAbilityStatusChangeListener() = default;
97         void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
98         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
99 
100     private:
101         std::shared_ptr<UserSwitchSubscriber> subscriber_ = nullptr;
102     };
103 
104     bool isLocating_ = false;
105     bool proxySwtich_ = false;
106     bool featureSwitch_ = true;
107     bool isWating_ = false;
108     bool isUserSwitchSubscribed_ = false;
109     int timeInterval_;
110     int32_t curUserId_ = 0;
111 
112     sptr<ILocatorCallback> callback_;
113     std::shared_ptr<Request> request_;
114     std::shared_ptr<UserSwitchSubscriber> subscriber_ = nullptr;
115     sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr;
116     std::shared_ptr<std::map<int32_t, std::shared_ptr<std::list<std::shared_ptr<Request>>>>> requestsMap_;
117     std::shared_ptr<std::list<std::shared_ptr<Request>>> requestsList_;
118     static std::mutex requestListMutex_;
119     static std::mutex locatorMutex_;
120     static std::mutex foregroundAppMutex_;
121     std::map<int32_t, int32_t> foregroundAppMap_;
122     sptr<AppExecFwk::IAppMgr> iAppMgr_ = nullptr;
123     sptr<AppStateChangeCallback> appStateObserver_ = nullptr;
124 };
125 } // namespace Location
126 } // namespace OHOS
127 #endif // LOCATOR_BACKGROUND_PROXY_H
128