1/* 2 * Copyright (c) 2021-2022 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 16import Log from '../../../../../../../common/src/main/ets/default/Log'; 17import HeadComponent from '../../../../../../../features/managementcomponent/src/main/ets/com/ohos/view/component/headComponent'; 18import AppItemComponent from '../../../../../../../features/managementcomponent/src/main/ets/com/ohos/view/component/appItemComponent'; 19import SwitchComponent from '../../../../../../../features/managementcomponent/src/main/ets/com/ohos/view/component/switchComponent'; 20import ConfigData from '../common/constants'; 21import NotificationConfigData from '../../../../../../../features/managementcomponent/src/main/ets/com/ohos/common/constants'; 22import Router from '@system.router'; 23import NotificationListener from '../../../../../../../features/managementcomponent/src/main/ets/com/ohos/model/notificationListener'; 24import notification from '@ohos.notification'; 25import { BundleItemData } from '@ohos/common'; 26import NotificationManager from '@ohos.notificationManager'; 27 28const TAG = 'NotificationManagement-SlotSetting'; 29const DEFAULT_SOUND = 'file://system/etc/Light.ogg'; 30 31interface ISlotParams { 32 appInfo?: BundleItemData, 33 slotInfo?: NotificationManager.NotificationSlot 34}; 35 36@Entry 37@Component 38export default struct SlotSetting { 39 private slotParams: ISlotParams = {}; 40 private appInfo: BundleItemData = {}; 41 private slotInfo: NotificationManager.NotificationSlot = {}; 42 @State headName: Resource = $r('app.string.notificationManagement') 43 @State allowNotice: Resource = $r('app.string.allowNotification') 44 @State noticeStyleBanner: Resource = $r('app.string.notificationStyle_banner') 45 @State noticeSound: Resource = $r('app.string.notificationSound') 46 @State initState: boolean = false; 47 48 build() { 49 Flex({ justifyContent: FlexAlign.SpaceBetween }) { 50 Column() { 51 GridContainer({ 52 columns: 12, 53 sizeType: SizeType.Auto, 54 gutter: vp2px(1) === 2 ? '12vp' : '0vp', 55 margin: vp2px(1) === 2 ? '12vp' : '0vp' 56 }) { 57 Row() { 58 Column() { 59 HeadComponent({ headName: $headName, isActive: true }); 60 Row() { 61 AppItemComponent({ 62 appIcon: this.appInfo.appIcon, 63 appTitle: this.appInfo.appTitle, 64 appSummary: this.appInfo.appSummary, 65 appValue: this.slotInfo.type?.toString(), 66 appArrow: this.appInfo.appArrow as string, 67 appArrowStyle: '', 68 appUri: this.appInfo.appUri, 69 appBundleName: this.appInfo.appBundleName, 70 appUid: this.appInfo.appUid, 71 appSwitch: 3 72 }); 73 } 74 .margin({ top: $r('app.float.page_notice_part_margin_t') }) 75 .padding({ 76 left: $r('sys.float.ohos_id_max_padding_start'), 77 right: $r('sys.float.ohos_id_default_padding_end') 78 }) 79 80 Row() { 81 SwitchComponent({ 82 title: $allowNotice, 83 initializationAction: (): Promise<boolean> => this.notificationSlotEnableInit(), 84 settingAction: (params): void => this.notificationSlotEnableSet(params as boolean) 85 }) 86 } 87 .margin({ top: $r('app.float.page_notice_title_margin_t') }) 88 .padding({ 89 left: $r('sys.float.ohos_id_card_margin_start'), 90 right: $r('sys.float.ohos_id_card_margin_end') 91 }) 92 .height($r('app.float.notice_row_height')) 93 94 Row() { 95 Text($r('app.string.notificationStyle')) 96 .fontColor($r('sys.color.ohos_id_color_text_secondary')) 97 .fontSize($r('sys.float.ohos_id_text_size_body2')) 98 .fontWeight(FontWeight.Bold) 99 .width(NotificationConfigData.WH_100_100) 100 .maxLines(NotificationConfigData.MAX_LINES_1) 101 .textOverflow({ overflow: TextOverflow.Ellipsis }) 102 }.height($r('app.float.notice_title_height')) 103 .padding({ 104 left: $r('sys.float.ohos_id_max_padding_start'), 105 right: $r('sys.float.ohos_id_max_padding_end') 106 }) 107 .margin({ top: $r('app.float.page_notice_part_margin_t')}) 108 .align(Alignment.Start) 109 .visibility(this.initState ? Visibility.Visible : Visibility.Hidden); 110 111 Row() { 112 SwitchComponent({ 113 title: $noticeStyleBanner, 114 initializationAction: (): Promise<boolean> => this.bannerEnableInit(), 115 settingAction: (params): void => this.bannerEnableSet(params as boolean) 116 }) 117 } 118 .padding({ 119 left: $r('sys.float.ohos_id_card_margin_start'), 120 right: $r('sys.float.ohos_id_card_margin_end') 121 }) 122 .margin({ top: $r('app.float.page_notice_title_margin_t')}) 123 .height($r('app.float.notice_row_height')) 124 .visibility(this.initState ? Visibility.Visible : Visibility.Hidden); 125 126 Row() { 127 SwitchComponent({ 128 title: $noticeSound, 129 initializationAction: (): Promise<boolean> => this.soundEnableInit(), 130 settingAction: (params): void => this.soundEnableSet(params as boolean) 131 }) 132 } 133 .padding({ 134 left: $r('sys.float.ohos_id_card_margin_start'), 135 right: $r('sys.float.ohos_id_card_margin_end') 136 }) 137 .margin({ top: $r('app.float.page_notice_row_margin_t') }) 138 .height($r('app.float.notice_row_height')) 139 .visibility(this.initState ? Visibility.Visible : Visibility.Hidden); 140 } 141 .width(ConfigData.WH_100_100) 142 .height(ConfigData.WH_100_100) 143 .useSizeType({ 144 xs: { span: 12, offset: 0 }, sm: { span: 12, offset: 0 }, 145 md: { span: 12, offset: 0 }, lg: { span: 8, offset: 2 } 146 }); 147 } 148 .width(ConfigData.WH_100_100) 149 .height(ConfigData.WH_100_100); 150 } 151 } 152 .backgroundColor($r("sys.color.ohos_id_color_sub_background")) 153 .width(ConfigData.WH_100_100) 154 .height(ConfigData.WH_100_100) 155 } 156 .width(ConfigData.WH_100_100) 157 } 158 159 aboutToAppear(): void{ 160 this.slotParams = Router.getParams(); 161 this.appInfo = this.slotParams.appInfo as BundleItemData; 162 this.slotInfo = this.slotParams.slotInfo as NotificationManager.NotificationSlot; 163 Log.showDebug(TAG, `aboutToAppear ${JSON.stringify(this.slotInfo)}`) 164 } 165 166 onBackPress() { 167 Log.showInfo(TAG, `onBackPress`) 168 } 169 170 notificationSlotEnableInit() { 171 Log.showDebug(TAG, `notificationSlotEnableInit`) 172 return NotificationListener.isNotificationSlotEnabled({ 173 bundle: this.appInfo.appBundleName, 174 uid: this.appInfo.appUid 175 }, this.slotInfo.type, 176 (data) => { 177 Log.showInfo(TAG, 'notificationSlotEnableInit callback' + data); 178 this.initState = data; 179 }); 180 } 181 182 notificationSlotEnableSet(data: boolean) { 183 Log.showDebug(TAG, `notificationEnableSet data:${JSON.stringify(data)}`) 184 this.initState = data; 185 NotificationListener.enableNotificationSlot({ bundle: this.appInfo.appBundleName, uid: this.appInfo.appUid }, this.slotInfo.type, data); 186 } 187 188 soundEnableInit(): Promise<boolean> { 189 Log.showDebug(TAG, `soundEnableInit`) 190 return new Promise((resolve, reject) => { 191 let result: boolean = false; 192 if (!!this.slotInfo.sound) { 193 result = true; 194 } 195 resolve(result); 196 Log.showInfo(TAG, `soundEnableInit state:${result}`) 197 }); 198 } 199 200 soundEnableSet(params: boolean) { 201 Log.showDebug(TAG, `soundEnableSet params:${JSON.stringify(params)}`) 202 if (!params) { 203 this.slotInfo.sound = ''; 204 if (this.slotInfo.level != notification.SlotLevel.LEVEL_HIGH) { 205 this.slotInfo.level = notification.SlotLevel.LEVEL_LOW; 206 } 207 } else if (params) { 208 this.slotInfo.sound = DEFAULT_SOUND; 209 if (this.slotInfo.level && this.slotInfo.level < notification.SlotLevel.LEVEL_DEFAULT) { 210 this.slotInfo.level = notification.SlotLevel.LEVEL_DEFAULT 211 } 212 } 213 this.notificationSlotSet(); 214 } 215 216 bannerEnableInit(): Promise<boolean> { 217 Log.showDebug(TAG, `bannerEnableInit`) 218 return new Promise((resolve, reject) => { 219 let result: boolean = false; 220 if (this.slotInfo.level == notification.SlotLevel.LEVEL_HIGH) { 221 result = true; 222 } 223 Log.showInfo(TAG, `bannerEnableInit state:${result}`) 224 resolve(result); 225 }); 226 } 227 228 bannerEnableSet(params: boolean) { 229 Log.showDebug(TAG, `bannerEnableSet params:${JSON.stringify(params)}`) 230 if (!params) { 231 if (this.slotInfo.sound) { 232 this.slotInfo.level = notification.SlotLevel.LEVEL_DEFAULT; 233 } else { 234 this.slotInfo.level = notification.SlotLevel.LEVEL_LOW; 235 } 236 } else if (params) { 237 this.slotInfo.level = notification.SlotLevel.LEVEL_HIGH; 238 } 239 this.notificationSlotSet(); 240 } 241 242 notificationSlotSet() { 243 Log.showDebug(TAG, `notificationSlotSet slotInfo:${JSON.stringify(this.slotInfo)}`); 244 if (this.appInfo.appBundleName == null || typeof(this.appInfo.appUid) !== 'number' || this.slotInfo == null) { 245 Log.showError(TAG, `notificationSlotSet, invalid parameter, bundle: ${this.appInfo?.appBundleName} ` + 246 `uid: ${this.appInfo?.appUid}`); 247 return; 248 } 249 NotificationListener.notificationSlotSet({ bundle: this.appInfo.appBundleName, uid: this.appInfo.appUid }, this.slotInfo); 250 } 251}