Skip to content
Snippets Groups Projects
Commit 8952ec8a authored by Sebastian Kürten's avatar Sebastian Kürten
Browse files

Create WebServerManager for managing the web server

parent b56ed85e
No related branches found
No related tags found
1 merge request!6ViewModel cleanup
......@@ -10,7 +10,7 @@ import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import org.briarproject.hotspot.MainViewModel.WebServerState;
import org.briarproject.hotspot.WebServerManager.WebServerState;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission;
......
......@@ -37,7 +37,7 @@ import static org.briarproject.hotspot.HotspotManager.HotspotState.STARTING_HOTS
import static org.briarproject.hotspot.HotspotManager.HotspotState.WAITING_TO_START_HOTSPOT;
import static org.briarproject.hotspot.StringUtils.getRandomString;
public class HotspotManager {
class HotspotManager {
private static final Logger LOG = getLogger(HotspotManager.class.getName());
......@@ -79,7 +79,7 @@ public class HotspotManager {
private WifiManager.WifiLock wifiLock;
private WifiP2pManager.Channel channel;
public HotspotManager(Context context) {
HotspotManager(Context context) {
this.context = context;
wifiManager = (WifiManager) context.getApplicationContext()
.getSystemService(WIFI_SERVICE);
......
......@@ -4,8 +4,8 @@ import android.app.Application;
import android.net.wifi.WifiManager;
import org.briarproject.hotspot.HotspotManager.HotspotState;
import org.briarproject.hotspot.WebServerManager.WebServerState;
import java.io.IOException;
import java.util.logging.Logger;
import androidx.annotation.NonNull;
......@@ -25,16 +25,14 @@ public class MainViewModel extends AndroidViewModel
private final MutableLiveData<Boolean> is5GhzSupported =
new MutableLiveData<>(false);
private final MutableLiveData<WebServerState> webServerState =
new MutableLiveData<>(WebServerState.STOPPED);
private final HotspotManager hotSpotManager;
private final WebServer webServer;
private final WebServerManager webServerManager;
public MainViewModel(@NonNull Application app) {
super(app);
hotSpotManager = new HotspotManager(app);
webServer = new WebServer(app);
webServerManager = new WebServerManager(app);
if (SDK_INT >= 21) {
WifiManager wifiManager =
......@@ -52,7 +50,7 @@ public class MainViewModel extends AndroidViewModel
}
LiveData<WebServerState> getWebServerState() {
return webServerState;
return webServerManager.getWebServerState();
}
HotspotManager getHotSpotManager() {
......@@ -69,35 +67,18 @@ public class MainViewModel extends AndroidViewModel
hotSpotManager.stopWifiP2pHotspot();
}
private void startWebServer() {
try {
webServer.start();
webServerState.postValue(WebServerState.STARTED);
} catch (IOException e) {
e.printStackTrace();
webServerState.postValue(WebServerState.ERROR);
}
}
private void stopWebServer() {
webServer.stop();
webServerState.postValue(WebServerState.STOPPED);
}
@Override
public void onChanged(HotspotState state) {
switch (state) {
case HOTSPOT_STARTED:
LOG.info("starting webserver");
startWebServer();
webServerManager.startWebServer();
break;
case HOTSPOT_STOPPED:
LOG.info("stopping webserver");
stopWebServer();
webServerManager.stopWebServer();
break;
}
}
enum WebServerState {STOPPED, STARTED, ERROR}
}
package org.briarproject.hotspot;
import android.app.Application;
import android.content.Context;
import java.io.File;
import java.io.FileInputStream;
......@@ -14,9 +14,9 @@ import static fi.iki.elonen.NanoHTTPD.Response.Status.OK;
public class WebServer extends NanoHTTPD {
private final Application ctx;
private final Context ctx;
public WebServer(Application ctx) {
public WebServer(Context ctx) {
super(9999);
this.ctx = ctx;
}
......
package org.briarproject.hotspot;
import android.content.Context;
import java.io.IOException;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import static org.briarproject.hotspot.WebServerManager.WebServerState.ERROR;
import static org.briarproject.hotspot.WebServerManager.WebServerState.STARTED;
import static org.briarproject.hotspot.WebServerManager.WebServerState.STOPPED;
class WebServerManager {
enum WebServerState {STOPPED, STARTED, ERROR}
private final WebServer webServer;
private final MutableLiveData<WebServerState> webServerState =
new MutableLiveData<>(STOPPED);
WebServerManager(Context context) {
webServer = new WebServer(context);
}
LiveData<WebServerState> getWebServerState() {
return webServerState;
}
void startWebServer() {
try {
webServer.start();
webServerState.postValue(STARTED);
} catch (IOException e) {
e.printStackTrace();
webServerState.postValue(ERROR);
}
}
void stopWebServer() {
webServer.stop();
webServerState.postValue(STOPPED);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment