1/* 2 * Copyright (c) 2021-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 */ 15 16import window from '@ohos.window'; 17import rpc from '@ohos.rpc'; 18import Log from '../../../../../../../common/src/main/ets/default/Log'; 19 20const TAG = 'Dialog-ServiceExtensionAbility'; 21 22export interface IDialogParameters { 23 bundleName?: string, 24 abilityName?: string, 25 parameters?: { [key: string]: any }, 26} 27 28interface ISystemDialogData { 29 windowName: string, 30 parameters: IDialogParameters, 31 remoteObject?: rpc.RemoteObject 32} 33 34class SystemDialogController { 35 count: number = 0; 36 private data: Map<string, ISystemDialogData> = new Map(); 37 private context; 38 39 constructor(context) { 40 this.context = context; 41 Log.showInfo(TAG, `SystemDialogController constructor ${this.context}`); 42 } 43 44 getContext() { 45 return this.context; 46 } 47 48 getData() { 49 return this.data; 50 } 51 52 getRemoteObject(key: string) { 53 Log.showInfo(TAG, `getRemoteObject start ${key}`); 54 const { remoteObject } = this.data.get(key); 55 56 Log.showDebug(TAG, `getRemoteObject end ${remoteObject}`); 57 return remoteObject; 58 } 59 60 clearRemoteObject(key: string) { 61 Log.showInfo(TAG, `clearRemoteObject start ${key}`); 62 63 const current = this.data.get(key); 64 if (current) { 65 current.remoteObject = undefined; 66 } 67 68 Log.showDebug(TAG, `clearRemoteObject end ${key}`); 69 } 70 71 addDataByKey(key: string, v: ISystemDialogData) { 72 Log.showInfo(TAG, `Controller-addDataByKey start ${key} ${v}`); 73 if (this.data.get(key)) { 74 Object.assign(this.data.get(key), v); 75 } else { 76 this.data.set(key, v); 77 } 78 Log.showDebug(TAG, `Controller-addDataByKey end ${key}`); 79 } 80 81 destroyWindow(key: string, needClear: boolean = true) { 82 const current = this.data.get(key); 83 84 if (!current) { 85 Log.showInfo(TAG, `destroyWindow fail. key:${key} not exist`); 86 return; 87 } 88 89 const { windowName } = current; 90 Log.showInfo(TAG, `destroyWindow start ${key} ${windowName}`); 91 92 if (windowName) { 93 const win = window.findWindow(windowName); 94 win.destroyWindow(); 95 } 96 97 if (!needClear) { 98 Log.showDebug(TAG, `destroyWindow end. not need clear. ${key} ${windowName}`); 99 return; 100 } 101 102 this.clearRemoteObject(key); 103 104 this.data.delete(key); 105 106 if (this.data.size === 0) { 107 this.clear(); 108 } 109 110 Log.showDebug(TAG, `destroyWindow end ${key} ${windowName}`); 111 } 112 113 destroyAllWindow() { 114 Log.showInfo(TAG, `destroyAllWindow start ${this.data.size}`); 115 [...this.data.keys()].forEach(key => { 116 Log.showInfo(TAG, `destroyAllWindow forEach ${key}`); 117 this.destroyWindow(key); 118 }) 119 Log.showDebug(TAG, `destroyAllWindow end`); 120 } 121 122 clear() { 123 Log.showInfo(TAG, `clear start`); 124 this.data.clear(); 125 this.context.terminateSelf(); 126 this.context = null; 127 Log.showDebug(TAG, `clear end`); 128 } 129} 130 131export default SystemDialogController; 132