package com.shukria.kiosklauncher.util import android.app.admin.DevicePolicyManager import android.content.ComponentName import android.content.Context import android.net.wifi.WifiManager import android.os.UserManager import android.util.Log import com.shukria.kiosklauncher.MyDeviceAdminReceiver import java.io.BufferedReader import java.io.InputStreamReader /** * Centralized Wi-Fi control utility that attempts multiple strategies to * disable Wi-Fi, handling both Device Owner and non-Device Owner scenarios. * * Strategies (tried in order): * 0. YSDK setProperties method (Morefun devices) * 1. DevicePolicyManager user restriction (Device Owner only) * 2. WifiManager.setWifiEnabled(false) (works on some custom ROMs) * 3. Shell command: "svc wifi disable" (works on Morefun devices) */ object WifiController { private const val TAG = "WifiController" /** * Attempts to disable Wi-Fi using every available privilege. * @return true if at least one method reports success */ fun disableWifi(context: Context): Boolean { val appContext = context.applicationContext val dpm = appContext.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager val admin = ComponentName(appContext, MyDeviceAdminReceiver::class.java) val wifiManager = appContext.getSystemService(Context.WIFI_SERVICE) as? WifiManager val isDeviceOwner = dpm.isDeviceOwnerApp(appContext.packageName) Log.i(TAG, "disableWifi called. isDeviceOwner=$isDeviceOwner, wifiEnabled=${wifiManager?.isWifiEnabled}") var anySuccess = false // ── Step 0: YSDK setProperties (Morefun devices) ── if (YSDKManager.isConnected()) { try { val success = YSDKManager.setWifiEnabled(false) Log.i(TAG, "Step 0: YSDKManager.setWifiEnabled(false) returned $success") if (success) anySuccess = true } catch (e: Exception) { Log.e(TAG, "Step 0: YSDKManager.setWifiEnabled(false) failed", e) } } else { Log.w(TAG, "Step 0: YSDK not connected") } // ── Step 1: Add DISALLOW_CONFIG_WIFI restriction (Device Owner only) ── if (isDeviceOwner) { try { dpm.addUserRestriction(admin, UserManager.DISALLOW_CONFIG_WIFI) Log.i(TAG, "Step 1: DISALLOW_CONFIG_WIFI restriction ADDED") anySuccess = true } catch (e: Exception) { Log.e(TAG, "Step 1: Failed to add DISALLOW_CONFIG_WIFI", e) } } else { Log.w(TAG, "Step 1: Skipped – app is NOT device owner") } // ── Step 2: WifiManager.setWifiEnabled(false) ── if (wifiManager?.isWifiEnabled == true) { try { @Suppress("DEPRECATION") val success = wifiManager.setWifiEnabled(false) Log.i(TAG, "Step 2: WifiManager.setWifiEnabled(false) returned $success") if (success) anySuccess = true } catch (e: Exception) { Log.e(TAG, "Step 2: WifiManager.setWifiEnabled(false) threw", e) } } else { Log.i(TAG, "Step 2: Wi-Fi already disabled, skipping") } // ── Step 3: Shell command "svc wifi disable" ── if (wifiManager?.isWifiEnabled == true) { try { val process = Runtime.getRuntime().exec("svc wifi disable") val exitCode = process.waitFor() val errorOutput = BufferedReader(InputStreamReader(process.errorStream)).readText().trim() Log.i(TAG, "Step 3: 'svc wifi disable' exitCode=$exitCode, error=$errorOutput") if (exitCode == 0) anySuccess = true } catch (e: Exception) { Log.e(TAG, "Step 3: 'svc wifi disable' failed", e) } } // ── Step 4: Shell command via "su" (if device is rooted) ── if (wifiManager?.isWifiEnabled == true) { try { val process = Runtime.getRuntime().exec(arrayOf("su", "-c", "svc wifi disable")) val exitCode = process.waitFor() val errorOutput = BufferedReader(InputStreamReader(process.errorStream)).readText().trim() Log.i(TAG, "Step 4: 'su -c svc wifi disable' exitCode=$exitCode, error=$errorOutput") if (exitCode == 0) anySuccess = true } catch (e: Exception) { Log.e(TAG, "Step 4: 'su -c svc wifi disable' failed", e) } } // ── Step 5: Shell "settings put global wifi_on 0" ── if (wifiManager?.isWifiEnabled == true) { try { val process = Runtime.getRuntime().exec(arrayOf("settings", "put", "global", "wifi_on", "0")) val exitCode = process.waitFor() val errorOutput = BufferedReader(InputStreamReader(process.errorStream)).readText().trim() Log.i(TAG, "Step 5: 'settings put global wifi_on 0' exitCode=$exitCode, error=$errorOutput") if (exitCode == 0) anySuccess = true } catch (e: Exception) { Log.e(TAG, "Step 5: 'settings put global wifi_on 0' failed", e) } } Log.i(TAG, "disableWifi finished. anySuccess=$anySuccess, wifiStillEnabled=${wifiManager?.isWifiEnabled}") return anySuccess } /** * Removes the DISALLOW_CONFIG_WIFI restriction and re-enables Wi-Fi. * Called when the Wi-Fi-check setting is turned off. */ fun allowWifi(context: Context) { val appContext = context.applicationContext val dpm = appContext.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager val admin = ComponentName(appContext, MyDeviceAdminReceiver::class.java) val isDeviceOwner = dpm.isDeviceOwnerApp(appContext.packageName) Log.i(TAG, "allowWifi called. isDeviceOwner=$isDeviceOwner") // ── Step 0: YSDK setProperties to enable Wi-Fi ── if (YSDKManager.isConnected()) { try { val success = YSDKManager.setWifiEnabled(true) Log.i(TAG, "allowWifi Step 0: YSDKManager.setWifiEnabled(true) returned $success") } catch (e: Exception) { Log.e(TAG, "allowWifi Step 0: YSDKManager.setWifiEnabled(true) failed", e) } } else { Log.w(TAG, "allowWifi Step 0: YSDK not connected") } if (isDeviceOwner) { try { dpm.clearUserRestriction(admin, UserManager.DISALLOW_CONFIG_WIFI) Log.i(TAG, "DISALLOW_CONFIG_WIFI restriction CLEARED") } catch (e: Exception) { Log.e(TAG, "Failed to clear DISALLOW_CONFIG_WIFI", e) } } else { Log.w(TAG, "allowWifi: Skipped DPM – app is NOT device owner") } } }