2011-02-26 12:31:56 -05:00
|
|
|
package com.fsck.k9.preferences;
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Stack;
|
|
|
|
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import javax.xml.parsers.SAXParser;
|
|
|
|
import javax.xml.parsers.SAXParserFactory;
|
|
|
|
|
|
|
|
import org.xml.sax.Attributes;
|
|
|
|
import org.xml.sax.InputSource;
|
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
import org.xml.sax.XMLReader;
|
|
|
|
import org.xml.sax.helpers.DefaultHandler;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.fsck.k9.K9;
|
|
|
|
import com.fsck.k9.Preferences;
|
2011-02-26 20:28:47 -05:00
|
|
|
import com.fsck.k9.helper.DateFormatter;
|
2011-02-26 12:31:56 -05:00
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
public class StorageImporter {
|
|
|
|
public static int importPreferences(Context context, String fileName, String encryptionKey) throws StorageImportExportException {
|
2011-03-03 11:14:19 -05:00
|
|
|
InputStream is = null;
|
2011-02-26 19:39:06 -05:00
|
|
|
try {
|
2011-03-03 11:14:19 -05:00
|
|
|
is = new FileInputStream(fileName);
|
2011-02-26 19:39:06 -05:00
|
|
|
} catch (FileNotFoundException fnfe) {
|
2011-03-03 11:14:19 -05:00
|
|
|
throw new StorageImportExportException("Failure opening settings file " + fileName, fnfe);
|
2011-02-26 12:31:56 -05:00
|
|
|
}
|
2011-03-03 11:14:19 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
int count = importPreferences(context, is, encryptionKey);
|
|
|
|
return count;
|
|
|
|
} finally {
|
|
|
|
if (is != null) {
|
|
|
|
try {
|
|
|
|
is.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.i(K9.LOG_TAG, "Unable to close InputStream for file " + fileName + ": " + e.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-26 12:31:56 -05:00
|
|
|
}
|
2011-02-26 19:39:06 -05:00
|
|
|
public static int importPreferences(Context context, InputStream is, String encryptionKey) throws StorageImportExportException {
|
|
|
|
try {
|
2011-02-26 12:31:56 -05:00
|
|
|
Preferences preferences = Preferences.getPreferences(context);
|
|
|
|
SharedPreferences storage = preferences.getPreferences();
|
|
|
|
SharedPreferences.Editor editor = storage.edit();
|
2011-02-26 19:39:06 -05:00
|
|
|
|
2011-02-26 12:31:56 -05:00
|
|
|
SAXParserFactory spf = SAXParserFactory.newInstance();
|
|
|
|
SAXParser sp = spf.newSAXParser();
|
|
|
|
XMLReader xr = sp.getXMLReader();
|
|
|
|
StorageImporterHandler handler = new StorageImporterHandler();
|
|
|
|
xr.setContentHandler(handler);
|
2011-02-26 19:39:06 -05:00
|
|
|
|
2011-02-26 12:31:56 -05:00
|
|
|
xr.parse(new InputSource(is));
|
2011-02-26 19:39:06 -05:00
|
|
|
|
2011-02-26 12:31:56 -05:00
|
|
|
Element dataset = handler.getRootElement();
|
|
|
|
String version = dataset.attributes.get("version");
|
|
|
|
Log.i(K9.LOG_TAG, "Got settings file version " + version);
|
2011-02-26 19:39:06 -05:00
|
|
|
|
2011-02-26 12:31:56 -05:00
|
|
|
IStorageImporter storageImporter = null;
|
2011-02-26 19:39:06 -05:00
|
|
|
if ("1".equals(version)) {
|
2011-02-26 12:31:56 -05:00
|
|
|
storageImporter = new StorageImporterVersion1();
|
2011-02-26 19:39:06 -05:00
|
|
|
} else {
|
|
|
|
throw new StorageImportExportException("Unable to read file of version " + version
|
|
|
|
+ "; (only version 1 is readable)");
|
2011-02-26 12:31:56 -05:00
|
|
|
}
|
|
|
|
int numAccounts = 0;
|
2011-02-26 19:39:06 -05:00
|
|
|
if (storageImporter != null) {
|
2011-02-26 12:31:56 -05:00
|
|
|
String data = dataset.data.toString();
|
|
|
|
numAccounts = storageImporter.importPreferences(preferences, editor, data, encryptionKey);
|
|
|
|
}
|
|
|
|
editor.commit();
|
|
|
|
Preferences.getPreferences(context).refreshAccounts();
|
2011-02-26 20:28:47 -05:00
|
|
|
DateFormatter.clearChosenFormat();
|
2011-02-26 19:39:06 -05:00
|
|
|
K9.loadPrefs(Preferences.getPreferences(context));
|
2011-02-26 12:31:56 -05:00
|
|
|
return numAccounts;
|
2011-02-26 19:39:06 -05:00
|
|
|
} catch (SAXException se) {
|
2011-02-26 12:31:56 -05:00
|
|
|
throw new StorageImportExportException("Failure reading settings file", se);
|
2011-02-26 19:39:06 -05:00
|
|
|
} catch (IOException ie) {
|
2011-02-26 12:31:56 -05:00
|
|
|
throw new StorageImportExportException("Failure reading settings file", ie);
|
2011-02-26 19:39:06 -05:00
|
|
|
} catch (ParserConfigurationException pce) {
|
2011-02-26 12:31:56 -05:00
|
|
|
throw new StorageImportExportException("Failure reading settings file", pce);
|
|
|
|
}
|
|
|
|
}
|
2011-02-26 19:39:06 -05:00
|
|
|
|
|
|
|
private static class Element {
|
2011-02-26 12:31:56 -05:00
|
|
|
String name;
|
|
|
|
Map<String, String> attributes = new HashMap<String, String>();
|
|
|
|
Map<String, Element> subElements = new HashMap<String, Element>();
|
|
|
|
StringBuilder data = new StringBuilder();
|
|
|
|
}
|
2011-02-26 19:39:06 -05:00
|
|
|
|
|
|
|
private static class StorageImporterHandler extends DefaultHandler {
|
2011-02-26 12:31:56 -05:00
|
|
|
private Element rootElement = new Element();
|
|
|
|
private Stack<Element> mOpenTags = new Stack<Element>();
|
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
public Element getRootElement() {
|
2011-02-26 12:31:56 -05:00
|
|
|
return this.rootElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-26 19:39:06 -05:00
|
|
|
public void startDocument() throws SAXException {
|
2011-02-26 12:31:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-26 19:39:06 -05:00
|
|
|
public void endDocument() throws SAXException {
|
2011-02-26 12:31:56 -05:00
|
|
|
/* Do nothing */
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void startElement(String namespaceURI, String localName,
|
2011-02-26 19:39:06 -05:00
|
|
|
String qName, Attributes attributes) throws SAXException {
|
2011-02-26 12:31:56 -05:00
|
|
|
Log.i(K9.LOG_TAG, "Starting element " + localName);
|
|
|
|
Element element = new Element();
|
|
|
|
element.name = localName;
|
|
|
|
mOpenTags.push(element);
|
2011-02-26 19:39:06 -05:00
|
|
|
for (int i = 0; i < attributes.getLength(); i++) {
|
2011-02-26 12:31:56 -05:00
|
|
|
String key = attributes.getLocalName(i);
|
|
|
|
String value = attributes.getValue(i);
|
|
|
|
Log.i(K9.LOG_TAG, "Got attribute " + key + " = " + value);
|
|
|
|
element.attributes.put(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-26 19:39:06 -05:00
|
|
|
public void endElement(String namespaceURI, String localName, String qName) {
|
2011-02-26 12:31:56 -05:00
|
|
|
Log.i(K9.LOG_TAG, "Ending element " + localName);
|
|
|
|
Element element = mOpenTags.pop();
|
|
|
|
Element superElement = mOpenTags.empty() ? null : mOpenTags.peek();
|
2011-02-26 19:39:06 -05:00
|
|
|
if (superElement != null) {
|
2011-02-26 12:31:56 -05:00
|
|
|
superElement.subElements.put(element.name, element);
|
2011-02-26 19:39:06 -05:00
|
|
|
} else {
|
2011-02-26 12:31:56 -05:00
|
|
|
rootElement = element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-26 19:39:06 -05:00
|
|
|
public void characters(char ch[], int start, int length) {
|
2011-02-26 12:31:56 -05:00
|
|
|
String value = new String(ch, start, length);
|
|
|
|
mOpenTags.peek().data.append(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|