• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 ShareExtensionAbility from '@ohos.app.ability.ShareExtensionAbility';
17import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
18import Want from '@ohos.app.ability.Want';
19import fs from '@ohos.file.fs';
20
21interface UIExtensionStorage {
22    session: UIExtensionContentSession,
23}
24
25const TAG: string = 'BluetoothServiceAbility: ';
26
27export default class BluetoothServiceAbility extends ShareExtensionAbility {
28    private uris: Array<string> = [];
29    onCreate(): void {
30        console.log(`${TAG} onCreate is called`);
31    }
32
33    onDestroy(): void {
34        console.log(`${TAG} onDestroy is called`);
35    }
36
37    public async onSessionCreate(want: Want, session: UIExtensionContentSession): Promise<void> {
38        console.log(`${TAG} onSessionCreate is called`);
39        let systemShareModule = loadNativeModule('@hms:collaboration.systemShare') as ESObject;
40        try {
41            let data = await systemShareModule.getSharedData(want);
42            let records = data.getRecords();
43            for (let i = 0; i < records.length; i++) {
44                let filePath = '';
45                if (records[i].uri == undefined && records[i].content != undefined) {
46                    console.log(`${TAG} share txt content`);
47                    filePath = this.context.filesDir + '/' + (i + 1) + '.txt';
48                    let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
49                    let writeLen = fs.writeSync(file.fd, records[i].content);
50                    console.log(`${TAG} write data to file success and size is:` + writeLen);
51                    fs.closeSync(file);
52                } else if (records[i].uri == undefined) {
53                    console.log(`${TAG} records[i].uri is undefined`);
54                } else {
55                    filePath = records[i].uri;
56                }
57                this.uris.push(filePath);
58            }
59            AppStorage.setOrCreate('sendFileUris', this.uris);
60            AppStorage.setOrCreate('sendFileUrisLength', this.uris.length);
61        } catch (err) {
62            console.log(`${TAG} records fail`);
63        }
64        let localStorage: LocalStorage = new LocalStorage({
65            session: session
66        } as UIExtensionStorage);
67        try {
68            session.loadContent('pages/BluetoothShare', localStorage);
69            session.setWindowBackgroundColor('#00000000');
70        } catch (err) {
71            console.log(`${TAG} loadContent fail`);
72        }
73    }
74
75    onSessionDestroy(): void {
76        console.log(`${TAG} onSessionDestroy is called`);
77    }
78
79    onForeground(): void {
80        console.log(`${TAG} onForeground is called`);
81    }
82
83    onBackground(): void {
84        console.log(`${TAG} onBackground is called`);
85    }
86}