1/*
2 * Copyright (c) 2022-2023 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 DEVICESTATUS_MANAGER_H
17#define DEVICESTATUS_MANAGER_H
18
19#include <set>
20#include <map>
21
22#include "accesstoken_kit.h"
23#include "devicestatus_msdp_client_impl.h"
24#include "stationary_callback.h"
25#include "stationary_data.h"
26
27namespace OHOS {
28namespace Msdp {
29namespace DeviceStatus {
30using namespace Security::AccessToken;
31class DeviceStatusService;
32class DeviceStatusManager {
33public:
34    DeviceStatusManager() = default;
35    ~DeviceStatusManager() = default;
36
37    class DeviceStatusCallbackDeathRecipient : public IRemoteObject::DeathRecipient {
38    public:
39        DeviceStatusCallbackDeathRecipient() = default;
40        virtual void OnRemoteDied(const wptr<IRemoteObject> &remote);
41        virtual ~DeviceStatusCallbackDeathRecipient() = default;
42    };
43
44    bool Init();
45    bool Enable(Type type);
46    bool InitAlgoMngrInterface(Type type);
47    bool Disable(Type type);
48    int32_t InitDataCallback();
49    int32_t NotifyDeviceStatusChange(const Data &devicestatusData);
50    void Subscribe(Type type, ActivityEvent event, ReportLatencyNs latency, sptr<IRemoteDevStaCallback> callback);
51    void Unsubscribe(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback);
52    Data GetLatestDeviceStatusData(Type type);
53    int32_t MsdpDataCallback(const Data &data);
54    int32_t LoadAlgorithm();
55    int32_t UnloadAlgorithm();
56    int32_t GetPackageName(AccessTokenID tokenId, std::string &packageName);
57
58private:
59    struct classcomp {
60        bool operator()(sptr<IRemoteDevStaCallback> left, sptr<IRemoteDevStaCallback> right) const
61        {
62            return left->AsObject() < right->AsObject();
63        }
64    };
65    static constexpr int32_t argSize_ { TYPE_MAX };
66
67    std::mutex mutex_;
68    sptr<IRemoteObject::DeathRecipient> devicestatusCBDeathRecipient_ { nullptr };
69    std::shared_ptr<DeviceStatusMsdpClientImpl> msdpImpl_ { nullptr };
70    std::map<Type, OnChangedValue> msdpData_;
71    std::map<Type, std::set<const sptr<IRemoteDevStaCallback>, classcomp>> listeners_;
72    int32_t type_ { -1 };
73    int32_t event_ { -1 };
74    int32_t arrs_[argSize_] {};
75};
76} // namespace DeviceStatus
77} // namespace Msdp
78} // namespace OHOS
79#endif // DEVICESTATUS_MANAGER_H
80