|
| 1 | +package io.homeassistant.companion.android.util |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.os.Build |
| 5 | +import android.os.Handler |
| 6 | +import android.os.Looper |
| 7 | +import androidx.annotation.RequiresApi |
| 8 | +import dagger.hilt.android.qualifiers.ApplicationContext |
| 9 | +import javax.inject.Inject |
| 10 | +import timber.log.Timber |
| 11 | +import java.util.concurrent.Executor |
| 12 | + |
| 13 | +/** |
| 14 | + * Helper class for managing Wi-Fi hotspot functionality. |
| 15 | + * Only supported on Android 11 (API 30) and above. |
| 16 | + */ |
| 17 | +class HotspotHelper @Inject constructor( |
| 18 | + @ApplicationContext private val context: Context |
| 19 | +) { |
| 20 | + companion object { |
| 21 | + private const val TETHERING_WIFI = 0 |
| 22 | + private const val METHOD_TETHERING_MANAGER = "TetheringManager" |
| 23 | + // private const val METHOD_CONNECTIVITY_MANAGER = "ConnectivityManager" |
| 24 | + } |
| 25 | + |
| 26 | + // Executor implementation to run on the main thread |
| 27 | + private val mainExecutor = Executor { command -> Handler(Looper.getMainLooper()).post(command) } |
| 28 | + |
| 29 | + /** |
| 30 | + * Enables the Wi-Fi hotspot. |
| 31 | + * Only works on Android 11+. |
| 32 | + * |
| 33 | + * @return true if operation was successful |
| 34 | + */ |
| 35 | + fun turnOnHotspot(): Boolean { |
| 36 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { |
| 37 | + Timber.i("Hotspot control not supported on Android versions below 11 (API 30)") |
| 38 | + return false |
| 39 | + } |
| 40 | + |
| 41 | + try { |
| 42 | + return enableHotspot() |
| 43 | + } catch (e: Exception) { |
| 44 | + Timber.e(e, "Failed to turn on hotspot") |
| 45 | + return false |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Disables the Wi-Fi hotspot. |
| 51 | + * Only works on Android 11+. |
| 52 | + * |
| 53 | + * @return true if operation was successful |
| 54 | + */ |
| 55 | + fun turnOffHotspot(): Boolean { |
| 56 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { |
| 57 | + Timber.i("Hotspot control not supported on Android versions below 11 (API 30)") |
| 58 | + return false |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + return disableHotspot() |
| 63 | + } catch (e: Exception) { |
| 64 | + Timber.e(e, "Failed to turn off hotspot") |
| 65 | + return false |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // API 30+ needed for TetheringManager |
| 70 | + @RequiresApi(Build.VERSION_CODES.R) |
| 71 | + private fun enableHotspot(): Boolean { |
| 72 | + // Try TetheringManager |
| 73 | + // Support least Android 11(API 30) Code.R |
| 74 | + if (enableHotspotWithTetheringManager()) { |
| 75 | + Timber.i("Hotspot enabled successfully using $METHOD_TETHERING_MANAGER") |
| 76 | + return true |
| 77 | + } |
| 78 | + |
| 79 | + // TODO: implement using ConnectivityManager as fallback |
| 80 | + |
| 81 | + Timber.e("Failed to enable hotspot: no viable method found") |
| 82 | + return false |
| 83 | + } |
| 84 | + |
| 85 | + @RequiresApi(Build.VERSION_CODES.R) |
| 86 | + private fun disableHotspot(): Boolean { |
| 87 | + // Try TetheringManager |
| 88 | + // Support least Android 11(API 30) Code.R |
| 89 | + if (disableHotspotWithTetheringManager()) { |
| 90 | + Timber.i("Hotspot disabled successfully using $METHOD_TETHERING_MANAGER") |
| 91 | + return true |
| 92 | + } |
| 93 | + |
| 94 | + // TODO: implement using ConnectivityManager as fallback |
| 95 | + |
| 96 | + Timber.e("Failed to disable hotspot: no viable method found") |
| 97 | + return false |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Attempts to enable hotspot using TetheringManager via reflection. |
| 102 | + */ |
| 103 | + @RequiresApi(Build.VERSION_CODES.R) |
| 104 | + private fun enableHotspotWithTetheringManager(): Boolean { |
| 105 | + try { |
| 106 | + val tetheringManagerClass = Class.forName("android.net.TetheringManager") |
| 107 | + val tetheringManager = context.getSystemService(tetheringManagerClass) ?: return false |
| 108 | + |
| 109 | + // Look for appropriate method |
| 110 | + for (method in tetheringManager.javaClass.methods) { |
| 111 | + if (method.name == "startTethering") { |
| 112 | + try { |
| 113 | + val callbackClass = Class.forName("android.net.TetheringManager\$StartTetheringCallback") |
| 114 | + |
| 115 | + // Try with TetheringRequest (preferred on newer devices) |
| 116 | + val tetheringRequestClass = Class.forName("android.net.TetheringManager\$TetheringRequest") |
| 117 | + val builderClass = Class.forName("android.net.TetheringManager\$TetheringRequest\$Builder") |
| 118 | + val builder = builderClass.getConstructor(Int::class.java).newInstance(TETHERING_WIFI) |
| 119 | + val request = builderClass.getMethod("build").invoke(builder) |
| 120 | + |
| 121 | + val startMethod = tetheringManager.javaClass.getMethod( |
| 122 | + "startTethering", |
| 123 | + tetheringRequestClass, |
| 124 | + Executor::class.java, |
| 125 | + callbackClass |
| 126 | + ) |
| 127 | + |
| 128 | + val callbackInstance = createCallbackInstance(callbackClass) |
| 129 | + |
| 130 | + Timber.d("Enabling hotspot with TetheringRequest approach") |
| 131 | + startMethod.invoke(tetheringManager, request, mainExecutor, callbackInstance) |
| 132 | + return true |
| 133 | + } |
| 134 | + catch (e: Exception) { |
| 135 | + Timber.e(e, "Error invoking TetheringManager.startTethering") |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + Timber.w("No suitable TetheringManager.startTethering method found") |
| 140 | + return false |
| 141 | + } catch (e: Exception) { |
| 142 | + Timber.e(e, "Error accessing TetheringManager") |
| 143 | + return false |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Creates appropriate callback instance through reflection. |
| 149 | + * Handles both interface and abstract class scenarios. |
| 150 | + */ |
| 151 | + private fun createCallbackInstance(callbackClass: Class<*>): Any { |
| 152 | + try { |
| 153 | + if (callbackClass.isInterface) { |
| 154 | + // For interfaces, use Proxy |
| 155 | + return java.lang.reflect.Proxy.newProxyInstance( |
| 156 | + callbackClass.classLoader, |
| 157 | + arrayOf(callbackClass) |
| 158 | + ) { _, method, _ -> |
| 159 | + when (method.name) { |
| 160 | + "onTetheringStarted" -> { |
| 161 | + Timber.d("Callback: Tethering started successfully") |
| 162 | + null |
| 163 | + } |
| 164 | + // this also causes when tethering already started |
| 165 | + "onTetheringFailed" -> { |
| 166 | + Timber.e("Callback: Tethering failed to start") |
| 167 | + null |
| 168 | + } |
| 169 | + else -> null |
| 170 | + } |
| 171 | + } |
| 172 | + } else { |
| 173 | + // For abstract classes, create instance via constructor |
| 174 | + val constructor = callbackClass.getDeclaredConstructor() |
| 175 | + constructor.isAccessible = true |
| 176 | + return constructor.newInstance() |
| 177 | + } |
| 178 | + } catch (e: Exception) { |
| 179 | + Timber.e(e, "Failed to create callback instance") |
| 180 | + throw e |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Attempts to disable hotspot using TetheringManager via reflection. |
| 186 | + */ |
| 187 | + @RequiresApi(Build.VERSION_CODES.R) |
| 188 | + private fun disableHotspotWithTetheringManager(): Boolean { |
| 189 | + try { |
| 190 | + val tetheringManagerClass = Class.forName("android.net.TetheringManager") |
| 191 | + val tetheringManager = context.getSystemService(tetheringManagerClass) ?: return false |
| 192 | + |
| 193 | + // Look for stopTethering method |
| 194 | + for (method in tetheringManager.javaClass.methods) { |
| 195 | + if (method.name == "stopTethering" && |
| 196 | + method.parameterTypes.size == 1 && |
| 197 | + method.parameterTypes[0] == Int::class.java) { |
| 198 | + try { |
| 199 | + Timber.d("Disabling hotspot with TetheringManager.stopTethering") |
| 200 | + method.invoke(tetheringManager, TETHERING_WIFI) |
| 201 | + return true |
| 202 | + } catch (e: Exception) { |
| 203 | + Timber.e(e, "Error invoking TetheringManager.stopTethering") |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + Timber.w("No suitable TetheringManager.stopTethering method found") |
| 208 | + return false |
| 209 | + } catch (e: Exception) { |
| 210 | + Timber.e(e, "Error accessing TetheringManager") |
| 211 | + return false |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments