• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024-2025 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 buffer from '@ohos.buffer';
17import InputMethodExtensionAbility from '@ohos.InputMethodExtensionAbility';
18import keyboardController from './model/KeyboardController';
19import Want from '@ohos.app.ability.Want';
20import fs, { ReadOptions } from '@ohos.file.fs';
21import { logger } from '../utils/Logger';
22
23export default class InputDemo1Service extends InputMethodExtensionAbility {
24  onCreate(want: Want): void {
25    keyboardController.onCreate(this.context); // 初始化窗口并注册对输入法框架的事件监听
26    let filesDir = this.context.filesDir;
27    let filesDir2 = this.context.getApplicationContext().filesDir;
28    // 创建inputmethod module级别的独立extension的沙箱文件 /entry
29    let file1 = fs.openSync(filesDir + '/inputtest1.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
30    // 写入一段内容至文件
31    let writeLen = fs.writeSync(file1.fd, 'main Try to write str.');
32    logger.info('main The length of str is:' + writeLen);
33    // 关闭文件
34    fs.closeSync(file1);
35    // 创建inputmethod应用级别的独立extension的沙箱文件
36    let file2 = fs.openSync(filesDir2 + '/inputtest2.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
37    // 写入一段内容至文件
38    let writeLen2 = fs.writeSync(file2.fd, 'main Try to write str.'); //返回流文件length
39    logger.info('main The length of str is:' + writeLen2);
40    // 关闭文件
41    fs.closeSync(file2);
42
43    //预期结果,无法读写
44    try {
45      fs.openSync(filesDir + '/inputtest3.txt', fs.OpenMode.READ_WRITE);
46    } catch (e) {
47      logger.error(`input openSync module err: ${JSON.stringify(e)}`);
48    }
49
50    //预期结果,无法读写
51    try {
52      fs.openSync(filesDir2 + '/inputtest4.txt', fs.OpenMode.READ_WRITE);
53    } catch (e) {
54      logger.error(`input openSync app err: ${JSON.stringify(e)}`);
55    }
56
57    // inputmethod 访问其配置groupId的宿主应用的共享沙箱 访问成功 用户开启非严格模式 可以读写
58    this.context.getGroupDir('test1', (err, data) => {
59      if (err) {
60        logger.error(`1:getGroupDir failed, err: ${JSON.stringify(err)}`);
61      } else {
62        filesDir = data;
63        try {
64          let file = fs.openSync(filesDir + '/groupTest1.txt', fs.OpenMode.READ_WRITE);
65          let writeLen = 0;
66          try {
67            writeLen = fs.writeSync(file.fd, '1:inputMethod Try to write str.');
68          } catch (e) {
69            logger.error(`1:fs.writeSync err: ${JSON.stringify(e)}`);
70          }
71          logger.info('1:The length of str is:' + writeLen);
72          // 从文件读取一段内容
73          let arrayBuffer = new ArrayBuffer(1024);
74          let readOptions: ReadOptions = {
75            offset: 0,
76            length: arrayBuffer.byteLength
77          };
78          let readLen = fs.readSync(file.fd, arrayBuffer, readOptions);
79          let buf = buffer.from(arrayBuffer, 0, readLen);
80          logger.info('1:the content of file:' + buf.toString());
81          // 关闭文件
82          fs.closeSync(file);
83        } catch (e) {
84          logger.error(`1:fs.openSync err: ${JSON.stringify(e)}`);
85        }
86      }
87    });
88
89    // 访问失败 没有权限
90    this.context.getGroupDir('test2', (err, data) => {
91      if (err) {
92        logger.error(`2:getGroupDir failed, err: ${JSON.stringify(err)}`);
93      } else {
94        filesDir = data;
95        try {
96          let file = fs.openSync(filesDir + '/groupTest2.txt', fs.OpenMode.READ_WRITE);
97          let writeLen = 0;
98          try {
99            writeLen = fs.writeSync(file.fd, '2:inputMethod Try to write str.');
100          } catch (e) {
101            logger.error(`2:fs.writeSync err: ${JSON.stringify(e)}`);
102          }
103          logger.info('2:The length of str is:' + writeLen);
104          // 从文件读取一段内容
105          let arrayBuffer = new ArrayBuffer(1024);
106          let readOptions: ReadOptions = {
107            offset: 0,
108            length: arrayBuffer.byteLength
109          };
110          let readLen = fs.readSync(file.fd, arrayBuffer, readOptions);
111          let buf = buffer.from(arrayBuffer, 0, readLen);
112          logger.info('2:the content of file:' + buf.toString());
113          // 关闭文件
114          fs.closeSync(file);
115        } catch (e) {
116          logger.error(`2:fs.openSync err: ${JSON.stringify(e)}`);
117        }
118      }
119    });
120  }
121
122  onDestroy(): void {
123    keyboardController.onDestroy(); // 销毁窗口并去注册事件监听
124  }
125}