blob: fe0c9d4f977c1c736c4c05d9bd89df44da0353bd [file] [log] [blame]
danielwbhuangf43c3582022-12-29 20:28:44 +08001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings.inputmethod;
18
19import android.content.Context;
danielwbhuang1f326142023-02-24 18:16:30 +080020import android.hardware.input.InputDeviceIdentifier;
21import android.hardware.input.InputManager;
danielwbhuangcfd02f52023-02-04 23:16:16 +080022import android.view.InputDevice;
danielwbhuangf43c3582022-12-29 20:28:44 +080023import android.view.inputmethod.InputMethodInfo;
24import android.view.inputmethod.InputMethodManager;
25import android.view.inputmethod.InputMethodSubtype;
26
27import java.util.ArrayList;
28import java.util.List;
29
30/**
31 * Utilities of keyboard settings
32 */
33public class NewKeyboardSettingsUtils {
34
danielwbhuangcc749152023-03-24 19:11:53 +080035 /**
36 * Record the class name of the intent sender for metrics.
37 */
38 public static final String EXTRA_INTENT_FROM =
39 "com.android.settings.inputmethod.EXTRA_INTENT_FROM";
40
danielwbhuangf43c3582022-12-29 20:28:44 +080041 static final String EXTRA_TITLE = "keyboard_layout_picker_title";
42 static final String EXTRA_KEYBOARD_LAYOUT = "keyboard_layout";
43 static final String EXTRA_USER_ID = "user_id";
44 static final String EXTRA_INPUT_DEVICE_IDENTIFIER = "input_device_identifier";
45 static final String EXTRA_INPUT_METHOD_INFO = "input_method_info";
46 static final String EXTRA_INPUT_METHOD_SUBTYPE = "input_method_subtype";
47
danielwbhuangcfd02f52023-02-04 23:16:16 +080048 static boolean isTouchpad() {
49 for (int deviceId : InputDevice.getDeviceIds()) {
50 final InputDevice device = InputDevice.getDevice(deviceId);
51 if (device == null) {
52 continue;
53 }
54 if ((device.getSources() & InputDevice.SOURCE_TOUCHPAD)
55 == InputDevice.SOURCE_TOUCHPAD) {
56 return true;
danielwbhuangf43c3582022-12-29 20:28:44 +080057 }
58 }
danielwbhuangcfd02f52023-02-04 23:16:16 +080059 return false;
danielwbhuangf43c3582022-12-29 20:28:44 +080060 }
61
62 static List<String> getSuitableImeLabels(Context context, InputMethodManager imm, int userId) {
63 List<String> suitableInputMethodInfoLabels = new ArrayList<>();
64 List<InputMethodInfo> infoList = imm.getEnabledInputMethodListAsUser(userId);
65 for (InputMethodInfo info : infoList) {
66 List<InputMethodSubtype> subtypes =
67 imm.getEnabledInputMethodSubtypeList(info, true);
68 for (InputMethodSubtype subtype : subtypes) {
69 if (subtype.isSuitableForPhysicalKeyboardLayoutMapping()) {
70 suitableInputMethodInfoLabels.add(
71 info.loadLabel(context.getPackageManager()).toString());
72 break;
73 }
74 }
75 }
76 return suitableInputMethodInfoLabels;
77 }
78
79 static class KeyboardInfo {
danielwbhuanga6b78f62023-03-07 21:43:33 +080080 CharSequence mSubtypeLabel;
danielwbhuangf43c3582022-12-29 20:28:44 +080081 String mLayout;
82 InputMethodInfo mInputMethodInfo;
83 InputMethodSubtype mInputMethodSubtype;
84
85 KeyboardInfo(
danielwbhuanga6b78f62023-03-07 21:43:33 +080086 CharSequence subtypeLabel,
danielwbhuangf43c3582022-12-29 20:28:44 +080087 String layout,
88 InputMethodInfo inputMethodInfo,
89 InputMethodSubtype inputMethodSubtype) {
danielwbhuanga6b78f62023-03-07 21:43:33 +080090 mSubtypeLabel = subtypeLabel;
danielwbhuangf43c3582022-12-29 20:28:44 +080091 mLayout = layout;
92 mInputMethodInfo = inputMethodInfo;
93 mInputMethodSubtype = inputMethodSubtype;
94 }
95
danielwbhuanga6b78f62023-03-07 21:43:33 +080096 String getPrefId() {
97 return mInputMethodInfo.getId() + "_" + mInputMethodSubtype.hashCode();
98 }
99
100 CharSequence getSubtypeLabel() {
101 return mSubtypeLabel;
danielwbhuangf43c3582022-12-29 20:28:44 +0800102 }
103
104 String getLayout() {
105 return mLayout;
106 }
107
108 InputMethodInfo getInputMethodInfo() {
109 return mInputMethodInfo;
110 }
111
112 InputMethodSubtype getInputMethodSubtype() {
113 return mInputMethodSubtype;
114 }
115 }
danielwbhuang1f326142023-02-24 18:16:30 +0800116
117 static InputDevice getInputDevice(InputManager im, InputDeviceIdentifier identifier) {
118 return im.getInputDeviceByDescriptor(identifier.getDescriptor());
119 }
danielwbhuangf43c3582022-12-29 20:28:44 +0800120}