Add HotspotStateListener

This commit is contained in:
Adeel 2019-08-23 16:38:25 +05:00
parent 316b9d9550
commit aee5c81baa
3 changed files with 50 additions and 14 deletions

View File

@ -9,6 +9,7 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiConfiguration;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
@ -31,7 +32,7 @@ import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.SELECTED_ZIM_PATHS
* Created by Adeel Zafar on 07/01/2019.
*/
public class HotspotService extends Service {
public class HotspotService extends Service implements HotspotStateListener {
public static final String ACTION_TURN_ON_AFTER_O = "Turn_on_hotspot_after_oreo";
public static final String ACTION_TURN_OFF_AFTER_O = "Turn_off_hotspot_after_oreo";
@ -55,6 +56,7 @@ public class HotspotService extends Service {
super.onCreate();
hotspotManager = new WifiHotspotManager(this);
hotspotManager.registerListener(HotspotService.this);
stopReceiver = new BroadcastReceiver() {
@Override
@ -83,7 +85,7 @@ public class HotspotService extends Service {
case ACTION_TURN_ON_AFTER_O:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
hotspotManager.turnOnHotspot(zimHostCallbacks);
hotspotManager.turnOnHotspot();
}
break;
@ -154,12 +156,13 @@ public class HotspotService extends Service {
void stopHotspotAndDismissNotification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
hotspotManager.turnOffHotspot();
} else {
webServerHelper.stopAndroidWebServer();
zimHostCallbacks.onServerStopped();
stopForeground(true);
stopSelf();
notificationManager.cancel(HOTSPOT_NOTIFICATION_ID);
}
webServerHelper.stopAndroidWebServer();
zimHostCallbacks.onServerStopped();
stopForeground(true);
stopSelf();
notificationManager.cancel(HOTSPOT_NOTIFICATION_ID);
}
@Override
@ -191,6 +194,23 @@ public class HotspotService extends Service {
buildForegroundNotification(getString(R.string.hotspot_running)));
}
@Override public void onHotspotTurnedOn(@NonNull WifiConfiguration wifiConfiguration) {
zimHostCallbacks.onHotspotTurnedOn(wifiConfiguration);
}
@Override public void onHotspotFailedToStart() {
zimHostCallbacks.onHotspotFailedToStart();
}
@Override public void onHotspotStopped() {
Log.v("DANG", "Inside Hotspot Service onHotspotStopped()");
webServerHelper.stopAndroidWebServer();
zimHostCallbacks.onServerStopped();
stopForeground(true);
stopSelf();
notificationManager.cancel(HOTSPOT_NOTIFICATION_ID);
}
public class HotspotBinder extends Binder {
@NonNull public HotspotService getService() {

View File

@ -0,0 +1,12 @@
package org.kiwix.kiwixmobile.wifi_hotspot;
import android.net.wifi.WifiConfiguration;
import androidx.annotation.NonNull;
public interface HotspotStateListener {
void onHotspotTurnedOn(@NonNull WifiConfiguration wifiConfiguration);
void onHotspotFailedToStart();
void onHotspotStopped();
}

View File

@ -8,7 +8,6 @@ import android.os.Handler;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import org.kiwix.kiwixmobile.webserver.ZimHostCallbacks;
/**
* WifiHotstopManager class makes use of the Android's WifiManager and WifiConfiguration class
@ -18,7 +17,8 @@ import org.kiwix.kiwixmobile.webserver.ZimHostCallbacks;
public class WifiHotspotManager {
private final WifiManager wifiManager;
WifiManager.LocalOnlyHotspotReservation hotspotReservation;
private WifiManager.LocalOnlyHotspotReservation hotspotReservation;
private HotspotStateListener hotspotStateListener;
private static final String TAG = "WifiHotspotManager";
public WifiHotspotManager(@NonNull Context context) {
@ -28,7 +28,7 @@ public class WifiHotspotManager {
//Workaround to turn on hotspot for Oreo versions
@RequiresApi(api = Build.VERSION_CODES.O)
public void turnOnHotspot(@NonNull ZimHostCallbacks zimHostCallbacks) {
public void turnOnHotspot() {
wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
@Override
@ -38,22 +38,21 @@ public class WifiHotspotManager {
WifiConfiguration currentConfig = hotspotReservation.getWifiConfiguration();
printCurrentConfig(currentConfig);
zimHostCallbacks.onHotspotTurnedOn(currentConfig);
hotspotStateListener.onHotspotTurnedOn(currentConfig);
Log.v(TAG, "Local Hotspot Started");
}
@Override
public void onStopped() {
super.onStopped();
zimHostCallbacks.onServerStopped();
hotspotStateListener.onHotspotStopped();
Log.v(TAG, "Local Hotspot Stopped");
}
@Override
public void onFailed(int reason) {
super.onFailed(reason);
zimHostCallbacks.onHotspotFailedToStart();
hotspotStateListener.onHotspotFailedToStart();
Log.v(TAG, "Local Hotspot failed to start");
}
}, new Handler());
@ -65,6 +64,7 @@ public class WifiHotspotManager {
if (hotspotReservation != null) {
hotspotReservation.close();
hotspotReservation = null;
hotspotStateListener.onHotspotStopped();
Log.v(TAG, "Turned off hotspot");
}
}
@ -81,4 +81,8 @@ public class WifiHotspotManager {
+ " \n SSID is : "
+ wifiConfiguration.SSID);
}
public void registerListener(@NonNull HotspotStateListener hotspotStateListener) {
this.hotspotStateListener = hotspotStateListener;
}
}