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 const bool innerLake 89 ); 90 91 /* 92 * Currently, notification dialog do not support multi device 93 * Due to that commonEvent is used for now and 94 * `NotificationDialogEventSubscriber` only subscribe commonEvent published by 95 * "com.ohos.notificationdialog", caller token is not checked 96 * when commonEvent callback is triggered. 97 */ 98 ErrCode OnBundleEnabledStatusChanged(DialogStatus status, const std::string& bundleName, const int32_t& uid); 99 100 /* 101 * AddDialogInfo 102 * @return ERR_OK when add Dialog successfully 103 */ 104 ErrCode AddDialogInfo(const sptr<NotificationBundleOption>& bundle, const sptr<AnsDialogCallback>& callback); 105 106 /* 107 * RemoveDialogInfoByBundleOption 108 * @return void 109 */ 110 void RemoveDialogInfoByBundleOption(const sptr<NotificationBundleOption>& bundle, 111 std::unique_ptr<DialogInfo>& dialogInfoRemoved); 112 113 inline static const std::string NOTIFICATION_DIALOG_SERVICE_BUNDLE = "com.ohos.notificationdialog"; 114 inline static const std::string NOTIFICATION_DIALOG_SERVICE_ABILITY = "EnableNotificationDialog"; 115 116 private: 117 inline static const std::string DEFAULT_DEVICE_ID = ""; 118 static bool SetHasPoppedDialog(const sptr<NotificationBundleOption>& bundleOption, bool hasPopped); 119 120 // bundle need to be not null 121 bool AddDialogInfoIfNotExist(const sptr<NotificationBundleOption>& bundle, const sptr<AnsDialogCallback>& callback); 122 sptr<NotificationBundleOption> GetBundleOptionByBundleName(const std::string& bundleName, const int32_t& uid); 123 void RemoveAllDialogInfos(std::list<std::unique_ptr<DialogInfo>>& dialogInfosRemoved); 124 125 bool OnDialogButtonClicked(const std::string& bundleName, const int32_t& uid, bool enabled); 126 bool OnDialogCrashed(const std::string& bundleName, const int32_t& uid); 127 bool OnDialogServiceDestroyed(); 128 bool onRemoveBundle(const std::string bundleName, const int32_t& uid); 129 130 bool HandleOneDialogClosed(sptr<NotificationBundleOption> bundleOption, EnabledDialogStatus status); 131 bool HandleAllDialogsClosed(); 132 133 std::shared_ptr<NotificationDialogEventSubscriber> dialogEventSubscriber = nullptr; 134 AdvancedNotificationService& ans_; 135 std::mutex dialogsMutex_; 136 std::list<std::unique_ptr<DialogInfo>> dialogsOpening_; 137 }; 138 } // namespace OHOS::Notification 139 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_DIALOG_MANAGER_H 140