Safeguarding against runtime exceptions when importing and exporting
This commit is contained in:
parent
bee7d4b8dd
commit
84f004706f
@ -24,6 +24,7 @@ import android.widget.ArrayAdapter;
|
|||||||
|
|
||||||
import com.anthonycr.bonsai.CompletableOnSubscribe;
|
import com.anthonycr.bonsai.CompletableOnSubscribe;
|
||||||
import com.anthonycr.bonsai.SingleOnSubscribe;
|
import com.anthonycr.bonsai.SingleOnSubscribe;
|
||||||
|
import com.anthonycr.bonsai.Subscription;
|
||||||
import com.anthonycr.grant.PermissionsManager;
|
import com.anthonycr.grant.PermissionsManager;
|
||||||
import com.anthonycr.grant.PermissionsResultAction;
|
import com.anthonycr.grant.PermissionsResultAction;
|
||||||
|
|
||||||
@ -48,6 +49,7 @@ import com.anthonycr.bonsai.Schedulers;
|
|||||||
import acr.browser.lightning.database.bookmark.BookmarkModel;
|
import acr.browser.lightning.database.bookmark.BookmarkModel;
|
||||||
import acr.browser.lightning.dialog.BrowserDialog;
|
import acr.browser.lightning.dialog.BrowserDialog;
|
||||||
import acr.browser.lightning.utils.Preconditions;
|
import acr.browser.lightning.utils.Preconditions;
|
||||||
|
import acr.browser.lightning.utils.SubscriptionUtils;
|
||||||
import acr.browser.lightning.utils.Utils;
|
import acr.browser.lightning.utils.Utils;
|
||||||
|
|
||||||
public class BookmarkSettingsFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener {
|
public class BookmarkSettingsFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener {
|
||||||
@ -66,8 +68,12 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
|
|||||||
|
|
||||||
private File[] mFileList;
|
private File[] mFileList;
|
||||||
private String[] mFileNameList;
|
private String[] mFileNameList;
|
||||||
|
|
||||||
@Nullable private BookmarkLocalSync mSync;
|
@Nullable private BookmarkLocalSync mSync;
|
||||||
|
|
||||||
|
@Nullable private Subscription mImportSubscription;
|
||||||
|
@Nullable private Subscription mExportSubscription;
|
||||||
|
|
||||||
private static final String[] REQUIRED_PERMISSIONS = new String[]{
|
private static final String[] REQUIRED_PERMISSIONS = new String[]{
|
||||||
Manifest.permission.READ_EXTERNAL_STORAGE,
|
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||||
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
||||||
@ -153,9 +159,21 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroyView() {
|
||||||
|
super.onDestroyView();
|
||||||
|
|
||||||
|
SubscriptionUtils.safeUnsubscribe(mExportSubscription);
|
||||||
|
SubscriptionUtils.safeUnsubscribe(mImportSubscription);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
|
||||||
|
SubscriptionUtils.safeUnsubscribe(mExportSubscription);
|
||||||
|
SubscriptionUtils.safeUnsubscribe(mImportSubscription);
|
||||||
|
|
||||||
mActivity = null;
|
mActivity = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,14 +215,21 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
|
|||||||
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
|
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
|
||||||
@Override
|
@Override
|
||||||
public void onItem(@Nullable List<HistoryItem> item) {
|
public void onItem(@Nullable List<HistoryItem> item) {
|
||||||
|
if (!isAdded()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Preconditions.checkNonNull(item);
|
Preconditions.checkNonNull(item);
|
||||||
final File exportFile = BookmarkExporter.createNewExportFile();
|
final File exportFile = BookmarkExporter.createNewExportFile();
|
||||||
BookmarkExporter.exportBookmarksToFile(item, exportFile)
|
SubscriptionUtils.safeUnsubscribe(mExportSubscription);
|
||||||
|
mExportSubscription = BookmarkExporter.exportBookmarksToFile(item, exportFile)
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(Schedulers.main())
|
.observeOn(Schedulers.main())
|
||||||
.subscribe(new CompletableOnSubscribe() {
|
.subscribe(new CompletableOnSubscribe() {
|
||||||
@Override
|
@Override
|
||||||
public void onComplete() {
|
public void onComplete() {
|
||||||
|
mExportSubscription = null;
|
||||||
|
|
||||||
Activity activity = getActivity();
|
Activity activity = getActivity();
|
||||||
if (activity != null) {
|
if (activity != null) {
|
||||||
Utils.showSnackbar(activity, activity.getString(R.string.bookmark_export_path)
|
Utils.showSnackbar(activity, activity.getString(R.string.bookmark_export_path)
|
||||||
@ -214,9 +239,11 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(@NonNull Throwable throwable) {
|
public void onError(@NonNull Throwable throwable) {
|
||||||
|
mExportSubscription = null;
|
||||||
|
|
||||||
Log.e(TAG, "onError: exporting bookmarks", throwable);
|
Log.e(TAG, "onError: exporting bookmarks", throwable);
|
||||||
Activity activity = getActivity();
|
Activity activity = getActivity();
|
||||||
if (activity != null) {
|
if (activity != null && !activity.isFinishing() && isAdded()) {
|
||||||
Utils.createInformativeDialog(activity, R.string.title_error, R.string.bookmark_export_failure);
|
Utils.createInformativeDialog(activity, R.string.title_error, R.string.bookmark_export_failure);
|
||||||
} else {
|
} else {
|
||||||
Utils.showToast(mApplication, R.string.bookmark_export_failure);
|
Utils.showToast(mApplication, R.string.bookmark_export_failure);
|
||||||
@ -229,7 +256,12 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDenied(String permission) {
|
public void onDenied(String permission) {
|
||||||
//TODO Show message
|
Activity activity = getActivity();
|
||||||
|
if (activity != null && !activity.isFinishing() && isAdded()) {
|
||||||
|
Utils.createInformativeDialog(activity, R.string.title_error, R.string.bookmark_export_failure);
|
||||||
|
} else {
|
||||||
|
Utils.showToast(mApplication, R.string.bookmark_export_failure);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
@ -445,11 +477,14 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
|
|||||||
Dialog dialog1 = builder.show();
|
Dialog dialog1 = builder.show();
|
||||||
BrowserDialog.setDialogSize(mActivity, dialog1);
|
BrowserDialog.setDialogSize(mActivity, dialog1);
|
||||||
} else {
|
} else {
|
||||||
BookmarkExporter.importBookmarksFromFile(mFileList[which])
|
SubscriptionUtils.safeUnsubscribe(mImportSubscription);
|
||||||
|
mImportSubscription = BookmarkExporter.importBookmarksFromFile(mFileList[which])
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
|
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
|
||||||
@Override
|
@Override
|
||||||
public void onItem(@Nullable final List<HistoryItem> importList) {
|
public void onItem(@Nullable final List<HistoryItem> importList) {
|
||||||
|
mImportSubscription = null;
|
||||||
|
|
||||||
Preconditions.checkNonNull(importList);
|
Preconditions.checkNonNull(importList);
|
||||||
mBookmarkManager.addBookmarkList(importList)
|
mBookmarkManager.addBookmarkList(importList)
|
||||||
.observeOn(Schedulers.main())
|
.observeOn(Schedulers.main())
|
||||||
@ -467,9 +502,11 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(@NonNull Throwable throwable) {
|
public void onError(@NonNull Throwable throwable) {
|
||||||
|
mImportSubscription = null;
|
||||||
|
|
||||||
Log.e(TAG, "onError: importing bookmarks", throwable);
|
Log.e(TAG, "onError: importing bookmarks", throwable);
|
||||||
Activity activity = getActivity();
|
Activity activity = getActivity();
|
||||||
if (activity != null) {
|
if (activity != null && !activity.isFinishing() && isAdded()) {
|
||||||
Utils.createInformativeDialog(activity, R.string.title_error, R.string.import_bookmark_error);
|
Utils.createInformativeDialog(activity, R.string.title_error, R.string.import_bookmark_error);
|
||||||
} else {
|
} else {
|
||||||
Utils.showToast(mApplication, R.string.import_bookmark_error);
|
Utils.showToast(mApplication, R.string.import_bookmark_error);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user