1/* 2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development 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 runningLock from '@ohos.runningLock'; 17import { ColumnOperation } from '../components/ColumnOperation'; 18import { getString } from '@ohos/commons/src/main/ets/util/ResourceUtil'; 19 20@Component 21export struct RunningLockManager { 22 @State result: string = ''; 23 private timeoutID: number = 0; 24 25 aboutToDisappear() { 26 if (this.timeoutID) { 27 clearTimeout(this.timeoutID); 28 } 29 } 30 31 build() { 32 Scroll() { 33 Column() { 34 Row() { 35 Text(this.result) 36 .fontWeight(FontWeight.Medium) 37 .fontSize(20) 38 } 39 .alignItems(VerticalAlign.Top) 40 .width('100%') 41 .backgroundColor($r("app.color.white")) 42 .height(160) 43 .padding(16) 44 .borderRadius(20) 45 .margin({ top: 16 }) 46 47 ColumnOperation({ operationRes: $r('app.strarray.running_lock_operations'), doOperation: this.doOperation }) 48 } 49 .width('100%') 50 .padding(4) 51 } 52 .scrollBar(BarState.Off) 53 } 54 55 doOperation = async (index: number) => { 56 switch (index) { 57 case 0: 58 let res1 = runningLock.isSupported(runningLock.RunningLockType.BACKGROUND); 59 let res2 = runningLock.isSupported(runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL); 60 this.result = `${getString($r('app.string.running_lock_background'))}${res1}\n${getString($r('app.string.running_lock_proximity_screen_control'))}${res2}`; 61 break 62 case 1: 63 try { 64 let lock = await runningLock.create('running_lock_test', runningLock.RunningLockType.BACKGROUND); 65 lock.hold(3000); 66 this.result = `lock time 3s, isLock: true`; 67 this.timeoutID = setTimeout(() => { 68 let isHolding = lock.isHolding(); 69 this.result = `lock time 3s, isLock: ${isHolding}`; 70 }, 3100) 71 } catch (err) { 72 this.result = `lock fail:${err}`; 73 } 74 break 75 case 2: 76 try { 77 let lock = await runningLock.create('running_lock_test2', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL); 78 lock.hold(3000); 79 this.result = `lock time 3s, isLock: true`; 80 this.timeoutID = setTimeout(() => { 81 let isHolding = lock.isHolding(); 82 this.result = `lock time 3s, isLock: ${isHolding}`; 83 }, 3100) 84 } catch (err) { 85 this.result = `lock fail:${err}`; 86 } 87 break 88 default: 89 break 90 } 91 } 92}