blob: e97aca886c030794c33f2776a86c95920e1a0d3d [file] [log] [blame]
Jeff Browncf39bdf2012-05-18 14:41:19 -07001/*
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
17package com.android.server.input;
18
Michael Wrightd5f7ed92016-01-19 11:23:51 -080019import com.android.internal.inputmethod.InputMethodSubtypeHandle;
Jeff Browncf39bdf2012-05-18 14:41:19 -070020import com.android.internal.util.ArrayUtils;
21import com.android.internal.util.FastXmlSerializer;
22import com.android.internal.util.XmlUtils;
23
24import org.xmlpull.v1.XmlPullParser;
25import org.xmlpull.v1.XmlPullParserException;
26import org.xmlpull.v1.XmlSerializer;
27
Jason Gerecked5220742014-03-10 09:47:59 -070028import android.view.Surface;
Jason Gerecked6396d62014-01-27 18:30:37 -080029import android.hardware.input.TouchCalibration;
Michael Wrightd5f7ed92016-01-19 11:23:51 -080030import android.text.TextUtils;
31import android.util.ArrayMap;
Dianne Hackborn39606a02012-07-31 17:54:35 -070032import android.util.AtomicFile;
Jeff Browncf39bdf2012-05-18 14:41:19 -070033import android.util.Slog;
34import android.util.Xml;
35
36import java.io.BufferedInputStream;
37import java.io.BufferedOutputStream;
38import java.io.File;
39import java.io.FileNotFoundException;
40import java.io.FileOutputStream;
41import java.io.IOException;
42import java.io.InputStream;
Michael Wrightd5f7ed92016-01-19 11:23:51 -080043import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010044import java.nio.charset.StandardCharsets;
Jeff Browncf39bdf2012-05-18 14:41:19 -070045import java.util.ArrayList;
Michael Wrightd5f7ed92016-01-19 11:23:51 -080046import java.util.Arrays;
Jeff Browncf39bdf2012-05-18 14:41:19 -070047import java.util.Collections;
48import java.util.HashMap;
Michael Wrightd5f7ed92016-01-19 11:23:51 -080049import java.util.List;
Jeff Browncf39bdf2012-05-18 14:41:19 -070050import java.util.Map;
51import java.util.Set;
52
53import libcore.io.IoUtils;
54import 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 * &lt;input-mananger-state>
63 * &lt;input-devices>
64 * &lt;input-device descriptor="xxxxx" keyboard-layout="yyyyy" />
65 * &gt;input-devices>
66 * &gt;/input-manager-state>
67 * </code>
68 */
69final 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 Gerecked5220742014-03-10 09:47:59 -070094 public TouchCalibration getTouchCalibration(String inputDeviceDescriptor, int surfaceRotation) {
Jason Gerecked6396d62014-01-27 18:30:37 -080095 InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, false);
96 if (state == null) {
97 return TouchCalibration.IDENTITY;
98 }
Jason Gerecked5220742014-03-10 09:47:59 -070099
100 TouchCalibration cal = state.getTouchCalibration(surfaceRotation);
101 if (cal == null) {
102 return TouchCalibration.IDENTITY;
Jason Gerecked6396d62014-01-27 18:30:37 -0800103 }
Jason Gerecked5220742014-03-10 09:47:59 -0700104 return cal;
Jason Gerecked6396d62014-01-27 18:30:37 -0800105 }
106
Jason Gerecked5220742014-03-10 09:47:59 -0700107 public boolean setTouchCalibration(String inputDeviceDescriptor, int surfaceRotation, TouchCalibration calibration) {
Jason Gerecked6396d62014-01-27 18:30:37 -0800108 InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, true);
Jason Gerecked5220742014-03-10 09:47:59 -0700109
110 if (state.setTouchCalibration(surfaceRotation, calibration)) {
Jason Gerecked6396d62014-01-27 18:30:37 -0800111 setDirty();
112 return true;
113 }
Jason Gerecked5220742014-03-10 09:47:59 -0700114
Jason Gerecked6396d62014-01-27 18:30:37 -0800115 return false;
116 }
117
Jeff Browncf39bdf2012-05-18 14:41:19 -0700118 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 Wrightd5f7ed92016-01-19 11:23:51 -0800140 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 Browncf39bdf2012-05-18 14:41:19 -0700148
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800149 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 Browncf39bdf2012-05-18 14:41:19 -0700160 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 Wrightd5f7ed92016-01-19 11:23:51 -0800178 public boolean switchKeyboardLayout(String inputDeviceDescriptor,
179 InputMethodSubtypeHandle imeHandle) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700180 InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, false);
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800181 if (state != null && state.switchKeyboardLayout(imeHandle)) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700182 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 Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100242 parser.setInput(new BufferedInputStream(is), StandardCharsets.UTF_8.name());
Jeff Browncf39bdf2012-05-18 14:41:19 -0700243 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 Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100262 serializer.setOutput(new BufferedOutputStream(os), StandardCharsets.UTF_8.name());
Jeff Browncf39bdf2012-05-18 14:41:19 -0700263 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 Wrightd5f7ed92016-01-19 11:23:51 -0800328 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 Browncf39bdf2012-05-18 14:41:19 -0700340 private static final class InputDeviceState {
Jason Gerecked6396d62014-01-27 18:30:37 -0800341 private static final String[] CALIBRATION_NAME = { "x_scale",
342 "x_ymix", "x_offset", "y_xmix", "y_scale", "y_offset" };
343
Jason Gerecked5220742014-03-10 09:47:59 -0700344 private TouchCalibration[] mTouchCalibration = new TouchCalibration[4];
Jeff Browncf39bdf2012-05-18 14:41:19 -0700345 private String mCurrentKeyboardLayout;
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800346 private List<String> mUnassociatedKeyboardLayouts = new ArrayList<>();
347 private ArrayMap<InputMethodSubtypeHandle, String> mKeyboardLayouts = new ArrayMap<>();
Jeff Browncf39bdf2012-05-18 14:41:19 -0700348
Jason Gerecked5220742014-03-10 09:47:59 -0700349 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 Gerecked6396d62014-01-27 18:30:37 -0800356 }
357
Jason Gerecked5220742014-03-10 09:47:59 -0700358 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 Gerecked6396d62014-01-27 18:30:37 -0800367 return false;
368 }
Jason Gerecked6396d62014-01-27 18:30:37 -0800369 }
370
Jeff Browncf39bdf2012-05-18 14:41:19 -0700371 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 Wrightd5f7ed92016-01-19 11:23:51 -0800385 if (mUnassociatedKeyboardLayouts.isEmpty()) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700386 return (String[])ArrayUtils.emptyArray(String.class);
387 }
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800388 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 Browncf39bdf2012-05-18 14:41:19 -0700404 }
405
406 public boolean addKeyboardLayout(String keyboardLayout) {
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800407 int index = Collections.binarySearch(
408 mUnassociatedKeyboardLayouts, keyboardLayout);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700409 if (index >= 0) {
410 return false;
411 }
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800412 mUnassociatedKeyboardLayouts.add(-index - 1, keyboardLayout);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700413 if (mCurrentKeyboardLayout == null) {
414 mCurrentKeyboardLayout = keyboardLayout;
415 }
416 return true;
417 }
418
419 public boolean removeKeyboardLayout(String keyboardLayout) {
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800420 int index = Collections.binarySearch(mUnassociatedKeyboardLayouts, keyboardLayout);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700421 if (index < 0) {
422 return false;
423 }
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800424 mUnassociatedKeyboardLayouts.remove(index);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700425 updateCurrentKeyboardLayoutIfRemoved(keyboardLayout, index);
426 return true;
427 }
428
429 private void updateCurrentKeyboardLayoutIfRemoved(
430 String removedKeyboardLayout, int removedIndex) {
431 if (Objects.equal(mCurrentKeyboardLayout, removedKeyboardLayout)) {
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800432 if (!mUnassociatedKeyboardLayouts.isEmpty()) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700433 int index = removedIndex;
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800434 if (index == mUnassociatedKeyboardLayouts.size()) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700435 index = 0;
436 }
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800437 mCurrentKeyboardLayout = mUnassociatedKeyboardLayouts.get(index);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700438 } else {
439 mCurrentKeyboardLayout = null;
440 }
441 }
442 }
443
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800444 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 Browncf39bdf2012-05-18 14:41:19 -0700449 }
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800450 return false;
Jeff Browncf39bdf2012-05-18 14:41:19 -0700451 }
452
453 public boolean removeUninstalledKeyboardLayouts(Set<String> availableKeyboardLayouts) {
454 boolean changed = false;
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800455 for (int i = mUnassociatedKeyboardLayouts.size(); i-- > 0; ) {
456 String keyboardLayout = mUnassociatedKeyboardLayouts.get(i);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700457 if (!availableKeyboardLayouts.contains(keyboardLayout)) {
458 Slog.i(TAG, "Removing uninstalled keyboard layout " + keyboardLayout);
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800459 mUnassociatedKeyboardLayouts.remove(i);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700460 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 Browncf39bdf2012-05-18 14:41:19 -0700477
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800478 String current = parser.getAttributeValue(null, "current");
Jeff Browncf39bdf2012-05-18 14:41:19 -0700479 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 Wrightd5f7ed92016-01-19 11:23:51 -0800486
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 Gerecked6396d62014-01-27 18:30:37 -0800512 } else if (parser.getName().equals("calibration")) {
513 String format = parser.getAttributeValue(null, "format");
Jason Gerecked5220742014-03-10 09:47:59 -0700514 String rotation = parser.getAttributeValue(null, "rotation");
515 int r = -1;
516
Jason Gerecked6396d62014-01-27 18:30:37 -0800517 if (format == null) {
518 throw new XmlPullParserException(
519 "Missing format attribute on calibration.");
520 }
Jason Gerecked5220742014-03-10 09:47:59 -0700521 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 Gerecked6396d62014-01-27 18:30:37 -0800533
Jason Gerecked5220742014-03-10 09:47:59 -0700534 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 Gerecked6396d62014-01-27 18:30:37 -0800544 }
545 }
Jason Gerecked5220742014-03-10 09:47:59 -0700546 }
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 Gerecked6396d62014-01-27 18:30:37 -0800554 } else {
Jason Gerecked5220742014-03-10 09:47:59 -0700555 mTouchCalibration[r] = new TouchCalibration(matrix[0],
556 matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
Jason Gerecked6396d62014-01-27 18:30:37 -0800557 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700558 }
559 }
560
561 // Maintain invariant that layouts are sorted.
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800562 Collections.sort(mUnassociatedKeyboardLayouts);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700563
564 // Maintain invariant that there is always a current keyboard layout unless
565 // there are none installed.
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800566 if (mCurrentKeyboardLayout == null && !mUnassociatedKeyboardLayouts.isEmpty()) {
567 mCurrentKeyboardLayout = mUnassociatedKeyboardLayouts.get(0);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700568 }
569 }
570
571 public void saveToXml(XmlSerializer serializer) throws IOException {
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800572 for (String layout : mUnassociatedKeyboardLayouts) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700573 serializer.startTag(null, "keyboard-layout");
574 serializer.attribute(null, "descriptor", layout);
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800575 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 Browncf39bdf2012-05-18 14:41:19 -0700587 if (layout.equals(mCurrentKeyboardLayout)) {
588 serializer.attribute(null, "current", "true");
589 }
590 serializer.endTag(null, "keyboard-layout");
591 }
Jason Gerecked6396d62014-01-27 18:30:37 -0800592
Jason Gerecked5220742014-03-10 09:47:59 -0700593 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 Gerecked6396d62014-01-27 18:30:37 -0800608 }
Jason Gerecked5220742014-03-10 09:47:59 -0700609 }
610
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800611 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 Gerecked5220742014-03-10 09:47:59 -0700627 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 Browncf39bdf2012-05-18 14:41:19 -0700651 }
652 }
Jason Gerecked6396d62014-01-27 18:30:37 -0800653}