• 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 common from '@ohos.app.ability.common';
17import window from '@ohos.window';
18import inputMethod from '@ohos.inputMethod';
19import commonEvent from '@ohos.commonEventManager';
20import Want from '@ohos.app.ability.Want';
21import { BusinessError } from '@ohos.base';
22
23let TAG: string = '[InputMethodChooseDialog]';
24let PACKAGE_ADDED: string = 'usual.event.PACKAGE_ADDED';
25let PACKAGE_REMOVED: string = 'usual.event.PACKAGE_REMOVED';
26let subscribeInfo: commonEvent.CommonEventSubscribeInfo = {
27  events: [PACKAGE_ADDED, PACKAGE_REMOVED]
28};
29
30interface DialogRect {
31  left: number;
32  top: number;
33  width: number;
34  height: number;
35}
36
37export default class ServiceExtAbility extends ServiceExtensionAbility {
38  private extensionWin: window.Window | undefined = undefined;
39  private mContext: common.ServiceExtensionContext | undefined = undefined;
40  private windowNum: number = 0;
41
42  onCreate(want: Want): void {
43    console.log(TAG, 'onCreate');
44    this.windowNum = 0;
45    this.mContext = this.context;
46  }
47
48  onRequest(want: Want, startId: number): void {
49    console.log(TAG, 'onRequest execute');
50    let dialogRect: DialogRect = {
51      left: 50,
52      top: 900,
53      width: 300,
54      height: 300,
55    };
56    let windowConfig: window.Configuration = {
57      name: 'inputmethod Dialog',
58      windowType: window.WindowType.TYPE_FLOAT,
59      ctx: this.mContext
60    };
61    this.getInputMethods().then(() => {
62      this.createWindow(windowConfig, dialogRect);
63    });
64
65    commonEvent.createSubscriber(subscribeInfo, (error: BusinessError, subcriber: commonEvent.CommonEventSubscriber) => {
66      commonEvent.subscribe(subcriber, (error: BusinessError, commonEventData: commonEvent.CommonEventData) => {
67        console.log(TAG + 'commonEvent:' + JSON.stringify(commonEventData.event));
68        if (commonEventData.event === PACKAGE_ADDED || commonEventData.event === PACKAGE_REMOVED) {
69          this.updateImeList();
70        }
71      });
72    });
73  }
74
75  onDestroy(): void {
76    console.log(TAG + 'ServiceExtAbility destroyed');
77    this.releaseContext();
78  }
79
80  private async createWindow(config: window.Configuration, rect: DialogRect): Promise<void> {
81    console.log(TAG + 'createWindow execute');
82    try {
83      if (this.windowNum > 0) {
84        this.updateImeList();
85        return;
86      }
87      try {
88        this.extensionWin = await window.createWindow(config);
89        console.info(TAG + 'Succeeded in creating the window. Data: ' + JSON.stringify(this.extensionWin));
90        this.extensionWin.on('windowEvent', async (data: window.WindowEventType) => {
91          console.log(TAG + 'windowEvent:' + JSON.stringify(data));
92          if (data === window.WindowEventType.WINDOW_INACTIVE) {
93            await this.releaseContext();
94          }
95        });
96        await this.extensionWin.moveWindowTo(rect.left, rect.top);
97        await this.extensionWin.resize(rect.width, rect.height);
98        await this.extensionWin.setUIContent('pages/index');
99        await this.extensionWin.showWindow();
100        this.windowNum++;
101        console.log(TAG + 'window create successfully');
102      } catch (exception) {
103        console.error('Failed to create the window. Cause: ' + JSON.stringify(exception));
104      }
105    } catch {
106      console.info(TAG + 'window create failed');
107    }
108  }
109
110  private async getInputMethods(): Promise<void> {
111    let inputMethodList: Array<inputMethod.InputMethodProperty> = [];
112    try {
113      let enableList = await inputMethod.getSetting().getInputMethods(true);
114      let disableList = await inputMethod.getSetting().getInputMethods(false);
115      inputMethodList = enableList.concat(disableList);
116      AppStorage.setOrCreate('inputMethodList', inputMethodList);
117    } catch {
118      console.log(TAG + 'getInputMethods failed');
119    }
120  }
121
122  private async updateImeList(): Promise<void> {
123    await this.getInputMethods().then(async () => {
124      if (this.extensionWin) {
125        await this.extensionWin.setUIContent('pages/index');
126        if (!this.extensionWin.isWindowShowing()) {
127          await this.extensionWin.showWindow();
128        }
129      }
130    });
131  }
132
133  public async releaseContext(): Promise<void> {
134    if (this.mContext && this.extensionWin) {
135      await this.extensionWin.destroyWindow();
136      await this.mContext.terminateSelf();
137    }
138  }
139};