• 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 UIAbility from '@ohos.app.ability.UIAbility';
17import hilog from '@ohos.hilog';
18import type window from '@ohos.window';
19import commonEvent from '@ohos.commonEventManager';
20import fs, { type Filter } from '@ohos.file.fs';
21
22const BUFSIZE = 1024;
23let DELAY_TIME = 100;
24let CLEAN_TEMP_DELAY_TIME = 1000;
25let OFFSETNUMBER = 0;
26
27let message;
28let secondMessage;
29let result;
30let files;
31let secFiles;
32let commonEventData = {
33  parameters: {
34    message: message,
35    secondMessage: secondMessage,
36    result: result,
37    files: files,
38    secFiles: secFiles
39  }
40};
41let actionStr;
42let tempDir;
43let applicationContext;
44let applicationTempDir;
45let currentContext;
46
47function getListFile(dir): number {
48  class ListFileOption {
49    public recursion: boolean = false;
50    public listNum: number = 0;
51    public filter: Filter = {};
52  }
53
54  let option = new ListFileOption();
55  option.filter.displayName = ['temp_useless*'];
56  let files = fs.listFileSync(dir, option);
57  for (let i = 0; i < files.length; i++) {
58    console.info(`The name of file: ${files[i]}`);
59  }
60  return files.length;
61}
62
63function startAndTerminate(action): void {
64  setTimeout(() => {
65    currentContext.startAbility(
66      {
67        bundleName: 'com.acts.cleancachefilesreply',
68        abilityName: 'SecondAbility',
69        parameters: {
70          key: action
71        }
72      }, (err) => {
73      console.log(action + ' startAbility err: ' + JSON.stringify(err));
74      if (action !== 'Acts_CleanTempFiles_0203') {
75        setTimeout(() => {
76          currentContext.terminateSelf().then(() => {
77            console.log(action + 'rely terminateSelf end');
78          }).catch((err) => {
79            console.log(action + 'rely terminateSelf err: ' + JSON.stringify(err));
80          });
81        }, DELAY_TIME);
82      }
83    });
84  }, DELAY_TIME);
85}
86
87function onForegroundInner(actionStr): void {
88  if (actionStr === 'Acts_CleanTempFiles_0201') {
89    let file = fs.openSync(tempDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
90    let writeLen = fs.writeSync(file.fd, 'Try to write str.');
91    let buf = new ArrayBuffer(BUFSIZE);
92    let readLen = fs.readSync(file.fd, buf, {
93      offset: OFFSETNUMBER
94    });
95    let readBuf = String.fromCharCode.apply(null, new Uint8Array(buf.slice(OFFSETNUMBER, readLen)));
96    console.log('Acts_CleanTempFiles_0201 writeLen is: ' + writeLen + 'readBuf is: ' + readBuf);
97    fs.closeSync(file);
98    commonEventData.parameters.message = readBuf;
99
100    let applicationFile = fs.openSync(applicationTempDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
101    let applicationWriteLen = fs.writeSync(applicationFile.fd, 'Try to write application str.');
102    let applicationReadLen = fs.readSync(applicationFile.fd, buf, {
103      offset: OFFSETNUMBER
104    });
105    let applicReadBuf = String.fromCharCode.apply(null, new Uint8Array(buf.slice(OFFSETNUMBER, applicationReadLen)));
106    console.log('Acts_CleanTempFiles_0201 secWriteLen is: ' + applicationWriteLen + 'secreadBuf is: ' + applicReadBuf);
107    fs.closeSync(applicationFile);
108    commonEventData.parameters.secondMessage = applicReadBuf;
109    commonEvent.publish('Acts_CleanTempFiles_0201', commonEventData, (err) => {
110      console.log('Acts_CleanTempFiles_0201 publish err: ' + JSON.stringify(err));
111      startAndTerminate('Acts_CleanTempFiles_0203');
112    });
113  } else if (actionStr === 'Acts_CleanTempFiles_0202') {
114    setTimeout(() => {
115      let file = fs.openSync(tempDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
116      let buf = new ArrayBuffer(BUFSIZE);
117      let readLen = fs.readSync(file.fd, buf, {
118        offset: OFFSETNUMBER
119      });
120      fs.closeSync(file);
121      let readBuf = String.fromCharCode.apply(null, new Uint8Array(buf.slice(OFFSETNUMBER, readLen)));
122      commonEventData.parameters.message = readBuf;
123
124      let applicationFile = fs.openSync(applicationTempDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
125      let applicationReadLen = fs.readSync(applicationFile.fd, buf, {
126        offset: OFFSETNUMBER
127      });
128      fs.closeSync(applicationFile);
129      let applicaReadBuf = String.fromCharCode.apply(null, new Uint8Array(buf.slice(OFFSETNUMBER, applicationReadLen)));
130      console.log('Acts_CleanTempFiles_0202 application Message: ' + JSON.stringify(applicationReadLen));
131      commonEventData.parameters.secondMessage = applicaReadBuf;
132      commonEvent.publish('Acts_CleanTempFiles_0202', commonEventData, (err) => {
133        console.log('Acts_CleanTempFiles_0202 publish err: ' + JSON.stringify(err));
134        startAndTerminate('Acts_CleanTempFiles_0204');
135      });
136    }, CLEAN_TEMP_DELAY_TIME)
137  }
138  cleanTempFiles(actionStr);
139}
140
141function cleanTempFiles(actionStr): void {
142  if (actionStr === 'Acts_CleanTempFiles_0301') {
143    AppStorage.setOrCreate('message', actionStr);
144    let file = fs.openSync(tempDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
145    let writeLen = fs.writeSync(file.fd, 'Try to write Acts_CleanTempFiles_0301 str.');
146    console.log('Acts_CleanTempFiles_0301 The length of str is: ' + writeLen);
147    let buf = new ArrayBuffer(BUFSIZE);
148    let readLen = fs.readSync(file.fd, buf, {
149      offset: OFFSETNUMBER
150    });
151    let readBuf = String.fromCharCode.apply(null, new Uint8Array(buf.slice(OFFSETNUMBER, readLen)));
152    console.log('Acts_CleanTempFiles_0301 the content of file: ' + readBuf);
153    fs.closeSync(file);
154    console.log('Acts_CleanTempFiles_0301 Message: ' + JSON.stringify(readBuf));
155    commonEventData.parameters.message = readBuf;
156    commonEvent.publish('Acts_CleanTempFiles_0301', commonEventData, (err) => {
157      console.log('Acts_CleanTempFiles_0301 publish err: ' + JSON.stringify(err));
158      setTimeout(() => {
159        this.context.terminateSelf();
160      }, DELAY_TIME);
161    });
162  } else if (actionStr === 'Acts_CleanTempFiles_0302') {
163    setTimeout(() => {
164      AppStorage.setOrCreate('message', actionStr);
165      let file = fs.openSync(tempDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
166      let buf = new ArrayBuffer(BUFSIZE);
167      let readLen = fs.readSync(file.fd, buf, {
168        offset: OFFSETNUMBER
169      });
170      fs.closeSync(file);
171      let readBuf = String.fromCharCode.apply(null, new Uint8Array(buf.slice(OFFSETNUMBER, readLen)));
172      console.log('Acts_CleanTempFiles_0302 Message: ' + JSON.stringify(readLen));
173      commonEventData.parameters.message = readBuf;
174      commonEvent.publish('Acts_CleanTempFiles_0302', commonEventData, (err) => {
175        console.log('Acts_CleanTempFiles_0302 publish err: ' + JSON.stringify(err));
176        currentContext.terminateSelf();
177      });
178    }, CLEAN_TEMP_DELAY_TIME)
179  }
180}
181
182export default class EntryAbility extends UIAbility {
183  onCreate(want): void {
184    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
185    actionStr = want.parameters.key;
186    tempDir = this.context.tempDir;
187    currentContext = this.context;
188    applicationContext = this.context.getApplicationContext();
189    applicationTempDir = applicationContext.tempDir;
190  }
191
192  onDestroy(): void {
193    hilog.info(0x0000, 'EntryAbility', '%{public}s', 'Ability onDestroy');
194  }
195
196  onWindowStageCreate(windowStage: window.WindowStage): void {
197    hilog.info(0x0000, 'EntryAbility', '%{public}s', 'Ability onWindowStageCreate');
198
199    windowStage.loadContent('pages/Index', (err, data) => {
200      if (err.code) {
201        hilog.error(0x0000, 'EntryAbility', 'Failed to load the content. Cause: ', JSON.stringify(err) ?? '');
202        return;
203      }
204      hilog.info(0x0000, 'EntryAbility', 'Succeeded in loading the content. Data: ', JSON.stringify(data) ?? '');
205    });
206  }
207
208  onWindowStageDestroy(): void {
209    hilog.info(0x0000, 'EntryAbility', '%{public}s', 'Ability onWindowStageDestroy');
210  }
211
212  onForeground(): void {
213    if (actionStr === 'Acts_CleanTempFiles_0101') {
214      let file = fs.openSync(tempDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
215      let writeLen = fs.writeSync(file.fd, 'Try to write str.');
216      let buf = new ArrayBuffer(BUFSIZE);
217      let readLen = fs.readSync(file.fd, buf, {
218        offset: OFFSETNUMBER
219      });
220      let readBuf = String.fromCharCode.apply(null, new Uint8Array(buf.slice(OFFSETNUMBER, readLen)));
221      console.log('Acts_CleanTempFiles_0101 writeLen is: ' + writeLen + 'readBuf is: ' + readBuf);
222      fs.closeSync(file);
223      commonEventData.parameters.message = readBuf;
224      let applicationFile = fs.openSync(applicationTempDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
225      let applicationWriteLen = fs.writeSync(applicationFile.fd, 'Try to write application str.');
226      let applicationReadLen = fs.readSync(applicationFile.fd, buf, {
227        offset: OFFSETNUMBER
228      });
229      let applicaReadBuf = String.fromCharCode.apply(null, new Uint8Array(buf.slice(OFFSETNUMBER, applicationReadLen)));
230      fs.closeSync(applicationFile);
231      console.log('Acts_CleanTempFiles_0101 secWriteLen: ' + applicationWriteLen + 'secreadBuf: ' + applicaReadBuf);
232      commonEventData.parameters.secondMessage = applicaReadBuf;
233      commonEvent.publish('Acts_CleanTempFiles_0101', commonEventData, (err) => {
234        console.log('Acts_CleanTempFiles_0101 publish err: ' + JSON.stringify(err));
235        startAndTerminate('Acts_CleanTempFiles_0103');
236      });
237    } else if (actionStr === 'Acts_CleanTempFiles_0102') {
238      setTimeout(() => {
239        let file = fs.openSync(tempDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
240        let buf = new ArrayBuffer(BUFSIZE);
241        let readLen = fs.readSync(file.fd, buf, {
242          offset: OFFSETNUMBER
243        });
244        fs.closeSync(file);
245        commonEventData.parameters.message = readLen;
246        let dir = tempDir.split('temp');
247        commonEventData.parameters.files = getListFile(dir[0]);
248        let applicationFile =
249          fs.openSync(applicationTempDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
250        let applicationReadLen = fs.readSync(applicationFile.fd, buf, {
251          offset: OFFSETNUMBER
252        });
253        fs.closeSync(applicationFile);
254        commonEventData.parameters.secondMessage = applicationReadLen;
255        let secDir = tempDir.split('temp');
256        commonEventData.parameters.secFiles = getListFile(secDir[0]);
257        commonEvent.publish('Acts_CleanTempFiles_0102', commonEventData, (err) => {
258          console.log('Acts_CleanTempFiles_0102 publish err: ' + JSON.stringify(err));
259          startAndTerminate('Acts_CleanTempFiles_0104');
260        });
261      }, CLEAN_TEMP_DELAY_TIME)
262    }
263    onForegroundInner(actionStr);
264  }
265
266  onBackground(): void {
267    hilog.info(0x0000, 'EntryAbility', '%{public}s', 'Ability onBackground');
268  }
269}