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 common from '@ohos.app.ability.common';
16import inputMethod from '@ohos.inputMethod';
17import prompt from '@ohos.promptAction';
18import { BusinessError } from '@ohos.base';
19
20const EXIT_TIME = 1000;
21
22@Entry
23@Component
24struct Index {
25  private arr: string[] = [];
26  private propertyMap: Map<string, inputMethod.InputMethodProperty> = new Map();
27  @StorageLink('inputMethodList') inputMethods: Array<inputMethod.InputMethodProperty> | undefined = [];
28  private TAG = '[InputMethodChooseDialog]';
29
30  aboutToAppear() {
31    console.log(this.TAG, 'dialog page appears')
32    this.inputMethods = AppStorage.get('inputMethodList');
33    if (this.inputMethods) {
34      for (let inputmethod of this.inputMethods) {
35        let name = inputmethod.packageName;
36        this.arr.push(name);
37        this.propertyMap.set(name, inputmethod);
38      }
39    }
40  }
41
42  onPrint(): void {
43    console.log(this.TAG + 'print file or text');
44  }
45
46  onCopy(): void {
47    console.log(this.TAG + 'copy file and html');
48  }
49
50  build() {
51    Column() {
52      List({ space: 1, initialIndex: 0 }) {
53        ListItem() {
54          Text($r('app.string.dialogTitle'))
55            .width('100%')
56            .height(40)
57            .fontSize(14)
58            .textAlign(TextAlign.Center)
59            .backgroundColor(Color.Pink)
60        }.sticky(Sticky.Normal)
61
62        ForEach(this.arr, (item: string, index: number) => {
63          ListItem() {
64            Text(item.split('.').length > 2 ? item.split('.')[2] : item.split('.')[-1])
65              .width('100%')
66              .height(60)
67              .fontSize(16)
68              .textAlign(TextAlign.Center)
69              .borderRadius(10)
70              .backgroundColor($r('app.color.btn_default'))
71              .onClick(async () => {
72                if (this.propertyMap.has(item)) {
73                  let prop = this.propertyMap.get(item);
74                  this.chooseInputMethods(prop);
75                }
76              })
77          }
78          .sticky(0 == index ? Sticky.Opacity : Sticky.None)
79        }, (item: string) => item)
80      }
81      .width('100%')
82      .height('100%')
83    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
84  }
85
86  chooseInputMethods(prop: inputMethod.InputMethodProperty): void {
87    let context = getContext(this) as common.ServiceExtensionContext;
88    inputMethod.switchInputMethod(prop).then(() => {
89      console.log(this.TAG + 'switchInputMethod success');
90      prompt.showToast({
91        message: 'switch success', duration: 200
92      });
93      setTimeout(() => {
94        context?.terminateSelf();
95      }, EXIT_TIME);
96    }).catch((err: BusinessError) => {
97      if (!err) {
98        console.log(this.TAG + 'switchInputMethod failed,' + JSON.stringify(err));
99        prompt.showToast({
100          message: 'switch failed', duration: 200
101        });
102      }
103    });
104  }
105}