1 /* 2 * Copyright (C) 2018 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 20 import android.content.Context; 21 import android.hardware.input.InputDeviceIdentifier; 22 import android.hardware.input.InputManager; 23 import android.hardware.input.KeyboardLayout; 24 import android.view.InputDevice; 25 26 import androidx.fragment.app.Fragment; 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceScreen; 29 import androidx.preference.SwitchPreference; 30 31 import com.android.settings.core.BasePreferenceController; 32 import com.android.settingslib.core.lifecycle.LifecycleObserver; 33 import com.android.settingslib.core.lifecycle.events.OnStart; 34 import com.android.settingslib.core.lifecycle.events.OnStop; 35 36 import java.util.Arrays; 37 import java.util.HashMap; 38 import java.util.Map; 39 40 41 public class KeyboardLayoutPickerController extends BasePreferenceController implements 42 InputManager.InputDeviceListener, LifecycleObserver, OnStart, OnStop { 43 44 private final InputManager mIm; 45 private final Map<SwitchPreference, KeyboardLayout> mPreferenceMap; 46 47 private Fragment mParent; 48 private int mInputDeviceId; 49 private InputDeviceIdentifier mInputDeviceIdentifier; 50 private KeyboardLayout[] mKeyboardLayouts; 51 private PreferenceScreen mScreen; 52 53 KeyboardLayoutPickerController(Context context, String key)54 public KeyboardLayoutPickerController(Context context, String key) { 55 super(context, key); 56 mIm = (InputManager) context.getSystemService(Context.INPUT_SERVICE); 57 mInputDeviceId = -1; 58 mPreferenceMap = new HashMap<>(); 59 } 60 initialize(Fragment parent, InputDeviceIdentifier inputDeviceIdentifier)61 public void initialize(Fragment parent, InputDeviceIdentifier inputDeviceIdentifier) { 62 mParent = parent; 63 mInputDeviceIdentifier = inputDeviceIdentifier; 64 mKeyboardLayouts = mIm.getKeyboardLayoutsForInputDevice(mInputDeviceIdentifier); 65 Arrays.sort(mKeyboardLayouts); 66 } 67 68 @Override onStart()69 public void onStart() { 70 mIm.registerInputDeviceListener(this, null); 71 72 final InputDevice inputDevice = 73 mIm.getInputDeviceByDescriptor(mInputDeviceIdentifier.getDescriptor()); 74 if (inputDevice == null) { 75 mParent.getActivity().finish(); 76 return; 77 } 78 mInputDeviceId = inputDevice.getId(); 79 80 updateCheckedState(); 81 } 82 83 @Override onStop()84 public void onStop() { 85 mIm.unregisterInputDeviceListener(this); 86 mInputDeviceId = -1; 87 } 88 89 @Override displayPreference(PreferenceScreen screen)90 public void displayPreference(PreferenceScreen screen) { 91 super.displayPreference(screen); 92 mScreen = screen; 93 createPreferenceHierarchy(); 94 } 95 96 @Override getAvailabilityStatus()97 public int getAvailabilityStatus() { 98 return AVAILABLE; 99 } 100 101 @Override handlePreferenceTreeClick(Preference preference)102 public boolean handlePreferenceTreeClick(Preference preference) { 103 if (!(preference instanceof SwitchPreference)) { 104 return false; 105 } 106 107 final SwitchPreference switchPref = (SwitchPreference) preference; 108 final KeyboardLayout layout = mPreferenceMap.get(switchPref); 109 if (layout != null) { 110 final boolean checked = switchPref.isChecked(); 111 if (checked) { 112 mIm.addKeyboardLayoutForInputDevice(mInputDeviceIdentifier, 113 layout.getDescriptor()); 114 } else { 115 mIm.removeKeyboardLayoutForInputDevice(mInputDeviceIdentifier, 116 layout.getDescriptor()); 117 } 118 } 119 return true; 120 } 121 122 @Override onInputDeviceAdded(int deviceId)123 public void onInputDeviceAdded(int deviceId) { 124 125 } 126 127 @Override onInputDeviceRemoved(int deviceId)128 public void onInputDeviceRemoved(int deviceId) { 129 if (mInputDeviceId >= 0 && deviceId == mInputDeviceId) { 130 mParent.getActivity().finish(); 131 } 132 } 133 134 @Override onInputDeviceChanged(int deviceId)135 public void onInputDeviceChanged(int deviceId) { 136 if (mInputDeviceId >= 0 && deviceId == mInputDeviceId) { 137 updateCheckedState(); 138 } 139 } 140 updateCheckedState()141 private void updateCheckedState() { 142 final String[] enabledKeyboardLayouts = mIm.getEnabledKeyboardLayoutsForInputDevice( 143 mInputDeviceIdentifier); 144 Arrays.sort(enabledKeyboardLayouts); 145 146 for (Map.Entry<SwitchPreference, KeyboardLayout> entry : mPreferenceMap.entrySet()) { 147 entry.getKey().setChecked(Arrays.binarySearch(enabledKeyboardLayouts, 148 entry.getValue().getDescriptor()) >= 0); 149 } 150 } 151 createPreferenceHierarchy()152 private void createPreferenceHierarchy() { 153 for (KeyboardLayout layout : mKeyboardLayouts) { 154 final SwitchPreference pref = new SwitchPreference(mScreen.getContext()); 155 pref.setTitle(layout.getLabel()); 156 pref.setSummary(layout.getCollection()); 157 pref.setKey(layout.getDescriptor()); 158 mScreen.addPreference(pref); 159 mPreferenceMap.put(pref, layout); 160 } 161 } 162 } 163 164