@Test(expected = SettingsImportExportException.class) public void importSettings_throwsExceptionOnInvalidVersion() throws SettingsImportExportException { InputStream inputStream = new StringInputStream("<k9settings format=\"1\" version=\"A\"></k9settings>"); List<String> accountUuids = new ArrayList<>(); SettingsImporter.importSettings(RuntimeEnvironment.application, inputStream, true, accountUuids, true); }
public static ImportResults importSettings(Context context, InputStream inputStream, boolean globalSettings, List<String> accountUuids, boolean overwrite) throws SettingsImportExportException { try { boolean globalSettingsImported = false; List<AccountDescriptionPair> importedAccounts = new ArrayList<>(); List<AccountDescription> erroneousAccounts = new ArrayList<>(); Imported imported = parseSettings(inputStream, globalSettings, accountUuids, false); Preferences preferences = Preferences.getPreferences(context); Storage storage = preferences.getStorage(); if (globalSettings) { try { StorageEditor editor = storage.edit(); if (imported.globalSettings != null) { importGlobalSettings(storage, editor, imported.contentVersion, imported.globalSettings); } else { Timber.w("Was asked to import global settings but none found."); } if (editor.commit()) { Timber.v("Committed global settings to the preference storage."); globalSettingsImported = true; } else { Timber.v("Failed to commit global settings to the preference storage"); } } catch (Exception e) { Timber.e(e, "Exception while importing global settings"); } } if (accountUuids != null && accountUuids.size() > 0) { if (imported.accounts != null) { for (String accountUuid : accountUuids) { if (imported.accounts.containsKey(accountUuid)) { ImportedAccount account = imported.accounts.get(accountUuid); try { StorageEditor editor = storage.edit(); AccountDescriptionPair importResult = importAccount(context, editor, imported.contentVersion, account, overwrite); if (editor.commit()) { Timber.v("Committed settings for account \"%s\" to the settings database.", importResult.imported.name); if (!importResult.overwritten) { editor = storage.edit(); String newUuid = importResult.imported.uuid; String oldAccountUuids = storage.getString("accountUuids", ""); String newAccountUuids = (oldAccountUuids.length() > 0) ? oldAccountUuids + "," + newUuid : newUuid; putString(editor, "accountUuids", newAccountUuids); if (!editor.commit()) { throw new SettingsImportExportException("Failed to set account UUID list"); } } preferences.loadAccounts(); importedAccounts.add(importResult); } else { Timber.w("Error while committing settings for account \"%s\" to the settings " + "database.", importResult.original.name); erroneousAccounts.add(importResult.original); } } catch (InvalidSettingValueException e) { Timber.e(e, "Encountered invalid setting while importing account \"%s\"", account.name); erroneousAccounts.add(new AccountDescription(account.name, account.uuid)); } catch (Exception e) { Timber.e(e, "Exception while importing account \"%s\"", account.name); erroneousAccounts.add(new AccountDescription(account.name, account.uuid)); } } else { Timber.w("Was asked to import account with UUID %s. But this account wasn't found.", accountUuid); } } StorageEditor editor = storage.edit(); String defaultAccountUuid = storage.getString("defaultAccountUuid", null); if (defaultAccountUuid == null) { putString(editor, "defaultAccountUuid", accountUuids.get(0)); } if (!editor.commit()) { throw new SettingsImportExportException("Failed to set default account"); } } else { Timber.w("Was asked to import at least one account but none found."); } } preferences.loadAccounts(); QMail.loadPrefs(preferences); QMail.setServicesEnabled(context); return new ImportResults(globalSettingsImported, importedAccounts, erroneousAccounts); } catch (SettingsImportExportException e) { throw e; } catch (Exception e) { throw new SettingsImportExportException(e); } }
SettingsImporter { public static ImportResults importSettings(Context context, InputStream inputStream, boolean globalSettings, List<String> accountUuids, boolean overwrite) throws SettingsImportExportException { try { boolean globalSettingsImported = false; List<AccountDescriptionPair> importedAccounts = new ArrayList<>(); List<AccountDescription> erroneousAccounts = new ArrayList<>(); Imported imported = parseSettings(inputStream, globalSettings, accountUuids, false); Preferences preferences = Preferences.getPreferences(context); Storage storage = preferences.getStorage(); if (globalSettings) { try { StorageEditor editor = storage.edit(); if (imported.globalSettings != null) { importGlobalSettings(storage, editor, imported.contentVersion, imported.globalSettings); } else { Timber.w("Was asked to import global settings but none found."); } if (editor.commit()) { Timber.v("Committed global settings to the preference storage."); globalSettingsImported = true; } else { Timber.v("Failed to commit global settings to the preference storage"); } } catch (Exception e) { Timber.e(e, "Exception while importing global settings"); } } if (accountUuids != null && accountUuids.size() > 0) { if (imported.accounts != null) { for (String accountUuid : accountUuids) { if (imported.accounts.containsKey(accountUuid)) { ImportedAccount account = imported.accounts.get(accountUuid); try { StorageEditor editor = storage.edit(); AccountDescriptionPair importResult = importAccount(context, editor, imported.contentVersion, account, overwrite); if (editor.commit()) { Timber.v("Committed settings for account \"%s\" to the settings database.", importResult.imported.name); if (!importResult.overwritten) { editor = storage.edit(); String newUuid = importResult.imported.uuid; String oldAccountUuids = storage.getString("accountUuids", ""); String newAccountUuids = (oldAccountUuids.length() > 0) ? oldAccountUuids + "," + newUuid : newUuid; putString(editor, "accountUuids", newAccountUuids); if (!editor.commit()) { throw new SettingsImportExportException("Failed to set account UUID list"); } } preferences.loadAccounts(); importedAccounts.add(importResult); } else { Timber.w("Error while committing settings for account \"%s\" to the settings " + "database.", importResult.original.name); erroneousAccounts.add(importResult.original); } } catch (InvalidSettingValueException e) { Timber.e(e, "Encountered invalid setting while importing account \"%s\"", account.name); erroneousAccounts.add(new AccountDescription(account.name, account.uuid)); } catch (Exception e) { Timber.e(e, "Exception while importing account \"%s\"", account.name); erroneousAccounts.add(new AccountDescription(account.name, account.uuid)); } } else { Timber.w("Was asked to import account with UUID %s. But this account wasn't found.", accountUuid); } } StorageEditor editor = storage.edit(); String defaultAccountUuid = storage.getString("defaultAccountUuid", null); if (defaultAccountUuid == null) { putString(editor, "defaultAccountUuid", accountUuids.get(0)); } if (!editor.commit()) { throw new SettingsImportExportException("Failed to set default account"); } } else { Timber.w("Was asked to import at least one account but none found."); } } preferences.loadAccounts(); QMail.loadPrefs(preferences); QMail.setServicesEnabled(context); return new ImportResults(globalSettingsImported, importedAccounts, erroneousAccounts); } catch (SettingsImportExportException e) { throw e; } catch (Exception e) { throw new SettingsImportExportException(e); } } }
SettingsImporter { public static ImportResults importSettings(Context context, InputStream inputStream, boolean globalSettings, List<String> accountUuids, boolean overwrite) throws SettingsImportExportException { try { boolean globalSettingsImported = false; List<AccountDescriptionPair> importedAccounts = new ArrayList<>(); List<AccountDescription> erroneousAccounts = new ArrayList<>(); Imported imported = parseSettings(inputStream, globalSettings, accountUuids, false); Preferences preferences = Preferences.getPreferences(context); Storage storage = preferences.getStorage(); if (globalSettings) { try { StorageEditor editor = storage.edit(); if (imported.globalSettings != null) { importGlobalSettings(storage, editor, imported.contentVersion, imported.globalSettings); } else { Timber.w("Was asked to import global settings but none found."); } if (editor.commit()) { Timber.v("Committed global settings to the preference storage."); globalSettingsImported = true; } else { Timber.v("Failed to commit global settings to the preference storage"); } } catch (Exception e) { Timber.e(e, "Exception while importing global settings"); } } if (accountUuids != null && accountUuids.size() > 0) { if (imported.accounts != null) { for (String accountUuid : accountUuids) { if (imported.accounts.containsKey(accountUuid)) { ImportedAccount account = imported.accounts.get(accountUuid); try { StorageEditor editor = storage.edit(); AccountDescriptionPair importResult = importAccount(context, editor, imported.contentVersion, account, overwrite); if (editor.commit()) { Timber.v("Committed settings for account \"%s\" to the settings database.", importResult.imported.name); if (!importResult.overwritten) { editor = storage.edit(); String newUuid = importResult.imported.uuid; String oldAccountUuids = storage.getString("accountUuids", ""); String newAccountUuids = (oldAccountUuids.length() > 0) ? oldAccountUuids + "," + newUuid : newUuid; putString(editor, "accountUuids", newAccountUuids); if (!editor.commit()) { throw new SettingsImportExportException("Failed to set account UUID list"); } } preferences.loadAccounts(); importedAccounts.add(importResult); } else { Timber.w("Error while committing settings for account \"%s\" to the settings " + "database.", importResult.original.name); erroneousAccounts.add(importResult.original); } } catch (InvalidSettingValueException e) { Timber.e(e, "Encountered invalid setting while importing account \"%s\"", account.name); erroneousAccounts.add(new AccountDescription(account.name, account.uuid)); } catch (Exception e) { Timber.e(e, "Exception while importing account \"%s\"", account.name); erroneousAccounts.add(new AccountDescription(account.name, account.uuid)); } } else { Timber.w("Was asked to import account with UUID %s. But this account wasn't found.", accountUuid); } } StorageEditor editor = storage.edit(); String defaultAccountUuid = storage.getString("defaultAccountUuid", null); if (defaultAccountUuid == null) { putString(editor, "defaultAccountUuid", accountUuids.get(0)); } if (!editor.commit()) { throw new SettingsImportExportException("Failed to set default account"); } } else { Timber.w("Was asked to import at least one account but none found."); } } preferences.loadAccounts(); QMail.loadPrefs(preferences); QMail.setServicesEnabled(context); return new ImportResults(globalSettingsImported, importedAccounts, erroneousAccounts); } catch (SettingsImportExportException e) { throw e; } catch (Exception e) { throw new SettingsImportExportException(e); } }  }
SettingsImporter { public static ImportResults importSettings(Context context, InputStream inputStream, boolean globalSettings, List<String> accountUuids, boolean overwrite) throws SettingsImportExportException { try { boolean globalSettingsImported = false; List<AccountDescriptionPair> importedAccounts = new ArrayList<>(); List<AccountDescription> erroneousAccounts = new ArrayList<>(); Imported imported = parseSettings(inputStream, globalSettings, accountUuids, false); Preferences preferences = Preferences.getPreferences(context); Storage storage = preferences.getStorage(); if (globalSettings) { try { StorageEditor editor = storage.edit(); if (imported.globalSettings != null) { importGlobalSettings(storage, editor, imported.contentVersion, imported.globalSettings); } else { Timber.w("Was asked to import global settings but none found."); } if (editor.commit()) { Timber.v("Committed global settings to the preference storage."); globalSettingsImported = true; } else { Timber.v("Failed to commit global settings to the preference storage"); } } catch (Exception e) { Timber.e(e, "Exception while importing global settings"); } } if (accountUuids != null && accountUuids.size() > 0) { if (imported.accounts != null) { for (String accountUuid : accountUuids) { if (imported.accounts.containsKey(accountUuid)) { ImportedAccount account = imported.accounts.get(accountUuid); try { StorageEditor editor = storage.edit(); AccountDescriptionPair importResult = importAccount(context, editor, imported.contentVersion, account, overwrite); if (editor.commit()) { Timber.v("Committed settings for account \"%s\" to the settings database.", importResult.imported.name); if (!importResult.overwritten) { editor = storage.edit(); String newUuid = importResult.imported.uuid; String oldAccountUuids = storage.getString("accountUuids", ""); String newAccountUuids = (oldAccountUuids.length() > 0) ? oldAccountUuids + "," + newUuid : newUuid; putString(editor, "accountUuids", newAccountUuids); if (!editor.commit()) { throw new SettingsImportExportException("Failed to set account UUID list"); } } preferences.loadAccounts(); importedAccounts.add(importResult); } else { Timber.w("Error while committing settings for account \"%s\" to the settings " + "database.", importResult.original.name); erroneousAccounts.add(importResult.original); } } catch (InvalidSettingValueException e) { Timber.e(e, "Encountered invalid setting while importing account \"%s\"", account.name); erroneousAccounts.add(new AccountDescription(account.name, account.uuid)); } catch (Exception e) { Timber.e(e, "Exception while importing account \"%s\"", account.name); erroneousAccounts.add(new AccountDescription(account.name, account.uuid)); } } else { Timber.w("Was asked to import account with UUID %s. But this account wasn't found.", accountUuid); } } StorageEditor editor = storage.edit(); String defaultAccountUuid = storage.getString("defaultAccountUuid", null); if (defaultAccountUuid == null) { putString(editor, "defaultAccountUuid", accountUuids.get(0)); } if (!editor.commit()) { throw new SettingsImportExportException("Failed to set default account"); } } else { Timber.w("Was asked to import at least one account but none found."); } } preferences.loadAccounts(); QMail.loadPrefs(preferences); QMail.setServicesEnabled(context); return new ImportResults(globalSettingsImported, importedAccounts, erroneousAccounts); } catch (SettingsImportExportException e) { throw e; } catch (Exception e) { throw new SettingsImportExportException(e); } }  static ImportContents getImportStreamContents(InputStream inputStream); static ImportResults importSettings(Context context, InputStream inputStream, boolean globalSettings,
            List<String> accountUuids, boolean overwrite); }
SettingsImporter { public static ImportResults importSettings(Context context, InputStream inputStream, boolean globalSettings, List<String> accountUuids, boolean overwrite) throws SettingsImportExportException { try { boolean globalSettingsImported = false; List<AccountDescriptionPair> importedAccounts = new ArrayList<>(); List<AccountDescription> erroneousAccounts = new ArrayList<>(); Imported imported = parseSettings(inputStream, globalSettings, accountUuids, false); Preferences preferences = Preferences.getPreferences(context); Storage storage = preferences.getStorage(); if (globalSettings) { try { StorageEditor editor = storage.edit(); if (imported.globalSettings != null) { importGlobalSettings(storage, editor, imported.contentVersion, imported.globalSettings); } else { Timber.w("Was asked to import global settings but none found."); } if (editor.commit()) { Timber.v("Committed global settings to the preference storage."); globalSettingsImported = true; } else { Timber.v("Failed to commit global settings to the preference storage"); } } catch (Exception e) { Timber.e(e, "Exception while importing global settings"); } } if (accountUuids != null && accountUuids.size() > 0) { if (imported.accounts != null) { for (String accountUuid : accountUuids) { if (imported.accounts.containsKey(accountUuid)) { ImportedAccount account = imported.accounts.get(accountUuid); try { StorageEditor editor = storage.edit(); AccountDescriptionPair importResult = importAccount(context, editor, imported.contentVersion, account, overwrite); if (editor.commit()) { Timber.v("Committed settings for account \"%s\" to the settings database.", importResult.imported.name); if (!importResult.overwritten) { editor = storage.edit(); String newUuid = importResult.imported.uuid; String oldAccountUuids = storage.getString("accountUuids", ""); String newAccountUuids = (oldAccountUuids.length() > 0) ? oldAccountUuids + "," + newUuid : newUuid; putString(editor, "accountUuids", newAccountUuids); if (!editor.commit()) { throw new SettingsImportExportException("Failed to set account UUID list"); } } preferences.loadAccounts(); importedAccounts.add(importResult); } else { Timber.w("Error while committing settings for account \"%s\" to the settings " + "database.", importResult.original.name); erroneousAccounts.add(importResult.original); } } catch (InvalidSettingValueException e) { Timber.e(e, "Encountered invalid setting while importing account \"%s\"", account.name); erroneousAccounts.add(new AccountDescription(account.name, account.uuid)); } catch (Exception e) { Timber.e(e, "Exception while importing account \"%s\"", account.name); erroneousAccounts.add(new AccountDescription(account.name, account.uuid)); } } else { Timber.w("Was asked to import account with UUID %s. But this account wasn't found.", accountUuid); } } StorageEditor editor = storage.edit(); String defaultAccountUuid = storage.getString("defaultAccountUuid", null); if (defaultAccountUuid == null) { putString(editor, "defaultAccountUuid", accountUuids.get(0)); } if (!editor.commit()) { throw new SettingsImportExportException("Failed to set default account"); } } else { Timber.w("Was asked to import at least one account but none found."); } } preferences.loadAccounts(); QMail.loadPrefs(preferences); QMail.setServicesEnabled(context); return new ImportResults(globalSettingsImported, importedAccounts, erroneousAccounts); } catch (SettingsImportExportException e) { throw e; } catch (Exception e) { throw new SettingsImportExportException(e); } }  static ImportContents getImportStreamContents(InputStream inputStream); static ImportResults importSettings(Context context, InputStream inputStream, boolean globalSettings,
            List<String> accountUuids, boolean overwrite);  }