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

More work based on review

* Replace error enum with just strings in state implementation
* throw AssertionError when permission is suddenly gone
* Add TODO note about network name logging
parent 6598c80c
No related branches found
No related tags found
1 merge request!6ViewModel cleanup
......@@ -148,35 +148,9 @@ public class HotspotFragment extends Fragment {
button.setEnabled(true);
hotspotStarted = false;
statusView.setText(
getString(R.string.hotspot_stopped));
statusView.setText(getString(R.string.hotspot_stopped));
if (state.hasError()) {
switch (state.getError()) {
case NO_WIFI_DIRECT:
statusView.setText(getString(R.string.no_wifi_direct));
break;
case P2P_ERROR:
statusView.setText(getString(R.string.callback_failed,
"p2p error"));
break;
case P2P_P2P_UNSUPPORTED:
statusView.setText(getString(R.string.callback_failed,
"p2p unsupported"));
break;
case P2P_NO_SERVICE_REQUESTS:
statusView.setText(
getString(R.string.callback_failed,
"no service requests"));
break;
case PERMISSION_DENIED:
statusView.setText(
getString(R.string.callback_permission_denied));
break;
case NO_GROUP_INFO:
statusView.setText(getString(
R.string.callback_no_group_info));
break;
}
statusView.setText(state.getError());
}
}
......
......@@ -8,7 +8,6 @@ import android.net.wifi.p2p.WifiP2pGroup;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Handler;
import org.briarproject.hotspot.HotspotState.HotspotError;
import org.briarproject.hotspot.HotspotState.HotspotStarted;
import org.briarproject.hotspot.HotspotState.HotspotStopped;
import org.briarproject.hotspot.HotspotState.NetworkConfig;
......@@ -33,13 +32,6 @@ import static android.net.wifi.p2p.WifiP2pManager.P2P_UNSUPPORTED;
import static android.os.Build.VERSION.SDK_INT;
import static java.util.logging.Level.INFO;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.hotspot.HotspotState.HotspotError.NO_GROUP_INFO;
import static org.briarproject.hotspot.HotspotState.HotspotError.NO_WIFI_DIRECT;
import static org.briarproject.hotspot.HotspotState.HotspotError.OTHER;
import static org.briarproject.hotspot.HotspotState.HotspotError.P2P_ERROR;
import static org.briarproject.hotspot.HotspotState.HotspotError.P2P_NO_SERVICE_REQUESTS;
import static org.briarproject.hotspot.HotspotState.HotspotError.P2P_P2P_UNSUPPORTED;
import static org.briarproject.hotspot.HotspotState.HotspotError.PERMISSION_DENIED;
import static org.briarproject.hotspot.StringUtils.getRandomString;
class HotspotManager {
......@@ -50,7 +42,7 @@ class HotspotManager {
static final double UNKNOWN_FREQUENCY = Double.NEGATIVE_INFINITY;
private final Context context;
private final Context ctx;
private final WifiManager wifiManager;
private final WifiP2pManager wifiP2pManager;
private final Handler handler;
......@@ -64,14 +56,14 @@ class HotspotManager {
private WifiManager.WifiLock wifiLock;
private WifiP2pManager.Channel channel;
HotspotManager(Context context) {
this.context = context;
wifiManager = (WifiManager) context.getApplicationContext()
HotspotManager(Context ctx) {
this.ctx = ctx;
wifiManager = (WifiManager) ctx.getApplicationContext()
.getSystemService(WIFI_SERVICE);
wifiP2pManager =
(WifiP2pManager) context.getSystemService(WIFI_P2P_SERVICE);
handler = new Handler(context.getMainLooper());
lockTag = context.getString(R.string.app_name);
(WifiP2pManager) ctx.getSystemService(WIFI_P2P_SERVICE);
handler = new Handler(ctx.getMainLooper());
lockTag = ctx.getString(R.string.app_name);
}
LiveData<HotspotState> getStatus() {
......@@ -80,14 +72,16 @@ class HotspotManager {
void startWifiP2pHotspot() {
if (wifiP2pManager == null) {
status.setValue(new HotspotStopped(NO_WIFI_DIRECT));
status.setValue(
new HotspotStopped(ctx.getString(R.string.no_wifi_direct)));
return;
}
status.setValue(new StartingHotspot());
channel = wifiP2pManager
.initialize(context, context.getMainLooper(), null);
.initialize(ctx, ctx.getMainLooper(), null);
if (channel == null) {
status.setValue(new HotspotStopped(NO_WIFI_DIRECT));
status.setValue(new HotspotStopped(
ctx.getString(R.string.no_wifi_direct)));
return;
}
acquireLock();
......@@ -106,18 +100,27 @@ class HotspotManager {
if (reason == BUSY) requestGroupInfo(1,
networkName); // Hotspot already running
else if (reason == P2P_UNSUPPORTED)
releaseWifiP2pHotspot(P2P_P2P_UNSUPPORTED);
releaseWifiP2pHotspot(
ctx.getString(R.string.callback_failed,
"p2p unsupported"));
else if (reason == ERROR)
releaseWifiP2pHotspot(P2P_ERROR);
releaseWifiP2pHotspot(
ctx.getString(R.string.callback_failed,
"p2p error"));
else if (reason == NO_SERVICE_REQUESTS)
releaseWifiP2pHotspot(P2P_NO_SERVICE_REQUESTS);
else releaseWifiP2pHotspot(P2P_ERROR);
releaseWifiP2pHotspot(ctx.getString(
R.string.callback_failed,
"no service requests"));
else releaseWifiP2pHotspot(
ctx.getString(R.string.callback_failed,
"p2p error"));
// all cases covered, in doubt set to error
}
};
try {
if (SDK_INT >= 29) {
String passphrase = getPassphrase();
// TODO: maybe remove this in the production version
LOG.info("networkName: " + networkName);
WifiP2pConfig config = new WifiP2pConfig.Builder()
.setGroupOperatingBand(GROUP_OWNER_BAND_2GHZ)
......@@ -129,7 +132,7 @@ class HotspotManager {
wifiP2pManager.createGroup(channel, listener);
}
} catch (SecurityException e) {
releaseWifiP2pHotspot(PERMISSION_DENIED);
throw new AssertionError();
}
}
......@@ -155,7 +158,7 @@ class HotspotManager {
@Override
public void onFailure(int reason) {
releaseWifiP2pHotspot(OTHER);
releaseWifiP2pHotspot(null);
}
});
......@@ -174,7 +177,7 @@ class HotspotManager {
wifiLock.release();
}
private void releaseWifiP2pHotspot(@Nullable HotspotError error) {
private void releaseWifiP2pHotspot(@Nullable String error) {
status.setValue(new HotspotStopped(error));
if (SDK_INT >= 27) channel.close();
channel = null;
......@@ -206,7 +209,7 @@ class HotspotManager {
try {
wifiP2pManager.requestGroupInfo(channel, listener);
} catch (SecurityException e) {
releaseWifiP2pHotspot(PERMISSION_DENIED);
throw new AssertionError();
}
}
......@@ -240,7 +243,8 @@ class HotspotManager {
handler.postDelayed(() -> requestGroupInfo(attempt + 1,
requestedNetworkName), 1000);
} else {
releaseWifiP2pHotspot(NO_GROUP_INFO);
releaseWifiP2pHotspot(
ctx.getString(R.string.callback_no_group_info));
}
}
......
......@@ -39,21 +39,11 @@ public abstract class HotspotState {
}
enum HotspotError {
NO_WIFI_DIRECT,
P2P_ERROR,
P2P_P2P_UNSUPPORTED,
P2P_NO_SERVICE_REQUESTS,
PERMISSION_DENIED,
NO_GROUP_INFO,
OTHER
}
static class HotspotStopped extends HotspotState {
private HotspotError error;
private String error;
public HotspotStopped(HotspotError error) {
HotspotStopped(String error) {
this.error = error;
}
......@@ -62,7 +52,7 @@ public abstract class HotspotState {
}
@Nullable
HotspotError getError() {
String getError() {
return error;
}
......
......@@ -28,7 +28,6 @@
<string name="no_wifi_manager">Device does not support Wi-Fi</string>
<string name="no_wifi_direct">Device does not support Wi-Fi Direct</string>
<string name="wifi_5ghz_supported">5Ghz Wi-Fi is supported</string>
<string name="callback_permission_denied">Location permission was denied</string>
<string name="connected">Peer connected</string>
<string name="web_server_error">Error starting web server!</string>
<string name="server_info">Visit this site on the other phone either by scanning the QR code or by typing this link manually.</string>
......
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