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 UIExtensionAbility from '@ohos.app.ability.UIExtensionAbility'; 17import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 18import Want from '@ohos.app.ability.Want'; 19import type { BusinessError } from '@ohos.base'; 20 21const TAG: string = '[BT_RECEIVE_UI]==>' 22 23export default class BluetoothReceiveServiceUIAbility extends UIExtensionAbility { 24 onSessionCreate(want: Want, session: UIExtensionContentSession) { 25 if (want.parameters == undefined) { 26 return; 27 } 28 29 30 let fileUriType: string = want.parameters?.['fileUriType'] as string; 31 if (fileUriType != undefined && fileUriType != '') { 32 this.startAbility(fileUriType, want); 33 return; 34 } 35 36 let fileName: string = want.parameters?.['fileName'] as string; 37 if (fileName != undefined && fileName != '') { 38 AppStorage.setOrCreate('fileName', fileName); 39 } 40 41 let param: Record<string, UIExtensionContentSession> = { 42 'session': session 43 } 44 let storage: LocalStorage = new LocalStorage(param); 45 session.loadContent('pages/BluetoothReceive', storage); 46 session.setWindowBackgroundColor('#00000000'); 47 AppStorage.setOrCreate('ConfirmSession', session); 48 } 49 50 onSessionDestroy(session: UIExtensionContentSession) { 51 console.info(TAG, `BluetoothReceiveServiceUIAbility onSessionDestroy`); 52 } 53 54 startAbility(fileType: string, want: Want) { 55 console.info(TAG, `startAbility begin.`); 56 let bundleName: string = want.parameters?.['bundleName'] as string; 57 let abilityName: string = want.parameters?.['abilityName'] as string; 58 let fileUri: string = want.parameters?.['fileUri'] as string; 59 if (bundleName == undefined || abilityName == undefined || fileUri == undefined) { 60 console.error(TAG, `undefined params`); 61 return; 62 } 63 switch (fileType) { 64 case 'media': 65 this.startMediaAbility(fileUri, bundleName, abilityName); 66 break; 67 case 'files': 68 this.startFileManagerAbility(fileUri, bundleName, abilityName); 69 break; 70 default: 71 console.error(TAG, `invalid fileType to start ability`); 72 this.context.terminateSelf(); 73 break; 74 } 75 console.info(TAG, `startUpAbility end.`); 76 return; 77 } 78 79 startFileManagerAbility(fileUri: string, bundleName: string, abilityName: string) { 80 console.info(TAG, `startFileManagerAbility bundleName ` + bundleName + ` abilityName ` + abilityName); 81 let want: Want = { 82 bundleName: bundleName, 83 abilityName: abilityName, 84 parameters: { 85 'fileUri': fileUri 86 } 87 }; 88 this.startWantAbility(want); 89 } 90 91 startMediaAbility(fileUri: string, bundleName: string, abilityName: string) { 92 console.info(TAG, `startMediaAbility bundleName ` + bundleName + ` abilityName ` + abilityName); 93 let want: Want = { 94 bundleName: bundleName, 95 abilityName: abilityName, 96 action: 'ohos.want.action.viewData', 97 parameters: { 98 'uri': fileUri 99 } 100 }; 101 this.startWantAbility(want); 102 } 103 104 startWantAbility(want: Want) { 105 this.context.startAbility(want).then(() => { 106 console.info(TAG, `startAbility successfully and terminateSelf`); 107 this.context.terminateSelf(); 108 }).catch((err: BusinessError) => { 109 console.error(TAG, `startAbility failed, code is ${err.code}, message is ${err.message}`); 110 this.context.terminateSelf(); 111 }); 112 } 113}