* Copyright (c) 2025 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.
*/
#ifndef SENSOR_DATA_MANAGER_H
#define SENSOR_DATA_MANAGER_H
#include "nlohmann/json.hpp"
#include "datashare_helper.h"
#include "singleton.h"
#include "sensor_observer.h"
namespace OHOS {
namespace Sensors {
struct CompatibleAppData {
std::string name;
int32_t policy;
};
class SensorDataManager {
DECLARE_DELAYED_SINGLETON(SensorDataManager);
public:
DISALLOW_COPY_AND_MOVE(SensorDataManager);
bool Init(int32_t deviceMode);
std::vector<CompatibleAppData> GetCompatibleAppStrategyList();
template<typename T>
static bool GetJsonValue(const nlohmann::json& payload, const std::string& key, T& result)
{
if (!payload.contains(key)) {
return false;
}
if constexpr (std::is_same_v<T, std::string>) {
if (payload[key].is_string()) {
result = payload[key].get<std::string>();
return true;
}
} else if constexpr (std::is_same_v<T, bool>) {
if (payload[key].is_boolean()) {
result = payload[key].get<bool>();
return true;
}
} else if constexpr (std::is_arithmetic_v<T>) {
if (payload[key].is_number()) {
result = payload[key].get<int32_t>();
return true;
}
}
return false;
}
private:
static void ExecRegisterCb(const sptr<SensorObserver> &observer);
int32_t RegisterObserver(const sptr<SensorObserver> &observer);
int32_t UnregisterObserver(const sptr<SensorObserver> &observer);
int32_t GetStringValue(const std::string &key, std::string &value);
Uri AssembleUri(const std::string &key);
std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(const std::string &tableUrl);
bool ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> &helper);
sptr<SensorObserver> CreateObserver(const SensorObserver::UpdateFunc &func);
void ParseAppLogicalDeviceList(const std::string &compatibleAppStrategy);
int32_t ParseJsonValue(const nlohmann::json &value, const std::string &strKey);
sptr<IRemoteObject> remoteObj_ { nullptr };
std::mutex observerMutex_;
sptr<SensorObserver> observer_ { nullptr };
std::mutex compatibleAppStrategyMutex_;
std::vector<CompatibleAppData> compatibleAppStrategyList_;
std::atomic_int32_t deviceMode_;
};
#define SENSOR_DATA_MGR DelayedSingleton<SensorDataManager>::GetInstance()
}
}
#endif