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