mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 14:19:53 +01:00
Rename ConditionManager classes
This commit is contained in:
@@ -0,0 +1,84 @@
|
|||||||
|
package org.briarproject.briar.android.hotspot;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.net.wifi.WifiManager;
|
||||||
|
|
||||||
|
import org.briarproject.briar.R;
|
||||||
|
|
||||||
|
import androidx.annotation.StringRes;
|
||||||
|
import androidx.appcompat.app.AlertDialog;
|
||||||
|
import androidx.core.util.Consumer;
|
||||||
|
import androidx.fragment.app.FragmentActivity;
|
||||||
|
|
||||||
|
import static android.content.Context.WIFI_SERVICE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract base class for the ConditionManagers that ensure that the conditions
|
||||||
|
* to open a hotspot are fulfilled. There are different extensions of this for
|
||||||
|
* API levels lower than 29 and 29+.
|
||||||
|
*/
|
||||||
|
abstract class AbstractConditionManager {
|
||||||
|
|
||||||
|
enum Permission {
|
||||||
|
UNKNOWN, GRANTED, SHOW_RATIONALE, PERMANENTLY_DENIED
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final Consumer<Boolean> permissionUpdateCallback;
|
||||||
|
protected FragmentActivity ctx;
|
||||||
|
WifiManager wifiManager;
|
||||||
|
|
||||||
|
AbstractConditionManager(Consumer<Boolean> permissionUpdateCallback) {
|
||||||
|
this.permissionUpdateCallback = permissionUpdateCallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pass a FragmentActivity context here during `onCreateView()`.
|
||||||
|
*/
|
||||||
|
void init(FragmentActivity ctx) {
|
||||||
|
this.ctx = ctx;
|
||||||
|
this.wifiManager = (WifiManager) ctx.getApplicationContext()
|
||||||
|
.getSystemService(WIFI_SERVICE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call this during onStart() in the fragment where the ConditionManager
|
||||||
|
* is used.
|
||||||
|
*/
|
||||||
|
abstract void onStart();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if all required conditions are met such that the hotspot can be
|
||||||
|
* started. If any precondition is not met yet, bring up relevant dialogs
|
||||||
|
* asking the user to grant relevant permissions or take relevant actions.
|
||||||
|
*
|
||||||
|
* @return true if conditions are fulfilled and flow can continue.
|
||||||
|
*/
|
||||||
|
abstract boolean checkAndRequestConditions();
|
||||||
|
|
||||||
|
void showDenialDialog(FragmentActivity ctx,
|
||||||
|
@StringRes int title, @StringRes int body,
|
||||||
|
DialogInterface.OnClickListener onOkClicked, Runnable onDismiss) {
|
||||||
|
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
|
||||||
|
builder.setTitle(title);
|
||||||
|
builder.setMessage(body);
|
||||||
|
builder.setPositiveButton(R.string.ok, onOkClicked);
|
||||||
|
builder.setNegativeButton(R.string.cancel,
|
||||||
|
(dialog, which) -> ctx.supportFinishAfterTransition());
|
||||||
|
builder.setOnDismissListener(dialog -> onDismiss.run());
|
||||||
|
builder.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
void showRationale(Context ctx, @StringRes int title,
|
||||||
|
@StringRes int body, Runnable onContinueClicked,
|
||||||
|
Runnable onDismiss) {
|
||||||
|
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
|
||||||
|
builder.setTitle(title);
|
||||||
|
builder.setMessage(body);
|
||||||
|
builder.setNeutralButton(R.string.continue_button,
|
||||||
|
(dialog, which) -> onContinueClicked.run());
|
||||||
|
builder.setOnDismissListener(dialog -> onDismiss.run());
|
||||||
|
builder.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,84 +1,83 @@
|
|||||||
package org.briarproject.briar.android.hotspot;
|
package org.briarproject.briar.android.hotspot;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Intent;
|
||||||
import android.content.DialogInterface;
|
import android.provider.Settings;
|
||||||
import android.net.wifi.WifiManager;
|
|
||||||
|
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
|
|
||||||
import androidx.annotation.StringRes;
|
import java.util.logging.Logger;
|
||||||
import androidx.appcompat.app.AlertDialog;
|
|
||||||
import androidx.core.util.Consumer;
|
|
||||||
import androidx.fragment.app.FragmentActivity;
|
|
||||||
|
|
||||||
import static android.content.Context.WIFI_SERVICE;
|
import androidx.activity.result.ActivityResultCaller;
|
||||||
|
import androidx.activity.result.ActivityResultLauncher;
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
|
||||||
|
import androidx.core.util.Consumer;
|
||||||
|
|
||||||
|
import static java.util.logging.Level.INFO;
|
||||||
|
import static java.util.logging.Logger.getLogger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract base class for the ConditionManagers that ensure that the conditions
|
* This class ensures that the conditions to open a hotspot are fulfilled on
|
||||||
* to open a hotspot are fulfilled. There are different extensions of this for
|
* API levels < 29.
|
||||||
* API levels lower than 29 and 29+.
|
* <p>
|
||||||
|
* As soon as {@link #checkAndRequestConditions()} returns true,
|
||||||
|
* all conditions are fulfilled.
|
||||||
*/
|
*/
|
||||||
abstract class ConditionManager {
|
class ConditionManager extends AbstractConditionManager {
|
||||||
|
|
||||||
enum Permission {
|
private static final Logger LOG =
|
||||||
UNKNOWN, GRANTED, SHOW_RATIONALE, PERMANENTLY_DENIED
|
getLogger(ConditionManager.class.getName());
|
||||||
|
|
||||||
|
private final ActivityResultLauncher<Intent> wifiRequest;
|
||||||
|
|
||||||
|
ConditionManager(ActivityResultCaller arc,
|
||||||
|
Consumer<Boolean> permissionUpdateCallback) {
|
||||||
|
super(permissionUpdateCallback);
|
||||||
|
wifiRequest = arc.registerForActivityResult(
|
||||||
|
new StartActivityForResult(),
|
||||||
|
result -> permissionUpdateCallback
|
||||||
|
.accept(wifiManager.isWifiEnabled()));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final Consumer<Boolean> permissionUpdateCallback;
|
@Override
|
||||||
protected FragmentActivity ctx;
|
void onStart() {
|
||||||
WifiManager wifiManager;
|
// nothing to do here
|
||||||
|
|
||||||
ConditionManager(Consumer<Boolean> permissionUpdateCallback) {
|
|
||||||
this.permissionUpdateCallback = permissionUpdateCallback;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private boolean areEssentialPermissionsGranted() {
|
||||||
* Pass a FragmentActivity context here during `onCreateView()`.
|
if (LOG.isLoggable(INFO)) {
|
||||||
*/
|
LOG.info(String.format("areEssentialPermissionsGranted(): " +
|
||||||
void init(FragmentActivity ctx) {
|
"wifiManager.isWifiEnabled()? %b",
|
||||||
this.ctx = ctx;
|
wifiManager.isWifiEnabled()));
|
||||||
this.wifiManager = (WifiManager) ctx.getApplicationContext()
|
}
|
||||||
.getSystemService(WIFI_SERVICE);
|
return wifiManager.isWifiEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Call this during onStart() in the fragment where the ConditionManager
|
boolean checkAndRequestConditions() {
|
||||||
* is used.
|
if (areEssentialPermissionsGranted()) return true;
|
||||||
*/
|
|
||||||
abstract void onStart();
|
|
||||||
|
|
||||||
/**
|
if (!wifiManager.isWifiEnabled()) {
|
||||||
* Check if all required conditions are met such that the hotspot can be
|
// Try enabling the Wifi and return true if that seems to have been
|
||||||
* started. If any precondition is not met yet, bring up relevant dialogs
|
// successful, i.e. "Wifi is either already in the requested state, or
|
||||||
* asking the user to grant relevant permissions or take relevant actions.
|
// in progress toward the requested state".
|
||||||
*
|
if (wifiManager.setWifiEnabled(true)) {
|
||||||
* @return true if conditions are fulfilled and flow can continue.
|
LOG.info("Enabled wifi");
|
||||||
*/
|
return true;
|
||||||
abstract boolean checkAndRequestConditions();
|
}
|
||||||
|
|
||||||
void showDenialDialog(FragmentActivity ctx,
|
// Wifi is not enabled and we can't seem to enable it, so ask the user
|
||||||
@StringRes int title, @StringRes int body,
|
// to enable it for us.
|
||||||
DialogInterface.OnClickListener onOkClicked, Runnable onDismiss) {
|
showRationale(ctx, R.string.wifi_settings_title,
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
|
R.string.wifi_settings_request_enable_body,
|
||||||
builder.setTitle(title);
|
this::requestEnableWiFi,
|
||||||
builder.setMessage(body);
|
() -> permissionUpdateCallback.accept(false));
|
||||||
builder.setPositiveButton(R.string.ok, onOkClicked);
|
}
|
||||||
builder.setNegativeButton(R.string.cancel,
|
|
||||||
(dialog, which) -> ctx.supportFinishAfterTransition());
|
return false;
|
||||||
builder.setOnDismissListener(dialog -> onDismiss.run());
|
|
||||||
builder.show();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void showRationale(Context ctx, @StringRes int title,
|
private void requestEnableWiFi() {
|
||||||
@StringRes int body, Runnable onContinueClicked,
|
wifiRequest.launch(new Intent(Settings.ACTION_WIFI_SETTINGS));
|
||||||
Runnable onDismiss) {
|
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
|
|
||||||
builder.setTitle(title);
|
|
||||||
builder.setMessage(body);
|
|
||||||
builder.setNeutralButton(R.string.continue_button,
|
|
||||||
(dialog, which) -> onContinueClicked.run());
|
|
||||||
builder.setOnDismissListener(dialog -> onDismiss.run());
|
|
||||||
builder.show();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,17 +30,17 @@ import static org.briarproject.briar.android.util.UiUtils.getGoToSettingsListene
|
|||||||
* all conditions are fulfilled.
|
* all conditions are fulfilled.
|
||||||
*/
|
*/
|
||||||
@RequiresApi(29)
|
@RequiresApi(29)
|
||||||
class ConditionManager29Impl extends ConditionManager {
|
class ConditionManager29 extends AbstractConditionManager {
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(ConditionManager29Impl.class.getName());
|
getLogger(ConditionManager29.class.getName());
|
||||||
|
|
||||||
private Permission locationPermission = Permission.UNKNOWN;
|
private Permission locationPermission = Permission.UNKNOWN;
|
||||||
|
|
||||||
private final ActivityResultLauncher<String> locationRequest;
|
private final ActivityResultLauncher<String> locationRequest;
|
||||||
private final ActivityResultLauncher<Intent> wifiRequest;
|
private final ActivityResultLauncher<Intent> wifiRequest;
|
||||||
|
|
||||||
ConditionManager29Impl(ActivityResultCaller arc,
|
ConditionManager29(ActivityResultCaller arc,
|
||||||
Consumer<Boolean> permissionUpdateCallback) {
|
Consumer<Boolean> permissionUpdateCallback) {
|
||||||
super(permissionUpdateCallback);
|
super(permissionUpdateCallback);
|
||||||
locationRequest = arc.registerForActivityResult(
|
locationRequest = arc.registerForActivityResult(
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
package org.briarproject.briar.android.hotspot;
|
|
||||||
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.provider.Settings;
|
|
||||||
|
|
||||||
import org.briarproject.briar.R;
|
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import androidx.activity.result.ActivityResultCaller;
|
|
||||||
import androidx.activity.result.ActivityResultLauncher;
|
|
||||||
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
|
|
||||||
import androidx.core.util.Consumer;
|
|
||||||
|
|
||||||
import static java.util.logging.Level.INFO;
|
|
||||||
import static java.util.logging.Logger.getLogger;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class ensures that the conditions to open a hotspot are fulfilled on
|
|
||||||
* API levels < 29.
|
|
||||||
* <p>
|
|
||||||
* As soon as {@link #checkAndRequestConditions()} returns true,
|
|
||||||
* all conditions are fulfilled.
|
|
||||||
*/
|
|
||||||
class ConditionManagerImpl extends ConditionManager {
|
|
||||||
|
|
||||||
private static final Logger LOG =
|
|
||||||
getLogger(ConditionManagerImpl.class.getName());
|
|
||||||
|
|
||||||
private final ActivityResultLauncher<Intent> wifiRequest;
|
|
||||||
|
|
||||||
ConditionManagerImpl(ActivityResultCaller arc,
|
|
||||||
Consumer<Boolean> permissionUpdateCallback) {
|
|
||||||
super(permissionUpdateCallback);
|
|
||||||
wifiRequest = arc.registerForActivityResult(
|
|
||||||
new StartActivityForResult(),
|
|
||||||
result -> permissionUpdateCallback
|
|
||||||
.accept(wifiManager.isWifiEnabled()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
void onStart() {
|
|
||||||
// nothing to do here
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean areEssentialPermissionsGranted() {
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info(String.format("areEssentialPermissionsGranted(): " +
|
|
||||||
"wifiManager.isWifiEnabled()? %b",
|
|
||||||
wifiManager.isWifiEnabled()));
|
|
||||||
}
|
|
||||||
return wifiManager.isWifiEnabled();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
boolean checkAndRequestConditions() {
|
|
||||||
if (areEssentialPermissionsGranted()) return true;
|
|
||||||
|
|
||||||
if (!wifiManager.isWifiEnabled()) {
|
|
||||||
// Try enabling the Wifi and return true if that seems to have been
|
|
||||||
// successful, i.e. "Wifi is either already in the requested state, or
|
|
||||||
// in progress toward the requested state".
|
|
||||||
if (wifiManager.setWifiEnabled(true)) {
|
|
||||||
LOG.info("Enabled wifi");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wifi is not enabled and we can't seem to enable it, so ask the user
|
|
||||||
// to enable it for us.
|
|
||||||
showRationale(ctx, R.string.wifi_settings_title,
|
|
||||||
R.string.wifi_settings_request_enable_body,
|
|
||||||
this::requestEnableWiFi,
|
|
||||||
() -> permissionUpdateCallback.accept(false));
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void requestEnableWiFi() {
|
|
||||||
wifiRequest.launch(new Intent(Settings.ACTION_WIFI_SETTINGS));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -47,9 +47,9 @@ public class HotspotIntroFragment extends Fragment {
|
|||||||
private ProgressBar progressBar;
|
private ProgressBar progressBar;
|
||||||
private TextView progressTextView;
|
private TextView progressTextView;
|
||||||
|
|
||||||
private final ConditionManager conditionManager = SDK_INT < 29 ?
|
private final AbstractConditionManager conditionManager = SDK_INT < 29 ?
|
||||||
new ConditionManagerImpl(this, this::onPermissionUpdate) :
|
new ConditionManager(this, this::onPermissionUpdate) :
|
||||||
new ConditionManager29Impl(this, this::onPermissionUpdate);
|
new ConditionManager29(this, this::onPermissionUpdate);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAttach(Context context) {
|
public void onAttach(Context context) {
|
||||||
|
|||||||
Reference in New Issue
Block a user