1 /* 2 * Copyright (C) 2012 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.InputManager.InputDeviceListener; 23 import android.hardware.input.KeyboardLayout; 24 import android.os.Bundle; 25 import android.support.v7.preference.CheckBoxPreference; 26 import android.support.v7.preference.Preference; 27 import android.support.v7.preference.PreferenceScreen; 28 import android.view.InputDevice; 29 30 import com.android.internal.logging.MetricsProto.MetricsEvent; 31 import com.android.settings.SettingsPreferenceFragment; 32 33 import java.util.Arrays; 34 import java.util.HashMap; 35 import java.util.Map; 36 37 public class KeyboardLayoutPickerFragment extends SettingsPreferenceFragment 38 implements InputDeviceListener { 39 private InputDeviceIdentifier mInputDeviceIdentifier; 40 private int mInputDeviceId = -1; 41 private InputManager mIm; 42 private KeyboardLayout[] mKeyboardLayouts; 43 private HashMap<CheckBoxPreference, KeyboardLayout> mPreferenceMap = 44 new HashMap<CheckBoxPreference, KeyboardLayout>(); 45 46 /** 47 * Intent extra: The input device descriptor of the keyboard whose keyboard 48 * layout is to be changed. 49 */ 50 public static final String EXTRA_INPUT_DEVICE_IDENTIFIER = "input_device_identifier"; 51 52 @Override 53 protected int getMetricsCategory() { 54 return MetricsEvent.INPUTMETHOD_KEYBOARD; 55 } 56 57 @Override 58 public void onCreate(Bundle icicle) { 59 super.onCreate(icicle); 60 61 mInputDeviceIdentifier = getActivity().getIntent().getParcelableExtra( 62 EXTRA_INPUT_DEVICE_IDENTIFIER); 63 if (mInputDeviceIdentifier == null) { 64 getActivity().finish(); 65 } 66 67 mIm = (InputManager)getSystemService(Context.INPUT_SERVICE); 68 mKeyboardLayouts = mIm.getKeyboardLayoutsForInputDevice(mInputDeviceIdentifier); 69 Arrays.sort(mKeyboardLayouts); 70 setPreferenceScreen(createPreferenceHierarchy()); 71 } 72 73 @Override 74 public void onResume() { 75 super.onResume(); 76 77 mIm.registerInputDeviceListener(this, null); 78 79 InputDevice inputDevice = 80 mIm.getInputDeviceByDescriptor(mInputDeviceIdentifier.getDescriptor()); 81 if (inputDevice == null) { 82 getActivity().finish(); 83 return; 84 } 85 mInputDeviceId = inputDevice.getId(); 86 87 updateCheckedState(); 88 } 89 90 @Override 91 public void onPause() { 92 mIm.unregisterInputDeviceListener(this); 93 mInputDeviceId = -1; 94 95 super.onPause(); 96 } 97 98 @Override 99 public boolean onPreferenceTreeClick(Preference preference) { 100 if (preference instanceof CheckBoxPreference) { 101 CheckBoxPreference checkboxPref = (CheckBoxPreference)preference; 102 KeyboardLayout layout = mPreferenceMap.get(checkboxPref); 103 if (layout != null) { 104 boolean checked = checkboxPref.isChecked(); 105 if (checked) { 106 mIm.addKeyboardLayoutForInputDevice(mInputDeviceIdentifier, 107 layout.getDescriptor()); 108 } else { 109 mIm.removeKeyboardLayoutForInputDevice(mInputDeviceIdentifier, 110 layout.getDescriptor()); 111 } 112 return true; 113 } 114 } 115 return super.onPreferenceTreeClick(preference); 116 } 117 118 @Override 119 public void onInputDeviceAdded(int deviceId) { 120 } 121 122 @Override 123 public void onInputDeviceChanged(int deviceId) { 124 if (mInputDeviceId >= 0 && deviceId == mInputDeviceId) { 125 updateCheckedState(); 126 } 127 } 128 129 @Override 130 public void onInputDeviceRemoved(int deviceId) { 131 if (mInputDeviceId >= 0 && deviceId == mInputDeviceId) { 132 getActivity().finish(); 133 } 134 } 135 136 private PreferenceScreen createPreferenceHierarchy() { 137 PreferenceScreen root = getPreferenceManager().createPreferenceScreen(getActivity()); 138 Context context = getActivity(); 139 140 for (KeyboardLayout layout : mKeyboardLayouts) { 141 CheckBoxPreference pref = new CheckBoxPreference(getPrefContext()); 142 pref.setTitle(layout.getLabel()); 143 pref.setSummary(layout.getCollection()); 144 root.addPreference(pref); 145 mPreferenceMap.put(pref, layout); 146 } 147 return root; 148 } 149 150 private void updateCheckedState() { 151 String[] enabledKeyboardLayouts = mIm.getEnabledKeyboardLayoutsForInputDevice( 152 mInputDeviceIdentifier); 153 Arrays.sort(enabledKeyboardLayouts); 154 155 for (Map.Entry<CheckBoxPreference, KeyboardLayout> entry : mPreferenceMap.entrySet()) { 156 entry.getKey().setChecked(Arrays.binarySearch(enabledKeyboardLayouts, 157 entry.getValue().getDescriptor()) >= 0); 158 } 159 } 160 } 161