diff --git a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt index 2ab1aab7f..35178124f 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt @@ -10,6 +10,8 @@ import org.kiwix.kiwixlib.JNIKiwixServer import org.kiwix.kiwixmobile.di.ServiceScope import org.kiwix.kiwixmobile.webserver.WebServerHelper import org.kiwix.kiwixmobile.wifi_hotspot.HotspotNotificationManager +import org.kiwix.kiwixmobile.wifi_hotspot.HotspotStateReceiver +import org.kiwix.kiwixmobile.wifi_hotspot.HotspotStateReceiver.Callback import org.kiwix.kiwixmobile.wifi_hotspot.IpAddressCallbacks @Module @@ -44,4 +46,16 @@ class ServiceModule { context: Context ): HotspotNotificationManager = HotspotNotificationManager(notificationManager, context) + + @Provides + @ServiceScope + fun providesHotspotStateReceiver( + callback: Callback + ): HotspotStateReceiver = HotspotStateReceiver(callback) + + @Provides + @ServiceScope + fun providesHotspotStateReceiverCallback( + service: Service + ): HotspotStateReceiver.Callback = service as Callback } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index d0773afa9..71ddd2024 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -10,6 +10,7 @@ import androidx.annotation.Nullable; import javax.inject.Inject; import org.kiwix.kiwixmobile.KiwixApplication; import org.kiwix.kiwixmobile.R; +import org.kiwix.kiwixmobile.extensions.ContextExtensionsKt; import org.kiwix.kiwixmobile.utils.ServerUtils; import org.kiwix.kiwixmobile.webserver.WebServerHelper; import org.kiwix.kiwixmobile.webserver.ZimHostCallbacks; @@ -22,7 +23,8 @@ import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotNotificationManager.HOTS * Created by Adeel Zafar on 07/01/2019. */ -public class HotspotService extends Service implements IpAddressCallbacks { +public class HotspotService extends Service + implements IpAddressCallbacks, HotspotStateReceiver.Callback { public static final String ACTION_START_SERVER = "start_server"; public static final String ACTION_STOP_SERVER = "stop_server"; @@ -35,6 +37,8 @@ public class HotspotService extends Service implements IpAddressCallbacks { WebServerHelper webServerHelper; @Inject HotspotNotificationManager hotspotNotificationManager; + @Inject + HotspotStateReceiver hotspotStateReceiver; @Override public void onCreate() { KiwixApplication.getApplicationComponent() @@ -43,6 +47,12 @@ public class HotspotService extends Service implements IpAddressCallbacks { .build() .inject(this); super.onCreate(); + ContextExtensionsKt.registerReceiver(this, hotspotStateReceiver); + } + + @Override public void onDestroy() { + unregisterReceiver(hotspotStateReceiver); + super.onDestroy(); } @Override public int onStartCommand(@NonNull Intent intent, int flags, int startId) { @@ -111,6 +121,10 @@ public class HotspotService extends Service implements IpAddressCallbacks { } } + @Override public void onHotspotDisabled() { + stopHotspotAndDismissNotification(); + } + public class HotspotBinder extends Binder { @NonNull public HotspotService getService() { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotStateReceiver.kt b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotStateReceiver.kt new file mode 100644 index 000000000..03f273c97 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotStateReceiver.kt @@ -0,0 +1,63 @@ +/* + * Kiwix Android + * Copyright (C) 2018 Kiwix + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.kiwix.kiwixmobile.wifi_hotspot + +import android.content.Context +import android.content.Intent +import org.kiwix.kiwixmobile.wifi_hotspot.HotspotStateReceiver.HotspotState.DISABLED +import org.kiwix.kiwixmobile.zim_manager.BaseBroadcastReceiver +import javax.inject.Inject + +const val EXTRA_WIFI_AP_STATE = "wifi_state" +const val ACTION_WIFI_AP_STATE = "android.net.wifi.WIFI_AP_STATE_CHANGED" + +const val WIFI_AP_STATE_DISABLING = 10 +const val WIFI_AP_STATE_DISABLED = 11 +const val WIFI_AP_STATE_ENABLING = 12 +const val WIFI_AP_STATE_ENABLED = 13 +const val WIFI_AP_STATE_FAILED = 14 + +class HotspotStateReceiver @Inject constructor(val callback: Callback) : BaseBroadcastReceiver() { + override val action: String = ACTION_WIFI_AP_STATE + + override fun onIntentWithActionReceived(context: Context, intent: Intent) { + if (DISABLED == hotspotState(intent)) { + callback.onHotspotDisabled() + } + } + + private fun hotspotState(intent: Intent) = + HotspotState.from(intent.getIntExtra(EXTRA_WIFI_AP_STATE, -1)) + + interface Callback { + fun onHotspotDisabled() + } + + private enum class HotspotState(val state: Int) { + + DISABLING(WIFI_AP_STATE_DISABLING), + DISABLED(WIFI_AP_STATE_DISABLED), + ENABLING(WIFI_AP_STATE_ENABLING), + ENABLED(WIFI_AP_STATE_ENABLED), + FAILED(WIFI_AP_STATE_FAILED); + + companion object { + fun from(state: Int) = HotspotState.values().firstOrNull { state == it.state } + } + } +}