Remove Context from Downloader & DownloadFile

This commit is contained in:
tzugen 2021-05-21 23:03:42 +02:00
parent 9b2e54b94e
commit b8fbbd8c49
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
14 changed files with 51 additions and 47 deletions

View File

@ -25,7 +25,7 @@
<ID>ImplicitDefaultLocale:LocalMediaPlayer.kt$LocalMediaPlayer.BufferTask$String.format("BufferTask (%s)", downloadFile)</ID>
<ID>ImplicitDefaultLocale:LocalMediaPlayer.kt$LocalMediaPlayer.CheckCompletionTask$String.format("CheckCompletionTask (%s)", downloadFile)</ID>
<ID>ImplicitDefaultLocale:ShareHandler.kt$ShareHandler$String.format("%d:%s", timeSpanAmount, timeSpanType)</ID>
<ID>ImplicitDefaultLocale:ShareHandler.kt$ShareHandler.&lt;no name provided&gt;$String.format("%s\n\n%s", Util.getShareGreeting(context), result.url)</ID>
<ID>ImplicitDefaultLocale:ShareHandler.kt$ShareHandler.&lt;no name provided&gt;$String.format("%s\n\n%s", Util.getShareGreeting(), result.url)</ID>
<ID>ImplicitDefaultLocale:SongView.kt$SongView$String.format("%02d.", trackNumber)</ID>
<ID>ImplicitDefaultLocale:SongView.kt$SongView$String.format("%s ", bitRate)</ID>
<ID>ImplicitDefaultLocale:SongView.kt$SongView$String.format("%s &gt; %s", suffix, transcodedSuffix)</ID>

View File

@ -139,7 +139,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
showArtistPicture = findPreference(Constants.PREFERENCES_KEY_SHOW_ARTIST_PICTURE);
setupServersCategory();
sharingDefaultGreeting.setText(Util.getShareGreeting(getActivity()));
sharingDefaultGreeting.setText(Util.getShareGreeting());
setupClearSearchPreference();
setupGaplessControlSettingsV14();
setupFeatureFlagsPreferences();

View File

@ -35,7 +35,6 @@ public class Downloader
private final ShufflePlayBuffer shufflePlayBuffer;
private final ExternalStorageMonitor externalStorageMonitor;
private final LocalMediaPlayer localMediaPlayer;
private final Context context;
// TODO: This is a circular reference, try to remove
private Lazy<JukeboxMediaPlayer> jukeboxMediaPlayer = inject(JukeboxMediaPlayer.class);
@ -45,10 +44,9 @@ public class Downloader
private ScheduledExecutorService executorService;
private long revision;
public Downloader(Context context, ShufflePlayBuffer shufflePlayBuffer, ExternalStorageMonitor externalStorageMonitor,
public Downloader(ShufflePlayBuffer shufflePlayBuffer, ExternalStorageMonitor externalStorageMonitor,
LocalMediaPlayer localMediaPlayer)
{
this.context = context;
this.shufflePlayBuffer = shufflePlayBuffer;
this.externalStorageMonitor = externalStorageMonitor;
this.localMediaPlayer = localMediaPlayer;
@ -100,10 +98,10 @@ public class Downloader
if (shufflePlayBuffer.isEnabled)
{
checkShufflePlay(context);
checkShufflePlay();
}
if (jukeboxMediaPlayer.getValue().isEnabled() || !Util.isNetworkConnected(context))
if (jukeboxMediaPlayer.getValue().isEnabled() || !Util.isNetworkConnected())
{
return;
}
@ -188,7 +186,7 @@ public class Downloader
DownloadFile downloadFile = backgroundDownloadList.get(i);
if (downloadFile.isWorkDone() && (!downloadFile.shouldSave() || downloadFile.isSaved()))
{
Util.scanMedia(context, downloadFile.getCompleteFile());
Util.scanMedia(downloadFile.getCompleteFile());
// Don't need to keep list like active song list
backgroundDownloadList.remove(i);
@ -316,7 +314,7 @@ public class Downloader
for (MusicDirectory.Entry song : songs)
{
DownloadFile downloadFile = new DownloadFile(context, song, save);
DownloadFile downloadFile = new DownloadFile(song, save);
downloadList.add(getCurrentPlayingIndex() + offset, downloadFile);
offset++;
}
@ -325,7 +323,7 @@ public class Downloader
{
for (MusicDirectory.Entry song : songs)
{
DownloadFile downloadFile = new DownloadFile(context, song, save);
DownloadFile downloadFile = new DownloadFile(song, save);
downloadList.add(downloadFile);
}
}
@ -336,7 +334,7 @@ public class Downloader
{
for (MusicDirectory.Entry song : songs)
{
DownloadFile downloadFile = new DownloadFile(context, song, save);
DownloadFile downloadFile = new DownloadFile(song, save);
backgroundDownloadList.add(downloadFile);
}
@ -376,7 +374,7 @@ public class Downloader
DownloadFile downloadFile = downloadFileCache.get(song);
if (downloadFile == null)
{
downloadFile = new DownloadFile(context, song, false);
downloadFile = new DownloadFile(song, false);
downloadFileCache.put(song, downloadFile);
}
return downloadFile;
@ -398,7 +396,7 @@ public class Downloader
}
}
private synchronized void checkShufflePlay(Context context)
private synchronized void checkShufflePlay()
{
// Get users desired random playlist size
int listSize = Util.getMaxSongs();
@ -412,7 +410,7 @@ public class Downloader
{
for (MusicDirectory.Entry song : shufflePlayBuffer.get(listSize - size))
{
DownloadFile downloadFile = new DownloadFile(context, song, false);
DownloadFile downloadFile = new DownloadFile(song, false);
downloadList.add(downloadFile);
revision++;
}
@ -426,7 +424,7 @@ public class Downloader
int songsToShift = currIndex - 2;
for (MusicDirectory.Entry song : shufflePlayBuffer.get(songsToShift))
{
downloadList.add(new DownloadFile(context, song, false));
downloadList.add(new DownloadFile(song, false));
downloadList.get(0).cancelDownload();
downloadList.remove(0);
revision++;

View File

@ -79,7 +79,7 @@ public class LegacyImageLoader implements Runnable, ImageLoader {
imageSizeDefault = drawable.getIntrinsicHeight();
}
imageSizeLarge = Util.getMaxDisplayMetric(context);
imageSizeLarge = Util.getMaxDisplayMetric();
createLargeUnknownImage(context);
createUnknownAvatarImage(context);
}

View File

@ -97,7 +97,7 @@ public class ShufflePlayBuffer
// Check if active server has changed.
clearBufferIfNecessary();
if (buffer.size() > REFILL_THRESHOLD || (!Util.isNetworkConnected(context) && !ActiveServerProvider.Companion.isOffline()))
if (buffer.size() > REFILL_THRESHOLD || (!Util.isNetworkConnected() && !ActiveServerProvider.Companion.isOffline()))
{
return;
}

View File

@ -167,10 +167,14 @@ public class Util
}
}
public static int getMaxBitRate(Context context)
{
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
public static ConnectivityManager getConnectivityManager() {
Context context = appContext();
return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
}
public static int getMaxBitRate()
{
ConnectivityManager manager = getConnectivityManager();
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if (networkInfo == null)
@ -548,9 +552,9 @@ public class Util
return null;
}
public static boolean isNetworkConnected(Context context)
public static boolean isNetworkConnected()
{
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
ConnectivityManager manager = getConnectivityManager();
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean connected = networkInfo != null && networkInfo.isConnected();
@ -648,9 +652,9 @@ public class Util
return bitmap;
}
public static WifiManager.WifiLock createWifiLock(Context context, String tag)
public static WifiManager.WifiLock createWifiLock(String tag)
{
WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiManager wm = (WifiManager) appContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
return wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, tag);
}
@ -945,15 +949,15 @@ public class Util
return size;
}
public static int getMinDisplayMetric(Context context)
public static int getMinDisplayMetric()
{
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
DisplayMetrics metrics = appContext().getResources().getDisplayMetrics();
return Math.min(metrics.widthPixels, metrics.heightPixels);
}
public static int getMaxDisplayMetric(Context context)
public static int getMaxDisplayMetric()
{
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
DisplayMetrics metrics = appContext().getResources().getDisplayMetrics();
return Math.max(metrics.widthPixels, metrics.heightPixels);
}
@ -1251,10 +1255,12 @@ public class Util
return preferences.getString(Constants.PREFERENCES_KEY_DEFAULT_SHARE_DESCRIPTION, "");
}
public static String getShareGreeting(Context context)
public static String getShareGreeting()
{
SharedPreferences preferences = getPreferences();
return preferences.getString(Constants.PREFERENCES_KEY_DEFAULT_SHARE_GREETING, String.format(context.getResources().getString(R.string.share_default_greeting), context.getResources().getString(R.string.common_appname)));
Context context = appContext();
String defaultVal = String.format(context.getResources().getString(R.string.share_default_greeting), context.getResources().getString(R.string.common_appname));
return preferences.getString(Constants.PREFERENCES_KEY_DEFAULT_SHARE_GREETING, defaultVal);
}
public static String getDefaultShareExpiration()
@ -1313,11 +1319,11 @@ public class Util
return preferences.getBoolean(Constants.PREFERENCES_KEY_SHOW_ALL_SONGS_BY_ARTIST, false);
}
public static void scanMedia(Context context, File file)
public static void scanMedia(File file)
{
Uri uri = Uri.fromFile(file);
Intent scanFileIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
context.sendBroadcast(scanFileIntent);
appContext().sendBroadcast(scanFileIntent);
}
public static int getImageLoaderConcurrency()

View File

@ -21,7 +21,7 @@ val mediaPlayerModule = module {
single { DownloadQueueSerializer(androidContext()) }
single { ExternalStorageMonitor() }
single { ShufflePlayBuffer(androidContext()) }
single { Downloader(androidContext(), get(), get(), get()) }
single { Downloader(get(), get(), get()) }
single { LocalMediaPlayer(get(), androidContext()) }
single { AudioFocusHandler(get()) }

View File

@ -51,7 +51,7 @@ class CommunicationErrorHandler {
}
fun getErrorMessage(error: Throwable, context: Context): String {
if (error is IOException && !Util.isNetworkConnected(context)) {
if (error is IOException && !Util.isNetworkConnected()) {
return context.resources.getString(R.string.background_task_no_network)
} else if (error is FileNotFoundException) {
return context.resources.getString(R.string.background_task_not_found)

View File

@ -21,6 +21,7 @@ import java.io.OutputStream
import java.io.RandomAccessFile
import org.koin.core.component.KoinApiExtension
import org.koin.java.KoinJavaComponent.inject
import org.moire.ultrasonic.app.UApp
import org.moire.ultrasonic.domain.MusicDirectory
import org.moire.ultrasonic.service.MusicServiceFactory.getMusicService
import org.moire.ultrasonic.util.CacheCleaner
@ -37,7 +38,6 @@ import timber.log.Timber
*/
@KoinApiExtension
class DownloadFile(
private val context: Context,
val song: MusicDirectory.Entry,
private val save: Boolean
) {
@ -48,7 +48,7 @@ class DownloadFile(
var isFailed = false
private var retryCount = MAX_RETRIES
private val desiredBitRate: Int = Util.getMaxBitRate(context)
private val desiredBitRate: Int = Util.getMaxBitRate()
@Volatile
private var isPlaying = false
@ -138,7 +138,7 @@ class DownloadFile(
Util.delete(completeFile)
Util.delete(saveFile)
Util.scanMedia(context, saveFile)
Util.scanMedia(saveFile)
}
fun unpin() {
@ -186,7 +186,7 @@ class DownloadFile(
} else if (completeWhenDone) {
if (save) {
Util.renameFile(partialFile, saveFile)
Util.scanMedia(context, saveFile)
Util.scanMedia(saveFile)
} else {
Util.renameFile(partialFile, completeFile)
}
@ -211,7 +211,7 @@ class DownloadFile(
var wifiLock: WifiLock? = null
try {
wakeLock = acquireWakeLock(wakeLock)
wifiLock = Util.createWifiLock(context, toString())
wifiLock = Util.createWifiLock(toString())
wifiLock.acquire()
if (saveFile.exists()) {
@ -285,7 +285,7 @@ class DownloadFile(
} else {
if (save) {
Util.renameFile(partialFile, saveFile)
Util.scanMedia(context, saveFile)
Util.scanMedia(saveFile)
} else {
Util.renameFile(partialFile, completeFile)
}
@ -317,6 +317,7 @@ class DownloadFile(
private fun acquireWakeLock(wakeLock: WakeLock?): WakeLock? {
var wakeLock1 = wakeLock
if (Util.isScreenLitOnDownload()) {
val context = UApp.applicationContext()
val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager
val flags = PowerManager.SCREEN_DIM_WAKE_LOCK or PowerManager.ON_AFTER_RELEASE
wakeLock1 = pm.newWakeLock(flags, toString())
@ -333,7 +334,7 @@ class DownloadFile(
private fun downloadAndSaveCoverArt(musicService: MusicService) {
try {
if (!TextUtils.isEmpty(song.coverArt)) {
val size = Util.getMinDisplayMetric(context)
val size = Util.getMinDisplayMetric()
musicService.getCoverArt(song, size, true, true)
}
} catch (e: Exception) {

View File

@ -397,7 +397,7 @@ class LocalMediaPlayer(
secondaryProgress = (percent.toDouble() / 100.toDouble() * progressBar.max).toInt()
if (song.transcodedContentType == null && Util.getMaxBitRate(context) == 0) {
if (song.transcodedContentType == null && Util.getMaxBitRate() == 0) {
progressBar?.secondaryProgress = secondaryProgress
}
}

View File

@ -478,12 +478,11 @@ class MediaPlayerService : Service() {
// Set Metadata
val metadata = MediaMetadataCompat.Builder()
val context = applicationContext
if (currentPlaying != null) {
try {
val song = currentPlaying.song
val cover = FileUtil.getAlbumArtBitmap(
song, Util.getMinDisplayMetric(context),
song, Util.getMinDisplayMetric(),
true
)
metadata.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, -1L)

View File

@ -12,7 +12,7 @@ class NetworkAndStorageChecker(val context: Context) {
fun warnIfNetworkOrStorageUnavailable() {
if (!Util.isExternalStoragePresent()) {
Util.toast(context, R.string.select_album_no_sdcard)
} else if (!isOffline() && !Util.isNetworkConnected(context)) {
} else if (!isOffline() && !Util.isNetworkConnected()) {
Util.toast(context, R.string.select_album_no_network)
}
}

View File

@ -91,7 +91,7 @@ class ShareHandler(val context: Context) {
intent.type = "text/plain"
intent.putExtra(
Intent.EXTRA_TEXT,
String.format("%s\n\n%s", Util.getShareGreeting(context), result.url)
String.format("%s\n\n%s", Util.getShareGreeting(), result.url)
)
fragment.activity?.startActivity(
Intent.createChooser(

View File

@ -10,7 +10,7 @@ import org.moire.ultrasonic.util.Util
*/
class VideoPlayer() {
fun playVideo(context: Context, entry: MusicDirectory.Entry?) {
if (!Util.isNetworkConnected(context)) {
if (!Util.isNetworkConnected()) {
Util.toast(context, R.string.select_album_no_network)
return
}