Skip to content

Commit 2768fc4

Browse files
committed
Remove dependency on broadcast reciever.
1 parent 860e26d commit 2768fc4

File tree

14 files changed

+46
-58
lines changed

14 files changed

+46
-58
lines changed

fetch2/src/androidTest/java/com/tonyodev/fetch2/DownloadManagerInstrumentedTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void useAppContext() throws Exception {
7878
progessInterval, fetchLogger, networkInfoProvider, retryOnNetworkGain,
7979
downloadInfoUpdater, downloadManagerCoordinator,
8080
listenerCoordinator, serverDownloader, false, storageResolver,
81-
appContext, namespace, groupInfoProvider, FetchDefaults.DEFAULT_GLOBAL_AUTO_RETRY_ATTEMPTS, false);
81+
namespace, groupInfoProvider, FetchDefaults.DEFAULT_GLOBAL_AUTO_RETRY_ATTEMPTS, false);
8282
}
8383

8484
@After

fetch2/src/androidTest/java/com/tonyodev/fetch2/DownloadPriorityIteratorProcessorTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.tonyodev.fetch2.downloader.DownloadManagerImpl;
1717
import com.tonyodev.fetch2.downloader.DownloadManagerCoordinator;
1818
import com.tonyodev.fetch2.fetch.LiveSettings;
19+
import com.tonyodev.fetch2.helper.PriorityBackoffResetCallback;
1920
import com.tonyodev.fetch2.provider.GroupInfoProvider;
2021
import com.tonyodev.fetch2core.DefaultStorageResolver;
2122
import com.tonyodev.fetch2core.Downloader;
@@ -72,11 +73,11 @@ public void useAppContext() throws Exception {
7273
final GroupInfoProvider groupInfoProvider = new GroupInfoProvider(namespace, downloadProvider);
7374
final ListenerCoordinator listenerCoordinator = new ListenerCoordinator(namespace, groupInfoProvider, downloadProvider, uiHandler);
7475
final DefaultStorageResolver storageResolver = new DefaultStorageResolver(appContext, tempDir);
75-
final DownloadManager downloadManager = new DownloadManagerImpl(client, concurrentLimit,
76+
final DownloadManagerImpl downloadManager = new DownloadManagerImpl(client, concurrentLimit,
7677
progessInterval, fetchLogger, networkInfoProvider, retryOnNetworkGain,
7778
downloadInfoUpdater, downloadManagerCoordinator,
78-
listenerCoordinator, serverDownloader, false, storageResolver,
79-
appContext, namespace, groupInfoProvider, FetchDefaults.DEFAULT_GLOBAL_AUTO_RETRY_ATTEMPTS, false);
79+
listenerCoordinator, serverDownloader, false, storageResolver
80+
, namespace, groupInfoProvider, FetchDefaults.DEFAULT_GLOBAL_AUTO_RETRY_ATTEMPTS, false);
8081
priorityListProcessorImpl = new PriorityListProcessorImpl(
8182
new HandlerWrapper(namespace, null),
8283
new DownloadProvider(databaseManagerWrapper),
@@ -85,9 +86,9 @@ public void useAppContext() throws Exception {
8586
fetchLogger,
8687
listenerCoordinator,
8788
concurrentLimit,
88-
appContext,
8989
namespace,
9090
PrioritySort.ASC);
91+
downloadManager.setPriorityBackoffResetCallback((PriorityBackoffResetCallback) priorityListProcessorImpl);
9192
}
9293

9394
@Test

fetch2/src/androidTest/java/com/tonyodev/fetch2/FetchHandlerInstrumentedTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.tonyodev.fetch2.fetch.FetchHandler;
2020
import com.tonyodev.fetch2.fetch.FetchHandlerImpl;
2121
import com.tonyodev.fetch2.fetch.LiveSettings;
22+
import com.tonyodev.fetch2.helper.PriorityBackoffResetCallback;
2223
import com.tonyodev.fetch2.provider.GroupInfoProvider;
2324
import com.tonyodev.fetch2core.DefaultStorageResolver;
2425
import com.tonyodev.fetch2core.Downloader;
@@ -89,11 +90,11 @@ public void useAppContext() throws Exception {
8990
final GroupInfoProvider groupInfoProvider = new GroupInfoProvider(namespace, downloadProvider);
9091
final ListenerCoordinator listenerCoordinator = new ListenerCoordinator(namespace, groupInfoProvider, downloadProvider, uiHandler);
9192
final DefaultStorageResolver storageResolver = new DefaultStorageResolver(appContext, tempDir);
92-
final DownloadManager downloadManager = new DownloadManagerImpl(client, concurrentLimit,
93+
final DownloadManagerImpl downloadManager = new DownloadManagerImpl(client, concurrentLimit,
9394
progessInterval, fetchLogger, networkInfoProvider, retryOnNetworkGain,
9495
downloadInfoUpdater, downloadManagerCoordinator,
9596
listenerCoordinator, serverDownloader, false, storageResolver,
96-
appContext, namespace, groupInfoProvider, FetchDefaults.DEFAULT_GLOBAL_AUTO_RETRY_ATTEMPTS, false);
97+
namespace, groupInfoProvider, FetchDefaults.DEFAULT_GLOBAL_AUTO_RETRY_ATTEMPTS, false);
9798
priorityListProcessorImpl = new PriorityListProcessorImpl(
9899
handlerWrapper,
99100
new DownloadProvider(databaseManagerWrapper),
@@ -102,9 +103,9 @@ public void useAppContext() throws Exception {
102103
fetchLogger,
103104
listenerCoordinator,
104105
concurrentLimit,
105-
appContext,
106106
namespace,
107107
PrioritySort.ASC);
108+
downloadManager.setPriorityBackoffResetCallback((PriorityBackoffResetCallback) priorityListProcessorImpl);
108109
fetchHandler = new FetchHandlerImpl(namespace, databaseManagerWrapper, downloadManager,
109110
priorityListProcessorImpl, fetchLogger, autoStart,
110111
client, serverClient, listenerCoordinator, uiHandler, storageResolver, null,

fetch2/src/main/java/com/tonyodev/fetch2/FetchIntent.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
package com.tonyodev.fetch2
44

5-
const val ACTION_QUEUE_BACKOFF_RESET = "com.tonyodev.fetch2.action.QUEUE_BACKOFF_RESET"
6-
75
const val ACTION_TYPE_INVALID = -1
86
const val ACTION_TYPE_PAUSE = 0
97
const val ACTION_TYPE_RESUME = 1

fetch2/src/main/java/com/tonyodev/fetch2/downloader/DownloadManagerImpl.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.tonyodev.fetch2.downloader
22

3-
import android.content.Context
4-
import android.content.Intent
53
import com.tonyodev.fetch2.*
64
import com.tonyodev.fetch2.exception.FetchException
75
import com.tonyodev.fetch2.helper.DownloadInfoUpdater
86
import com.tonyodev.fetch2.helper.FileDownloaderDelegate
97
import com.tonyodev.fetch2.fetch.ListenerCoordinator
8+
import com.tonyodev.fetch2.helper.PriorityBackoffResetCallback
109
import com.tonyodev.fetch2.provider.GroupInfoProvider
1110
import com.tonyodev.fetch2.provider.NetworkInfoProvider
1211
import com.tonyodev.fetch2.util.getRequestForDownload
@@ -26,14 +25,17 @@ class DownloadManagerImpl(private val httpDownloader: Downloader<*, *>,
2625
private val fileServerDownloader: FileServerDownloader,
2726
private val hashCheckingEnabled: Boolean,
2827
private val storageResolver: StorageResolver,
29-
private val context: Context,
3028
private val namespace: String,
3129
private val groupInfoProvider: GroupInfoProvider,
3230
private val globalAutoRetryMaxAttempts: Int,
33-
private val preAllocateFileOnCreation: Boolean) : DownloadManager {
31+
private val preAllocateFileOnCreation: Boolean
32+
) : DownloadManager {
3433

3534
private val lock = Any()
3635
private var executor: ExecutorService? = getNewDownloadExecutorService(concurrentLimit)
36+
37+
var priorityBackoffResetCallback: PriorityBackoffResetCallback? = null
38+
3739
@Volatile
3840
override var concurrentLimit: Int = concurrentLimit
3941
set(value) {
@@ -108,10 +110,7 @@ class DownloadManagerImpl(private val httpDownloader: Downloader<*, *>,
108110
logger.e("DownloadManager failed to start download $download", e)
109111
} finally {
110112
removeDownloadMappings(download)
111-
val intent = Intent(ACTION_QUEUE_BACKOFF_RESET)
112-
intent.setPackage(context.packageName)
113-
intent.putExtra(EXTRA_NAMESPACE, namespace)
114-
context.sendBroadcast(intent)
113+
priorityBackoffResetCallback?.onResetBackoffTime(namespace)
115114
}
116115
}
117116
return true

fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchModulesBuilder.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ object FetchModulesBuilder {
117117
fileServerDownloader = fetchConfiguration.fileServerDownloader,
118118
hashCheckingEnabled = fetchConfiguration.hashCheckingEnabled,
119119
storageResolver = fetchConfiguration.storageResolver,
120-
context = fetchConfiguration.appContext,
121120
namespace = fetchConfiguration.namespace,
122121
groupInfoProvider = groupInfoProvider,
123122
globalAutoRetryMaxAttempts = fetchConfiguration.maxAutoRetryAttempts,
124-
preAllocateFileOnCreation = fetchConfiguration.preAllocateFileOnCreation)
123+
preAllocateFileOnCreation = fetchConfiguration.preAllocateFileOnCreation
124+
)
125125
priorityListProcessor = PriorityListProcessorImpl(
126126
handlerWrapper = handlerWrapper,
127127
downloadProvider = downloadProvider,
@@ -130,9 +130,9 @@ object FetchModulesBuilder {
130130
logger = fetchConfiguration.logger,
131131
listenerCoordinator = listenerCoordinator,
132132
downloadConcurrentLimit = fetchConfiguration.concurrentLimit,
133-
context = fetchConfiguration.appContext,
134133
namespace = fetchConfiguration.namespace,
135134
prioritySort = fetchConfiguration.prioritySort)
135+
downloadManager.priorityBackoffResetCallback = priorityListProcessor
136136
priorityListProcessor.globalNetworkType = fetchConfiguration.globalNetworkType
137137
fetchHandler = fetchConfiguration.fetchHandler ?: FetchHandlerImpl(
138138
namespace = fetchConfiguration.namespace,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.tonyodev.fetch2.helper
2+
3+
interface PriorityBackoffResetCallback {
4+
5+
fun onResetBackoffTime(namespace: String?)
6+
7+
}

fetch2/src/main/java/com/tonyodev/fetch2/helper/PriorityListProcessorImpl.kt

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package com.tonyodev.fetch2.helper
22

33
import android.annotation.SuppressLint
4-
import android.content.BroadcastReceiver
5-
import android.content.Context
6-
import android.content.Intent
7-
import android.content.IntentFilter
8-
import android.os.Build
94
import com.tonyodev.fetch2.*
105
import com.tonyodev.fetch2.downloader.DownloadManager
116
import com.tonyodev.fetch2core.HandlerWrapper
@@ -27,10 +22,10 @@ class PriorityListProcessorImpl(private val handlerWrapper: HandlerWrapper,
2722
private val listenerCoordinator: ListenerCoordinator,
2823
@Volatile
2924
override var downloadConcurrentLimit: Int,
30-
private val context: Context,
3125
private val namespace: String,
32-
private val prioritySort: PrioritySort)
33-
: PriorityListProcessor<Download> {
26+
private val prioritySort: PrioritySort
27+
)
28+
: PriorityListProcessor<Download>, PriorityBackoffResetCallback {
3429

3530
private val lock = Any()
3631
@Volatile
@@ -55,28 +50,9 @@ class PriorityListProcessorImpl(private val handlerWrapper: HandlerWrapper,
5550
}
5651
}
5752
}
58-
private val priorityBackoffResetReceiver: BroadcastReceiver = object : BroadcastReceiver() {
59-
override fun onReceive(context: Context?, intent: Intent?) {
60-
if (context != null && intent != null) {
61-
when (intent.action) {
62-
ACTION_QUEUE_BACKOFF_RESET -> {
63-
if (!stopped && !paused && namespace == intent.getStringExtra(EXTRA_NAMESPACE)) {
64-
resetBackOffTime()
65-
}
66-
}
67-
}
68-
}
69-
}
70-
}
7153

7254
init {
7355
networkInfoProvider.registerNetworkChangeListener(networkChangeListener)
74-
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
75-
context.registerReceiver(priorityBackoffResetReceiver, IntentFilter(ACTION_QUEUE_BACKOFF_RESET), Context.RECEIVER_NOT_EXPORTED)
76-
}
77-
else {
78-
context.registerReceiver(priorityBackoffResetReceiver, IntentFilter(ACTION_QUEUE_BACKOFF_RESET))
79-
}
8056
}
8157

8258
private val priorityIteratorRunnable = Runnable {
@@ -178,6 +154,15 @@ class PriorityListProcessorImpl(private val handlerWrapper: HandlerWrapper,
178154
}
179155
}
180156

157+
override fun onResetBackoffTime(namespace: String?) {
158+
handlerWrapper.post {
159+
if (!stopped && !paused && this.namespace == namespace) {
160+
resetBackOffTime()
161+
}
162+
}
163+
}
164+
165+
181166
private fun registerPriorityIterator() {
182167
if (downloadConcurrentLimit > 0) {
183168
handlerWrapper.postDelayed(priorityIteratorRunnable, backOffTime)
@@ -205,17 +190,13 @@ class PriorityListProcessorImpl(private val handlerWrapper: HandlerWrapper,
205190

206191
override fun sendBackOffResetSignal() {
207192
synchronized(lock) {
208-
val intent = Intent(ACTION_QUEUE_BACKOFF_RESET)
209-
intent.setPackage(context.packageName)
210-
intent.putExtra(EXTRA_NAMESPACE, namespace)
211-
context.sendBroadcast(intent)
193+
onResetBackoffTime(namespace)
212194
}
213195
}
214196

215197
override fun close() {
216198
synchronized(lock) {
217199
networkInfoProvider.unregisterNetworkChangeListener(networkChangeListener)
218-
context.unregisterReceiver(priorityBackoffResetReceiver)
219200
}
220201
}
221202

fetch2core/src/main/java/com/tonyodev/fetch2core/AndroidExtentions.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@file:JvmName("FetchAndroidExtensions")
2+
@file:Suppress("DEPRECATION")
23

34
package com.tonyodev.fetch2core
45

fetch2core/src/main/java/com/tonyodev/fetch2core/Extras.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ open class Extras(
173173

174174
companion object CREATOR : Parcelable.Creator<Extras> {
175175

176-
@Suppress("UNCHECKED_CAST")
176+
@Suppress("UNCHECKED_CAST", "DEPRECATION")
177177
override fun createFromParcel(source: Parcel): Extras {
178178
return Extras(source.readSerializable() as HashMap<String, String>)
179179
}

fetch2core/src/main/java/com/tonyodev/fetch2core/FileResource.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class FileResource : Parcelable, Serializable {
8383

8484
companion object CREATOR : Parcelable.Creator<FileResource> {
8585

86-
@Suppress("UNCHECKED_CAST")
86+
@Suppress("UNCHECKED_CAST", "DEPRECATION")
8787
override fun createFromParcel(source: Parcel): FileResource {
8888
val fileResource = FileResource()
8989
fileResource.id = source.readLong()

fetch2core/src/main/java/com/tonyodev/fetch2core/MutableExtras.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ open class MutableExtras(protected val mutableData: MutableMap<String, String> =
107107

108108
companion object CREATOR : Parcelable.Creator<MutableExtras> {
109109

110-
@Suppress("UNCHECKED_CAST")
110+
@Suppress("UNCHECKED_CAST", "DEPRECATION")
111111
override fun createFromParcel(source: Parcel): MutableExtras {
112112
return MutableExtras((source.readSerializable() as HashMap<String, String>).toMutableMap())
113113
}

fetch2core/src/main/java/com/tonyodev/fetch2core/server/FileRequest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ data class FileRequest(val type: Int = TYPE_INVALID,
7575
const val FIELD_SIZE = "Size"
7676
const val FIELD_PERSIST_CONNECTION = "Persist-Connection"
7777

78-
@Suppress("UNCHECKED_CAST")
78+
@Suppress("UNCHECKED_CAST", "DEPRECATION")
7979
override fun createFromParcel(source: Parcel): FileRequest {
8080
return FileRequest(
8181
type = source.readInt(),

fetch2fileserver/src/main/java/com/tonyodev/fetch2fileserver/FileResolver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ abstract class FileResolver(context: Context) {
6565
}
6666
}
6767
"file" -> {
68-
val file = File(fileUri.path)
68+
val file = File(fileUri.path.toString())
6969
if (file.exists() && file.canWrite()) {
7070
createUriInputResourceWrapper(FileInputStream(file), null)
7171
} else {

0 commit comments

Comments
 (0)