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 com.android.internal.logging.MetricsLogger; 20 import com.android.settings.SettingsPreferenceFragment; 21 22 import android.content.Context; 23 import android.hardware.input.InputDeviceIdentifier; 24 import android.hardware.input.InputManager; 25 import android.hardware.input.InputManager.InputDeviceListener; 26 import android.hardware.input.KeyboardLayout; 27 import android.os.Bundle; 28 import android.preference.CheckBoxPreference; 29 import android.preference.Preference; 30 import android.preference.PreferenceScreen; 31 import android.view.InputDevice; 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 getMetricsCategory()53 protected int getMetricsCategory() { 54 return MetricsLogger.INPUTMETHOD_KEYBOARD; 55 } 56 57 @Override onCreate(Bundle icicle)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.getKeyboardLayouts(); 69 Arrays.sort(mKeyboardLayouts); 70 setPreferenceScreen(createPreferenceHierarchy()); 71 } 72 73 @Override onResume()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 onPause()91 public void onPause() { 92 mIm.unregisterInputDeviceListener(this); 93 mInputDeviceId = -1; 94 95 super.onPause(); 96 } 97 98 @Override onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)99 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 100 Preference preference) { 101 if (preference instanceof CheckBoxPreference) { 102 CheckBoxPreference checkboxPref = (CheckBoxPreference)preference; 103 KeyboardLayout layout = mPreferenceMap.get(checkboxPref); 104 if (layout != null) { 105 boolean checked = checkboxPref.isChecked(); 106 if (checked) { 107 mIm.addKeyboardLayoutForInputDevice(mInputDeviceIdentifier, 108 layout.getDescriptor()); 109 } else { 110 mIm.removeKeyboardLayoutForInputDevice(mInputDeviceIdentifier, 111 layout.getDescriptor()); 112 } 113 return true; 114 } 115 } 116 return super.onPreferenceTreeClick(preferenceScreen, preference); 117 } 118 119 @Override onInputDeviceAdded(int deviceId)120 public void onInputDeviceAdded(int deviceId) { 121 } 122 123 @Override onInputDeviceChanged(int deviceId)124 public void onInputDeviceChanged(int deviceId) { 125 if (mInputDeviceId >= 0 && deviceId == mInputDeviceId) { 126 updateCheckedState(); 127 } 128 } 129 130 @Override onInputDeviceRemoved(int deviceId)131 public void onInputDeviceRemoved(int deviceId) { 132 if (mInputDeviceId >= 0 && deviceId == mInputDeviceId) { 133 getActivity().finish(); 134 } 135 } 136 createPreferenceHierarchy()137 private PreferenceScreen createPreferenceHierarchy() { 138 PreferenceScreen root = getPreferenceManager().createPreferenceScreen(getActivity()); 139 Context context = getActivity(); 140 141 for (KeyboardLayout layout : mKeyboardLayouts) { 142 CheckBoxPreference pref = new CheckBoxPreference(context); 143 pref.setTitle(layout.getLabel()); 144 pref.setSummary(layout.getCollection()); 145 root.addPreference(pref); 146 mPreferenceMap.put(pref, layout); 147 } 148 return root; 149 } 150 updateCheckedState()151 private void updateCheckedState() { 152 String[] enabledKeyboardLayouts = mIm.getKeyboardLayoutsForInputDevice( 153 mInputDeviceIdentifier); 154 Arrays.sort(enabledKeyboardLayouts); 155 156 for (Map.Entry<CheckBoxPreference, KeyboardLayout> entry : mPreferenceMap.entrySet()) { 157 entry.getKey().setChecked(Arrays.binarySearch(enabledKeyboardLayouts, 158 entry.getValue().getDescriptor()) >= 0); 159 } 160 } 161 } 162