• 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 #include "location_data_manager.h"
17 
18 #include "uri.h"
19 
20 #include "switch_callback_proxy.h"
21 #include "constant_definition.h"
22 #include "location_data_rdb_helper.h"
23 #include "location_log.h"
24 #include "common_hisysevent.h"
25 
26 namespace OHOS {
27 namespace Location {
LocationDataManager()28 LocationDataManager::LocationDataManager()
29 {
30     switchCallbacks_ = std::make_unique<std::map<pid_t, sptr<ISwitchCallback>>>();
31 }
32 
~LocationDataManager()33 LocationDataManager::~LocationDataManager()
34 {
35 }
36 
ReportSwitchState(bool isEnabled)37 LocationErrCode LocationDataManager::ReportSwitchState(bool isEnabled)
38 {
39     if (switchCallbacks_ == nullptr) {
40         LBSLOGE(LOCATOR, "switchCallbacks_ is nullptr");
41         return ERRCODE_INVALID_PARAM;
42     }
43     int state = isEnabled ? ENABLED : DISABLED;
44 
45     std::unique_lock<std::mutex> lock(mutex_);
46     for (auto iter = switchCallbacks_->begin(); iter != switchCallbacks_->end(); iter++) {
47         sptr<IRemoteObject> remoteObject = (iter->second)->AsObject();
48         auto callback = std::make_unique<SwitchCallbackProxy>(remoteObject);
49         callback->OnSwitchChange(state);
50     }
51     return ERRCODE_SUCCESS;
52 }
53 
RegisterSwitchCallback(const sptr<IRemoteObject> & callback,pid_t uid)54 LocationErrCode LocationDataManager::RegisterSwitchCallback(const sptr<IRemoteObject>& callback, pid_t uid)
55 {
56     if (callback == nullptr || switchCallbacks_ == nullptr) {
57         LBSLOGE(LOCATOR, "register an invalid switch callback");
58         return ERRCODE_INVALID_PARAM;
59     }
60     sptr<ISwitchCallback> switchCallback = iface_cast<ISwitchCallback>(callback);
61     if (switchCallback == nullptr) {
62         LBSLOGE(LOCATOR, "cast switch callback fail!");
63         return ERRCODE_INVALID_PARAM;
64     }
65 
66     std::unique_lock<std::mutex> lock(mutex_);
67     switchCallbacks_->erase(uid);
68     switchCallbacks_->insert(std::make_pair(uid, switchCallback));
69     LBSLOGD(LOCATOR, "after uid:%{public}d register, switch callback size:%{public}s",
70         uid, std::to_string(switchCallbacks_->size()).c_str());
71     return ERRCODE_SUCCESS;
72 }
73 
UnregisterSwitchCallback(const sptr<IRemoteObject> & callback)74 LocationErrCode LocationDataManager::UnregisterSwitchCallback(const sptr<IRemoteObject>& callback)
75 {
76     if (callback == nullptr || switchCallbacks_ == nullptr) {
77         LBSLOGE(LOCATOR, "unregister an invalid switch callback");
78         return ERRCODE_INVALID_PARAM;
79     }
80     sptr<ISwitchCallback> switchCallback = iface_cast<ISwitchCallback>(callback);
81     if (switchCallback == nullptr) {
82         LBSLOGE(LOCATOR, "cast switch callback fail!");
83         return ERRCODE_INVALID_PARAM;
84     }
85 
86     pid_t uid = -1;
87     std::unique_lock<std::mutex> lock(mutex_);
88     for (auto iter = switchCallbacks_->begin(); iter != switchCallbacks_->end(); iter++) {
89         sptr<IRemoteObject> remoteObject = (iter->second)->AsObject();
90         if (remoteObject == callback) {
91             uid = iter->first;
92             break;
93         }
94     }
95     switchCallbacks_->erase(uid);
96     LBSLOGD(LOCATOR, "after uid:%{public}d unregister, switch callback size:%{public}s",
97         uid, std::to_string(switchCallbacks_->size()).c_str());
98     return ERRCODE_SUCCESS;
99 }
100 
QuerySwitchState(bool & isEnabled)101 LocationErrCode LocationDataManager::QuerySwitchState(bool &isEnabled)
102 {
103     std::unique_lock<std::mutex> lock(switchStateMutex_);
104     // Default value is DISABLED
105     int32_t state = isStateCached_ ? cachedSwitchState_ : DISABLED;
106     LocationErrCode errCode = ERRCODE_SUCCESS;
107     if (!isStateCached_) {
108         Uri locationDataEnableUri(LOCATION_DATA_URI);
109         errCode = DelayedSingleton<LocationDataRdbHelper>::GetInstance()->
110             GetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, state);
111         // At the first time, the key "location_switch_enable" is not in the database
112         // DISABLED will be set in the database
113         if (errCode != ERRCODE_SUCCESS) {
114             LBSLOGE(LOCATOR, "%{public}s: can not query state, reset state.", __func__);
115             errCode = DelayedSingleton<LocationDataRdbHelper>::GetInstance()->
116                 SetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, state);
117         }
118         // cache the value
119         isStateCached_ = true;
120         cachedSwitchState_ = state;
121     }
122     isEnabled = (state == ENABLED);
123     return errCode;
124 }
125 
SetCachedSwitchState(int state)126 void LocationDataManager::SetCachedSwitchState(int state)
127 {
128     std::unique_lock<std::mutex> lock(switchStateMutex_);
129     isStateCached_ = true;
130     cachedSwitchState_ = state;
131 }
132 }  // namespace Location
133 }  // namespace OHOS
134