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