• 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 Trace from '../../../../../../../../common/src/main/ets/default/Trace'
17import Log from '../../../../../../../../common/src/main/ets/default/Log'
18import Constants from '../common/constants'
19import BaseViewModel, {service, AuthType, AuthSubType} from './baseViewModel'
20import {Callback} from 'basic';
21
22const TAG = 'ScreenLock-CustomPSDViewModel'
23
24export default class CustomPSDViewModel extends BaseViewModel {
25    passwordArr: any[] = [];
26    numKeyboard: any[] = Constants.NUMKEY_BOARD;
27
28    constructor() {
29        super();
30    }
31
32    ViewModelInit(): void{
33        Log.showDebug(TAG, 'ViewModelInit');
34        super.ViewModelInit();
35    }
36
37    onKeyPress(index, callback) {
38        Log.showInfo(TAG, `onKeyPress start param: ${index}`)
39        let keyValue = this.numKeyboard[index].value;
40        if (keyValue >= 0 && !this.inhibitInput) {
41            if (this.passwordArr.length < Constants.PASSWORD_MAX_LEN) {
42                this.passwordArr.push(keyValue);
43                this.numKeyboard[11].row1 = $r('app.string.delete');
44                this.numKeyboard[11].value = Constants.DEL_PWD;
45                this.updateStorage(callback);
46            }
47        } else if (keyValue == Constants.DEL_PWD) {
48            this.passwordArr.pop()
49            if (this.passwordArr.length == 0) {
50                this.numKeyboard[11].row1 = $r('app.string.back');
51                this.numKeyboard[11].value = Constants.GO_BACK;
52            }
53            this.updateStorage(callback);
54        } else if (keyValue == Constants.GO_BACK) {
55            AppStorage.SetOrCreate('slidestatus', false);
56            service.goBack();
57        } else if (keyValue == Constants.CALL_PHONE) {
58        }
59    }
60
61    onCallPhone() {
62        Log.showInfo(TAG, 'onCallPhone');
63    }
64
65    onAuthPassword(callback: Callback<void>) {
66        Log.showInfo(TAG, `onAuthPassword`);
67        if (this.passwordArr.length == 0 || this.inhibitInput) {
68            this.updateStorage(callback);
69            return;
70        }
71        Trace.start(Trace.CORE_METHOD_UNLOCK_SCREEN);
72        Trace.start(Trace.CORE_METHOD_CALL_ACCOUNT_SYSTEM);
73        service.authUser(AuthSubType.PIN_MIXED, this.passwordArr, (result, extraInfo) => {
74            this.clearPassword();
75            if (result == 0) {
76                //unlock the screen
77                service.unlocking();
78            } else {
79                //Clear the entered password
80                super.changePrompt(extraInfo.remainTimes, extraInfo.freezingTime, callback);
81            }
82        });
83    }
84
85    clearPassword() {
86        Log.showInfo(TAG, `clearPassword`)
87        this.passwordArr = [];
88        this.numKeyboard[11].row1 = $r('app.string.back');
89        this.numKeyboard[11].value = Constants.GO_BACK;
90        this.updateStorage(() => {
91        })
92    }
93
94    updateStorage(callback) {
95        Log.showInfo(TAG, `updateStorage child`)
96        //refresh  the page
97        AppStorage.SetOrCreate('passwordArr', this.passwordArr);
98        AppStorage.SetOrCreate('numKeyboard', this.numKeyboard);
99        super.updateStorage(callback)
100    }
101}
102