Skip to content

Commit

Permalink
Remove dependency on broadcast reciever.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyofrancis committed Dec 3, 2024
1 parent 860e26d commit 2768fc4
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void useAppContext() throws Exception {
progessInterval, fetchLogger, networkInfoProvider, retryOnNetworkGain,
downloadInfoUpdater, downloadManagerCoordinator,
listenerCoordinator, serverDownloader, false, storageResolver,
appContext, namespace, groupInfoProvider, FetchDefaults.DEFAULT_GLOBAL_AUTO_RETRY_ATTEMPTS, false);
namespace, groupInfoProvider, FetchDefaults.DEFAULT_GLOBAL_AUTO_RETRY_ATTEMPTS, false);
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.tonyodev.fetch2.downloader.DownloadManagerImpl;
import com.tonyodev.fetch2.downloader.DownloadManagerCoordinator;
import com.tonyodev.fetch2.fetch.LiveSettings;
import com.tonyodev.fetch2.helper.PriorityBackoffResetCallback;
import com.tonyodev.fetch2.provider.GroupInfoProvider;
import com.tonyodev.fetch2core.DefaultStorageResolver;
import com.tonyodev.fetch2core.Downloader;
Expand Down Expand Up @@ -72,11 +73,11 @@ public void useAppContext() throws Exception {
final GroupInfoProvider groupInfoProvider = new GroupInfoProvider(namespace, downloadProvider);
final ListenerCoordinator listenerCoordinator = new ListenerCoordinator(namespace, groupInfoProvider, downloadProvider, uiHandler);
final DefaultStorageResolver storageResolver = new DefaultStorageResolver(appContext, tempDir);
final DownloadManager downloadManager = new DownloadManagerImpl(client, concurrentLimit,
final DownloadManagerImpl downloadManager = new DownloadManagerImpl(client, concurrentLimit,
progessInterval, fetchLogger, networkInfoProvider, retryOnNetworkGain,
downloadInfoUpdater, downloadManagerCoordinator,
listenerCoordinator, serverDownloader, false, storageResolver,
appContext, namespace, groupInfoProvider, FetchDefaults.DEFAULT_GLOBAL_AUTO_RETRY_ATTEMPTS, false);
listenerCoordinator, serverDownloader, false, storageResolver
, namespace, groupInfoProvider, FetchDefaults.DEFAULT_GLOBAL_AUTO_RETRY_ATTEMPTS, false);
priorityListProcessorImpl = new PriorityListProcessorImpl(
new HandlerWrapper(namespace, null),
new DownloadProvider(databaseManagerWrapper),
Expand All @@ -85,9 +86,9 @@ public void useAppContext() throws Exception {
fetchLogger,
listenerCoordinator,
concurrentLimit,
appContext,
namespace,
PrioritySort.ASC);
downloadManager.setPriorityBackoffResetCallback((PriorityBackoffResetCallback) priorityListProcessorImpl);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.tonyodev.fetch2.fetch.FetchHandler;
import com.tonyodev.fetch2.fetch.FetchHandlerImpl;
import com.tonyodev.fetch2.fetch.LiveSettings;
import com.tonyodev.fetch2.helper.PriorityBackoffResetCallback;
import com.tonyodev.fetch2.provider.GroupInfoProvider;
import com.tonyodev.fetch2core.DefaultStorageResolver;
import com.tonyodev.fetch2core.Downloader;
Expand Down Expand Up @@ -89,11 +90,11 @@ public void useAppContext() throws Exception {
final GroupInfoProvider groupInfoProvider = new GroupInfoProvider(namespace, downloadProvider);
final ListenerCoordinator listenerCoordinator = new ListenerCoordinator(namespace, groupInfoProvider, downloadProvider, uiHandler);
final DefaultStorageResolver storageResolver = new DefaultStorageResolver(appContext, tempDir);
final DownloadManager downloadManager = new DownloadManagerImpl(client, concurrentLimit,
final DownloadManagerImpl downloadManager = new DownloadManagerImpl(client, concurrentLimit,
progessInterval, fetchLogger, networkInfoProvider, retryOnNetworkGain,
downloadInfoUpdater, downloadManagerCoordinator,
listenerCoordinator, serverDownloader, false, storageResolver,
appContext, namespace, groupInfoProvider, FetchDefaults.DEFAULT_GLOBAL_AUTO_RETRY_ATTEMPTS, false);
namespace, groupInfoProvider, FetchDefaults.DEFAULT_GLOBAL_AUTO_RETRY_ATTEMPTS, false);
priorityListProcessorImpl = new PriorityListProcessorImpl(
handlerWrapper,
new DownloadProvider(databaseManagerWrapper),
Expand All @@ -102,9 +103,9 @@ public void useAppContext() throws Exception {
fetchLogger,
listenerCoordinator,
concurrentLimit,
appContext,
namespace,
PrioritySort.ASC);
downloadManager.setPriorityBackoffResetCallback((PriorityBackoffResetCallback) priorityListProcessorImpl);
fetchHandler = new FetchHandlerImpl(namespace, databaseManagerWrapper, downloadManager,
priorityListProcessorImpl, fetchLogger, autoStart,
client, serverClient, listenerCoordinator, uiHandler, storageResolver, null,
Expand Down
2 changes: 0 additions & 2 deletions fetch2/src/main/java/com/tonyodev/fetch2/FetchIntent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

package com.tonyodev.fetch2

const val ACTION_QUEUE_BACKOFF_RESET = "com.tonyodev.fetch2.action.QUEUE_BACKOFF_RESET"

const val ACTION_TYPE_INVALID = -1
const val ACTION_TYPE_PAUSE = 0
const val ACTION_TYPE_RESUME = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.tonyodev.fetch2.downloader

import android.content.Context
import android.content.Intent
import com.tonyodev.fetch2.*
import com.tonyodev.fetch2.exception.FetchException
import com.tonyodev.fetch2.helper.DownloadInfoUpdater
import com.tonyodev.fetch2.helper.FileDownloaderDelegate
import com.tonyodev.fetch2.fetch.ListenerCoordinator
import com.tonyodev.fetch2.helper.PriorityBackoffResetCallback
import com.tonyodev.fetch2.provider.GroupInfoProvider
import com.tonyodev.fetch2.provider.NetworkInfoProvider
import com.tonyodev.fetch2.util.getRequestForDownload
Expand All @@ -26,14 +25,17 @@ class DownloadManagerImpl(private val httpDownloader: Downloader<*, *>,
private val fileServerDownloader: FileServerDownloader,
private val hashCheckingEnabled: Boolean,
private val storageResolver: StorageResolver,
private val context: Context,
private val namespace: String,
private val groupInfoProvider: GroupInfoProvider,
private val globalAutoRetryMaxAttempts: Int,
private val preAllocateFileOnCreation: Boolean) : DownloadManager {
private val preAllocateFileOnCreation: Boolean
) : DownloadManager {

private val lock = Any()
private var executor: ExecutorService? = getNewDownloadExecutorService(concurrentLimit)

var priorityBackoffResetCallback: PriorityBackoffResetCallback? = null

@Volatile
override var concurrentLimit: Int = concurrentLimit
set(value) {
Expand Down Expand Up @@ -108,10 +110,7 @@ class DownloadManagerImpl(private val httpDownloader: Downloader<*, *>,
logger.e("DownloadManager failed to start download $download", e)
} finally {
removeDownloadMappings(download)
val intent = Intent(ACTION_QUEUE_BACKOFF_RESET)
intent.setPackage(context.packageName)
intent.putExtra(EXTRA_NAMESPACE, namespace)
context.sendBroadcast(intent)
priorityBackoffResetCallback?.onResetBackoffTime(namespace)
}
}
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ object FetchModulesBuilder {
fileServerDownloader = fetchConfiguration.fileServerDownloader,
hashCheckingEnabled = fetchConfiguration.hashCheckingEnabled,
storageResolver = fetchConfiguration.storageResolver,
context = fetchConfiguration.appContext,
namespace = fetchConfiguration.namespace,
groupInfoProvider = groupInfoProvider,
globalAutoRetryMaxAttempts = fetchConfiguration.maxAutoRetryAttempts,
preAllocateFileOnCreation = fetchConfiguration.preAllocateFileOnCreation)
preAllocateFileOnCreation = fetchConfiguration.preAllocateFileOnCreation
)
priorityListProcessor = PriorityListProcessorImpl(
handlerWrapper = handlerWrapper,
downloadProvider = downloadProvider,
Expand All @@ -130,9 +130,9 @@ object FetchModulesBuilder {
logger = fetchConfiguration.logger,
listenerCoordinator = listenerCoordinator,
downloadConcurrentLimit = fetchConfiguration.concurrentLimit,
context = fetchConfiguration.appContext,
namespace = fetchConfiguration.namespace,
prioritySort = fetchConfiguration.prioritySort)
downloadManager.priorityBackoffResetCallback = priorityListProcessor
priorityListProcessor.globalNetworkType = fetchConfiguration.globalNetworkType
fetchHandler = fetchConfiguration.fetchHandler ?: FetchHandlerImpl(
namespace = fetchConfiguration.namespace,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.tonyodev.fetch2.helper

interface PriorityBackoffResetCallback {

fun onResetBackoffTime(namespace: String?)

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package com.tonyodev.fetch2.helper

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import com.tonyodev.fetch2.*
import com.tonyodev.fetch2.downloader.DownloadManager
import com.tonyodev.fetch2core.HandlerWrapper
Expand All @@ -27,10 +22,10 @@ class PriorityListProcessorImpl(private val handlerWrapper: HandlerWrapper,
private val listenerCoordinator: ListenerCoordinator,
@Volatile
override var downloadConcurrentLimit: Int,
private val context: Context,
private val namespace: String,
private val prioritySort: PrioritySort)
: PriorityListProcessor<Download> {
private val prioritySort: PrioritySort
)
: PriorityListProcessor<Download>, PriorityBackoffResetCallback {

private val lock = Any()
@Volatile
Expand All @@ -55,28 +50,9 @@ class PriorityListProcessorImpl(private val handlerWrapper: HandlerWrapper,
}
}
}
private val priorityBackoffResetReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (context != null && intent != null) {
when (intent.action) {
ACTION_QUEUE_BACKOFF_RESET -> {
if (!stopped && !paused && namespace == intent.getStringExtra(EXTRA_NAMESPACE)) {
resetBackOffTime()
}
}
}
}
}
}

init {
networkInfoProvider.registerNetworkChangeListener(networkChangeListener)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.registerReceiver(priorityBackoffResetReceiver, IntentFilter(ACTION_QUEUE_BACKOFF_RESET), Context.RECEIVER_NOT_EXPORTED)
}
else {
context.registerReceiver(priorityBackoffResetReceiver, IntentFilter(ACTION_QUEUE_BACKOFF_RESET))
}
}

private val priorityIteratorRunnable = Runnable {
Expand Down Expand Up @@ -178,6 +154,15 @@ class PriorityListProcessorImpl(private val handlerWrapper: HandlerWrapper,
}
}

override fun onResetBackoffTime(namespace: String?) {
handlerWrapper.post {
if (!stopped && !paused && this.namespace == namespace) {
resetBackOffTime()
}
}
}


private fun registerPriorityIterator() {
if (downloadConcurrentLimit > 0) {
handlerWrapper.postDelayed(priorityIteratorRunnable, backOffTime)
Expand Down Expand Up @@ -205,17 +190,13 @@ class PriorityListProcessorImpl(private val handlerWrapper: HandlerWrapper,

override fun sendBackOffResetSignal() {
synchronized(lock) {
val intent = Intent(ACTION_QUEUE_BACKOFF_RESET)
intent.setPackage(context.packageName)
intent.putExtra(EXTRA_NAMESPACE, namespace)
context.sendBroadcast(intent)
onResetBackoffTime(namespace)
}
}

override fun close() {
synchronized(lock) {
networkInfoProvider.unregisterNetworkChangeListener(networkChangeListener)
context.unregisterReceiver(priorityBackoffResetReceiver)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@file:JvmName("FetchAndroidExtensions")
@file:Suppress("DEPRECATION")

package com.tonyodev.fetch2core

Expand Down
2 changes: 1 addition & 1 deletion fetch2core/src/main/java/com/tonyodev/fetch2core/Extras.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ open class Extras(

companion object CREATOR : Parcelable.Creator<Extras> {

@Suppress("UNCHECKED_CAST")
@Suppress("UNCHECKED_CAST", "DEPRECATION")
override fun createFromParcel(source: Parcel): Extras {
return Extras(source.readSerializable() as HashMap<String, String>)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class FileResource : Parcelable, Serializable {

companion object CREATOR : Parcelable.Creator<FileResource> {

@Suppress("UNCHECKED_CAST")
@Suppress("UNCHECKED_CAST", "DEPRECATION")
override fun createFromParcel(source: Parcel): FileResource {
val fileResource = FileResource()
fileResource.id = source.readLong()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ open class MutableExtras(protected val mutableData: MutableMap<String, String> =

companion object CREATOR : Parcelable.Creator<MutableExtras> {

@Suppress("UNCHECKED_CAST")
@Suppress("UNCHECKED_CAST", "DEPRECATION")
override fun createFromParcel(source: Parcel): MutableExtras {
return MutableExtras((source.readSerializable() as HashMap<String, String>).toMutableMap())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ data class FileRequest(val type: Int = TYPE_INVALID,
const val FIELD_SIZE = "Size"
const val FIELD_PERSIST_CONNECTION = "Persist-Connection"

@Suppress("UNCHECKED_CAST")
@Suppress("UNCHECKED_CAST", "DEPRECATION")
override fun createFromParcel(source: Parcel): FileRequest {
return FileRequest(
type = source.readInt(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ abstract class FileResolver(context: Context) {
}
}
"file" -> {
val file = File(fileUri.path)
val file = File(fileUri.path.toString())
if (file.exists() && file.canWrite()) {
createUriInputResourceWrapper(FileInputStream(file), null)
} else {
Expand Down

0 comments on commit 2768fc4

Please sign in to comment.