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 private: 58 void StartLocator(); 59 void StopLocator(); 60 void StartLocatorThread(); 61 void StopLocatorThread(); 62 void OnUserSwitch(int32_t userId); 63 void OnUserRemove(int32_t userId); 64 void UpdateListOnSuspend(const std::shared_ptr<Request>& request, bool active); 65 void UpdateListOnUserSwitch(int32_t userId); 66 void InitArgsFromProp(); 67 void SubscribeSaStatusChangeListerner(); 68 69 bool CheckPermission(const std::shared_ptr<Request>& request) const; 70 bool CheckMaxRequestNum(int32_t uid, const std::string& packageName) const; 71 int32_t GetUserId(int32_t uid) const; 72 const std::list<std::shared_ptr<Request>>& GetRequestsInProxy() const; 73 74 class mLocatorCallback : public IRemoteStub<ILocatorCallback> { 75 public: 76 void OnLocationReport(const std::unique_ptr<Location>& location); 77 void OnLocatingStatusChange(const int status); 78 void OnErrorReport(const int errorCode); 79 }; 80 81 class UserSwitchSubscriber : public OHOS::EventFwk::CommonEventSubscriber { 82 public: 83 explicit UserSwitchSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &info); 84 ~UserSwitchSubscriber() override = default; 85 static bool Subscribe(); 86 private: 87 void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &event) override; 88 }; 89 90 class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { 91 public: 92 explicit SystemAbilityStatusChangeListener(std::shared_ptr<UserSwitchSubscriber> &subscriber); 93 ~SystemAbilityStatusChangeListener() = default; 94 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 95 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 96 97 private: 98 std::shared_ptr<UserSwitchSubscriber> subscriber_ = nullptr; 99 }; 100 101 bool isLocating_ = false; 102 bool proxySwtich_ = false; 103 bool featureSwitch_ = true; 104 bool isWating_ = false; 105 bool isUserSwitchSubscribed_ = false; 106 int timeInterval_; 107 int32_t curUserId_ = 0; 108 109 sptr<ILocatorCallback> callback_; 110 std::shared_ptr<Request> request_; 111 std::shared_ptr<UserSwitchSubscriber> subscriber_ = nullptr; 112 sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr; 113 std::shared_ptr<std::map<int32_t, std::shared_ptr<std::list<std::shared_ptr<Request>>>>> requestsMap_; 114 std::shared_ptr<std::list<std::shared_ptr<Request>>> requestsList_; 115 static std::mutex requestListMutex_; 116 static std::mutex locatorMutex_; 117 sptr<AppExecFwk::IAppMgr> iAppMgr_ = nullptr; 118 sptr<AppStateChangeCallback> appStateObserver_ = nullptr; 119 }; 120 } // namespace Location 121 } // namespace OHOS 122 #endif // LOCATOR_BACKGROUND_PROXY_H 123