• 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 ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant';
17import fs from '@ohos.file.fs';
18import prompt from '@ohos.promptAction';
19import { logger } from '../util/Logger';
20
21const TAG: string = 'EntryAbility';
22const TIME: number = 3000;
23
24export default class CharacterOperation {
25  private context: Context;
26
27  constructor(context: Context) {
28    this.context = context;
29  }
30  // 设置中文
31  setZHCNLanguage = () => {
32    let applicationContext = this.context.getApplicationContext();
33    applicationContext.setLanguage('zh-cn');
34    logger.info(`${TAG} Chinese success`);
35  }
36  // 设置英文
37  setENUSLanguage = () => {
38    let applicationContext = this.context.getApplicationContext();
39    applicationContext.setLanguage('en-us');
40    logger.info(`${TAG} English success`);
41  }
42  // 设置深色模式
43  setDarKColorMode = () => {
44    let applicationContext = this.context.getApplicationContext();
45    applicationContext.setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_DARK);
46    prompt.showToast({
47      message: $r('app.string.switchDarkSuccess'), duration: TIME
48    });
49  }
50  // 设置浅色模式
51  setLightColorMode = () => {
52    let applicationContext = this.context.getApplicationContext();
53    applicationContext.setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT);
54    prompt.showToast({
55      message: $r('app.string.switchLightSuccess'), duration: TIME
56    });
57  }
58  // 获取缓存路径
59  generateStorageFiles = () => {
60    let context = this.context;
61    let filePathName = '/test.txt';
62    let cacheDirPath = context.cacheDir + filePathName;
63    let filesDirPath = context.filesDir + filePathName;
64    let preferencesDirPath = context.preferencesDir + filePathName;
65    let tempDirPath = context.tempDir + filePathName;
66    let databaseDirPath = context.databaseDir + filePathName;
67
68    return [cacheDirPath, filesDirPath, preferencesDirPath, tempDirPath, databaseDirPath];
69  }
70  // 创建存储数据
71  createStorageData = () => {
72    let context = this.context;
73    let filePathName = '/test.txt';
74    let cacheDirPath = context.cacheDir + filePathName;
75    let filesDirPath = context.filesDir + filePathName;
76    let preferencesDirPath = context.preferencesDir + filePathName;
77    let tempDirPath = context.tempDir + filePathName;
78    let databaseDirPath = context.databaseDir + filePathName;
79    try {
80      let file1 = fs.openSync(cacheDirPath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
81      let file2 = fs.openSync(filesDirPath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
82      let file3 = fs.openSync(preferencesDirPath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
83      let file4 = fs.openSync(tempDirPath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
84      let file5 = fs.openSync(databaseDirPath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
85
86      fs.closeSync(file1);
87      fs.closeSync(file2);
88      fs.closeSync(file3);
89      fs.closeSync(file4);
90      fs.closeSync(file5);
91
92      if (fs.accessSync(cacheDirPath) && fs.accessSync(filesDirPath)
93      && fs.accessSync(preferencesDirPath) && fs.accessSync(tempDirPath)
94      && fs.accessSync(databaseDirPath)) {
95        prompt.showToast({
96          message: $r('app.string.create_File_Success'), duration: TIME
97        });
98      }
99    } catch (error) {
100    }
101  }
102  // 检查文件
103  checkStorageData = () => {
104    let context = this.context;
105    let filePathName = '/test.txt';
106    let cacheDirPath = context.cacheDir + filePathName;
107    let filesDirPath = context.filesDir + filePathName;
108    let preferencesDirPath = context.preferencesDir + filePathName;
109    let tempDirPath = context.tempDir + filePathName;
110    let databaseDirPath = context.databaseDir + filePathName;
111    if (fs.accessSync(cacheDirPath) && fs.accessSync(filesDirPath)
112    && fs.accessSync(preferencesDirPath) && fs.accessSync(tempDirPath) && fs.accessSync(databaseDirPath)) {
113      prompt.showToast({
114        message: $r('app.string.search_File_Success'), duration: TIME
115      });
116    } else {
117      prompt.showToast({
118        message: $r('app.string.search_File_Failed'), duration: TIME
119      });
120    }
121  }
122  // 清理存储数据
123  clearStorageData = () => {
124    let context = this.context;
125    let filePathName = '/test.txt';
126    let cacheDirPath = context.cacheDir + filePathName;
127    let filesDirPath = context.filesDir + filePathName;
128    let preferencesDirPath = context.preferencesDir + filePathName;
129    let tempDirPath = context.tempDir + filePathName;
130    let databaseDirPath = context.databaseDir + filePathName;
131    if (fs.accessSync(cacheDirPath) && fs.accessSync(filesDirPath) && fs.accessSync(preferencesDirPath)
132    && fs.accessSync(tempDirPath) && fs.accessSync(databaseDirPath)) {
133      context.getApplicationContext().clearUpApplicationData((err) => {
134        logger.info(`clearUpApplicationData err:${JSON.stringify(err)}}`);
135        prompt.showToast({
136          message: $r('app.string.clean_Data_Success'), duration: TIME
137        });
138      });
139    } else {
140      // 清理存储数据失败
141      prompt.showToast({
142        message: $r('app.string.clean_Data_Failed'), duration: TIME
143      });
144    }
145  }
146}
147