Fix settings export on API 19+

This commit is contained in:
cketti 2017-03-12 17:03:10 +01:00
parent 17ebb4c613
commit 5fa77adfcb
2 changed files with 48 additions and 43 deletions

View file

@ -29,6 +29,7 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.text.Editable; import android.text.Editable;
@ -137,6 +138,9 @@ public class Accounts extends K9ListActivity implements OnItemClickListener {
private TextView mActionBarSubTitle; private TextView mActionBarSubTitle;
private TextView mActionBarUnread; private TextView mActionBarUnread;
private boolean exportGlobalSettings;
private ArrayList<String> exportAccountUuids;
/** /**
* Contains information about objects that need to be retained on configuration changes. * Contains information about objects that need to be retained on configuration changes.
* *
@ -329,6 +333,9 @@ public class Accounts extends K9ListActivity implements OnItemClickListener {
private static String ACCOUNT_STATS = "accountStats"; private static String ACCOUNT_STATS = "accountStats";
private static String STATE_UNREAD_COUNT = "unreadCount"; private static String STATE_UNREAD_COUNT = "unreadCount";
private static String SELECTED_CONTEXT_ACCOUNT = "selectedContextAccount"; private static String SELECTED_CONTEXT_ACCOUNT = "selectedContextAccount";
private static final String STATE_EXPORT_GLOBAL_SETTINGS = "exportGlobalSettings";
private static final String STATE_EXPORT_ACCOUNTS = "exportAccountUuids";
public static final String EXTRA_STARTUP = "startup"; public static final String EXTRA_STARTUP = "startup";
@ -477,6 +484,17 @@ public class Accounts extends K9ListActivity implements OnItemClickListener {
} }
outState.putSerializable(STATE_UNREAD_COUNT, mUnreadMessageCount); outState.putSerializable(STATE_UNREAD_COUNT, mUnreadMessageCount);
outState.putSerializable(ACCOUNT_STATS, accountStats); outState.putSerializable(ACCOUNT_STATS, accountStats);
outState.putBoolean(STATE_EXPORT_GLOBAL_SETTINGS, exportGlobalSettings);
outState.putStringArrayList(STATE_EXPORT_ACCOUNTS, exportAccountUuids);
}
@Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
exportGlobalSettings = state.getBoolean(STATE_EXPORT_GLOBAL_SETTINGS, false);
exportAccountUuids = state.getStringArrayList(STATE_EXPORT_ACCOUNTS);
} }
private StorageManager.StorageListener storageListener = new StorageManager.StorageListener() { private StorageManager.StorageListener storageListener = new StorageManager.StorageListener() {
@ -1887,9 +1905,6 @@ public class Accounts extends K9ListActivity implements OnItemClickListener {
} }
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) { 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
ArrayList<String> accountUuids = null; ArrayList<String> accountUuids = null;
@ -1898,36 +1913,28 @@ public class Accounts extends K9ListActivity implements OnItemClickListener {
accountUuids.add(account.getUuid()); accountUuids.add(account.getUuid());
} }
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
exportGlobalSettings = includeGlobals;
exportAccountUuids = accountUuids;
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.setType("application/octet-stream");
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, SettingsExporter.EXPORT_FILENAME); intent.putExtra(Intent.EXTRA_TITLE, SettingsExporter.EXPORT_FILENAME);
intent.putStringArrayListExtra(EXTRA_ACCOUNTS, accountUuids); intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(EXTRA_INC_GLOBALS, includeGlobals);
PackageManager packageManager = getPackageManager(); startActivityForResult(intent, ACTIVITY_REQUEST_SAVE_SETTINGS_FILE);
List<ResolveInfo> infos = packageManager.queryIntentActivities(intent, 0);
if (infos.size() > 0) {
startActivityForResult(Intent.createChooser(intent, null), ACTIVITY_REQUEST_SAVE_SETTINGS_FILE);
} else { } else {
showDialog(DIALOG_NO_FILE_MANAGER); startExport(includeGlobals, accountUuids, null);
}
} else {
//Pre-Kitkat
ExportAsyncTask asyncTask = new ExportAsyncTask(this, includeGlobals, accountUuids, null);
setNonConfigurationInstance(asyncTask);
asyncTask.execute();
} }
} }
public void onExport(Intent intent) { public void onExport(Intent intent) {
boolean includeGlobals = intent.getBooleanExtra(EXTRA_INC_GLOBALS, false); Uri documentsUri = intent.getData();
ArrayList<String> accountUuids = intent.getStringArrayListExtra(EXTRA_ACCOUNTS); startExport(exportGlobalSettings, exportAccountUuids, documentsUri);
}
ExportAsyncTask asyncTask = new ExportAsyncTask(this, includeGlobals, accountUuids, intent.getData()); private void startExport(boolean exportGlobalSettings, ArrayList<String> exportAccountUuids, Uri documentsUri) {
ExportAsyncTask asyncTask = new ExportAsyncTask(this, exportGlobalSettings, exportAccountUuids, documentsUri);
setNonConfigurationInstance(asyncTask); setNonConfigurationInstance(asyncTask);
asyncTask.execute(); asyncTask.execute();
} }

View file

@ -17,11 +17,11 @@ import java.util.TreeMap;
import android.content.Context; import android.content.Context;
import android.net.Uri; 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;
import com.fsck.k9.Account; import com.fsck.k9.Account;
import com.fsck.k9.K9;
import com.fsck.k9.Preferences; import com.fsck.k9.Preferences;
import com.fsck.k9.helper.FileHelper; import com.fsck.k9.helper.FileHelper;
import com.fsck.k9.mail.ServerSettings; import com.fsck.k9.mail.ServerSettings;
@ -81,7 +81,6 @@ public class SettingsExporter {
throws SettingsImportExportException { throws SettingsImportExportException {
OutputStream os = null; OutputStream os = null;
String filename = null;
try { try {
File dir = new File(Environment.getExternalStorageDirectory() + File.separator + context.getPackageName()); File dir = new File(Environment.getExternalStorageDirectory() + File.separator + context.getPackageName());
if (!dir.mkdirs()) { if (!dir.mkdirs()) {
@ -89,7 +88,7 @@ public class SettingsExporter {
} }
File file = FileHelper.createUniqueFile(dir, EXPORT_FILENAME); File file = FileHelper.createUniqueFile(dir, EXPORT_FILENAME);
filename = file.getAbsolutePath(); String filename = file.getAbsolutePath();
os = new FileOutputStream(filename); os = new FileOutputStream(filename);
exportPreferences(context, os, includeGlobals, accountUuids); exportPreferences(context, os, includeGlobals, accountUuids);
@ -99,13 +98,7 @@ public class SettingsExporter {
} catch (Exception e) { } catch (Exception e) {
throw new SettingsImportExportException(e); throw new SettingsImportExportException(e);
} finally { } finally {
if (os != null) { closeOrThrow(os);
try {
os.close();
} catch (IOException ioe) {
Timber.w("Couldn't close exported settings file: %s", filename);
}
}
} }
} }
@ -113,20 +106,25 @@ public class SettingsExporter {
throws SettingsImportExportException { throws SettingsImportExportException {
OutputStream os = null; OutputStream os = null;
String filename = null;
try { try {
os = context.getContentResolver().openOutputStream(uri); os = context.getContentResolver().openOutputStream(uri);
exportPreferences(context, os, includeGlobals, accountUuids); exportPreferences(context, os, includeGlobals, accountUuids);
} catch (Exception e) { } catch (Exception e) {
throw new SettingsImportExportException(e); throw new SettingsImportExportException(e);
} finally { } finally {
if (os != null) { closeOrThrow(os);
}
}
private static void closeOrThrow(OutputStream outputStream) throws SettingsImportExportException {
if (outputStream == null) {
return;
}
try { try {
os.close(); outputStream.close();
} catch (IOException ioe) { } catch (IOException e) {
Log.w(K9.LOG_TAG, "Couldn't close exported settings file: " + filename); throw new SettingsImportExportException(e);
}
}
} }
} }