use of com.fsck.k9.mailstore.SpecialLocalFoldersCreator in project k-9 by k9mail.
the class SettingsImporter method importSettings.
/**
* Reads an import {@link InputStream} and imports the global settings and/or account
* configurations specified by the arguments.
*
* @param context
* A {@link Context} instance.
* @param inputStream
* The {@code InputStream} to read the settings from.
* @param globalSettings
* {@code true} if global settings should be imported from the file.
* @param accountUuids
* A list of UUIDs of the accounts that should be imported.
* @param overwrite
* {@code true} if existing accounts should be overwritten when an account with the
* same UUID is found in the settings file.<br>
* <strong>Note:</strong> This can have side-effects we currently don't handle, e.g.
* changing the account type from IMAP to POP3. So don't use this for now!
* @return An {@link ImportResults} instance containing information about errors and
* successfully imported accounts.
*
* @throws SettingsImportExportException
* In case of an error.
*/
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 = preferences.createStorageEditor();
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 = preferences.createStorageEditor();
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);
// account UUIDs
if (!importResult.overwritten) {
editor = preferences.createStorageEditor();
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");
}
}
// Reload accounts
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 = preferences.createStorageEditor();
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();
SpecialLocalFoldersCreator localFoldersCreator = DI.get(SpecialLocalFoldersCreator.class);
// Create special local folders
for (AccountDescriptionPair importedAccount : importedAccounts) {
String accountUuid = importedAccount.imported.uuid;
Account account = preferences.getAccount(accountUuid);
localFoldersCreator.createSpecialLocalFolders(account);
}
DI.get(RealGeneralSettingsManager.class).loadSettings();
Core.setServicesEnabled(context);
return new ImportResults(globalSettingsImported, importedAccounts, erroneousAccounts);
} catch (SettingsImportExportException e) {
throw e;
} catch (Exception e) {
throw new SettingsImportExportException(e);
}
}
Aggregations