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