FileDocCategorySizeDatePackage
PersistentDataStore.javaAPI DocAndroid 5.1 API21453Thu Mar 12 22:22:42 GMT 2015com.android.server.input

PersistentDataStore

public final class PersistentDataStore extends Object
Manages persistent state recorded by the input manager service as an XML file. Caller must acquire lock on the data store before accessing it. File format: <input-mananger-state> <input-devices> <input-device descriptor="xxxxx" keyboard-layout="yyyyy" /> >input-devices> >/input-manager-state>

Fields Summary
static final String
TAG
private final HashMap
mInputDevices
private final android.util.AtomicFile
mAtomicFile
private boolean
mLoaded
private boolean
mDirty
Constructors Summary
public PersistentDataStore()


      
        mAtomicFile = new AtomicFile(new File("/data/system/input-manager-state.xml"));
    
Methods Summary
public booleanaddKeyboardLayout(java.lang.String inputDeviceDescriptor, java.lang.String keyboardLayoutDescriptor)

        InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, true);
        if (state.addKeyboardLayout(keyboardLayoutDescriptor)) {
            setDirty();
            return true;
        }
        return false;
    
private voidclearState()

        mInputDevices.clear();
    
public java.lang.StringgetCurrentKeyboardLayout(java.lang.String inputDeviceDescriptor)

        InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, false);
        return state != null ? state.getCurrentKeyboardLayout() : null;
    
private com.android.server.input.PersistentDataStore$InputDeviceStategetInputDeviceState(java.lang.String inputDeviceDescriptor, boolean createIfAbsent)

        loadIfNeeded();
        InputDeviceState state = mInputDevices.get(inputDeviceDescriptor);
        if (state == null && createIfAbsent) {
            state = new InputDeviceState();
            mInputDevices.put(inputDeviceDescriptor, state);
            setDirty();
        }
        return state;
    
public java.lang.String[]getKeyboardLayouts(java.lang.String inputDeviceDescriptor)

        InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, false);
        if (state == null) {
            return (String[])ArrayUtils.emptyArray(String.class);
        }
        return state.getKeyboardLayouts();
    
public android.hardware.input.TouchCalibrationgetTouchCalibration(java.lang.String inputDeviceDescriptor, int surfaceRotation)

        InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, false);
        if (state == null) {
            return TouchCalibration.IDENTITY;
        }

        TouchCalibration cal = state.getTouchCalibration(surfaceRotation);
        if (cal == null) {
            return TouchCalibration.IDENTITY;
        }
        return cal;
    
private voidload()

        clearState();

        final InputStream is;
        try {
            is = mAtomicFile.openRead();
        } catch (FileNotFoundException ex) {
            return;
        }

        XmlPullParser parser;
        try {
            parser = Xml.newPullParser();
            parser.setInput(new BufferedInputStream(is), null);
            loadFromXml(parser);
        } catch (IOException ex) {
            Slog.w(InputManagerService.TAG, "Failed to load input manager persistent store data.", ex);
            clearState();
        } catch (XmlPullParserException ex) {
            Slog.w(InputManagerService.TAG, "Failed to load input manager persistent store data.", ex);
            clearState();
        } finally {
            IoUtils.closeQuietly(is);
        }
    
private voidloadFromXml(org.xmlpull.v1.XmlPullParser parser)

        XmlUtils.beginDocument(parser, "input-manager-state");
        final int outerDepth = parser.getDepth();
        while (XmlUtils.nextElementWithin(parser, outerDepth)) {
            if (parser.getName().equals("input-devices")) {
                loadInputDevicesFromXml(parser);
            }
        }
    
private voidloadIfNeeded()

        if (!mLoaded) {
            load();
            mLoaded = true;
        }
    
private voidloadInputDevicesFromXml(org.xmlpull.v1.XmlPullParser parser)

        final int outerDepth = parser.getDepth();
        while (XmlUtils.nextElementWithin(parser, outerDepth)) {
            if (parser.getName().equals("input-device")) {
                String descriptor = parser.getAttributeValue(null, "descriptor");
                if (descriptor == null) {
                    throw new XmlPullParserException(
                            "Missing descriptor attribute on input-device.");
                }
                if (mInputDevices.containsKey(descriptor)) {
                    throw new XmlPullParserException("Found duplicate input device.");
                }

                InputDeviceState state = new InputDeviceState();
                state.loadFromXml(parser);
                mInputDevices.put(descriptor, state);
            }
        }
    
public booleanremoveKeyboardLayout(java.lang.String inputDeviceDescriptor, java.lang.String keyboardLayoutDescriptor)

        InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, true);
        if (state.removeKeyboardLayout(keyboardLayoutDescriptor)) {
            setDirty();
            return true;
        }
        return false;
    
public booleanremoveUninstalledKeyboardLayouts(java.util.Set availableKeyboardLayouts)

        boolean changed = false;
        for (InputDeviceState state : mInputDevices.values()) {
            if (state.removeUninstalledKeyboardLayouts(availableKeyboardLayouts)) {
                changed = true;
            }
        }
        if (changed) {
            setDirty();
            return true;
        }
        return false;
    
private voidsave()

        final FileOutputStream os;
        try {
            os = mAtomicFile.startWrite();
            boolean success = false;
            try {
                XmlSerializer serializer = new FastXmlSerializer();
                serializer.setOutput(new BufferedOutputStream(os), "utf-8");
                saveToXml(serializer);
                serializer.flush();
                success = true;
            } finally {
                if (success) {
                    mAtomicFile.finishWrite(os);
                } else {
                    mAtomicFile.failWrite(os);
                }
            }
        } catch (IOException ex) {
            Slog.w(InputManagerService.TAG, "Failed to save input manager persistent store data.", ex);
        }
    
public voidsaveIfNeeded()

        if (mDirty) {
            save();
            mDirty = false;
        }
    
private voidsaveToXml(org.xmlpull.v1.XmlSerializer serializer)

        serializer.startDocument(null, true);
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startTag(null, "input-manager-state");
        serializer.startTag(null, "input-devices");
        for (Map.Entry<String, InputDeviceState> entry : mInputDevices.entrySet()) {
            final String descriptor = entry.getKey();
            final InputDeviceState state = entry.getValue();
            serializer.startTag(null, "input-device");
            serializer.attribute(null, "descriptor", descriptor);
            state.saveToXml(serializer);
            serializer.endTag(null, "input-device");
        }
        serializer.endTag(null, "input-devices");
        serializer.endTag(null, "input-manager-state");
        serializer.endDocument();
    
public booleansetCurrentKeyboardLayout(java.lang.String inputDeviceDescriptor, java.lang.String keyboardLayoutDescriptor)

        InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, true);
        if (state.setCurrentKeyboardLayout(keyboardLayoutDescriptor)) {
            setDirty();
            return true;
        }
        return false;
    
private voidsetDirty()

        mDirty = true;
    
public booleansetTouchCalibration(java.lang.String inputDeviceDescriptor, int surfaceRotation, android.hardware.input.TouchCalibration calibration)

        InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, true);

        if (state.setTouchCalibration(surfaceRotation, calibration)) {
            setDirty();
            return true;
        }

        return false;
    
public booleanswitchKeyboardLayout(java.lang.String inputDeviceDescriptor, int direction)

        InputDeviceState state = getInputDeviceState(inputDeviceDescriptor, false);
        if (state != null && state.switchKeyboardLayout(direction)) {
            setDirty();
            return true;
        }
        return false;