public static void exportToUri(Context context, boolean includeGlobals, Set<String> accountUuids, Uri uri) throws SettingsImportExportException { OutputStream os = null; try { os = context.getContentResolver().openOutputStream(uri); exportPreferences(context, os, includeGlobals, accountUuids); } catch (Exception e) { throw new SettingsImportExportException(e); } finally { closeOrThrow(os); } }
@Override protected Boolean doInBackground(Void... params) { try { SettingsExporter.exportToUri(mContext, mIncludeGlobals, mAccountUuids, mUri); } catch (SettingsImportExportException e) { Timber.w(e, "Exception during export"); return false; } return true; }
public void onExport(final boolean includeGlobals, final Account account) { // TODO, prompt to allow a user to choose which accounts to export ArrayList<String> accountUuids = null; if (account != null) { accountUuids = new ArrayList<>(); accountUuids.add(account.getUuid()); } exportGlobalSettings = includeGlobals; exportAccountUuids = accountUuids; Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); intent.setType("application/octet-stream"); intent.putExtra(Intent.EXTRA_TITLE, SettingsExporter.generateDatedExportFileName()); intent.addCategory(Intent.CATEGORY_OPENABLE); startActivityForResult(intent, ACTIVITY_REQUEST_SAVE_SETTINGS_FILE); }
private static void writeSettings(XmlSerializer serializer, Map<String, Object> prefs) throws IOException { for (Entry<String, TreeMap<Integer, SettingsDescription>> versionedSetting : GlobalSettings.SETTINGS.entrySet()) { String key = versionedSetting.getKey(); String valueString = (String) prefs.get(key); TreeMap<Integer, SettingsDescription> versions = versionedSetting.getValue(); Integer highestVersion = versions.lastKey(); SettingsDescription setting = versions.get(highestVersion); if (setting == null) { // Setting was removed. continue; } if (valueString != null) { try { writeKeyAndPrettyValueFromSetting(serializer, key, setting, valueString); } catch (InvalidSettingValueException e) { Timber.w("Global setting \"%s\" has invalid value \"%s\" in preference storage. " + "This shouldn't happen!", key, valueString); } } else { Timber.d("Couldn't find key \"%s\" in preference storage. Using default value.", key); writeKeyAndDefaultValueFromSetting(serializer, key, setting); } } }
writeElement(serializer, HOST_ELEMENT, incoming.host); if (incoming.port != -1) { writeElement(serializer, PORT_ELEMENT, Integer.toString(incoming.port)); writeElement(serializer, CONNECTION_SECURITY_ELEMENT, incoming.connectionSecurity.name()); writeElement(serializer, AUTHENTICATION_TYPE_ELEMENT, incoming.authenticationType.name()); writeElement(serializer, USERNAME_ELEMENT, incoming.username); writeElement(serializer, CLIENT_CERTIFICATE_ALIAS_ELEMENT, incoming.clientCertificateAlias); serializer.startTag(null, EXTRA_ELEMENT); for (Entry<String, String> extra : extras.entrySet()) { writeKeyAndPrettyValueFromSetting(serializer, extra.getKey(), extra.getValue()); writeElement(serializer, HOST_ELEMENT, outgoing.host); if (outgoing.port != -1) { writeElement(serializer, PORT_ELEMENT, Integer.toString(outgoing.port)); writeElement(serializer, CONNECTION_SECURITY_ELEMENT, outgoing.connectionSecurity.name()); writeElement(serializer, AUTHENTICATION_TYPE_ELEMENT, outgoing.authenticationType.name()); writeElement(serializer, USERNAME_ELEMENT, outgoing.username); writeElement(serializer, CLIENT_CERTIFICATE_ALIAS_ELEMENT, outgoing.clientCertificateAlias); serializer.startTag(null, EXTRA_ELEMENT); for (Entry<String, String> extra : extras.entrySet()) { writeKeyAndPrettyValueFromSetting(serializer, extra.getKey(), extra.getValue());
private Document exportPreferences(boolean globalSettings, Set<String> accounts) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); SettingsExporter.exportPreferences(RuntimeEnvironment.application, outputStream, globalSettings, accounts); Document document = parseXML(outputStream.toByteArray()); outputStream.close(); return document; }
writeSettings(serializer, prefs); serializer.endTag(null, GLOBAL_ELEMENT); for (String accountUuid : exportAccounts) { Account account = preferences.getAccount(accountUuid); writeAccount(serializer, account, prefs);
private static <T> void writeKeyAndDefaultValueFromSetting(XmlSerializer serializer, String key, SettingsDescription<T> setting) throws IllegalArgumentException, IllegalStateException, IOException { T value = setting.getDefaultValue(); String outputValue = setting.toPrettyString(value); writeKeyAndPrettyValueFromSetting(serializer, key, outputValue); }
private static <T> void writeKeyAndPrettyValueFromSetting(XmlSerializer serializer, String key, SettingsDescription<T> setting, String valueString) throws IllegalArgumentException, IllegalStateException, IOException, InvalidSettingValueException { T value = setting.fromString(valueString); String outputValue = setting.toPrettyString(value); writeKeyAndPrettyValueFromSetting(serializer, key, outputValue); }
writeKeyAndPrettyValueFromSetting(serializer, folderKey, setting, valueString); } catch (InvalidSettingValueException e) { Timber.w("Folder setting \"%s\" has invalid value \"%s\" in preference storage. " +
writeKeyAndPrettyValueFromSetting(serializer, identityKey, setting, valueString); } catch (InvalidSettingValueException e) { Timber.w("Identity setting \"%s\" has invalid value \"%s\" in preference storage. " +