• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef SYSTEM_EVENT_OBSERVER_H
16 #define SYSTEM_EVENT_OBSERVER_H
17 
18 #include <memory>
19 #include <mutex>
20 #include <functional>
21 #include "common_event_subscriber.h"
22 #include "event_handler.h"
23 
24 namespace OHOS {
25 namespace IntellVoiceEngine {
26 using SystemEventReceiver = std::function<void(const OHOS::EventFwk::CommonEventData&)>;
27 class SystemEventObserver : public OHOS::EventFwk::CommonEventSubscriber,
28     public std::enable_shared_from_this<SystemEventObserver> {
29 public:
30     ~SystemEventObserver();
31     static std::shared_ptr<SystemEventObserver> Create(const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo,
32         SystemEventReceiver receiver);
GetPtr()33     std::shared_ptr<SystemEventObserver> GetPtr()
34     {
35         return shared_from_this();
36     }
37     void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) override;
38     bool Subscribe();
39     bool Unsubscribe();
40 
41 private:
42     enum SubscribeState {
43         IDLE = 0,
44         SUBSCRIBED = 1,
45         UNSUBSCRIBED = 2,
46     };
47 
48 private:
49     SystemEventObserver(const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo,
50         SystemEventReceiver receiver);
51     SubscribeState state_ = IDLE;
52     std::mutex mutex_;
53     SystemEventReceiver receiver_;
54 };
55 }
56 }
57 #endif