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

Introduce common base class for both ConditionManager implementations

parent 9fcb673f
No related branches found
No related tags found
1 merge request!15Try starting the WifiP2p framework multiple times with delay if BUSY
package org.briarproject.hotspot;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import java.util.logging.Logger;
import androidx.annotation.UiThread;
import androidx.fragment.app.FragmentActivity;
import static android.content.Context.WIFI_SERVICE;
import static android.net.wifi.p2p.WifiP2pManager.EXTRA_WIFI_STATE;
import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION;
import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_DISABLED;
import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_ENABLED;
import static java.util.logging.Level.INFO;
import static java.util.logging.Logger.getLogger;
abstract class AbstractConditionManager implements ConditionManager {
private static final Logger LOG =
getLogger(AbstractConditionManager.class.getName());
protected Runnable permissionUpdateCallback;
protected FragmentActivity ctx;
protected WifiManager wifiManager;
protected boolean wifiP2pEnabled = false;
public AbstractConditionManager(Runnable permissionUpdateCallback) {
this.permissionUpdateCallback = permissionUpdateCallback;
}
@Override
public void init(FragmentActivity ctx) {
this.ctx = ctx;
this.wifiManager = (WifiManager) ctx.getApplicationContext()
.getSystemService(WIFI_SERVICE);
}
/**
* When Wifi is off and gets enabled using {@link WifiManager#setWifiEnabled},
* it takes a while until Wifi P2P is also available and it is safe to call
* {@link android.net.wifi.p2p.WifiP2pManager#createGroup}. On API levels 29
* and above, there's {@link android.net.wifi.p2p.WifiP2pManager#requestP2pState},
* but on pre 29 the only thing we can do is register a broadcast on
* WIFI_P2P_STATE_CHANGED_ACTION and wait until Wifi P2P is available.
* Issue #2088 uncovered that this is necessary.
*/
final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
@UiThread
public void onReceive(Context context, Intent intent) {
if (!WIFI_P2P_STATE_CHANGED_ACTION.equals(intent.getAction())) {
return;
}
int state = intent.getIntExtra(EXTRA_WIFI_STATE,
WIFI_P2P_STATE_DISABLED);
wifiP2pEnabled = state == WIFI_P2P_STATE_ENABLED;
if (LOG.isLoggable(INFO))
LOG.info("WifiP2pState: " + wifiP2pEnabled);
permissionUpdateCallback.run();
}
};
@Override
public void onStart() {
ctx.registerReceiver(receiver, new IntentFilter(
WIFI_P2P_STATE_CHANGED_ACTION));
}
@Override
public void onStop() {
ctx.unregisterReceiver(receiver);
}
}
package org.briarproject.hotspot;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import java.util.logging.Logger;
......@@ -14,15 +10,8 @@ import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission;
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
import androidx.annotation.RequiresApi;
import androidx.annotation.UiThread;
import androidx.fragment.app.FragmentActivity;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.content.Context.WIFI_SERVICE;
import static android.net.wifi.p2p.WifiP2pManager.EXTRA_WIFI_STATE;
import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION;
import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_DISABLED;
import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_ENABLED;
import static androidx.core.app.ActivityCompat.shouldShowRequestPermissionRationale;
import static java.util.logging.Level.INFO;
import static java.util.logging.Logger.getLogger;
......@@ -38,24 +27,21 @@ import static org.briarproject.hotspot.UiUtils.showRationale;
* all conditions are fulfilled.
*/
@RequiresApi(29)
public class ConditionManager29Impl implements ConditionManager {
public class ConditionManager29Impl extends AbstractConditionManager
implements ConditionManager {
private static final Logger LOG =
getLogger(ConditionManager29Impl.class.getName());
private Permission locationPermission = Permission.UNKNOWN;
private FragmentActivity ctx;
private WifiManager wifiManager;
private final ActivityResultLauncher<String> locationRequest;
private final ActivityResultLauncher<Intent> wifiRequest;
private boolean wifiP2pEnabled = false;
private boolean wifiRequestInProgress = false;
private final Runnable permissionUpdateCallback;
ConditionManager29Impl(ActivityResultCaller arc,
Runnable permissionUpdateCallback) {
this.permissionUpdateCallback = permissionUpdateCallback;
super(permissionUpdateCallback);
locationRequest = arc.registerForActivityResult(
new RequestPermission(), granted -> {
onRequestPermissionResult(granted);
......@@ -69,50 +55,10 @@ public class ConditionManager29Impl implements ConditionManager {
});
}
/**
* When Wifi is off and gets enabled using {@link WifiManager#setWifiEnabled},
* it takes a while until Wifi P2P is also available and it is safe to call
* {@link android.net.wifi.p2p.WifiP2pManager#createGroup}. On API levels 29
* and above, there's {@link android.net.wifi.p2p.WifiP2pManager#requestP2pState},
* but on pre 29 the only thing we can do is register a broadcast on
* WIFI_P2P_STATE_CHANGED_ACTION and wait until Wifi P2P is available.
* Issue #2088 uncovered that this is necessary.
*/
final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
@UiThread
public void onReceive(Context context, Intent intent) {
if (!WIFI_P2P_STATE_CHANGED_ACTION.equals(intent.getAction())) {
return;
}
int state = intent.getIntExtra(EXTRA_WIFI_STATE,
WIFI_P2P_STATE_DISABLED);
wifiP2pEnabled = state == WIFI_P2P_STATE_ENABLED;
if (LOG.isLoggable(INFO))
LOG.info("WifiP2pState: " + wifiP2pEnabled);
permissionUpdateCallback.run();
}
};
@Override
public void init(FragmentActivity ctx) {
this.ctx = ctx;
this.wifiManager = (WifiManager) ctx.getApplicationContext()
.getSystemService(WIFI_SERVICE);
}
@Override
public void onStart() {
super.onStart();
locationPermission = Permission.UNKNOWN;
ctx.registerReceiver(receiver, new IntentFilter(
WIFI_P2P_STATE_CHANGED_ACTION));
}
@Override
public void onStop() {
ctx.unregisterReceiver(receiver);
}
private boolean areEssentialPermissionsGranted() {
......
package org.briarproject.hotspot;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import java.util.logging.Logger;
......@@ -12,14 +8,8 @@ import java.util.logging.Logger;
import androidx.activity.result.ActivityResultCaller;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
import androidx.annotation.UiThread;
import androidx.fragment.app.FragmentActivity;
import static android.content.Context.WIFI_SERVICE;
import static android.net.wifi.p2p.WifiP2pManager.EXTRA_WIFI_STATE;
import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION;
import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_DISABLED;
import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_ENABLED;
import static java.util.logging.Level.INFO;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.hotspot.UiUtils.showRationale;
......@@ -31,68 +21,25 @@ import static org.briarproject.hotspot.UiUtils.showRationale;
* As soon as {@link #checkAndRequestConditions()} returns true,
* all conditions are fulfilled.
*/
public class ConditionManagerImpl implements ConditionManager {
public class ConditionManagerImpl extends AbstractConditionManager
implements ConditionManager {
private static final Logger LOG =
getLogger(ConditionManagerImpl.class.getName());
private FragmentActivity ctx;
private WifiManager wifiManager;
private final ActivityResultLauncher<Intent> wifiRequest;
private boolean wifiP2pEnabled = false;
private final Runnable permissionUpdateCallback;
ConditionManagerImpl(ActivityResultCaller arc,
Runnable permissionUpdateCallback) {
this.permissionUpdateCallback = permissionUpdateCallback;
super(permissionUpdateCallback);
wifiRequest = arc.registerForActivityResult(
new StartActivityForResult(),
result -> permissionUpdateCallback.run());
}
/**
* When Wifi is off and gets enabled using {@link WifiManager#setWifiEnabled},
* it takes a while until Wifi P2P is also available and it is safe to call
* {@link android.net.wifi.p2p.WifiP2pManager#createGroup}. On API levels 29
* and above, there's {@link android.net.wifi.p2p.WifiP2pManager#requestP2pState},
* but on pre 29 the only thing we can do is register a broadcast on
* WIFI_P2P_STATE_CHANGED_ACTION and wait until Wifi P2P is available.
* Issue #2088 uncovered that this is necessary.
*/
final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
@UiThread
public void onReceive(Context context, Intent intent) {
if (!WIFI_P2P_STATE_CHANGED_ACTION.equals(intent.getAction())) {
return;
}
int state = intent.getIntExtra(EXTRA_WIFI_STATE,
WIFI_P2P_STATE_DISABLED);
wifiP2pEnabled = state == WIFI_P2P_STATE_ENABLED;
if (LOG.isLoggable(INFO))
LOG.info("WifiP2pState: " + wifiP2pEnabled);
permissionUpdateCallback.run();
}
};
@Override
public void init(FragmentActivity ctx) {
this.ctx = ctx;
this.wifiManager = (WifiManager) ctx.getApplicationContext()
.getSystemService(WIFI_SERVICE);
}
@Override
public void onStart() {
ctx.registerReceiver(receiver, new IntentFilter(
WIFI_P2P_STATE_CHANGED_ACTION));
}
@Override
public void onStop() {
ctx.unregisterReceiver(receiver);
super.init(ctx);
}
private boolean areEssentialPermissionsGranted() {
......
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