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