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 REMOVE_BUNDLE 43 }; 44 45 class NotificationDialogEventSubscriber : public EventFwk::CommonEventSubscriber { 46 public: 47 DISALLOW_COPY_AND_MOVE(NotificationDialogEventSubscriber); 48 explicit NotificationDialogEventSubscriber( 49 NotificationDialogManager& dialogManager, 50 const EventFwk::CommonEventSubscribeInfo& subscribeInfo); 51 ~NotificationDialogEventSubscriber() override; 52 53 static std::shared_ptr<NotificationDialogEventSubscriber> Create(NotificationDialogManager& dialogManager); 54 void OnReceiveEvent(const EventFwk::CommonEventData& data) override; 55 56 private: 57 inline static const std::string EVENT_NAME = "OnNotificationServiceDialogClicked"; 58 59 NotificationDialogManager& dialogManager_; 60 }; 61 62 class NotificationDialogManager final { 63 public: 64 DISALLOW_COPY_AND_MOVE(NotificationDialogManager); 65 NotificationDialogManager(AdvancedNotificationService& ans); 66 ~NotificationDialogManager(); 67 68 /* 69 * Subscribe CommonEvent, return false if failed 70 */ 71 bool Init(); 72 73 struct DialogInfo { 74 sptr<NotificationBundleOption> bundleOption; 75 // When multi devices are going to be supported, a deviceId need to be stored 76 sptr<AnsDialogCallback> callback; 77 }; 78 79 /** 80 * @return ERR_OK when dialog serivce is requested successfully 81 * @return ERR_ANS_DIALOG_IS_POPPING when dialog is already popped 82 * @return ERROR_INTERNAL_ERROR for other errors 83 */ 84 ErrCode RequestEnableNotificationDailog( 85 const sptr<NotificationBundleOption>& bundle, 86 const sptr<AnsDialogCallback>& callback, 87 const sptr<IRemoteObject>& callerToken 88 ); 89 90 /* 91 * Currently, notification dialog do not support multi device 92 * Due to that commonEvent is used for now and 93 * `NotificationDialogEventSubscriber` only subscribe commonEvent published by 94 * "com.ohos.notificationdialog", caller token is not checked 95 * when commonEvent callback is triggered. 96 */ 97 ErrCode OnBundleEnabledStatusChanged(DialogStatus status, const std::string& bundleName, const int32_t& uid); 98 99 /* 100 * AddDialogInfo 101 * @return ERR_OK when add Dialog successfully 102 */ 103 ErrCode AddDialogInfo(const sptr<NotificationBundleOption>& bundle, const sptr<AnsDialogCallback>& callback); 104 105 /* 106 * RemoveDialogInfoByBundleOption 107 * @return void 108 */ 109 void RemoveDialogInfoByBundleOption(const sptr<NotificationBundleOption>& bundle, 110 std::unique_ptr<DialogInfo>& dialogInfoRemoved); 111 112 inline static const std::string NOTIFICATION_DIALOG_SERVICE_BUNDLE = "com.ohos.notificationdialog"; 113 inline static const std::string NOTIFICATION_DIALOG_SERVICE_ABILITY = "EnableNotificationDialog"; 114 115 private: 116 inline static const std::string DEFAULT_DEVICE_ID = ""; 117 static bool SetHasPoppedDialog(const sptr<NotificationBundleOption>& bundleOption, bool hasPopped); 118 119 // bundle need to be not null 120 bool AddDialogInfoIfNotExist(const sptr<NotificationBundleOption>& bundle, const sptr<AnsDialogCallback>& callback); 121 sptr<NotificationBundleOption> GetBundleOptionByBundleName(const std::string& bundleName, const int32_t& uid); 122 void RemoveAllDialogInfos(std::list<std::unique_ptr<DialogInfo>>& dialogInfosRemoved); 123 124 bool OnDialogButtonClicked(const std::string& bundleName, const int32_t& uid, bool enabled); 125 bool OnDialogCrashed(const std::string& bundleName, const int32_t& uid); 126 bool OnDialogServiceDestroyed(); 127 bool onRemoveBundle(const std::string bundleName, const int32_t& uid); 128 129 bool HandleOneDialogClosed(sptr<NotificationBundleOption> bundleOption, EnabledDialogStatus status); 130 bool HandleAllDialogsClosed(); 131 132 std::shared_ptr<NotificationDialogEventSubscriber> dialogEventSubscriber = nullptr; 133 AdvancedNotificationService& ans_; 134 std::mutex dialogsMutex_; 135 std::list<std::unique_ptr<DialogInfo>> dialogsOpening_; 136 }; 137 } // namespace OHOS::Notification 138 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_DIALOG_MANAGER_H 139