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