Changed settings export to use the storage access framework

This commit is contained in:
GoneUp 2017-02-18 00:22:23 +01:00 committed by cketti
parent 0252db0992
commit 17ebb4c613
3 changed files with 87 additions and 15 deletions

View file

@ -146,6 +146,7 @@ public class Accounts extends K9ListActivity implements OnItemClickListener {
private static final int ACTIVITY_REQUEST_PICK_SETTINGS_FILE = 1; private static final int ACTIVITY_REQUEST_PICK_SETTINGS_FILE = 1;
private static final int ACTIVITY_REQUEST_SAVE_SETTINGS_FILE = 2;
class AccountsHandler extends Handler { class AccountsHandler extends Handler {
private void setViewTitle() { private void setViewTitle() {
@ -1290,7 +1291,7 @@ public class Accounts extends K9ListActivity implements OnItemClickListener {
getString(R.string.app_revision_url) + getString(R.string.app_revision_url) +
"</a>")) "</a>"))
.append("</p><hr/><p>") .append("</p><hr/><p>")
.append(String.format(getString(R.string.app_copyright_fmt), year, year)) .append(String.format(getString(R.string.app_copyright_fmt), Integer.toString(year), Integer.toString(year)))
.append("</p><hr/><p>") .append("</p><hr/><p>")
.append(getString(R.string.app_license)) .append(getString(R.string.app_license))
.append("</p><hr/><p>"); .append("</p><hr/><p>");
@ -1421,6 +1422,9 @@ public class Accounts extends K9ListActivity implements OnItemClickListener {
case ACTIVITY_REQUEST_PICK_SETTINGS_FILE: case ACTIVITY_REQUEST_PICK_SETTINGS_FILE:
onImport(data.getData()); onImport(data.getData());
break; break;
case ACTIVITY_REQUEST_SAVE_SETTINGS_FILE:
onExport(data);
break;
} }
} }
@ -1883,16 +1887,47 @@ public class Accounts extends K9ListActivity implements OnItemClickListener {
} }
public void onExport(final boolean includeGlobals, final Account account) { public static final String EXTRA_INC_GLOBALS = "include_globals";
public static final String EXTRA_ACCOUNTS = "accountUuids";
public void onExport(final boolean includeGlobals, final Account account) {
// TODO, prompt to allow a user to choose which accounts to export // TODO, prompt to allow a user to choose which accounts to export
Set<String> accountUuids = null; ArrayList<String> accountUuids = null;
if (account != null) { if (account != null) {
accountUuids = new HashSet<String>(); accountUuids = new ArrayList<>();
accountUuids.add(account.getUuid()); accountUuids.add(account.getUuid());
} }
ExportAsyncTask asyncTask = new ExportAsyncTask(this, includeGlobals, accountUuids); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, SettingsExporter.EXPORT_FILENAME);
intent.putStringArrayListExtra(EXTRA_ACCOUNTS, accountUuids);
intent.putExtra(EXTRA_INC_GLOBALS, includeGlobals);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> infos = packageManager.queryIntentActivities(intent, 0);
if (infos.size() > 0) {
startActivityForResult(Intent.createChooser(intent, null), ACTIVITY_REQUEST_SAVE_SETTINGS_FILE);
} else {
showDialog(DIALOG_NO_FILE_MANAGER);
}
} else {
//Pre-Kitkat
ExportAsyncTask asyncTask = new ExportAsyncTask(this, includeGlobals, accountUuids, null);
setNonConfigurationInstance(asyncTask);
asyncTask.execute();
}
}
public void onExport(Intent intent) {
boolean includeGlobals = intent.getBooleanExtra(EXTRA_INC_GLOBALS, false);
ArrayList<String> accountUuids = intent.getStringArrayListExtra(EXTRA_ACCOUNTS);
ExportAsyncTask asyncTask = new ExportAsyncTask(this, includeGlobals, accountUuids, intent.getData());
setNonConfigurationInstance(asyncTask); setNonConfigurationInstance(asyncTask);
asyncTask.execute(); asyncTask.execute();
} }
@ -1904,13 +1939,17 @@ public class Accounts extends K9ListActivity implements OnItemClickListener {
private boolean mIncludeGlobals; private boolean mIncludeGlobals;
private Set<String> mAccountUuids; private Set<String> mAccountUuids;
private String mFileName; private String mFileName;
private Uri mUri;
private ExportAsyncTask(Accounts activity, boolean includeGlobals, private ExportAsyncTask(Accounts activity, boolean includeGlobals,
Set<String> accountUuids) { List<String> accountUuids, Uri uri) {
super(activity); super(activity);
mIncludeGlobals = includeGlobals; mIncludeGlobals = includeGlobals;
mAccountUuids = accountUuids; mUri = uri;
if (accountUuids != null) {
mAccountUuids = new HashSet<>(accountUuids);
}
} }
@Override @Override
@ -1923,8 +1962,13 @@ public class Accounts extends K9ListActivity implements OnItemClickListener {
@Override @Override
protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) {
try { try {
if (mUri == null) {
mFileName = SettingsExporter.exportToFile(mContext, mIncludeGlobals, mFileName = SettingsExporter.exportToFile(mContext, mIncludeGlobals,
mAccountUuids); mAccountUuids);
} else {
SettingsExporter.exportToUri(mContext, mIncludeGlobals, mAccountUuids, mUri);
}
} catch (SettingsImportExportException e) { } catch (SettingsImportExportException e) {
Timber.w(e, "Exception during export"); Timber.w(e, "Exception during export");
return false; return false;
@ -1942,8 +1986,13 @@ public class Accounts extends K9ListActivity implements OnItemClickListener {
removeProgressDialog(); removeProgressDialog();
if (success) { if (success) {
if (mFileName != null) {
activity.showSimpleDialog(R.string.settings_export_success_header, activity.showSimpleDialog(R.string.settings_export_success_header,
R.string.settings_export_success, mFileName); R.string.settings_export_success, mFileName);
} else {
activity.showSimpleDialog(R.string.settings_export_success_header,
R.string.settings_export_success_generic);
}
} else { } else {
//TODO: better error messages //TODO: better error messages
activity.showSimpleDialog(R.string.settings_export_failed_header, activity.showSimpleDialog(R.string.settings_export_failed_header,

View file

@ -15,6 +15,7 @@ import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import android.content.Context; import android.content.Context;
import android.net.Uri;
import android.os.Environment; import android.os.Environment;
import timber.log.Timber; import timber.log.Timber;
import android.util.Xml; import android.util.Xml;
@ -32,7 +33,7 @@ import org.xmlpull.v1.XmlSerializer;
public class SettingsExporter { public class SettingsExporter {
private static final String EXPORT_FILENAME = "settings.k9s"; public static final String EXPORT_FILENAME = "settings.k9s";
/** /**
* File format version number. * File format version number.
@ -108,6 +109,27 @@ public class SettingsExporter {
} }
} }
public static void exportToUri(Context context, boolean includeGlobals, Set<String> accountUuids, Uri uri)
throws SettingsImportExportException {
OutputStream os = null;
String filename = null;
try {
os = context.getContentResolver().openOutputStream(uri);
exportPreferences(context, os, includeGlobals, accountUuids);
} catch (Exception e) {
throw new SettingsImportExportException(e);
} finally {
if (os != null) {
try {
os.close();
} catch (IOException ioe) {
Log.w(K9.LOG_TAG, "Couldn't close exported settings file: " + filename);
}
}
}
}
static void exportPreferences(Context context, OutputStream os, boolean includeGlobals, Set<String> accountUuids) static void exportPreferences(Context context, OutputStream os, boolean includeGlobals, Set<String> accountUuids)
throws SettingsImportExportException { throws SettingsImportExportException {

View file

@ -975,6 +975,7 @@ Please submit bug reports, contribute new features and ask questions at
<string name="settings_importing">Importing settings…</string> <string name="settings_importing">Importing settings…</string>
<string name="settings_import_scanning_file">Scanning file…</string> <string name="settings_import_scanning_file">Scanning file…</string>
<string name="settings_export_success">Saved exported settings to <xliff:g id="filename">%s</xliff:g></string> <string name="settings_export_success">Saved exported settings to <xliff:g id="filename">%s</xliff:g></string>
<string name="settings_export_success_generic">Settings successfully exported</string>
<string name="settings_import_global_settings_success">Imported global settings from <xliff:g id="filename">%s</xliff:g></string> <string name="settings_import_global_settings_success">Imported global settings from <xliff:g id="filename">%s</xliff:g></string>
<string name="settings_import_success">Imported <xliff:g id="accounts">%s</xliff:g> from <xliff:g id="filename">%s</xliff:g></string> <string name="settings_import_success">Imported <xliff:g id="accounts">%s</xliff:g> from <xliff:g id="filename">%s</xliff:g></string>
<plurals name="settings_import_accounts"> <plurals name="settings_import_accounts">