Remove unused context references in MusicServiceFactory

This commit is contained in:
tzugen 2021-05-09 10:38:03 +02:00
parent 1fa9e604b5
commit f2b1ba0e94
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
23 changed files with 52 additions and 49 deletions

View File

@ -381,7 +381,7 @@ public class BookmarksFragment extends Fragment {
@Override
protected Pair<MusicDirectory, Boolean> doInBackground() throws Throwable
{
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
MusicService musicService = MusicServiceFactory.getMusicService();
MusicDirectory dir = load(musicService);
boolean valid = musicService.isLicenseValid();
return new Pair<>(dir, valid);

View File

@ -249,7 +249,7 @@ public class ChatFragment extends Fragment {
@Override
protected Void doInBackground() throws Throwable
{
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
MusicService musicService = MusicServiceFactory.getMusicService();
musicService.addChatMessage(message, getContext());
return null;
}
@ -273,7 +273,7 @@ public class ChatFragment extends Fragment {
@Override
protected List<ChatMessage> doInBackground() throws Throwable
{
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
MusicService musicService = MusicServiceFactory.getMusicService();
return musicService.getChatMessages(lastChatMessageTime, getContext());
}

View File

@ -78,7 +78,7 @@ public class LyricsFragment extends Fragment {
if (arguments == null) return null;
String artist = arguments.getString(Constants.INTENT_EXTRA_NAME_ARTIST);
String title = arguments.getString(Constants.INTENT_EXTRA_NAME_TITLE);
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
MusicService musicService = MusicServiceFactory.getMusicService();
return musicService.getLyrics(artist, title, getContext());
}

View File

@ -994,7 +994,7 @@ public class PlayerFragment extends Fragment implements GestureDetector.OnGestur
new Thread(new Runnable() {
@Override
public void run() {
final MusicService musicService = MusicServiceFactory.getMusicService(getContext());
final MusicService musicService = MusicServiceFactory.getMusicService();
try {
if (isStarred) {
@ -1024,7 +1024,7 @@ public class PlayerFragment extends Fragment implements GestureDetector.OnGestur
new Thread(new Runnable() {
@Override
public void run() {
final MusicService musicService = MusicServiceFactory.getMusicService(getContext());
final MusicService musicService = MusicServiceFactory.getMusicService();
try {
musicService.createBookmark(songId, playerPosition, getContext());
@ -1050,7 +1050,7 @@ public class PlayerFragment extends Fragment implements GestureDetector.OnGestur
new Thread(new Runnable() {
@Override
public void run() {
final MusicService musicService = MusicServiceFactory.getMusicService(getContext());
final MusicService musicService = MusicServiceFactory.getMusicService();
try {
musicService.deleteBookmark(bookmarkSongId, getContext());
@ -1127,7 +1127,7 @@ public class PlayerFragment extends Fragment implements GestureDetector.OnGestur
{
entries.add(downloadFile.getSong());
}
final MusicService musicService = MusicServiceFactory.getMusicService(getContext());
final MusicService musicService = MusicServiceFactory.getMusicService();
musicService.createPlaylist(null, playlistName, entries, getContext());
return null;
}

View File

@ -127,7 +127,7 @@ public class PlaylistsFragment extends Fragment {
@Override
protected List<Playlist> doInBackground() throws Throwable
{
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
MusicService musicService = MusicServiceFactory.getMusicService();
List<Playlist> playlists = musicService.getPlaylists(refresh, getContext());
if (!ActiveServerProvider.Companion.isOffline())
@ -222,7 +222,7 @@ public class PlaylistsFragment extends Fragment {
@Override
protected Void doInBackground() throws Throwable
{
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
MusicService musicService = MusicServiceFactory.getMusicService();
musicService.deletePlaylist(playlist.getId(), getContext());
return null;
}
@ -312,7 +312,7 @@ public class PlaylistsFragment extends Fragment {
String name = nameBoxText != null ? nameBoxText.toString() : null;
String comment = commentBoxText != null ? commentBoxText.toString() : null;
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
MusicService musicService = MusicServiceFactory.getMusicService();
musicService.updatePlaylist(playlist.getId(), name, comment, publicBox.isChecked(), getContext());
return null;
}

View File

@ -96,7 +96,7 @@ public class PodcastFragment extends Fragment {
@Override
protected List<PodcastsChannel> doInBackground() throws Throwable
{
MusicService musicService = MusicServiceFactory.getMusicService(context);
MusicService musicService = MusicServiceFactory.getMusicService();
return musicService.getPodcastsChannels(refresh, context);
}

View File

@ -420,7 +420,7 @@ public class SearchFragment extends Fragment {
protected SearchResult doInBackground() throws Throwable
{
SearchCriteria criteria = new SearchCriteria(query, maxArtists, maxAlbums, maxSongs);
MusicService service = MusicServiceFactory.getMusicService(getContext());
MusicService service = MusicServiceFactory.getMusicService();
return service.search(criteria, getContext());
}

View File

@ -102,7 +102,7 @@ public class SelectGenreFragment extends Fragment {
@Override
protected List<Genre> doInBackground()
{
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
MusicService musicService = MusicServiceFactory.getMusicService();
List<Genre> genres = new ArrayList<>();

View File

@ -128,7 +128,7 @@ public class SharesFragment extends Fragment {
@Override
protected List<Share> doInBackground() throws Throwable
{
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
MusicService musicService = MusicServiceFactory.getMusicService();
return musicService.getShares(refresh, getContext());
}
@ -195,7 +195,7 @@ public class SharesFragment extends Fragment {
@Override
protected Void doInBackground() throws Throwable
{
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
MusicService musicService = MusicServiceFactory.getMusicService();
musicService.deleteShare(share.getId(), getContext());
return null;
}
@ -301,7 +301,7 @@ public class SharesFragment extends Fragment {
Editable shareDescriptionText = shareDescription.getText();
String description = shareDescriptionText != null ? shareDescriptionText.toString() : null;
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
MusicService musicService = MusicServiceFactory.getMusicService();
musicService.updateShare(share.getId(), description, millis, getContext());
return null;
}

View File

@ -300,7 +300,7 @@ public class JukeboxMediaPlayer
private MusicService getMusicService()
{
return MusicServiceFactory.getMusicService(context);
return MusicServiceFactory.getMusicService();
}
public int getPositionSeconds()

View File

@ -527,7 +527,7 @@ public class MediaPlayerControllerImpl implements MediaPlayerController
try
{
String username = activeServerProvider.getValue().getActiveServer().getUserName();
UserInfo user = MusicServiceFactory.getMusicService(context).getUser(username, context);
UserInfo user = MusicServiceFactory.getMusicService().getUser(username, context);
return user.getJukeboxRole();
}
catch (Exception e)
@ -608,7 +608,7 @@ public class MediaPlayerControllerImpl implements MediaPlayerController
new Thread(() -> {
try
{
MusicServiceFactory.getMusicService(context).setRating(song.getId(), rating);
MusicServiceFactory.getMusicService().setRating(song.getId(), rating);
}
catch (Exception e)
{

View File

@ -36,7 +36,7 @@ public class Scrobbler
@Override
public void run()
{
MusicService service = MusicServiceFactory.getMusicService(context);
MusicService service = MusicServiceFactory.getMusicService();
try
{
service.scrobble(id, submission, context);

View File

@ -104,7 +104,7 @@ public class ShufflePlayBuffer
try
{
MusicService service = MusicServiceFactory.getMusicService(context);
MusicService service = MusicServiceFactory.getMusicService();
int n = CAPACITY - buffer.size();
MusicDirectory songs = service.getRandomSongs(n, context);

View File

@ -79,7 +79,7 @@ public enum VideoPlayerType
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(installedPro ? PACKAGE_NAME_MX_PRO : PACKAGE_NAME_MX_AD);
intent.putExtra("title", entry.getTitle());
intent.setDataAndType(Uri.parse(MusicServiceFactory.getMusicService(context).getVideoUrl(context, entry.getId(), false)), "video/*");
intent.setDataAndType(Uri.parse(MusicServiceFactory.getMusicService().getVideoUrl(context, entry.getId(), false)), "video/*");
context.startActivity(intent);
}
}
@ -91,7 +91,7 @@ public enum VideoPlayerType
public void playVideo(Context context, MusicDirectory.Entry entry) throws Exception
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(MusicServiceFactory.getMusicService(context).getVideoUrl(context, entry.getId(), true)));
intent.setData(Uri.parse(MusicServiceFactory.getMusicService().getVideoUrl(context, entry.getId(), true)));
context.startActivity(intent);
}
},
@ -102,7 +102,7 @@ public enum VideoPlayerType
public void playVideo(Context context, MusicDirectory.Entry entry) throws Exception
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(MusicServiceFactory.getMusicService(context).getVideoUrl(context, entry.getId(), false)), "video/*");
intent.setDataAndType(Uri.parse(MusicServiceFactory.getMusicService().getVideoUrl(context, entry.getId(), false)), "video/*");
context.startActivity(intent);
}
};

View File

@ -146,7 +146,7 @@ public class AlbumView extends UpdateView
album.setStarred(false);
}
final MusicService musicService = MusicServiceFactory.getMusicService(view.getContext());
final MusicService musicService = MusicServiceFactory.getMusicService();
new Thread(new Runnable()
{
@Override

View File

@ -78,7 +78,7 @@ class ArtistListModel(
private suspend fun loadFromServer(refresh: Boolean, swipe: SwipeRefreshLayout) =
withContext(Dispatchers.IO) {
val musicService = MusicServiceFactory.getMusicService(context)
val musicService = MusicServiceFactory.getMusicService()
val isOffline = ActiveServerProvider.isOffline()
val useId3Tags = Util.getShouldUseId3Tags()

View File

@ -41,7 +41,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
suspend fun getMusicFolders(refresh: Boolean) {
withContext(Dispatchers.IO) {
if (!ActiveServerProvider.isOffline()) {
val musicService = MusicServiceFactory.getMusicService(context)
val musicService = MusicServiceFactory.getMusicService()
musicFolders.postValue(musicService.getMusicFolders(refresh))
}
}
@ -55,7 +55,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
) {
withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService(context)
val service = MusicServiceFactory.getMusicService()
var root = MusicDirectory()
@ -105,7 +105,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
parent: MusicDirectory,
songs: MutableList<MusicDirectory.Entry>
) {
val service = MusicServiceFactory.getMusicService(context)
val service = MusicServiceFactory.getMusicService()
for (song in parent.getChildren(includeDirs = false, includeFiles = true)) {
if (!song.isVideo && !song.isDirectory) {
@ -127,7 +127,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
suspend fun getArtist(refresh: Boolean, id: String?, name: String?) {
withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService(context)
val service = MusicServiceFactory.getMusicService()
var root = MusicDirectory()
@ -160,7 +160,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService(context)
val service = MusicServiceFactory.getMusicService()
val musicDirectory: MusicDirectory
@ -199,7 +199,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
suspend fun getSongsForGenre(genre: String, count: Int, offset: Int) {
withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService(context)
val service = MusicServiceFactory.getMusicService()
val musicDirectory = service.getSongsByGenre(genre, count, offset, context)
songsForGenre.postValue(musicDirectory)
}
@ -209,7 +209,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService(context)
val service = MusicServiceFactory.getMusicService()
val musicDirectory: MusicDirectory
if (Util.getShouldUseId3Tags()) {
@ -226,7 +226,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
showHeader = false
withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService(context)
val service = MusicServiceFactory.getMusicService()
currentDirectory.postValue(service.getVideos(refresh, context))
}
}
@ -234,7 +234,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
suspend fun getRandom(size: Int) {
withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService(context)
val service = MusicServiceFactory.getMusicService()
val musicDirectory = service.getRandomSongs(size, context)
currentDirectoryIsSortable = false
@ -245,7 +245,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
suspend fun getPlaylist(playlistId: String, playlistName: String?) {
withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService(context)
val service = MusicServiceFactory.getMusicService()
val musicDirectory = service.getPlaylist(playlistId, playlistName, context)
currentDirectory.postValue(musicDirectory)
@ -255,7 +255,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
suspend fun getPodcastEpisodes(podcastChannelId: String) {
withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService(context)
val service = MusicServiceFactory.getMusicService()
val musicDirectory = service.getPodcastEpisodes(podcastChannelId, context)
currentDirectory.postValue(musicDirectory)
}
@ -264,7 +264,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
suspend fun getShare(shareId: String) {
withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService(context)
val service = MusicServiceFactory.getMusicService()
val musicDirectory = MusicDirectory()
val shares = service.getShares(true, context)
@ -291,7 +291,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
)
withContext(Dispatchers.IO) {
val service = MusicServiceFactory.getMusicService(context)
val service = MusicServiceFactory.getMusicService()
val musicDirectory: MusicDirectory
val musicFolderId = if (showSelectFolderHeader) {
activeServerProvider.getActiveServer().musicFolderId

View File

@ -228,7 +228,7 @@ class DownloadFile(
return
}
val musicService = getMusicService(context)
val musicService = getMusicService()
// Some devices seem to throw error on partial file which doesn't exist
val needsDownloading: Boolean

View File

@ -422,7 +422,7 @@ class MediaPlayerService : Service() {
if (currentPlaying != null) {
val song = currentPlaying.song
if (song.bookmarkPosition > 0 && Util.getShouldClearBookmark()) {
val musicService = getMusicService(context)
val musicService = getMusicService()
try {
musicService.deleteBookmark(song.id, context)
} catch (ignored: Exception) {

View File

@ -18,7 +18,7 @@
*/
package org.moire.ultrasonic.service
import android.content.Context
import org.koin.core.component.KoinApiExtension
import org.koin.core.component.KoinComponent
import org.koin.core.component.get
import org.koin.core.context.loadKoinModules
@ -30,9 +30,10 @@ import org.moire.ultrasonic.di.ONLINE_MUSIC_SERVICE
import org.moire.ultrasonic.di.musicServiceModule
// TODO Refactor everywhere to use DI way to get MusicService, and then remove this class
@KoinApiExtension
object MusicServiceFactory : KoinComponent {
@JvmStatic
fun getMusicService(context: Context): MusicService {
fun getMusicService(): MusicService {
return if (ActiveServerProvider.isOffline()) {
get(named(OFFLINE_MUSIC_SERVICE))
} else {

View File

@ -3,6 +3,7 @@ package org.moire.ultrasonic.subsonic
import android.app.Activity
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import org.koin.core.component.KoinApiExtension
import java.util.Collections
import java.util.LinkedList
import org.moire.ultrasonic.R
@ -18,6 +19,7 @@ import org.moire.ultrasonic.util.Util
/**
* Retrieves a list of songs and adds them to the now playing list
*/
@KoinApiExtension
class DownloadHandler(
val mediaPlayerController: MediaPlayerController,
val networkAndStorageChecker: NetworkAndStorageChecker
@ -200,7 +202,7 @@ class DownloadHandler(
@Throws(Throwable::class)
override fun doInBackground(): List<MusicDirectory.Entry> {
val musicService = getMusicService(activity)
val musicService = getMusicService()
val songs: MutableList<MusicDirectory.Entry> = LinkedList()
val root: MusicDirectory
if (!isOffline() && isArtist && Util.getShouldUseId3Tags()) {
@ -243,7 +245,7 @@ class DownloadHandler(
songs.add(song)
}
}
val musicService = getMusicService(activity)
val musicService = getMusicService()
for (
(id1, _, _, title) in parent.getChildren(
includeDirs = true,
@ -267,7 +269,7 @@ class DownloadHandler(
if (songs.size > maxSongs) {
return
}
val musicService = getMusicService(activity)
val musicService = getMusicService()
val artist = musicService.getArtist(id, "", false)
for ((id1) in artist.getChildren()) {
val albumDirectory = musicService.getAlbum(

View File

@ -76,7 +76,7 @@ class ShareHandler(val context: Context) {
ids.add(id)
}
}
val musicService = getMusicService(context)
val musicService = getMusicService()
var timeInMillis: Long = 0
if (shareDetails.Expiration != 0L) {
timeInMillis = shareDetails.Expiration

View File

@ -201,7 +201,7 @@ class SongView(context: Context) : UpdateView(context), Checkable {
song.starred = false
}
Thread {
val musicService = getMusicService(this@SongView.context)
val musicService = getMusicService()
try {
if (!isStarred) {
musicService.star(id, null, null)