Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 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.server.input; |
| 18 | |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 19 | import com.android.internal.inputmethod.InputMethodSubtypeHandle; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 20 | import com.android.internal.util.ArrayUtils; |
| 21 | import com.android.internal.util.FastXmlSerializer; |
| 22 | import com.android.internal.util.XmlUtils; |
| 23 | |
| 24 | import org.xmlpull.v1.XmlPullParser; |
| 25 | import org.xmlpull.v1.XmlPullParserException; |
| 26 | import org.xmlpull.v1.XmlSerializer; |
| 27 | |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 28 | import android.view.Surface; |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 29 | import android.hardware.input.TouchCalibration; |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 30 | import android.text.TextUtils; |
| 31 | import android.util.ArrayMap; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 32 | import android.util.AtomicFile; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 33 | import android.util.Slog; |
| 34 | import android.util.Xml; |
| 35 | |
| 36 | import java.io.BufferedInputStream; |
| 37 | import java.io.BufferedOutputStream; |
| 38 | import java.io.File; |
| 39 | import java.io.FileNotFoundException; |
| 40 | import java.io.FileOutputStream; |
| 41 | import java.io.IOException; |
| 42 | import java.io.InputStream; |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 43 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 44 | import java.nio.charset.StandardCharsets; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 45 | import java.util.ArrayList; |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 46 | import java.util.Arrays; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 47 | import java.util.Collections; |
| 48 | import java.util.HashMap; |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 49 | import java.util.List; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 50 | import java.util.Map; |
| 51 | import java.util.Set; |
| 52 | |
| 53 | import libcore.io.IoUtils; |
| 54 | import libcore.util.Objects; |
| 55 | |
| 56 | /** |
| 57 | * Manages persistent state recorded by the input manager service as an XML file. |
| 58 | * Caller must acquire lock on the data store before accessing it. |
| 59 | * |
| 60 | * File format: |
| 61 | * <code> |
| 62 | * <input-mananger-state> |
| 63 | * <input-devices> |
| 64 | * <input-device descriptor="xxxxx" keyboard-layout="yyyyy" /> |
| 65 | * >input-devices> |
| 66 | * >/input-manager-state> |
| 67 | * </code> |
| 68 | */ |
| 69 | final class PersistentDataStore { |
| 70 | static final String TAG = "InputManager"; |
| 71 | |
| 72 | // Input device state by descriptor. |
| 73 | private final HashMap<String, InputDeviceState> mInputDevices = |
| 74 | new HashMap<String, InputDeviceState>(); |
| 75 | private final AtomicFile mAtomicFile; |
| 76 | |
| 77 | // True if the data has been loaded. |
| 78 | private boolean mLoaded; |
| 79 | |
| 80 | // True if there are changes to be saved. |
| 81 | private boolean mDirty; |
| 82 | |
| 83 | public PersistentDataStore() { |
| 84 | mAtomicFile = new AtomicFile(new File("/data/system/input-manager-state.xml")); |
| 85 | } |
| 86 | |
| 87 | public void saveIfNeeded() { |
| 88 | if (mDirty) { |
| 89 | save(); |
| 90 | mDirty = false; |
| 91 | } |
| 92 | } |
| 93 | |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 94 | public TouchCalibration getTouchCalibration(String inputDeviceDescriptor, int surfaceRotation) { |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 95 | InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, false); |
| 96 | if (state == null) { |
| 97 | return TouchCalibration.IDENTITY; |
| 98 | } |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 99 | |
| 100 | TouchCalibration cal = state.getTouchCalibration(surfaceRotation); |
| 101 | if (cal == null) { |
| 102 | return TouchCalibration.IDENTITY; |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 103 | } |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 104 | return cal; |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 107 | public boolean setTouchCalibration(String inputDeviceDescriptor, int surfaceRotation, TouchCalibration calibration) { |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 108 | InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, true); |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 109 | |
| 110 | if (state.setTouchCalibration(surfaceRotation, calibration)) { |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 111 | setDirty(); |
| 112 | return true; |
| 113 | } |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 114 | |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 118 | public String getCurrentKeyboardLayout(String inputDeviceDescriptor) { |
| 119 | InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, false); |
| 120 | return state != null ? state.getCurrentKeyboardLayout() : null; |
| 121 | } |
| 122 | |
| 123 | public boolean setCurrentKeyboardLayout(String inputDeviceDescriptor, |
| 124 | String keyboardLayoutDescriptor) { |
| 125 | InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, true); |
| 126 | if (state.setCurrentKeyboardLayout(keyboardLayoutDescriptor)) { |
| 127 | setDirty(); |
| 128 | return true; |
| 129 | } |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | public String[] getKeyboardLayouts(String inputDeviceDescriptor) { |
| 134 | InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, false); |
| 135 | if (state == null) { |
| 136 | return (String[])ArrayUtils.emptyArray(String.class); |
| 137 | } |
| 138 | return state.getKeyboardLayouts(); |
| 139 | } |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 140 | public String getKeyboardLayout(String inputDeviceDescriptor, |
| 141 | InputMethodSubtypeHandle imeHandle) { |
| 142 | InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, false); |
| 143 | if (state == null) { |
| 144 | return null; |
| 145 | } |
| 146 | return state.getKeyboardLayout(imeHandle); |
| 147 | } |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 148 | |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 149 | public boolean setKeyboardLayout(String inputDeviceDescriptor, |
| 150 | InputMethodSubtypeHandle imeHandle, String keyboardLayoutDescriptor) { |
| 151 | InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, true); |
| 152 | if (state.setKeyboardLayout(imeHandle, keyboardLayoutDescriptor)) { |
| 153 | setDirty(); |
| 154 | return true; |
| 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | public boolean addKeyboardLayout(String inputDeviceDescriptor, String keyboardLayoutDescriptor) { |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 160 | InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, true); |
| 161 | if (state.addKeyboardLayout(keyboardLayoutDescriptor)) { |
| 162 | setDirty(); |
| 163 | return true; |
| 164 | } |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | public boolean removeKeyboardLayout(String inputDeviceDescriptor, |
| 169 | String keyboardLayoutDescriptor) { |
| 170 | InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, true); |
| 171 | if (state.removeKeyboardLayout(keyboardLayoutDescriptor)) { |
| 172 | setDirty(); |
| 173 | return true; |
| 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 178 | public boolean switchKeyboardLayout(String inputDeviceDescriptor, |
| 179 | InputMethodSubtypeHandle imeHandle) { |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 180 | InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, false); |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 181 | if (state != null && state.switchKeyboardLayout(imeHandle)) { |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 182 | setDirty(); |
| 183 | return true; |
| 184 | } |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | public boolean removeUninstalledKeyboardLayouts(Set<String> availableKeyboardLayouts) { |
| 189 | boolean changed = false; |
| 190 | for (InputDeviceState state : mInputDevices.values()) { |
| 191 | if (state.removeUninstalledKeyboardLayouts(availableKeyboardLayouts)) { |
| 192 | changed = true; |
| 193 | } |
| 194 | } |
| 195 | if (changed) { |
| 196 | setDirty(); |
| 197 | return true; |
| 198 | } |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | private InputDeviceState getInputDeviceState(String inputDeviceDescriptor, |
| 203 | boolean createIfAbsent) { |
| 204 | loadIfNeeded(); |
| 205 | InputDeviceState state = mInputDevices.get(inputDeviceDescriptor); |
| 206 | if (state == null && createIfAbsent) { |
| 207 | state = new InputDeviceState(); |
| 208 | mInputDevices.put(inputDeviceDescriptor, state); |
| 209 | setDirty(); |
| 210 | } |
| 211 | return state; |
| 212 | } |
| 213 | |
| 214 | private void loadIfNeeded() { |
| 215 | if (!mLoaded) { |
| 216 | load(); |
| 217 | mLoaded = true; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | private void setDirty() { |
| 222 | mDirty = true; |
| 223 | } |
| 224 | |
| 225 | private void clearState() { |
| 226 | mInputDevices.clear(); |
| 227 | } |
| 228 | |
| 229 | private void load() { |
| 230 | clearState(); |
| 231 | |
| 232 | final InputStream is; |
| 233 | try { |
| 234 | is = mAtomicFile.openRead(); |
| 235 | } catch (FileNotFoundException ex) { |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | XmlPullParser parser; |
| 240 | try { |
| 241 | parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 242 | parser.setInput(new BufferedInputStream(is), StandardCharsets.UTF_8.name()); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 243 | loadFromXml(parser); |
| 244 | } catch (IOException ex) { |
| 245 | Slog.w(InputManagerService.TAG, "Failed to load input manager persistent store data.", ex); |
| 246 | clearState(); |
| 247 | } catch (XmlPullParserException ex) { |
| 248 | Slog.w(InputManagerService.TAG, "Failed to load input manager persistent store data.", ex); |
| 249 | clearState(); |
| 250 | } finally { |
| 251 | IoUtils.closeQuietly(is); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | private void save() { |
| 256 | final FileOutputStream os; |
| 257 | try { |
| 258 | os = mAtomicFile.startWrite(); |
| 259 | boolean success = false; |
| 260 | try { |
| 261 | XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 262 | serializer.setOutput(new BufferedOutputStream(os), StandardCharsets.UTF_8.name()); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 263 | saveToXml(serializer); |
| 264 | serializer.flush(); |
| 265 | success = true; |
| 266 | } finally { |
| 267 | if (success) { |
| 268 | mAtomicFile.finishWrite(os); |
| 269 | } else { |
| 270 | mAtomicFile.failWrite(os); |
| 271 | } |
| 272 | } |
| 273 | } catch (IOException ex) { |
| 274 | Slog.w(InputManagerService.TAG, "Failed to save input manager persistent store data.", ex); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | private void loadFromXml(XmlPullParser parser) |
| 279 | throws IOException, XmlPullParserException { |
| 280 | XmlUtils.beginDocument(parser, "input-manager-state"); |
| 281 | final int outerDepth = parser.getDepth(); |
| 282 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 283 | if (parser.getName().equals("input-devices")) { |
| 284 | loadInputDevicesFromXml(parser); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | private void loadInputDevicesFromXml(XmlPullParser parser) |
| 290 | throws IOException, XmlPullParserException { |
| 291 | final int outerDepth = parser.getDepth(); |
| 292 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 293 | if (parser.getName().equals("input-device")) { |
| 294 | String descriptor = parser.getAttributeValue(null, "descriptor"); |
| 295 | if (descriptor == null) { |
| 296 | throw new XmlPullParserException( |
| 297 | "Missing descriptor attribute on input-device."); |
| 298 | } |
| 299 | if (mInputDevices.containsKey(descriptor)) { |
| 300 | throw new XmlPullParserException("Found duplicate input device."); |
| 301 | } |
| 302 | |
| 303 | InputDeviceState state = new InputDeviceState(); |
| 304 | state.loadFromXml(parser); |
| 305 | mInputDevices.put(descriptor, state); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | private void saveToXml(XmlSerializer serializer) throws IOException { |
| 311 | serializer.startDocument(null, true); |
| 312 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 313 | serializer.startTag(null, "input-manager-state"); |
| 314 | serializer.startTag(null, "input-devices"); |
| 315 | for (Map.Entry<String, InputDeviceState> entry : mInputDevices.entrySet()) { |
| 316 | final String descriptor = entry.getKey(); |
| 317 | final InputDeviceState state = entry.getValue(); |
| 318 | serializer.startTag(null, "input-device"); |
| 319 | serializer.attribute(null, "descriptor", descriptor); |
| 320 | state.saveToXml(serializer); |
| 321 | serializer.endTag(null, "input-device"); |
| 322 | } |
| 323 | serializer.endTag(null, "input-devices"); |
| 324 | serializer.endTag(null, "input-manager-state"); |
| 325 | serializer.endDocument(); |
| 326 | } |
| 327 | |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 328 | public void dump(PrintWriter pw, String prefix) { |
| 329 | pw.println(prefix + "PersistentDataStore"); |
| 330 | pw.println(prefix + " mLoaded=" + mLoaded); |
| 331 | pw.println(prefix + " mDirty=" + mDirty); |
| 332 | pw.println(prefix + " InputDeviceStates:"); |
| 333 | int i = 0; |
| 334 | for (Map.Entry<String, InputDeviceState> entry : mInputDevices.entrySet()) { |
| 335 | pw.println(prefix + " " + i++ + ": " + entry.getKey()); |
| 336 | entry.getValue().dump(pw, prefix + " "); |
| 337 | } |
| 338 | } |
| 339 | |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 340 | private static final class InputDeviceState { |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 341 | private static final String[] CALIBRATION_NAME = { "x_scale", |
| 342 | "x_ymix", "x_offset", "y_xmix", "y_scale", "y_offset" }; |
| 343 | |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 344 | private TouchCalibration[] mTouchCalibration = new TouchCalibration[4]; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 345 | private String mCurrentKeyboardLayout; |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 346 | private List<String> mUnassociatedKeyboardLayouts = new ArrayList<>(); |
| 347 | private ArrayMap<InputMethodSubtypeHandle, String> mKeyboardLayouts = new ArrayMap<>(); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 348 | |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 349 | public TouchCalibration getTouchCalibration(int surfaceRotation) { |
| 350 | try { |
| 351 | return mTouchCalibration[surfaceRotation]; |
| 352 | } catch (ArrayIndexOutOfBoundsException ex) { |
| 353 | Slog.w(InputManagerService.TAG, "Cannot get touch calibration.", ex); |
| 354 | return null; |
| 355 | } |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 358 | public boolean setTouchCalibration(int surfaceRotation, TouchCalibration calibration) { |
| 359 | try { |
| 360 | if (!calibration.equals(mTouchCalibration[surfaceRotation])) { |
| 361 | mTouchCalibration[surfaceRotation] = calibration; |
| 362 | return true; |
| 363 | } |
| 364 | return false; |
| 365 | } catch (ArrayIndexOutOfBoundsException ex) { |
| 366 | Slog.w(InputManagerService.TAG, "Cannot set touch calibration.", ex); |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 367 | return false; |
| 368 | } |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 371 | public String getCurrentKeyboardLayout() { |
| 372 | return mCurrentKeyboardLayout; |
| 373 | } |
| 374 | |
| 375 | public boolean setCurrentKeyboardLayout(String keyboardLayout) { |
| 376 | if (Objects.equal(mCurrentKeyboardLayout, keyboardLayout)) { |
| 377 | return false; |
| 378 | } |
| 379 | addKeyboardLayout(keyboardLayout); |
| 380 | mCurrentKeyboardLayout = keyboardLayout; |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | public String[] getKeyboardLayouts() { |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 385 | if (mUnassociatedKeyboardLayouts.isEmpty()) { |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 386 | return (String[])ArrayUtils.emptyArray(String.class); |
| 387 | } |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 388 | return mUnassociatedKeyboardLayouts.toArray( |
| 389 | new String[mUnassociatedKeyboardLayouts.size()]); |
| 390 | } |
| 391 | |
| 392 | public String getKeyboardLayout(InputMethodSubtypeHandle handle) { |
| 393 | return mKeyboardLayouts.get(handle); |
| 394 | } |
| 395 | |
| 396 | public boolean setKeyboardLayout(InputMethodSubtypeHandle imeHandle, |
| 397 | String keyboardLayout) { |
| 398 | String existingLayout = mKeyboardLayouts.get(imeHandle); |
| 399 | if (TextUtils.equals(existingLayout, keyboardLayout)) { |
| 400 | return false; |
| 401 | } |
| 402 | mKeyboardLayouts.put(imeHandle, keyboardLayout); |
| 403 | return true; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | public boolean addKeyboardLayout(String keyboardLayout) { |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 407 | int index = Collections.binarySearch( |
| 408 | mUnassociatedKeyboardLayouts, keyboardLayout); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 409 | if (index >= 0) { |
| 410 | return false; |
| 411 | } |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 412 | mUnassociatedKeyboardLayouts.add(-index - 1, keyboardLayout); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 413 | if (mCurrentKeyboardLayout == null) { |
| 414 | mCurrentKeyboardLayout = keyboardLayout; |
| 415 | } |
| 416 | return true; |
| 417 | } |
| 418 | |
| 419 | public boolean removeKeyboardLayout(String keyboardLayout) { |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 420 | int index = Collections.binarySearch(mUnassociatedKeyboardLayouts, keyboardLayout); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 421 | if (index < 0) { |
| 422 | return false; |
| 423 | } |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 424 | mUnassociatedKeyboardLayouts.remove(index); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 425 | updateCurrentKeyboardLayoutIfRemoved(keyboardLayout, index); |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | private void updateCurrentKeyboardLayoutIfRemoved( |
| 430 | String removedKeyboardLayout, int removedIndex) { |
| 431 | if (Objects.equal(mCurrentKeyboardLayout, removedKeyboardLayout)) { |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 432 | if (!mUnassociatedKeyboardLayouts.isEmpty()) { |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 433 | int index = removedIndex; |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 434 | if (index == mUnassociatedKeyboardLayouts.size()) { |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 435 | index = 0; |
| 436 | } |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 437 | mCurrentKeyboardLayout = mUnassociatedKeyboardLayouts.get(index); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 438 | } else { |
| 439 | mCurrentKeyboardLayout = null; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 444 | public boolean switchKeyboardLayout(InputMethodSubtypeHandle imeHandle) { |
| 445 | final String layout = mKeyboardLayouts.get(imeHandle); |
| 446 | if (layout != null && !TextUtils.equals(mCurrentKeyboardLayout, layout)) { |
| 447 | mCurrentKeyboardLayout = layout; |
| 448 | return true; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 449 | } |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 450 | return false; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | public boolean removeUninstalledKeyboardLayouts(Set<String> availableKeyboardLayouts) { |
| 454 | boolean changed = false; |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 455 | for (int i = mUnassociatedKeyboardLayouts.size(); i-- > 0; ) { |
| 456 | String keyboardLayout = mUnassociatedKeyboardLayouts.get(i); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 457 | if (!availableKeyboardLayouts.contains(keyboardLayout)) { |
| 458 | Slog.i(TAG, "Removing uninstalled keyboard layout " + keyboardLayout); |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 459 | mUnassociatedKeyboardLayouts.remove(i); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 460 | updateCurrentKeyboardLayoutIfRemoved(keyboardLayout, i); |
| 461 | changed = true; |
| 462 | } |
| 463 | } |
| 464 | return changed; |
| 465 | } |
| 466 | |
| 467 | public void loadFromXml(XmlPullParser parser) |
| 468 | throws IOException, XmlPullParserException { |
| 469 | final int outerDepth = parser.getDepth(); |
| 470 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 471 | if (parser.getName().equals("keyboard-layout")) { |
| 472 | String descriptor = parser.getAttributeValue(null, "descriptor"); |
| 473 | if (descriptor == null) { |
| 474 | throw new XmlPullParserException( |
| 475 | "Missing descriptor attribute on keyboard-layout."); |
| 476 | } |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 477 | |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 478 | String current = parser.getAttributeValue(null, "current"); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 479 | if (current != null && current.equals("true")) { |
| 480 | if (mCurrentKeyboardLayout != null) { |
| 481 | throw new XmlPullParserException( |
| 482 | "Found multiple current keyboard layouts."); |
| 483 | } |
| 484 | mCurrentKeyboardLayout = descriptor; |
| 485 | } |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 486 | |
| 487 | String inputMethodId = parser.getAttributeValue(null, "input-method-id"); |
| 488 | String inputMethodSubtypeId = |
| 489 | parser.getAttributeValue(null, "input-method-subtype-id"); |
| 490 | if (inputMethodId == null && inputMethodSubtypeId != null |
| 491 | || inputMethodId != null && inputMethodSubtypeId == null) { |
| 492 | throw new XmlPullParserException( |
| 493 | "Found an incomplete input method description"); |
| 494 | } |
| 495 | |
| 496 | if (inputMethodSubtypeId != null) { |
| 497 | InputMethodSubtypeHandle handle = new InputMethodSubtypeHandle( |
| 498 | inputMethodId, Integer.parseInt(inputMethodSubtypeId)); |
| 499 | if (mKeyboardLayouts.containsKey(handle)) { |
| 500 | throw new XmlPullParserException( |
| 501 | "Found duplicate subtype to keyboard layout mapping: " |
| 502 | + handle); |
| 503 | } |
| 504 | mKeyboardLayouts.put(handle, descriptor); |
| 505 | } else { |
| 506 | if (mUnassociatedKeyboardLayouts.contains(descriptor)) { |
| 507 | throw new XmlPullParserException( |
| 508 | "Found duplicate unassociated keyboard layout: " + descriptor); |
| 509 | } |
| 510 | mUnassociatedKeyboardLayouts.add(descriptor); |
| 511 | } |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 512 | } else if (parser.getName().equals("calibration")) { |
| 513 | String format = parser.getAttributeValue(null, "format"); |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 514 | String rotation = parser.getAttributeValue(null, "rotation"); |
| 515 | int r = -1; |
| 516 | |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 517 | if (format == null) { |
| 518 | throw new XmlPullParserException( |
| 519 | "Missing format attribute on calibration."); |
| 520 | } |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 521 | if (!format.equals("affine")) { |
| 522 | throw new XmlPullParserException( |
| 523 | "Unsupported format for calibration."); |
| 524 | } |
| 525 | if (rotation != null) { |
| 526 | try { |
| 527 | r = stringToSurfaceRotation(rotation); |
| 528 | } catch (IllegalArgumentException e) { |
| 529 | throw new XmlPullParserException( |
| 530 | "Unsupported rotation for calibration."); |
| 531 | } |
| 532 | } |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 533 | |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 534 | float[] matrix = TouchCalibration.IDENTITY.getAffineTransform(); |
| 535 | int depth = parser.getDepth(); |
| 536 | while (XmlUtils.nextElementWithin(parser, depth)) { |
| 537 | String tag = parser.getName().toLowerCase(); |
| 538 | String value = parser.nextText(); |
| 539 | |
| 540 | for (int i = 0; i < matrix.length && i < CALIBRATION_NAME.length; i++) { |
| 541 | if (tag.equals(CALIBRATION_NAME[i])) { |
| 542 | matrix[i] = Float.parseFloat(value); |
| 543 | break; |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 544 | } |
| 545 | } |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | if (r == -1) { |
| 549 | // Assume calibration applies to all rotations |
| 550 | for (r = 0; r < mTouchCalibration.length; r++) { |
| 551 | mTouchCalibration[r] = new TouchCalibration(matrix[0], |
| 552 | matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]); |
| 553 | } |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 554 | } else { |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 555 | mTouchCalibration[r] = new TouchCalibration(matrix[0], |
| 556 | matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]); |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 557 | } |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
| 561 | // Maintain invariant that layouts are sorted. |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 562 | Collections.sort(mUnassociatedKeyboardLayouts); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 563 | |
| 564 | // Maintain invariant that there is always a current keyboard layout unless |
| 565 | // there are none installed. |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 566 | if (mCurrentKeyboardLayout == null && !mUnassociatedKeyboardLayouts.isEmpty()) { |
| 567 | mCurrentKeyboardLayout = mUnassociatedKeyboardLayouts.get(0); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | public void saveToXml(XmlSerializer serializer) throws IOException { |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 572 | for (String layout : mUnassociatedKeyboardLayouts) { |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 573 | serializer.startTag(null, "keyboard-layout"); |
| 574 | serializer.attribute(null, "descriptor", layout); |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 575 | serializer.endTag(null, "keyboard-layout"); |
| 576 | } |
| 577 | |
| 578 | final int N = mKeyboardLayouts.size(); |
| 579 | for (int i = 0; i < N; i++) { |
| 580 | final InputMethodSubtypeHandle handle = mKeyboardLayouts.keyAt(i); |
| 581 | final String layout = mKeyboardLayouts.valueAt(i); |
| 582 | serializer.startTag(null, "keyboard-layout"); |
| 583 | serializer.attribute(null, "descriptor", layout); |
| 584 | serializer.attribute(null, "input-method-id", handle.getInputMethodId()); |
| 585 | serializer.attribute(null, "input-method-subtype-id", |
| 586 | Integer.toString(handle.getSubtypeId())); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 587 | if (layout.equals(mCurrentKeyboardLayout)) { |
| 588 | serializer.attribute(null, "current", "true"); |
| 589 | } |
| 590 | serializer.endTag(null, "keyboard-layout"); |
| 591 | } |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 592 | |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 593 | for (int i = 0; i < mTouchCalibration.length; i++) { |
| 594 | if (mTouchCalibration[i] != null) { |
| 595 | String rotation = surfaceRotationToString(i); |
| 596 | float[] transform = mTouchCalibration[i].getAffineTransform(); |
| 597 | |
| 598 | serializer.startTag(null, "calibration"); |
| 599 | serializer.attribute(null, "format", "affine"); |
| 600 | serializer.attribute(null, "rotation", rotation); |
| 601 | for (int j = 0; j < transform.length && j < CALIBRATION_NAME.length; j++) { |
| 602 | serializer.startTag(null, CALIBRATION_NAME[j]); |
| 603 | serializer.text(Float.toString(transform[j])); |
| 604 | serializer.endTag(null, CALIBRATION_NAME[j]); |
| 605 | } |
| 606 | serializer.endTag(null, "calibration"); |
| 607 | } |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 608 | } |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Michael Wright | d5f7ed9 | 2016-01-19 11:23:51 -0800 | [diff] [blame] | 611 | private void dump(final PrintWriter pw, final String prefix) { |
| 612 | pw.println(prefix + "CurrentKeyboardLayout=" + mCurrentKeyboardLayout); |
| 613 | pw.println(prefix + "UnassociatedKeyboardLayouts=" + mUnassociatedKeyboardLayouts); |
| 614 | pw.println(prefix + "TouchCalibration=" + Arrays.toString(mTouchCalibration)); |
| 615 | pw.println(prefix + "Subtype to Layout Mappings:"); |
| 616 | final int N = mKeyboardLayouts.size(); |
| 617 | if (N != 0) { |
| 618 | for (int i = 0; i < N; i++) { |
| 619 | pw.println(prefix + " " + mKeyboardLayouts.keyAt(i) + ": " |
| 620 | + mKeyboardLayouts.valueAt(i)); |
| 621 | } |
| 622 | } else { |
| 623 | pw.println(prefix + " <none>"); |
| 624 | } |
| 625 | } |
| 626 | |
Jason Gerecke | d522074 | 2014-03-10 09:47:59 -0700 | [diff] [blame] | 627 | private static String surfaceRotationToString(int surfaceRotation) { |
| 628 | switch (surfaceRotation) { |
| 629 | case Surface.ROTATION_0: return "0"; |
| 630 | case Surface.ROTATION_90: return "90"; |
| 631 | case Surface.ROTATION_180: return "180"; |
| 632 | case Surface.ROTATION_270: return "270"; |
| 633 | } |
| 634 | throw new IllegalArgumentException("Unsupported surface rotation value" + surfaceRotation); |
| 635 | } |
| 636 | |
| 637 | private static int stringToSurfaceRotation(String s) { |
| 638 | if ("0".equals(s)) { |
| 639 | return Surface.ROTATION_0; |
| 640 | } |
| 641 | if ("90".equals(s)) { |
| 642 | return Surface.ROTATION_90; |
| 643 | } |
| 644 | if ("180".equals(s)) { |
| 645 | return Surface.ROTATION_180; |
| 646 | } |
| 647 | if ("270".equals(s)) { |
| 648 | return Surface.ROTATION_270; |
| 649 | } |
| 650 | throw new IllegalArgumentException("Unsupported surface rotation string '" + s + "'"); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 651 | } |
| 652 | } |
Jason Gerecke | d6396d6 | 2014-01-27 18:30:37 -0800 | [diff] [blame] | 653 | } |