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 '@ohos/common' 17import Constants from '../common/constants' 18import service, {UnlockResult, AuthType, AuthSubType} from '../model/screenLockService' 19import {Callback} from '@ohos.base'; 20 21const TAG = 'ScreenLock-BaseViewModel' 22const MINUTE_NM = '分钟' 23const SECOND_NM = "秒" 24const TRY_AGAIN = "后重试" 25 26export {service, UnlockResult, AuthType, AuthSubType} 27 28export default class BaseViewModel { 29 prompt: Resource | string; 30 inhibitInput: boolean = false; 31 32 constructor() { 33 this.ViewModelInit() 34 } 35 36 ViewModelInit(): void{ 37 Log.showDebug(TAG, 'ViewModelInit'); 38 this.prompt = $r('app.string.input'); 39 } 40 41 countdown() { 42 Log.showInfo(TAG, `countdown`) 43 service.getAuthProperty(AuthType.PIN, (properties) => { 44 let promptText: Resource | string = ''; 45 let freezingMillisecond = properties.freezingTime; 46 Log.showInfo(TAG, `countdown freezingMillisecond:${freezingMillisecond}`) 47 if (freezingMillisecond > 0) { 48 promptText = this.getFreezingTimeNm(freezingMillisecond); 49 promptText += TRY_AGAIN; 50 setTimeout(this.countdown.bind(this), Constants.INTERVAL) 51 } else { 52 Log.showDebug(TAG, `countdown clearInterval`) 53 this.inhibitInput = false 54 promptText = $r('app.string.input'); 55 } 56 Log.showDebug(TAG, `countdown promptText:${promptText}`) 57 this.prompt = promptText; 58 }) 59 } 60 61 changePrompt(remainTimes, freezingTime, callback: Callback<void>) { 62 Log.showInfo(TAG, `changePrompt remainTimes:${remainTimes} freezingTime:${freezingTime}`) 63 let promptText: Resource | string = $r('app.string.incorrect'); 64 65 if (0 < remainTimes && remainTimes <= 2) { 66 if (freezingTime > 0) { 67 let freezingTimeNm = this.getFreezingTimeNm(freezingTime) 68 promptText = $r('app.string.incorrect_promp_freezing', remainTimes, freezingTimeNm); 69 } else { 70 promptText = $r('app.string.incorrect_promp_times', remainTimes); 71 } 72 } else if (0 == remainTimes) { 73 if (freezingTime > 0) { 74 this.inhibitInput = true 75 promptText = $r('app.string.input_promp', this.getFreezingTimeNm(freezingTime)); 76 setTimeout(this.countdown.bind(this), Constants.INTERVAL) 77 } 78 } 79 this.prompt = promptText; 80 this.updateStorage(callback) 81 //notify the base service that the unlock is fail 82 service.notifyUnlockScreenResult(UnlockResult.Fail); 83 } 84 85 getFreezingTimeNm(freezingMillisecond: number): string { 86 let minute = Math.floor(freezingMillisecond / (60 * 1000)); 87 let second = Math.round((freezingMillisecond % (60 * 1000)) / 1000); 88 Log.showInfo(TAG, `getFreezingTimeNm minute:${minute}, second:${second}`) 89 let timeName = ''; 90 if (minute != 0) { 91 timeName += minute + MINUTE_NM 92 } 93 if (second != 0) { 94 timeName += second + SECOND_NM 95 } 96 return timeName; 97 } 98 99 updateStorage(callback) { 100 Log.showInfo(TAG, `updateStorage`) 101 callback() 102 } 103 104 checkFreezingTime(callback: Callback<void>) { 105 service.getAuthProperty(AuthType.PIN, (properties) => { 106 if (properties.freezingTime > 0) { 107 this.inhibitInput = true 108 //Clear the entered password 109 this.changePrompt(properties.remainTimes, properties.freezingTime, callback) 110 } 111 }) 112 } 113} 114