Support Wi-Fi keep alives

This commit is contained in:
Mygod
2018-12-23 23:35:55 +08:00
parent c4260638f2
commit 55443902be
12 changed files with 131 additions and 2 deletions

View File

@@ -0,0 +1,61 @@
package be.mygod.vpnhotspot.net.wifi
import android.annotation.SuppressLint
import android.content.SharedPreferences
import android.net.wifi.WifiManager
import android.os.PowerManager
import androidx.core.content.getSystemService
import be.mygod.vpnhotspot.App.Companion.app
/**
* This mechanism is used to maximize profit. Source: https://stackoverflow.com/a/29657230/2245107
*/
class WifiDoubleLock(lockType: Int) : AutoCloseable {
companion object : SharedPreferences.OnSharedPreferenceChangeListener {
private const val KEY = "service.wifiLock"
private val lockType get() =
WifiDoubleLock.Mode.valueOf(app.pref.getString(KEY, WifiDoubleLock.Mode.Full.toString()) ?: "").lockType
private val service by lazy { app.getSystemService<PowerManager>()!! }
private var referenceCount = 0
private var lock: WifiDoubleLock? = null
fun acquire() = synchronized(this) {
if (referenceCount == 0) {
app.pref.registerOnSharedPreferenceChangeListener(this)
val lockType = lockType
if (lockType != null) lock = WifiDoubleLock(lockType)
}
referenceCount += 1
}
fun release() = synchronized(this) {
referenceCount -= 1
if (referenceCount == 0) {
lock?.close()
lock = null
app.pref.unregisterOnSharedPreferenceChangeListener(this)
}
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
if (key == KEY) synchronized(this) {
lock?.close()
val lockType = lockType
lock = if (lockType == null) null else WifiDoubleLock(lockType)
}
}
}
enum class Mode(val lockType: Int? = null) {
None, Full(WifiManager.WIFI_MODE_FULL), HighPerf(WifiManager.WIFI_MODE_FULL_HIGH_PERF)
}
private val wifi = app.wifi.createWifiLock(lockType, "vpnhotspot:wifi").apply { acquire() }
@SuppressLint("WakelockTimeout")
private val power = service.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "vpnhotspot:power").apply { acquire() }
override fun close() {
wifi.release()
power.release()
}
}