• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 router from '@ohos.router'
16import Logger from '../model/Logger'
17import PreferencesUtils from '../model/PreferencesUtils'
18import TitleBar from '../common/TitleBar'
19
20const TAG: string = 'Index'
21
22@Entry
23@Component
24struct Index {
25  private defaultPassword: string = ''
26  private patternLockController: PatternLockController = new PatternLockController()
27  @State message: Resource = $r('app.string.message_input')
28  @State isReset: boolean = false
29  @State password: Array<number> = []
30  @State isHasPass: boolean = true
31  @State isShowSetting: boolean = false
32
33  build() {
34    Column() {
35      TitleBar()
36      Column() {
37        if (this.isHasPass && !this.isReset) {
38          Text($r('app.string.reset_password'))
39            .fontSize(25)
40            .fontWeight(FontWeight.Bold)
41            .padding(20)
42            .width('100%')
43            .fontColor(Color.Blue)
44            .onClick(() => {
45              this.isReset = true
46              this.message = $r('app.string.message_input_old')
47              this.password = []
48              this.patternLockController.reset()
49            })
50        }
51        Text(this.message)
52          .textAlign(TextAlign.Center)
53          .fontSize(30)
54          .width('90%')
55          .padding(10)
56        PatternLock(this.patternLockController)
57          .sideLength(300)
58          .circleRadius(15)
59          .pathStrokeWidth(15)
60          .autoReset(true)
61          .margin({ top: 30, bottom: 50 })
62          .onPatternComplete((input: Array<number>) => {
63            if (input === null || input === undefined || input.length < 5) {
64              this.message = $r('app.string.message_password_length_insufficient')
65              return
66            }
67            if (this.isHasPass) {
68              if (this.defaultPassword === input.toString()) {
69                if (this.isReset) {
70                  this.message = $r('app.string.message_input_new')
71                  this.defaultPassword = 'null'
72                  this.patternLockController.reset()
73                  this.password = []
74                  this.isHasPass = false
75                  return
76                }
77                router.replace({
78                  url: 'pages/Home'
79                })
80              } else {
81                this.message = $r('app.string.message_incorrect')
82                this.password = []
83                return
84              }
85            }
86            if (this.password.length > 0) {
87              if (this.password.toString() === input.toString()) {
88                this.password = input
89                this.message = $r('app.string.message_correct')
90                this.isShowSetting = true
91              } else {
92                this.message = $r('app.string.message_not_match')
93                this.patternLockController.reset()
94              }
95            } else {
96              this.password = input
97              this.message = $r('app.string.message_input_again')
98              this.patternLockController.reset()
99            }
100          })
101        if (this.isShowSetting) {
102          Button($r('app.string.message_set_password'))
103            .key('setPassword')
104            .margin(30)
105            .width('60%')
106            .onClick(() => {
107              this.setPassword()
108            })
109        }
110      }
111      .layoutWeight(1)
112      .justifyContent(FlexAlign.End)
113    }
114    .width('100%')
115    .height('100%')
116    .backgroundImage($r('app.media.bg'))
117    .backgroundImageSize(ImageSize.Cover)
118    .opacity(0.7)
119  }
120
121  async aboutToAppear() {
122    this.password = []
123    this.defaultPassword = <string> await PreferencesUtils.getPassword(getContext(this))
124    if (this.defaultPassword === 'null') {
125      this.isHasPass = false
126    } else {
127      this.isHasPass = true
128    }
129  }
130
131  async setPassword() {
132    this.defaultPassword = this.password.toString()
133    await PreferencesUtils.setPassword(this.defaultPassword, getContext(this))
134    this.message = $r('app.string.message_set_success')
135    this.isShowSetting = false
136    this.isHasPass = true
137    this.password = []
138    this.patternLockController.reset()
139    this.isReset = false
140  }
141}