mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 19:29:06 +01:00
Merge branch '2151-bluetooth-connect-ui' into 'master'
Add simple UI for Connect via Bluetooth feature Closes #2151 and #1821 See merge request briar/briar!1524
This commit is contained in:
@@ -35,6 +35,7 @@ import org.briarproject.briar.BriarCoreEagerSingletons;
|
||||
import org.briarproject.briar.BriarCoreModule;
|
||||
import org.briarproject.briar.android.attachment.AttachmentModule;
|
||||
import org.briarproject.briar.android.attachment.media.MediaModule;
|
||||
import org.briarproject.briar.android.contact.connect.BluetoothIntroFragment;
|
||||
import org.briarproject.briar.android.conversation.glide.BriarModelLoader;
|
||||
import org.briarproject.briar.android.hotspot.AbstractTabsFragment;
|
||||
import org.briarproject.briar.android.hotspot.FallbackFragment;
|
||||
@@ -236,4 +237,6 @@ public interface AndroidComponent
|
||||
void inject(SendFragment sendFragment);
|
||||
|
||||
void inject(ReceiveFragment receiveFragment);
|
||||
|
||||
void inject(BluetoothIntroFragment bluetoothIntroFragment);
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.briarproject.briar.android.account.SetupModule;
|
||||
import org.briarproject.briar.android.blog.BlogModule;
|
||||
import org.briarproject.briar.android.contact.ContactListModule;
|
||||
import org.briarproject.briar.android.contact.add.nearby.AddNearbyContactModule;
|
||||
import org.briarproject.briar.android.contact.connect.ConnectViaBluetoothModule;
|
||||
import org.briarproject.briar.android.forum.ForumModule;
|
||||
import org.briarproject.briar.android.hotspot.HotspotModule;
|
||||
import org.briarproject.briar.android.introduction.IntroductionModule;
|
||||
@@ -89,6 +90,7 @@ import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
||||
DevReportModule.class,
|
||||
ContactListModule.class,
|
||||
IntroductionModule.class,
|
||||
ConnectViaBluetoothModule.class,
|
||||
// below need to be within same scope as ViewModelProvider.Factory
|
||||
BlogModule.class,
|
||||
ForumModule.class,
|
||||
|
||||
@@ -28,8 +28,8 @@ import org.briarproject.briar.android.contact.add.remote.AddContactActivity;
|
||||
import org.briarproject.briar.android.contact.add.remote.LinkExchangeFragment;
|
||||
import org.briarproject.briar.android.contact.add.remote.NicknameFragment;
|
||||
import org.briarproject.briar.android.contact.add.remote.PendingContactListActivity;
|
||||
import org.briarproject.briar.android.contact.connect.ConnectViaBluetoothActivity;
|
||||
import org.briarproject.briar.android.conversation.AliasDialogFragment;
|
||||
import org.briarproject.briar.android.conversation.BluetoothConnecterDialogFragment;
|
||||
import org.briarproject.briar.android.conversation.ConversationActivity;
|
||||
import org.briarproject.briar.android.conversation.ConversationSettingsDialog;
|
||||
import org.briarproject.briar.android.conversation.ImageActivity;
|
||||
@@ -238,9 +238,6 @@ public interface ActivityComponent {
|
||||
|
||||
void inject(ConversationSettingsDialog dialog);
|
||||
|
||||
void inject(
|
||||
BluetoothConnecterDialogFragment bluetoothConnecterDialogFragment);
|
||||
|
||||
void inject(RssFeedImportFragment fragment);
|
||||
|
||||
void inject(RssFeedManageFragment fragment);
|
||||
@@ -248,4 +245,6 @@ public interface ActivityComponent {
|
||||
void inject(RssFeedImportFailedDialogFragment fragment);
|
||||
|
||||
void inject(RssFeedDeleteFeedDialogFragment fragment);
|
||||
|
||||
void inject(ConnectViaBluetoothActivity connectViaBluetoothActivity);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package org.briarproject.briar.android.contact.connect;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
|
||||
import org.briarproject.briar.R;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
|
||||
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static androidx.core.app.ActivityCompat.shouldShowRequestPermissionRationale;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getGoToSettingsListener;
|
||||
import static org.briarproject.briar.android.util.UiUtils.isLocationEnabled;
|
||||
import static org.briarproject.briar.android.util.UiUtils.showLocationDialog;
|
||||
|
||||
class BluetoothConditionManager {
|
||||
|
||||
private enum Permission {
|
||||
UNKNOWN, GRANTED, SHOW_RATIONALE, PERMANENTLY_DENIED
|
||||
}
|
||||
|
||||
private Permission locationPermission = Permission.UNKNOWN;
|
||||
|
||||
/**
|
||||
* Call this when the using activity or fragment starts,
|
||||
* because permissions might have changed while it was stopped.
|
||||
*/
|
||||
void reset() {
|
||||
locationPermission = Permission.UNKNOWN;
|
||||
}
|
||||
|
||||
@UiThread
|
||||
void onLocationPermissionResult(Activity activity,
|
||||
@Nullable Boolean result) {
|
||||
if (result != null && result) {
|
||||
locationPermission = Permission.GRANTED;
|
||||
} else if (shouldShowRequestPermissionRationale(activity,
|
||||
ACCESS_FINE_LOCATION)) {
|
||||
locationPermission = Permission.SHOW_RATIONALE;
|
||||
} else {
|
||||
locationPermission = Permission.PERMANENTLY_DENIED;
|
||||
}
|
||||
}
|
||||
|
||||
boolean areRequirementsFulfilled(Context ctx,
|
||||
ActivityResultLauncher<String> permissionRequest,
|
||||
Runnable onLocationDenied) {
|
||||
boolean permissionGranted =
|
||||
SDK_INT < 23 || locationPermission == Permission.GRANTED;
|
||||
boolean locationEnabled = isLocationEnabled(ctx);
|
||||
if (permissionGranted && locationEnabled) return true;
|
||||
|
||||
if (locationPermission == Permission.PERMANENTLY_DENIED) {
|
||||
showDenialDialog(ctx, onLocationDenied);
|
||||
} else if (locationPermission == Permission.SHOW_RATIONALE) {
|
||||
showRationale(ctx, permissionRequest);
|
||||
} else if (!locationEnabled) {
|
||||
showLocationDialog(ctx);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void showDenialDialog(Context ctx, Runnable onLocationDenied) {
|
||||
new AlertDialog.Builder(ctx, R.style.BriarDialogTheme)
|
||||
.setTitle(R.string.permission_location_title)
|
||||
.setMessage(R.string.permission_location_denied_body)
|
||||
.setPositiveButton(R.string.ok, getGoToSettingsListener(ctx))
|
||||
.setNegativeButton(R.string.cancel, (v, d) ->
|
||||
onLocationDenied.run())
|
||||
.show();
|
||||
}
|
||||
|
||||
private void showRationale(Context ctx,
|
||||
ActivityResultLauncher<String> permissionRequest) {
|
||||
new AlertDialog.Builder(ctx, R.style.BriarDialogTheme)
|
||||
.setTitle(R.string.permission_location_title)
|
||||
.setMessage(R.string.permission_location_request_body)
|
||||
.setPositiveButton(R.string.ok, (dialog, which) ->
|
||||
permissionRequest.launch(ACCESS_FINE_LOCATION))
|
||||
.show();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package org.briarproject.briar.android.contact.connect;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.util.ActivityLaunchers.RequestBluetoothDiscoverable;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
|
||||
import static android.widget.Toast.LENGTH_LONG;
|
||||
import static org.briarproject.briar.android.AppModule.getAndroidComponent;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class BluetoothIntroFragment extends Fragment {
|
||||
|
||||
final static String TAG = BluetoothIntroFragment.class.getName();
|
||||
|
||||
@Inject
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
|
||||
private final BluetoothConditionManager conditionManager =
|
||||
new BluetoothConditionManager();
|
||||
private ConnectViaBluetoothViewModel viewModel;
|
||||
|
||||
private final ActivityResultLauncher<Integer> bluetoothDiscoverableRequest =
|
||||
registerForActivityResult(new RequestBluetoothDiscoverable(),
|
||||
this::onBluetoothDiscoverable);
|
||||
private final ActivityResultLauncher<String> permissionRequest =
|
||||
registerForActivityResult(new RequestPermission(),
|
||||
this::onPermissionRequestResult);
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
getAndroidComponent(requireContext()).inject(this);
|
||||
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory)
|
||||
.get(ConnectViaBluetoothViewModel.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater,
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
return inflater
|
||||
.inflate(R.layout.fragment_bluetooth_intro, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
||||
Button startButton = view.findViewById(R.id.startButton);
|
||||
startButton.setOnClickListener(this::onStartClicked);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
conditionManager.reset();
|
||||
}
|
||||
|
||||
private void onStartClicked(View v) {
|
||||
if (viewModel.shouldStartFlow()) {
|
||||
// The dialog starts a permission request which comes back as true
|
||||
// if the permission is already granted.
|
||||
// So we can use the request as a generic entry point
|
||||
// to the whole flow.
|
||||
permissionRequest.launch(ACCESS_FINE_LOCATION);
|
||||
}
|
||||
}
|
||||
|
||||
private void onPermissionRequestResult(@Nullable Boolean result) {
|
||||
Activity a = requireActivity();
|
||||
// update permission result in BluetoothConnecter
|
||||
conditionManager.onLocationPermissionResult(a, result);
|
||||
// what to do when the user denies granting the location permission
|
||||
Runnable onLocationPermissionDenied = () -> Toast.makeText(
|
||||
requireContext(),
|
||||
R.string.connect_via_bluetooth_no_location_permission,
|
||||
LENGTH_LONG).show();
|
||||
// if requirements are fulfilled, request Bluetooth discoverability
|
||||
if (conditionManager.areRequirementsFulfilled(a, permissionRequest,
|
||||
onLocationPermissionDenied)) {
|
||||
bluetoothDiscoverableRequest.launch(120); // for 2min
|
||||
}
|
||||
}
|
||||
|
||||
private void onBluetoothDiscoverable(@Nullable Boolean result) {
|
||||
if (result != null && result) {
|
||||
viewModel.onBluetoothDiscoverable();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.briarproject.briar.android.contact.connect;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class BluetoothProgressFragment extends Fragment {
|
||||
|
||||
final static String TAG = BluetoothProgressFragment.class.getName();
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater,
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
return inflater
|
||||
.inflate(R.layout.fragment_bluetooth_progress, container, false);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package org.briarproject.briar.android.contact.connect;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.activity.BriarActivity;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import static android.widget.Toast.LENGTH_LONG;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static org.briarproject.briar.android.conversation.ConversationActivity.CONTACT_ID;
|
||||
import static org.briarproject.briar.android.util.UiUtils.showFragment;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class ConnectViaBluetoothActivity extends BriarActivity {
|
||||
|
||||
@Inject
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
|
||||
private ConnectViaBluetoothViewModel viewModel;
|
||||
|
||||
@Override
|
||||
public void injectActivity(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
|
||||
viewModel = new ViewModelProvider(this, viewModelFactory)
|
||||
.get(ConnectViaBluetoothViewModel.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
Intent intent = requireNonNull(getIntent());
|
||||
int contactId = intent.getIntExtra(CONTACT_ID, -1);
|
||||
if (contactId == -1) throw new IllegalArgumentException("ContactId");
|
||||
viewModel.setContactId(new ContactId(contactId));
|
||||
|
||||
setContentView(R.layout.activity_fragment_container);
|
||||
|
||||
viewModel.getState().observeEvent(this, this::onStateChanged);
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
Fragment f = new BluetoothIntroFragment();
|
||||
String tag = BluetoothIntroFragment.TAG;
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.replace(R.id.fragmentContainer, f, tag)
|
||||
.commitNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
viewModel.reset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
||||
if (item.getItemId() == android.R.id.home) {
|
||||
onBackPressed();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
private void onStateChanged(ConnectViaBluetoothState state) {
|
||||
if (state instanceof ConnectViaBluetoothState.Connecting) {
|
||||
Fragment f = new BluetoothProgressFragment();
|
||||
String tag = BluetoothProgressFragment.TAG;
|
||||
showFragment(getSupportFragmentManager(), f, tag, false);
|
||||
} else if (state instanceof ConnectViaBluetoothState.Success) {
|
||||
Toast.makeText(this, R.string.connect_via_bluetooth_success,
|
||||
LENGTH_LONG).show();
|
||||
supportFinishAfterTransition();
|
||||
} else if (state instanceof ConnectViaBluetoothState.Error) {
|
||||
Toast.makeText(this,
|
||||
((ConnectViaBluetoothState.Error) state).errorRes,
|
||||
LENGTH_LONG).show();
|
||||
supportFinishAfterTransition();
|
||||
} else throw new AssertionError();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.briarproject.briar.android.contact.connect;
|
||||
|
||||
import org.briarproject.briar.android.viewmodel.ViewModelKey;
|
||||
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import dagger.Binds;
|
||||
import dagger.Module;
|
||||
import dagger.multibindings.IntoMap;
|
||||
|
||||
@Module
|
||||
public abstract class ConnectViaBluetoothModule {
|
||||
|
||||
@Binds
|
||||
@IntoMap
|
||||
@ViewModelKey(ConnectViaBluetoothViewModel.class)
|
||||
abstract ViewModel bindContactListViewModel(
|
||||
ConnectViaBluetoothViewModel connectViaBluetoothViewModel);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.briarproject.briar.android.contact.connect;
|
||||
|
||||
import androidx.annotation.StringRes;
|
||||
|
||||
abstract class ConnectViaBluetoothState {
|
||||
|
||||
static class Connecting extends ConnectViaBluetoothState {
|
||||
}
|
||||
|
||||
static class Success extends ConnectViaBluetoothState {
|
||||
}
|
||||
|
||||
static class Error extends ConnectViaBluetoothState {
|
||||
@StringRes
|
||||
final int errorRes;
|
||||
|
||||
Error(@StringRes int errorRes) {
|
||||
this.errorRes = errorRes;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
package org.briarproject.briar.android.conversation;
|
||||
package org.briarproject.briar.android.contact.connect;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Application;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.Context;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.briarproject.bramble.api.connection.ConnectionManager;
|
||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.TransactionManager;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.PluginManager;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
||||
@@ -21,23 +22,22 @@ import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||
import org.briarproject.bramble.plugin.bluetooth.BluetoothPlugin;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.contact.ContactItem;
|
||||
import org.briarproject.briar.android.contact.connect.ConnectViaBluetoothState.Connecting;
|
||||
import org.briarproject.briar.android.contact.connect.ConnectViaBluetoothState.Success;
|
||||
import org.briarproject.briar.android.viewmodel.DbViewModel;
|
||||
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
||||
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
|
||||
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static androidx.core.app.ActivityCompat.shouldShowRequestPermissionRationale;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
@@ -46,48 +46,50 @@ import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_UUID;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getGoToSettingsListener;
|
||||
import static org.briarproject.briar.android.util.UiUtils.isLocationEnabled;
|
||||
import static org.briarproject.briar.android.util.UiUtils.showLocationDialog;
|
||||
|
||||
class BluetoothConnecter implements EventListener {
|
||||
@UiThread
|
||||
@NotNullByDefault
|
||||
class ConnectViaBluetoothViewModel extends DbViewModel implements
|
||||
EventListener {
|
||||
|
||||
private final Logger LOG = getLogger(BluetoothConnecter.class.getName());
|
||||
private final Logger LOG =
|
||||
getLogger(ConnectViaBluetoothViewModel.class.getName());
|
||||
|
||||
private final long BT_ACTIVE_TIMEOUT = SECONDS.toMillis(5);
|
||||
|
||||
private enum Permission {
|
||||
UNKNOWN, GRANTED, SHOW_RATIONALE, PERMANENTLY_DENIED
|
||||
}
|
||||
|
||||
private final Application app;
|
||||
private final PluginManager pluginManager;
|
||||
private final Executor ioExecutor;
|
||||
private final AndroidExecutor androidExecutor;
|
||||
private final ConnectionRegistry connectionRegistry;
|
||||
@Nullable
|
||||
private final BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
|
||||
private final EventBus eventBus;
|
||||
private final TransportPropertyManager transportPropertyManager;
|
||||
private final ConnectionManager connectionManager;
|
||||
|
||||
@Nullable
|
||||
private volatile BluetoothPlugin bluetoothPlugin;
|
||||
|
||||
private Permission locationPermission = Permission.UNKNOWN;
|
||||
@Nullable
|
||||
private ContactId contactId = null;
|
||||
|
||||
private final MutableLiveEvent<ConnectViaBluetoothState> state =
|
||||
new MutableLiveEvent<>();
|
||||
|
||||
@Inject
|
||||
BluetoothConnecter(Application app,
|
||||
ConnectViaBluetoothViewModel(
|
||||
Application app,
|
||||
@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager,
|
||||
TransactionManager db,
|
||||
AndroidExecutor androidExecutor,
|
||||
PluginManager pluginManager,
|
||||
@IoExecutor Executor ioExecutor,
|
||||
AndroidExecutor androidExecutor,
|
||||
ConnectionRegistry connectionRegistry,
|
||||
EventBus eventBus,
|
||||
TransportPropertyManager transportPropertyManager,
|
||||
ConnectionManager connectionManager) {
|
||||
this.app = app;
|
||||
super(app, dbExecutor, lifecycleManager, db, androidExecutor);
|
||||
this.pluginManager = pluginManager;
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.androidExecutor = androidExecutor;
|
||||
this.bluetoothPlugin = (BluetoothPlugin) pluginManager.getPlugin(ID);
|
||||
this.connectionRegistry = connectionRegistry;
|
||||
this.eventBus = eventBus;
|
||||
@@ -95,20 +97,22 @@ class BluetoothConnecter implements EventListener {
|
||||
this.connectionManager = connectionManager;
|
||||
}
|
||||
|
||||
boolean isConnectedViaBluetooth(ContactId contactId) {
|
||||
return connectionRegistry.isConnected(contactId, ID);
|
||||
}
|
||||
|
||||
boolean isDiscovering() {
|
||||
return bluetoothPlugin.isDiscovering();
|
||||
@Override
|
||||
protected void onCleared() {
|
||||
stopConnecting();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this when the using activity or fragment starts,
|
||||
* because permissions might have changed while it was stopped.
|
||||
* Set this as soon as it becomes available.
|
||||
*/
|
||||
void setContactId(ContactId contactId) {
|
||||
this.contactId = contactId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this when the using activity or fragment starts.
|
||||
*/
|
||||
void reset() {
|
||||
locationPermission = Permission.UNKNOWN;
|
||||
// When this class is instantiated before we are logged in
|
||||
// (like when returning to a killed activity), bluetoothPlugin would be
|
||||
// null and we consider bluetooth not supported. So reset here.
|
||||
@@ -116,94 +120,52 @@ class BluetoothConnecter implements EventListener {
|
||||
}
|
||||
|
||||
@UiThread
|
||||
void onLocationPermissionResult(Activity activity,
|
||||
@Nullable Boolean result) {
|
||||
if (result != null && result) {
|
||||
locationPermission = Permission.GRANTED;
|
||||
} else if (shouldShowRequestPermissionRationale(activity,
|
||||
ACCESS_FINE_LOCATION)) {
|
||||
locationPermission = Permission.SHOW_RATIONALE;
|
||||
} else {
|
||||
locationPermission = Permission.PERMANENTLY_DENIED;
|
||||
boolean shouldStartFlow() {
|
||||
if (isBluetoothNotSupported()) {
|
||||
state.setEvent(new ConnectViaBluetoothState.Error(
|
||||
R.string.bt_plugin_status_inactive));
|
||||
return false;
|
||||
} else if (isConnectedViaBluetooth()) {
|
||||
state.setEvent(new Success());
|
||||
return false;
|
||||
} else if (isDiscovering()) {
|
||||
state.setEvent(new ConnectViaBluetoothState.Error(
|
||||
R.string.connect_via_bluetooth_already_discovering));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean isBluetoothNotSupported() {
|
||||
private boolean isBluetoothNotSupported() {
|
||||
return bt == null || bluetoothPlugin == null;
|
||||
}
|
||||
|
||||
boolean areRequirementsFulfilled(Context ctx,
|
||||
ActivityResultLauncher<String> permissionRequest,
|
||||
Runnable onLocationDenied) {
|
||||
boolean permissionGranted =
|
||||
SDK_INT < 23 || locationPermission == Permission.GRANTED;
|
||||
boolean locationEnabled = isLocationEnabled(ctx);
|
||||
if (permissionGranted && locationEnabled) return true;
|
||||
|
||||
if (locationPermission == Permission.PERMANENTLY_DENIED) {
|
||||
showDenialDialog(ctx, onLocationDenied);
|
||||
} else if (locationPermission == Permission.SHOW_RATIONALE) {
|
||||
showRationale(ctx, permissionRequest);
|
||||
} else if (!locationEnabled) {
|
||||
showLocationDialog(ctx);
|
||||
}
|
||||
return false;
|
||||
private boolean isDiscovering() {
|
||||
// we should not be calling this if isBluetoothNotSupported() is true
|
||||
return requireNonNull(bluetoothPlugin).isDiscovering();
|
||||
}
|
||||
|
||||
private void showDenialDialog(Context ctx, Runnable onLocationDenied) {
|
||||
new AlertDialog.Builder(ctx, R.style.BriarDialogTheme)
|
||||
.setTitle(R.string.permission_location_title)
|
||||
.setMessage(R.string.permission_location_denied_body)
|
||||
.setPositiveButton(R.string.ok, getGoToSettingsListener(ctx))
|
||||
.setNegativeButton(R.string.cancel, (v, d) ->
|
||||
onLocationDenied.run())
|
||||
.show();
|
||||
}
|
||||
|
||||
private void showRationale(Context ctx,
|
||||
ActivityResultLauncher<String> permissionRequest) {
|
||||
new AlertDialog.Builder(ctx, R.style.BriarDialogTheme)
|
||||
.setTitle(R.string.permission_location_title)
|
||||
.setMessage(R.string.permission_location_request_body)
|
||||
.setPositiveButton(R.string.ok, (dialog, which) ->
|
||||
permissionRequest.launch(ACCESS_FINE_LOCATION))
|
||||
.show();
|
||||
private boolean isConnectedViaBluetooth() {
|
||||
return connectionRegistry.isConnected(requireNonNull(contactId), ID);
|
||||
}
|
||||
|
||||
@UiThread
|
||||
void onBluetoothDiscoverable(ContactItem contact) {
|
||||
contactId = contact.getContact().getId();
|
||||
connect();
|
||||
}
|
||||
void onBluetoothDiscoverable() {
|
||||
ContactId contactId = requireNonNull(this.contactId);
|
||||
BluetoothPlugin bluetoothPlugin = requireNonNull(this.bluetoothPlugin);
|
||||
|
||||
@UiThread
|
||||
@Override
|
||||
public void eventOccurred(@NonNull Event e) {
|
||||
if (e instanceof ConnectionOpenedEvent) {
|
||||
ConnectionOpenedEvent c = (ConnectionOpenedEvent) e;
|
||||
if (c.getContactId().equals(contactId) && c.isIncoming() &&
|
||||
c.getTransportId() == ID) {
|
||||
if (bluetoothPlugin != null) {
|
||||
bluetoothPlugin.stopDiscoverAndConnect();
|
||||
}
|
||||
LOG.info("Contact connected to us");
|
||||
showToast(R.string.toast_connect_via_bluetooth_success);
|
||||
}
|
||||
}
|
||||
}
|
||||
state.setEvent(new Connecting());
|
||||
|
||||
private void connect() {
|
||||
bluetoothPlugin.disablePolling();
|
||||
pluginManager.setPluginEnabled(ID, true);
|
||||
|
||||
ioExecutor.execute(() -> {
|
||||
try {
|
||||
if (!waitForBluetoothActive()) {
|
||||
showToast(R.string.bt_plugin_status_inactive);
|
||||
state.postEvent(new ConnectViaBluetoothState.Error(
|
||||
R.string.bt_plugin_status_inactive));
|
||||
LOG.warning("Bluetooth plugin didn't become active");
|
||||
return;
|
||||
}
|
||||
showToast(R.string.toast_connect_via_bluetooth_start);
|
||||
eventBus.addListener(this);
|
||||
try {
|
||||
String uuid = null;
|
||||
@@ -226,7 +188,7 @@ class BluetoothConnecter implements EventListener {
|
||||
LOG.info("Could connect, handling connection");
|
||||
connectionManager
|
||||
.manageOutgoingConnection(contactId, ID, conn);
|
||||
showToast(R.string.toast_connect_via_bluetooth_success);
|
||||
state.postEvent(new Success());
|
||||
}
|
||||
} finally {
|
||||
eventBus.removeListener(this);
|
||||
@@ -237,8 +199,23 @@ class BluetoothConnecter implements EventListener {
|
||||
});
|
||||
}
|
||||
|
||||
@UiThread
|
||||
@Override
|
||||
public void eventOccurred(@NonNull Event e) {
|
||||
if (e instanceof ConnectionOpenedEvent) {
|
||||
ConnectionOpenedEvent c = (ConnectionOpenedEvent) e;
|
||||
if (c.getContactId().equals(contactId) && c.isIncoming() &&
|
||||
c.getTransportId() == ID) {
|
||||
stopConnecting();
|
||||
LOG.info("Contact connected to us");
|
||||
state.postEvent(new Success());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@IoExecutor
|
||||
private boolean waitForBluetoothActive() {
|
||||
BluetoothPlugin bluetoothPlugin = requireNonNull(this.bluetoothPlugin);
|
||||
long left = BT_ACTIVE_TIMEOUT;
|
||||
final long sleep = 250;
|
||||
try {
|
||||
@@ -264,9 +241,9 @@ class BluetoothConnecter implements EventListener {
|
||||
final long sleep = 250;
|
||||
try {
|
||||
while (left > 0) {
|
||||
if (isConnectedViaBluetooth(contactId)) {
|
||||
if (isConnectedViaBluetooth()) {
|
||||
LOG.info("Failed to connect, but contact connected");
|
||||
// no Toast needed here, as it gets shown when
|
||||
// no success state needed here, as it gets shown when
|
||||
// ConnectionOpenedEvent is received
|
||||
return;
|
||||
}
|
||||
@@ -277,13 +254,19 @@ class BluetoothConnecter implements EventListener {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
LOG.warning("Failed to connect");
|
||||
showToast(R.string.toast_connect_via_bluetooth_error);
|
||||
state.postEvent(new ConnectViaBluetoothState.Error(
|
||||
R.string.connect_via_bluetooth_error));
|
||||
}
|
||||
|
||||
private void showToast(@StringRes int res) {
|
||||
androidExecutor.runOnUiThread(() ->
|
||||
Toast.makeText(app, res, Toast.LENGTH_LONG).show()
|
||||
);
|
||||
private void stopConnecting() {
|
||||
BluetoothPlugin bluetoothPlugin = this.bluetoothPlugin;
|
||||
if (bluetoothPlugin != null) {
|
||||
bluetoothPlugin.stopDiscoverAndConnect();
|
||||
}
|
||||
}
|
||||
|
||||
LiveEvent<ConnectViaBluetoothState> getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
package org.briarproject.briar.android.conversation;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.BaseActivity;
|
||||
import org.briarproject.briar.android.contact.ContactItem;
|
||||
import org.briarproject.briar.android.util.ActivityLaunchers.RequestBluetoothDiscoverable;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
|
||||
import static android.content.DialogInterface.BUTTON_POSITIVE;
|
||||
import static android.widget.Toast.LENGTH_LONG;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class BluetoothConnecterDialogFragment extends DialogFragment {
|
||||
|
||||
final static String TAG = BluetoothConnecterDialogFragment.class.getName();
|
||||
|
||||
@Inject
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
|
||||
private ConversationViewModel viewModel;
|
||||
private BluetoothConnecter bluetoothConnecter;
|
||||
|
||||
private final ActivityResultLauncher<Integer> bluetoothDiscoverableRequest =
|
||||
registerForActivityResult(new RequestBluetoothDiscoverable(),
|
||||
this::onBluetoothDiscoverable);
|
||||
private final ActivityResultLauncher<String> permissionRequest =
|
||||
registerForActivityResult(new RequestPermission(),
|
||||
this::onPermissionRequestResult);
|
||||
|
||||
@Override
|
||||
public void onAttach(Context ctx) {
|
||||
super.onAttach(ctx);
|
||||
((BaseActivity) requireActivity()).getActivityComponent().inject(this);
|
||||
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory)
|
||||
.get(ConversationViewModel.class);
|
||||
bluetoothConnecter = viewModel.getBluetoothConnecter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
||||
Context ctx = requireContext();
|
||||
return new AlertDialog.Builder(ctx, R.style.BriarDialogTheme)
|
||||
.setTitle(R.string.dialog_title_connect_via_bluetooth)
|
||||
.setMessage(R.string.dialog_message_connect_via_bluetooth)
|
||||
// actual listener gets set in onResume()
|
||||
.setPositiveButton(R.string.start, null)
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.setCancelable(false) // keep it open until dismissed
|
||||
.create();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
bluetoothConnecter.reset();
|
||||
if (bluetoothConnecter.isBluetoothNotSupported()) {
|
||||
showToast(R.string.toast_connect_via_bluetooth_error);
|
||||
dismiss();
|
||||
return;
|
||||
}
|
||||
// MenuItem only gets enabled after contactItem has loaded
|
||||
ContactItem contact =
|
||||
requireNonNull(viewModel.getContactItem().getValue());
|
||||
ContactId contactId = contact.getContact().getId();
|
||||
if (bluetoothConnecter.isConnectedViaBluetooth(contactId)) {
|
||||
showToast(R.string.toast_connect_via_bluetooth_success);
|
||||
dismiss();
|
||||
return;
|
||||
}
|
||||
if (bluetoothConnecter.isDiscovering()) {
|
||||
showToast(R.string.toast_connect_via_bluetooth_already_discovering);
|
||||
dismiss();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
// Set the click listener for the START button here
|
||||
// to prevent it from automatically dismissing the dialog.
|
||||
// The dialog is shown in onStart(), so we set the listener here later.
|
||||
AlertDialog dialog = (AlertDialog) getDialog();
|
||||
Button positiveButton = dialog.getButton(BUTTON_POSITIVE);
|
||||
positiveButton.setOnClickListener(this::onStartClicked);
|
||||
}
|
||||
|
||||
private void onStartClicked(View v) {
|
||||
// The dialog starts a permission request which comes back as true
|
||||
// if the permission is already granted.
|
||||
// So we can use the request as a generic entry point to the whole flow.
|
||||
permissionRequest.launch(ACCESS_FINE_LOCATION);
|
||||
}
|
||||
|
||||
private void onPermissionRequestResult(@Nullable Boolean result) {
|
||||
Activity a = requireActivity();
|
||||
// update permission result in BluetoothConnecter
|
||||
bluetoothConnecter.onLocationPermissionResult(a, result);
|
||||
// what to do when the user denies granting the location permission
|
||||
Runnable onLocationPermissionDenied = () -> {
|
||||
Toast.makeText(requireContext(),
|
||||
R.string.toast_connect_via_bluetooth_no_location_permission,
|
||||
LENGTH_LONG).show();
|
||||
dismiss();
|
||||
};
|
||||
// if requirements are fulfilled, request Bluetooth discoverability
|
||||
if (bluetoothConnecter.areRequirementsFulfilled(a, permissionRequest,
|
||||
onLocationPermissionDenied)) {
|
||||
bluetoothDiscoverableRequest.launch(120); // for 2min
|
||||
}
|
||||
}
|
||||
|
||||
private void onBluetoothDiscoverable(@Nullable Boolean result) {
|
||||
if (result != null && result) {
|
||||
// MenuItem only gets enabled after contactItem has loaded
|
||||
ContactItem contact =
|
||||
requireNonNull(viewModel.getContactItem().getValue());
|
||||
bluetoothConnecter.onBluetoothDiscoverable(contact);
|
||||
dismiss();
|
||||
} else {
|
||||
showToast(R.string.toast_connect_via_bluetooth_not_discoverable);
|
||||
}
|
||||
}
|
||||
|
||||
private void showToast(@StringRes int stringRes) {
|
||||
Toast.makeText(requireContext(), stringRes, LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -48,6 +48,7 @@ import org.briarproject.briar.android.activity.BriarActivity;
|
||||
import org.briarproject.briar.android.attachment.AttachmentItem;
|
||||
import org.briarproject.briar.android.attachment.AttachmentRetriever;
|
||||
import org.briarproject.briar.android.blog.BlogActivity;
|
||||
import org.briarproject.briar.android.contact.connect.ConnectViaBluetoothActivity;
|
||||
import org.briarproject.briar.android.conversation.ConversationVisitor.AttachmentCache;
|
||||
import org.briarproject.briar.android.conversation.ConversationVisitor.TextCache;
|
||||
import org.briarproject.briar.android.forum.ForumActivity;
|
||||
@@ -104,7 +105,6 @@ import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.app.ActivityOptionsCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
@@ -422,9 +422,9 @@ public class ConversationActivity extends BriarActivity
|
||||
onAutoDeleteTimerNoticeClicked();
|
||||
return true;
|
||||
} else if (itemId == R.id.action_connect_via_bluetooth) {
|
||||
FragmentManager fm = getSupportFragmentManager();
|
||||
new BluetoothConnecterDialogFragment().show(fm,
|
||||
BluetoothConnecterDialogFragment.TAG);
|
||||
Intent intent = new Intent(this, ConnectViaBluetoothActivity.class);
|
||||
intent.putExtra(CONTACT_ID, contactId.getInt());
|
||||
startActivity(intent);
|
||||
return true;
|
||||
} else if (itemId == R.id.action_transfer_data) {
|
||||
Intent intent = new Intent(this, RemovableDriveActivity.class);
|
||||
|
||||
@@ -101,7 +101,6 @@ public class ConversationViewModel extends DbViewModel
|
||||
private final AttachmentCreator attachmentCreator;
|
||||
private final AutoDeleteManager autoDeleteManager;
|
||||
private final ConversationManager conversationManager;
|
||||
private final BluetoothConnecter bluetoothConnecter;
|
||||
|
||||
@Nullable
|
||||
private ContactId contactId = null;
|
||||
@@ -140,8 +139,7 @@ public class ConversationViewModel extends DbViewModel
|
||||
AttachmentRetriever attachmentRetriever,
|
||||
AttachmentCreator attachmentCreator,
|
||||
AutoDeleteManager autoDeleteManager,
|
||||
ConversationManager conversationManager,
|
||||
BluetoothConnecter bluetoothConnecter) {
|
||||
ConversationManager conversationManager) {
|
||||
super(application, dbExecutor, lifecycleManager, db, androidExecutor);
|
||||
this.db = db;
|
||||
this.eventBus = eventBus;
|
||||
@@ -154,7 +152,6 @@ public class ConversationViewModel extends DbViewModel
|
||||
this.attachmentCreator = attachmentCreator;
|
||||
this.autoDeleteManager = autoDeleteManager;
|
||||
this.conversationManager = conversationManager;
|
||||
this.bluetoothConnecter = bluetoothConnecter;
|
||||
messagingGroupId = map(contactItem, c ->
|
||||
messagingManager.getContactGroup(c.getContact()).getId());
|
||||
eventBus.addListener(this);
|
||||
@@ -414,10 +411,6 @@ public class ConversationViewModel extends DbViewModel
|
||||
return attachmentRetriever;
|
||||
}
|
||||
|
||||
BluetoothConnecter getBluetoothConnecter() {
|
||||
return bluetoothConnecter;
|
||||
}
|
||||
|
||||
LiveData<ContactItem> getContactItem() {
|
||||
return contactItem;
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import androidx.core.widget.NestedScrollView;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import static android.view.View.FOCUS_DOWN;
|
||||
import static android.view.View.GONE;
|
||||
|
||||
/**
|
||||
* A fragment to be used at the end of a user flow
|
||||
@@ -81,7 +82,12 @@ public class FinalFragment extends Fragment {
|
||||
int color = getResources().getColor(args.getInt(ARG_ICON_TINT));
|
||||
ColorStateList tint = ColorStateList.valueOf(color);
|
||||
ImageViewCompat.setImageTintList(iconView, tint);
|
||||
textView.setText(args.getInt(ARG_TEXT));
|
||||
int textRes = args.getInt(ARG_TEXT);
|
||||
if (textRes == 0) {
|
||||
textView.setVisibility(GONE);
|
||||
} else {
|
||||
textView.setText(textRes);
|
||||
}
|
||||
|
||||
buttonView.setOnClickListener(view -> onBackButtonPressed());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user