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 */ 15import rpc from '@ohos.rpc'; 16import hilog from '@ohos.hilog'; 17import window from '@ohos.window'; 18import display from '@ohos.display'; 19import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 20import type Want from '@ohos.application.Want'; 21import GlobalContext, { GlobalExtensionWindow } from './GlobalParam'; 22 23interface IRect { 24 left: number; 25 top: number; 26 width: number; 27 height: number; 28} 29 30const DISTANCE_NUMBER = 68; 31const TAG = 'ToastExtensionAbility'; 32 33class ToastStub extends rpc.RemoteObject { 34 constructor(des: string) { 35 super(des); 36 } 37} 38 39export class ToastInfo { 40 public fromAppName: string = ''; 41 public toAppName: string = ''; 42 public displayHeight: number = 0; 43 private static toastInfo: ToastInfo; 44 45 public static getInstance(): ToastInfo { 46 if (ToastInfo.toastInfo == null) { 47 ToastInfo.toastInfo = new ToastInfo(); 48 } 49 50 return ToastInfo.toastInfo; 51 } 52} 53 54export default class ToastExtensionAbility extends ServiceExtensionAbility { 55 onCreate(want: Want): void { 56 hilog.info(0, TAG, 'onCreate'); 57 GlobalContext.getInstance().context = this.context; 58 } 59 60 onConnect(want: Want): ToastStub { 61 hilog.info(0, TAG, 'onConnect'); 62 display 63 .getDefaultDisplay() 64 .then((display: display.Display) => { 65 const toastRect = { 66 left: 0, 67 top: 0, 68 width: display.width, 69 height: display.height, 70 }; 71 ToastInfo.getInstance().fromAppName = want.parameters.fromAppName; 72 ToastInfo.getInstance().toAppName = want.parameters.toAppName; 73 ToastInfo.getInstance().displayHeight = display.height / display.densityPixels - DISTANCE_NUMBER; 74 this.createToastWindow('PasteboardToast' + new Date().getTime(), toastRect); 75 }) 76 .catch((err) => { 77 hilog.info(0, TAG, 'getDefaultDisplay err: ' + JSON.stringify(err)); 78 }); 79 return new ToastStub('PasteboardToast'); 80 } 81 82 onRequest(want: Want, startId: number): void { 83 hilog.info(0, TAG, 'onRequest'); 84 this.onConnect(want); 85 } 86 87 onDisconnet(): void { 88 hilog.info(0, TAG, 'onDisconnet'); 89 this.onDestroy(); 90 } 91 92 onDestroy(): void { 93 hilog.info(0, TAG, 'onDestroy'); 94 GlobalExtensionWindow.getInstance().extensionWin.destroyWindow(); 95 GlobalContext.getInstance().context.terminateSelf(); 96 } 97 98 private async createToastWindow(name: string, rect: IRect): Promise<void> { 99 hilog.info(0, TAG, 'create toast begin'); 100 101 if (globalThis.windowNum > 0) { 102 globalThis.windowNum = 0; 103 this.onDestroy(); 104 } 105 let windowClass = null; 106 let config = { 107 name, 108 windowType: window.WindowType.TYPE_FLOAT, 109 ctx: this.context, 110 }; 111 try { 112 window.createWindow(config, (err, data) => { 113 if (err.code) { 114 hilog.error(0, TAG, 'Failed to create the window. Cause: ' + JSON.stringify(err)); 115 return; 116 } 117 windowClass = data; 118 GlobalExtensionWindow.getInstance().extensionWin = data; 119 hilog.info(0, TAG, 'Succeeded in creating the window. Data: ' + JSON.stringify(data)); 120 try { 121 windowClass.setUIContent('pages/index', (err) => { 122 if (err.code) { 123 hilog.error(0, TAG, 'Failed to load the content. Cause:' + JSON.stringify(err)); 124 return; 125 } 126 windowClass.moveWindowTo(rect.left, rect.top); 127 windowClass.resize(rect.width, rect.height); 128 windowClass.setBackgroundColor('#00000000'); 129 windowClass.setWindowTouchable(false); 130 windowClass.showWindow(); 131 globalThis.windowNum++; 132 hilog.info(0, TAG, 'Create window successfully'); 133 }); 134 } catch (exception) { 135 hilog.error(0, TAG, 'Failed to load the content. Cause:' + JSON.stringify(exception)); 136 } 137 }); 138 } catch (exception) { 139 hilog.error(0, TAG, 'Failed to create the window. Cause: ' + JSON.stringify(exception)); 140 } 141 } 142} 143