Now we can stop passing around the context for a lot of API calls

This commit is contained in:
tzugen 2021-05-09 09:24:05 +02:00
parent 9ec92c6cf3
commit 2c5f8d9dd0
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
13 changed files with 94 additions and 112 deletions

View File

@ -383,7 +383,7 @@ public class BookmarksFragment extends Fragment {
{
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
MusicDirectory dir = load(musicService);
boolean valid = musicService.isLicenseValid(getContext());
boolean valid = musicService.isLicenseValid();
return new Pair<>(dir, valid);
}

View File

@ -998,9 +998,9 @@ public class PlayerFragment extends Fragment implements GestureDetector.OnGestur
try {
if (isStarred) {
musicService.unstar(id, null, null, getContext());
musicService.unstar(id, null, null);
} else {
musicService.star(id, null, null, getContext());
musicService.star(id, null, null);
}
} catch (Exception e) {
Timber.e(e);

View File

@ -108,7 +108,7 @@ public class SelectGenreFragment extends Fragment {
try
{
genres = musicService.getGenres(refresh, getContext());
genres = musicService.getGenres(refresh);
}
catch (Exception x)
{

View File

@ -87,27 +87,27 @@ public class CachedMusicService implements MusicService
}
@Override
public void ping(Context context) throws Exception
public void ping() throws Exception
{
checkSettingsChanged();
musicService.ping(context);
musicService.ping();
}
@Override
public boolean isLicenseValid(Context context) throws Exception
public boolean isLicenseValid() throws Exception
{
checkSettingsChanged();
Boolean result = cachedLicenseValid.get();
if (result == null)
{
result = musicService.isLicenseValid(context);
result = musicService.isLicenseValid();
cachedLicenseValid.set(result, result ? 30L * 60L : 2L * 60L, TimeUnit.SECONDS);
}
return result;
}
@Override
public List<MusicFolder> getMusicFolders(boolean refresh, Context context) throws Exception
public List<MusicFolder> getMusicFolders(boolean refresh) throws Exception
{
checkSettingsChanged();
if (refresh)
@ -117,7 +117,7 @@ public class CachedMusicService implements MusicService
List<MusicFolder> result = cachedMusicFolders.get();
if (result == null)
{
result = musicService.getMusicFolders(refresh, context);
result = musicService.getMusicFolders(refresh);
cachedMusicFolders.set(result);
}
return result;
@ -143,7 +143,7 @@ public class CachedMusicService implements MusicService
}
@Override
public Indexes getArtists(boolean refresh, Context context) throws Exception
public Indexes getArtists(boolean refresh) throws Exception
{
checkSettingsChanged();
if (refresh)
@ -153,7 +153,7 @@ public class CachedMusicService implements MusicService
Indexes result = cachedArtists.get();
if (result == null)
{
result = musicService.getArtists(refresh, context);
result = musicService.getArtists(refresh);
cachedArtists.set(result);
}
return result;
@ -178,14 +178,14 @@ public class CachedMusicService implements MusicService
}
@Override
public MusicDirectory getArtist(String id, String name, boolean refresh, Context context) throws Exception
public MusicDirectory getArtist(String id, String name, boolean refresh) throws Exception
{
checkSettingsChanged();
TimeLimitedCache<MusicDirectory> cache = refresh ? null : cachedArtist.get(id);
MusicDirectory dir = cache == null ? null : cache.get();
if (dir == null)
{
dir = musicService.getArtist(id, name, refresh, context);
dir = musicService.getArtist(id, name, refresh);
cache = new TimeLimitedCache<>(Util.getDirectoryCacheTime(), TimeUnit.SECONDS);
cache.set(dir);
cachedArtist.put(id, cache);
@ -194,14 +194,14 @@ public class CachedMusicService implements MusicService
}
@Override
public MusicDirectory getAlbum(String id, String name, boolean refresh, Context context) throws Exception
public MusicDirectory getAlbum(String id, String name, boolean refresh) throws Exception
{
checkSettingsChanged();
TimeLimitedCache<MusicDirectory> cache = refresh ? null : cachedAlbum.get(id);
MusicDirectory dir = cache == null ? null : cache.get();
if (dir == null)
{
dir = musicService.getAlbum(id, name, refresh, context);
dir = musicService.getAlbum(id, name, refresh);
cache = new TimeLimitedCache<>(Util.getDirectoryCacheTime(), TimeUnit.SECONDS);
cache.set(dir);
cachedAlbum.put(id, cache);
@ -284,15 +284,15 @@ public class CachedMusicService implements MusicService
}
@Override
public MusicDirectory getAlbumList(String type, int size, int offset, String musicFolderId, Context context) throws Exception
public MusicDirectory getAlbumList(String type, int size, int offset, String musicFolderId) throws Exception
{
return musicService.getAlbumList(type, size, offset, musicFolderId, context);
return musicService.getAlbumList(type, size, offset, musicFolderId);
}
@Override
public MusicDirectory getAlbumList2(String type, int size, int offset, String musicFolderId, Context context) throws Exception
public MusicDirectory getAlbumList2(String type, int size, int offset, String musicFolderId) throws Exception
{
return musicService.getAlbumList2(type, size, offset, musicFolderId, context);
return musicService.getAlbumList2(type, size, offset, musicFolderId);
}
@Override
@ -302,15 +302,15 @@ public class CachedMusicService implements MusicService
}
@Override
public SearchResult getStarred(Context context) throws Exception
public SearchResult getStarred() throws Exception
{
return musicService.getStarred(context);
return musicService.getStarred();
}
@Override
public SearchResult getStarred2(Context context) throws Exception
public SearchResult getStarred2() throws Exception
{
return musicService.getStarred2(context);
return musicService.getStarred2();
}
@Override
@ -388,25 +388,25 @@ public class CachedMusicService implements MusicService
}
@Override
public void star(String id, String albumId, String artistId, Context context) throws Exception
public void star(String id, String albumId, String artistId) throws Exception
{
musicService.star(id, albumId, artistId, context);
musicService.star(id, albumId, artistId);
}
@Override
public void unstar(String id, String albumId, String artistId, Context context) throws Exception
public void unstar(String id, String albumId, String artistId) throws Exception
{
musicService.unstar(id, albumId, artistId, context);
musicService.unstar(id, albumId, artistId);
}
@Override
public void setRating(String id, int rating, Context context) throws Exception
public void setRating(String id, int rating) throws Exception
{
musicService.setRating(id, rating, context);
musicService.setRating(id, rating);
}
@Override
public List<Genre> getGenres(boolean refresh, Context context) throws Exception
public List<Genre> getGenres(boolean refresh) throws Exception
{
checkSettingsChanged();
if (refresh)
@ -417,7 +417,7 @@ public class CachedMusicService implements MusicService
if (result == null)
{
result = musicService.getGenres(refresh, context);
result = musicService.getGenres(refresh);
cachedGenres.set(result);
}

View File

@ -608,7 +608,7 @@ public class MediaPlayerControllerImpl implements MediaPlayerController
new Thread(() -> {
try
{
MusicServiceFactory.getMusicService(context).setRating(song.getId(), rating, context);
MusicServiceFactory.getMusicService(context).setRating(song.getId(), rating);
}
catch (Exception e)
{

View File

@ -35,7 +35,6 @@ import org.moire.ultrasonic.domain.SearchCriteria;
import org.moire.ultrasonic.domain.SearchResult;
import org.moire.ultrasonic.domain.Share;
import org.moire.ultrasonic.domain.UserInfo;
import org.moire.ultrasonic.util.CancellableTask;
import java.io.InputStream;
import java.util.List;
@ -48,29 +47,29 @@ import kotlin.Pair;
public interface MusicService
{
void ping(Context context) throws Exception;
void ping() throws Exception;
boolean isLicenseValid(Context context) throws Exception;
boolean isLicenseValid() throws Exception;
List<Genre> getGenres(boolean refresh, Context context) throws Exception;
List<Genre> getGenres(boolean refresh) throws Exception;
void star(String id, String albumId, String artistId, Context context) throws Exception;
void star(String id, String albumId, String artistId) throws Exception;
void unstar(String id, String albumId, String artistId, Context context) throws Exception;
void unstar(String id, String albumId, String artistId) throws Exception;
void setRating(String id, int rating, Context context) throws Exception;
void setRating(String id, int rating) throws Exception;
List<MusicFolder> getMusicFolders(boolean refresh, Context context) throws Exception;
List<MusicFolder> getMusicFolders(boolean refresh) throws Exception;
Indexes getIndexes(String musicFolderId, boolean refresh, Context context) throws Exception;
Indexes getArtists(boolean refresh, Context context) throws Exception;
Indexes getArtists(boolean refresh) throws Exception;
MusicDirectory getMusicDirectory(String id, String name, boolean refresh, Context context) throws Exception;
MusicDirectory getArtist(String id, String name, boolean refresh, Context context) throws Exception;
MusicDirectory getArtist(String id, String name, boolean refresh) throws Exception;
MusicDirectory getAlbum(String id, String name, boolean refresh, Context context) throws Exception;
MusicDirectory getAlbum(String id, String name, boolean refresh) throws Exception;
SearchResult search(SearchCriteria criteria, Context context) throws Exception;
@ -90,17 +89,17 @@ public interface MusicService
void scrobble(String id, boolean submission, Context context) throws Exception;
MusicDirectory getAlbumList(String type, int size, int offset, String musicFolderId, Context context) throws Exception;
MusicDirectory getAlbumList(String type, int size, int offset, String musicFolderId) throws Exception;
MusicDirectory getAlbumList2(String type, int size, int offset, String musicFolderId, Context context) throws Exception;
MusicDirectory getAlbumList2(String type, int size, int offset, String musicFolderId) throws Exception;
MusicDirectory getRandomSongs(int size, Context context) throws Exception;
MusicDirectory getSongsByGenre(String genre, int count, int offset, Context context) throws Exception;
SearchResult getStarred(Context context) throws Exception;
SearchResult getStarred() throws Exception;
SearchResult getStarred2(Context context) throws Exception;
SearchResult getStarred2() throws Exception;
Bitmap getCoverArt(Context context, MusicDirectory.Entry entry, int size, boolean saveToFile, boolean highQuality) throws Exception;

View File

@ -41,7 +41,6 @@ import org.moire.ultrasonic.domain.SearchCriteria;
import org.moire.ultrasonic.domain.SearchResult;
import org.moire.ultrasonic.domain.Share;
import org.moire.ultrasonic.domain.UserInfo;
import org.moire.ultrasonic.util.CancellableTask;
import org.moire.ultrasonic.util.Constants;
import org.moire.ultrasonic.util.FileUtil;
import org.moire.ultrasonic.util.Util;
@ -702,7 +701,7 @@ public class OfflineMusicService implements MusicService
}
@Override
public MusicDirectory getAlbumList(String type, int size, int offset, String musicFolderId, Context context) throws Exception
public MusicDirectory getAlbumList(String type, int size, int offset, String musicFolderId) throws Exception
{
throw new OfflineException("Album lists not available in offline mode");
}
@ -744,7 +743,7 @@ public class OfflineMusicService implements MusicService
}
@Override
public SearchResult getStarred(Context context) throws Exception
public SearchResult getStarred() throws Exception
{
throw new OfflineException("Starred not available in offline mode");
}
@ -756,7 +755,7 @@ public class OfflineMusicService implements MusicService
}
@Override
public List<Genre> getGenres(boolean refresh, Context context) throws Exception
public List<Genre> getGenres(boolean refresh) throws Exception
{
throw new OfflineException("Getting Genres not available in offline mode");
}
@ -792,24 +791,24 @@ public class OfflineMusicService implements MusicService
}
@Override
public void star(String id, String albumId, String artistId, Context context) throws Exception
public void star(String id, String albumId, String artistId) throws Exception
{
throw new OfflineException("Star not available in offline mode");
}
@Override
public void unstar(String id, String albumId, String artistId, Context context) throws Exception
public void unstar(String id, String albumId, String artistId) throws Exception
{
throw new OfflineException("UnStar not available in offline mode");
}
@Override
public List<MusicFolder> getMusicFolders(boolean refresh, Context context) throws Exception
public List<MusicFolder> getMusicFolders(boolean refresh) throws Exception
{
throw new OfflineException("Music folders not available in offline mode");
}
@Override
public MusicDirectory getAlbumList2(String type, int size, int offset, String musicFolderId, Context context) {
public MusicDirectory getAlbumList2(String type, int size, int offset, String musicFolderId) {
Timber.w("OfflineMusicService.getAlbumList2 was called but it isn't available");
return null;
}
@ -854,34 +853,34 @@ public class OfflineMusicService implements MusicService
}
@Override
public SearchResult getStarred2(Context context) {
public SearchResult getStarred2() {
Timber.w("OfflineMusicService.getStarred2 was called but it isn't available");
return null;
}
@Override
public void ping(Context context) {
public void ping() {
}
@Override
public boolean isLicenseValid(Context context) {
public boolean isLicenseValid() {
return true;
}
@Override
public Indexes getArtists(boolean refresh, Context context) {
public Indexes getArtists(boolean refresh) {
Timber.w("OfflineMusicService.getArtists was called but it isn't available");
return null;
}
@Override
public MusicDirectory getArtist(String id, String name, boolean refresh, Context context) {
public MusicDirectory getArtist(String id, String name, boolean refresh) {
Timber.w("OfflineMusicService.getArtist was called but it isn't available");
return null;
}
@Override
public MusicDirectory getAlbum(String id, String name, boolean refresh, Context context) {
public MusicDirectory getAlbum(String id, String name, boolean refresh) {
Timber.w("OfflineMusicService.getAlbum was called but it isn't available");
return null;
}
@ -899,7 +898,7 @@ public class OfflineMusicService implements MusicService
}
@Override
public void setRating(String id, int rating, Context context) {
public void setRating(String id, int rating) {
Timber.w("OfflineMusicService.setRating was called but it isn't available");
}

View File

@ -158,11 +158,11 @@ public class AlbumView extends UpdateView
{
if (!isStarred)
{
musicService.star(!useId3 ? id : null, useId3 ? id : null, null, getContext());
musicService.star(!useId3 ? id : null, useId3 ? id : null, null);
}
else
{
musicService.unstar(!useId3 ? id : null, useId3 ? id : null, null, getContext());
musicService.unstar(!useId3 ? id : null, useId3 ? id : null, null);
}
}
catch (Exception e)

View File

@ -85,14 +85,14 @@ class ArtistListModel(
try {
if (!isOffline && !useId3Tags) {
musicFolders.postValue(
musicService.getMusicFolders(refresh, context)
musicService.getMusicFolders(refresh)
)
}
val musicFolderId = activeServerProvider.getActiveServer().musicFolderId
val result = if (!isOffline && useId3Tags)
musicService.getArtists(refresh, context)
musicService.getArtists(refresh)
else musicService.getIndexes(musicFolderId, refresh, context)
val retrievedArtists: MutableList<Artist> =

View File

@ -42,7 +42,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
withContext(Dispatchers.IO) {
if (!ActiveServerProvider.isOffline(context)) {
val musicService = MusicServiceFactory.getMusicService(context)
musicFolders.postValue(musicService.getMusicFolders(refresh, context))
musicFolders.postValue(musicService.getMusicFolders(refresh))
}
}
}
@ -131,7 +131,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
var root = MusicDirectory()
val musicDirectory = service.getArtist(id, name, refresh, context)
val musicDirectory = service.getArtist(id, name, refresh)
if (Util.getShouldShowAllSongsByArtist() &&
musicDirectory.findChild(allSongsId) == null &&
@ -168,12 +168,12 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
val root = MusicDirectory()
val songs: MutableCollection<MusicDirectory.Entry> = LinkedList()
val artist = service.getArtist(parentId, "", false, context)
val artist = service.getArtist(parentId, "", false)
for ((id1) in artist.getChildren()) {
if (allSongsId != id1) {
val albumDirectory = service.getAlbum(
id1, "", false, context
id1, "", false
)
for (song in albumDirectory.getChildren()) {
@ -191,7 +191,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
}
root
} else {
service.getAlbum(id, name, refresh, context)
service.getAlbum(id, name, refresh)
}
currentDirectory.postValue(musicDirectory)
}
@ -211,12 +211,11 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
val service = MusicServiceFactory.getMusicService(context)
val musicDirectory: MusicDirectory
val context = context
if (Util.getShouldUseId3Tags()) {
musicDirectory = Util.getSongsFromSearchResult(service.getStarred2(context))
musicDirectory = Util.getSongsFromSearchResult(service.starred2)
} else {
musicDirectory = Util.getSongsFromSearchResult(service.getStarred(context))
musicDirectory = Util.getSongsFromSearchResult(service.starred)
}
currentDirectory.postValue(musicDirectory)
@ -303,12 +302,12 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
if (Util.getShouldUseId3Tags()) {
musicDirectory = service.getAlbumList2(
albumListType, size,
offset, musicFolderId, context
offset, musicFolderId
)
} else {
musicDirectory = service.getAlbumList(
albumListType, size,
offset, musicFolderId, context
offset, musicFolderId
)
}

View File

@ -75,12 +75,12 @@ open class RESTMusicService(
) : MusicService {
@Throws(Exception::class)
override fun ping(context: Context) {
override fun ping() {
responseChecker.callWithResponseCheck { api -> api.ping().execute() }
}
@Throws(Exception::class)
override fun isLicenseValid(context: Context): Boolean {
override fun isLicenseValid(): Boolean {
val response = responseChecker.callWithResponseCheck { api -> api.getLicense().execute() }
return response.body()!!.license.valid
@ -88,8 +88,7 @@ open class RESTMusicService(
@Throws(Exception::class)
override fun getMusicFolders(
refresh: Boolean,
context: Context
refresh: Boolean
): List<MusicFolder> {
val cachedMusicFolders = fileStorage.load(
MUSIC_FOLDER_STORAGE_NAME, getMusicFolderListSerializer()
@ -129,8 +128,7 @@ open class RESTMusicService(
@Throws(Exception::class)
override fun getArtists(
refresh: Boolean,
context: Context
refresh: Boolean
): Indexes {
val cachedArtists = fileStorage.load(ARTISTS_STORAGE_NAME, getIndexesSerializer())
if (cachedArtists != null && !refresh) return cachedArtists
@ -148,8 +146,7 @@ open class RESTMusicService(
override fun star(
id: String?,
albumId: String?,
artistId: String?,
context: Context
artistId: String?
) {
responseChecker.callWithResponseCheck { api -> api.star(id, albumId, artistId).execute() }
}
@ -158,8 +155,7 @@ open class RESTMusicService(
override fun unstar(
id: String?,
albumId: String?,
artistId: String?,
context: Context
artistId: String?
) {
responseChecker.callWithResponseCheck { api -> api.unstar(id, albumId, artistId).execute() }
}
@ -167,8 +163,7 @@ open class RESTMusicService(
@Throws(Exception::class)
override fun setRating(
id: String,
rating: Int,
context: Context
rating: Int
) {
responseChecker.callWithResponseCheck { api -> api.setRating(id, rating).execute() }
}
@ -191,8 +186,7 @@ open class RESTMusicService(
override fun getArtist(
id: String,
name: String?,
refresh: Boolean,
context: Context
refresh: Boolean
): MusicDirectory {
val response = responseChecker.callWithResponseCheck { api -> api.getArtist(id).execute() }
@ -203,8 +197,7 @@ open class RESTMusicService(
override fun getAlbum(
id: String,
name: String?,
refresh: Boolean,
context: Context
refresh: Boolean
): MusicDirectory {
val response = responseChecker.callWithResponseCheck { api -> api.getAlbum(id).execute() }
@ -446,8 +439,7 @@ open class RESTMusicService(
type: String,
size: Int,
offset: Int,
musicFolderId: String?,
context: Context
musicFolderId: String?
): MusicDirectory {
val response = responseChecker.callWithResponseCheck { api ->
api.getAlbumList(fromName(type), size, offset, null, null, null, musicFolderId)
@ -466,8 +458,7 @@ open class RESTMusicService(
type: String,
size: Int,
offset: Int,
musicFolderId: String?,
context: Context
musicFolderId: String?
): MusicDirectory {
val response = responseChecker.callWithResponseCheck { api ->
api.getAlbumList2(
@ -509,9 +500,7 @@ open class RESTMusicService(
}
@Throws(Exception::class)
override fun getStarred(
context: Context
): SearchResult {
override fun getStarred(): SearchResult {
val response = responseChecker.callWithResponseCheck { api ->
api.getStarred(null).execute()
}
@ -520,9 +509,7 @@ open class RESTMusicService(
}
@Throws(Exception::class)
override fun getStarred2(
context: Context
): SearchResult {
override fun getStarred2(): SearchResult {
val response = responseChecker.callWithResponseCheck { api ->
api.getStarred2(null).execute()
}
@ -743,8 +730,7 @@ open class RESTMusicService(
@Throws(Exception::class)
override fun getGenres(
refresh: Boolean,
context: Context
refresh: Boolean
): List<Genre> {
val response = responseChecker.callWithResponseCheck { api -> api.getGenres().execute() }

View File

@ -208,7 +208,7 @@ class DownloadHandler(
} else {
if (isDirectory) {
root = if (!isOffline(activity) && Util.getShouldUseId3Tags())
musicService.getAlbum(id, name, false, activity)
musicService.getAlbum(id, name, false)
else
musicService.getMusicDirectory(id, name, false, activity)
} else if (isShare) {
@ -253,7 +253,7 @@ class DownloadHandler(
val root: MusicDirectory = if (
!isOffline(activity) &&
Util.getShouldUseId3Tags()
) musicService.getAlbum(id1, title, false, activity)
) musicService.getAlbum(id1, title, false)
else musicService.getMusicDirectory(id1, title, false, activity)
getSongsRecursively(root, songs)
}
@ -268,13 +268,12 @@ class DownloadHandler(
return
}
val musicService = getMusicService(activity)
val artist = musicService.getArtist(id, "", false, activity)
val artist = musicService.getArtist(id, "", false)
for ((id1) in artist.getChildren()) {
val albumDirectory = musicService.getAlbum(
id1,
"",
false,
activity
false
)
for (song in albumDirectory.getChildren()) {
if (!song.isVideo) {

View File

@ -204,9 +204,9 @@ class SongView(context: Context) : UpdateView(context), Checkable {
val musicService = getMusicService(this@SongView.context)
try {
if (!isStarred) {
musicService.star(id, null, null, this@SongView.context)
musicService.star(id, null, null)
} else {
musicService.unstar(id, null, null, this@SongView.context)
musicService.unstar(id, null, null)
}
} catch (e: Exception) {
Timber.e(e)