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