| danielwbhuang | f43c358 | 2022-12-29 20:28:44 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings.inputmethod; |
| 18 | |
| 19 | import android.content.Context; |
| danielwbhuang | 1f32614 | 2023-02-24 18:16:30 +0800 | [diff] [blame] | 20 | import android.hardware.input.InputDeviceIdentifier; |
| 21 | import android.hardware.input.InputManager; |
| danielwbhuang | cfd02f5 | 2023-02-04 23:16:16 +0800 | [diff] [blame] | 22 | import android.view.InputDevice; |
| danielwbhuang | f43c358 | 2022-12-29 20:28:44 +0800 | [diff] [blame] | 23 | import android.view.inputmethod.InputMethodInfo; |
| 24 | import android.view.inputmethod.InputMethodManager; |
| 25 | import android.view.inputmethod.InputMethodSubtype; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.List; |
| 29 | |
| 30 | /** |
| 31 | * Utilities of keyboard settings |
| 32 | */ |
| 33 | public class NewKeyboardSettingsUtils { |
| 34 | |
| danielwbhuang | cc74915 | 2023-03-24 19:11:53 +0800 | [diff] [blame^] | 35 | /** |
| 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 | |
| danielwbhuang | f43c358 | 2022-12-29 20:28:44 +0800 | [diff] [blame] | 41 | 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 | |
| danielwbhuang | cfd02f5 | 2023-02-04 23:16:16 +0800 | [diff] [blame] | 48 | 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; |
| danielwbhuang | f43c358 | 2022-12-29 20:28:44 +0800 | [diff] [blame] | 57 | } |
| 58 | } |
| danielwbhuang | cfd02f5 | 2023-02-04 23:16:16 +0800 | [diff] [blame] | 59 | return false; |
| danielwbhuang | f43c358 | 2022-12-29 20:28:44 +0800 | [diff] [blame] | 60 | } |
| 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 { |
| danielwbhuang | a6b78f6 | 2023-03-07 21:43:33 +0800 | [diff] [blame] | 80 | CharSequence mSubtypeLabel; |
| danielwbhuang | f43c358 | 2022-12-29 20:28:44 +0800 | [diff] [blame] | 81 | String mLayout; |
| 82 | InputMethodInfo mInputMethodInfo; |
| 83 | InputMethodSubtype mInputMethodSubtype; |
| 84 | |
| 85 | KeyboardInfo( |
| danielwbhuang | a6b78f6 | 2023-03-07 21:43:33 +0800 | [diff] [blame] | 86 | CharSequence subtypeLabel, |
| danielwbhuang | f43c358 | 2022-12-29 20:28:44 +0800 | [diff] [blame] | 87 | String layout, |
| 88 | InputMethodInfo inputMethodInfo, |
| 89 | InputMethodSubtype inputMethodSubtype) { |
| danielwbhuang | a6b78f6 | 2023-03-07 21:43:33 +0800 | [diff] [blame] | 90 | mSubtypeLabel = subtypeLabel; |
| danielwbhuang | f43c358 | 2022-12-29 20:28:44 +0800 | [diff] [blame] | 91 | mLayout = layout; |
| 92 | mInputMethodInfo = inputMethodInfo; |
| 93 | mInputMethodSubtype = inputMethodSubtype; |
| 94 | } |
| 95 | |
| danielwbhuang | a6b78f6 | 2023-03-07 21:43:33 +0800 | [diff] [blame] | 96 | String getPrefId() { |
| 97 | return mInputMethodInfo.getId() + "_" + mInputMethodSubtype.hashCode(); |
| 98 | } |
| 99 | |
| 100 | CharSequence getSubtypeLabel() { |
| 101 | return mSubtypeLabel; |
| danielwbhuang | f43c358 | 2022-12-29 20:28:44 +0800 | [diff] [blame] | 102 | } |
| 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 | } |
| danielwbhuang | 1f32614 | 2023-02-24 18:16:30 +0800 | [diff] [blame] | 116 | |
| 117 | static InputDevice getInputDevice(InputManager im, InputDeviceIdentifier identifier) { |
| 118 | return im.getInputDeviceByDescriptor(identifier.getDescriptor()); |
| 119 | } |
| danielwbhuang | f43c358 | 2022-12-29 20:28:44 +0800 | [diff] [blame] | 120 | } |