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/common'; 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 ColumnOperation({ operationRes: $r('app.strarray.running_lock_operations'), doOperation: this.doOperation }) 47 } 48 .width('100%') 49 .padding(4) 50 } 51 .scrollBar(BarState.Off) 52 } 53 54 doOperation = async (index: number) => { 55 switch (index) { 56 case 0: 57 let res1 = runningLock.isSupported(runningLock.RunningLockType.BACKGROUND); 58 let res2 = runningLock.isSupported(runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL); 59 this.result = `${getString($r('app.string.running_lock_background'))}${res1}\n${getString($r('app.string.running_lock_proximity_screen_control'))}${res2}`; 60 break 61 case 1: 62 try { 63 let lock = await runningLock.create('running_lock_test', runningLock.RunningLockType.BACKGROUND); 64 lock.hold(3000); 65 this.result = `lock time 3s, isLock: true`; 66 this.timeoutID = setTimeout(() => { 67 let isHolding = lock.isHolding(); 68 this.result = `lock time 3s, isLock: ${isHolding}`; 69 }, 3100) 70 } catch (err) { 71 this.result = `lock fail:${err}`; 72 } 73 break 74 case 2: 75 try { 76 let lock = await runningLock.create('running_lock_test2', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL); 77 lock.hold(3000); 78 this.result = `lock time 3s, isLock: true`; 79 this.timeoutID = setTimeout(() => { 80 let isHolding = lock.isHolding(); 81 this.result = `lock time 3s, isLock: ${isHolding}`; 82 }, 3100) 83 } catch (err) { 84 this.result = `lock fail:${err}`; 85 } 86 break 87 default: 88 break 89 } 90 } 91}