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 */ 15import Log from '../../../../../../../../common/src/main/ets/default/Log'; 16import Notification from '@ohos.notification'; 17import {DoNotDisturbType} from '../common/constants'; 18import TimeManager from '../../../../../../../../common/src/main/ets/default/TimeManager'; 19 20const TAG = 'NotificationManagenment-NoDisturbingModel'; 21const MK_DATE_SPLIT = '-'; 22const MK_NUM_FILL = '0'; 23const VALUE_NUM_FILL = 10; 24 25export interface DoNotDisturbDateStr { 26 type?: number; 27 begin?: string; 28 end?: string; 29} 30 31export interface DoNotDisturbDate { 32 type: number; 33 begin: Date; 34 end: Date; 35} 36 37export default class NoDisturbingModel { 38 static getNoDisturbingDate(callback: { (data: DoNotDisturbDateStr): void;}): void { 39 Notification.getDoNotDisturbDate((error, data) => { 40 if (error.code != 0) { 41 Log.showError(TAG, 'getNoDisturbingDate error:' + JSON.stringify(error)); 42 } else { 43 Log.showDebug(TAG, 'getNoDisturbingDate data:' + JSON.stringify(data)); 44 let noDisturbingData: DoNotDisturbDateStr= { }; 45 noDisturbingData['type'] = data.type; 46 if (data.type == DoNotDisturbType.TYPE_CLEARLY) { 47 noDisturbingData['begin'] = this.formatDateTime(data.begin) ; 48 noDisturbingData['end'] = this.formatDateTime(data.end) ; 49 } else { 50 noDisturbingData['begin'] = this.formatTime(data.begin) ; 51 noDisturbingData['end'] = this.formatTime(data.end) ; 52 } 53 callback(noDisturbingData); 54 } 55 }); 56 } 57 58 static setNoDisturbingDate(noDisturbingTime: DoNotDisturbDate, callback: {(): void}): void { 59 let targetDate = noDisturbingTime; 60 Log.showDebug(TAG, `Notification.setDoNotDisturbDate targetDate['type'] : ${JSON.stringify(targetDate)}`); 61 Notification.setDoNotDisturbDate(targetDate, callback); 62 } 63 64 static formatDate (data: Date): string { 65 let result = data.getFullYear().toString() + MK_DATE_SPLIT; 66 let numTmp = data.getMonth() + 1; 67 if (numTmp < VALUE_NUM_FILL) { 68 result += MK_NUM_FILL; 69 } 70 result += (data.getMonth() + 1); 71 result += MK_DATE_SPLIT; 72 numTmp = data.getDate(); 73 if (numTmp < VALUE_NUM_FILL) { 74 result += MK_NUM_FILL; 75 } 76 result += numTmp; 77 return result; 78 } 79 static formatDateTime (data: Date): string { 80 let result = this.formatDate(data); 81 result += ' ' + TimeManager.formatTime(data); 82 return result; 83 } 84 static formatTime (data: Date): string { 85 return TimeManager.formatTime(data, true); 86 } 87} 88 89