6
6
package org.erat.nup
7
7
8
8
import android.content.Context
9
- import android.net.wifi.WifiManager
10
- import android.net.wifi.WifiManager.WifiLock
11
9
import android.os.Environment
12
10
import android.os.SystemClock
13
11
import android.util.Log
@@ -50,13 +48,6 @@ class FileCache constructor(
50
48
// Status returned by [DownloadTask.startDownload] and [DownloadTask.writeFile].
51
49
private enum class DownloadStatus { SUCCESS , ABORTED , RETRYABLE_ERROR , FATAL_ERROR }
52
50
53
- // Current state of our use of the wifi connection.
54
- private enum class WifiState {
55
- ACTIVE , // We have an active download.
56
- WAITING , // No active downloads; waiting for another one to start before releasing the lock.
57
- INACTIVE , // No active downloads and the lock is released.
58
- }
59
-
60
51
private val executor = Executors .newSingleThreadScheduledExecutor()
61
52
private val threadChecker = ThreadChecker (executor)
62
53
@@ -72,10 +63,6 @@ class FileCache constructor(
72
63
private val readyLock: Lock = ReentrantLock ()
73
64
private val readyCond = readyLock.newCondition()
74
65
75
- private var wifiState = WifiState .INACTIVE
76
- private lateinit var wifiLock: WifiLock
77
- private var wifiLockFuture: ScheduledFuture <* >? = null // runs [updateWifiLock]
78
-
79
66
/* * Shut down the cache. */
80
67
fun quit () {
81
68
synchronized(inProgressSongIds) { inProgressSongIds.clear() }
@@ -113,7 +100,6 @@ class FileCache constructor(
113
100
}
114
101
inProgressSongIds.remove(songId)
115
102
}
116
- executor.execute { updateWifiLock() }
117
103
Log .d(TAG , " Canceled download of song $songId " )
118
104
}
119
105
@@ -129,7 +115,6 @@ class FileCache constructor(
129
115
waitUntilReady()
130
116
executor.execute {
131
117
synchronized(inProgressSongIds) { inProgressSongIds.clear() }
132
- updateWifiLock()
133
118
clearPinnedSongIds()
134
119
for (songId: Long in db.songIdsByAge) {
135
120
val entry = db.getEntry(songId) ? : continue
@@ -215,8 +200,6 @@ class FileCache constructor(
215
200
return
216
201
}
217
202
218
- updateWifiLock()
219
-
220
203
while (true ) {
221
204
try {
222
205
if (backoffTimeMs > 0 ) {
@@ -387,14 +370,12 @@ class FileCache constructor(
387
370
private fun handleFailure () {
388
371
threadChecker.assertThread()
389
372
synchronized(inProgressSongIds) { inProgressSongIds.remove(entry.songId) }
390
- updateWifiLock()
391
373
listenerExecutor.execute { listener.onCacheDownloadFail(entry, reason) }
392
374
}
393
375
394
376
private fun handleSuccess () {
395
377
threadChecker.assertThread()
396
378
synchronized(inProgressSongIds) { inProgressSongIds.remove(entry.songId) }
397
- updateWifiLock()
398
379
listenerExecutor.execute { listener.onCacheDownloadComplete(entry) }
399
380
}
400
381
@@ -508,51 +489,15 @@ class FileCache constructor(
508
489
return neededBytes <= availableBytes
509
490
}
510
491
511
- /* * Acquire or release the wifi lock, depending on our current state. */
512
- private fun updateWifiLock () {
513
- threadChecker.assertThread()
514
-
515
- wifiLockFuture?.cancel(false )
516
- wifiLockFuture = null
517
-
518
- val active = synchronized(inProgressSongIds) { ! inProgressSongIds.isEmpty() }
519
- when {
520
- active -> {
521
- Log .d(TAG , " Acquiring wifi lock" )
522
- wifiState = WifiState .ACTIVE
523
- wifiLock.acquire()
524
- }
525
- wifiState == WifiState .ACTIVE -> {
526
- Log .d(TAG , " Waiting $RELEASE_WIFI_LOCK_DELAY_SEC sec before releasing wifi lock" )
527
- wifiState = WifiState .WAITING
528
- wifiLockFuture = executor.schedule(
529
- { updateWifiLock() },
530
- RELEASE_WIFI_LOCK_DELAY_SEC ,
531
- TimeUnit .SECONDS
532
- )
533
- }
534
- else -> {
535
- Log .d(TAG , " Releasing wifi lock" )
536
- wifiState = WifiState .INACTIVE
537
- wifiLock.release()
538
- }
539
- }
540
- }
541
-
542
492
companion object {
543
493
private const val TAG = " FileCache"
544
- private const val RELEASE_WIFI_LOCK_DELAY_SEC = 600L
545
494
private const val SHUTDOWN_TIMEOUT_MS = 1000L
546
495
}
547
496
548
497
init {
549
498
// Avoid hitting the disk on the main thread.
550
499
// (Note that [waitUntilReady] will still hold everything up. :-/)
551
500
executor.execute {
552
- wifiLock = (context.getSystemService(Context .WIFI_SERVICE ) as WifiManager )
553
- .createWifiLock(WifiManager .WIFI_MODE_FULL , context.getString(R .string.app_name))
554
- wifiLock.setReferenceCounted(false )
555
-
556
501
val state = Environment .getExternalStorageState()
557
502
if (state != Environment .MEDIA_MOUNTED ) {
558
503
Log .e(TAG , " Media has state $state ; we need ${Environment .MEDIA_MOUNTED } " )
0 commit comments