Refactor OPML export from asynctask to observable
This commit is contained in:
parent
8accc12048
commit
d1bbe8a181
@ -26,11 +26,9 @@ import de.danoeh.antennapod.preferences.PreferenceController;
|
||||
*/
|
||||
public class PreferenceActivity extends AppCompatActivity {
|
||||
|
||||
private static WeakReference<PreferenceActivity> instance;
|
||||
private PreferenceController preferenceController;
|
||||
private MainFragment prefFragment;
|
||||
private static WeakReference<PreferenceActivity> instance;
|
||||
|
||||
|
||||
private final PreferenceController.PreferenceUI preferenceUI = new PreferenceController.PreferenceUI() {
|
||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||||
@Override
|
||||
@ -128,5 +126,14 @@ public class PreferenceActivity extends AppCompatActivity {
|
||||
}
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
PreferenceActivity activity = instance.get();
|
||||
if(activity != null && activity.preferenceController != null) {
|
||||
activity.preferenceController.onStop();
|
||||
}
|
||||
super.onStop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,9 +18,6 @@ import de.danoeh.antennapod.preferences.PreferenceController;
|
||||
*/
|
||||
public class PreferenceActivityGingerbread extends android.preference.PreferenceActivity {
|
||||
private static final String TAG = "PreferenceActivity";
|
||||
|
||||
private PreferenceController preferenceController;
|
||||
|
||||
private final PreferenceController.PreferenceUI preferenceUI = new PreferenceController.PreferenceUI() {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@ -34,6 +31,7 @@ public class PreferenceActivityGingerbread extends android.preference.Preference
|
||||
return PreferenceActivityGingerbread.this;
|
||||
}
|
||||
};
|
||||
private PreferenceController preferenceController;
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
@SuppressWarnings("deprecation")
|
||||
@ -60,6 +58,12 @@ public class PreferenceActivityGingerbread extends android.preference.Preference
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
preferenceController.onStop();
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onApplyThemeResource(Theme theme, int resid, boolean first) {
|
||||
theme.applyStyle(UserPreferences.getTheme(), true);
|
||||
|
@ -1,12 +1,5 @@
|
||||
package de.danoeh.antennapod.asynctask;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
@ -14,109 +7,55 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
import de.danoeh.antennapod.core.R;
|
||||
import de.danoeh.antennapod.core.opml.OpmlWriter;
|
||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||
import de.danoeh.antennapod.core.storage.DBReader;
|
||||
import de.danoeh.antennapod.core.util.LangUtils;
|
||||
import rx.Observable;
|
||||
|
||||
/**
|
||||
* Writes an OPML file into the export directory in the background.
|
||||
*/
|
||||
public class OpmlExportWorker extends AsyncTask<Void, Void, Void> {
|
||||
public class OpmlExportWorker {
|
||||
|
||||
public static final String EXPORT_DIR = "export/";
|
||||
private static final String TAG = "OpmlExportWorker";
|
||||
private static final String DEFAULT_OUTPUT_NAME = "antennapod-feeds.opml";
|
||||
public static final String EXPORT_DIR = "export/";
|
||||
|
||||
private Context context;
|
||||
private File output;
|
||||
|
||||
private ProgressDialog progDialog;
|
||||
private Exception exception;
|
||||
public OpmlExportWorker() {
|
||||
this(new File(UserPreferences.getDataFolder(EXPORT_DIR), DEFAULT_OUTPUT_NAME));
|
||||
}
|
||||
|
||||
public OpmlExportWorker(Context context, File output) {
|
||||
this.context = context;
|
||||
public OpmlExportWorker(File output) {
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
public OpmlExportWorker(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
public Observable<File> exportObservable() {
|
||||
if (output.exists()) {
|
||||
Log.w(TAG, "Overwriting previously exported file.");
|
||||
output.delete();
|
||||
}
|
||||
OpmlWriter opmlWriter = new OpmlWriter();
|
||||
if (output == null) {
|
||||
output = new File(
|
||||
UserPreferences.getDataFolder(EXPORT_DIR),
|
||||
DEFAULT_OUTPUT_NAME);
|
||||
if (output.exists()) {
|
||||
Log.w(TAG, "Overwriting previously exported file.");
|
||||
output.delete();
|
||||
}
|
||||
}
|
||||
OutputStreamWriter writer = null;
|
||||
try {
|
||||
writer = new OutputStreamWriter(new FileOutputStream(output), LangUtils.UTF_8);
|
||||
opmlWriter.writeDocument(DBReader.getFeedList(), writer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
exception = e;
|
||||
} finally {
|
||||
if (writer != null) {
|
||||
try {
|
||||
writer.close();
|
||||
} catch (IOException ioe) {
|
||||
exception = ioe;
|
||||
return Observable.create(subscriber -> {
|
||||
OutputStreamWriter writer = null;
|
||||
try {
|
||||
writer = new OutputStreamWriter(new FileOutputStream(output), LangUtils.UTF_8);
|
||||
opmlWriter.writeDocument(DBReader.getFeedList(), writer);
|
||||
subscriber.onNext(output);
|
||||
} catch (IOException e) {
|
||||
subscriber.onError(e);
|
||||
} finally {
|
||||
if (writer != null) {
|
||||
try {
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
subscriber.onError(e);
|
||||
}
|
||||
}
|
||||
subscriber.onCompleted();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
progDialog.dismiss();
|
||||
AlertDialog.Builder alert = new AlertDialog.Builder(context)
|
||||
.setNeutralButton(android.R.string.ok,
|
||||
(dialog, which) -> dialog.dismiss());
|
||||
if (exception != null) {
|
||||
alert.setTitle(R.string.export_error_label);
|
||||
alert.setMessage(exception.getMessage());
|
||||
} else {
|
||||
alert.setTitle(R.string.opml_export_success_title);
|
||||
alert.setMessage(context
|
||||
.getString(R.string.opml_export_success_sum)
|
||||
+ output.toString())
|
||||
.setPositiveButton(R.string.send_label, (dialog, which) -> {
|
||||
Uri outputUri = Uri.fromFile(output);
|
||||
Intent sendIntent = new Intent(Intent.ACTION_SEND);
|
||||
sendIntent.putExtra(Intent.EXTRA_SUBJECT,
|
||||
context.getResources().getText(R.string.opml_export_label));
|
||||
sendIntent.putExtra(Intent.EXTRA_STREAM, outputUri);
|
||||
sendIntent.setType("text/plain");
|
||||
context.startActivity(Intent.createChooser(sendIntent,
|
||||
context.getResources().getText(R.string.send_label)));
|
||||
});
|
||||
}
|
||||
alert.create().show();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
progDialog = new ProgressDialog(context);
|
||||
progDialog.setMessage(context.getString(R.string.exporting_label));
|
||||
progDialog.setIndeterminate(true);
|
||||
progDialog.show();
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
public void executeAsync() {
|
||||
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
|
||||
executeOnExecutor(THREAD_POOL_EXECUTOR);
|
||||
} else {
|
||||
execute();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package de.danoeh.antennapod.preferences;
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.ProgressDialog;
|
||||
import android.app.TimePickerDialog;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Context;
|
||||
@ -66,6 +67,10 @@ import de.danoeh.antennapod.dialog.AutoFlattrPreferenceDialog;
|
||||
import de.danoeh.antennapod.dialog.GpodnetSetHostnameDialog;
|
||||
import de.danoeh.antennapod.dialog.ProxyDialog;
|
||||
import de.danoeh.antennapod.dialog.VariableSpeedDialog;
|
||||
import rx.Observable;
|
||||
import rx.Subscription;
|
||||
import rx.android.schedulers.AndroidSchedulers;
|
||||
import rx.schedulers.Schedulers;
|
||||
|
||||
/**
|
||||
* Sets up a preference UI that lets the user change user preferences.
|
||||
@ -97,32 +102,11 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
|
||||
private static final String PREF_KNOWN_ISSUES = "prefKnownIssues";
|
||||
private static final String PREF_FAQ = "prefFaq";
|
||||
private static final String PREF_SEND_CRASH_REPORT = "prefSendCrashReport";
|
||||
|
||||
private final PreferenceUI ui;
|
||||
|
||||
private CheckBoxPreference[] selectedNetworks;
|
||||
|
||||
private static final String[] EXTERNAL_STORAGE_PERMISSIONS = {
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE };
|
||||
private static final int PERMISSION_REQUEST_EXTERNAL_STORAGE = 41;
|
||||
|
||||
public PreferenceController(PreferenceUI ui) {
|
||||
this.ui = ui;
|
||||
PreferenceManager.getDefaultSharedPreferences(ui.getActivity().getApplicationContext())
|
||||
.registerOnSharedPreferenceChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
if(key.equals(UserPreferences.PREF_SONIC)) {
|
||||
CheckBoxPreference prefSonic = (CheckBoxPreference) ui.findPreference(UserPreferences.PREF_SONIC);
|
||||
if(prefSonic != null) {
|
||||
prefSonic.setChecked(sharedPreferences.getBoolean(UserPreferences.PREF_SONIC, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final PreferenceUI ui;
|
||||
private final SharedPreferences.OnSharedPreferenceChangeListener gpoddernetListener =
|
||||
(sharedPreferences, key) -> {
|
||||
if (GpodnetPreferences.PREF_LAST_SYNC_ATTEMPT_TIMESTAMP.equals(key)) {
|
||||
@ -130,6 +114,14 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
|
||||
GpodnetPreferences.getLastSyncAttemptTimestamp());
|
||||
}
|
||||
};
|
||||
private CheckBoxPreference[] selectedNetworks;
|
||||
private Subscription subscription;
|
||||
|
||||
public PreferenceController(PreferenceUI ui) {
|
||||
this.ui = ui;
|
||||
PreferenceManager.getDefaultSharedPreferences(ui.getActivity().getApplicationContext())
|
||||
.registerOnSharedPreferenceChangeListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the preference activity that should be used on this device.
|
||||
@ -144,6 +136,16 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
if(key.equals(UserPreferences.PREF_SONIC)) {
|
||||
CheckBoxPreference prefSonic = (CheckBoxPreference) ui.findPreference(UserPreferences.PREF_SONIC);
|
||||
if(prefSonic != null) {
|
||||
prefSonic.setChecked(sharedPreferences.getBoolean(UserPreferences.PREF_SONIC, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onCreate() {
|
||||
final Activity activity = ui.getActivity();
|
||||
|
||||
@ -179,11 +181,7 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
|
||||
}
|
||||
);
|
||||
ui.findPreference(PreferenceController.PREF_OPML_EXPORT).setOnPreferenceClickListener(
|
||||
preference -> {
|
||||
new OpmlExportWorker(activity).executeAsync();
|
||||
return true;
|
||||
}
|
||||
);
|
||||
preference -> exportOpml());
|
||||
ui.findPreference(PreferenceController.PREF_CHOOSE_DATA_DIR).setOnPreferenceClickListener(
|
||||
preference -> {
|
||||
if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT &&
|
||||
@ -440,6 +438,40 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
|
||||
setSelectedNetworksEnabled(UserPreferences.isEnableAutodownloadWifiFilter());
|
||||
}
|
||||
|
||||
private boolean exportOpml() {
|
||||
Context context = ui.getActivity();
|
||||
final ProgressDialog progressDialog = new ProgressDialog(context);
|
||||
progressDialog.setMessage(context.getString(R.string.exporting_label));
|
||||
progressDialog.setIndeterminate(true);
|
||||
progressDialog.show();
|
||||
final AlertDialog.Builder alert = new AlertDialog.Builder(context)
|
||||
.setNeutralButton(android.R.string.ok, (dialog, which) -> dialog.dismiss());
|
||||
Observable<File> observable = new OpmlExportWorker().exportObservable();
|
||||
subscription = observable.subscribeOn(Schedulers.newThread())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(output -> {
|
||||
alert.setTitle(R.string.opml_export_success_title);
|
||||
String message = context.getString(R.string.opml_export_success_sum) + output.toString();
|
||||
alert.setMessage(message);
|
||||
alert.setPositiveButton(R.string.send_label, (dialog, which) -> {
|
||||
Uri outputUri = Uri.fromFile(output);
|
||||
Intent sendIntent = new Intent(Intent.ACTION_SEND);
|
||||
sendIntent.putExtra(Intent.EXTRA_SUBJECT,
|
||||
context.getResources().getText(R.string.opml_export_label));
|
||||
sendIntent.putExtra(Intent.EXTRA_STREAM, outputUri);
|
||||
sendIntent.setType("text/plain");
|
||||
context.startActivity(Intent.createChooser(sendIntent,
|
||||
context.getResources().getText(R.string.send_label)));
|
||||
});
|
||||
alert.create().show();
|
||||
}, error -> {
|
||||
alert.setTitle(R.string.export_error_label);
|
||||
alert.setMessage(error.getMessage());
|
||||
alert.show();
|
||||
}, () -> progressDialog.dismiss());
|
||||
return true;
|
||||
}
|
||||
|
||||
private void openInBrowser(String url) {
|
||||
try {
|
||||
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
|
||||
@ -464,6 +496,12 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
|
||||
GpodnetPreferences.unregisterOnSharedPreferenceChangeListener(gpoddernetListener);
|
||||
}
|
||||
|
||||
public void onStop() {
|
||||
if(subscription != null) {
|
||||
subscription.unsubscribe();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (resultCode == Activity.RESULT_OK &&
|
||||
|
Loading…
x
Reference in New Issue
Block a user