From c70fcd74472966aefa001e4ed1c83ae60957d34a Mon Sep 17 00:00:00 2001
From: birdbird <6892457-tzugen@users.noreply.gitlab.com>
Date: Mon, 10 Apr 2023 11:26:27 +0000
Subject: [PATCH] Fix two exceptions
---
detekt-baseline.xml | 8 +-
.../ultrasonic/util/ModalBackgroundTask.java | 158 ----------
.../ultrasonic/subsonic/DownloadHandler.kt | 288 ++++++++++--------
.../ultrasonic/util/MediaItemConverter.kt | 12 +-
4 files changed, 181 insertions(+), 285 deletions(-)
delete mode 100644 ultrasonic/src/main/java/org/moire/ultrasonic/util/ModalBackgroundTask.java
diff --git a/detekt-baseline.xml b/detekt-baseline.xml
index ef17eeb4..3acd3952 100644
--- a/detekt-baseline.xml
+++ b/detekt-baseline.xml
@@ -1,9 +1,9 @@
-
+
TooManyFunctions:PlaybackService.kt$PlaybackService : MediaLibraryServiceKoinComponentCoroutineScope
UnusedPrivateMember:UApp.kt$private fun VmPolicy.Builder.detectAllExceptSocket(): VmPolicy.Builder
- ImplicitDefaultLocale:EditServerFragment.kt$EditServerFragment.<no name provided>$String.format( "%s %s", resources.getString(R.string.settings_connection_failure), getErrorMessage(error) )
+ ImplicitDefaultLocale:EditServerFragment.kt$EditServerFragment.<no name provided>$String.format( "%s %s", resources.getString(R.string.settings_connection_failure), getErrorMessage(error) )
ImplicitDefaultLocale:FileLoggerTree.kt$FileLoggerTree$String.format("Failed to write log to %s", file)
ImplicitDefaultLocale:FileLoggerTree.kt$FileLoggerTree$String.format("Log file rotated, logging into file %s", file?.name)
ImplicitDefaultLocale:FileLoggerTree.kt$FileLoggerTree$String.format("Logging into file %s", file?.name)
@@ -13,7 +13,7 @@
LongMethod:PlaylistsFragment.kt$PlaylistsFragment$override fun onContextItemSelected(menuItem: MenuItem): Boolean
LongMethod:ShareHandler.kt$ShareHandler$private fun showDialog( fragment: Fragment, shareDetails: ShareDetails, swipe: SwipeRefreshLayout?, cancellationToken: CancellationToken, additionalId: String? )
LongMethod:SharesFragment.kt$SharesFragment$override fun onContextItemSelected(menuItem: MenuItem): Boolean
- LongParameterList:ServerRowAdapter.kt$ServerRowAdapter$( private var context: Context, passedData: Array<ServerSetting>, private val model: ServerSettingsModel, private val activeServerProvider: ActiveServerProvider, private val manageMode: Boolean, private val serverDeletedCallback: ((Int) -> Unit), private val serverEditRequestedCallback: ((Int) -> Unit) )
+ LongParameterList:ServerRowAdapter.kt$ServerRowAdapter$( private var context: Context, passedData: Array<ServerSetting>, private val model: ServerSettingsModel, private val activeServerProvider: ActiveServerProvider, private val manageMode: Boolean, private val serverDeletedCallback: ((Int) -> Unit), private val serverEditRequestedCallback: ((Int) -> Unit) )
MagicNumber:ActiveServerProvider.kt$ActiveServerProvider$8192
MagicNumber:JukeboxMediaPlayer.kt$JukeboxMediaPlayer$0.05f
MagicNumber:JukeboxMediaPlayer.kt$JukeboxMediaPlayer$50
@@ -25,5 +25,5 @@
TooManyFunctions:RESTMusicService.kt$RESTMusicService : MusicService
UtilityClassWithPublicConstructor:FragmentTitle.kt$FragmentTitle
-
+
diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/util/ModalBackgroundTask.java b/ultrasonic/src/main/java/org/moire/ultrasonic/util/ModalBackgroundTask.java
deleted file mode 100644
index 1a963027..00000000
--- a/ultrasonic/src/main/java/org/moire/ultrasonic/util/ModalBackgroundTask.java
+++ /dev/null
@@ -1,158 +0,0 @@
-/*
- This file is part of Subsonic.
-
- Subsonic is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- Subsonic is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Subsonic. If not, see .
-
- Copyright 2009 (C) Sindre Mehus
- */
-package org.moire.ultrasonic.util;
-
-import android.app.Activity;
-import androidx.appcompat.app.AlertDialog;
-import org.moire.ultrasonic.R;
-import timber.log.Timber;
-
-/**
- * @author Sindre Mehus
- */
-public abstract class ModalBackgroundTask extends BackgroundTask
-{
- private final AlertDialog progressDialog;
- private Thread thread;
- private final boolean finishActivityOnCancel;
- private boolean cancelled;
-
- public ModalBackgroundTask(Activity activity, boolean finishActivityOnCancel)
- {
- super(activity);
- this.finishActivityOnCancel = finishActivityOnCancel;
- progressDialog = createProgressDialog();
- }
-
- public ModalBackgroundTask(Activity activity)
- {
- this(activity, true);
- }
-
- private androidx.appcompat.app.AlertDialog createProgressDialog()
- {
- InfoDialog.Builder builder = new InfoDialog.Builder(getActivity().getApplicationContext());
- builder.setTitle(R.string.background_task_wait);
- builder.setMessage(R.string.background_task_loading);
- builder.setOnCancelListener(dialogInterface -> cancel());
- builder.setPositiveButton(R.string.common_cancel, (dialogInterface, i) -> cancel());
-
- return builder.create();
- }
-
- @Override
- public void execute()
- {
- cancelled = false;
- progressDialog.show();
-
- thread = new Thread()
- {
- @Override
- public void run()
- {
- try
- {
- final T result = doInBackground();
- if (cancelled)
- {
- progressDialog.dismiss();
- return;
- }
-
- getHandler().post(new Runnable()
- {
- @Override
- public void run()
- {
- try
- {
- progressDialog.dismiss();
- }
- catch (Exception e)
- {
- // nothing
- }
-
- done(result);
- }
- });
-
- }
- catch (final Throwable t)
- {
- if (cancelled)
- {
- return;
- }
- getHandler().post(new Runnable()
- {
- @Override
- public void run()
- {
- try
- {
- progressDialog.dismiss();
- }
- catch (Exception e)
- {
- // nothing
- }
-
- error(t);
- }
- });
- }
- }
- };
- thread.start();
- }
-
- protected void cancel()
- {
- cancelled = true;
- if (thread != null)
- {
- thread.interrupt();
- }
-
- if (finishActivityOnCancel)
- {
- getActivity().finish();
- }
- }
-
- protected boolean isCancelled()
- {
- return cancelled;
- }
-
- @Override
- protected void error(Throwable error)
- {
- Timber.w(error);
- new ErrorDialog(getActivity(), getErrorMessage(error), getActivity(), finishActivityOnCancel);
- }
-
- @Override
- public void updateProgress(final String message)
- {
- getHandler().post(() -> progressDialog.setMessage(message));
- }
-}
diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/subsonic/DownloadHandler.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/subsonic/DownloadHandler.kt
index 16685364..9ef03db7 100644
--- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/subsonic/DownloadHandler.kt
+++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/subsonic/DownloadHandler.kt
@@ -7,19 +7,24 @@
package org.moire.ultrasonic.subsonic
-import android.app.Activity
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import java.util.Collections
import java.util.LinkedList
+import kotlinx.coroutines.CancellationException
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
import org.moire.ultrasonic.R
import org.moire.ultrasonic.data.ActiveServerProvider.Companion.isOffline
import org.moire.ultrasonic.domain.MusicDirectory
import org.moire.ultrasonic.domain.Track
import org.moire.ultrasonic.service.MediaPlayerController
import org.moire.ultrasonic.service.MusicServiceFactory.getMusicService
+import org.moire.ultrasonic.util.CommunicationError
import org.moire.ultrasonic.util.EntryByDiscAndTrackComparator
-import org.moire.ultrasonic.util.ModalBackgroundTask
+import org.moire.ultrasonic.util.InfoDialog
import org.moire.ultrasonic.util.Settings
import org.moire.ultrasonic.util.Util
@@ -30,7 +35,7 @@ import org.moire.ultrasonic.util.Util
class DownloadHandler(
val mediaPlayerController: MediaPlayerController,
val networkAndStorageChecker: NetworkAndStorageChecker
-) {
+) : CoroutineScope by CoroutineScope(Dispatchers.IO) {
private val maxSongs = 500
fun download(
@@ -203,137 +208,176 @@ class DownloadHandler(
unpin: Boolean,
isArtist: Boolean
) {
- val activity = fragment.activity as Activity
- val task = object : ModalBackgroundTask>(
- activity,
- false
- ) {
+ // Launch the Job
+ val job = launch {
+ val songs: MutableList