1/* 2 * Copyright (c) 2022 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 ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 16import window from '@ohos.window'; 17import display from '@ohos.display'; 18import inputMethod from '@ohos.inputMethod'; 19import prompt from '@ohos.prompt'; 20import type Want from './@ohos.app.ability.Want'; 21 22let TAG = '[InputMethodChooseDialog]'; 23 24export default class ServiceExtAbility extends ServiceExtensionAbility { 25 onCreate(want): void { 26 console.log(TAG, 'onCreate'); 27 globalThis.windowNum = 0; 28 } 29 30 onRequest(want: Want, startId: number): void { 31 console.log(TAG, 'onRequest execute'); 32 globalThis.abilityWant = want; 33 display.getDefaultDisplay().then(() => { 34 let dialogRect = { 35 left: 50, 36 top: 900, 37 width: 300, 38 height: 300, 39 }; 40 this.getInputMethods().then(() => { 41 this.createWindow('inputmethod Dialog: ' + startId, window.WindowType.TYPE_DIALOG, dialogRect); 42 }); 43 }).catch((err) => { 44 console.log(TAG + 'getDefaultDisplay err: ' + JSON.stringify(err)); 45 }); 46 47 globalThis.chooseInputMethods = ((prop: inputMethod.InputMethodProperty): void => { 48 inputMethod.switchInputMethod(prop).then((err) => { 49 if (!err) { 50 console.log(TAG + 'switchInputMethod failed, ' + JSON.stringify(err)); 51 prompt.showToast({ 52 message: 'switch failed', duration: 200 53 }); 54 } else { 55 console.log(TAG + 'switchInputMethod success.'); 56 prompt.showToast({ 57 message: 'switch success', duration: 200 58 }); 59 } 60 }) 61 }) 62 } 63 64 onDestroy(): void { 65 console.log(TAG + 'ServiceExtAbility destroyed.'); 66 globalThis.extensionWin.destroy(); 67 globalThis.context.terminateSelf(); 68 } 69 70 private async createWindow(name: string, windowType: number, rect): Promise<void> { 71 console.log(TAG + 'createWindow execute.'); 72 try { 73 if (globalThis.windowNum > 0) { 74 globalThis.windowNum = 0; 75 globalThis.extensionWin.destroy(); 76 globalThis.context.terminateSelf(); 77 } 78 const win = await window.create(this.context, name, windowType); 79 globalThis.extensionWin = win; 80 globalThis.context = this.context; 81 await win.moveTo(rect.left, rect.top); 82 await win.resetSize(rect.width, rect.height); 83 await win.loadContent('pages/index'); 84 await win.show(); 85 globalThis.windowNum++; 86 console.log(TAG + 'window create successfully.'); 87 } catch { 88 console.info(TAG + 'window create failed.'); 89 } 90 } 91 92 private async getInputMethods(): Promise<void> { 93 globalThis.inputMethodList = []; 94 try { 95 let enableList = await inputMethod.getInputMethodSetting().listInputMethod(true); 96 let disableList = await inputMethod.getInputMethodSetting().listInputMethod(false); 97 globalThis.inputMethodList = [...enableList, ...disableList]; 98 } catch { 99 console.log(TAG + 'getInputMethods failed.'); 100 } 101 } 102};