mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-22 20:24:03 -04:00
Add HotspotStateListener
This commit is contained in:
parent
316b9d9550
commit
aee5c81baa
@ -9,6 +9,7 @@ import android.content.BroadcastReceiver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.net.wifi.WifiConfiguration;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.IBinder;
|
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.
|
* 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_ON_AFTER_O = "Turn_on_hotspot_after_oreo";
|
||||||
public static final String ACTION_TURN_OFF_AFTER_O = "Turn_off_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();
|
super.onCreate();
|
||||||
|
|
||||||
hotspotManager = new WifiHotspotManager(this);
|
hotspotManager = new WifiHotspotManager(this);
|
||||||
|
hotspotManager.registerListener(HotspotService.this);
|
||||||
|
|
||||||
stopReceiver = new BroadcastReceiver() {
|
stopReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
@ -83,7 +85,7 @@ public class HotspotService extends Service {
|
|||||||
|
|
||||||
case ACTION_TURN_ON_AFTER_O:
|
case ACTION_TURN_ON_AFTER_O:
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
hotspotManager.turnOnHotspot(zimHostCallbacks);
|
hotspotManager.turnOnHotspot();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -154,13 +156,14 @@ public class HotspotService extends Service {
|
|||||||
void stopHotspotAndDismissNotification() {
|
void stopHotspotAndDismissNotification() {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
hotspotManager.turnOffHotspot();
|
hotspotManager.turnOffHotspot();
|
||||||
}
|
} else {
|
||||||
webServerHelper.stopAndroidWebServer();
|
webServerHelper.stopAndroidWebServer();
|
||||||
zimHostCallbacks.onServerStopped();
|
zimHostCallbacks.onServerStopped();
|
||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
stopSelf();
|
stopSelf();
|
||||||
notificationManager.cancel(HOTSPOT_NOTIFICATION_ID);
|
notificationManager.cancel(HOTSPOT_NOTIFICATION_ID);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
@ -191,6 +194,23 @@ public class HotspotService extends Service {
|
|||||||
buildForegroundNotification(getString(R.string.hotspot_running)));
|
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 {
|
public class HotspotBinder extends Binder {
|
||||||
|
|
||||||
@NonNull public HotspotService getService() {
|
@NonNull public HotspotService getService() {
|
||||||
|
@ -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();
|
||||||
|
}
|
@ -8,7 +8,6 @@ import android.os.Handler;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.RequiresApi;
|
import androidx.annotation.RequiresApi;
|
||||||
import org.kiwix.kiwixmobile.webserver.ZimHostCallbacks;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* WifiHotstopManager class makes use of the Android's WifiManager and WifiConfiguration class
|
* 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 {
|
public class WifiHotspotManager {
|
||||||
private final WifiManager wifiManager;
|
private final WifiManager wifiManager;
|
||||||
WifiManager.LocalOnlyHotspotReservation hotspotReservation;
|
private WifiManager.LocalOnlyHotspotReservation hotspotReservation;
|
||||||
|
private HotspotStateListener hotspotStateListener;
|
||||||
private static final String TAG = "WifiHotspotManager";
|
private static final String TAG = "WifiHotspotManager";
|
||||||
|
|
||||||
public WifiHotspotManager(@NonNull Context context) {
|
public WifiHotspotManager(@NonNull Context context) {
|
||||||
@ -28,7 +28,7 @@ public class WifiHotspotManager {
|
|||||||
|
|
||||||
//Workaround to turn on hotspot for Oreo versions
|
//Workaround to turn on hotspot for Oreo versions
|
||||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||||
public void turnOnHotspot(@NonNull ZimHostCallbacks zimHostCallbacks) {
|
public void turnOnHotspot() {
|
||||||
wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
|
wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -38,22 +38,21 @@ public class WifiHotspotManager {
|
|||||||
WifiConfiguration currentConfig = hotspotReservation.getWifiConfiguration();
|
WifiConfiguration currentConfig = hotspotReservation.getWifiConfiguration();
|
||||||
|
|
||||||
printCurrentConfig(currentConfig);
|
printCurrentConfig(currentConfig);
|
||||||
|
hotspotStateListener.onHotspotTurnedOn(currentConfig);
|
||||||
zimHostCallbacks.onHotspotTurnedOn(currentConfig);
|
|
||||||
Log.v(TAG, "Local Hotspot Started");
|
Log.v(TAG, "Local Hotspot Started");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStopped() {
|
public void onStopped() {
|
||||||
super.onStopped();
|
super.onStopped();
|
||||||
zimHostCallbacks.onServerStopped();
|
hotspotStateListener.onHotspotStopped();
|
||||||
Log.v(TAG, "Local Hotspot Stopped");
|
Log.v(TAG, "Local Hotspot Stopped");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailed(int reason) {
|
public void onFailed(int reason) {
|
||||||
super.onFailed(reason);
|
super.onFailed(reason);
|
||||||
zimHostCallbacks.onHotspotFailedToStart();
|
hotspotStateListener.onHotspotFailedToStart();
|
||||||
Log.v(TAG, "Local Hotspot failed to start");
|
Log.v(TAG, "Local Hotspot failed to start");
|
||||||
}
|
}
|
||||||
}, new Handler());
|
}, new Handler());
|
||||||
@ -65,6 +64,7 @@ public class WifiHotspotManager {
|
|||||||
if (hotspotReservation != null) {
|
if (hotspotReservation != null) {
|
||||||
hotspotReservation.close();
|
hotspotReservation.close();
|
||||||
hotspotReservation = null;
|
hotspotReservation = null;
|
||||||
|
hotspotStateListener.onHotspotStopped();
|
||||||
Log.v(TAG, "Turned off hotspot");
|
Log.v(TAG, "Turned off hotspot");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -81,4 +81,8 @@ public class WifiHotspotManager {
|
|||||||
+ " \n SSID is : "
|
+ " \n SSID is : "
|
||||||
+ wifiConfiguration.SSID);
|
+ wifiConfiguration.SSID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void registerListener(@NonNull HotspotStateListener hotspotStateListener) {
|
||||||
|
this.hotspotStateListener = hotspotStateListener;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user