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