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