Skip to content
This repository was archived by the owner on Apr 18, 2023. It is now read-only.

Commit 5106bca

Browse files
committed
Remove wifi lock code from FileCache.
WIFI_MODE_FULL is deprecated and non-functional in API level 29. I think that Android is possibly more aggressive about staying connected to wifi now.
1 parent ec04642 commit 5106bca

File tree

1 file changed

+0
-55
lines changed

1 file changed

+0
-55
lines changed

nup/src/main/java/FileCache.kt

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
package org.erat.nup
77

88
import android.content.Context
9-
import android.net.wifi.WifiManager
10-
import android.net.wifi.WifiManager.WifiLock
119
import android.os.Environment
1210
import android.os.SystemClock
1311
import android.util.Log
@@ -50,13 +48,6 @@ class FileCache constructor(
5048
// Status returned by [DownloadTask.startDownload] and [DownloadTask.writeFile].
5149
private enum class DownloadStatus { SUCCESS, ABORTED, RETRYABLE_ERROR, FATAL_ERROR }
5250

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-
6051
private val executor = Executors.newSingleThreadScheduledExecutor()
6152
private val threadChecker = ThreadChecker(executor)
6253

@@ -72,10 +63,6 @@ class FileCache constructor(
7263
private val readyLock: Lock = ReentrantLock()
7364
private val readyCond = readyLock.newCondition()
7465

75-
private var wifiState = WifiState.INACTIVE
76-
private lateinit var wifiLock: WifiLock
77-
private var wifiLockFuture: ScheduledFuture<*>? = null // runs [updateWifiLock]
78-
7966
/** Shut down the cache. */
8067
fun quit() {
8168
synchronized(inProgressSongIds) { inProgressSongIds.clear() }
@@ -113,7 +100,6 @@ class FileCache constructor(
113100
}
114101
inProgressSongIds.remove(songId)
115102
}
116-
executor.execute { updateWifiLock() }
117103
Log.d(TAG, "Canceled download of song $songId")
118104
}
119105

@@ -129,7 +115,6 @@ class FileCache constructor(
129115
waitUntilReady()
130116
executor.execute {
131117
synchronized(inProgressSongIds) { inProgressSongIds.clear() }
132-
updateWifiLock()
133118
clearPinnedSongIds()
134119
for (songId: Long in db.songIdsByAge) {
135120
val entry = db.getEntry(songId) ?: continue
@@ -215,8 +200,6 @@ class FileCache constructor(
215200
return
216201
}
217202

218-
updateWifiLock()
219-
220203
while (true) {
221204
try {
222205
if (backoffTimeMs > 0) {
@@ -387,14 +370,12 @@ class FileCache constructor(
387370
private fun handleFailure() {
388371
threadChecker.assertThread()
389372
synchronized(inProgressSongIds) { inProgressSongIds.remove(entry.songId) }
390-
updateWifiLock()
391373
listenerExecutor.execute { listener.onCacheDownloadFail(entry, reason) }
392374
}
393375

394376
private fun handleSuccess() {
395377
threadChecker.assertThread()
396378
synchronized(inProgressSongIds) { inProgressSongIds.remove(entry.songId) }
397-
updateWifiLock()
398379
listenerExecutor.execute { listener.onCacheDownloadComplete(entry) }
399380
}
400381

@@ -508,51 +489,15 @@ class FileCache constructor(
508489
return neededBytes <= availableBytes
509490
}
510491

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-
542492
companion object {
543493
private const val TAG = "FileCache"
544-
private const val RELEASE_WIFI_LOCK_DELAY_SEC = 600L
545494
private const val SHUTDOWN_TIMEOUT_MS = 1000L
546495
}
547496

548497
init {
549498
// Avoid hitting the disk on the main thread.
550499
// (Note that [waitUntilReady] will still hold everything up. :-/)
551500
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-
556501
val state = Environment.getExternalStorageState()
557502
if (state != Environment.MEDIA_MOUNTED) {
558503
Log.e(TAG, "Media has state $state; we need ${Environment.MEDIA_MOUNTED}")

0 commit comments

Comments
 (0)