• 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 type common from '@ohos.app.ability.common';
17import fs from '@ohos.file.fs';
18import Logger from './Logger';
19
20const TAG = '[Sample_StatePersistence]';
21const DATA_SIZE = 6;
22const PERSISTENT_FILE_NAME = '/Persistent.txt';
23
24function getConditionIDtoFile(context: common.Context, fileName: string): string {
25  Logger.debug(TAG, 'getConditionIDtoFile call');
26  if (context === undefined || context === null || typeof fileName !== 'string' || fileName === '') {
27    Logger.error(TAG, `getConditionIDtoFile failed, context : ${JSON.stringify(context)}`);
28    return '';
29  }
30
31  try {
32    let filePath = context.filesDir + fileName;
33    let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY);
34    let buff = new ArrayBuffer(DATA_SIZE);
35    let num = fs.readSync(file.fd, buff);
36    let conditionID = String.fromCharCode.apply(null, new Uint8Array(buff));
37    Logger.info(TAG, `getConditionIDtoFile : ${conditionID.length} ${conditionID}, ${num} ${filePath}`);
38    fs.closeSync(file);
39    return conditionID;
40  } catch (err) {
41    Logger.error(TAG, `getConditionIDtoFile err : ${JSON.stringify(err)}`);
42    return '';
43  }
44}
45
46function updateConditionIDtoFile(context: common.Context, conditionID: string, fileName: string): void {
47  Logger.debug(TAG, 'updateConditionIDtoFile call');
48  if (context === undefined || context === null ||
49  typeof conditionID !== 'string' || conditionID === '' ||
50  typeof fileName !== 'string' || fileName === '') {
51    Logger.error(TAG, `updateConditionIDtoFile failed, conditionID : ${JSON.stringify(conditionID)}, context : ${JSON.stringify(context)}`);
52    return;
53  }
54
55  try {
56    let filePath = context.filesDir + fileName;
57    let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
58    let num = fs.writeSync(file.fd, conditionID);
59    fs.closeSync(file);
60    Logger.info(TAG, `updateConditionIDtoFile : ${conditionID}, ${num} ${filePath}`);
61  } catch (err) {
62    Logger.error(TAG, `updateConditionIDtoFile err : ${JSON.stringify(err)}`);
63  }
64}
65
66export function getPersistentConditionID(context: common.Context): string {
67  return getConditionIDtoFile(context, PERSISTENT_FILE_NAME);
68}
69
70export function savePersistentConditionID(context: common.Context, conditionID: string): void {
71  updateConditionIDtoFile(context, conditionID, PERSISTENT_FILE_NAME);
72}