• 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 
16 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_DIALOG_MANAGER_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_DIALOG_MANAGER_H
18 
19 #include <list>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 
24 #include "common_event_data.h"
25 #include "common_event_subscriber.h"
26 #include "common_event_subscribe_info.h"
27 #include "refbase.h"
28 
29 #include "ans_dialog_callback_interface.h"
30 #include "ans_inner_errors.h"
31 
32 namespace OHOS::Notification {
33 class AdvancedNotificationService;
34 class NotificationBundleOption;
35 class NotificationDialogManager;
36 
37 enum class DialogStatus {
38     ALLOW_CLICKED,
39     DENY_CLICKED,
40     DIALOG_CRASHED,
41     DIALOG_SERVICE_DESTROYED
42 };
43 
44 class NotificationDialogEventSubscriber : public EventFwk::CommonEventSubscriber {
45 public:
46     DISALLOW_COPY_AND_MOVE(NotificationDialogEventSubscriber);
47     explicit NotificationDialogEventSubscriber(
48         NotificationDialogManager& dialogManager,
49         const EventFwk::CommonEventSubscribeInfo& subscribeInfo);
50     ~NotificationDialogEventSubscriber() override;
51 
52     static std::shared_ptr<NotificationDialogEventSubscriber> Create(NotificationDialogManager& dialogManager);
53     void OnReceiveEvent(const EventFwk::CommonEventData& data) override;
54 
55 private:
56     inline static const std::string EVENT_NAME = "OnNotificationServiceDialogClicked";
57     NotificationDialogManager& dialogManager_;
58 };
59 
60 class NotificationDialogManager final {
61 public:
62     DISALLOW_COPY_AND_MOVE(NotificationDialogManager);
63     NotificationDialogManager(AdvancedNotificationService& ans);
64     ~NotificationDialogManager();
65 
66     /*
67      * Subscribe CommonEvent, return false if failed
68      */
69     bool Init();
70 
71     struct DialogInfo {
72         sptr<NotificationBundleOption> bundleOption;
73         // When multi devices are going to be supported, a deviceId need to be stored
74         sptr<AnsDialogCallback> callback;
75     };
76 
77     /**
78      * @return ERR_OK when dialog serivce is requested successfully
79      * @return ERR_ANS_DIALOG_IS_POPPING when dialog is already popped
80      * @return ERROR_INTERNAL_ERROR for other errors
81      */
82     ErrCode RequestEnableNotificationDailog(
83         const sptr<NotificationBundleOption>& bundle,
84         const sptr<AnsDialogCallback>& callback,
85         const sptr<IRemoteObject>& callerToken
86     );
87 
88     /*
89      * Currently, notification dialog do not support multi device
90      * Due to that commonEvent is used for now and
91      * `NotificationDialogEventSubscriber` only subscribe commonEvent published by
92      * "com.ohos.notificationdialog", caller token is not checked
93      * when commonEvent callback is triggered.
94      */
95     ErrCode OnBundleEnabledStatusChanged(DialogStatus status, const std::string& bundleName);
96 
97     inline static const std::string NOTIFICATION_DIALOG_SERVICE_BUNDLE = "com.ohos.notificationdialog";
98     inline static const std::string NOTIFICATION_DIALOG_SERVICE_ABILITY = "EnableNotificationDialog";
99 
100 private:
101     inline static const std::string DEFAULT_DEVICE_ID = "";
102     static bool SetHasPoppedDialog(const sptr<NotificationBundleOption>& bundleOption, bool hasPopped);
103 
104     // bundle need to be not null
105     bool AddDialogInfoIfNotExist(const sptr<NotificationBundleOption>& bundle, const sptr<AnsDialogCallback>& callback);
106     sptr<NotificationBundleOption> GetBundleOptionByBundleName(const std::string& bundleName);
107     // bundle need to be not null
108     void RemoveDialogInfoByBundleOption(const sptr<NotificationBundleOption>& bundle,
109         std::unique_ptr<DialogInfo>& dialogInfoRemoved);
110     void RemoveAllDialogInfos(std::list<std::unique_ptr<DialogInfo>>& dialogInfosRemoved);
111 
112     bool OnDialogButtonClicked(const std::string& bundleName, bool enabled);
113     bool OnDialogCrashed(const std::string& bundleName);
114     bool OnDialogServiceDestroyed();
115 
116     bool HandleOneDialogClosed(sptr<NotificationBundleOption> bundleOption, EnabledDialogStatus status);
117     bool HandleAllDialogsClosed();
118 
119     std::shared_ptr<NotificationDialogEventSubscriber> dialogEventSubscriber = nullptr;
120     AdvancedNotificationService& ans_;
121     std::mutex dialogsMutex_;
122     std::list<std::unique_ptr<DialogInfo>> dialogsOpening_;
123 };
124 } // namespace OHOS::Notification
125 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_DIALOG_MANAGER_H
126