Listen to hotspot state changes

This commit is contained in:
Sean Mac Gillicuddy 2019-09-23 14:30:18 +01:00
parent db7ce3b7ee
commit 32b349e5de
3 changed files with 92 additions and 1 deletions

View File

@ -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
}

View File

@ -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() {

View File

@ -0,0 +1,63 @@
/*
* Kiwix Android
* Copyright (C) 2018 Kiwix <android.kiwix.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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 }
}
}
}