Commit 48d907dd authored by Torsten Grote's avatar Torsten Grote

Merge branch '185-transports-activity' into 'master'

Add connections screen with information about transports

Closes #185

See merge request !1277
parents 3e5b7f45 2cf146a1
Pipeline #4754 passed with stage
in 9 minutes and 31 seconds
......@@ -53,6 +53,7 @@ import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.api.plugin.Plugin.PREF_PLUGIN_ENABLE;
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
import static org.briarproject.bramble.util.LogUtils.logDuration;
import static org.briarproject.bramble.util.LogUtils.logException;
......@@ -354,6 +355,10 @@ class PluginManagerImpl implements PluginManager, Service {
} else if (oldState == ACTIVE) {
eventBus.broadcast(new TransportInactiveEvent(id));
}
} else if (newState == DISABLED) {
// Broadcast an event even though the state hasn't changed, as
// the reasons for the plugin being disabled may have changed
eventBus.broadcast(new TransportStateEvent(id, newState));
}
}
......
This diff is collapsed.
......@@ -47,6 +47,7 @@ import org.briarproject.briar.android.login.OpenDatabaseFragment;
import org.briarproject.briar.android.login.PasswordFragment;
import org.briarproject.briar.android.login.StartupActivity;
import org.briarproject.briar.android.navdrawer.NavDrawerActivity;
import org.briarproject.briar.android.navdrawer.TransportsActivity;
import org.briarproject.briar.android.panic.PanicPreferencesActivity;
import org.briarproject.briar.android.panic.PanicResponderActivity;
import org.briarproject.briar.android.privategroup.conversation.GroupActivity;
......@@ -163,6 +164,8 @@ public interface ActivityComponent {
void inject(SettingsActivity activity);
void inject(TransportsActivity activity);
void inject(TestDataActivity activity);
void inject(ChangePasswordActivity activity);
......
......@@ -11,6 +11,7 @@ import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.material.navigation.NavigationView;
......@@ -56,8 +57,10 @@ import androidx.core.content.ContextCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
......@@ -75,6 +78,8 @@ import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_PASSWORD;
import static org.briarproject.briar.android.navdrawer.IntentRouter.handleExternalIntent;
import static org.briarproject.briar.android.util.UiUtils.getDaysUntilExpiry;
import static org.briarproject.briar.android.util.UiUtils.observeOnce;
import static org.briarproject.briar.android.util.UiUtils.resolveColorAttribute;
@MethodsNotNullByDefault
@ParametersNotNullByDefault
......@@ -97,6 +102,9 @@ public class NavDrawerActivity extends BriarActivity implements
public static Uri SIGN_OUT_URI =
Uri.parse("briar-content://org.briarproject.briar/sign-out");
private final List<Transport> transports = new ArrayList<>(3);
private final MutableLiveData<ImageView> torIcon = new MutableLiveData<>();
private NavDrawerViewModel navDrawerViewModel;
private PluginViewModel pluginViewModel;
private ActionBarDrawerToggle drawerToggle;
......@@ -110,7 +118,6 @@ public class NavDrawerActivity extends BriarActivity implements
private DrawerLayout drawerLayout;
private NavigationView navigation;
private List<Transport> transports;
private BaseAdapter transportsAdapter;
@Override
......@@ -141,6 +148,11 @@ public class NavDrawerActivity extends BriarActivity implements
drawerLayout = findViewById(R.id.drawer_layout);
navigation = findViewById(R.id.navigation);
GridView transportsView = findViewById(R.id.transportsView);
LinearLayout transportsLayout = findViewById(R.id.transports);
transportsLayout.setOnClickListener(v -> {
LOG.info("Starting transports activity");
startActivity(new Intent(this, TransportsActivity.class));
});
setSupportActionBar(toolbar);
ActionBar actionBar = requireNonNull(getSupportActionBar());
......@@ -149,13 +161,23 @@ public class NavDrawerActivity extends BriarActivity implements
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.nav_drawer_open_description,
R.string.nav_drawer_close_description);
R.string.nav_drawer_close_description) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
navDrawerViewModel.checkTransportsOnboarding();
}
};
drawerLayout.addDrawerListener(drawerToggle);
navigation.setNavigationItemSelectedListener(this);
initializeTransports();
transportsView.setAdapter(transportsAdapter);
observeOnce(navDrawerViewModel.showTransportsOnboarding(), this, show ->
observeOnce(torIcon, this, imageView ->
showTransportsOnboarding(show, imageView)));
lockManager.isLockable().observe(this, this::setLockVisible);
if (lifecycleManager.getLifecycleState().isAfter(RUNNING)) {
......@@ -380,8 +402,6 @@ public class NavDrawerActivity extends BriarActivity implements
}
private void initializeTransports() {
transports = new ArrayList<>(3);
transportsAdapter = new BaseAdapter() {
@Override
......@@ -422,6 +442,8 @@ public class NavDrawerActivity extends BriarActivity implements
TextView text = view.findViewById(R.id.textView);
text.setText(getString(t.label));
if (t.id.equals(TorConstants.ID)) torIcon.setValue(icon);
return view;
}
};
......@@ -444,7 +466,7 @@ public class NavDrawerActivity extends BriarActivity implements
private Transport createTransport(TransportId id,
@DrawableRes int iconDrawable, @StringRes int label) {
int iconColor = getIconColor(STARTING_STOPPING);
Transport transport = new Transport(iconDrawable, label, iconColor);
Transport transport = new Transport(id, iconDrawable, label, iconColor);
pluginViewModel.getPluginState(id).observe(this, state -> {
transport.iconColor = getIconColor(state);
transportsAdapter.notifyDataSetChanged();
......@@ -452,8 +474,25 @@ public class NavDrawerActivity extends BriarActivity implements
return transport;
}
private void showTransportsOnboarding(boolean show, ImageView imageView) {
if (show) {
int color = resolveColorAttribute(this, R.attr.colorControlNormal);
new MaterialTapTargetPrompt.Builder(NavDrawerActivity.this,
R.style.OnboardingDialogTheme).setTarget(imageView)
.setPrimaryText(R.string.network_settings_title)
.setSecondaryText(R.string.transports_onboarding_text)
.setIcon(R.drawable.transport_tor)
.setIconDrawableColourFilter(color)
.setBackgroundColour(
ContextCompat.getColor(this, R.color.briar_primary))
.show();
navDrawerViewModel.transportsOnboardingShown();
}
}
private static class Transport {
private final TransportId id;
@DrawableRes
private final int iconDrawable;
@StringRes
......@@ -462,8 +501,9 @@ public class NavDrawerActivity extends BriarActivity implements
@ColorRes
private int iconColor;
private Transport(@DrawableRes int iconDrawable, @StringRes int label,
@ColorRes int iconColor) {
private Transport(TransportId id, @DrawableRes int iconDrawable,
@StringRes int label, @ColorRes int iconColor) {
this.id = id;
this.iconDrawable = iconDrawable;
this.label = label;
this.iconColor = iconColor;
......
......@@ -34,6 +34,8 @@ public class NavDrawerViewModel extends AndroidViewModel {
getLogger(NavDrawerViewModel.class.getName());
private static final String EXPIRY_DATE_WARNING = "expiryDateWarning";
private static final String SHOW_TRANSPORTS_ONBOARDING =
"showTransportsOnboarding";
@DatabaseExecutor
private final Executor dbExecutor;
......@@ -43,6 +45,8 @@ public class NavDrawerViewModel extends AndroidViewModel {
new MutableLiveData<>();
private final MutableLiveData<Boolean> shouldAskForDozeWhitelisting =
new MutableLiveData<>();
private final MutableLiveData<Boolean> showTransportsOnboarding =
new MutableLiveData<>();
@Inject
NavDrawerViewModel(Application app, @DatabaseExecutor Executor dbExecutor,
......@@ -128,4 +132,39 @@ public class NavDrawerViewModel extends AndroidViewModel {
}
});
}
@UiThread
LiveData<Boolean> showTransportsOnboarding() {
return showTransportsOnboarding;
}
@UiThread
void checkTransportsOnboarding() {
if (showTransportsOnboarding.getValue() != null) return;
dbExecutor.execute(() -> {
try {
Settings settings =
settingsManager.getSettings(SETTINGS_NAMESPACE);
boolean show =
settings.getBoolean(SHOW_TRANSPORTS_ONBOARDING, true);
showTransportsOnboarding.postValue(show);
} catch (DbException e) {
logException(LOG, WARNING, e);
}
});
}
@UiThread
void transportsOnboardingShown() {
showTransportsOnboarding.setValue(false);
dbExecutor.execute(() -> {
try {
Settings settings = new Settings();
settings.putBoolean(SHOW_TRANSPORTS_ONBOARDING, false);
settingsManager.mergeSettings(settings, SETTINGS_NAMESPACE);
} catch (DbException e) {
logException(LOG, WARNING, e);
}
});
}
}
package org.briarproject.briar.android.navdrawer;
import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import org.briarproject.bramble.api.db.DatabaseExecutor;
import org.briarproject.bramble.api.db.DbException;
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.network.NetworkManager;
import org.briarproject.bramble.api.network.NetworkStatus;
import org.briarproject.bramble.api.network.event.NetworkStatusEvent;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.BluetoothConstants;
import org.briarproject.bramble.api.plugin.LanTcpConstants;
......@@ -12,28 +24,44 @@ import org.briarproject.bramble.api.plugin.PluginManager;
import org.briarproject.bramble.api.plugin.TorConstants;
import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.plugin.event.TransportStateEvent;
import org.briarproject.bramble.api.settings.Settings;
import org.briarproject.bramble.api.settings.SettingsManager;
import org.briarproject.bramble.api.settings.event.SettingsUpdatedEvent;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
import androidx.annotation.Nullable;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import static android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED;
import static android.bluetooth.BluetoothAdapter.EXTRA_STATE;
import static android.bluetooth.BluetoothAdapter.STATE_ON;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.api.plugin.Plugin.PREF_PLUGIN_ENABLE;
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
import static org.briarproject.bramble.util.LogUtils.logDuration;
import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.bramble.util.LogUtils.now;
@NotNullByDefault
public class PluginViewModel extends ViewModel implements EventListener {
public class PluginViewModel extends AndroidViewModel implements EventListener {
private static final Logger LOG =
getLogger(PluginViewModel.class.getName());
private final Application app;
private final Executor dbExecutor;
private final SettingsManager settingsManager;
private final PluginManager pluginManager;
private final EventBus eventBus;
private final BroadcastReceiver receiver;
private final MutableLiveData<State> torPluginState =
new MutableLiveData<>();
......@@ -42,24 +70,68 @@ public class PluginViewModel extends ViewModel implements EventListener {
private final MutableLiveData<State> btPluginState =
new MutableLiveData<>();
private final MutableLiveData<Boolean> torEnabledSetting =
new MutableLiveData<>(false);
private final MutableLiveData<Boolean> wifiEnabledSetting =
new MutableLiveData<>(false);
private final MutableLiveData<Boolean> btEnabledSetting =
new MutableLiveData<>(false);
private final MutableLiveData<NetworkStatus> networkStatus =
new MutableLiveData<>();
private final MutableLiveData<Boolean> bluetoothTurnedOn =
new MutableLiveData<>(false);
@Inject
PluginViewModel(PluginManager pluginManager, EventBus eventBus) {
PluginViewModel(Application app, @DatabaseExecutor Executor dbExecutor,
SettingsManager settingsManager, PluginManager pluginManager,
EventBus eventBus, NetworkManager networkManager) {
super(app);
this.app = app;
this.dbExecutor = dbExecutor;
this.settingsManager = settingsManager;
this.pluginManager = pluginManager;
this.eventBus = eventBus;
eventBus.addListener(this);
receiver = new BluetoothStateReceiver();
app.registerReceiver(receiver, new IntentFilter(ACTION_STATE_CHANGED));
networkStatus.setValue(networkManager.getNetworkStatus());
torPluginState.setValue(getTransportState(TorConstants.ID));
wifiPluginState.setValue(getTransportState(LanTcpConstants.ID));
btPluginState.setValue(getTransportState(BluetoothConstants.ID));
initialiseBluetoothState();
loadSettings();
}
@Override
protected void onCleared() {
eventBus.removeListener(this);
app.unregisterReceiver(receiver);
}
@Override
public void eventOccurred(Event e) {
if (e instanceof TransportStateEvent) {
if (e instanceof NetworkStatusEvent) {
networkStatus.setValue(((NetworkStatusEvent) e).getStatus());
} else if (e instanceof SettingsUpdatedEvent) {
SettingsUpdatedEvent s = (SettingsUpdatedEvent) e;
if (s.getNamespace().equals(TorConstants.ID.getString())) {
boolean enable = s.getSettings().getBoolean(PREF_PLUGIN_ENABLE,
TorConstants.DEFAULT_PREF_PLUGIN_ENABLE);
torEnabledSetting.setValue(enable);
} else if (s.getNamespace().equals(
LanTcpConstants.ID.getString())) {
boolean enable = s.getSettings().getBoolean(PREF_PLUGIN_ENABLE,
LanTcpConstants.DEFAULT_PREF_PLUGIN_ENABLE);
wifiEnabledSetting.setValue(enable);
} else if (s.getNamespace().equals(
BluetoothConstants.ID.getString())) {
boolean enable = s.getSettings().getBoolean(PREF_PLUGIN_ENABLE,
BluetoothConstants.DEFAULT_PREF_PLUGIN_ENABLE);
btEnabledSetting.setValue(enable);
}
} else if (e instanceof TransportStateEvent) {
TransportStateEvent t = (TransportStateEvent) e;
TransportId id = t.getTransportId();
State state = t.getState();
......@@ -77,6 +149,62 @@ public class PluginViewModel extends ViewModel implements EventListener {
return liveData;
}
LiveData<Boolean> getPluginEnabledSetting(TransportId id) {
if (id.equals(TorConstants.ID)) return torEnabledSetting;
else if (id.equals(LanTcpConstants.ID)) return wifiEnabledSetting;
else if (id.equals(BluetoothConstants.ID)) return btEnabledSetting;
else throw new IllegalArgumentException();
}
LiveData<NetworkStatus> getNetworkStatus() {
return networkStatus;
}
LiveData<Boolean> getBluetoothTurnedOn() {
return bluetoothTurnedOn;
}
int getReasonsTorDisabled() {
Plugin plugin = pluginManager.getPlugin(TorConstants.ID);
return plugin == null ? 0 : plugin.getReasonsDisabled();
}
void enableTransport(TransportId id, boolean enable) {
Settings s = new Settings();
s.putBoolean(PREF_PLUGIN_ENABLE, enable);
mergeSettings(s, id.getString());
}
private void initialiseBluetoothState() {
BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
if (bt == null) bluetoothTurnedOn.setValue(false);
else bluetoothTurnedOn.setValue(bt.getState() == STATE_ON);
}
private void loadSettings() {
dbExecutor.execute(() -> {
try {
boolean tor = isPluginEnabled(TorConstants.ID,
TorConstants.DEFAULT_PREF_PLUGIN_ENABLE);
torEnabledSetting.postValue(tor);
boolean wifi = isPluginEnabled(LanTcpConstants.ID,
LanTcpConstants.DEFAULT_PREF_PLUGIN_ENABLE);
wifiEnabledSetting.postValue(wifi);
boolean bt = isPluginEnabled(BluetoothConstants.ID,
BluetoothConstants.DEFAULT_PREF_PLUGIN_ENABLE);
btEnabledSetting.postValue(bt);
} catch (DbException e) {
logException(LOG, WARNING, e);
}
});
}
private boolean isPluginEnabled(TransportId id, boolean defaultValue)
throws DbException {
Settings s = settingsManager.getSettings(id.getString());
return s.getBoolean(PREF_PLUGIN_ENABLE, defaultValue);
}
private State getTransportState(TransportId id) {
Plugin plugin = pluginManager.getPlugin(id);
return plugin == null ? STARTING_STOPPING : plugin.getState();
......@@ -89,4 +217,26 @@ public class PluginViewModel extends ViewModel implements EventListener {
else if (id.equals(BluetoothConstants.ID)) return btPluginState;
else return null;
}
private void mergeSettings(Settings s, String namespace) {
dbExecutor.execute(() -> {
try {
long start = now();
settingsManager.mergeSettings(s, namespace);
logDuration(LOG, "Merging settings", start);
} catch (DbException e) {
logException(LOG, WARNING, e);
}
});
}
private class BluetoothStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(EXTRA_STATE, 0);
if (state == STATE_ON) bluetoothTurnedOn.postValue(true);
else bluetoothTurnedOn.postValue(false);
}
}
}
package org.briarproject.briar.android.widget;
import android.annotation.TargetApi;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import androidx.annotation.Nullable;
@NotNullByDefault
public class TouchInterceptingLinearLayout extends LinearLayout {
public TouchInterceptingLinearLayout(Context context) {
super(context);
}
public TouchInterceptingLinearLayout(Context context,
@Nullable AttributeSet attrs) {
super(context, attrs);
}
public TouchInterceptingLinearLayout(Context context,
@Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(21)
public TouchInterceptingLinearLayout(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
onTouchEvent(e);
return false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="1"
android:padding="6dp"
android:verticalSpacing="12dp"
tools:listitem="@layout/list_item_transport_card" />
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/BriarCard"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables">
<ImageView
android:id="@+id/icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
tools:background="@drawable/transport_tor"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorPrimary"
android:textSize="@dimen/text_size_large"
tools:text="@string/transport_tor" />
</LinearLayout>
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switchCompat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorPrimary"
android:textSize="@dimen/text_size_medium"
android:widgetLayout="@layout/preference_switch_compat"
tools:checked="true"
tools:text="@string/tor_enable_title" />
<TextView
android:id="@+id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:textColor="?android:attr/textColorSecondary"
android:visibility="gone"
tools:text="@string/tor_enable_summary"
tools:visibility="visible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/status_heading"
android:textColor="?android:attr/textColorPrimary" />
<TextView
android:id="@+id/deviceStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:textColor="?android:attr/textColorPrimary"
tools:text="@string/tor_device_status_online_wifi" />
<TextView
android:id="@+id/appStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:textColor="?android:attr/textColorPrimary"
tools:text="@string/tor_plugin_status_active" />
</LinearLayout>
</androidx.cardview.widget.CardView>
......@@ -26,21 +26,13 @@
app:layout_constraintTop_toTopOf="parent"
app:menu="@menu/navigation_drawer" />
<View
android:id="@+id/divider1"
style="@style/Divider.Horizontal"
android:layout_width="0dp"
app:layout_constraintEnd_toEndOf="@+id/navigation"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/navigation" />
<View
android:id="@+id/spacer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/transports"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider1"
app:layout_constraintTop_toBottomOf="@+id/navigation"