Merge pull request #5939 from ByteHamster/fix-subscribe-password

Do not forget feed password when subscribing
This commit is contained in:
ByteHamster 2022-06-30 12:05:05 +02:00 committed by GitHub
commit d9bcae09ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 70 deletions

View File

@ -101,7 +101,6 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
private static final String PREF_LAST_AUTO_DOWNLOAD = "lastAutoDownload"; private static final String PREF_LAST_AUTO_DOWNLOAD = "lastAutoDownload";
private volatile List<Feed> feeds; private volatile List<Feed> feeds;
private Feed feed;
private String selectedDownloadUrl; private String selectedDownloadUrl;
private Downloader downloader; private Downloader downloader;
private String username = null; private String username = null;
@ -289,13 +288,11 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
private void startFeedDownload(String url) { private void startFeedDownload(String url) {
Log.d(TAG, "Starting feed download"); Log.d(TAG, "Starting feed download");
url = URLChecker.prepareURL(url); selectedDownloadUrl = URLChecker.prepareURL(url);
feed = new Feed(url, null); DownloadRequest request = DownloadRequestCreator.create(new Feed(selectedDownloadUrl, null))
DownloadRequest request = DownloadRequestCreator.create(feed)
.withAuthentication(username, password) .withAuthentication(username, password)
.withInitiatedByUser(true) .withInitiatedByUser(true)
.build(); .build();
feed.setFile_url(request.getDestination());
download = Observable.fromCallable(() -> { download = Observable.fromCallable(() -> {
feeds = DBReader.getFeedList(); feeds = DBReader.getFeedList();
@ -305,16 +302,16 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
}) })
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(this::checkDownloadResult, .subscribe(status -> checkDownloadResult(status, request.getDestination()),
error -> Log.e(TAG, Log.getStackTraceString(error))); error -> Log.e(TAG, Log.getStackTraceString(error)));
} }
private void checkDownloadResult(@NonNull DownloadStatus status) { private void checkDownloadResult(@NonNull DownloadStatus status, String destination) {
if (status.isCancelled()) { if (status.isCancelled()) {
return; return;
} }
if (status.isSuccessful()) { if (status.isSuccessful()) {
parseFeed(); parseFeed(destination);
} else if (status.getReason() == DownloadError.ERROR_UNAUTHORIZED) { } else if (status.getReason() == DownloadError.ERROR_UNAUTHORIZED) {
if (!isFinishing() && !isPaused) { if (!isFinishing() && !isPaused) {
if (username != null && password != null) { if (username != null && password != null) {
@ -338,7 +335,7 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
.subscribe( .subscribe(
feeds -> { feeds -> {
OnlineFeedViewActivity.this.feeds = feeds; OnlineFeedViewActivity.this.feeds = feeds;
handleUpdatedFeedStatus(feed); handleUpdatedFeedStatus();
}, error -> Log.e(TAG, Log.getStackTraceString(error)) }, error -> Log.e(TAG, Log.getStackTraceString(error))
); );
} }
@ -346,16 +343,12 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(DownloadEvent event) { public void onEventMainThread(DownloadEvent event) {
Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]"); Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]");
handleUpdatedFeedStatus(feed); handleUpdatedFeedStatus();
} }
private void parseFeed() { private void parseFeed(String destination) {
if (feed == null || (feed.getFile_url() == null && feed.isDownloaded())) {
throw new IllegalStateException("feed must be non-null and downloaded when parseFeed is called");
}
Log.d(TAG, "Parsing feed"); Log.d(TAG, "Parsing feed");
parser = Maybe.fromCallable(() -> doParseFeed(destination))
parser = Maybe.fromCallable(this::doParseFeed)
.subscribeOn(Schedulers.computation()) .subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableMaybeObserver<FeedHandlerResult>() { .subscribeWith(new DisposableMaybeObserver<FeedHandlerResult>() {
@ -384,14 +377,17 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
* @throws Exception If unsuccessful but we do not know a resolution. * @throws Exception If unsuccessful but we do not know a resolution.
*/ */
@Nullable @Nullable
private FeedHandlerResult doParseFeed() throws Exception { private FeedHandlerResult doParseFeed(String destination) throws Exception {
FeedHandler handler = new FeedHandler(); FeedHandler handler = new FeedHandler();
Feed feed = new Feed(selectedDownloadUrl, null);
feed.setFile_url(destination);
File destinationFile = new File(destination);
try { try {
return handler.parseFeed(feed); return handler.parseFeed(feed);
} catch (UnsupportedFeedtypeException e) { } catch (UnsupportedFeedtypeException e) {
Log.d(TAG, "Unsupported feed type detected"); Log.d(TAG, "Unsupported feed type detected");
if ("html".equalsIgnoreCase(e.getRootElement())) { if ("html".equalsIgnoreCase(e.getRootElement())) {
boolean dialogShown = showFeedDiscoveryDialog(new File(feed.getFile_url()), feed.getDownload_url()); boolean dialogShown = showFeedDiscoveryDialog(destinationFile, selectedDownloadUrl);
if (dialogShown) { if (dialogShown) {
return null; // Should not display an error message return null; // Should not display an error message
} else { } else {
@ -404,7 +400,7 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
Log.e(TAG, Log.getStackTraceString(e)); Log.e(TAG, Log.getStackTraceString(e));
throw e; throw e;
} finally { } finally {
boolean rc = new File(feed.getFile_url()).delete(); boolean rc = destinationFile.delete();
Log.d(TAG, "Deleted feed source file. Result: " + rc); Log.d(TAG, "Deleted feed source file. Result: " + rc);
} }
} }
@ -420,8 +416,6 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
int resId = R.string.no_feed_url_podcast_found_by_search; int resId = R.string.no_feed_url_podcast_found_by_search;
Snackbar.make(findViewById(android.R.id.content), resId, Snackbar.LENGTH_LONG).show(); Snackbar.make(findViewById(android.R.id.content), resId, Snackbar.LENGTH_LONG).show();
} }
this.feed = feed;
this.selectedDownloadUrl = feed.getDownload_url();
viewBinding.backgroundImage.setColorFilter(new LightingColorFilter(0xff828282, 0x000000)); viewBinding.backgroundImage.setColorFilter(new LightingColorFilter(0xff828282, 0x000000));
@ -459,15 +453,15 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
description.setText(HtmlToPlainText.getPlainText(feed.getDescription())); description.setText(HtmlToPlainText.getPlainText(feed.getDescription()));
viewBinding.subscribeButton.setOnClickListener(v -> { viewBinding.subscribeButton.setOnClickListener(v -> {
if (feedInFeedlist(feed)) { if (feedInFeedlist()) {
openFeed(); openFeed();
} else { } else {
Feed f = new Feed(selectedDownloadUrl, null, feed.getTitle()); Feed f = new Feed(selectedDownloadUrl, null, feed.getTitle());
f.setPreferences(feed.getPreferences()); DownloadService.download(this, false, DownloadRequestCreator.create(f)
this.feed = f; .withAuthentication(username, password)
DownloadService.download(this, false, DownloadRequestCreator.create(f).build()); .build());
didPressSubscribe = true; didPressSubscribe = true;
handleUpdatedFeedStatus(feed); handleUpdatedFeedStatus();
} }
}); });
@ -531,72 +525,62 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
} }
}); });
} }
handleUpdatedFeedStatus(feed); handleUpdatedFeedStatus();
} }
private void openFeed() { private void openFeed() {
// feed.getId() is always 0, we have to retrieve the id from the feed list from // feed.getId() is always 0, we have to retrieve the id from the feed list from
// the database // the database
Intent intent = MainActivity.getIntentToOpenFeed(this, getFeedId(feed)); Intent intent = MainActivity.getIntentToOpenFeed(this, getFeedId());
intent.putExtra(MainActivity.EXTRA_STARTED_FROM_SEARCH, intent.putExtra(MainActivity.EXTRA_STARTED_FROM_SEARCH,
getIntent().getBooleanExtra(MainActivity.EXTRA_STARTED_FROM_SEARCH, false)); getIntent().getBooleanExtra(MainActivity.EXTRA_STARTED_FROM_SEARCH, false));
finish(); finish();
startActivity(intent); startActivity(intent);
} }
private void handleUpdatedFeedStatus(Feed feed) { private void handleUpdatedFeedStatus() {
if (feed != null) { if (DownloadService.isDownloadingFile(selectedDownloadUrl)) {
if (DownloadService.isDownloadingFile(feed.getDownload_url())) { viewBinding.subscribeButton.setEnabled(false);
viewBinding.subscribeButton.setEnabled(false); viewBinding.subscribeButton.setText(R.string.subscribing_label);
viewBinding.subscribeButton.setText(R.string.subscribing_label); } else if (feedInFeedlist()) {
} else if (feedInFeedlist(feed)) { viewBinding.subscribeButton.setEnabled(true);
viewBinding.subscribeButton.setEnabled(true); viewBinding.subscribeButton.setText(R.string.open_podcast);
viewBinding.subscribeButton.setText(R.string.open_podcast); if (didPressSubscribe) {
if (didPressSubscribe) { didPressSubscribe = false;
didPressSubscribe = false;
if (UserPreferences.isEnableAutodownload()) {
boolean autoDownload = viewBinding.autoDownloadCheckBox.isChecked();
Feed feed1 = DBReader.getFeed(getFeedId(feed));
FeedPreferences feedPreferences = feed1.getPreferences();
feedPreferences.setAutoDownload(autoDownload);
DBWriter.setFeedPreferences(feedPreferences);
SharedPreferences preferences = getSharedPreferences(PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(PREF_LAST_AUTO_DOWNLOAD, autoDownload);
editor.apply();
}
openFeed();
}
} else {
viewBinding.subscribeButton.setEnabled(true);
viewBinding.subscribeButton.setText(R.string.subscribe_label);
if (UserPreferences.isEnableAutodownload()) { if (UserPreferences.isEnableAutodownload()) {
viewBinding.autoDownloadCheckBox.setVisibility(View.VISIBLE); boolean autoDownload = viewBinding.autoDownloadCheckBox.isChecked();
Feed feed1 = DBReader.getFeed(getFeedId());
FeedPreferences feedPreferences = feed1.getPreferences();
feedPreferences.setAutoDownload(autoDownload);
DBWriter.setFeedPreferences(feedPreferences);
SharedPreferences preferences = getSharedPreferences(PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(PREF_LAST_AUTO_DOWNLOAD, autoDownload);
editor.apply();
} }
openFeed();
}
} else {
viewBinding.subscribeButton.setEnabled(true);
viewBinding.subscribeButton.setText(R.string.subscribe_label);
if (UserPreferences.isEnableAutodownload()) {
viewBinding.autoDownloadCheckBox.setVisibility(View.VISIBLE);
} }
} }
} }
private boolean feedInFeedlist(Feed feed) { private boolean feedInFeedlist() {
if (feeds == null || feed == null) { return getFeedId() != 0;
return false;
}
for (Feed f : feeds) {
if (f.getIdentifyingValue().equals(feed.getIdentifyingValue())) {
return true;
}
}
return false;
} }
private long getFeedId(Feed feed) { private long getFeedId() {
if (feeds == null || feed == null) { if (feeds == null) {
return 0; return 0;
} }
for (Feed f : feeds) { for (Feed f : feeds) {
if (f.getIdentifyingValue().equals(feed.getIdentifyingValue())) { if (f.getDownload_url().equals(selectedDownloadUrl)) {
return f.getId(); return f.getId();
} }
} }