Browse Source

Safeguarding against runtime exceptions when importing and exporting

master
anthony restaino 8 years ago
parent
commit
84f004706f
  1. 47
      app/src/main/java/acr/browser/lightning/fragment/BookmarkSettingsFragment.java

47
app/src/main/java/acr/browser/lightning/fragment/BookmarkSettingsFragment.java

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

Loading…
Cancel
Save