• 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, Trace} from '@ohos/common'
17import Constants from '../common/constants'
18import BaseViewModel, {service, AuthSubType} from './baseViewModel'
19import {Callback} from '@ohos.base';
20
21const TAG = 'ScreenLock-CustomPSDViewModel'
22
23export default class CustomPSDViewModel extends BaseViewModel {
24    passwordArr: any[] = [];
25    numKeyboard: any[] = Constants.NUMKEY_BOARD;
26
27    constructor() {
28        super();
29    }
30
31    ViewModelInit(): void{
32        Log.showDebug(TAG, 'ViewModelInit');
33        super.ViewModelInit();
34    }
35
36    onKeyPress(index, callback) {
37        Log.showInfo(TAG, `onKeyPress start param: ${index}`)
38        let keyValue = this.numKeyboard[index].value;
39        if (keyValue >= 0 && !this.inhibitInput) {
40            if (this.passwordArr.length < Constants.PASSWORD_MAX_LEN) {
41                this.passwordArr.push(keyValue);
42                this.numKeyboard[11].row1 = $r('app.string.delete');
43                this.numKeyboard[11].value = Constants.DEL_PWD;
44                this.updateStorage(callback);
45            }
46        } else if (keyValue == Constants.DEL_PWD) {
47            this.passwordArr.pop()
48            if (this.passwordArr.length == 0) {
49                this.numKeyboard[11].row1 = $r('app.string.back');
50                this.numKeyboard[11].value = Constants.GO_BACK;
51            }
52            this.updateStorage(callback);
53        } else if (keyValue == Constants.GO_BACK) {
54            AppStorage.SetOrCreate('slidestatus', false);
55            service.goBack();
56        } else if (keyValue == Constants.CALL_PHONE) {
57        }
58    }
59
60    onCallPhone() {
61        Log.showInfo(TAG, 'onCallPhone');
62    }
63
64    onAuthPassword(callback: Callback<void>) {
65        Log.showInfo(TAG, `onAuthPassword`);
66        if (this.passwordArr.length == 0 || this.inhibitInput) {
67            this.updateStorage(callback);
68            return;
69        }
70        Trace.start(Trace.CORE_METHOD_UNLOCK_SCREEN);
71        Trace.start(Trace.CORE_METHOD_CALL_ACCOUNT_SYSTEM);
72        service.authUser(AuthSubType.PIN_MIXED, this.passwordArr, (result, extraInfo) => {
73            this.clearPassword();
74            if (result == 0) {
75                //unlock the screen
76                service.unlocking();
77                service.goBack();
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