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

Review fixes

parent 679f0589
No related branches found
No related tags found
1 merge request!6ViewModel cleanup
......@@ -82,19 +82,16 @@ public class HotspotFragment extends Fragment {
if (state instanceof StartingHotspot) {
statusView.setText(getString(R.string.starting_hotspot));
} else if (state instanceof HotspotStarted) {
hotspotStarted((HotspotStarted) state);
serverButton.setVisibility(VISIBLE);
onHotspotStarted((HotspotStarted) state);
} else if (state instanceof HotspotStopped) {
hotspotStopped();
serverButton.setVisibility(GONE);
onHotspotStopped();
} else if (state instanceof HotspotError) {
hotspotError((HotspotError) state);
serverButton.setVisibility(GONE);
onHotspotError((HotspotError) state);
}
});
}
private void hotspotStarted(HotspotStarted state) {
private void onHotspotStarted(HotspotStarted state) {
button.setText(R.string.stop_hotspot);
button.setEnabled(true);
hotspotStarted = true;
......@@ -122,9 +119,11 @@ public class HotspotFragment extends Fragment {
}
ssidView.setText(getString(R.string.ssid, config.ssid));
passwordView.setText(getString(R.string.password, config.password));
serverButton.setVisibility(VISIBLE);
}
private void hotspotStopped() {
private void onHotspotStopped() {
qrCode.setVisibility(GONE);
ssidView.setText("");
passwordView.setText("");
......@@ -134,10 +133,12 @@ public class HotspotFragment extends Fragment {
hotspotStarted = false;
statusView.setText(getString(R.string.hotspot_stopped));
serverButton.setVisibility(GONE);
}
private void hotspotError(HotspotError state) {
hotspotStopped();
private void onHotspotError(HotspotError state) {
onHotspotStopped();
statusView.setText(state.getError());
}
......
......@@ -14,6 +14,7 @@ import org.briarproject.hotspot.HotspotState.NetworkConfig;
import java.util.logging.Logger;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import static android.content.Context.WIFI_P2P_SERVICE;
import static android.content.Context.WIFI_SERVICE;
......@@ -107,9 +108,7 @@ class HotspotManager implements ActionListener {
}
}
/**
* Only used on API level 29+
*/
@RequiresApi(29)
private String getNetworkName() {
return "DIRECT-" + getRandomString(2) + "-" +
getRandomString(10);
......
......@@ -6,12 +6,14 @@ import android.net.wifi.WifiManager;
import org.briarproject.hotspot.HotspotState.HotspotError;
import org.briarproject.hotspot.HotspotState.HotspotStarted;
import org.briarproject.hotspot.HotspotState.HotspotStopped;
import org.briarproject.hotspot.HotspotState.NetworkConfig;
import org.briarproject.hotspot.HotspotState.StartingHotspot;
import org.briarproject.hotspot.WebServerManager.WebServerState;
import java.util.logging.Logger;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
......@@ -35,7 +37,6 @@ public class MainViewModel extends AndroidViewModel
private final MutableLiveData<HotspotState> status =
new MutableLiveData<>();
private HotspotState.NetworkConfig networkConfig;
public MainViewModel(@NonNull Application app) {
super(app);
......@@ -79,8 +80,13 @@ public class MainViewModel extends AndroidViewModel
status.setValue(new StartingHotspot());
}
@Nullable
// Field to temporarily store the network config received via onHotspotStarted()
// in order to post it along with a HotspotStarted status
private NetworkConfig networkConfig;
@Override
public void onHotspotStarted(HotspotState.NetworkConfig networkConfig) {
public void onHotspotStarted(NetworkConfig networkConfig) {
this.networkConfig = networkConfig;
LOG.info("starting webserver");
webServerManager.startWebServer();
......@@ -99,10 +105,11 @@ public class MainViewModel extends AndroidViewModel
}
@Override
public void onStateChanged(WebServerState webServerStatus) {
public void onWebServerStateChanged(WebServerState webServerStatus) {
switch (webServerStatus) {
case STARTED:
status.postValue(new HotspotStarted(networkConfig));
networkConfig = null;
break;
case STOPPED:
// nothing to do in this case
......
......@@ -18,7 +18,7 @@ class WebServerManager {
interface WebServerListener {
void onStateChanged(WebServerState status);
void onWebServerStateChanged(WebServerState status);
}
......@@ -40,15 +40,15 @@ class WebServerManager {
webServer.start();
} catch (IOException e) {
logException(LOG, WARNING, e);
listener.onStateChanged(ERROR);
listener.onWebServerStateChanged(ERROR);
}
listener.onStateChanged(STARTED);
listener.onWebServerStateChanged(STARTED);
}).start();
}
void stopWebServer() {
webServer.stop();
listener.onStateChanged(STOPPED);
listener.onWebServerStateChanged(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