2011-02-26 12:31:56 -05:00
|
|
|
package com.fsck.k9.preferences;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Stack;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-03-20 12:52:13 -04:00
|
|
|
import android.app.Activity;
|
2011-02-26 12:31:56 -05:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.fsck.k9.K9;
|
|
|
|
import com.fsck.k9.Preferences;
|
2011-03-20 12:52:13 -04:00
|
|
|
import com.fsck.k9.R;
|
|
|
|
import com.fsck.k9.activity.AsyncUIProcessor;
|
|
|
|
import com.fsck.k9.activity.ImportListener;
|
|
|
|
import com.fsck.k9.activity.PasswordEntryDialog;
|
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 {
|
2011-03-03 11:14:19 -05:00
|
|
|
|
2011-03-20 12:52:13 -04:00
|
|
|
public static void importPreferences(Activity activity, InputStream is, String providedEncryptionKey, ImportListener listener) {
|
2011-03-03 11:14:19 -05:00
|
|
|
try {
|
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-03-20 12:52:13 -04:00
|
|
|
ImportElement dataset = handler.getRootElement();
|
2011-02-26 12:31:56 -05:00
|
|
|
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-03-20 12:52:13 -04:00
|
|
|
IStorageImporter storageImporter = StorageVersioning.createImporter(version);
|
|
|
|
if (storageImporter == null)
|
|
|
|
{
|
|
|
|
throw new StorageImportExportException(activity.getString(R.string.settings_unknown_version, version));
|
2011-02-26 12:31:56 -05:00
|
|
|
}
|
2011-03-20 13:01:29 -04:00
|
|
|
if (storageImporter.needsKey() && providedEncryptionKey == null) {
|
|
|
|
gatherPassword(activity, storageImporter, dataset, listener);
|
2011-02-26 12:31:56 -05:00
|
|
|
}
|
2011-03-20 12:52:13 -04:00
|
|
|
else {
|
2011-03-20 13:01:29 -04:00
|
|
|
finishImport(activity, storageImporter, dataset, providedEncryptionKey, listener);
|
2011-03-20 12:52:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
if (listener != null) {
|
|
|
|
listener.failure(e.getLocalizedMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void finishImport(Activity context, IStorageImporter storageImporter, ImportElement dataset, String encryptionKey, ImportListener listener) throws StorageImportExportException {
|
|
|
|
if (listener != null) {
|
|
|
|
listener.started();
|
|
|
|
}
|
|
|
|
Preferences preferences = Preferences.getPreferences(context);
|
|
|
|
SharedPreferences storage = preferences.getPreferences();
|
|
|
|
SharedPreferences.Editor editor = storage.edit();
|
|
|
|
int numAccounts = 0;
|
|
|
|
if (storageImporter != null) {
|
|
|
|
numAccounts = storageImporter.importPreferences(preferences, editor, dataset, encryptionKey);
|
|
|
|
}
|
|
|
|
editor.commit();
|
|
|
|
Preferences.getPreferences(context).refreshAccounts();
|
|
|
|
DateFormatter.clearChosenFormat();
|
|
|
|
K9.loadPrefs(Preferences.getPreferences(context));
|
|
|
|
K9.setServicesEnabled(context);
|
|
|
|
if (listener != null) {
|
|
|
|
listener.success(numAccounts);
|
2011-02-26 12:31:56 -05:00
|
|
|
}
|
|
|
|
}
|
2011-03-20 12:52:13 -04:00
|
|
|
|
|
|
|
private static void gatherPassword(final Activity activity, final IStorageImporter storageImporter, final ImportElement dataset, final ImportListener listener) {
|
|
|
|
activity.runOnUiThread(new Runnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
PasswordEntryDialog dialog = new PasswordEntryDialog(activity, activity.getString(R.string.settings_encryption_password_prompt),
|
|
|
|
new PasswordEntryDialog.PasswordEntryListener() {
|
|
|
|
public void passwordChosen(final String chosenPassword) {
|
|
|
|
AsyncUIProcessor.getInstance(activity.getApplication()).execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
finishImport(activity, storageImporter, dataset, chosenPassword, listener);
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
Log.w(K9.LOG_TAG, "Failure during import", e);
|
|
|
|
if (listener != null) {
|
|
|
|
listener.failure(e.getLocalizedMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancel() {
|
|
|
|
if (listener != null) {
|
|
|
|
listener.canceled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
dialog.show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-02-26 19:39:06 -05:00
|
|
|
|
2011-03-20 12:52:13 -04:00
|
|
|
public static class ImportElement {
|
2011-02-26 12:31:56 -05:00
|
|
|
String name;
|
|
|
|
Map<String, String> attributes = new HashMap<String, String>();
|
2011-03-20 12:52:13 -04:00
|
|
|
Map<String, ImportElement> subElements = new HashMap<String, ImportElement>();
|
2011-02-26 12:31:56 -05:00
|
|
|
StringBuilder data = new StringBuilder();
|
|
|
|
}
|
2011-02-26 19:39:06 -05:00
|
|
|
|
|
|
|
private static class StorageImporterHandler extends DefaultHandler {
|
2011-03-20 12:52:13 -04:00
|
|
|
private ImportElement rootElement = new ImportElement();
|
|
|
|
private Stack<ImportElement> mOpenTags = new Stack<ImportElement>();
|
2011-02-26 12:31:56 -05:00
|
|
|
|
2011-03-20 12:52:13 -04:00
|
|
|
public ImportElement 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);
|
2011-03-20 12:52:13 -04:00
|
|
|
ImportElement element = new ImportElement();
|
2011-02-26 12:31:56 -05:00
|
|
|
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);
|
2011-03-20 12:52:13 -04:00
|
|
|
ImportElement element = mOpenTags.pop();
|
|
|
|
ImportElement 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|