| /* | |
| * Copyright (C) 2026 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| package com.android.settings.inputmethod | |
| import android.view.inputmethod.InputMethodManager | |
| import com.android.settings.R | |
| import com.android.settings.flags.Flags | |
| import com.android.settingslib.metadata.ProvidePreferenceScreen | |
| import com.android.settingslib.metadata.preferencesapi.PreferencesApiScreen | |
| import com.android.settingslib.metadata.preferencesapi.Stability | |
| import com.android.settingslib.metadata.preferencesapi.category.Category | |
| import com.android.settingslib.metadata.preferencesapi.preconditions.Allowed | |
| import com.android.settingslib.metadata.preferencesapi.preconditions.Custom | |
| import com.android.settingslib.metadata.preferencesapi.safe | |
| import com.android.settingslib.metadata.preferencesapi.types.GeneratedParameterType | |
| import com.android.settingslib.metadata.preferencesapi.types.GeneratedValue | |
| import com.android.settingslib.metadata.preferencesapi.unsafe | |
| // LINT.IfChange | |
| @ProvidePreferenceScreen(NewKeyboardLayoutPickerApiScreen.KEY, parameterized = true) | |
| class NewKeyboardLayoutPickerApiScreen : | |
| PreferencesApiScreen( | |
| key = KEY, | |
| topLevelSettingsCategory = Category.SYSTEM, | |
| fragment = NewKeyboardLayoutPickerFragment::class, | |
| purpose = R.string.keyboard_layout_picker_screen_purpose, | |
| ) { | |
| init { | |
| flag { Flags.catalystMigration26q3() } | |
| parameters { | |
| parameter( | |
| name = PARAM_KEYBOARD_DESCRIPTOR, | |
| purpose = R.string.keyboard_layout_picker_device_descriptor_param_purpose, | |
| required = true, | |
| type = | |
| GeneratedParameterType( | |
| R.string.keyboard_layout_picker_device_descriptor_param_description, | |
| key = "InputDeviceDescriptor", | |
| ) { | |
| PhysicalKeyboardFragment.getHardKeyboards(context).map { | |
| GeneratedValue( | |
| value = it.mDeviceIdentifier.descriptor.safe(), | |
| description = it.mDeviceName.unsafe(), | |
| ) | |
| } | |
| }, | |
| ) | |
| prepareScreenExtras { extras -> | |
| val descriptor = parameters[PARAM_KEYBOARD_DESCRIPTOR] | |
| val keyboards = PhysicalKeyboardFragment.getHardKeyboards(context) | |
| val targetKeyboard = keyboards.find { | |
| it.mDeviceIdentifier.descriptor == descriptor | |
| } | |
| if (targetKeyboard != null) { | |
| extras.putParcelable( | |
| InputPeripheralsSettingsUtils.EXTRA_INPUT_DEVICE_IDENTIFIER, | |
| targetKeyboard.mDeviceIdentifier, | |
| ) | |
| extras.putCharSequence( | |
| InputPeripheralsSettingsUtils.EXTRA_TITLE, | |
| targetKeyboard.mDeviceName, | |
| ) | |
| } | |
| val imm = context.getSystemService(InputMethodManager::class.java) | |
| if (imm != null) { | |
| var imeInfo = imm.getCurrentInputMethodInfoAsUser(userHandle) | |
| var subtype = imm.getCurrentInputMethodSubtype() | |
| if (imeInfo == null) { | |
| val infoList = imm.getEnabledInputMethodListAsUser(userHandle) | |
| if (infoList.isNotEmpty()) { | |
| imeInfo = infoList[0] | |
| val subtypes = | |
| imm.getEnabledInputMethodSubtypeListAsUser( | |
| imeInfo.id, | |
| true, | |
| userHandle, | |
| ) | |
| if (subtypes.isNotEmpty()) { | |
| subtype = subtypes[0] | |
| } | |
| } | |
| } | |
| if (imeInfo != null) { | |
| extras.putParcelable( | |
| InputPeripheralsSettingsUtils.EXTRA_INPUT_METHOD_INFO, | |
| imeInfo, | |
| ) | |
| extras.putParcelable( | |
| InputPeripheralsSettingsUtils.EXTRA_INPUT_METHOD_SUBTYPE, | |
| subtype, | |
| ) | |
| extras.putInt(InputPeripheralsSettingsUtils.EXTRA_USER_ID, userId) | |
| } | |
| } | |
| } | |
| } | |
| precondition(R.string.physical_keyboard_layout_picker_precondition) { | |
| val descriptor = parameters[PARAM_KEYBOARD_DESCRIPTOR] | |
| val keyboards = PhysicalKeyboardFragment.getHardKeyboards(context) | |
| if ( | |
| keyboards.isEmpty() || | |
| keyboards.find { it.mDeviceIdentifier.descriptor == descriptor } == null | |
| ) { | |
| Custom(R.string.physical_keyboard_layout_picker_unsupported, Stability.UNSTABLE) | |
| } else { | |
| Allowed | |
| } | |
| } | |
| } | |
| companion object { | |
| const val KEY = "keyboard_layout_picker_list" | |
| const val PARAM_KEYBOARD_DESCRIPTOR = "keyboardDescriptor" | |
| } | |
| } | |
| // LINT.ThenChange(NewKeyboardLayoutPickerFragment.java) |