1/* 2 * Copyright (c) 2023 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 process from '@ohos.process'; 17import { ColumnOperation } from '../components/ColumnOperation'; 18import { getString } from '@ohos/common/src/main/ets/util/ResourceUtil'; 19import { logger } from '@ohos/common/src/main/ets/util/Logger'; 20 21let child: process.ChildProcess | undefined = undefined; 22const TAG: string = 'Process'; 23 24@Component 25export struct Process { 26 @State result: string = ''; 27 28 build() { 29 Scroll() { 30 Column() { 31 Text(this.result) 32 .width('100%') 33 .height(100) 34 .backgroundColor('#DFDFDF') 35 .fontSize(20) 36 .margin({ top: 20 }) 37 .textAlign(TextAlign.Start) 38 .padding(10) 39 ColumnOperation({ operationRes: $r('app.strarray.process_operations'), doOperation: this.doOperation }) 40 } 41 .width('100%') 42 .padding(16) 43 } 44 .scrollBar(BarState.Off) 45 .align(Alignment.Start) 46 } 47 48 doOperation = (index: number) => { 49 let proManager = new process.ProcessManager(); 50 switch (index) { 51 case 0: 52 this.result = process.uptime().toString() + 's'; 53 logger.info(TAG, 'uptime=' + this.result); 54 break; 55 case 1: 56 logger.info(TAG, 'process cwd'); 57 this.result = process.cwd().toString(); 58 break; 59 case 2: 60 logger.info(TAG, 'process change dir'); 61 process.chdir('/system'); 62 this.result = process.cwd().toString(); 63 break; 64 case 3: 65 child = process.runCmd('sleep 2;ls'); 66 this.result = child == null ? 'failed' : 'succeed'; 67 logger.info(TAG, 'runCmd=' + this.result); 68 let childResult = child.wait(); 69 childResult.then(val => { 70 this.result = 'child process run finish' + JSON.stringify(val); 71 logger.info(TAG, 'runCmd result =' + val); 72 }) 73 break; 74 case 4: 75 if (child != null) { 76 this.result = getString($r('app.string.close_success')); 77 child.close(); 78 } else { 79 this.result = getString($r('app.string.child_null')); 80 } 81 logger.info(TAG, this.result); 82 break; 83 case 5: 84 let pres = process.pid; 85 let result = proManager.kill(28, pres); 86 if (result === true) { 87 this.result = getString($r('app.string.success')); 88 } else { 89 this.result = getString($r('app.string.failed')); 90 } 91 break; 92 case 6: 93 proManager.exit(0); 94 break; 95 default: 96 break; 97 } 98 } 99}