From 922487676888844fd28ef96e11133f1af83bbae8 Mon Sep 17 00:00:00 2001 From: danapple Date: Thu, 3 Mar 2011 10:00:58 -0600 Subject: [PATCH] Make exporter usable by other things handling OutputStreams, like a facility to use the Android-wide Backup Service. --- .../fsck/k9/preferences/StorageExporter.java | 55 +++++++++++++------ 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/src/com/fsck/k9/preferences/StorageExporter.java b/src/com/fsck/k9/preferences/StorageExporter.java index 5a41042b7..a70f37caf 100644 --- a/src/com/fsck/k9/preferences/StorageExporter.java +++ b/src/com/fsck/k9/preferences/StorageExporter.java @@ -1,39 +1,62 @@ package com.fsck.k9.preferences; import java.io.File; -import java.io.IOException; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.HashSet; import java.util.Map; import java.util.Set; -import org.apache.commons.codec.binary.Base64; +import android.content.Context; +import android.content.SharedPreferences; +import android.util.Log; import com.fsck.k9.Account; import com.fsck.k9.K9; import com.fsck.k9.Preferences; -import android.content.Context; -import android.content.SharedPreferences; -import android.util.Log; - public class StorageExporter { - //public static String VALIDITY = "K-9MailExport"; // Does outputting a fixed string in a known location make the encrypted data easier to break? public static void exportPreferences(Context context, String uuid, String fileName, String encryptionKey) throws StorageImportExportException { + + Log.i(K9.LOG_TAG, "Exporting preferences for account " + uuid + " to file " + fileName); + File outFile = new File(fileName); + OutputStream os = null; try { + os = new FileOutputStream(outFile); + } catch (FileNotFoundException e) { + throw new StorageImportExportException("Unable to export settings", e); + } + + try { + exportPrefererences(context, uuid, os, encryptionKey); + } finally { + if (os != null) { + try { + os.close(); + } catch (Exception e) { + Log.i(K9.LOG_TAG, "Unable to close OutputStream for file " + fileName + ": " + e.getLocalizedMessage()); + } + } + } + + Log.i(K9.LOG_TAG, "Exported preferences for account " + uuid + " to file " + fileName + " which is size " + outFile.length()); + } + + public static void exportPrefererences(Context context, String uuid, OutputStream os, String encryptionKey) throws StorageImportExportException { + try { + Log.i(K9.LOG_TAG, "Exporting preferences for account " + uuid + " to OutputStream"); K9Krypto krypto = new K9Krypto(encryptionKey, K9Krypto.MODE.ENCRYPT); - File outFile = new File(fileName); - PrintWriter pf = new PrintWriter(outFile); + OutputStreamWriter sw = new OutputStreamWriter(os); + PrintWriter pf = new PrintWriter(sw); long keysEvaluated = 0; long keysExported = 0; pf.println(""); - // String testval = SimpleCrypto.encrypt(encryptionKey, VALIDITY); - pf.print(""); - Log.i(K9.LOG_TAG, "Exporting preferences for account " + uuid + " to file " + fileName); Preferences preferences = Preferences.getPreferences(context); SharedPreferences storage = preferences.getPreferences(); @@ -78,12 +101,10 @@ public class StorageExporter { } pf.println(""); - pf.close(); + pf.flush(); Log.i(K9.LOG_TAG, "Exported " + keysExported + " settings of " + keysEvaluated - + " total for preferences for account " + uuid + " to file " + fileName + " which is size " + outFile.length()); - } catch (IOException ie) { - throw new StorageImportExportException("Unable to export settings", ie); + + " total for preferences for account " + uuid); } catch (Exception e) { throw new StorageImportExportException("Unable to encrypt settings", e); }