• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
20 import android.hardware.input.InputDeviceIdentifier;
21 import android.hardware.input.InputManager;
22 import android.hardware.input.KeyboardLayout;
23 import android.view.InputDevice;
24 import android.view.inputmethod.InputMethodInfo;
25 import android.view.inputmethod.InputMethodManager;
26 import android.view.inputmethod.InputMethodSubtype;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 /**
32  * Utilities of keyboard settings
33  */
34 public class NewKeyboardSettingsUtils {
35 
36     static final String EXTRA_TITLE = "keyboard_layout_picker_title";
37     static final String EXTRA_USER_ID = "user_id";
38     static final String EXTRA_INPUT_DEVICE_IDENTIFIER = "input_device_identifier";
39     static final String EXTRA_INPUT_METHOD_INFO = "input_method_info";
40     static final String EXTRA_INPUT_METHOD_SUBTYPE = "input_method_subtype";
41 
isTouchpad()42     static boolean isTouchpad() {
43         for (int deviceId : InputDevice.getDeviceIds()) {
44             final InputDevice device = InputDevice.getDevice(deviceId);
45             if (device == null) {
46                 continue;
47             }
48             if ((device.getSources() & InputDevice.SOURCE_TOUCHPAD)
49                     == InputDevice.SOURCE_TOUCHPAD) {
50                 return true;
51             }
52         }
53         return false;
54     }
55 
getSuitableImeLabels(Context context, InputMethodManager imm, int userId)56     static List<String> getSuitableImeLabels(Context context, InputMethodManager imm, int userId) {
57         List<String> suitableInputMethodInfoLabels = new ArrayList<>();
58         List<InputMethodInfo> infoList = imm.getEnabledInputMethodListAsUser(userId);
59         for (InputMethodInfo info : infoList) {
60             List<InputMethodSubtype> subtypes =
61                     imm.getEnabledInputMethodSubtypeList(info, true);
62             for (InputMethodSubtype subtype : subtypes) {
63                 if (subtype.isSuitableForPhysicalKeyboardLayoutMapping()) {
64                     suitableInputMethodInfoLabels.add(
65                             info.loadLabel(context.getPackageManager()).toString());
66                     break;
67                 }
68             }
69         }
70         return suitableInputMethodInfoLabels;
71     }
72 
73     static class KeyboardInfo {
74         CharSequence mSubtypeLabel;
75         String mLayout;
76         InputMethodInfo mInputMethodInfo;
77         InputMethodSubtype mInputMethodSubtype;
78 
KeyboardInfo( CharSequence subtypeLabel, String layout, InputMethodInfo inputMethodInfo, InputMethodSubtype inputMethodSubtype)79         KeyboardInfo(
80                 CharSequence subtypeLabel,
81                 String layout,
82                 InputMethodInfo inputMethodInfo,
83                 InputMethodSubtype inputMethodSubtype) {
84             mSubtypeLabel = subtypeLabel;
85             mLayout = layout;
86             mInputMethodInfo = inputMethodInfo;
87             mInputMethodSubtype = inputMethodSubtype;
88         }
89 
getPrefId()90         String getPrefId() {
91             return mInputMethodInfo.getId() + "_" + mInputMethodSubtype.hashCode();
92         }
93 
getSubtypeLabel()94         CharSequence getSubtypeLabel() {
95             return mSubtypeLabel;
96         }
97 
getLayout()98         String getLayout() {
99             return mLayout;
100         }
101 
getInputMethodInfo()102         InputMethodInfo getInputMethodInfo() {
103             return mInputMethodInfo;
104         }
105 
getInputMethodSubtype()106         InputMethodSubtype getInputMethodSubtype() {
107             return mInputMethodSubtype;
108         }
109     }
110 
getInputDevice(InputManager im, InputDeviceIdentifier identifier)111     static InputDevice getInputDevice(InputManager im, InputDeviceIdentifier identifier) {
112         return im.getInputDeviceByDescriptor(identifier.getDescriptor());
113     }
114 
getKeyboardLayouts(InputManager inputManager, int userId, InputDeviceIdentifier identifier, InputMethodInfo info, InputMethodSubtype subtype)115     static KeyboardLayout[] getKeyboardLayouts(InputManager inputManager, int userId,
116             InputDeviceIdentifier identifier, InputMethodInfo info, InputMethodSubtype subtype) {
117         return inputManager.getKeyboardLayoutListForInputDevice(identifier, userId, info, subtype);
118     }
119 
getKeyboardLayout(InputManager inputManager, int userId, InputDeviceIdentifier identifier, InputMethodInfo info, InputMethodSubtype subtype)120     static String getKeyboardLayout(InputManager inputManager, int userId,
121             InputDeviceIdentifier identifier, InputMethodInfo info, InputMethodSubtype subtype) {
122         return inputManager.getKeyboardLayoutForInputDevice(identifier, userId, info, subtype);
123     }
124 }
125