Files
base_location/frameworks/native/source/location_data_manager.cpp
z30025928 6ec296507b fix query switch bug
Signed-off-by: z30025928 <734222381@qq.com>
2023-03-06 14:23:52 +08:00

102 lines
3.8 KiB
C++

/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "location_data_manager.h"
#include "uri.h"
#include "switch_callback_proxy.h"
#include "constant_definition.h"
#include "location_data_rdb_helper.h"
#include "location_log.h"
#include "common_hisysevent.h"
#include "common_utils.h"
namespace OHOS {
namespace Location {
DECLARE_SINGLE_INSTANCE_IMPLEMENT(LocationDataManager);
auto g_switchCallbacks = std::make_unique<std::map<pid_t, sptr<ISwitchCallback>>>();
LocationErrCode LocationDataManager::ReportSwitchState(bool isEnabled)
{
int state = isEnabled ? ENABLED : DISABLED;
for (auto iter = g_switchCallbacks->begin(); iter != g_switchCallbacks->end(); iter++) {
sptr<IRemoteObject> remoteObject = (iter->second)->AsObject();
auto callback = std::make_unique<SwitchCallbackProxy>(remoteObject);
callback->OnSwitchChange(state);
}
return ERRCODE_SUCCESS;
}
LocationErrCode LocationDataManager::RegisterSwitchCallback(const sptr<IRemoteObject>& callback, pid_t uid)
{
if (callback == nullptr) {
LBSLOGE(LOCATOR, "register an invalid switch callback");
return ERRCODE_INVALID_PARAM;
}
sptr<ISwitchCallback> switchCallback = iface_cast<ISwitchCallback>(callback);
if (switchCallback == nullptr) {
LBSLOGE(LOCATOR, "cast switch callback fail!");
return ERRCODE_INVALID_PARAM;
}
g_switchCallbacks->erase(uid);
g_switchCallbacks->insert(std::make_pair(uid, switchCallback));
LBSLOGD(LOCATOR, "after uid:%{public}d register, switch callback size:%{public}s",
uid, std::to_string(g_switchCallbacks->size()).c_str());
return ERRCODE_SUCCESS;
}
LocationErrCode LocationDataManager::UnregisterSwitchCallback(const sptr<IRemoteObject>& callback)
{
if (callback == nullptr) {
LBSLOGE(LOCATOR, "unregister an invalid switch callback");
return ERRCODE_INVALID_PARAM;
}
sptr<ISwitchCallback> switchCallback = iface_cast<ISwitchCallback>(callback);
if (switchCallback == nullptr) {
LBSLOGE(LOCATOR, "cast switch callback fail!");
return ERRCODE_INVALID_PARAM;
}
pid_t uid = -1;
for (auto iter = g_switchCallbacks->begin(); iter != g_switchCallbacks->end(); iter++) {
sptr<IRemoteObject> remoteObject = (iter->second)->AsObject();
if (remoteObject == callback) {
uid = iter->first;
break;
}
}
g_switchCallbacks->erase(uid);
LBSLOGD(LOCATOR, "after uid:%{public}d unregister, switch callback size:%{public}s",
uid, std::to_string(g_switchCallbacks->size()).c_str());
return ERRCODE_SUCCESS;
}
LocationErrCode LocationDataManager::QuerySwitchState(bool &isEnabled)
{
int32_t state = DISABLED;
Uri locationDataEnableUri(LOCATION_DATA_URI);
LocationErrCode errCode =
LocationDataRdbHelper::GetInstance().GetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, state);
if (errCode != ERRCODE_SUCCESS) {
LBSLOGE(LOCATOR, "%{public}s: can not query state, reset state.", __func__);
errCode = LocationDataRdbHelper::GetInstance().SetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, state);
}
isEnabled = (state == ENABLED);
return errCode;
}
} // namespace Location
} // namespace OHOS