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 16//import Window from '@ohos.window'; 17import Log from '../../../../../../../common/src/main/ets/default/Log'; 18import HeadComponent from '../../../../../../../features/managementcomponent/src/main/ets/com/ohos/view/component/headComponent'; 19import AppItemComponent from '../../../../../../../features/managementcomponent/src/main/ets/com/ohos/view/component/appItemComponent'; 20import SwitchComponent from '../../../../../../../features/managementcomponent/src/main/ets/com/ohos/view/component/switchComponent'; 21import SlotLstComponent from '../../../../../../../features/managementcomponent/src/main/ets/com/ohos/view/component/slotLstComponent'; 22import ConfigData from '../common/constants'; 23import Router from '@system.router' 24import notificationListener from '../../../../../../../features/managementcomponent/src/main/ets/com/ohos/model/notificationListener'; 25import Notification from '@ohos.notification'; 26import NotificationManager from '@ohos.notificationManager'; 27import { BundleItemData } from '@ohos/common'; 28 29const TAG = 'NotificationManagement-SetEnable'; 30 31let appInfo: BundleItemData; 32 33@Entry 34@Component 35export default struct SetEnable { 36 @State headName:Resource = $r('app.string.notificationManagement'); 37 @State allowNotice:Resource = $r('app.string.allowNotification'); 38 @State slotLst: NotificationManager.NotificationSlot[] = []; 39 @State initState: boolean = false; 40 build() { 41 Flex({ justifyContent: FlexAlign.SpaceBetween }) { 42 Column() { 43 GridContainer({columns:12, sizeType: SizeType.Auto, gutter: vp2px(1) === 2 ? '12vp' : '0vp', margin: vp2px(1) === 2 ? '12vp' : '0vp'}) { 44 Row() { 45 Column() { 46 HeadComponent({ headName: $headName, isActive: true }); 47 Row() { 48 AppItemComponent({ 49 appIcon: appInfo.appIcon, 50 appTitle: appInfo.appTitle, 51 appSummary: appInfo.appSummary, 52 appValue: '', 53 appArrow: appInfo.appArrow as string, 54 appArrowStyle: '', 55 appUri: appInfo.appUri, 56 appBundleName: appInfo.appBundleName, 57 appUid: appInfo.appUid, 58 appSwitch: 2 59 }); 60 } 61 .margin({ top: $r('app.float.page_notice_part_margin_t'), right: $r('app.float.page_margin_r') }) 62 .padding({ left: $r('sys.float.ohos_id_max_padding_start'), right: $r('sys.float.ohos_id_default_padding_end')}) 63 64 65 Row() { 66 SwitchComponent({ 67 title: $allowNotice, 68 initializationAction: (): Promise<boolean> => this.switchComponentInit(), 69 settingAction: (params?: boolean): void => this.switchComponentSet(params), 70 register: (listener) => notificationListener.register({bundle:appInfo.appBundleName,onEnableChanged:listener}) 71 }) 72 } 73 .margin({ top: $r('app.float.page_notice_title_margin_t')}) 74 .padding({ left: $r('sys.float.ohos_id_default_padding_start'), right: $r('sys.float.ohos_id_default_padding_end')}) 75 .height($r('app.float.notice_row_height')) 76 77 if (this.initState && this.slotLst?.length > 0) { 78 Row() { 79 SlotLstComponent({ appInfo: appInfo ,slotLst: $slotLst}) 80 } 81 .align(Alignment.Start) 82 } 83 } 84 .width(ConfigData.WH_100_100) 85 .height(ConfigData.WH_100_100) 86 .useSizeType({ 87 xs: { span: 12, offset: 0 }, sm: { span: 12, offset: 0 }, 88 md: { span: 12, offset: 0 }, lg: { span: 8, offset: 2 } 89 }); 90 } 91 .width(ConfigData.WH_100_100) 92 .height(ConfigData.WH_100_100); 93 } 94 } 95 .backgroundColor($r("sys.color.ohos_id_color_sub_background")) 96 .width(ConfigData.WH_100_100) 97 .height(ConfigData.WH_100_100) 98 } 99 .width(ConfigData.WH_100_100) 100 } 101 102 aboutToAppear(): void{ 103 Log.showInfo(TAG, `aboutToAppear`) 104 appInfo = Router.getParams(); 105 appInfo.slotSettingUrl = 'pages/slotSetting' 106 notificationListener.subscribeEnableChanged(); 107 Log.showInfo(TAG, `aboutToAppear end`); 108 } 109 110 onPageShow (): void{ 111 Log.showInfo(TAG, `onPageShow`); 112 Notification.getSlotsByBundle({ bundle: appInfo.appBundleName, uid: appInfo.appUid }, (err, data) => { 113 this.slotLst = []; 114 data.forEach((val, idx, array) => { 115 this.slotLst.push(val); 116 }) 117 }) 118 } 119 120 aboutToDisappear(): void{ 121 Log.showInfo(TAG, `aboutToDisappear`) 122 this.slotLst = []; 123 notificationListener.unsubscribeEnableChanged(); 124 } 125 126 onBackPress() { 127 Log.showInfo(TAG, `onBackPress`) 128 } 129 130 switchComponentInit() { 131 Log.showDebug(TAG, `switchComponentInit`) 132 return notificationListener.isNotificationEnabled({ bundle: appInfo.appBundleName, uid: appInfo.appUid }, (data: boolean) => { 133 Log.showDebug(TAG, 'switchComponentInit callback' + data); 134 this.initState = data; 135 }); 136 } 137 138 switchComponentSet(params?: boolean) { 139 Log.showDebug(TAG, `switchComponentSet`) 140 this.initState = params as boolean; 141 notificationListener.enableNotification({ bundle: appInfo.appBundleName, uid: appInfo.appUid }, params); 142 } 143}