Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,37 @@ object FeatureRegistry {
viewModel.setWirelessDisplayCertificationEnabled(enabled, context)
},

object : Feature(
id = "Auto turn off Wi-Fi",
title = R.string.wifi_auto_off_title,
iconRes = R.drawable.rounded_power_settings_new_24,
category = R.string.cat_connectivity,
description = R.string.wifi_auto_off_desc,
permissionKeys = listOf("ACCESSIBILITY"),
searchableSettings = listOf(
SearchSetting(
R.string.search_wifi_auto_off_title,
R.string.search_wifi_auto_off_desc,
"wifi_auto_off_toggle"
),
SearchSetting(
R.string.search_wifi_auto_off_timeout_title,
R.string.search_wifi_auto_off_timeout_desc,
"wifi_auto_off_timeout_slider"
)
),
parentFeatureId = "Networks"
) {
override fun isEnabled(viewModel: MainViewModel) =
viewModel.isWifiAutoOffEnabled.value

override fun isToggleEnabled(viewModel: MainViewModel, context: Context) =
viewModel.isAccessibilityEnabled.value

override fun onToggle(viewModel: MainViewModel, context: Context, enabled: Boolean) =
viewModel.setWifiAutoOffEnabled(enabled)
},

object : Feature(
id = "Watch",
title = R.string.feat_watch_title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ class ScreenOffAccessibilityService : AccessibilityService(), SensorEventListene
}
}

private var isWifiConnected = false
private var networkCallback: android.net.ConnectivityManager.NetworkCallback? = null
private val wifiAutoOffHandler = Handler(Looper.getMainLooper())
private val wifiAutoOffRunnable = Runnable {
android.util.Log.d("ScreenOffService", "Executing Wifi auto turn off")
com.sameerasw.essentials.utils.ShellUtils.runCommand(this, "svc wifi disable")
}

private fun schedulePocketFlashlightTurnOff() {
pocketFlashlightHandler.removeCallbacks(pocketFlashlightRunnable)
pocketFlashlightHandler.postDelayed(pocketFlashlightRunnable, 1500L)
Expand Down Expand Up @@ -213,6 +221,13 @@ class ScreenOffAccessibilityService : AccessibilityService(), SensorEventListene
updatePocketModeExcludedAppsSet()
} else if (key == SettingsRepository.KEY_SMART_PIXELS_ENABLED || key == SettingsRepository.KEY_SMART_PIXELS_INTENSITY) {
smartPixelsHandler.updateState()
} else if (key == "wifi_auto_off_enabled" || key == "wifi_auto_off_timeout") {
val isAutoOffEnabled = prefs.getBoolean("wifi_auto_off_enabled", false)
if (!isAutoOffEnabled) {
wifiAutoOffHandler.removeCallbacks(wifiAutoOffRunnable)
} else if (!isWifiConnected) {
checkAndScheduleWifiAutoOff()
}
}
}

Expand All @@ -235,6 +250,7 @@ class ScreenOffAccessibilityService : AccessibilityService(), SensorEventListene
flashlightHandler.register()
statusBarIconHandler.register()
smartPixelsHandler.init()
registerWifiNetworkCallback()

// Screen Receiver
screenReceiver = object : BroadcastReceiver() {
Expand Down Expand Up @@ -396,9 +412,83 @@ class ScreenOffAccessibilityService : AccessibilityService(), SensorEventListene
getSharedPreferences("essentials_prefs", MODE_PRIVATE)
.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener)
instance = null
unregisterWifiNetworkCallback()
super.onDestroy()
}

private fun registerWifiNetworkCallback() {
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as? android.net.ConnectivityManager ?: return

isWifiConnected = isWifiCurrentlyConnected(connectivityManager)

val networkRequest = android.net.NetworkRequest.Builder()
.addTransportType(android.net.NetworkCapabilities.TRANSPORT_WIFI)
.build()

val callback = object : android.net.ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: android.net.Network) {
super.onAvailable(network)
isWifiConnected = true
wifiAutoOffHandler.removeCallbacks(wifiAutoOffRunnable)
android.util.Log.d("ScreenOffService", "Wi-Fi Connected - Cancelled auto off timer")
}

override fun onLost(network: android.net.Network) {
super.onLost(network)
isWifiConnected = false
checkAndScheduleWifiAutoOff()
}
}

networkCallback = callback
try {
connectivityManager.registerNetworkCallback(networkRequest, callback)
} catch (e: Exception) {
android.util.Log.e("ScreenOffService", "Failed to register network callback", e)
}
}

private fun unregisterWifiNetworkCallback() {
wifiAutoOffHandler.removeCallbacks(wifiAutoOffRunnable)
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as? android.net.ConnectivityManager
networkCallback?.let {
try {
connectivityManager?.unregisterNetworkCallback(it)
} catch (e: Exception) {
// Ignore
}
}
networkCallback = null
}

private fun isWifiCurrentlyConnected(connectivityManager: android.net.ConnectivityManager): Boolean {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
val activeNetwork = connectivityManager.activeNetwork ?: return false
val capabilities = connectivityManager.getNetworkCapabilities(activeNetwork) ?: return false
return capabilities.hasTransport(android.net.NetworkCapabilities.TRANSPORT_WIFI)
} else {
@Suppress("DEPRECATION")
val activeNetworkInfo = connectivityManager.activeNetworkInfo
@Suppress("DEPRECATION")
return activeNetworkInfo != null && activeNetworkInfo.type == android.net.ConnectivityManager.TYPE_WIFI && activeNetworkInfo.isConnected
}
}

private fun checkAndScheduleWifiAutoOff() {
val isAutoOffEnabled = prefs.getBoolean("wifi_auto_off_enabled", false)
if (!isAutoOffEnabled) return

val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as? android.net.wifi.WifiManager
if (wifiManager?.isWifiEnabled != true) return

val timeoutSeconds = prefs.getFloat("wifi_auto_off_timeout", 60f)
val delayMs = (timeoutSeconds * 1000).toLong()

wifiAutoOffHandler.removeCallbacks(wifiAutoOffRunnable)
wifiAutoOffHandler.postDelayed(wifiAutoOffRunnable, delayMs)
android.util.Log.d("ScreenOffService", "Scheduled Wi-Fi auto turn off in $timeoutSeconds seconds")
}

override fun onAccessibilityEvent(event: AccessibilityEvent?) {
if (event == null) return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,33 @@ fun NetworksSettingsUI(
iconRes = R.drawable.rounded_cast_24,
modifier = Modifier.highlight(highlightSetting == "wireless_display_certification_toggle")
)

IconToggleItem(
title = stringResource(R.string.wifi_auto_off_title),
description = stringResource(R.string.wifi_auto_off_desc),
isChecked = viewModel.isWifiAutoOffEnabled.value,
onCheckedChange = { enabled ->
viewModel.setWifiAutoOffEnabled(enabled)
},
enabled = true,
iconRes = R.drawable.rounded_power_settings_new_24,
modifier = Modifier.highlight(highlightSetting == "wifi_auto_off_toggle")
)

ConfigSliderItem(
title = stringResource(R.string.wifi_auto_off_timeout_title),
value = viewModel.wifiAutoOffTimeout.floatValue,
onValueChange = { seconds ->
viewModel.setWifiAutoOffTimeout(seconds)
},
valueRange = 10f..300f,
steps = 29,
increment = 10f,
valueFormatter = { "${it.toInt()}s" },
enabled = viewModel.isWifiAutoOffEnabled.value,
iconRes = R.drawable.rounded_timer_24,
modifier = Modifier.highlight(highlightSetting == "wifi_auto_off_timeout_slider")
)
}
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,15 @@
<string name="smart_wifi_title">Smart WiFi</string>
<string name="smart_wifi_desc">Hide mobile data when WiFi is connected</string>
<string name="smart_data_desc">Hide mobile data in certain modes</string>

<string name="wifi_auto_off_title">Auto Turn Off Wi-Fi</string>
<string name="wifi_auto_off_desc">Automatically turn off Wi-Fi when disconnected from any network</string>
<string name="wifi_auto_off_timeout_title">Auto Turn Off Timeout</string>
<string name="wifi_auto_off_timeout_desc">Delay before Wi-Fi turns off: %1$d seconds</string>
<string name="search_wifi_auto_off_title">Auto Turn Off Wi-Fi</string>
<string name="search_wifi_auto_off_desc">Automatically turn off Wi-Fi when disconnected from network</string>
<string name="search_wifi_auto_off_timeout_title">Wi-Fi auto turn off timeout</string>
<string name="search_wifi_auto_off_timeout_desc">Adjust delay before Wi-Fi is automatically disabled</string>
<string name="action_reset_all_icons">Reset All Icons</string>
<string name="status_bar_section_advanced">More Settings</string>
<string name="stb_advanced_flags_title">Advanced (Requires Shizuku/Root)</string>
Expand Down
Loading