mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 20:59:54 +01:00
Compare commits
6 Commits
recently-o
...
1592-image
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fc9d4dbb66 | ||
|
|
e67e55227b | ||
|
|
7b106f952d | ||
|
|
e896b1fdd8 | ||
|
|
bb8e736804 | ||
|
|
0e5231955c |
@@ -11,8 +11,8 @@ android {
|
|||||||
defaultConfig {
|
defaultConfig {
|
||||||
minSdkVersion 16
|
minSdkVersion 16
|
||||||
targetSdkVersion 28
|
targetSdkVersion 28
|
||||||
versionCode 10207
|
versionCode 10205
|
||||||
versionName "1.2.7"
|
versionName "1.2.5"
|
||||||
consumerProguardFiles 'proguard-rules.txt'
|
consumerProguardFiles 'proguard-rules.txt'
|
||||||
|
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
@@ -38,7 +38,7 @@ configurations {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation project(path: ':bramble-core', configuration: 'default')
|
implementation project(path: ':bramble-core', configuration: 'default')
|
||||||
tor 'org.briarproject:tor-android:0.3.5.10@zip'
|
tor 'org.briarproject:tor-android:0.3.5.9@zip'
|
||||||
tor 'org.briarproject:obfs4proxy-android:0.0.11-2@zip'
|
tor 'org.briarproject:obfs4proxy-android:0.0.11-2@zip'
|
||||||
|
|
||||||
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
|
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import org.briarproject.bramble.api.identity.IdentityManager;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -21,7 +20,6 @@ import javax.annotation.concurrent.GuardedBy;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static android.os.Build.VERSION.SDK_INT;
|
import static android.os.Build.VERSION.SDK_INT;
|
||||||
import static java.util.Arrays.asList;
|
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static org.briarproject.bramble.util.IoUtils.deleteFileOrDir;
|
import static org.briarproject.bramble.util.IoUtils.deleteFileOrDir;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logFileOrDir;
|
import static org.briarproject.bramble.util.LogUtils.logFileOrDir;
|
||||||
@@ -32,12 +30,6 @@ class AndroidAccountManager extends AccountManagerImpl
|
|||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
Logger.getLogger(AndroidAccountManager.class.getName());
|
Logger.getLogger(AndroidAccountManager.class.getName());
|
||||||
|
|
||||||
/**
|
|
||||||
* Directories that shouldn't be deleted when deleting the user's account.
|
|
||||||
*/
|
|
||||||
private static final List<String> PROTECTED_DIR_NAMES =
|
|
||||||
asList("cache", "code_cache", "lib", "shared_prefs");
|
|
||||||
|
|
||||||
protected final Context appContext;
|
protected final Context appContext;
|
||||||
private final SharedPreferences prefs;
|
private final SharedPreferences prefs;
|
||||||
|
|
||||||
@@ -89,7 +81,7 @@ class AndroidAccountManager extends AccountManagerImpl
|
|||||||
if (!prefs.edit().clear().commit())
|
if (!prefs.edit().clear().commit())
|
||||||
LOG.warning("Could not clear shared preferences");
|
LOG.warning("Could not clear shared preferences");
|
||||||
}
|
}
|
||||||
// Delete files, except protected directories
|
// Delete files, except lib and shared_prefs directories
|
||||||
Set<File> files = new HashSet<>();
|
Set<File> files = new HashSet<>();
|
||||||
File dataDir = getDataDir();
|
File dataDir = getDataDir();
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -98,12 +90,14 @@ class AndroidAccountManager extends AccountManagerImpl
|
|||||||
LOG.warning("Could not list files in app data dir");
|
LOG.warning("Could not list files in app data dir");
|
||||||
} else {
|
} else {
|
||||||
for (File file : fileArray) {
|
for (File file : fileArray) {
|
||||||
if (!PROTECTED_DIR_NAMES.contains(file.getName())) {
|
String name = file.getName();
|
||||||
|
if (!name.equals("lib") && !name.equals("shared_prefs")) {
|
||||||
files.add(file);
|
files.add(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
files.add(appContext.getFilesDir());
|
files.add(appContext.getFilesDir());
|
||||||
|
files.add(appContext.getCacheDir());
|
||||||
addIfNotNull(files, appContext.getExternalCacheDir());
|
addIfNotNull(files, appContext.getExternalCacheDir());
|
||||||
if (SDK_INT >= 19) {
|
if (SDK_INT >= 19) {
|
||||||
for (File file : appContext.getExternalCacheDirs()) {
|
for (File file : appContext.getExternalCacheDirs()) {
|
||||||
@@ -115,16 +109,12 @@ class AndroidAccountManager extends AccountManagerImpl
|
|||||||
addIfNotNull(files, file);
|
addIfNotNull(files, file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Clear the cache directory but don't delete it
|
|
||||||
File cacheDir = appContext.getCacheDir();
|
|
||||||
File[] children = cacheDir.listFiles();
|
|
||||||
if (children != null) files.addAll(asList(children));
|
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Deleting " + file.getAbsolutePath());
|
|
||||||
}
|
|
||||||
deleteFileOrDir(file);
|
deleteFileOrDir(file);
|
||||||
}
|
}
|
||||||
|
// Recreate the cache dir as some OpenGL drivers expect it to exist
|
||||||
|
if (!new File(dataDir, "cache").mkdirs())
|
||||||
|
LOG.warning("Could not recreate cache dir");
|
||||||
}
|
}
|
||||||
|
|
||||||
private File getDataDir() {
|
private File getDataDir() {
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ import static android.content.Intent.ACTION_SCREEN_OFF;
|
|||||||
import static android.content.Intent.ACTION_SCREEN_ON;
|
import static android.content.Intent.ACTION_SCREEN_ON;
|
||||||
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
|
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
|
||||||
import static android.net.ConnectivityManager.TYPE_WIFI;
|
import static android.net.ConnectivityManager.TYPE_WIFI;
|
||||||
import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION;
|
|
||||||
import static android.os.Build.VERSION.SDK_INT;
|
import static android.os.Build.VERSION.SDK_INT;
|
||||||
import static android.os.PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED;
|
import static android.os.PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED;
|
||||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||||
@@ -77,9 +76,9 @@ class AndroidNetworkManager implements NetworkManager, Service {
|
|||||||
filter.addAction(ACTION_SCREEN_ON);
|
filter.addAction(ACTION_SCREEN_ON);
|
||||||
filter.addAction(ACTION_SCREEN_OFF);
|
filter.addAction(ACTION_SCREEN_OFF);
|
||||||
filter.addAction(WIFI_AP_STATE_CHANGED_ACTION);
|
filter.addAction(WIFI_AP_STATE_CHANGED_ACTION);
|
||||||
filter.addAction(WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
|
|
||||||
if (SDK_INT >= 23) filter.addAction(ACTION_DEVICE_IDLE_MODE_CHANGED);
|
if (SDK_INT >= 23) filter.addAction(ACTION_DEVICE_IDLE_MODE_CHANGED);
|
||||||
appContext.registerReceiver(networkStateReceiver, filter);
|
appContext.registerReceiver(networkStateReceiver, filter);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -137,8 +136,7 @@ class AndroidNetworkManager implements NetworkManager, Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isApEvent(@Nullable String action) {
|
private boolean isApEvent(@Nullable String action) {
|
||||||
return WIFI_AP_STATE_CHANGED_ACTION.equals(action) ||
|
return WIFI_AP_STATE_CHANGED_ACTION.equals(action);
|
||||||
WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import java.io.IOException;
|
|||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
@@ -46,10 +47,7 @@ import static android.bluetooth.BluetoothAdapter.SCAN_MODE_NONE;
|
|||||||
import static android.bluetooth.BluetoothAdapter.STATE_OFF;
|
import static android.bluetooth.BluetoothAdapter.STATE_OFF;
|
||||||
import static android.bluetooth.BluetoothAdapter.STATE_ON;
|
import static android.bluetooth.BluetoothAdapter.STATE_ON;
|
||||||
import static android.bluetooth.BluetoothDevice.ACTION_FOUND;
|
import static android.bluetooth.BluetoothDevice.ACTION_FOUND;
|
||||||
import static android.bluetooth.BluetoothDevice.DEVICE_TYPE_LE;
|
|
||||||
import static android.bluetooth.BluetoothDevice.EXTRA_DEVICE;
|
import static android.bluetooth.BluetoothDevice.EXTRA_DEVICE;
|
||||||
import static android.os.Build.VERSION.SDK_INT;
|
|
||||||
import static java.util.Collections.shuffle;
|
|
||||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
@@ -242,15 +240,11 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
|||||||
break;
|
break;
|
||||||
} else if (ACTION_FOUND.equals(action)) {
|
} else if (ACTION_FOUND.equals(action)) {
|
||||||
BluetoothDevice d = i.getParcelableExtra(EXTRA_DEVICE);
|
BluetoothDevice d = i.getParcelableExtra(EXTRA_DEVICE);
|
||||||
// Ignore Bluetooth LE devices
|
String address = d.getAddress();
|
||||||
if (SDK_INT < 18 || d.getType() != DEVICE_TYPE_LE) {
|
if (LOG.isLoggable(INFO))
|
||||||
String address = d.getAddress();
|
LOG.info("Discovered " + scrubMacAddress(address));
|
||||||
if (LOG.isLoggable(INFO))
|
if (!addresses.contains(address))
|
||||||
LOG.info("Discovered " +
|
addresses.add(address);
|
||||||
scrubMacAddress(address));
|
|
||||||
if (!addresses.contains(address))
|
|
||||||
addresses.add(address);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
now = clock.currentTimeMillis();
|
now = clock.currentTimeMillis();
|
||||||
}
|
}
|
||||||
@@ -266,7 +260,7 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
|||||||
appContext.unregisterReceiver(receiver);
|
appContext.unregisterReceiver(receiver);
|
||||||
}
|
}
|
||||||
// Shuffle the addresses so we don't always try the same one first
|
// Shuffle the addresses so we don't always try the same one first
|
||||||
shuffle(addresses);
|
Collections.shuffle(addresses);
|
||||||
return addresses;
|
return addresses;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,6 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_ADDRESS;
|
|
||||||
import static org.briarproject.bramble.util.AndroidUtils.isValidBluetoothAddress;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class AndroidBluetoothTransportConnection
|
class AndroidBluetoothTransportConnection
|
||||||
extends AbstractDuplexTransportConnection {
|
extends AbstractDuplexTransportConnection {
|
||||||
@@ -26,8 +23,6 @@ class AndroidBluetoothTransportConnection
|
|||||||
super(plugin);
|
super(plugin);
|
||||||
this.connectionManager = connectionManager;
|
this.connectionManager = connectionManager;
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
String address = socket.getRemoteDevice().getAddress();
|
|
||||||
if (isValidBluetoothAddress(address)) remote.put(PROP_ADDRESS, address);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import java.io.IOException;
|
|||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.List;
|
import java.util.Collection;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -40,6 +40,19 @@ class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
|||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(AndroidLanTcpPlugin.class.getName());
|
getLogger(AndroidLanTcpPlugin.class.getName());
|
||||||
|
|
||||||
|
private static final byte[] WIFI_AP_ADDRESS_BYTES =
|
||||||
|
{(byte) 192, (byte) 168, 43, 1};
|
||||||
|
private static final InetAddress WIFI_AP_ADDRESS;
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
WIFI_AP_ADDRESS = InetAddress.getByAddress(WIFI_AP_ADDRESS_BYTES);
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
// Should only be thrown if the address has an illegal length
|
||||||
|
throw new AssertionError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private final Executor connectionStatusExecutor;
|
private final Executor connectionStatusExecutor;
|
||||||
private final ConnectivityManager connectivityManager;
|
private final ConnectivityManager connectivityManager;
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -49,9 +62,8 @@ class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
|||||||
|
|
||||||
AndroidLanTcpPlugin(Executor ioExecutor, Context appContext,
|
AndroidLanTcpPlugin(Executor ioExecutor, Context appContext,
|
||||||
Backoff backoff, PluginCallback callback, int maxLatency,
|
Backoff backoff, PluginCallback callback, int maxLatency,
|
||||||
int maxIdleTime, int connectionTimeout) {
|
int maxIdleTime) {
|
||||||
super(ioExecutor, backoff, callback, maxLatency, maxIdleTime,
|
super(ioExecutor, backoff, callback, maxLatency, maxIdleTime);
|
||||||
connectionTimeout);
|
|
||||||
// Don't execute more than one connection status check at a time
|
// Don't execute more than one connection status check at a time
|
||||||
connectionStatusExecutor =
|
connectionStatusExecutor =
|
||||||
new PoliteExecutor("AndroidLanTcpPlugin", ioExecutor, 1);
|
new PoliteExecutor("AndroidLanTcpPlugin", ioExecutor, 1);
|
||||||
@@ -67,7 +79,6 @@ class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
|||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
if (used.getAndSet(true)) throw new IllegalStateException();
|
if (used.getAndSet(true)) throw new IllegalStateException();
|
||||||
initialisePortProperty();
|
|
||||||
running = true;
|
running = true;
|
||||||
updateConnectionStatus();
|
updateConnectionStatus();
|
||||||
}
|
}
|
||||||
@@ -84,19 +95,16 @@ class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<InetAddress> getUsableLocalInetAddresses() {
|
protected Collection<InetAddress> getLocalIpAddresses() {
|
||||||
// If the device doesn't have wifi, don't open any sockets
|
// If the device doesn't have wifi, don't open any sockets
|
||||||
if (wifiManager == null) return emptyList();
|
if (wifiManager == null) return emptyList();
|
||||||
// If we're connected to a wifi network, return its address
|
// If we're connected to a wifi network, use that network
|
||||||
WifiInfo info = wifiManager.getConnectionInfo();
|
WifiInfo info = wifiManager.getConnectionInfo();
|
||||||
if (info != null && info.getIpAddress() != 0) {
|
if (info != null && info.getIpAddress() != 0)
|
||||||
return singletonList(intToInetAddress(info.getIpAddress()));
|
return singletonList(intToInetAddress(info.getIpAddress()));
|
||||||
}
|
|
||||||
// If we're running an access point, return its address
|
// If we're running an access point, return its address
|
||||||
for (InetAddress addr : getLocalInetAddresses()) {
|
if (super.getLocalIpAddresses().contains(WIFI_AP_ADDRESS))
|
||||||
if (addr.equals(WIFI_AP_ADDRESS)) return singletonList(addr);
|
return singletonList(WIFI_AP_ADDRESS);
|
||||||
if (addr.equals(WIFI_DIRECT_AP_ADDRESS)) return singletonList(addr);
|
|
||||||
}
|
|
||||||
// No suitable addresses
|
// No suitable addresses
|
||||||
return emptyList();
|
return emptyList();
|
||||||
}
|
}
|
||||||
@@ -136,9 +144,8 @@ class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
|||||||
private void updateConnectionStatus() {
|
private void updateConnectionStatus() {
|
||||||
connectionStatusExecutor.execute(() -> {
|
connectionStatusExecutor.execute(() -> {
|
||||||
if (!running) return;
|
if (!running) return;
|
||||||
List<InetAddress> addrs = getUsableLocalInetAddresses();
|
Collection<InetAddress> addrs = getLocalIpAddresses();
|
||||||
if (addrs.contains(WIFI_AP_ADDRESS)
|
if (addrs.contains(WIFI_AP_ADDRESS)) {
|
||||||
|| addrs.contains(WIFI_DIRECT_AP_ADDRESS)) {
|
|
||||||
LOG.info("Providing wifi hotspot");
|
LOG.info("Providing wifi hotspot");
|
||||||
// There's no corresponding Network object and thus no way
|
// There's no corresponding Network object and thus no way
|
||||||
// to get a suitable socket factory, so we won't be able to
|
// to get a suitable socket factory, so we won't be able to
|
||||||
|
|||||||
@@ -21,11 +21,10 @@ import static org.briarproject.bramble.api.plugin.LanTcpConstants.ID;
|
|||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class AndroidLanTcpPluginFactory implements DuplexPluginFactory {
|
public class AndroidLanTcpPluginFactory implements DuplexPluginFactory {
|
||||||
|
|
||||||
private static final int MAX_LATENCY = 30_000; // 30 seconds
|
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
|
||||||
private static final int MAX_IDLE_TIME = 30_000; // 30 seconds
|
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
|
||||||
private static final int CONNECTION_TIMEOUT = 3_000; // 3 seconds
|
private static final int MIN_POLLING_INTERVAL = 60 * 1000; // 1 minute
|
||||||
private static final int MIN_POLLING_INTERVAL = 60_000; // 1 minute
|
private static final int MAX_POLLING_INTERVAL = 10 * 60 * 1000; // 10 mins
|
||||||
private static final int MAX_POLLING_INTERVAL = 600_000; // 10 mins
|
|
||||||
private static final double BACKOFF_BASE = 1.2;
|
private static final double BACKOFF_BASE = 1.2;
|
||||||
|
|
||||||
private final Executor ioExecutor;
|
private final Executor ioExecutor;
|
||||||
@@ -56,8 +55,7 @@ public class AndroidLanTcpPluginFactory implements DuplexPluginFactory {
|
|||||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||||
AndroidLanTcpPlugin plugin = new AndroidLanTcpPlugin(ioExecutor,
|
AndroidLanTcpPlugin plugin = new AndroidLanTcpPlugin(ioExecutor,
|
||||||
appContext, backoff, callback, MAX_LATENCY, MAX_IDLE_TIME,
|
appContext, backoff, callback, MAX_LATENCY, MAX_IDLE_TIME);
|
||||||
CONNECTION_TIMEOUT);
|
|
||||||
eventBus.addListener(plugin);
|
eventBus.addListener(plugin);
|
||||||
return plugin;
|
return plugin;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ public class AndroidUtils {
|
|||||||
return new Pair<>("", "");
|
return new Pair<>("", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isValidBluetoothAddress(@Nullable String address) {
|
private static boolean isValidBluetoothAddress(@Nullable String address) {
|
||||||
return !StringUtils.isNullOrEmpty(address)
|
return !StringUtils.isNullOrEmpty(address)
|
||||||
&& BluetoothAdapter.checkBluetoothAddress(address)
|
&& BluetoothAdapter.checkBluetoothAddress(address)
|
||||||
&& !address.equals(FAKE_BLUETOOTH_ADDRESS);
|
&& !address.equals(FAKE_BLUETOOTH_ADDRESS);
|
||||||
|
|||||||
@@ -72,9 +72,7 @@ public class AndroidAccountManagerTest extends BrambleMockTestCase {
|
|||||||
@Test
|
@Test
|
||||||
public void testDeleteAccountClearsSharedPrefsAndDeletesFiles()
|
public void testDeleteAccountClearsSharedPrefsAndDeletesFiles()
|
||||||
throws Exception {
|
throws Exception {
|
||||||
// Directories 'code_cache', 'lib' and 'shared_prefs' should be spared
|
// Directories 'lib' and 'shared_prefs' should be spared
|
||||||
File codeCacheDir = new File(testDir, "code_cache");
|
|
||||||
File codeCacheFile = new File(codeCacheDir, "file");
|
|
||||||
File libDir = new File(testDir, "lib");
|
File libDir = new File(testDir, "lib");
|
||||||
File libFile = new File(libDir, "file");
|
File libFile = new File(libDir, "file");
|
||||||
File sharedPrefsDir = new File(testDir, "shared_prefs");
|
File sharedPrefsDir = new File(testDir, "shared_prefs");
|
||||||
@@ -113,8 +111,6 @@ public class AndroidAccountManagerTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
assertTrue(dbDir.mkdirs());
|
assertTrue(dbDir.mkdirs());
|
||||||
assertTrue(keyDir.mkdirs());
|
assertTrue(keyDir.mkdirs());
|
||||||
assertTrue(codeCacheDir.mkdirs());
|
|
||||||
assertTrue(codeCacheFile.createNewFile());
|
|
||||||
assertTrue(libDir.mkdirs());
|
assertTrue(libDir.mkdirs());
|
||||||
assertTrue(libFile.createNewFile());
|
assertTrue(libFile.createNewFile());
|
||||||
assertTrue(sharedPrefsDir.mkdirs());
|
assertTrue(sharedPrefsDir.mkdirs());
|
||||||
@@ -130,8 +126,6 @@ public class AndroidAccountManagerTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
assertFalse(dbDir.exists());
|
assertFalse(dbDir.exists());
|
||||||
assertFalse(keyDir.exists());
|
assertFalse(keyDir.exists());
|
||||||
assertTrue(codeCacheDir.exists());
|
|
||||||
assertTrue(codeCacheFile.exists());
|
|
||||||
assertTrue(libDir.exists());
|
assertTrue(libDir.exists());
|
||||||
assertTrue(libFile.exists());
|
assertTrue(libFile.exists());
|
||||||
assertTrue(sharedPrefsDir.exists());
|
assertTrue(sharedPrefsDir.exists());
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ dependencyVerification {
|
|||||||
'org.bouncycastle:bcpkix-jdk15on:1.56:bcpkix-jdk15on-1.56.jar:7043dee4e9e7175e93e0b36f45b1ec1ecb893c5f755667e8b916eb8dd201c6ca',
|
'org.bouncycastle:bcpkix-jdk15on:1.56:bcpkix-jdk15on-1.56.jar:7043dee4e9e7175e93e0b36f45b1ec1ecb893c5f755667e8b916eb8dd201c6ca',
|
||||||
'org.bouncycastle:bcprov-jdk15on:1.56:bcprov-jdk15on-1.56.jar:963e1ee14f808ffb99897d848ddcdb28fa91ddda867eb18d303e82728f878349',
|
'org.bouncycastle:bcprov-jdk15on:1.56:bcprov-jdk15on-1.56.jar:963e1ee14f808ffb99897d848ddcdb28fa91ddda867eb18d303e82728f878349',
|
||||||
'org.briarproject:obfs4proxy-android:0.0.11-2:obfs4proxy-android-0.0.11-2.zip:57e55cbe87aa2aac210fdbb6cd8cdeafe15f825406a08ebf77a8b787aa2c6a8a',
|
'org.briarproject:obfs4proxy-android:0.0.11-2:obfs4proxy-android-0.0.11-2.zip:57e55cbe87aa2aac210fdbb6cd8cdeafe15f825406a08ebf77a8b787aa2c6a8a',
|
||||||
'org.briarproject:tor-android:0.3.5.10:tor-android-0.3.5.10.zip:edd83bf557fcff2105eaa0bdb3f607a6852ebe7360920929ae3039dd5f4774c5',
|
'org.briarproject:tor-android:0.3.5.9:tor-android-0.3.5.9.zip:853b0440feccd6904bd03e6b2de53a62ebcde1d58068beeadc447a7dff950bc8',
|
||||||
'org.checkerframework:checker-compat-qual:2.5.3:checker-compat-qual-2.5.3.jar:d76b9afea61c7c082908023f0cbc1427fab9abd2df915c8b8a3e7a509bccbc6d',
|
'org.checkerframework:checker-compat-qual:2.5.3:checker-compat-qual-2.5.3.jar:d76b9afea61c7c082908023f0cbc1427fab9abd2df915c8b8a3e7a509bccbc6d',
|
||||||
'org.checkerframework:checker-qual:2.5.2:checker-qual-2.5.2.jar:64b02691c8b9d4e7700f8ee2e742dce7ea2c6e81e662b7522c9ee3bf568c040a',
|
'org.checkerframework:checker-qual:2.5.2:checker-qual-2.5.2.jar:64b02691c8b9d4e7700f8ee2e742dce7ea2c6e81e662b7522c9ee3bf568c040a',
|
||||||
'org.codehaus.groovy:groovy-all:2.4.15:groovy-all-2.4.15.jar:51d6c4e71782e85674239189499854359d380fb75e1a703756e3aaa5b98a5af0',
|
'org.codehaus.groovy:groovy-all:2.4.15:groovy-all-2.4.15.jar:51d6c4e71782e85674239189499854359d380fb75e1a703756e3aaa5b98a5af0',
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.briarproject.bramble.api.account;
|
package org.briarproject.bramble.api.account;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
@@ -14,8 +13,7 @@ public interface AccountManager {
|
|||||||
* Returns true if the manager has the database key. This will be false
|
* Returns true if the manager has the database key. This will be false
|
||||||
* before {@link #createAccount(String, String)} or {@link #signIn(String)}
|
* before {@link #createAccount(String, String)} or {@link #signIn(String)}
|
||||||
* has been called, and true after {@link #createAccount(String, String)}
|
* has been called, and true after {@link #createAccount(String, String)}
|
||||||
* or {@link #signIn(String)} has returned true, until
|
* or {@link #signIn(String)} has returned true, until the process exits.
|
||||||
* {@link #deleteAccount()} is called or the process exits.
|
|
||||||
*/
|
*/
|
||||||
boolean hasDatabaseKey();
|
boolean hasDatabaseKey();
|
||||||
|
|
||||||
@@ -24,22 +22,25 @@ public interface AccountManager {
|
|||||||
* before {@link #createAccount(String, String)} or {@link #signIn(String)}
|
* before {@link #createAccount(String, String)} or {@link #signIn(String)}
|
||||||
* has been called, and non-null after
|
* has been called, and non-null after
|
||||||
* {@link #createAccount(String, String)} or {@link #signIn(String)} has
|
* {@link #createAccount(String, String)} or {@link #signIn(String)} has
|
||||||
* returned true, until {@link #deleteAccount()} is called or the process
|
* returned true, until the process exits.
|
||||||
* exits.
|
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
SecretKey getDatabaseKey();
|
SecretKey getDatabaseKey();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the encrypted database key can be loaded from disk.
|
* Returns true if the encrypted database key can be loaded from disk, and
|
||||||
|
* the database directory exists and is a directory.
|
||||||
*/
|
*/
|
||||||
boolean accountExists();
|
boolean accountExists();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an identity with the given name and registers it with the
|
* Creates an identity with the given name and registers it with the
|
||||||
* {@link IdentityManager}. Creates a database key, encrypts it with the
|
* {@link IdentityManager}. Creates a database key, encrypts it with the
|
||||||
* given password and stores it on disk. {@link #accountExists()} will
|
* given password and stores it on disk.
|
||||||
* return true after this method returns true.
|
* <p/>
|
||||||
|
* This method does not create the database directory, so
|
||||||
|
* {@link #accountExists()} will continue to return false until the
|
||||||
|
* database directory is created.
|
||||||
*/
|
*/
|
||||||
boolean createAccount(String name, String password);
|
boolean createAccount(String name, String password);
|
||||||
|
|
||||||
@@ -53,19 +54,17 @@ public interface AccountManager {
|
|||||||
* Loads the encrypted database key from disk and decrypts it with the
|
* Loads the encrypted database key from disk and decrypts it with the
|
||||||
* given password.
|
* given password.
|
||||||
*
|
*
|
||||||
* @throws DecryptionException If the database key could not be loaded and
|
* @return true if the database key was successfully loaded and decrypted.
|
||||||
* decrypted.
|
|
||||||
*/
|
*/
|
||||||
void signIn(String password) throws DecryptionException;
|
boolean signIn(String password);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the encrypted database key from disk, decrypts it with the old
|
* Loads the encrypted database key from disk, decrypts it with the old
|
||||||
* password, encrypts it with the new password, and stores it on disk,
|
* password, encrypts it with the new password, and stores it on disk,
|
||||||
* replacing the old key.
|
* replacing the old key.
|
||||||
*
|
*
|
||||||
* @throws DecryptionException If the database key could not be loaded and
|
* @return true if the database key was successfully loaded, re-encrypted
|
||||||
* decrypted.
|
* and stored.
|
||||||
*/
|
*/
|
||||||
void changePassword(String oldPassword, String newPassword)
|
boolean changePassword(String oldPassword, String newPassword);
|
||||||
throws DecryptionException;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -142,17 +142,16 @@ public interface CryptoComponent {
|
|||||||
/**
|
/**
|
||||||
* Decrypts and authenticates the given ciphertext that has been read from
|
* Decrypts and authenticates the given ciphertext that has been read from
|
||||||
* storage. The encryption and authentication keys are derived from the
|
* storage. The encryption and authentication keys are derived from the
|
||||||
* given password.
|
* given password. Returns null if the ciphertext cannot be decrypted and
|
||||||
|
* authenticated (for example, if the password is wrong).
|
||||||
*
|
*
|
||||||
* @param keyStrengthener Used to strengthen the password-based key. If
|
* @param keyStrengthener Used to strengthen the password-based key. If
|
||||||
* null, or if strengthening was not used when encrypting the ciphertext,
|
* null, or if strengthening was not used when encrypting the ciphertext,
|
||||||
* the password-based key will not be strengthened
|
* the password-based key will not be strengthened
|
||||||
* @throws DecryptionException If the ciphertext cannot be decrypted and
|
|
||||||
* authenticated (for example, if the password is wrong).
|
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
byte[] decryptWithPassword(byte[] ciphertext, String password,
|
byte[] decryptWithPassword(byte[] ciphertext, String password,
|
||||||
@Nullable KeyStrengthener keyStrengthener)
|
@Nullable KeyStrengthener keyStrengthener);
|
||||||
throws DecryptionException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the given ciphertext was encrypted using a strengthened
|
* Returns true if the given ciphertext was encrypted using a strengthened
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.crypto;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
public class DecryptionException extends Exception {
|
|
||||||
|
|
||||||
private final DecryptionResult result;
|
|
||||||
|
|
||||||
public DecryptionException(DecryptionResult result) {
|
|
||||||
this.result = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DecryptionResult getDecryptionResult() {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.crypto;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The result of a password-based decryption operation.
|
|
||||||
*/
|
|
||||||
public enum DecryptionResult {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decryption succeeded.
|
|
||||||
*/
|
|
||||||
SUCCESS,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decryption failed because the format of the ciphertext was invalid.
|
|
||||||
*/
|
|
||||||
INVALID_CIPHERTEXT,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decryption failed because the {@link KeyStrengthener} used for
|
|
||||||
* encryption was not available for decryption.
|
|
||||||
*/
|
|
||||||
KEY_STRENGTHENER_ERROR,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decryption failed because the password used for decryption did not match
|
|
||||||
* the password used for encryption.
|
|
||||||
*/
|
|
||||||
INVALID_PASSWORD
|
|
||||||
}
|
|
||||||
@@ -5,7 +5,8 @@ import org.briarproject.bramble.api.contact.PendingContactId;
|
|||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionStatusChangedEvent;
|
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionClosedEvent;
|
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionClosedEvent;
|
||||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedEvent;
|
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedEvent;
|
||||||
|
|
||||||
@@ -20,15 +21,15 @@ public interface ConnectionRegistry {
|
|||||||
/**
|
/**
|
||||||
* Registers a connection with the given contact over the given transport.
|
* Registers a connection with the given contact over the given transport.
|
||||||
* Broadcasts {@link ConnectionOpenedEvent}. Also broadcasts
|
* Broadcasts {@link ConnectionOpenedEvent}. Also broadcasts
|
||||||
* {@link ConnectionStatusChangedEvent} if this is the only connection with
|
* {@link ContactConnectedEvent} if this is the only connection with the
|
||||||
* the contact.
|
* contact.
|
||||||
*/
|
*/
|
||||||
void registerConnection(ContactId c, TransportId t, boolean incoming);
|
void registerConnection(ContactId c, TransportId t, boolean incoming);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unregisters a connection with the given contact over the given transport.
|
* Unregisters a connection with the given contact over the given transport.
|
||||||
* Broadcasts {@link ConnectionClosedEvent}. Also broadcasts
|
* Broadcasts {@link ConnectionClosedEvent}. Also broadcasts
|
||||||
* {@link ConnectionStatusChangedEvent} if this is the only connection with
|
* {@link ContactDisconnectedEvent} if this is the only connection with
|
||||||
* the contact.
|
* the contact.
|
||||||
*/
|
*/
|
||||||
void unregisterConnection(ContactId c, TransportId t, boolean incoming);
|
void unregisterConnection(ContactId c, TransportId t, boolean incoming);
|
||||||
@@ -44,9 +45,9 @@ public interface ConnectionRegistry {
|
|||||||
boolean isConnected(ContactId c, TransportId t);
|
boolean isConnected(ContactId c, TransportId t);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the connection status of the given contact via all transports.
|
* Returns true if the given contact is connected via any transport.
|
||||||
*/
|
*/
|
||||||
ConnectionStatus getConnectionStatus(ContactId c);
|
boolean isConnected(ContactId c);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a connection with the given pending contact. Broadcasts
|
* Registers a connection with the given pending contact. Broadcasts
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.plugin;
|
|
||||||
|
|
||||||
public enum ConnectionStatus {
|
|
||||||
CONNECTED, RECENTLY_CONNECTED, DISCONNECTED
|
|
||||||
}
|
|
||||||
@@ -4,10 +4,10 @@ public interface LanTcpConstants {
|
|||||||
|
|
||||||
TransportId ID = new TransportId("org.briarproject.bramble.lan");
|
TransportId ID = new TransportId("org.briarproject.bramble.lan");
|
||||||
|
|
||||||
// Transport properties (shared with contacts)
|
// a transport property (shared with contacts)
|
||||||
String PROP_IP_PORTS = "ipPorts";
|
String PROP_IP_PORTS = "ipPorts";
|
||||||
String PROP_PORT = "port";
|
|
||||||
|
|
||||||
// A local setting
|
// a local setting
|
||||||
String PREF_LAN_IP_PORTS = "ipPorts";
|
String PREF_LAN_IP_PORTS = "ipPorts";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|||||||
import org.briarproject.bramble.api.plugin.Plugin;
|
import org.briarproject.bramble.api.plugin.Plugin;
|
||||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -15,8 +14,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||||||
public abstract class AbstractDuplexTransportConnection
|
public abstract class AbstractDuplexTransportConnection
|
||||||
implements DuplexTransportConnection {
|
implements DuplexTransportConnection {
|
||||||
|
|
||||||
protected final TransportProperties remote = new TransportProperties();
|
|
||||||
|
|
||||||
private final Plugin plugin;
|
private final Plugin plugin;
|
||||||
private final Reader reader;
|
private final Reader reader;
|
||||||
private final Writer writer;
|
private final Writer writer;
|
||||||
@@ -47,11 +44,6 @@ public abstract class AbstractDuplexTransportConnection
|
|||||||
return writer;
|
return writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public TransportProperties getRemoteProperties() {
|
|
||||||
return remote;
|
|
||||||
}
|
|
||||||
|
|
||||||
private class Reader implements TransportConnectionReader {
|
private class Reader implements TransportConnectionReader {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package org.briarproject.bramble.api.plugin.duplex;
|
|||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An interface for reading and writing data over a duplex transport. The
|
* An interface for reading and writing data over a duplex transport. The
|
||||||
@@ -24,10 +23,4 @@ public interface DuplexTransportConnection {
|
|||||||
* for writing to the connection.
|
* for writing to the connection.
|
||||||
*/
|
*/
|
||||||
TransportConnectionWriter getWriter();
|
TransportConnectionWriter getWriter();
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a possibly empty set of {@link TransportProperties} describing
|
|
||||||
* the remote peer.
|
|
||||||
*/
|
|
||||||
TransportProperties getRemoteProperties();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,31 +3,24 @@ package org.briarproject.bramble.api.plugin.event;
|
|||||||
import org.briarproject.bramble.api.contact.ContactId;
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
import org.briarproject.bramble.api.event.Event;
|
import org.briarproject.bramble.api.event.Event;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An event that is broadcast when a contact's connection status changes.
|
* An event that is broadcast when a contact connects that was not previously
|
||||||
|
* connected via any transport.
|
||||||
*/
|
*/
|
||||||
@Immutable
|
@Immutable
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class ConnectionStatusChangedEvent extends Event {
|
public class ContactConnectedEvent extends Event {
|
||||||
|
|
||||||
private final ContactId contactId;
|
private final ContactId contactId;
|
||||||
private final ConnectionStatus status;
|
|
||||||
|
|
||||||
public ConnectionStatusChangedEvent(ContactId contactId,
|
public ContactConnectedEvent(ContactId contactId) {
|
||||||
ConnectionStatus status) {
|
|
||||||
this.contactId = contactId;
|
this.contactId = contactId;
|
||||||
this.status = status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ContactId getContactId() {
|
public ContactId getContactId() {
|
||||||
return contactId;
|
return contactId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConnectionStatus getConnectionStatus() {
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package org.briarproject.bramble.api.plugin.event;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
|
import org.briarproject.bramble.api.event.Event;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event that is broadcast when a contact disconnects and is no longer
|
||||||
|
* connected via any transport.
|
||||||
|
*/
|
||||||
|
@Immutable
|
||||||
|
@NotNullByDefault
|
||||||
|
public class ContactDisconnectedEvent extends Event {
|
||||||
|
|
||||||
|
private final ContactId contactId;
|
||||||
|
|
||||||
|
public ContactDisconnectedEvent(ContactId contactId) {
|
||||||
|
this.contactId = contactId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ContactId getContactId() {
|
||||||
|
return contactId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,28 +11,4 @@ public interface TransportPropertyConstants {
|
|||||||
* The maximum length of a property's key or value in UTF-8 bytes.
|
* The maximum length of a property's key or value in UTF-8 bytes.
|
||||||
*/
|
*/
|
||||||
int MAX_PROPERTY_LENGTH = 100;
|
int MAX_PROPERTY_LENGTH = 100;
|
||||||
|
|
||||||
/**
|
|
||||||
* Message metadata key for the transport ID of a local or remote update,
|
|
||||||
* as a BDF string.
|
|
||||||
*/
|
|
||||||
String MSG_KEY_TRANSPORT_ID = "transportId";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Message metadata key for the version number of a local or remote update,
|
|
||||||
* as a BDF long.
|
|
||||||
*/
|
|
||||||
String MSG_KEY_VERSION = "version";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Message metadata key for whether an update is local or remote, as a BDF
|
|
||||||
* boolean.
|
|
||||||
*/
|
|
||||||
String MSG_KEY_LOCAL = "local";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Group metadata key for any discovered transport properties of the
|
|
||||||
* contact, as a BDF dictionary.
|
|
||||||
*/
|
|
||||||
String GROUP_KEY_DISCOVERED = "discovered";
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,14 +34,6 @@ public interface TransportPropertyManager {
|
|||||||
void addRemoteProperties(Transaction txn, ContactId c,
|
void addRemoteProperties(Transaction txn, ContactId c,
|
||||||
Map<TransportId, TransportProperties> props) throws DbException;
|
Map<TransportId, TransportProperties> props) throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Stores the given properties discovered from an incoming transport
|
|
||||||
* connection. They will be overridden by any properties received while
|
|
||||||
* adding the contact or synced from the contact.
|
|
||||||
*/
|
|
||||||
void addRemotePropertiesFromConnection(ContactId c, TransportId t,
|
|
||||||
TransportProperties props) throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the local transport properties for all transports.
|
* Returns the local transport properties for all transports.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -117,10 +117,4 @@ public class IoUtils {
|
|||||||
throw new IOException(e);
|
throw new IOException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isNonEmptyDirectory(File f) {
|
|
||||||
if (!f.isDirectory()) return false;
|
|
||||||
File[] children = f.listFiles();
|
|
||||||
return children != null && children.length > 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package org.briarproject.bramble.account;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.account.AccountManager;
|
import org.briarproject.bramble.api.account.AccountManager;
|
||||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
|
||||||
import org.briarproject.bramble.api.crypto.KeyStrengthener;
|
import org.briarproject.bramble.api.crypto.KeyStrengthener;
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||||
@@ -18,7 +17,6 @@ import java.io.FileInputStream;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
@@ -26,7 +24,6 @@ import javax.annotation.concurrent.GuardedBy;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.INVALID_CIPHERTEXT;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
import static org.briarproject.bramble.util.StringUtils.fromHexString;
|
import static org.briarproject.bramble.util.StringUtils.fromHexString;
|
||||||
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||||
@@ -98,7 +95,7 @@ class AccountManagerImpl implements AccountManager {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(
|
BufferedReader reader = new BufferedReader(new InputStreamReader(
|
||||||
new FileInputStream(f), Charset.forName("UTF-8")));
|
new FileInputStream(f), "UTF-8"));
|
||||||
String key = reader.readLine();
|
String key = reader.readLine();
|
||||||
reader.close();
|
reader.close();
|
||||||
return key;
|
return key;
|
||||||
@@ -150,7 +147,7 @@ class AccountManagerImpl implements AccountManager {
|
|||||||
@GuardedBy("stateChangeLock")
|
@GuardedBy("stateChangeLock")
|
||||||
private void writeDbKeyToFile(String key, File f) throws IOException {
|
private void writeDbKeyToFile(String key, File f) throws IOException {
|
||||||
FileOutputStream out = new FileOutputStream(f);
|
FileOutputStream out = new FileOutputStream(f);
|
||||||
out.write(key.getBytes(Charset.forName("UTF-8")));
|
out.write(key.getBytes("UTF-8"));
|
||||||
out.flush();
|
out.flush();
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
@@ -158,7 +155,8 @@ class AccountManagerImpl implements AccountManager {
|
|||||||
@Override
|
@Override
|
||||||
public boolean accountExists() {
|
public boolean accountExists() {
|
||||||
synchronized (stateChangeLock) {
|
synchronized (stateChangeLock) {
|
||||||
return loadEncryptedDatabaseKey() != null;
|
return loadEncryptedDatabaseKey() != null
|
||||||
|
&& databaseConfig.getDatabaseDirectory().isDirectory();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,24 +193,31 @@ class AccountManagerImpl implements AccountManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void signIn(String password) throws DecryptionException {
|
public boolean signIn(String password) {
|
||||||
synchronized (stateChangeLock) {
|
synchronized (stateChangeLock) {
|
||||||
databaseKey = loadAndDecryptDatabaseKey(password);
|
SecretKey key = loadAndDecryptDatabaseKey(password);
|
||||||
|
if (key == null) return false;
|
||||||
|
databaseKey = key;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GuardedBy("stateChangeLock")
|
@GuardedBy("stateChangeLock")
|
||||||
private SecretKey loadAndDecryptDatabaseKey(String password)
|
@Nullable
|
||||||
throws DecryptionException {
|
private SecretKey loadAndDecryptDatabaseKey(String password) {
|
||||||
String hex = loadEncryptedDatabaseKey();
|
String hex = loadEncryptedDatabaseKey();
|
||||||
if (hex == null) {
|
if (hex == null) {
|
||||||
LOG.warning("Failed to load encrypted database key");
|
LOG.warning("Failed to load encrypted database key");
|
||||||
throw new DecryptionException(INVALID_CIPHERTEXT);
|
return null;
|
||||||
}
|
}
|
||||||
byte[] ciphertext = fromHexString(hex);
|
byte[] ciphertext = fromHexString(hex);
|
||||||
KeyStrengthener keyStrengthener = databaseConfig.getKeyStrengthener();
|
KeyStrengthener keyStrengthener = databaseConfig.getKeyStrengthener();
|
||||||
byte[] plaintext = crypto.decryptWithPassword(ciphertext, password,
|
byte[] plaintext = crypto.decryptWithPassword(ciphertext, password,
|
||||||
keyStrengthener);
|
keyStrengthener);
|
||||||
|
if (plaintext == null) {
|
||||||
|
LOG.info("Failed to decrypt database key");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
SecretKey key = new SecretKey(plaintext);
|
SecretKey key = new SecretKey(plaintext);
|
||||||
// If the DB key was encrypted with a weak key and a key strengthener
|
// If the DB key was encrypted with a weak key and a key strengthener
|
||||||
// is now available, re-encrypt the DB key with a strengthened key
|
// is now available, re-encrypt the DB key with a strengthened key
|
||||||
@@ -225,11 +230,10 @@ class AccountManagerImpl implements AccountManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void changePassword(String oldPassword, String newPassword)
|
public boolean changePassword(String oldPassword, String newPassword) {
|
||||||
throws DecryptionException {
|
|
||||||
synchronized (stateChangeLock) {
|
synchronized (stateChangeLock) {
|
||||||
SecretKey key = loadAndDecryptDatabaseKey(oldPassword);
|
SecretKey key = loadAndDecryptDatabaseKey(oldPassword);
|
||||||
encryptAndStoreDatabaseKey(key, newPassword);
|
return key != null && encryptAndStoreDatabaseKey(key, newPassword);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import net.i2p.crypto.eddsa.KeyPairGenerator;
|
|||||||
import org.briarproject.bramble.api.crypto.AgreementPrivateKey;
|
import org.briarproject.bramble.api.crypto.AgreementPrivateKey;
|
||||||
import org.briarproject.bramble.api.crypto.AgreementPublicKey;
|
import org.briarproject.bramble.api.crypto.AgreementPublicKey;
|
||||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
|
||||||
import org.briarproject.bramble.api.crypto.KeyPair;
|
import org.briarproject.bramble.api.crypto.KeyPair;
|
||||||
import org.briarproject.bramble.api.crypto.KeyParser;
|
import org.briarproject.bramble.api.crypto.KeyParser;
|
||||||
import org.briarproject.bramble.api.crypto.KeyStrengthener;
|
import org.briarproject.bramble.api.crypto.KeyStrengthener;
|
||||||
@@ -40,9 +39,6 @@ import static java.lang.System.arraycopy;
|
|||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static org.briarproject.bramble.api.crypto.CryptoConstants.KEY_TYPE_AGREEMENT;
|
import static org.briarproject.bramble.api.crypto.CryptoConstants.KEY_TYPE_AGREEMENT;
|
||||||
import static org.briarproject.bramble.api.crypto.CryptoConstants.KEY_TYPE_SIGNATURE;
|
import static org.briarproject.bramble.api.crypto.CryptoConstants.KEY_TYPE_SIGNATURE;
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.INVALID_CIPHERTEXT;
|
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.INVALID_PASSWORD;
|
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.KEY_STRENGTHENER_ERROR;
|
|
||||||
import static org.briarproject.bramble.util.ByteUtils.INT_32_BYTES;
|
import static org.briarproject.bramble.util.ByteUtils.INT_32_BYTES;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||||
import static org.briarproject.bramble.util.LogUtils.now;
|
import static org.briarproject.bramble.util.LogUtils.now;
|
||||||
@@ -363,17 +359,16 @@ class CryptoComponentImpl implements CryptoComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Nullable
|
||||||
public byte[] decryptWithPassword(byte[] input, String password,
|
public byte[] decryptWithPassword(byte[] input, String password,
|
||||||
@Nullable KeyStrengthener keyStrengthener)
|
@Nullable KeyStrengthener keyStrengthener) {
|
||||||
throws DecryptionException {
|
|
||||||
AuthenticatedCipher cipher = new XSalsa20Poly1305AuthenticatedCipher();
|
AuthenticatedCipher cipher = new XSalsa20Poly1305AuthenticatedCipher();
|
||||||
int macBytes = cipher.getMacBytes();
|
int macBytes = cipher.getMacBytes();
|
||||||
// The input contains the format version, salt, cost parameter, IV,
|
// The input contains the format version, salt, cost parameter, IV,
|
||||||
// ciphertext and MAC
|
// ciphertext and MAC
|
||||||
if (input.length < 1 + PBKDF_SALT_BYTES + INT_32_BYTES
|
if (input.length < 1 + PBKDF_SALT_BYTES + INT_32_BYTES
|
||||||
+ STORAGE_IV_BYTES + macBytes) {
|
+ STORAGE_IV_BYTES + macBytes)
|
||||||
throw new DecryptionException(INVALID_CIPHERTEXT);
|
return null; // Invalid input
|
||||||
}
|
|
||||||
int inputOff = 0;
|
int inputOff = 0;
|
||||||
// Format version
|
// Format version
|
||||||
byte formatVersion = input[inputOff];
|
byte formatVersion = input[inputOff];
|
||||||
@@ -381,7 +376,7 @@ class CryptoComponentImpl implements CryptoComponent {
|
|||||||
// Check whether we support this format version
|
// Check whether we support this format version
|
||||||
if (formatVersion != PBKDF_FORMAT_SCRYPT &&
|
if (formatVersion != PBKDF_FORMAT_SCRYPT &&
|
||||||
formatVersion != PBKDF_FORMAT_SCRYPT_STRENGTHENED) {
|
formatVersion != PBKDF_FORMAT_SCRYPT_STRENGTHENED) {
|
||||||
throw new DecryptionException(INVALID_CIPHERTEXT);
|
return null;
|
||||||
}
|
}
|
||||||
// Salt
|
// Salt
|
||||||
byte[] salt = new byte[PBKDF_SALT_BYTES];
|
byte[] salt = new byte[PBKDF_SALT_BYTES];
|
||||||
@@ -390,9 +385,8 @@ class CryptoComponentImpl implements CryptoComponent {
|
|||||||
// Cost parameter
|
// Cost parameter
|
||||||
long cost = ByteUtils.readUint32(input, inputOff);
|
long cost = ByteUtils.readUint32(input, inputOff);
|
||||||
inputOff += INT_32_BYTES;
|
inputOff += INT_32_BYTES;
|
||||||
if (cost < 2 || cost > Integer.MAX_VALUE) {
|
if (cost < 2 || cost > Integer.MAX_VALUE)
|
||||||
throw new DecryptionException(INVALID_CIPHERTEXT);
|
return null; // Invalid cost parameter
|
||||||
}
|
|
||||||
// IV
|
// IV
|
||||||
byte[] iv = new byte[STORAGE_IV_BYTES];
|
byte[] iv = new byte[STORAGE_IV_BYTES];
|
||||||
arraycopy(input, inputOff, iv, 0, iv.length);
|
arraycopy(input, inputOff, iv, 0, iv.length);
|
||||||
@@ -400,10 +394,8 @@ class CryptoComponentImpl implements CryptoComponent {
|
|||||||
// Derive the decryption key from the password
|
// Derive the decryption key from the password
|
||||||
SecretKey key = passwordBasedKdf.deriveKey(password, salt, (int) cost);
|
SecretKey key = passwordBasedKdf.deriveKey(password, salt, (int) cost);
|
||||||
if (formatVersion == PBKDF_FORMAT_SCRYPT_STRENGTHENED) {
|
if (formatVersion == PBKDF_FORMAT_SCRYPT_STRENGTHENED) {
|
||||||
if (keyStrengthener == null || !keyStrengthener.isInitialised()) {
|
if (keyStrengthener == null || !keyStrengthener.isInitialised())
|
||||||
// Can't derive the same strengthened key
|
return null; // Can't derive the same strengthened key
|
||||||
throw new DecryptionException(KEY_STRENGTHENER_ERROR);
|
|
||||||
}
|
|
||||||
key = keyStrengthener.strengthenKey(key);
|
key = keyStrengthener.strengthenKey(key);
|
||||||
}
|
}
|
||||||
// Initialise the cipher
|
// Initialise the cipher
|
||||||
@@ -419,7 +411,7 @@ class CryptoComponentImpl implements CryptoComponent {
|
|||||||
cipher.process(input, inputOff, inputLen, output, 0);
|
cipher.process(input, inputOff, inputLen, output, 0);
|
||||||
return output;
|
return output;
|
||||||
} catch (GeneralSecurityException e) {
|
} catch (GeneralSecurityException e) {
|
||||||
throw new DecryptionException(INVALID_PASSWORD);
|
return null; // Invalid ciphertext
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import static java.util.logging.Level.INFO;
|
|||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
||||||
import static org.briarproject.bramble.util.IoUtils.isNonEmptyDirectory;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.logFileOrDir;
|
import static org.briarproject.bramble.util.LogUtils.logFileOrDir;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -70,9 +69,8 @@ class H2Database extends JdbcDatabase {
|
|||||||
LOG.info("Contents of account directory before opening DB:");
|
LOG.info("Contents of account directory before opening DB:");
|
||||||
logFileOrDir(LOG, INFO, dir.getParentFile());
|
logFileOrDir(LOG, INFO, dir.getParentFile());
|
||||||
}
|
}
|
||||||
boolean reopen = isNonEmptyDirectory(dir);
|
boolean reopen = !dir.mkdirs();
|
||||||
if (LOG.isLoggable(INFO)) LOG.info("Reopening DB: " + reopen);
|
if (LOG.isLoggable(INFO)) LOG.info("Reopening DB: " + reopen);
|
||||||
if (!reopen && dir.mkdirs()) LOG.info("Created database directory");
|
|
||||||
super.open("org.h2.Driver", reopen, key, listener);
|
super.open("org.h2.Driver", reopen, key, listener);
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
LOG.info("Contents of account directory after opening DB:");
|
LOG.info("Contents of account directory after opening DB:");
|
||||||
|
|||||||
@@ -20,11 +20,9 @@ import java.util.logging.Logger;
|
|||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static java.util.logging.Level.INFO;
|
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
||||||
import static org.briarproject.bramble.util.IoUtils.isNonEmptyDirectory;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains all the HSQLDB-specific code for the database.
|
* Contains all the HSQLDB-specific code for the database.
|
||||||
@@ -66,10 +64,7 @@ class HyperSqlDatabase extends JdbcDatabase {
|
|||||||
public boolean open(SecretKey key, @Nullable MigrationListener listener)
|
public boolean open(SecretKey key, @Nullable MigrationListener listener)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
File dir = config.getDatabaseDirectory();
|
boolean reopen = !config.getDatabaseDirectory().mkdirs();
|
||||||
boolean reopen = isNonEmptyDirectory(dir);
|
|
||||||
if (LOG.isLoggable(INFO)) LOG.info("Reopening DB: " + reopen);
|
|
||||||
if (!reopen && dir.mkdirs()) LOG.info("Created database directory");
|
|
||||||
super.open("org.hsqldb.jdbc.JDBCDriver", reopen, key, listener);
|
super.open("org.hsqldb.jdbc.JDBCDriver", reopen, key, listener);
|
||||||
return reopen;
|
return reopen;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,6 @@ import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
|||||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
|
||||||
import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
|
||||||
import org.briarproject.bramble.api.sync.SyncSession;
|
import org.briarproject.bramble.api.sync.SyncSession;
|
||||||
import org.briarproject.bramble.api.sync.SyncSessionFactory;
|
import org.briarproject.bramble.api.sync.SyncSessionFactory;
|
||||||
import org.briarproject.bramble.api.transport.KeyManager;
|
import org.briarproject.bramble.api.transport.KeyManager;
|
||||||
@@ -54,7 +52,6 @@ class ConnectionManagerImpl implements ConnectionManager {
|
|||||||
private final HandshakeManager handshakeManager;
|
private final HandshakeManager handshakeManager;
|
||||||
private final ContactExchangeManager contactExchangeManager;
|
private final ContactExchangeManager contactExchangeManager;
|
||||||
private final ConnectionRegistry connectionRegistry;
|
private final ConnectionRegistry connectionRegistry;
|
||||||
private final TransportPropertyManager transportPropertyManager;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ConnectionManagerImpl(@IoExecutor Executor ioExecutor,
|
ConnectionManagerImpl(@IoExecutor Executor ioExecutor,
|
||||||
@@ -63,8 +60,7 @@ class ConnectionManagerImpl implements ConnectionManager {
|
|||||||
SyncSessionFactory syncSessionFactory,
|
SyncSessionFactory syncSessionFactory,
|
||||||
HandshakeManager handshakeManager,
|
HandshakeManager handshakeManager,
|
||||||
ContactExchangeManager contactExchangeManager,
|
ContactExchangeManager contactExchangeManager,
|
||||||
ConnectionRegistry connectionRegistry,
|
ConnectionRegistry connectionRegistry) {
|
||||||
TransportPropertyManager transportPropertyManager) {
|
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
this.keyManager = keyManager;
|
this.keyManager = keyManager;
|
||||||
this.streamReaderFactory = streamReaderFactory;
|
this.streamReaderFactory = streamReaderFactory;
|
||||||
@@ -73,7 +69,6 @@ class ConnectionManagerImpl implements ConnectionManager {
|
|||||||
this.handshakeManager = handshakeManager;
|
this.handshakeManager = handshakeManager;
|
||||||
this.contactExchangeManager = contactExchangeManager;
|
this.contactExchangeManager = contactExchangeManager;
|
||||||
this.connectionRegistry = connectionRegistry;
|
this.connectionRegistry = connectionRegistry;
|
||||||
this.transportPropertyManager = transportPropertyManager;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -274,7 +269,6 @@ class ConnectionManagerImpl implements ConnectionManager {
|
|||||||
private final TransportId transportId;
|
private final TransportId transportId;
|
||||||
private final TransportConnectionReader reader;
|
private final TransportConnectionReader reader;
|
||||||
private final TransportConnectionWriter writer;
|
private final TransportConnectionWriter writer;
|
||||||
private final TransportProperties remote;
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private volatile SyncSession outgoingSession = null;
|
private volatile SyncSession outgoingSession = null;
|
||||||
@@ -284,7 +278,6 @@ class ConnectionManagerImpl implements ConnectionManager {
|
|||||||
this.transportId = transportId;
|
this.transportId = transportId;
|
||||||
reader = connection.getReader();
|
reader = connection.getReader();
|
||||||
writer = connection.getWriter();
|
writer = connection.getWriter();
|
||||||
remote = connection.getRemoteProperties();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -320,16 +313,13 @@ class ConnectionManagerImpl implements ConnectionManager {
|
|||||||
// Start the outgoing session on another thread
|
// Start the outgoing session on another thread
|
||||||
ioExecutor.execute(() -> runOutgoingSession(contactId));
|
ioExecutor.execute(() -> runOutgoingSession(contactId));
|
||||||
try {
|
try {
|
||||||
// Store any transport properties discovered from the connection
|
|
||||||
transportPropertyManager.addRemotePropertiesFromConnection(
|
|
||||||
contactId, transportId, remote);
|
|
||||||
// Create and run the incoming session
|
// Create and run the incoming session
|
||||||
createIncomingSession(ctx, reader).run();
|
createIncomingSession(ctx, reader).run();
|
||||||
reader.dispose(false, true);
|
reader.dispose(false, true);
|
||||||
// Interrupt the outgoing session so it finishes cleanly
|
// Interrupt the outgoing session so it finishes cleanly
|
||||||
SyncSession out = outgoingSession;
|
SyncSession out = outgoingSession;
|
||||||
if (out != null) out.interrupt();
|
if (out != null) out.interrupt();
|
||||||
} catch (DbException | IOException e) {
|
} catch (IOException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
onReadError(true);
|
onReadError(true);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -385,7 +375,6 @@ class ConnectionManagerImpl implements ConnectionManager {
|
|||||||
private final TransportId transportId;
|
private final TransportId transportId;
|
||||||
private final TransportConnectionReader reader;
|
private final TransportConnectionReader reader;
|
||||||
private final TransportConnectionWriter writer;
|
private final TransportConnectionWriter writer;
|
||||||
private final TransportProperties remote;
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private volatile SyncSession outgoingSession = null;
|
private volatile SyncSession outgoingSession = null;
|
||||||
@@ -396,7 +385,6 @@ class ConnectionManagerImpl implements ConnectionManager {
|
|||||||
this.transportId = transportId;
|
this.transportId = transportId;
|
||||||
reader = connection.getReader();
|
reader = connection.getReader();
|
||||||
writer = connection.getWriter();
|
writer = connection.getWriter();
|
||||||
remote = connection.getRemoteProperties();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -473,16 +461,13 @@ class ConnectionManagerImpl implements ConnectionManager {
|
|||||||
connectionRegistry.registerConnection(contactId, transportId,
|
connectionRegistry.registerConnection(contactId, transportId,
|
||||||
false);
|
false);
|
||||||
try {
|
try {
|
||||||
// Store any transport properties discovered from the connection
|
|
||||||
transportPropertyManager.addRemotePropertiesFromConnection(
|
|
||||||
contactId, transportId, remote);
|
|
||||||
// Create and run the incoming session
|
// Create and run the incoming session
|
||||||
createIncomingSession(ctx, reader).run();
|
createIncomingSession(ctx, reader).run();
|
||||||
reader.dispose(false, true);
|
reader.dispose(false, true);
|
||||||
// Interrupt the outgoing session so it finishes cleanly
|
// Interrupt the outgoing session so it finishes cleanly
|
||||||
SyncSession out = outgoingSession;
|
SyncSession out = outgoingSession;
|
||||||
if (out != null) out.interrupt();
|
if (out != null) out.interrupt();
|
||||||
} catch (DbException | IOException e) {
|
} catch (IOException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
onReadError();
|
onReadError();
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -6,41 +6,30 @@ import org.briarproject.bramble.api.contact.PendingContactId;
|
|||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
|
||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionStatusChangedEvent;
|
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionClosedEvent;
|
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionClosedEvent;
|
||||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedEvent;
|
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedEvent;
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
|
||||||
import org.briarproject.bramble.api.system.Scheduler;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.concurrent.GuardedBy;
|
import javax.annotation.concurrent.GuardedBy;
|
||||||
import javax.annotation.concurrent.ThreadSafe;
|
import javax.annotation.concurrent.ThreadSafe;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static java.util.Collections.emptyList;
|
|
||||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
|
||||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
|
||||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.CONNECTED;
|
|
||||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.DISCONNECTED;
|
|
||||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.RECENTLY_CONNECTED;
|
|
||||||
|
|
||||||
@ThreadSafe
|
@ThreadSafe
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
@@ -49,30 +38,22 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
|||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(ConnectionRegistryImpl.class.getName());
|
getLogger(ConnectionRegistryImpl.class.getName());
|
||||||
|
|
||||||
private static final long RECENTLY_CONNECTED_MS = MINUTES.toMillis(1);
|
|
||||||
private static final long EXPIRY_INTERVAL_MS = SECONDS.toMillis(10);
|
|
||||||
|
|
||||||
private final EventBus eventBus;
|
private final EventBus eventBus;
|
||||||
private final Clock clock;
|
|
||||||
|
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
@GuardedBy("lock")
|
@GuardedBy("lock")
|
||||||
private final Map<TransportId, Multiset<ContactId>> contactConnections;
|
private final Map<TransportId, Multiset<ContactId>> contactConnections;
|
||||||
@GuardedBy("lock")
|
@GuardedBy("lock")
|
||||||
private final Map<ContactId, Counter> contactCounts;
|
private final Multiset<ContactId> contactCounts;
|
||||||
@GuardedBy("lock")
|
@GuardedBy("lock")
|
||||||
private final Set<PendingContactId> connectedPendingContacts;
|
private final Set<PendingContactId> connectedPendingContacts;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ConnectionRegistryImpl(EventBus eventBus, Clock clock,
|
ConnectionRegistryImpl(EventBus eventBus) {
|
||||||
@Scheduler ScheduledExecutorService scheduler) {
|
|
||||||
this.eventBus = eventBus;
|
this.eventBus = eventBus;
|
||||||
this.clock = clock;
|
|
||||||
contactConnections = new HashMap<>();
|
contactConnections = new HashMap<>();
|
||||||
contactCounts = new HashMap<>();
|
contactCounts = new Multiset<>();
|
||||||
connectedPendingContacts = new HashSet<>();
|
connectedPendingContacts = new HashSet<>();
|
||||||
scheduler.scheduleWithFixedDelay(this::expireRecentConnections,
|
|
||||||
EXPIRY_INTERVAL_MS, EXPIRY_INTERVAL_MS, MILLISECONDS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -90,22 +71,12 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
|||||||
contactConnections.put(t, m);
|
contactConnections.put(t, m);
|
||||||
}
|
}
|
||||||
m.add(c);
|
m.add(c);
|
||||||
|
if (contactCounts.add(c) == 1) firstConnection = true;
|
||||||
Counter counter = contactCounts.get(c);
|
|
||||||
if (counter == null) {
|
|
||||||
counter = new Counter();
|
|
||||||
contactCounts.put(c, counter);
|
|
||||||
}
|
|
||||||
if (counter.connections == 0) {
|
|
||||||
counter.disconnectedTime = 0;
|
|
||||||
firstConnection = true;
|
|
||||||
}
|
|
||||||
counter.connections++;
|
|
||||||
}
|
}
|
||||||
eventBus.broadcast(new ConnectionOpenedEvent(c, t, incoming));
|
eventBus.broadcast(new ConnectionOpenedEvent(c, t, incoming));
|
||||||
if (firstConnection) {
|
if (firstConnection) {
|
||||||
LOG.info("Contact connected");
|
LOG.info("Contact connected");
|
||||||
eventBus.broadcast(new ConnectionStatusChangedEvent(c, CONNECTED));
|
eventBus.broadcast(new ContactConnectedEvent(c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,22 +93,12 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
|||||||
if (m == null || !m.contains(c))
|
if (m == null || !m.contains(c))
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
m.remove(c);
|
m.remove(c);
|
||||||
|
if (contactCounts.remove(c) == 0) lastConnection = true;
|
||||||
Counter counter = contactCounts.get(c);
|
|
||||||
if (counter == null || counter.connections == 0) {
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
}
|
|
||||||
counter.connections--;
|
|
||||||
if (counter.connections == 0) {
|
|
||||||
counter.disconnectedTime = clock.currentTimeMillis();
|
|
||||||
lastConnection = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
eventBus.broadcast(new ConnectionClosedEvent(c, t, incoming));
|
eventBus.broadcast(new ConnectionClosedEvent(c, t, incoming));
|
||||||
if (lastConnection) {
|
if (lastConnection) {
|
||||||
LOG.info("Contact disconnected");
|
LOG.info("Contact disconnected");
|
||||||
eventBus.broadcast(
|
eventBus.broadcast(new ContactDisconnectedEvent(c));
|
||||||
new ConnectionStatusChangedEvent(c, RECENTLY_CONNECTED));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +106,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
|||||||
public Collection<ContactId> getConnectedContacts(TransportId t) {
|
public Collection<ContactId> getConnectedContacts(TransportId t) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
Multiset<ContactId> m = contactConnections.get(t);
|
Multiset<ContactId> m = contactConnections.get(t);
|
||||||
if (m == null) return emptyList();
|
if (m == null) return Collections.emptyList();
|
||||||
List<ContactId> ids = new ArrayList<>(m.keySet());
|
List<ContactId> ids = new ArrayList<>(m.keySet());
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info(ids.size() + " contacts connected: " + t);
|
LOG.info(ids.size() + " contacts connected: " + t);
|
||||||
@@ -162,11 +123,9 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConnectionStatus getConnectionStatus(ContactId c) {
|
public boolean isConnected(ContactId c) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
Counter counter = contactCounts.get(c);
|
return contactCounts.contains(c);
|
||||||
if (counter == null) return DISCONNECTED;
|
|
||||||
return counter.connections > 0 ? CONNECTED : RECENTLY_CONNECTED;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,36 +147,4 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
|||||||
}
|
}
|
||||||
eventBus.broadcast(new RendezvousConnectionClosedEvent(p, success));
|
eventBus.broadcast(new RendezvousConnectionClosedEvent(p, success));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Scheduler
|
|
||||||
private void expireRecentConnections() {
|
|
||||||
long now = clock.currentTimeMillis();
|
|
||||||
List<ContactId> disconnected = new ArrayList<>();
|
|
||||||
synchronized (lock) {
|
|
||||||
Iterator<Entry<ContactId, Counter>> it =
|
|
||||||
contactCounts.entrySet().iterator();
|
|
||||||
while (it.hasNext()) {
|
|
||||||
Entry<ContactId, Counter> e = it.next();
|
|
||||||
if (e.getValue().isExpired(now)) {
|
|
||||||
disconnected.add(e.getKey());
|
|
||||||
it.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (ContactId c : disconnected) {
|
|
||||||
eventBus.broadcast(
|
|
||||||
new ConnectionStatusChangedEvent(c, DISCONNECTED));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class Counter {
|
|
||||||
|
|
||||||
private int connections = 0;
|
|
||||||
private long disconnectedTime = 0;
|
|
||||||
|
|
||||||
private boolean isExpired(long now) {
|
|
||||||
return connections == 0 &&
|
|
||||||
now - disconnectedTime > RECENTLY_CONNECTED_MS;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,20 +17,18 @@ import java.io.IOException;
|
|||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.InterfaceAddress;
|
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.net.SocketAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
|
|
||||||
import static java.lang.Integer.parseInt;
|
|
||||||
import static java.util.Collections.addAll;
|
import static java.util.Collections.addAll;
|
||||||
|
import static java.util.Collections.emptyList;
|
||||||
import static java.util.Collections.sort;
|
import static java.util.Collections.sort;
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
@@ -39,7 +37,6 @@ import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.TR
|
|||||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.ID;
|
import static org.briarproject.bramble.api.plugin.LanTcpConstants.ID;
|
||||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PREF_LAN_IP_PORTS;
|
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PREF_LAN_IP_PORTS;
|
||||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PROP_IP_PORTS;
|
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PROP_IP_PORTS;
|
||||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PROP_PORT;
|
|
||||||
import static org.briarproject.bramble.util.ByteUtils.MAX_16_BIT_UNSIGNED;
|
import static org.briarproject.bramble.util.ByteUtils.MAX_16_BIT_UNSIGNED;
|
||||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubSocketAddress;
|
import static org.briarproject.bramble.util.PrivacyUtils.scrubSocketAddress;
|
||||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||||
@@ -50,36 +47,15 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
|
|
||||||
private static final Logger LOG = getLogger(LanTcpPlugin.class.getName());
|
private static final Logger LOG = getLogger(LanTcpPlugin.class.getName());
|
||||||
|
|
||||||
|
private static final LanAddressComparator ADDRESS_COMPARATOR =
|
||||||
|
new LanAddressComparator();
|
||||||
|
|
||||||
private static final int MAX_ADDRESSES = 4;
|
private static final int MAX_ADDRESSES = 4;
|
||||||
private static final String SEPARATOR = ",";
|
private static final String SEPARATOR = ",";
|
||||||
|
|
||||||
/**
|
|
||||||
* The IP address of an Android device providing a wifi access point.
|
|
||||||
*/
|
|
||||||
protected static final InetAddress WIFI_AP_ADDRESS;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The IP address of an Android device providing a wifi direct
|
|
||||||
* legacy mode access point.
|
|
||||||
*/
|
|
||||||
protected static final InetAddress WIFI_DIRECT_AP_ADDRESS;
|
|
||||||
|
|
||||||
static {
|
|
||||||
try {
|
|
||||||
WIFI_AP_ADDRESS = InetAddress.getByAddress(
|
|
||||||
new byte[] {(byte) 192, (byte) 168, 43, 1});
|
|
||||||
WIFI_DIRECT_AP_ADDRESS = InetAddress.getByAddress(
|
|
||||||
new byte[] {(byte) 192, (byte) 168, 49, 1});
|
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
// Should only be thrown if the address has an illegal length
|
|
||||||
throw new AssertionError(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LanTcpPlugin(Executor ioExecutor, Backoff backoff, PluginCallback callback,
|
LanTcpPlugin(Executor ioExecutor, Backoff backoff, PluginCallback callback,
|
||||||
int maxLatency, int maxIdleTime, int connectionTimeout) {
|
int maxLatency, int maxIdleTime) {
|
||||||
super(ioExecutor, backoff, callback, maxLatency, maxIdleTime,
|
super(ioExecutor, backoff, callback, maxLatency, maxIdleTime);
|
||||||
connectionTimeout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -87,82 +63,38 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
return ID;
|
return ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void start() {
|
|
||||||
if (used.getAndSet(true)) throw new IllegalStateException();
|
|
||||||
initialisePortProperty();
|
|
||||||
running = true;
|
|
||||||
bind();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void initialisePortProperty() {
|
|
||||||
TransportProperties p = callback.getLocalProperties();
|
|
||||||
if (isNullOrEmpty(p.get(PROP_PORT))) {
|
|
||||||
int port = new Random().nextInt(32768) + 32768;
|
|
||||||
p.put(PROP_PORT, String.valueOf(port));
|
|
||||||
callback.mergeLocalProperties(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<InetSocketAddress> getLocalSocketAddresses() {
|
protected List<InetSocketAddress> getLocalSocketAddresses() {
|
||||||
|
// Use the same address and port as last time if available
|
||||||
TransportProperties p = callback.getLocalProperties();
|
TransportProperties p = callback.getLocalProperties();
|
||||||
int preferredPort = parsePortProperty(p.get(PROP_PORT));
|
|
||||||
String oldIpPorts = p.get(PROP_IP_PORTS);
|
String oldIpPorts = p.get(PROP_IP_PORTS);
|
||||||
List<InetSocketAddress> olds = parseSocketAddresses(oldIpPorts);
|
List<InetSocketAddress> olds = parseSocketAddresses(oldIpPorts);
|
||||||
|
|
||||||
List<InetSocketAddress> locals = new ArrayList<>();
|
List<InetSocketAddress> locals = new ArrayList<>();
|
||||||
List<InetSocketAddress> fallbacks = new ArrayList<>();
|
for (InetAddress local : getLocalIpAddresses()) {
|
||||||
for (InetAddress local : getUsableLocalInetAddresses()) {
|
if (isAcceptableAddress(local)) {
|
||||||
// If we've used this address before, try to use the same port
|
// If this is the old address, try to use the same port
|
||||||
int port = preferredPort;
|
for (InetSocketAddress old : olds) {
|
||||||
for (InetSocketAddress old : olds) {
|
if (old.getAddress().equals(local))
|
||||||
if (old.getAddress().equals(local)) {
|
locals.add(new InetSocketAddress(local, old.getPort()));
|
||||||
port = old.getPort();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
locals.add(new InetSocketAddress(local, 0));
|
||||||
}
|
}
|
||||||
locals.add(new InetSocketAddress(local, port));
|
|
||||||
// Fall back to any available port
|
|
||||||
fallbacks.add(new InetSocketAddress(local, 0));
|
|
||||||
}
|
}
|
||||||
locals.addAll(fallbacks);
|
sort(locals, ADDRESS_COMPARATOR);
|
||||||
return locals;
|
return locals;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int parsePortProperty(@Nullable String portProperty) {
|
|
||||||
if (isNullOrEmpty(portProperty)) return 0;
|
|
||||||
try {
|
|
||||||
return parseInt(portProperty);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<InetSocketAddress> parseSocketAddresses(String ipPorts) {
|
private List<InetSocketAddress> parseSocketAddresses(String ipPorts) {
|
||||||
|
if (isNullOrEmpty(ipPorts)) return emptyList();
|
||||||
|
String[] split = ipPorts.split(SEPARATOR);
|
||||||
List<InetSocketAddress> addresses = new ArrayList<>();
|
List<InetSocketAddress> addresses = new ArrayList<>();
|
||||||
if (isNullOrEmpty(ipPorts)) return addresses;
|
for (String ipPort : split) {
|
||||||
for (String ipPort : ipPorts.split(SEPARATOR)) {
|
|
||||||
InetSocketAddress a = parseSocketAddress(ipPort);
|
InetSocketAddress a = parseSocketAddress(ipPort);
|
||||||
if (a != null) addresses.add(a);
|
if (a != null) addresses.add(a);
|
||||||
}
|
}
|
||||||
return addresses;
|
return addresses;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected List<InetAddress> getUsableLocalInetAddresses() {
|
|
||||||
List<InterfaceAddress> ifAddrs =
|
|
||||||
new ArrayList<>(getLocalInterfaceAddresses());
|
|
||||||
// Prefer longer network prefixes
|
|
||||||
sort(ifAddrs, (a, b) ->
|
|
||||||
b.getNetworkPrefixLength() - a.getNetworkPrefixLength());
|
|
||||||
List<InetAddress> addrs = new ArrayList<>();
|
|
||||||
for (InterfaceAddress ifAddr : ifAddrs) {
|
|
||||||
InetAddress addr = ifAddr.getAddress();
|
|
||||||
if (isAcceptableAddress(addr)) addrs.add(addr);
|
|
||||||
}
|
|
||||||
return addrs;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setLocalSocketAddress(InetSocketAddress a) {
|
protected void setLocalSocketAddress(InetSocketAddress a) {
|
||||||
String ipPort = getIpPortString(a);
|
String ipPort = getIpPortString(a);
|
||||||
@@ -200,20 +132,7 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
@Override
|
@Override
|
||||||
protected List<InetSocketAddress> getRemoteSocketAddresses(
|
protected List<InetSocketAddress> getRemoteSocketAddresses(
|
||||||
TransportProperties p) {
|
TransportProperties p) {
|
||||||
String ipPorts = p.get(PROP_IP_PORTS);
|
return parseSocketAddresses(p.get(PROP_IP_PORTS));
|
||||||
List<InetSocketAddress> remotes = parseSocketAddresses(ipPorts);
|
|
||||||
int port = parsePortProperty(p.get(PROP_PORT));
|
|
||||||
// If the contact has a preferred port, we can guess their IP:port when
|
|
||||||
// they're providing a wifi access point
|
|
||||||
if (port != 0) {
|
|
||||||
InetSocketAddress wifiAp =
|
|
||||||
new InetSocketAddress(WIFI_AP_ADDRESS, port);
|
|
||||||
if (!remotes.contains(wifiAp)) remotes.add(wifiAp);
|
|
||||||
InetSocketAddress wifiDirectAp =
|
|
||||||
new InetSocketAddress(WIFI_DIRECT_AP_ADDRESS, port);
|
|
||||||
if (!remotes.contains(wifiDirectAp)) remotes.add(wifiDirectAp);
|
|
||||||
}
|
|
||||||
return remotes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isAcceptableAddress(InetAddress a) {
|
private boolean isAcceptableAddress(InetAddress a) {
|
||||||
@@ -226,33 +145,52 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isConnectable(InterfaceAddress local,
|
protected boolean isConnectable(InetSocketAddress remote) {
|
||||||
InetSocketAddress remote) {
|
|
||||||
if (remote.getPort() == 0) return false;
|
if (remote.getPort() == 0) return false;
|
||||||
if (!isAcceptableAddress(remote.getAddress())) return false;
|
if (!isAcceptableAddress(remote.getAddress())) return false;
|
||||||
// Try to determine whether the address is on the same LAN as us
|
// Try to determine whether the address is on the same LAN as us
|
||||||
byte[] localIp = local.getAddress().getAddress();
|
if (socket == null) return false;
|
||||||
|
byte[] localIp = socket.getInetAddress().getAddress();
|
||||||
byte[] remoteIp = remote.getAddress().getAddress();
|
byte[] remoteIp = remote.getAddress().getAddress();
|
||||||
int prefixLength = local.getNetworkPrefixLength();
|
return addressesAreOnSameLan(localIp, remoteIp);
|
||||||
return areAddressesInSameNetwork(localIp, remoteIp, prefixLength);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Package access for testing
|
// Package access for testing
|
||||||
static boolean areAddressesInSameNetwork(byte[] localIp, byte[] remoteIp,
|
boolean addressesAreOnSameLan(byte[] localIp, byte[] remoteIp) {
|
||||||
int prefixLength) {
|
// 10.0.0.0/8
|
||||||
if (localIp.length != remoteIp.length) return false;
|
if (isPrefix10(localIp)) return isPrefix10(remoteIp);
|
||||||
// Compare the first prefixLength bits of the addresses
|
// 172.16.0.0/12
|
||||||
for (int i = 0; i < prefixLength; i++) {
|
if (isPrefix172(localIp)) return isPrefix172(remoteIp);
|
||||||
int byteIndex = i >> 3;
|
// 192.168.0.0/16
|
||||||
int bitIndex = i & 7; // 0 to 7
|
if (isPrefix192(localIp)) return isPrefix192(remoteIp);
|
||||||
int mask = 128 >> bitIndex; // Select the bit at bitIndex
|
// Unrecognised prefix - may be compatible
|
||||||
if ((localIp[byteIndex] & mask) != (remoteIp[byteIndex] & mask)) {
|
|
||||||
return false; // Addresses differ at bit i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isPrefix10(byte[] ipv4) {
|
||||||
|
return ipv4[0] == 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isPrefix172(byte[] ipv4) {
|
||||||
|
return ipv4[0] == (byte) 172 && (ipv4[1] & 0xF0) == 16;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isPrefix192(byte[] ipv4) {
|
||||||
|
return ipv4[0] == (byte) 192 && ipv4[1] == (byte) 168;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the prefix length for an RFC 1918 address, or 0 for any other
|
||||||
|
// address
|
||||||
|
private static int getRfc1918PrefixLength(InetAddress addr) {
|
||||||
|
if (!(addr instanceof Inet4Address)) return 0;
|
||||||
|
if (!addr.isSiteLocalAddress()) return 0;
|
||||||
|
byte[] ipv4 = addr.getAddress();
|
||||||
|
if (isPrefix10(ipv4)) return 8;
|
||||||
|
if (isPrefix172(ipv4)) return 12;
|
||||||
|
if (isPrefix192(ipv4)) return 16;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supportsKeyAgreement() {
|
public boolean supportsKeyAgreement() {
|
||||||
return true;
|
return true;
|
||||||
@@ -291,12 +229,6 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
public DuplexTransportConnection createKeyAgreementConnection(
|
public DuplexTransportConnection createKeyAgreementConnection(
|
||||||
byte[] commitment, BdfList descriptor) {
|
byte[] commitment, BdfList descriptor) {
|
||||||
if (!isRunning()) return null;
|
if (!isRunning()) return null;
|
||||||
ServerSocket ss = socket;
|
|
||||||
InterfaceAddress local = getLocalInterfaceAddress(ss.getInetAddress());
|
|
||||||
if (local == null) {
|
|
||||||
LOG.warning("No interface for key agreement server socket");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
InetSocketAddress remote;
|
InetSocketAddress remote;
|
||||||
try {
|
try {
|
||||||
remote = parseSocketAddress(descriptor);
|
remote = parseSocketAddress(descriptor);
|
||||||
@@ -304,11 +236,12 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
LOG.info("Invalid IP/port in key agreement descriptor");
|
LOG.info("Invalid IP/port in key agreement descriptor");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (!isConnectable(local, remote)) {
|
if (!isConnectable(remote)) {
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
|
SocketAddress local = socket.getLocalSocketAddress();
|
||||||
LOG.info(scrubSocketAddress(remote) +
|
LOG.info(scrubSocketAddress(remote) +
|
||||||
" is not connectable from " +
|
" is not connectable from " +
|
||||||
scrubSocketAddress(ss.getLocalSocketAddress()));
|
scrubSocketAddress(local));
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -316,8 +249,8 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Connecting to " + scrubSocketAddress(remote));
|
LOG.info("Connecting to " + scrubSocketAddress(remote));
|
||||||
Socket s = createSocket();
|
Socket s = createSocket();
|
||||||
s.bind(new InetSocketAddress(ss.getInetAddress(), 0));
|
s.bind(new InetSocketAddress(socket.getInetAddress(), 0));
|
||||||
s.connect(remote, connectionTimeout);
|
s.connect(remote);
|
||||||
s.setSoTimeout(socketTimeout);
|
s.setSoTimeout(socketTimeout);
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Connected to " + scrubSocketAddress(remote));
|
LOG.info("Connected to " + scrubSocketAddress(remote));
|
||||||
@@ -366,4 +299,19 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
IoUtils.tryToClose(ss, LOG, WARNING);
|
IoUtils.tryToClose(ss, LOG, WARNING);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class LanAddressComparator implements Comparator<InetSocketAddress> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compare(InetSocketAddress a, InetSocketAddress b) {
|
||||||
|
// Prefer addresses with non-zero ports
|
||||||
|
int aPort = a.getPort(), bPort = b.getPort();
|
||||||
|
if (aPort > 0 && bPort == 0) return -1;
|
||||||
|
if (aPort == 0 && bPort > 0) return 1;
|
||||||
|
// Prefer addresses with longer RFC 1918 prefixes
|
||||||
|
int aPrefix = getRfc1918PrefixLength(a.getAddress());
|
||||||
|
int bPrefix = getRfc1918PrefixLength(b.getAddress());
|
||||||
|
return bPrefix - aPrefix;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,11 +18,10 @@ import static org.briarproject.bramble.api.plugin.LanTcpConstants.ID;
|
|||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class LanTcpPluginFactory implements DuplexPluginFactory {
|
public class LanTcpPluginFactory implements DuplexPluginFactory {
|
||||||
|
|
||||||
private static final int MAX_LATENCY = 30_000; // 30 seconds
|
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
|
||||||
private static final int MAX_IDLE_TIME = 30_000; // 30 seconds
|
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
|
||||||
private static final int CONNECTION_TIMEOUT = 3_000; // 3 seconds
|
private static final int MIN_POLLING_INTERVAL = 60 * 1000; // 1 minute
|
||||||
private static final int MIN_POLLING_INTERVAL = 60_000; // 1 minute
|
private static final int MAX_POLLING_INTERVAL = 10 * 60 * 1000; // 10 mins
|
||||||
private static final int MAX_POLLING_INTERVAL = 600_000; // 10 mins
|
|
||||||
private static final double BACKOFF_BASE = 1.2;
|
private static final double BACKOFF_BASE = 1.2;
|
||||||
|
|
||||||
private final Executor ioExecutor;
|
private final Executor ioExecutor;
|
||||||
@@ -49,6 +48,6 @@ public class LanTcpPluginFactory implements DuplexPluginFactory {
|
|||||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||||
return new LanTcpPlugin(ioExecutor, backoff, callback, MAX_LATENCY,
|
return new LanTcpPlugin(ioExecutor, backoff, callback, MAX_LATENCY,
|
||||||
MAX_IDLE_TIME, CONNECTION_TIMEOUT);
|
MAX_IDLE_TIME);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,10 +19,10 @@ import org.briarproject.bramble.util.IoUtils;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.InterfaceAddress;
|
|
||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.net.SocketAddress;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -36,6 +36,7 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import static java.net.NetworkInterface.getNetworkInterfaces;
|
||||||
import static java.util.Collections.emptyList;
|
import static java.util.Collections.emptyList;
|
||||||
import static java.util.Collections.list;
|
import static java.util.Collections.list;
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
@@ -57,8 +58,7 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
protected final Executor ioExecutor, bindExecutor;
|
protected final Executor ioExecutor, bindExecutor;
|
||||||
protected final Backoff backoff;
|
protected final Backoff backoff;
|
||||||
protected final PluginCallback callback;
|
protected final PluginCallback callback;
|
||||||
protected final int maxLatency, maxIdleTime;
|
protected final int maxLatency, maxIdleTime, socketTimeout;
|
||||||
protected final int connectionTimeout, socketTimeout;
|
|
||||||
protected final AtomicBoolean used = new AtomicBoolean(false);
|
protected final AtomicBoolean used = new AtomicBoolean(false);
|
||||||
|
|
||||||
protected volatile boolean running = false;
|
protected volatile boolean running = false;
|
||||||
@@ -86,18 +86,15 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
/**
|
/**
|
||||||
* Returns true if connections to the given address can be attempted.
|
* Returns true if connections to the given address can be attempted.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
protected abstract boolean isConnectable(InetSocketAddress remote);
|
||||||
protected abstract boolean isConnectable(InterfaceAddress local,
|
|
||||||
InetSocketAddress remote);
|
|
||||||
|
|
||||||
TcpPlugin(Executor ioExecutor, Backoff backoff, PluginCallback callback,
|
TcpPlugin(Executor ioExecutor, Backoff backoff, PluginCallback callback,
|
||||||
int maxLatency, int maxIdleTime, int connectionTimeout) {
|
int maxLatency, int maxIdleTime) {
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
this.backoff = backoff;
|
this.backoff = backoff;
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
this.maxLatency = maxLatency;
|
this.maxLatency = maxLatency;
|
||||||
this.maxIdleTime = maxIdleTime;
|
this.maxIdleTime = maxIdleTime;
|
||||||
this.connectionTimeout = connectionTimeout;
|
|
||||||
if (maxIdleTime > Integer.MAX_VALUE / 2)
|
if (maxIdleTime > Integer.MAX_VALUE / 2)
|
||||||
socketTimeout = Integer.MAX_VALUE;
|
socketTimeout = Integer.MAX_VALUE;
|
||||||
else socketTimeout = maxIdleTime * 2;
|
else socketTimeout = maxIdleTime * 2;
|
||||||
@@ -233,23 +230,13 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public DuplexTransportConnection createConnection(TransportProperties p) {
|
public DuplexTransportConnection createConnection(TransportProperties p) {
|
||||||
if (!isRunning()) return null;
|
if (!isRunning()) return null;
|
||||||
ServerSocket ss = socket;
|
|
||||||
InterfaceAddress local = getLocalInterfaceAddress(ss.getInetAddress());
|
|
||||||
if (local == null) {
|
|
||||||
LOG.warning("No interface for server socket");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
for (InetSocketAddress remote : getRemoteSocketAddresses(p)) {
|
for (InetSocketAddress remote : getRemoteSocketAddresses(p)) {
|
||||||
// Don't try to connect to our own address
|
if (!isConnectable(remote)) {
|
||||||
if (!canConnectToOwnAddress() &&
|
|
||||||
remote.getAddress().equals(ss.getInetAddress())) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!isConnectable(local, remote)) {
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
|
SocketAddress local = socket.getLocalSocketAddress();
|
||||||
LOG.info(scrubSocketAddress(remote) +
|
LOG.info(scrubSocketAddress(remote) +
|
||||||
" is not connectable from " +
|
" is not connectable from " +
|
||||||
scrubSocketAddress(ss.getLocalSocketAddress()));
|
scrubSocketAddress(local));
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -257,8 +244,8 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Connecting to " + scrubSocketAddress(remote));
|
LOG.info("Connecting to " + scrubSocketAddress(remote));
|
||||||
Socket s = createSocket();
|
Socket s = createSocket();
|
||||||
s.bind(new InetSocketAddress(ss.getInetAddress(), 0));
|
s.bind(new InetSocketAddress(socket.getInetAddress(), 0));
|
||||||
s.connect(remote, connectionTimeout);
|
s.connect(remote);
|
||||||
s.setSoTimeout(socketTimeout);
|
s.setSoTimeout(socketTimeout);
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Connected to " + scrubSocketAddress(remote));
|
LOG.info("Connected to " + scrubSocketAddress(remote));
|
||||||
@@ -272,19 +259,6 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
InterfaceAddress getLocalInterfaceAddress(InetAddress a) {
|
|
||||||
for (InterfaceAddress ifAddr : getLocalInterfaceAddresses()) {
|
|
||||||
if (ifAddr.getAddress().equals(a)) return ifAddr;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Override for testing
|
|
||||||
protected boolean canConnectToOwnAddress() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Socket createSocket() throws IOException {
|
protected Socket createSocket() throws IOException {
|
||||||
return new Socket();
|
return new Socket();
|
||||||
}
|
}
|
||||||
@@ -340,27 +314,14 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<InterfaceAddress> getLocalInterfaceAddresses() {
|
Collection<InetAddress> getLocalIpAddresses() {
|
||||||
List<InterfaceAddress> addrs = new ArrayList<>();
|
|
||||||
for (NetworkInterface iface : getNetworkInterfaces()) {
|
|
||||||
addrs.addAll(iface.getInterfaceAddresses());
|
|
||||||
}
|
|
||||||
return addrs;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<InetAddress> getLocalInetAddresses() {
|
|
||||||
List<InetAddress> addrs = new ArrayList<>();
|
|
||||||
for (NetworkInterface iface : getNetworkInterfaces()) {
|
|
||||||
addrs.addAll(list(iface.getInetAddresses()));
|
|
||||||
}
|
|
||||||
return addrs;
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<NetworkInterface> getNetworkInterfaces() {
|
|
||||||
try {
|
try {
|
||||||
Enumeration<NetworkInterface> ifaces =
|
Enumeration<NetworkInterface> ifaces = getNetworkInterfaces();
|
||||||
NetworkInterface.getNetworkInterfaces();
|
if (ifaces == null) return emptyList();
|
||||||
return ifaces == null ? emptyList() : list(ifaces);
|
List<InetAddress> addrs = new ArrayList<>();
|
||||||
|
for (NetworkInterface iface : list(ifaces))
|
||||||
|
addrs.addAll(list(iface.getInetAddresses()));
|
||||||
|
return addrs;
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
return emptyList();
|
return emptyList();
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import org.briarproject.bramble.api.properties.TransportProperties;
|
|||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.InterfaceAddress;
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
@@ -30,10 +29,8 @@ class WanTcpPlugin extends TcpPlugin {
|
|||||||
private volatile MappingResult mappingResult;
|
private volatile MappingResult mappingResult;
|
||||||
|
|
||||||
WanTcpPlugin(Executor ioExecutor, Backoff backoff, PortMapper portMapper,
|
WanTcpPlugin(Executor ioExecutor, Backoff backoff, PortMapper portMapper,
|
||||||
PluginCallback callback, int maxLatency, int maxIdleTime,
|
PluginCallback callback, int maxLatency, int maxIdleTime) {
|
||||||
int connectionTimeout) {
|
super(ioExecutor, backoff, callback, maxLatency, maxIdleTime);
|
||||||
super(ioExecutor, backoff, callback, maxLatency, maxIdleTime,
|
|
||||||
connectionTimeout);
|
|
||||||
this.portMapper = portMapper;
|
this.portMapper = portMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +45,7 @@ class WanTcpPlugin extends TcpPlugin {
|
|||||||
TransportProperties p = callback.getLocalProperties();
|
TransportProperties p = callback.getLocalProperties();
|
||||||
InetSocketAddress old = parseSocketAddress(p.get(PROP_IP_PORT));
|
InetSocketAddress old = parseSocketAddress(p.get(PROP_IP_PORT));
|
||||||
List<InetSocketAddress> addrs = new LinkedList<>();
|
List<InetSocketAddress> addrs = new LinkedList<>();
|
||||||
for (InetAddress a : getLocalInetAddresses()) {
|
for (InetAddress a : getLocalIpAddresses()) {
|
||||||
if (isAcceptableAddress(a)) {
|
if (isAcceptableAddress(a)) {
|
||||||
// If this is the old address, try to use the same port
|
// If this is the old address, try to use the same port
|
||||||
if (old != null && old.getAddress().equals(a))
|
if (old != null && old.getAddress().equals(a))
|
||||||
@@ -89,8 +86,7 @@ class WanTcpPlugin extends TcpPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isConnectable(InterfaceAddress local,
|
protected boolean isConnectable(InetSocketAddress remote) {
|
||||||
InetSocketAddress remote) {
|
|
||||||
if (remote.getPort() == 0) return false;
|
if (remote.getPort() == 0) return false;
|
||||||
return isAcceptableAddress(remote.getAddress());
|
return isAcceptableAddress(remote.getAddress());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,11 +19,10 @@ import static org.briarproject.bramble.api.plugin.WanTcpConstants.ID;
|
|||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class WanTcpPluginFactory implements DuplexPluginFactory {
|
public class WanTcpPluginFactory implements DuplexPluginFactory {
|
||||||
|
|
||||||
private static final int MAX_LATENCY = 30_000; // 30 seconds
|
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
|
||||||
private static final int MAX_IDLE_TIME = 30_000; // 30 seconds
|
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
|
||||||
private static final int CONNECTION_TIMEOUT = 30_000; // 30 seconds
|
private static final int MIN_POLLING_INTERVAL = 60 * 1000; // 1 minute
|
||||||
private static final int MIN_POLLING_INTERVAL = 60_000; // 1 minute
|
private static final int MAX_POLLING_INTERVAL = 10 * 60 * 1000; // 10 mins
|
||||||
private static final int MAX_POLLING_INTERVAL = 600_000; // 10 mins
|
|
||||||
private static final double BACKOFF_BASE = 1.2;
|
private static final double BACKOFF_BASE = 1.2;
|
||||||
|
|
||||||
private final Executor ioExecutor;
|
private final Executor ioExecutor;
|
||||||
@@ -53,6 +52,6 @@ public class WanTcpPluginFactory implements DuplexPluginFactory {
|
|||||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||||
return new WanTcpPlugin(ioExecutor, backoff,
|
return new WanTcpPlugin(ioExecutor, backoff,
|
||||||
new PortMapperImpl(shutdownManager), callback, MAX_LATENCY,
|
new PortMapperImpl(shutdownManager), callback, MAX_LATENCY,
|
||||||
MAX_IDLE_TIME, CONNECTION_TIMEOUT);
|
MAX_IDLE_TIME);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,11 +37,6 @@ import javax.annotation.Nullable;
|
|||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.GROUP_KEY_DISCOVERED;
|
|
||||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MSG_KEY_LOCAL;
|
|
||||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MSG_KEY_TRANSPORT_ID;
|
|
||||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MSG_KEY_VERSION;
|
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class TransportPropertyManagerImpl implements TransportPropertyManager,
|
class TransportPropertyManagerImpl implements TransportPropertyManager,
|
||||||
@@ -116,10 +111,10 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
|
|||||||
try {
|
try {
|
||||||
// Find the latest update for this transport, if any
|
// Find the latest update for this transport, if any
|
||||||
BdfDictionary d = metadataParser.parse(meta);
|
BdfDictionary d = metadataParser.parse(meta);
|
||||||
TransportId t = new TransportId(d.getString(MSG_KEY_TRANSPORT_ID));
|
TransportId t = new TransportId(d.getString("transportId"));
|
||||||
LatestUpdate latest = findLatest(txn, m.getGroupId(), t, false);
|
LatestUpdate latest = findLatest(txn, m.getGroupId(), t, false);
|
||||||
if (latest != null) {
|
if (latest != null) {
|
||||||
if (d.getLong(MSG_KEY_VERSION) > latest.version) {
|
if (d.getLong("version") > latest.version) {
|
||||||
// This update is newer - delete the previous update
|
// This update is newer - delete the previous update
|
||||||
db.deleteMessage(txn, latest.messageId);
|
db.deleteMessage(txn, latest.messageId);
|
||||||
db.deleteMessageMetadata(txn, latest.messageId);
|
db.deleteMessageMetadata(txn, latest.messageId);
|
||||||
@@ -145,27 +140,6 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addRemotePropertiesFromConnection(ContactId c, TransportId t,
|
|
||||||
TransportProperties props) throws DbException {
|
|
||||||
if (props.isEmpty()) return;
|
|
||||||
try {
|
|
||||||
db.transaction(false, txn -> {
|
|
||||||
Group g = getContactGroup(db.getContact(txn, c));
|
|
||||||
BdfDictionary meta = clientHelper.getGroupMetadataAsDictionary(
|
|
||||||
txn, g.getId());
|
|
||||||
BdfDictionary discovered =
|
|
||||||
meta.getOptionalDictionary(GROUP_KEY_DISCOVERED);
|
|
||||||
if (discovered == null) discovered = new BdfDictionary();
|
|
||||||
discovered.putAll(props);
|
|
||||||
meta.put(GROUP_KEY_DISCOVERED, discovered);
|
|
||||||
clientHelper.mergeGroupMetadata(txn, g.getId(), meta);
|
|
||||||
});
|
|
||||||
} catch (FormatException e) {
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<TransportId, TransportProperties> getLocalProperties()
|
public Map<TransportId, TransportProperties> getLocalProperties()
|
||||||
throws DbException {
|
throws DbException {
|
||||||
@@ -229,26 +203,12 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
|
|||||||
Group g = getContactGroup(c);
|
Group g = getContactGroup(c);
|
||||||
try {
|
try {
|
||||||
// Find the latest remote update
|
// Find the latest remote update
|
||||||
TransportProperties remote;
|
|
||||||
LatestUpdate latest = findLatest(txn, g.getId(), t, false);
|
LatestUpdate latest = findLatest(txn, g.getId(), t, false);
|
||||||
if (latest == null) {
|
if (latest == null) return new TransportProperties();
|
||||||
remote = new TransportProperties();
|
// Retrieve and parse the latest remote properties
|
||||||
} else {
|
BdfList message =
|
||||||
// Retrieve and parse the latest remote properties
|
clientHelper.getMessageAsList(txn, latest.messageId);
|
||||||
BdfList message =
|
return parseProperties(message);
|
||||||
clientHelper.getMessageAsList(txn, latest.messageId);
|
|
||||||
remote = parseProperties(message);
|
|
||||||
}
|
|
||||||
// Merge in any discovered properties
|
|
||||||
BdfDictionary meta =
|
|
||||||
clientHelper.getGroupMetadataAsDictionary(txn, g.getId());
|
|
||||||
BdfDictionary d = meta.getOptionalDictionary(GROUP_KEY_DISCOVERED);
|
|
||||||
if (d == null) return remote;
|
|
||||||
TransportProperties merged =
|
|
||||||
clientHelper.parseAndValidateTransportProperties(d);
|
|
||||||
// Received properties override discovered properties
|
|
||||||
merged.putAll(remote);
|
|
||||||
return merged;
|
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
}
|
}
|
||||||
@@ -321,9 +281,9 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
|
|||||||
long now = clock.currentTimeMillis();
|
long now = clock.currentTimeMillis();
|
||||||
Message m = clientHelper.createMessage(g, now, body);
|
Message m = clientHelper.createMessage(g, now, body);
|
||||||
BdfDictionary meta = new BdfDictionary();
|
BdfDictionary meta = new BdfDictionary();
|
||||||
meta.put(MSG_KEY_TRANSPORT_ID, t.getString());
|
meta.put("transportId", t.getString());
|
||||||
meta.put(MSG_KEY_VERSION, version);
|
meta.put("version", version);
|
||||||
meta.put(MSG_KEY_LOCAL, local);
|
meta.put("local", local);
|
||||||
clientHelper.addLocalMessage(txn, m, meta, shared, false);
|
clientHelper.addLocalMessage(txn, m, meta, shared, false);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
@@ -342,9 +302,8 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
|
|||||||
.getMessageMetadataAsDictionary(txn, localGroup.getId());
|
.getMessageMetadataAsDictionary(txn, localGroup.getId());
|
||||||
for (Entry<MessageId, BdfDictionary> e : metadata.entrySet()) {
|
for (Entry<MessageId, BdfDictionary> e : metadata.entrySet()) {
|
||||||
BdfDictionary meta = e.getValue();
|
BdfDictionary meta = e.getValue();
|
||||||
TransportId t =
|
TransportId t = new TransportId(meta.getString("transportId"));
|
||||||
new TransportId(meta.getString(MSG_KEY_TRANSPORT_ID));
|
long version = meta.getLong("version");
|
||||||
long version = meta.getLong(MSG_KEY_VERSION);
|
|
||||||
latestUpdates.put(t, new LatestUpdate(e.getKey(), version));
|
latestUpdates.put(t, new LatestUpdate(e.getKey(), version));
|
||||||
}
|
}
|
||||||
return latestUpdates;
|
return latestUpdates;
|
||||||
@@ -357,10 +316,9 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
|
|||||||
clientHelper.getMessageMetadataAsDictionary(txn, g);
|
clientHelper.getMessageMetadataAsDictionary(txn, g);
|
||||||
for (Entry<MessageId, BdfDictionary> e : metadata.entrySet()) {
|
for (Entry<MessageId, BdfDictionary> e : metadata.entrySet()) {
|
||||||
BdfDictionary meta = e.getValue();
|
BdfDictionary meta = e.getValue();
|
||||||
if (meta.getString(MSG_KEY_TRANSPORT_ID).equals(t.getString())
|
if (meta.getString("transportId").equals(t.getString())
|
||||||
&& meta.getBoolean(MSG_KEY_LOCAL) == local) {
|
&& meta.getBoolean("local") == local) {
|
||||||
return new LatestUpdate(e.getKey(),
|
return new LatestUpdate(e.getKey(), meta.getLong("version"));
|
||||||
meta.getLong(MSG_KEY_VERSION));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package org.briarproject.bramble.account;
|
package org.briarproject.bramble.account;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
|
||||||
import org.briarproject.bramble.api.crypto.KeyStrengthener;
|
import org.briarproject.bramble.api.crypto.KeyStrengthener;
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||||
@@ -20,15 +19,12 @@ import java.io.FileInputStream;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import static junit.framework.Assert.assertFalse;
|
import static junit.framework.Assert.assertFalse;
|
||||||
import static junit.framework.Assert.assertNull;
|
import static junit.framework.Assert.assertNull;
|
||||||
import static junit.framework.Assert.assertTrue;
|
import static junit.framework.Assert.assertTrue;
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.INVALID_CIPHERTEXT;
|
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.INVALID_PASSWORD;
|
|
||||||
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getIdentity;
|
import static org.briarproject.bramble.test.TestUtils.getIdentity;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||||
@@ -39,7 +35,6 @@ import static org.briarproject.bramble.util.StringUtils.toHexString;
|
|||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
public class AccountManagerImplTest extends BrambleMockTestCase {
|
public class AccountManagerImplTest extends BrambleMockTestCase {
|
||||||
|
|
||||||
@@ -88,13 +83,8 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSignInThrowsExceptionIfDbKeyCannotBeLoaded() {
|
public void testSignInReturnsFalseIfDbKeyCannotBeLoaded() {
|
||||||
try {
|
assertFalse(accountManager.signIn(password));
|
||||||
accountManager.signIn(password);
|
|
||||||
fail();
|
|
||||||
} catch (DecryptionException expected) {
|
|
||||||
assertEquals(INVALID_CIPHERTEXT, expected.getDecryptionResult());
|
|
||||||
}
|
|
||||||
assertFalse(accountManager.hasDatabaseKey());
|
assertFalse(accountManager.hasDatabaseKey());
|
||||||
|
|
||||||
assertFalse(keyFile.exists());
|
assertFalse(keyFile.exists());
|
||||||
@@ -102,11 +92,11 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSignInThrowsExceptionIfPasswordIsWrong() throws Exception {
|
public void testSignInReturnsFalseIfPasswordIsWrong() throws Exception {
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(crypto).decryptWithPassword(encryptedKey, password,
|
oneOf(crypto).decryptWithPassword(encryptedKey, password,
|
||||||
keyStrengthener);
|
keyStrengthener);
|
||||||
will(throwException(new DecryptionException(INVALID_PASSWORD)));
|
will(returnValue(null));
|
||||||
}});
|
}});
|
||||||
|
|
||||||
storeDatabaseKey(keyFile, encryptedKeyHex);
|
storeDatabaseKey(keyFile, encryptedKeyHex);
|
||||||
@@ -115,12 +105,7 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
|||||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||||
|
|
||||||
try {
|
assertFalse(accountManager.signIn(password));
|
||||||
accountManager.signIn(password);
|
|
||||||
fail();
|
|
||||||
} catch (DecryptionException expected) {
|
|
||||||
assertEquals(INVALID_PASSWORD, expected.getDecryptionResult());
|
|
||||||
}
|
|
||||||
assertFalse(accountManager.hasDatabaseKey());
|
assertFalse(accountManager.hasDatabaseKey());
|
||||||
|
|
||||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||||
@@ -143,7 +128,7 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
|||||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||||
|
|
||||||
accountManager.signIn(password);
|
assertTrue(accountManager.signIn(password));
|
||||||
assertTrue(accountManager.hasDatabaseKey());
|
assertTrue(accountManager.hasDatabaseKey());
|
||||||
SecretKey decrypted = accountManager.getDatabaseKey();
|
SecretKey decrypted = accountManager.getDatabaseKey();
|
||||||
assertNotNull(decrypted);
|
assertNotNull(decrypted);
|
||||||
@@ -172,7 +157,7 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
|||||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||||
|
|
||||||
accountManager.signIn(password);
|
assertTrue(accountManager.signIn(password));
|
||||||
assertTrue(accountManager.hasDatabaseKey());
|
assertTrue(accountManager.hasDatabaseKey());
|
||||||
SecretKey decrypted = accountManager.getDatabaseKey();
|
SecretKey decrypted = accountManager.getDatabaseKey();
|
||||||
assertNotNull(decrypted);
|
assertNotNull(decrypted);
|
||||||
@@ -254,6 +239,55 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
|||||||
assertFalse(keyBackupFile.exists());
|
assertFalse(keyBackupFile.exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAccountExistsReturnsFalseIfDbDirectoryDoesNotExist()
|
||||||
|
throws Exception {
|
||||||
|
storeDatabaseKey(keyFile, encryptedKeyHex);
|
||||||
|
storeDatabaseKey(keyBackupFile, encryptedKeyHex);
|
||||||
|
|
||||||
|
assertFalse(dbDir.exists());
|
||||||
|
|
||||||
|
assertFalse(accountManager.accountExists());
|
||||||
|
|
||||||
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||||
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||||
|
assertFalse(dbDir.exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAccountExistsReturnsFalseIfDbDirectoryIsNotDirectory()
|
||||||
|
throws Exception {
|
||||||
|
storeDatabaseKey(keyFile, encryptedKeyHex);
|
||||||
|
storeDatabaseKey(keyBackupFile, encryptedKeyHex);
|
||||||
|
|
||||||
|
assertTrue(dbDir.createNewFile());
|
||||||
|
assertFalse(dbDir.isDirectory());
|
||||||
|
|
||||||
|
assertFalse(accountManager.accountExists());
|
||||||
|
|
||||||
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||||
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||||
|
assertTrue(dbDir.exists());
|
||||||
|
assertFalse(dbDir.isDirectory());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAccountExistsReturnsTrueIfDbDirectoryIsDirectory()
|
||||||
|
throws Exception {
|
||||||
|
storeDatabaseKey(keyFile, encryptedKeyHex);
|
||||||
|
storeDatabaseKey(keyBackupFile, encryptedKeyHex);
|
||||||
|
|
||||||
|
assertTrue(dbDir.mkdirs());
|
||||||
|
assertTrue(dbDir.isDirectory());
|
||||||
|
|
||||||
|
assertTrue(accountManager.accountExists());
|
||||||
|
|
||||||
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||||
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||||
|
assertTrue(dbDir.exists());
|
||||||
|
assertTrue(dbDir.isDirectory());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreateAccountStoresDbKey() throws Exception {
|
public void testCreateAccountStoresDbKey() throws Exception {
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
@@ -281,36 +315,26 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testChangePasswordThrowsExceptionIfDbKeyCannotBeLoaded() {
|
public void testChangePasswordReturnsFalseIfDbKeyCannotBeLoaded() {
|
||||||
try {
|
assertFalse(accountManager.changePassword(password, newPassword));
|
||||||
accountManager.changePassword(password, newPassword);
|
|
||||||
fail();
|
|
||||||
} catch (DecryptionException expected) {
|
|
||||||
assertEquals(INVALID_CIPHERTEXT, expected.getDecryptionResult());
|
|
||||||
}
|
|
||||||
|
|
||||||
assertFalse(keyFile.exists());
|
assertFalse(keyFile.exists());
|
||||||
assertFalse(keyBackupFile.exists());
|
assertFalse(keyBackupFile.exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testChangePasswordThrowsExceptionIfPasswordIsWrong()
|
public void testChangePasswordReturnsFalseIfPasswordIsWrong()
|
||||||
throws Exception {
|
throws Exception {
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(crypto).decryptWithPassword(encryptedKey, password,
|
oneOf(crypto).decryptWithPassword(encryptedKey, password,
|
||||||
keyStrengthener);
|
keyStrengthener);
|
||||||
will(throwException(new DecryptionException(INVALID_PASSWORD)));
|
will(returnValue(null));
|
||||||
}});
|
}});
|
||||||
|
|
||||||
storeDatabaseKey(keyFile, encryptedKeyHex);
|
storeDatabaseKey(keyFile, encryptedKeyHex);
|
||||||
storeDatabaseKey(keyBackupFile, encryptedKeyHex);
|
storeDatabaseKey(keyBackupFile, encryptedKeyHex);
|
||||||
|
|
||||||
try {
|
assertFalse(accountManager.changePassword(password, newPassword));
|
||||||
accountManager.changePassword(password, newPassword);
|
|
||||||
fail();
|
|
||||||
} catch (DecryptionException expected) {
|
|
||||||
assertEquals(INVALID_PASSWORD, expected.getDecryptionResult());
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||||
@@ -333,7 +357,7 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
|||||||
storeDatabaseKey(keyFile, encryptedKeyHex);
|
storeDatabaseKey(keyFile, encryptedKeyHex);
|
||||||
storeDatabaseKey(keyBackupFile, encryptedKeyHex);
|
storeDatabaseKey(keyBackupFile, encryptedKeyHex);
|
||||||
|
|
||||||
accountManager.changePassword(password, newPassword);
|
assertTrue(accountManager.changePassword(password, newPassword));
|
||||||
|
|
||||||
assertEquals(newEncryptedKeyHex, loadDatabaseKey(keyFile));
|
assertEquals(newEncryptedKeyHex, loadDatabaseKey(keyFile));
|
||||||
assertEquals(newEncryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
assertEquals(newEncryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||||
@@ -342,7 +366,7 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
|||||||
private void storeDatabaseKey(File f, String hex) throws IOException {
|
private void storeDatabaseKey(File f, String hex) throws IOException {
|
||||||
f.getParentFile().mkdirs();
|
f.getParentFile().mkdirs();
|
||||||
FileOutputStream out = new FileOutputStream(f);
|
FileOutputStream out = new FileOutputStream(f);
|
||||||
out.write(hex.getBytes(Charset.forName("UTF-8")));
|
out.write(hex.getBytes("UTF-8"));
|
||||||
out.flush();
|
out.flush();
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
@@ -350,7 +374,7 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private String loadDatabaseKey(File f) throws IOException {
|
private String loadDatabaseKey(File f) throws IOException {
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(
|
BufferedReader reader = new BufferedReader(new InputStreamReader(
|
||||||
new FileInputStream(f), Charset.forName("UTF-8")));
|
new FileInputStream(f), "UTF-8"));
|
||||||
String hex = reader.readLine();
|
String hex = reader.readLine();
|
||||||
reader.close();
|
reader.close();
|
||||||
return hex;
|
return hex;
|
||||||
|
|||||||
@@ -1,35 +1,25 @@
|
|||||||
package org.briarproject.bramble.crypto;
|
package org.briarproject.bramble.crypto;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
|
||||||
import org.briarproject.bramble.api.crypto.KeyStrengthener;
|
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
|
||||||
import org.briarproject.bramble.system.SystemClock;
|
import org.briarproject.bramble.system.SystemClock;
|
||||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
import org.briarproject.bramble.test.BrambleTestCase;
|
||||||
import org.briarproject.bramble.test.TestSecureRandomProvider;
|
import org.briarproject.bramble.test.TestSecureRandomProvider;
|
||||||
import org.jmock.Expectations;
|
import org.briarproject.bramble.test.TestUtils;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.INVALID_CIPHERTEXT;
|
import java.util.Random;
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.INVALID_PASSWORD;
|
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.KEY_STRENGTHENER_ERROR;
|
|
||||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
|
||||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
public class PasswordBasedEncryptionTest extends BrambleMockTestCase {
|
public class PasswordBasedEncryptionTest extends BrambleTestCase {
|
||||||
|
|
||||||
private final KeyStrengthener keyStrengthener =
|
|
||||||
context.mock(KeyStrengthener.class);
|
|
||||||
|
|
||||||
private final CryptoComponentImpl crypto =
|
private final CryptoComponentImpl crypto =
|
||||||
new CryptoComponentImpl(new TestSecureRandomProvider(),
|
new CryptoComponentImpl(new TestSecureRandomProvider(),
|
||||||
new ScryptKdf(new SystemClock()));
|
new ScryptKdf(new SystemClock()));
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEncryptionAndDecryption() throws Exception {
|
public void testEncryptionAndDecryption() {
|
||||||
byte[] input = getRandomBytes(1234);
|
byte[] input = TestUtils.getRandomBytes(1234);
|
||||||
String password = "password";
|
String password = "password";
|
||||||
byte[] ciphertext = crypto.encryptWithPassword(input, password, null);
|
byte[] ciphertext = crypto.encryptWithPassword(input, password, null);
|
||||||
byte[] output = crypto.decryptWithPassword(ciphertext, password, null);
|
byte[] output = crypto.decryptWithPassword(ciphertext, password, null);
|
||||||
@@ -37,80 +27,14 @@ public class PasswordBasedEncryptionTest extends BrambleMockTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvalidFormatVersionThrowsException() {
|
public void testInvalidCiphertextReturnsNull() {
|
||||||
byte[] input = getRandomBytes(1234);
|
byte[] input = TestUtils.getRandomBytes(1234);
|
||||||
String password = "password";
|
String password = "password";
|
||||||
byte[] ciphertext = crypto.encryptWithPassword(input, password, null);
|
byte[] ciphertext = crypto.encryptWithPassword(input, password, null);
|
||||||
|
// Modify the ciphertext
|
||||||
// Modify the format version
|
int position = new Random().nextInt(ciphertext.length);
|
||||||
ciphertext[0] ^= (byte) 0xFF;
|
ciphertext[position] = (byte) (ciphertext[position] ^ 0xFF);
|
||||||
try {
|
byte[] output = crypto.decryptWithPassword(ciphertext, password, null);
|
||||||
crypto.decryptWithPassword(ciphertext, password, null);
|
assertNull(output);
|
||||||
fail();
|
|
||||||
} catch (DecryptionException expected) {
|
|
||||||
assertEquals(INVALID_CIPHERTEXT, expected.getDecryptionResult());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testInvalidPasswordThrowsException() {
|
|
||||||
byte[] input = getRandomBytes(1234);
|
|
||||||
byte[] ciphertext = crypto.encryptWithPassword(input, "password", null);
|
|
||||||
|
|
||||||
// Try to decrypt with the wrong password
|
|
||||||
try {
|
|
||||||
crypto.decryptWithPassword(ciphertext, "wrong", null);
|
|
||||||
fail();
|
|
||||||
} catch (DecryptionException expected) {
|
|
||||||
assertEquals(INVALID_PASSWORD, expected.getDecryptionResult());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testMissingKeyStrengthenerThrowsException() {
|
|
||||||
SecretKey strengthened = getSecretKey();
|
|
||||||
context.checking(new Expectations() {{
|
|
||||||
oneOf(keyStrengthener).strengthenKey(with(any(SecretKey.class)));
|
|
||||||
will(returnValue(strengthened));
|
|
||||||
}});
|
|
||||||
|
|
||||||
// Use the key strengthener during encryption
|
|
||||||
byte[] input = getRandomBytes(1234);
|
|
||||||
String password = "password";
|
|
||||||
byte[] ciphertext =
|
|
||||||
crypto.encryptWithPassword(input, password, keyStrengthener);
|
|
||||||
|
|
||||||
// The key strengthener is missing during decryption
|
|
||||||
try {
|
|
||||||
crypto.decryptWithPassword(ciphertext, password, null);
|
|
||||||
fail();
|
|
||||||
} catch (DecryptionException expected) {
|
|
||||||
assertEquals(KEY_STRENGTHENER_ERROR, expected.getDecryptionResult());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testKeyStrengthenerFailureThrowsException() {
|
|
||||||
SecretKey strengthened = getSecretKey();
|
|
||||||
context.checking(new Expectations() {{
|
|
||||||
oneOf(keyStrengthener).strengthenKey(with(any(SecretKey.class)));
|
|
||||||
will(returnValue(strengthened));
|
|
||||||
oneOf(keyStrengthener).isInitialised();
|
|
||||||
will(returnValue(false));
|
|
||||||
}});
|
|
||||||
|
|
||||||
// Use the key strengthener during encryption
|
|
||||||
byte[] input = getRandomBytes(1234);
|
|
||||||
String password = "password";
|
|
||||||
byte[] ciphertext =
|
|
||||||
crypto.encryptWithPassword(input, password, keyStrengthener);
|
|
||||||
|
|
||||||
// The key strengthener fails during decryption
|
|
||||||
try {
|
|
||||||
crypto.decryptWithPassword(ciphertext, password, keyStrengthener);
|
|
||||||
fail();
|
|
||||||
} catch (DecryptionException expected) {
|
|
||||||
assertEquals(KEY_STRENGTHENER_ERROR, expected.getDecryptionResult());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,20 +7,18 @@ import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
|||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionStatusChangedEvent;
|
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionClosedEvent;
|
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionClosedEvent;
|
||||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedEvent;
|
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedEvent;
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
|
||||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||||
import org.jmock.Expectations;
|
import org.jmock.Expectations;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
|
|
||||||
import static java.util.Collections.emptyList;
|
import static java.util.Collections.emptyList;
|
||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
|
||||||
import static org.briarproject.bramble.test.TestUtils.getContactId;
|
import static org.briarproject.bramble.test.TestUtils.getContactId;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getTransportId;
|
import static org.briarproject.bramble.test.TestUtils.getTransportId;
|
||||||
@@ -32,9 +30,6 @@ import static org.junit.Assert.fail;
|
|||||||
public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
||||||
|
|
||||||
private final EventBus eventBus = context.mock(EventBus.class);
|
private final EventBus eventBus = context.mock(EventBus.class);
|
||||||
private final Clock clock = context.mock(Clock.class);
|
|
||||||
private final ScheduledExecutorService scheduler =
|
|
||||||
context.mock(ScheduledExecutorService.class);
|
|
||||||
|
|
||||||
private final ContactId contactId = getContactId();
|
private final ContactId contactId = getContactId();
|
||||||
private final ContactId contactId1 = getContactId();
|
private final ContactId contactId1 = getContactId();
|
||||||
@@ -45,25 +40,17 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRegisterAndUnregister() {
|
public void testRegisterAndUnregister() {
|
||||||
context.checking(new Expectations() {{
|
ConnectionRegistry c = new ConnectionRegistryImpl(eventBus);
|
||||||
oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)),
|
|
||||||
with(10_000L), with(10_000L), with(MILLISECONDS));
|
|
||||||
}});
|
|
||||||
|
|
||||||
ConnectionRegistry c = new ConnectionRegistryImpl(eventBus, clock,
|
|
||||||
scheduler);
|
|
||||||
context.assertIsSatisfied();
|
|
||||||
|
|
||||||
// The registry should be empty
|
// The registry should be empty
|
||||||
assertEquals(emptyList(), c.getConnectedContacts(transportId));
|
assertEquals(emptyList(), c.getConnectedContacts(transportId));
|
||||||
assertEquals(emptyList(), c.getConnectedContacts(transportId1));
|
assertEquals(emptyList(), c.getConnectedContacts(transportId1));
|
||||||
|
|
||||||
// Check that a registered connection shows up - this should
|
// Check that a registered connection shows up - this should
|
||||||
// broadcast a ConnectionOpenedEvent and a ConnectionStatusChangedEvent
|
// broadcast a ConnectionOpenedEvent and a ContactConnectedEvent
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(eventBus).broadcast(with(any(ConnectionOpenedEvent.class)));
|
oneOf(eventBus).broadcast(with(any(ConnectionOpenedEvent.class)));
|
||||||
oneOf(eventBus).broadcast(with(any(
|
oneOf(eventBus).broadcast(with(any(ContactConnectedEvent.class)));
|
||||||
ConnectionStatusChangedEvent.class)));
|
|
||||||
}});
|
}});
|
||||||
c.registerConnection(contactId, transportId, true);
|
c.registerConnection(contactId, transportId, true);
|
||||||
assertEquals(singletonList(contactId),
|
assertEquals(singletonList(contactId),
|
||||||
@@ -94,13 +81,11 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
|||||||
context.assertIsSatisfied();
|
context.assertIsSatisfied();
|
||||||
|
|
||||||
// Unregister the other connection - this should broadcast a
|
// Unregister the other connection - this should broadcast a
|
||||||
// ConnectionClosedEvent and a ConnectionStatusChangedEvent
|
// ConnectionClosedEvent and a ContactDisconnectedEvent
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(clock).currentTimeMillis();
|
|
||||||
will(returnValue(System.currentTimeMillis()));
|
|
||||||
oneOf(eventBus).broadcast(with(any(ConnectionClosedEvent.class)));
|
oneOf(eventBus).broadcast(with(any(ConnectionClosedEvent.class)));
|
||||||
oneOf(eventBus).broadcast(with(any(
|
oneOf(eventBus).broadcast(with(any(
|
||||||
ConnectionStatusChangedEvent.class)));
|
ContactDisconnectedEvent.class)));
|
||||||
}});
|
}});
|
||||||
c.unregisterConnection(contactId, transportId, true);
|
c.unregisterConnection(contactId, transportId, true);
|
||||||
assertEquals(emptyList(), c.getConnectedContacts(transportId));
|
assertEquals(emptyList(), c.getConnectedContacts(transportId));
|
||||||
@@ -117,12 +102,12 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
// Register both contacts with one transport, one contact with both -
|
// Register both contacts with one transport, one contact with both -
|
||||||
// this should broadcast three ConnectionOpenedEvents and two
|
// this should broadcast three ConnectionOpenedEvents and two
|
||||||
// ConnectionStatusChangedEvents
|
// ContactConnectedEvents
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
exactly(3).of(eventBus).broadcast(with(any(
|
exactly(3).of(eventBus).broadcast(with(any(
|
||||||
ConnectionOpenedEvent.class)));
|
ConnectionOpenedEvent.class)));
|
||||||
exactly(2).of(eventBus).broadcast(with(any(
|
exactly(2).of(eventBus).broadcast(with(any(
|
||||||
ConnectionStatusChangedEvent.class)));
|
ContactConnectedEvent.class)));
|
||||||
}});
|
}});
|
||||||
c.registerConnection(contactId, transportId, true);
|
c.registerConnection(contactId, transportId, true);
|
||||||
c.registerConnection(contactId1, transportId, true);
|
c.registerConnection(contactId1, transportId, true);
|
||||||
@@ -137,14 +122,7 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRegisterAndUnregisterPendingContacts() {
|
public void testRegisterAndUnregisterPendingContacts() {
|
||||||
context.checking(new Expectations() {{
|
ConnectionRegistry c = new ConnectionRegistryImpl(eventBus);
|
||||||
oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)),
|
|
||||||
with(10_000L), with(10_000L), with(MILLISECONDS));
|
|
||||||
}});
|
|
||||||
|
|
||||||
ConnectionRegistry c = new ConnectionRegistryImpl(eventBus, clock,
|
|
||||||
scheduler);
|
|
||||||
context.assertIsSatisfied();
|
|
||||||
|
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(eventBus).broadcast(with(any(
|
oneOf(eventBus).broadcast(with(any(
|
||||||
|
|||||||
@@ -7,11 +7,12 @@ import org.briarproject.bramble.api.plugin.Backoff;
|
|||||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||||
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.settings.Settings;
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
|
import org.briarproject.bramble.plugin.tcp.LanTcpPlugin.LanAddressComparator;
|
||||||
import org.briarproject.bramble.test.BrambleTestCase;
|
import org.briarproject.bramble.test.BrambleTestCase;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -21,6 +22,7 @@ import java.net.InetSocketAddress;
|
|||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
@@ -31,89 +33,56 @@ import static java.util.concurrent.Executors.newCachedThreadPool;
|
|||||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||||
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.COMMIT_LENGTH;
|
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.COMMIT_LENGTH;
|
||||||
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_LAN;
|
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_LAN;
|
||||||
import static org.briarproject.bramble.plugin.tcp.LanTcpPlugin.areAddressesInSameNetwork;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assume.assumeTrue;
|
|
||||||
|
|
||||||
public class LanTcpPluginTest extends BrambleTestCase {
|
public class LanTcpPluginTest extends BrambleTestCase {
|
||||||
|
|
||||||
private final Backoff backoff = new TestBackoff();
|
private final Backoff backoff = new TestBackoff();
|
||||||
private final ExecutorService ioExecutor = newCachedThreadPool();
|
private final ExecutorService ioExecutor = newCachedThreadPool();
|
||||||
|
|
||||||
private Callback callback = null;
|
|
||||||
private LanTcpPlugin plugin = null;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
callback = new Callback();
|
|
||||||
plugin = new LanTcpPlugin(ioExecutor, backoff, callback, 0, 0, 1000) {
|
|
||||||
@Override
|
|
||||||
protected boolean canConnectToOwnAddress() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAreAddressesInSameNetwork() {
|
public void testAddressesAreOnSameLan() {
|
||||||
// Local and remote in 10.0.0.0/8
|
Callback callback = new Callback();
|
||||||
assertTrue(areAddressesInSameNetwork(makeAddress(10, 0, 0, 0),
|
LanTcpPlugin plugin = new LanTcpPlugin(ioExecutor, backoff, callback,
|
||||||
makeAddress(10, 255, 255, 255), 8));
|
0, 0);
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(10, 0, 0, 0),
|
// Local and remote in 10.0.0.0/8 should return true
|
||||||
makeAddress(10, 255, 255, 255), 9));
|
assertTrue(plugin.addressesAreOnSameLan(makeAddress(10, 0, 0, 0),
|
||||||
|
makeAddress(10, 255, 255, 255)));
|
||||||
// Local and remote in 172.16.0.0/12
|
// Local and remote in 172.16.0.0/12 should return true
|
||||||
assertTrue(areAddressesInSameNetwork(makeAddress(172, 16, 0, 0),
|
assertTrue(plugin.addressesAreOnSameLan(makeAddress(172, 16, 0, 0),
|
||||||
makeAddress(172, 31, 255, 255), 12));
|
makeAddress(172, 31, 255, 255)));
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(172, 16, 0, 0),
|
// Local and remote in 192.168.0.0/16 should return true
|
||||||
makeAddress(172, 31, 255, 255), 13));
|
assertTrue(plugin.addressesAreOnSameLan(makeAddress(192, 168, 0, 0),
|
||||||
|
makeAddress(192, 168, 255, 255)));
|
||||||
// Local and remote in 192.168.0.0/16
|
// Local and remote in 169.254.0.0/16 (link-local) should return true
|
||||||
assertTrue(areAddressesInSameNetwork(makeAddress(192, 168, 0, 0),
|
assertTrue(plugin.addressesAreOnSameLan(makeAddress(169, 254, 0, 0),
|
||||||
makeAddress(192, 168, 255, 255), 16));
|
makeAddress(169, 254, 255, 255)));
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(192, 168, 0, 0),
|
// Local and remote in different recognised prefixes should return false
|
||||||
makeAddress(192, 168, 255, 255), 17));
|
assertFalse(plugin.addressesAreOnSameLan(makeAddress(10, 0, 0, 0),
|
||||||
|
makeAddress(172, 31, 255, 255)));
|
||||||
// Local and remote in 169.254.0.0/16
|
assertFalse(plugin.addressesAreOnSameLan(makeAddress(10, 0, 0, 0),
|
||||||
assertTrue(areAddressesInSameNetwork(makeAddress(169, 254, 0, 0),
|
makeAddress(192, 168, 255, 255)));
|
||||||
makeAddress(169, 254, 255, 255), 16));
|
assertFalse(plugin.addressesAreOnSameLan(makeAddress(172, 16, 0, 0),
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(169, 254, 0, 0),
|
makeAddress(10, 255, 255, 255)));
|
||||||
makeAddress(169, 254, 255, 255), 17));
|
assertFalse(plugin.addressesAreOnSameLan(makeAddress(172, 16, 0, 0),
|
||||||
|
makeAddress(192, 168, 255, 255)));
|
||||||
// Local in 10.0.0.0/8, remote in a different network
|
assertFalse(plugin.addressesAreOnSameLan(makeAddress(192, 168, 0, 0),
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(10, 0, 0, 0),
|
makeAddress(10, 255, 255, 255)));
|
||||||
makeAddress(172, 31, 255, 255), 8));
|
assertFalse(plugin.addressesAreOnSameLan(makeAddress(192, 168, 0, 0),
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(10, 0, 0, 0),
|
makeAddress(172, 31, 255, 255)));
|
||||||
makeAddress(192, 168, 255, 255), 8));
|
// Remote prefix unrecognised should return false
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(10, 0, 0, 0),
|
assertFalse(plugin.addressesAreOnSameLan(makeAddress(10, 0, 0, 0),
|
||||||
makeAddress(169, 254, 255, 255), 8));
|
makeAddress(1, 2, 3, 4)));
|
||||||
|
assertFalse(plugin.addressesAreOnSameLan(makeAddress(172, 16, 0, 0),
|
||||||
// Local in 172.16.0.0/12, remote in a different network
|
makeAddress(1, 2, 3, 4)));
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(172, 16, 0, 0),
|
assertFalse(plugin.addressesAreOnSameLan(makeAddress(192, 168, 0, 0),
|
||||||
makeAddress(10, 255, 255, 255), 12));
|
makeAddress(1, 2, 3, 4)));
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(172, 16, 0, 0),
|
// Both prefixes unrecognised should return true (could be link-local)
|
||||||
makeAddress(192, 168, 255, 255), 12));
|
assertTrue(plugin.addressesAreOnSameLan(makeAddress(1, 2, 3, 4),
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(172, 16, 0, 0),
|
makeAddress(5, 6, 7, 8)));
|
||||||
makeAddress(169, 254, 255, 255), 12));
|
|
||||||
|
|
||||||
// Local in 192.168.0.0/16, remote in a different network
|
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(192, 168, 0, 0),
|
|
||||||
makeAddress(10, 255, 255, 255), 16));
|
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(192, 168, 0, 0),
|
|
||||||
makeAddress(172, 31, 255, 255), 16));
|
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(192, 168, 0, 0),
|
|
||||||
makeAddress(169, 254, 255, 255), 16));
|
|
||||||
|
|
||||||
// Local in 169.254.0.0/16, remote in a different network
|
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(169, 254, 0, 0),
|
|
||||||
makeAddress(10, 255, 255, 255), 16));
|
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(169, 254, 0, 0),
|
|
||||||
makeAddress(172, 31, 255, 255), 16));
|
|
||||||
assertFalse(areAddressesInSameNetwork(makeAddress(169, 254, 0, 0),
|
|
||||||
makeAddress(192, 168, 255, 255), 16));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] makeAddress(int... parts) {
|
private byte[] makeAddress(int... parts) {
|
||||||
@@ -124,7 +93,13 @@ public class LanTcpPluginTest extends BrambleTestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIncomingConnection() throws Exception {
|
public void testIncomingConnection() throws Exception {
|
||||||
assumeTrue(systemHasLocalIpv4Address());
|
if (!systemHasLocalIpv4Address()) {
|
||||||
|
System.err.println("WARNING: Skipping test, no local IPv4 address");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Callback callback = new Callback();
|
||||||
|
DuplexPlugin plugin = new LanTcpPlugin(ioExecutor, backoff, callback,
|
||||||
|
0, 0);
|
||||||
plugin.start();
|
plugin.start();
|
||||||
// The plugin should have bound a socket and stored the port number
|
// The plugin should have bound a socket and stored the port number
|
||||||
assertTrue(callback.propertiesLatch.await(5, SECONDS));
|
assertTrue(callback.propertiesLatch.await(5, SECONDS));
|
||||||
@@ -153,7 +128,13 @@ public class LanTcpPluginTest extends BrambleTestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOutgoingConnection() throws Exception {
|
public void testOutgoingConnection() throws Exception {
|
||||||
assumeTrue(systemHasLocalIpv4Address());
|
if (!systemHasLocalIpv4Address()) {
|
||||||
|
System.err.println("WARNING: Skipping test, no local IPv4 address");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Callback callback = new Callback();
|
||||||
|
DuplexPlugin plugin = new LanTcpPlugin(ioExecutor, backoff, callback,
|
||||||
|
0, 0);
|
||||||
plugin.start();
|
plugin.start();
|
||||||
// The plugin should have bound a socket and stored the port number
|
// The plugin should have bound a socket and stored the port number
|
||||||
assertTrue(callback.propertiesLatch.await(5, SECONDS));
|
assertTrue(callback.propertiesLatch.await(5, SECONDS));
|
||||||
@@ -196,7 +177,13 @@ public class LanTcpPluginTest extends BrambleTestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIncomingKeyAgreementConnection() throws Exception {
|
public void testIncomingKeyAgreementConnection() throws Exception {
|
||||||
assumeTrue(systemHasLocalIpv4Address());
|
if (!systemHasLocalIpv4Address()) {
|
||||||
|
System.err.println("WARNING: Skipping test, no local IPv4 address");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Callback callback = new Callback();
|
||||||
|
DuplexPlugin plugin = new LanTcpPlugin(ioExecutor, backoff, callback,
|
||||||
|
0, 0);
|
||||||
plugin.start();
|
plugin.start();
|
||||||
assertTrue(callback.propertiesLatch.await(5, SECONDS));
|
assertTrue(callback.propertiesLatch.await(5, SECONDS));
|
||||||
KeyAgreementListener kal =
|
KeyAgreementListener kal =
|
||||||
@@ -238,7 +225,13 @@ public class LanTcpPluginTest extends BrambleTestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOutgoingKeyAgreementConnection() throws Exception {
|
public void testOutgoingKeyAgreementConnection() throws Exception {
|
||||||
assumeTrue(systemHasLocalIpv4Address());
|
if (!systemHasLocalIpv4Address()) {
|
||||||
|
System.err.println("WARNING: Skipping test, no local IPv4 address");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Callback callback = new Callback();
|
||||||
|
DuplexPlugin plugin = new LanTcpPlugin(ioExecutor, backoff, callback,
|
||||||
|
0, 0);
|
||||||
plugin.start();
|
plugin.start();
|
||||||
// The plugin should have bound a socket and stored the port number
|
// The plugin should have bound a socket and stored the port number
|
||||||
assertTrue(callback.propertiesLatch.await(5, SECONDS));
|
assertTrue(callback.propertiesLatch.await(5, SECONDS));
|
||||||
@@ -283,12 +276,62 @@ public class LanTcpPluginTest extends BrambleTestCase {
|
|||||||
plugin.stop();
|
plugin.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testComparatorPrefersNonZeroPorts() {
|
||||||
|
Comparator<InetSocketAddress> comparator = new LanAddressComparator();
|
||||||
|
InetSocketAddress nonZero = new InetSocketAddress("1.2.3.4", 1234);
|
||||||
|
InetSocketAddress zero = new InetSocketAddress("1.2.3.4", 0);
|
||||||
|
|
||||||
|
assertEquals(0, comparator.compare(nonZero, nonZero));
|
||||||
|
assertTrue(comparator.compare(nonZero, zero) < 0);
|
||||||
|
|
||||||
|
assertTrue(comparator.compare(zero, nonZero) > 0);
|
||||||
|
assertEquals(0, comparator.compare(zero, zero));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testComparatorPrefersLongerPrefixes() {
|
||||||
|
Comparator<InetSocketAddress> comparator = new LanAddressComparator();
|
||||||
|
InetSocketAddress prefix192 = new InetSocketAddress("192.168.0.1", 0);
|
||||||
|
InetSocketAddress prefix172 = new InetSocketAddress("172.16.0.1", 0);
|
||||||
|
InetSocketAddress prefix10 = new InetSocketAddress("10.0.0.1", 0);
|
||||||
|
|
||||||
|
assertEquals(0, comparator.compare(prefix192, prefix192));
|
||||||
|
assertTrue(comparator.compare(prefix192, prefix172) < 0);
|
||||||
|
assertTrue(comparator.compare(prefix192, prefix10) < 0);
|
||||||
|
|
||||||
|
assertTrue(comparator.compare(prefix172, prefix192) > 0);
|
||||||
|
assertEquals(0, comparator.compare(prefix172, prefix172));
|
||||||
|
assertTrue(comparator.compare(prefix172, prefix10) < 0);
|
||||||
|
|
||||||
|
assertTrue(comparator.compare(prefix10, prefix192) > 0);
|
||||||
|
assertTrue(comparator.compare(prefix10, prefix172) > 0);
|
||||||
|
assertEquals(0, comparator.compare(prefix10, prefix10));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testComparatorPrefersSiteLocalToLinkLocal() {
|
||||||
|
Comparator<InetSocketAddress> comparator = new LanAddressComparator();
|
||||||
|
InetSocketAddress prefix192 = new InetSocketAddress("192.168.0.1", 0);
|
||||||
|
InetSocketAddress prefix172 = new InetSocketAddress("172.16.0.1", 0);
|
||||||
|
InetSocketAddress prefix10 = new InetSocketAddress("10.0.0.1", 0);
|
||||||
|
InetSocketAddress linkLocal = new InetSocketAddress("169.254.0.1", 0);
|
||||||
|
|
||||||
|
assertTrue(comparator.compare(prefix192, linkLocal) < 0);
|
||||||
|
assertTrue(comparator.compare(prefix172, linkLocal) < 0);
|
||||||
|
assertTrue(comparator.compare(prefix10, linkLocal) < 0);
|
||||||
|
|
||||||
|
assertTrue(comparator.compare(linkLocal, prefix192) > 0);
|
||||||
|
assertTrue(comparator.compare(linkLocal, prefix172) > 0);
|
||||||
|
assertTrue(comparator.compare(linkLocal, prefix10) > 0);
|
||||||
|
assertEquals(0, comparator.compare(linkLocal, linkLocal));
|
||||||
|
}
|
||||||
|
|
||||||
private boolean systemHasLocalIpv4Address() throws Exception {
|
private boolean systemHasLocalIpv4Address() throws Exception {
|
||||||
for (NetworkInterface i : list(getNetworkInterfaces())) {
|
for (NetworkInterface i : list(getNetworkInterfaces())) {
|
||||||
for (InetAddress a : list(i.getInetAddresses())) {
|
for (InetAddress a : list(i.getInetAddresses())) {
|
||||||
if (a instanceof Inet4Address) {
|
if (a instanceof Inet4Address)
|
||||||
return a.isLinkLocalAddress() || a.isSiteLocalAddress();
|
return a.isLinkLocalAddress() || a.isSiteLocalAddress();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -297,9 +340,7 @@ public class LanTcpPluginTest extends BrambleTestCase {
|
|||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
private static class Callback implements PluginCallback {
|
private static class Callback implements PluginCallback {
|
||||||
|
|
||||||
// Properties will be stored twice: the preferred port at startup,
|
private final CountDownLatch propertiesLatch = new CountDownLatch(1);
|
||||||
// and the IP:port when the server socket is bound
|
|
||||||
private final CountDownLatch propertiesLatch = new CountDownLatch(2);
|
|
||||||
private final CountDownLatch connectionsLatch = new CountDownLatch(1);
|
private final CountDownLatch connectionsLatch = new CountDownLatch(1);
|
||||||
private final TransportProperties local = new TransportProperties();
|
private final TransportProperties local = new TransportProperties();
|
||||||
|
|
||||||
|
|||||||
@@ -24,18 +24,14 @@ import org.briarproject.bramble.test.DbExpectations;
|
|||||||
import org.jmock.Expectations;
|
import org.jmock.Expectations;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
import static java.util.Collections.emptyMap;
|
|
||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
import static java.util.Collections.singletonMap;
|
import static java.util.Collections.singletonMap;
|
||||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.GROUP_KEY_DISCOVERED;
|
|
||||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MSG_KEY_LOCAL;
|
|
||||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MSG_KEY_TRANSPORT_ID;
|
|
||||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MSG_KEY_VERSION;
|
|
||||||
import static org.briarproject.bramble.api.properties.TransportPropertyManager.CLIENT_ID;
|
import static org.briarproject.bramble.api.properties.TransportPropertyManager.CLIENT_ID;
|
||||||
import static org.briarproject.bramble.api.properties.TransportPropertyManager.MAJOR_VERSION;
|
import static org.briarproject.bramble.api.properties.TransportPropertyManager.MAJOR_VERSION;
|
||||||
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
||||||
@@ -190,25 +186,25 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
|||||||
Message message = getMessage(contactGroupId);
|
Message message = getMessage(contactGroupId);
|
||||||
Metadata meta = new Metadata();
|
Metadata meta = new Metadata();
|
||||||
BdfDictionary metaDictionary = BdfDictionary.of(
|
BdfDictionary metaDictionary = BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
new BdfEntry("transportId", "foo"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 2),
|
new BdfEntry("version", 2),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
new BdfEntry("local", false)
|
||||||
);
|
);
|
||||||
Map<MessageId, BdfDictionary> messageMetadata =
|
Map<MessageId, BdfDictionary> messageMetadata =
|
||||||
new LinkedHashMap<>();
|
new LinkedHashMap<>();
|
||||||
// A remote update for another transport should be ignored
|
// A remote update for another transport should be ignored
|
||||||
MessageId barUpdateId = new MessageId(getRandomId());
|
MessageId barUpdateId = new MessageId(getRandomId());
|
||||||
messageMetadata.put(barUpdateId, BdfDictionary.of(
|
messageMetadata.put(barUpdateId, BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "bar"),
|
new BdfEntry("transportId", "bar"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
new BdfEntry("version", 1),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
new BdfEntry("local", false)
|
||||||
));
|
));
|
||||||
// A local update for the same transport should be ignored
|
// A local update for the same transport should be ignored
|
||||||
MessageId localUpdateId = new MessageId(getRandomId());
|
MessageId localUpdateId = new MessageId(getRandomId());
|
||||||
messageMetadata.put(localUpdateId, BdfDictionary.of(
|
messageMetadata.put(localUpdateId, BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
new BdfEntry("transportId", "foo"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
new BdfEntry("version", 1),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, true)
|
new BdfEntry("local", true)
|
||||||
));
|
));
|
||||||
|
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
@@ -232,18 +228,18 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
|||||||
Metadata meta = new Metadata();
|
Metadata meta = new Metadata();
|
||||||
// Version 4 is being delivered
|
// Version 4 is being delivered
|
||||||
BdfDictionary metaDictionary = BdfDictionary.of(
|
BdfDictionary metaDictionary = BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
new BdfEntry("transportId", "foo"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 4),
|
new BdfEntry("version", 4),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
new BdfEntry("local", false)
|
||||||
);
|
);
|
||||||
Map<MessageId, BdfDictionary> messageMetadata =
|
Map<MessageId, BdfDictionary> messageMetadata =
|
||||||
new LinkedHashMap<>();
|
new LinkedHashMap<>();
|
||||||
// An older remote update for the same transport should be deleted
|
// An older remote update for the same transport should be deleted
|
||||||
MessageId fooVersion3 = new MessageId(getRandomId());
|
MessageId fooVersion3 = new MessageId(getRandomId());
|
||||||
messageMetadata.put(fooVersion3, BdfDictionary.of(
|
messageMetadata.put(fooVersion3, BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
new BdfEntry("transportId", "foo"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 3),
|
new BdfEntry("version", 3),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
new BdfEntry("local", false)
|
||||||
));
|
));
|
||||||
|
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
@@ -269,18 +265,18 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
|||||||
Metadata meta = new Metadata();
|
Metadata meta = new Metadata();
|
||||||
// Version 3 is being delivered
|
// Version 3 is being delivered
|
||||||
BdfDictionary metaDictionary = BdfDictionary.of(
|
BdfDictionary metaDictionary = BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
new BdfEntry("transportId", "foo"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 3),
|
new BdfEntry("version", 3),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
new BdfEntry("local", false)
|
||||||
);
|
);
|
||||||
Map<MessageId, BdfDictionary> messageMetadata =
|
Map<MessageId, BdfDictionary> messageMetadata =
|
||||||
new LinkedHashMap<>();
|
new LinkedHashMap<>();
|
||||||
// A newer remote update for the same transport should not be deleted
|
// A newer remote update for the same transport should not be deleted
|
||||||
MessageId fooVersion4 = new MessageId(getRandomId());
|
MessageId fooVersion4 = new MessageId(getRandomId());
|
||||||
messageMetadata.put(fooVersion4, BdfDictionary.of(
|
messageMetadata.put(fooVersion4, BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
new BdfEntry("transportId", "foo"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 4),
|
new BdfEntry("version", 4),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
new BdfEntry("local", false)
|
||||||
));
|
));
|
||||||
|
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
@@ -346,9 +342,9 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
|||||||
// A local update for another transport should be ignored
|
// A local update for another transport should be ignored
|
||||||
MessageId barUpdateId = new MessageId(getRandomId());
|
MessageId barUpdateId = new MessageId(getRandomId());
|
||||||
messageMetadata.put(barUpdateId, BdfDictionary.of(
|
messageMetadata.put(barUpdateId, BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "bar"),
|
new BdfEntry("transportId", "bar"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
new BdfEntry("version", 1),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, true)
|
new BdfEntry("local", true)
|
||||||
));
|
));
|
||||||
|
|
||||||
context.checking(new DbExpectations() {{
|
context.checking(new DbExpectations() {{
|
||||||
@@ -370,16 +366,16 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
|||||||
// A local update for another transport should be ignored
|
// A local update for another transport should be ignored
|
||||||
MessageId barUpdateId = new MessageId(getRandomId());
|
MessageId barUpdateId = new MessageId(getRandomId());
|
||||||
messageMetadata.put(barUpdateId, BdfDictionary.of(
|
messageMetadata.put(barUpdateId, BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "bar"),
|
new BdfEntry("transportId", "bar"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
new BdfEntry("version", 1),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, true)
|
new BdfEntry("local", true)
|
||||||
));
|
));
|
||||||
// A local update for the right transport should be returned
|
// A local update for the right transport should be returned
|
||||||
MessageId fooUpdateId = new MessageId(getRandomId());
|
MessageId fooUpdateId = new MessageId(getRandomId());
|
||||||
messageMetadata.put(fooUpdateId, BdfDictionary.of(
|
messageMetadata.put(fooUpdateId, BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
new BdfEntry("transportId", "foo"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
new BdfEntry("version", 1),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, true)
|
new BdfEntry("local", true)
|
||||||
));
|
));
|
||||||
BdfList fooUpdate = BdfList.of("foo", 1, fooPropertiesDict);
|
BdfList fooUpdate = BdfList.of("foo", 1, fooPropertiesDict);
|
||||||
|
|
||||||
@@ -409,28 +405,28 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
|||||||
List<Contact> contacts = asList(contact1, contact2);
|
List<Contact> contacts = asList(contact1, contact2);
|
||||||
Group contactGroup1 = getGroup(CLIENT_ID, MAJOR_VERSION);
|
Group contactGroup1 = getGroup(CLIENT_ID, MAJOR_VERSION);
|
||||||
Group contactGroup2 = getGroup(CLIENT_ID, MAJOR_VERSION);
|
Group contactGroup2 = getGroup(CLIENT_ID, MAJOR_VERSION);
|
||||||
Map<MessageId, BdfDictionary> messageMetadata =
|
Map<MessageId, BdfDictionary> messageMetadata2 =
|
||||||
new LinkedHashMap<>();
|
new LinkedHashMap<>();
|
||||||
// A remote update for another transport should be ignored
|
// A remote update for another transport should be ignored
|
||||||
MessageId barUpdateId = new MessageId(getRandomId());
|
MessageId barUpdateId = new MessageId(getRandomId());
|
||||||
messageMetadata.put(barUpdateId, BdfDictionary.of(
|
messageMetadata2.put(barUpdateId, BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "bar"),
|
new BdfEntry("transportId", "bar"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
new BdfEntry("version", 1),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
new BdfEntry("local", false)
|
||||||
));
|
));
|
||||||
// A local update for the right transport should be ignored
|
// A local update for the right transport should be ignored
|
||||||
MessageId localUpdateId = new MessageId(getRandomId());
|
MessageId localUpdateId = new MessageId(getRandomId());
|
||||||
messageMetadata.put(localUpdateId, BdfDictionary.of(
|
messageMetadata2.put(localUpdateId, BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
new BdfEntry("transportId", "foo"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
new BdfEntry("version", 1),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, true)
|
new BdfEntry("local", true)
|
||||||
));
|
));
|
||||||
// A remote update for the right transport should be returned
|
// A remote update for the right transport should be returned
|
||||||
MessageId fooUpdateId = new MessageId(getRandomId());
|
MessageId fooUpdateId = new MessageId(getRandomId());
|
||||||
messageMetadata.put(fooUpdateId, BdfDictionary.of(
|
messageMetadata2.put(fooUpdateId, BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
new BdfEntry("transportId", "foo"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
new BdfEntry("version", 1),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
new BdfEntry("local", false)
|
||||||
));
|
));
|
||||||
BdfList fooUpdate = BdfList.of("foo", 1, fooPropertiesDict);
|
BdfList fooUpdate = BdfList.of("foo", 1, fooPropertiesDict);
|
||||||
|
|
||||||
@@ -444,25 +440,19 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
|||||||
will(returnValue(contactGroup1));
|
will(returnValue(contactGroup1));
|
||||||
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
|
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
|
||||||
contactGroup1.getId());
|
contactGroup1.getId());
|
||||||
will(returnValue(emptyMap()));
|
will(returnValue(Collections.emptyMap()));
|
||||||
oneOf(clientHelper).getGroupMetadataAsDictionary(txn,
|
|
||||||
contactGroup1.getId());
|
|
||||||
will(returnValue(new BdfDictionary()));
|
|
||||||
// Second contact: returns an update
|
// Second contact: returns an update
|
||||||
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID,
|
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID,
|
||||||
MAJOR_VERSION, contact2);
|
MAJOR_VERSION, contact2);
|
||||||
will(returnValue(contactGroup2));
|
will(returnValue(contactGroup2));
|
||||||
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
|
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
|
||||||
contactGroup2.getId());
|
contactGroup2.getId());
|
||||||
will(returnValue(messageMetadata));
|
will(returnValue(messageMetadata2));
|
||||||
oneOf(clientHelper).getMessageAsList(txn, fooUpdateId);
|
oneOf(clientHelper).getMessageAsList(txn, fooUpdateId);
|
||||||
will(returnValue(fooUpdate));
|
will(returnValue(fooUpdate));
|
||||||
oneOf(clientHelper).parseAndValidateTransportProperties(
|
oneOf(clientHelper).parseAndValidateTransportProperties(
|
||||||
fooPropertiesDict);
|
fooPropertiesDict);
|
||||||
will(returnValue(fooProperties));
|
will(returnValue(fooProperties));
|
||||||
oneOf(clientHelper).getGroupMetadataAsDictionary(txn,
|
|
||||||
contactGroup2.getId());
|
|
||||||
will(returnValue(new BdfDictionary()));
|
|
||||||
}});
|
}});
|
||||||
|
|
||||||
TransportPropertyManagerImpl t = createInstance();
|
TransportPropertyManagerImpl t = createInstance();
|
||||||
@@ -473,62 +463,6 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
|||||||
assertEquals(fooProperties, properties.get(contact2.getId()));
|
assertEquals(fooProperties, properties.get(contact2.getId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testReceivePropertiesOverrideDiscoveredProperties()
|
|
||||||
throws Exception {
|
|
||||||
Transaction txn = new Transaction(null, true);
|
|
||||||
Contact contact = getContact();
|
|
||||||
List<Contact> contacts = singletonList(contact);
|
|
||||||
Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION);
|
|
||||||
MessageId updateId = new MessageId(getRandomId());
|
|
||||||
Map<MessageId, BdfDictionary> messageMetadata = singletonMap(updateId,
|
|
||||||
BdfDictionary.of(
|
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
|
||||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
|
||||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
|
||||||
));
|
|
||||||
BdfList update = BdfList.of("foo", 1, fooPropertiesDict);
|
|
||||||
TransportProperties discovered = new TransportProperties();
|
|
||||||
discovered.put("fooKey1", "overridden");
|
|
||||||
discovered.put("fooKey3", "fooValue3");
|
|
||||||
BdfDictionary discoveredDict = new BdfDictionary(discovered);
|
|
||||||
BdfDictionary groupMeta = BdfDictionary.of(
|
|
||||||
new BdfEntry(GROUP_KEY_DISCOVERED, discoveredDict)
|
|
||||||
);
|
|
||||||
TransportProperties merged = new TransportProperties();
|
|
||||||
merged.putAll(fooProperties);
|
|
||||||
merged.put("fooKey3", "fooValue3");
|
|
||||||
|
|
||||||
context.checking(new DbExpectations() {{
|
|
||||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
|
||||||
oneOf(db).getContacts(txn);
|
|
||||||
will(returnValue(contacts));
|
|
||||||
// One update
|
|
||||||
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID,
|
|
||||||
MAJOR_VERSION, contact);
|
|
||||||
will(returnValue(contactGroup));
|
|
||||||
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
|
|
||||||
contactGroup.getId());
|
|
||||||
will(returnValue(messageMetadata));
|
|
||||||
oneOf(clientHelper).getMessageAsList(txn, updateId);
|
|
||||||
will(returnValue(update));
|
|
||||||
oneOf(clientHelper).parseAndValidateTransportProperties(
|
|
||||||
fooPropertiesDict);
|
|
||||||
will(returnValue(fooProperties));
|
|
||||||
oneOf(clientHelper).getGroupMetadataAsDictionary(txn,
|
|
||||||
contactGroup.getId());
|
|
||||||
will(returnValue(groupMeta));
|
|
||||||
oneOf(clientHelper).parseAndValidateTransportProperties(
|
|
||||||
discoveredDict);
|
|
||||||
will(returnValue(discovered));
|
|
||||||
}});
|
|
||||||
|
|
||||||
TransportPropertyManagerImpl t = createInstance();
|
|
||||||
Map<ContactId, TransportProperties> properties =
|
|
||||||
t.getRemoteProperties(new TransportId("foo"));
|
|
||||||
assertEquals(merged, properties.get(contact.getId()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMergingUnchangedPropertiesDoesNotCreateUpdate()
|
public void testMergingUnchangedPropertiesDoesNotCreateUpdate()
|
||||||
throws Exception {
|
throws Exception {
|
||||||
@@ -536,9 +470,9 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
|||||||
MessageId updateId = new MessageId(getRandomId());
|
MessageId updateId = new MessageId(getRandomId());
|
||||||
Map<MessageId, BdfDictionary> messageMetadata = singletonMap(updateId,
|
Map<MessageId, BdfDictionary> messageMetadata = singletonMap(updateId,
|
||||||
BdfDictionary.of(
|
BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
new BdfEntry("transportId", "foo"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
new BdfEntry("version", 1),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, true)
|
new BdfEntry("local", true)
|
||||||
));
|
));
|
||||||
BdfList update = BdfList.of("foo", 1, fooPropertiesDict);
|
BdfList update = BdfList.of("foo", 1, fooPropertiesDict);
|
||||||
|
|
||||||
@@ -571,7 +505,7 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
|||||||
// There are no existing properties to merge with
|
// There are no existing properties to merge with
|
||||||
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
|
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
|
||||||
localGroup.getId());
|
localGroup.getId());
|
||||||
will(returnValue(emptyMap()));
|
will(returnValue(Collections.emptyMap()));
|
||||||
// Store the new properties in the local group, version 1
|
// Store the new properties in the local group, version 1
|
||||||
expectStoreMessage(txn, localGroup.getId(), "foo",
|
expectStoreMessage(txn, localGroup.getId(), "foo",
|
||||||
fooPropertiesDict, 1, true, false);
|
fooPropertiesDict, 1, true, false);
|
||||||
@@ -583,7 +517,7 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
|||||||
will(returnValue(contactGroup));
|
will(returnValue(contactGroup));
|
||||||
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
|
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
|
||||||
contactGroup.getId());
|
contactGroup.getId());
|
||||||
will(returnValue(emptyMap()));
|
will(returnValue(Collections.emptyMap()));
|
||||||
expectStoreMessage(txn, contactGroup.getId(), "foo",
|
expectStoreMessage(txn, contactGroup.getId(), "foo",
|
||||||
fooPropertiesDict, 1, true, true);
|
fooPropertiesDict, 1, true, true);
|
||||||
}});
|
}});
|
||||||
@@ -598,9 +532,9 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
|||||||
Contact contact = getContact();
|
Contact contact = getContact();
|
||||||
Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION);
|
Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION);
|
||||||
BdfDictionary oldMetadata = BdfDictionary.of(
|
BdfDictionary oldMetadata = BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
new BdfEntry("transportId", "foo"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
new BdfEntry("version", 1),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, true)
|
new BdfEntry("local", true)
|
||||||
);
|
);
|
||||||
MessageId localGroupUpdateId = new MessageId(getRandomId());
|
MessageId localGroupUpdateId = new MessageId(getRandomId());
|
||||||
Map<MessageId, BdfDictionary> localGroupMessageMetadata =
|
Map<MessageId, BdfDictionary> localGroupMessageMetadata =
|
||||||
@@ -655,14 +589,14 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
|||||||
// The latest update for transport "foo" should be returned
|
// The latest update for transport "foo" should be returned
|
||||||
MessageId fooVersion999 = new MessageId(getRandomId());
|
MessageId fooVersion999 = new MessageId(getRandomId());
|
||||||
messageMetadata.put(fooVersion999, BdfDictionary.of(
|
messageMetadata.put(fooVersion999, BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
new BdfEntry("transportId", "foo"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 999)
|
new BdfEntry("version", 999)
|
||||||
));
|
));
|
||||||
// The latest update for transport "bar" should be returned
|
// The latest update for transport "bar" should be returned
|
||||||
MessageId barVersion3 = new MessageId(getRandomId());
|
MessageId barVersion3 = new MessageId(getRandomId());
|
||||||
messageMetadata.put(barVersion3, BdfDictionary.of(
|
messageMetadata.put(barVersion3, BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "bar"),
|
new BdfEntry("transportId", "bar"),
|
||||||
new BdfEntry(MSG_KEY_VERSION, 3)
|
new BdfEntry("version", 3)
|
||||||
));
|
));
|
||||||
BdfList fooUpdate = BdfList.of("foo", 999, fooPropertiesDict);
|
BdfList fooUpdate = BdfList.of("foo", 999, fooPropertiesDict);
|
||||||
BdfList barUpdate = BdfList.of("bar", 3, barPropertiesDict);
|
BdfList barUpdate = BdfList.of("bar", 3, barPropertiesDict);
|
||||||
@@ -693,9 +627,9 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
|||||||
Message message = getMessage(g);
|
Message message = getMessage(g);
|
||||||
long timestamp = message.getTimestamp();
|
long timestamp = message.getTimestamp();
|
||||||
BdfDictionary meta = BdfDictionary.of(
|
BdfDictionary meta = BdfDictionary.of(
|
||||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, transportId),
|
new BdfEntry("transportId", transportId),
|
||||||
new BdfEntry(MSG_KEY_VERSION, version),
|
new BdfEntry("version", version),
|
||||||
new BdfEntry(MSG_KEY_LOCAL, local)
|
new BdfEntry("local", local)
|
||||||
);
|
);
|
||||||
|
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|||||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -38,11 +37,6 @@ public class TestDuplexTransportConnection
|
|||||||
return writer;
|
return writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public TransportProperties getRemoteProperties() {
|
|
||||||
return new TransportProperties();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates and returns a pair of TestDuplexTransportConnections that are
|
* Creates and returns a pair of TestDuplexTransportConnections that are
|
||||||
* connected to each other.
|
* connected to each other.
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ dependencies {
|
|||||||
implementation fileTree(dir: 'libs', include: '*.jar')
|
implementation fileTree(dir: 'libs', include: '*.jar')
|
||||||
implementation 'net.java.dev.jna:jna:4.5.2'
|
implementation 'net.java.dev.jna:jna:4.5.2'
|
||||||
implementation 'net.java.dev.jna:jna-platform:4.5.2'
|
implementation 'net.java.dev.jna:jna-platform:4.5.2'
|
||||||
tor 'org.briarproject:tor:0.3.5.10@zip'
|
tor 'org.briarproject:tor:0.3.5.9@zip'
|
||||||
tor 'org.briarproject:obfs4proxy:0.0.7@zip'
|
tor 'org.briarproject:obfs4proxy:0.0.7@zip'
|
||||||
|
|
||||||
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
|
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ dependencyVerification {
|
|||||||
'org.apache.ant:ant:1.9.4:ant-1.9.4.jar:649ae0730251de07b8913f49286d46bba7b92d47c5f332610aa426c4f02161d8',
|
'org.apache.ant:ant:1.9.4:ant-1.9.4.jar:649ae0730251de07b8913f49286d46bba7b92d47c5f332610aa426c4f02161d8',
|
||||||
'org.beanshell:bsh:1.3.0:bsh-1.3.0.jar:9b04edc75d19db54f1b4e8b5355e9364384c6cf71eb0a1b9724c159d779879f8',
|
'org.beanshell:bsh:1.3.0:bsh-1.3.0.jar:9b04edc75d19db54f1b4e8b5355e9364384c6cf71eb0a1b9724c159d779879f8',
|
||||||
'org.briarproject:obfs4proxy:0.0.7:obfs4proxy-0.0.7.zip:5b2f693262ce43a7e130f7cc7d5d1617925330640a2eb6d71085e95df8ee0642',
|
'org.briarproject:obfs4proxy:0.0.7:obfs4proxy-0.0.7.zip:5b2f693262ce43a7e130f7cc7d5d1617925330640a2eb6d71085e95df8ee0642',
|
||||||
'org.briarproject:tor:0.3.5.10:tor-0.3.5.10.zip:7b387d3523ae8af289c23be59dc4c64ec5d3721385d7825a09705095e3318d5c',
|
'org.briarproject:tor:0.3.5.9:tor-0.3.5.9.zip:6c3994b129db019cc23caaf50d6b4383903c40d05fbc47fc94211170a3e5d38c',
|
||||||
'org.checkerframework:checker-compat-qual:2.5.3:checker-compat-qual-2.5.3.jar:d76b9afea61c7c082908023f0cbc1427fab9abd2df915c8b8a3e7a509bccbc6d',
|
'org.checkerframework:checker-compat-qual:2.5.3:checker-compat-qual-2.5.3.jar:d76b9afea61c7c082908023f0cbc1427fab9abd2df915c8b8a3e7a509bccbc6d',
|
||||||
'org.checkerframework:checker-qual:2.5.2:checker-qual-2.5.2.jar:64b02691c8b9d4e7700f8ee2e742dce7ea2c6e81e662b7522c9ee3bf568c040a',
|
'org.checkerframework:checker-qual:2.5.2:checker-qual-2.5.2.jar:64b02691c8b9d4e7700f8ee2e742dce7ea2c6e81e662b7522c9ee3bf568c040a',
|
||||||
'org.codehaus.mojo:animal-sniffer-annotations:1.17:animal-sniffer-annotations-1.17.jar:92654f493ecfec52082e76354f0ebf87648dc3d5cec2e3c3cdb947c016747a53',
|
'org.codehaus.mojo:animal-sniffer-annotations:1.17:animal-sniffer-annotations-1.17.jar:92654f493ecfec52082e76354f0ebf87648dc3d5cec2e3c3cdb947c016747a53',
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ android {
|
|||||||
defaultConfig {
|
defaultConfig {
|
||||||
minSdkVersion 16
|
minSdkVersion 16
|
||||||
targetSdkVersion 28
|
targetSdkVersion 28
|
||||||
versionCode 10207
|
versionCode 10205
|
||||||
versionName "1.2.7"
|
versionName "1.2.5"
|
||||||
applicationId "org.briarproject.briar.android"
|
applicationId "org.briarproject.briar.android"
|
||||||
buildConfigField "String", "GitHash",
|
buildConfigField "String", "GitHash",
|
||||||
"\"${getStdout(['git', 'rev-parse', '--short=7', 'HEAD'], 'No commit hash')}\""
|
"\"${getStdout(['git', 'rev-parse', '--short=7', 'HEAD'], 'No commit hash')}\""
|
||||||
@@ -98,7 +98,7 @@ dependencies {
|
|||||||
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
|
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
|
||||||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
|
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
|
||||||
implementation 'com.google.android.material:material:1.1.0-beta01'
|
implementation 'com.google.android.material:material:1.1.0-beta01'
|
||||||
implementation 'androidx.recyclerview:recyclerview-selection:1.1.0-rc01'
|
implementation 'androidx.recyclerview:recyclerview-selection:1.0.0'
|
||||||
|
|
||||||
implementation 'ch.acra:acra:4.11'
|
implementation 'ch.acra:acra:4.11'
|
||||||
implementation 'info.guardianproject.panic:panic:1.0'
|
implementation 'info.guardianproject.panic:panic:1.0'
|
||||||
|
|||||||
@@ -13,8 +13,9 @@ import java.util.Random;
|
|||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
|
|
||||||
import static androidx.test.InstrumentationRegistry.getContext;
|
import static androidx.test.InstrumentationRegistry.getContext;
|
||||||
|
import static org.briarproject.briar.android.attachment.AttachmentItem.State.AVAILABLE;
|
||||||
|
import static org.briarproject.briar.android.attachment.AttachmentItem.State.ERROR;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
@RunWith(AndroidJUnit4.class)
|
||||||
@@ -27,7 +28,7 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
|
|
||||||
private final ImageHelper imageHelper = new ImageHelperImpl();
|
private final ImageHelper imageHelper = new ImageHelperImpl();
|
||||||
private final AttachmentRetriever retriever =
|
private final AttachmentRetriever retriever =
|
||||||
new AttachmentRetrieverImpl(null, dimensions, imageHelper,
|
new AttachmentRetrieverImpl(null, null, dimensions, imageHelper,
|
||||||
new ImageSizeCalculator(imageHelper));
|
new ImageSizeCalculator(imageHelper));
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -35,7 +36,7 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||||
InputStream is = getAssetInputStream("kitten_small.jpg");
|
InputStream is = getAssetInputStream("kitten_small.jpg");
|
||||||
Attachment a = new Attachment(h, is);
|
Attachment a = new Attachment(h, is);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
AttachmentItem item = retriever.createAttachmentItem(a, true);
|
||||||
assertEquals(msgId, item.getMessageId());
|
assertEquals(msgId, item.getMessageId());
|
||||||
assertEquals(160, item.getWidth());
|
assertEquals(160, item.getWidth());
|
||||||
assertEquals(240, item.getHeight());
|
assertEquals(240, item.getHeight());
|
||||||
@@ -43,7 +44,7 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
assertEquals(240, item.getThumbnailHeight());
|
assertEquals(240, item.getThumbnailHeight());
|
||||||
assertEquals("image/jpeg", item.getMimeType());
|
assertEquals("image/jpeg", item.getMimeType());
|
||||||
assertJpgOrJpeg(item.getExtension());
|
assertJpgOrJpeg(item.getExtension());
|
||||||
assertFalse(item.hasError());
|
assertEquals(AVAILABLE, item.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -51,7 +52,7 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||||
InputStream is = getAssetInputStream("kitten_original.jpg");
|
InputStream is = getAssetInputStream("kitten_original.jpg");
|
||||||
Attachment a = new Attachment(h, is);
|
Attachment a = new Attachment(h, is);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
AttachmentItem item = retriever.createAttachmentItem(a, true);
|
||||||
assertEquals(msgId, item.getMessageId());
|
assertEquals(msgId, item.getMessageId());
|
||||||
assertEquals(1728, item.getWidth());
|
assertEquals(1728, item.getWidth());
|
||||||
assertEquals(2592, item.getHeight());
|
assertEquals(2592, item.getHeight());
|
||||||
@@ -59,7 +60,7 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
assertEquals(dimensions.maxHeight, item.getThumbnailHeight());
|
assertEquals(dimensions.maxHeight, item.getThumbnailHeight());
|
||||||
assertEquals("image/jpeg", item.getMimeType());
|
assertEquals("image/jpeg", item.getMimeType());
|
||||||
assertJpgOrJpeg(item.getExtension());
|
assertJpgOrJpeg(item.getExtension());
|
||||||
assertFalse(item.hasError());
|
assertEquals(AVAILABLE, item.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -67,7 +68,7 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/png");
|
AttachmentHeader h = new AttachmentHeader(msgId, "image/png");
|
||||||
InputStream is = getAssetInputStream("kitten.png");
|
InputStream is = getAssetInputStream("kitten.png");
|
||||||
Attachment a = new Attachment(h, is);
|
Attachment a = new Attachment(h, is);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
AttachmentItem item = retriever.createAttachmentItem(a, true);
|
||||||
assertEquals(msgId, item.getMessageId());
|
assertEquals(msgId, item.getMessageId());
|
||||||
assertEquals(737, item.getWidth());
|
assertEquals(737, item.getWidth());
|
||||||
assertEquals(510, item.getHeight());
|
assertEquals(510, item.getHeight());
|
||||||
@@ -75,7 +76,7 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
assertEquals(138, item.getThumbnailHeight());
|
assertEquals(138, item.getThumbnailHeight());
|
||||||
assertEquals("image/png", item.getMimeType());
|
assertEquals("image/png", item.getMimeType());
|
||||||
assertEquals("png", item.getExtension());
|
assertEquals("png", item.getExtension());
|
||||||
assertFalse(item.hasError());
|
assertEquals(AVAILABLE, item.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -83,14 +84,14 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
||||||
InputStream is = getAssetInputStream("uber.gif");
|
InputStream is = getAssetInputStream("uber.gif");
|
||||||
Attachment a = new Attachment(h, is);
|
Attachment a = new Attachment(h, is);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
AttachmentItem item = retriever.createAttachmentItem(a, true);
|
||||||
assertEquals(1, item.getWidth());
|
assertEquals(1, item.getWidth());
|
||||||
assertEquals(1, item.getHeight());
|
assertEquals(1, item.getHeight());
|
||||||
assertEquals(dimensions.minHeight, item.getThumbnailWidth());
|
assertEquals(dimensions.minHeight, item.getThumbnailWidth());
|
||||||
assertEquals(dimensions.minHeight, item.getThumbnailHeight());
|
assertEquals(dimensions.minHeight, item.getThumbnailHeight());
|
||||||
assertEquals("image/gif", item.getMimeType());
|
assertEquals("image/gif", item.getMimeType());
|
||||||
assertEquals("gif", item.getExtension());
|
assertEquals("gif", item.getExtension());
|
||||||
assertFalse(item.hasError());
|
assertEquals(AVAILABLE, item.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -98,14 +99,14 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||||
InputStream is = getAssetInputStream("lottapixel.jpg");
|
InputStream is = getAssetInputStream("lottapixel.jpg");
|
||||||
Attachment a = new Attachment(h, is);
|
Attachment a = new Attachment(h, is);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
AttachmentItem item = retriever.createAttachmentItem(a, true);
|
||||||
assertEquals(64250, item.getWidth());
|
assertEquals(64250, item.getWidth());
|
||||||
assertEquals(64250, item.getHeight());
|
assertEquals(64250, item.getHeight());
|
||||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||||
assertEquals(dimensions.maxWidth, item.getThumbnailHeight());
|
assertEquals(dimensions.maxWidth, item.getThumbnailHeight());
|
||||||
assertEquals("image/jpeg", item.getMimeType());
|
assertEquals("image/jpeg", item.getMimeType());
|
||||||
assertJpgOrJpeg(item.getExtension());
|
assertJpgOrJpeg(item.getExtension());
|
||||||
assertFalse(item.hasError());
|
assertEquals(AVAILABLE, item.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -113,14 +114,14 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/png");
|
AttachmentHeader h = new AttachmentHeader(msgId, "image/png");
|
||||||
InputStream is = getAssetInputStream("image_io_crash.png");
|
InputStream is = getAssetInputStream("image_io_crash.png");
|
||||||
Attachment a = new Attachment(h, is);
|
Attachment a = new Attachment(h, is);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
AttachmentItem item = retriever.createAttachmentItem(a, true);
|
||||||
assertEquals(1184, item.getWidth());
|
assertEquals(1184, item.getWidth());
|
||||||
assertEquals(448, item.getHeight());
|
assertEquals(448, item.getHeight());
|
||||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||||
assertEquals(dimensions.minHeight, item.getThumbnailHeight());
|
assertEquals(dimensions.minHeight, item.getThumbnailHeight());
|
||||||
assertEquals("image/png", item.getMimeType());
|
assertEquals("image/png", item.getMimeType());
|
||||||
assertEquals("png", item.getExtension());
|
assertEquals("png", item.getExtension());
|
||||||
assertFalse(item.hasError());
|
assertEquals(AVAILABLE, item.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -128,14 +129,14 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
||||||
InputStream is = getAssetInputStream("gimp_crash.gif");
|
InputStream is = getAssetInputStream("gimp_crash.gif");
|
||||||
Attachment a = new Attachment(h, is);
|
Attachment a = new Attachment(h, is);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
AttachmentItem item = retriever.createAttachmentItem(a, true);
|
||||||
assertEquals(1, item.getWidth());
|
assertEquals(1, item.getWidth());
|
||||||
assertEquals(1, item.getHeight());
|
assertEquals(1, item.getHeight());
|
||||||
assertEquals(dimensions.minHeight, item.getThumbnailWidth());
|
assertEquals(dimensions.minHeight, item.getThumbnailWidth());
|
||||||
assertEquals(dimensions.minHeight, item.getThumbnailHeight());
|
assertEquals(dimensions.minHeight, item.getThumbnailHeight());
|
||||||
assertEquals("image/gif", item.getMimeType());
|
assertEquals("image/gif", item.getMimeType());
|
||||||
assertEquals("gif", item.getExtension());
|
assertEquals("gif", item.getExtension());
|
||||||
assertFalse(item.hasError());
|
assertEquals(AVAILABLE, item.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -143,14 +144,14 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
||||||
InputStream is = getAssetInputStream("opti_png_afl.gif");
|
InputStream is = getAssetInputStream("opti_png_afl.gif");
|
||||||
Attachment a = new Attachment(h, is);
|
Attachment a = new Attachment(h, is);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
AttachmentItem item = retriever.createAttachmentItem(a, true);
|
||||||
assertEquals(32, item.getWidth());
|
assertEquals(32, item.getWidth());
|
||||||
assertEquals(32, item.getHeight());
|
assertEquals(32, item.getHeight());
|
||||||
assertEquals(dimensions.minHeight, item.getThumbnailWidth());
|
assertEquals(dimensions.minHeight, item.getThumbnailWidth());
|
||||||
assertEquals(dimensions.minHeight, item.getThumbnailHeight());
|
assertEquals(dimensions.minHeight, item.getThumbnailHeight());
|
||||||
assertEquals("image/gif", item.getMimeType());
|
assertEquals("image/gif", item.getMimeType());
|
||||||
assertEquals("gif", item.getExtension());
|
assertEquals("gif", item.getExtension());
|
||||||
assertFalse(item.hasError());
|
assertEquals(AVAILABLE, item.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -158,8 +159,8 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||||
InputStream is = getAssetInputStream("libraw_error.jpg");
|
InputStream is = getAssetInputStream("libraw_error.jpg");
|
||||||
Attachment a = new Attachment(h, is);
|
Attachment a = new Attachment(h, is);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
AttachmentItem item = retriever.createAttachmentItem(a, true);
|
||||||
assertTrue(item.hasError());
|
assertEquals(ERROR, item.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -167,14 +168,14 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
||||||
InputStream is = getAssetInputStream("animated.gif");
|
InputStream is = getAssetInputStream("animated.gif");
|
||||||
Attachment a = new Attachment(h, is);
|
Attachment a = new Attachment(h, is);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
AttachmentItem item = retriever.createAttachmentItem(a, true);
|
||||||
assertEquals(65535, item.getWidth());
|
assertEquals(65535, item.getWidth());
|
||||||
assertEquals(65535, item.getHeight());
|
assertEquals(65535, item.getHeight());
|
||||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||||
assertEquals(dimensions.maxWidth, item.getThumbnailHeight());
|
assertEquals(dimensions.maxWidth, item.getThumbnailHeight());
|
||||||
assertEquals("image/gif", item.getMimeType());
|
assertEquals("image/gif", item.getMimeType());
|
||||||
assertEquals("gif", item.getExtension());
|
assertEquals("gif", item.getExtension());
|
||||||
assertFalse(item.hasError());
|
assertEquals(AVAILABLE, item.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -182,14 +183,14 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
||||||
InputStream is = getAssetInputStream("animated2.gif");
|
InputStream is = getAssetInputStream("animated2.gif");
|
||||||
Attachment a = new Attachment(h, is);
|
Attachment a = new Attachment(h, is);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
AttachmentItem item = retriever.createAttachmentItem(a, true);
|
||||||
assertEquals(10000, item.getWidth());
|
assertEquals(10000, item.getWidth());
|
||||||
assertEquals(10000, item.getHeight());
|
assertEquals(10000, item.getHeight());
|
||||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||||
assertEquals(dimensions.maxWidth, item.getThumbnailHeight());
|
assertEquals(dimensions.maxWidth, item.getThumbnailHeight());
|
||||||
assertEquals("image/gif", item.getMimeType());
|
assertEquals("image/gif", item.getMimeType());
|
||||||
assertEquals("gif", item.getExtension());
|
assertEquals("gif", item.getExtension());
|
||||||
assertFalse(item.hasError());
|
assertEquals(AVAILABLE, item.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -197,14 +198,14 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
||||||
InputStream is = getAssetInputStream("error_large.gif");
|
InputStream is = getAssetInputStream("error_large.gif");
|
||||||
Attachment a = new Attachment(h, is);
|
Attachment a = new Attachment(h, is);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
AttachmentItem item = retriever.createAttachmentItem(a, true);
|
||||||
assertEquals(16384, item.getWidth());
|
assertEquals(16384, item.getWidth());
|
||||||
assertEquals(16384, item.getHeight());
|
assertEquals(16384, item.getHeight());
|
||||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||||
assertEquals(dimensions.maxWidth, item.getThumbnailHeight());
|
assertEquals(dimensions.maxWidth, item.getThumbnailHeight());
|
||||||
assertEquals("image/gif", item.getMimeType());
|
assertEquals("image/gif", item.getMimeType());
|
||||||
assertEquals("gif", item.getExtension());
|
assertEquals("gif", item.getExtension());
|
||||||
assertFalse(item.hasError());
|
assertEquals(AVAILABLE, item.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -212,14 +213,14 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||||
InputStream is = getAssetInputStream("error_high.jpg");
|
InputStream is = getAssetInputStream("error_high.jpg");
|
||||||
Attachment a = new Attachment(h, is);
|
Attachment a = new Attachment(h, is);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
AttachmentItem item = retriever.createAttachmentItem(a, true);
|
||||||
assertEquals(1, item.getWidth());
|
assertEquals(1, item.getWidth());
|
||||||
assertEquals(10000, item.getHeight());
|
assertEquals(10000, item.getHeight());
|
||||||
assertEquals(dimensions.minWidth, item.getThumbnailWidth());
|
assertEquals(dimensions.minWidth, item.getThumbnailWidth());
|
||||||
assertEquals(dimensions.maxHeight, item.getThumbnailHeight());
|
assertEquals(dimensions.maxHeight, item.getThumbnailHeight());
|
||||||
assertEquals("image/jpeg", item.getMimeType());
|
assertEquals("image/jpeg", item.getMimeType());
|
||||||
assertJpgOrJpeg(item.getExtension());
|
assertJpgOrJpeg(item.getExtension());
|
||||||
assertFalse(item.hasError());
|
assertEquals(AVAILABLE, item.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -227,14 +228,14 @@ public class AttachmentRetrieverIntegrationTest {
|
|||||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||||
InputStream is = getAssetInputStream("error_wide.jpg");
|
InputStream is = getAssetInputStream("error_wide.jpg");
|
||||||
Attachment a = new Attachment(h, is);
|
Attachment a = new Attachment(h, is);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
AttachmentItem item = retriever.createAttachmentItem(a, true);
|
||||||
assertEquals(1920, item.getWidth());
|
assertEquals(1920, item.getWidth());
|
||||||
assertEquals(1, item.getHeight());
|
assertEquals(1, item.getHeight());
|
||||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||||
assertEquals(dimensions.minHeight, item.getThumbnailHeight());
|
assertEquals(dimensions.minHeight, item.getThumbnailHeight());
|
||||||
assertEquals("image/jpeg", item.getMimeType());
|
assertEquals("image/jpeg", item.getMimeType());
|
||||||
assertJpgOrJpeg(item.getExtension());
|
assertJpgOrJpeg(item.getExtension());
|
||||||
assertFalse(item.hasError());
|
assertEquals(AVAILABLE, item.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
private InputStream getAssetInputStream(String name) throws Exception {
|
private InputStream getAssetInputStream(String name) throws Exception {
|
||||||
|
|||||||
@@ -28,9 +28,7 @@ import static android.security.keystore.KeyProperties.PURPOSE_SIGN;
|
|||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
|
||||||
|
|
||||||
@RequiresApi(23)
|
@RequiresApi(23)
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
@@ -81,10 +79,7 @@ class AndroidKeyStrengthener implements KeyStrengthener {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
} catch (GeneralSecurityException e) {
|
} catch (GeneralSecurityException | IOException e) {
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
return false;
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ import org.briarproject.bramble.util.AndroidUtils;
|
|||||||
import org.briarproject.bramble.util.StringUtils;
|
import org.briarproject.bramble.util.StringUtils;
|
||||||
import org.briarproject.briar.android.account.LockManagerImpl;
|
import org.briarproject.briar.android.account.LockManagerImpl;
|
||||||
import org.briarproject.briar.android.keyagreement.ContactExchangeModule;
|
import org.briarproject.briar.android.keyagreement.ContactExchangeModule;
|
||||||
import org.briarproject.briar.android.login.LoginModule;
|
|
||||||
import org.briarproject.briar.android.viewmodel.ViewModelModule;
|
import org.briarproject.briar.android.viewmodel.ViewModelModule;
|
||||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||||
import org.briarproject.briar.api.android.DozeWatchdog;
|
import org.briarproject.briar.api.android.DozeWatchdog;
|
||||||
@@ -65,11 +64,7 @@ import static org.briarproject.bramble.api.reporting.ReportingConstants.DEV_ONIO
|
|||||||
import static org.briarproject.bramble.api.reporting.ReportingConstants.DEV_PUBLIC_KEY_HEX;
|
import static org.briarproject.bramble.api.reporting.ReportingConstants.DEV_PUBLIC_KEY_HEX;
|
||||||
import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
||||||
|
|
||||||
@Module(includes = {
|
@Module(includes = {ContactExchangeModule.class, ViewModelModule.class})
|
||||||
ContactExchangeModule.class,
|
|
||||||
LoginModule.class,
|
|
||||||
ViewModelModule.class
|
|
||||||
})
|
|
||||||
public class AppModule {
|
public class AppModule {
|
||||||
|
|
||||||
static class EagerSingletons {
|
static class EagerSingletons {
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import org.briarproject.briar.android.controller.BriarController;
|
|||||||
import org.briarproject.briar.android.controller.BriarControllerImpl;
|
import org.briarproject.briar.android.controller.BriarControllerImpl;
|
||||||
import org.briarproject.briar.android.controller.DbController;
|
import org.briarproject.briar.android.controller.DbController;
|
||||||
import org.briarproject.briar.android.controller.DbControllerImpl;
|
import org.briarproject.briar.android.controller.DbControllerImpl;
|
||||||
|
import org.briarproject.briar.android.login.ChangePasswordController;
|
||||||
|
import org.briarproject.briar.android.login.ChangePasswordControllerImpl;
|
||||||
import org.briarproject.briar.android.navdrawer.NavDrawerController;
|
import org.briarproject.briar.android.navdrawer.NavDrawerController;
|
||||||
import org.briarproject.briar.android.navdrawer.NavDrawerControllerImpl;
|
import org.briarproject.briar.android.navdrawer.NavDrawerControllerImpl;
|
||||||
|
|
||||||
@@ -44,6 +46,13 @@ public class ActivityModule {
|
|||||||
return setupController;
|
return setupController;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ActivityScope
|
||||||
|
@Provides
|
||||||
|
ChangePasswordController providePasswordController(
|
||||||
|
ChangePasswordControllerImpl passwordController) {
|
||||||
|
return passwordController;
|
||||||
|
}
|
||||||
|
|
||||||
@ActivityScope
|
@ActivityScope
|
||||||
@Provides
|
@Provides
|
||||||
protected BriarController provideBriarController(
|
protected BriarController provideBriarController(
|
||||||
@@ -71,4 +80,5 @@ public class ActivityModule {
|
|||||||
BriarServiceConnection provideBriarServiceConnection() {
|
BriarServiceConnection provideBriarServiceConnection() {
|
||||||
return new BriarServiceConnection();
|
return new BriarServiceConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,9 +92,6 @@ public abstract class BaseActivity extends AppCompatActivity
|
|||||||
.build();
|
.build();
|
||||||
injectActivity(activityComponent);
|
injectActivity(activityComponent);
|
||||||
super.onCreate(state);
|
super.onCreate(state);
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Creating " + getClass().getSimpleName());
|
|
||||||
}
|
|
||||||
|
|
||||||
// WARNING: When removing this or making it possible to turn it off,
|
// WARNING: When removing this or making it possible to turn it off,
|
||||||
// we need a solution for the app lock feature.
|
// we need a solution for the app lock feature.
|
||||||
@@ -130,9 +127,8 @@ public abstract class BaseActivity extends AppCompatActivity
|
|||||||
@Override
|
@Override
|
||||||
protected void onStart() {
|
protected void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Starting " + getClass().getSimpleName());
|
LOG.info("Starting " + this.getClass().getSimpleName());
|
||||||
}
|
|
||||||
for (ActivityLifecycleController alc : lifecycleControllers) {
|
for (ActivityLifecycleController alc : lifecycleControllers) {
|
||||||
alc.onActivityStart();
|
alc.onActivityStart();
|
||||||
}
|
}
|
||||||
@@ -148,28 +144,11 @@ public abstract class BaseActivity extends AppCompatActivity
|
|||||||
return (ScreenFilterDialogFragment) f;
|
return (ScreenFilterDialogFragment) f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onResume() {
|
|
||||||
super.onResume();
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Resuming " + getClass().getSimpleName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPause() {
|
|
||||||
super.onPause();
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Pausing " + getClass().getSimpleName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onStop() {
|
protected void onStop() {
|
||||||
super.onStop();
|
super.onStop();
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Stopping " + getClass().getSimpleName());
|
LOG.info("Stopping " + this.getClass().getSimpleName());
|
||||||
}
|
|
||||||
for (ActivityLifecycleController alc : lifecycleControllers) {
|
for (ActivityLifecycleController alc : lifecycleControllers) {
|
||||||
alc.onActivityStop();
|
alc.onActivityStop();
|
||||||
}
|
}
|
||||||
@@ -224,9 +203,6 @@ public abstract class BaseActivity extends AppCompatActivity
|
|||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Destroying " + getClass().getSimpleName());
|
|
||||||
}
|
|
||||||
destroyed = true;
|
destroyed = true;
|
||||||
for (ActivityLifecycleController alc : lifecycleControllers) {
|
for (ActivityLifecycleController alc : lifecycleControllers) {
|
||||||
alc.onActivityDestroy();
|
alc.onActivityDestroy();
|
||||||
|
|||||||
@@ -95,14 +95,12 @@ public abstract class BriarActivity extends BaseActivity {
|
|||||||
// Also check that the activity isn't finishing already.
|
// Also check that the activity isn't finishing already.
|
||||||
// This is possible if we finished in onActivityResult().
|
// This is possible if we finished in onActivityResult().
|
||||||
// Launching another StartupActivity would cause a loop.
|
// Launching another StartupActivity would cause a loop.
|
||||||
LOG.info("Not signed in, launching StartupActivity");
|
|
||||||
Intent i = new Intent(this, StartupActivity.class);
|
Intent i = new Intent(this, StartupActivity.class);
|
||||||
startActivityForResult(i, REQUEST_PASSWORD);
|
startActivityForResult(i, REQUEST_PASSWORD);
|
||||||
} else if (lockManager.isLocked() && !isFinishing()) {
|
} else if (lockManager.isLocked() && !isFinishing()) {
|
||||||
// Also check that the activity isn't finishing already.
|
// Also check that the activity isn't finishing already.
|
||||||
// This is possible if we finished in onActivityResult().
|
// This is possible if we finished in onActivityResult().
|
||||||
// Launching another UnlockActivity would cause a loop.
|
// Launching another UnlockActivity would cause a loop.
|
||||||
LOG.info("Locked, launching UnlockActivity");
|
|
||||||
Intent i = new Intent(this, UnlockActivity.class);
|
Intent i = new Intent(this, UnlockActivity.class);
|
||||||
startActivityForResult(i, REQUEST_UNLOCK);
|
startActivityForResult(i, REQUEST_UNLOCK);
|
||||||
} else if (SDK_INT >= 23) {
|
} else if (SDK_INT >= 23) {
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import androidx.lifecycle.MutableLiveData;
|
|||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
import static org.briarproject.briar.android.attachment.AttachmentItem.State.ERROR;
|
||||||
import static org.briarproject.briar.android.util.UiUtils.observeForeverOnce;
|
import static org.briarproject.briar.android.util.UiUtils.observeForeverOnce;
|
||||||
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_IMAGE_SIZE;
|
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_IMAGE_SIZE;
|
||||||
|
|
||||||
@@ -109,8 +110,8 @@ class AttachmentCreatorImpl implements AttachmentCreator {
|
|||||||
// get and cache AttachmentItem for ImagePreview
|
// get and cache AttachmentItem for ImagePreview
|
||||||
try {
|
try {
|
||||||
Attachment a = retriever.getMessageAttachment(h);
|
Attachment a = retriever.getMessageAttachment(h);
|
||||||
AttachmentItem item = retriever.getAttachmentItem(a, needsSize);
|
AttachmentItem item = retriever.createAttachmentItem(a, needsSize);
|
||||||
if (item.hasError()) throw new IOException();
|
if (item.getState() == ERROR) throw new IOException();
|
||||||
AttachmentItemResult itemResult =
|
AttachmentItemResult itemResult =
|
||||||
new AttachmentItemResult(uri, item);
|
new AttachmentItemResult(uri, item);
|
||||||
itemResults.add(itemResult);
|
itemResults.add(itemResult);
|
||||||
@@ -167,13 +168,6 @@ class AttachmentCreatorImpl implements AttachmentCreator {
|
|||||||
@Override
|
@Override
|
||||||
@UiThread
|
@UiThread
|
||||||
public void onAttachmentsSent(MessageId id) {
|
public void onAttachmentsSent(MessageId id) {
|
||||||
List<AttachmentItem> items = new ArrayList<>(itemResults.size());
|
|
||||||
for (AttachmentItemResult itemResult : itemResults) {
|
|
||||||
// check if we are trying to send attachment items with errors
|
|
||||||
if (itemResult.getItem() == null) throw new IllegalStateException();
|
|
||||||
items.add(itemResult.getItem());
|
|
||||||
}
|
|
||||||
retriever.cachePut(id, items);
|
|
||||||
resetState();
|
resetState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,24 +7,33 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|||||||
import org.briarproject.bramble.api.sync.MessageId;
|
import org.briarproject.bramble.api.sync.MessageId;
|
||||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
|
import static java.lang.System.arraycopy;
|
||||||
import static java.util.Objects.requireNonNull;
|
import static java.util.Objects.requireNonNull;
|
||||||
|
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||||
|
import static org.briarproject.briar.android.attachment.AttachmentItem.State.LOADING;
|
||||||
|
import static org.briarproject.briar.android.attachment.AttachmentItem.State.MISSING;
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class AttachmentItem implements Parcelable {
|
public class AttachmentItem implements Parcelable {
|
||||||
|
|
||||||
|
public enum State {
|
||||||
|
LOADING, MISSING, AVAILABLE, ERROR;
|
||||||
|
|
||||||
|
public boolean isFinal() {
|
||||||
|
return this == AVAILABLE || this == ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private final AttachmentHeader header;
|
private final AttachmentHeader header;
|
||||||
private final int width, height;
|
private final int width, height;
|
||||||
private final String extension;
|
private final String extension;
|
||||||
private final int thumbnailWidth, thumbnailHeight;
|
private final int thumbnailWidth, thumbnailHeight;
|
||||||
private final boolean hasError;
|
private final State state;
|
||||||
private final long instanceId;
|
|
||||||
|
|
||||||
public static final Creator<AttachmentItem> CREATOR =
|
public static final Creator<AttachmentItem> CREATOR =
|
||||||
new Creator<AttachmentItem>() {
|
new Creator<AttachmentItem>() {
|
||||||
@@ -39,19 +48,33 @@ public class AttachmentItem implements Parcelable {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private static final AtomicLong NEXT_INSTANCE_ID = new AtomicLong(0);
|
|
||||||
|
|
||||||
AttachmentItem(AttachmentHeader header, int width, int height,
|
AttachmentItem(AttachmentHeader header, int width, int height,
|
||||||
String extension, int thumbnailWidth, int thumbnailHeight,
|
String extension, int thumbnailWidth, int thumbnailHeight,
|
||||||
boolean hasError) {
|
State state) {
|
||||||
this.header = header;
|
this.header = header;
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.extension = extension;
|
this.extension = extension;
|
||||||
this.thumbnailWidth = thumbnailWidth;
|
this.thumbnailWidth = thumbnailWidth;
|
||||||
this.thumbnailHeight = thumbnailHeight;
|
this.thumbnailHeight = thumbnailHeight;
|
||||||
this.hasError = hasError;
|
this.state = state;
|
||||||
instanceId = NEXT_INSTANCE_ID.getAndIncrement();
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use only for {@link State MISSING} or {@link State LOADING} items.
|
||||||
|
*/
|
||||||
|
AttachmentItem(AttachmentHeader header, int width, int height,
|
||||||
|
State state) {
|
||||||
|
this(header, width, height, "", width, height, state);
|
||||||
|
if (state != MISSING && state != LOADING)
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use when the item does not need a size.
|
||||||
|
*/
|
||||||
|
AttachmentItem(AttachmentHeader header, String extension, State state) {
|
||||||
|
this(header, 0, 0, extension, 0, 0, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected AttachmentItem(Parcel in) {
|
protected AttachmentItem(Parcel in) {
|
||||||
@@ -64,8 +87,7 @@ public class AttachmentItem implements Parcelable {
|
|||||||
extension = requireNonNull(in.readString());
|
extension = requireNonNull(in.readString());
|
||||||
thumbnailWidth = in.readInt();
|
thumbnailWidth = in.readInt();
|
||||||
thumbnailHeight = in.readInt();
|
thumbnailHeight = in.readInt();
|
||||||
hasError = in.readByte() != 0;
|
state = State.valueOf(requireNonNull(in.readString()));
|
||||||
instanceId = in.readLong();
|
|
||||||
header = new AttachmentHeader(messageId, mimeType);
|
header = new AttachmentHeader(messageId, mimeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,12 +123,20 @@ public class AttachmentItem implements Parcelable {
|
|||||||
return thumbnailHeight;
|
return thumbnailHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasError() {
|
public State getState() {
|
||||||
return hasError;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTransitionName() {
|
public String getTransitionName(MessageId conversationItemId) {
|
||||||
return String.valueOf(instanceId);
|
int len = MessageId.LENGTH;
|
||||||
|
byte[] instanceId = new byte[len * 2];
|
||||||
|
arraycopy(header.getMessageId().getBytes(), 0, instanceId, 0, len);
|
||||||
|
arraycopy(conversationItemId.getBytes(), 0, instanceId, len, len);
|
||||||
|
return toHexString(instanceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean hasSize() {
|
||||||
|
return width != 0 && height != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -123,14 +153,15 @@ public class AttachmentItem implements Parcelable {
|
|||||||
dest.writeString(extension);
|
dest.writeString(extension);
|
||||||
dest.writeInt(thumbnailWidth);
|
dest.writeInt(thumbnailWidth);
|
||||||
dest.writeInt(thumbnailHeight);
|
dest.writeInt(thumbnailHeight);
|
||||||
dest.writeByte((byte) (hasError ? 1 : 0));
|
dest.writeString(state.name());
|
||||||
dest.writeLong(instanceId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(@Nullable Object o) {
|
public boolean equals(@Nullable Object o) {
|
||||||
return o instanceof AttachmentItem &&
|
return o instanceof AttachmentItem &&
|
||||||
instanceId == ((AttachmentItem) o).instanceId;
|
header.getMessageId().equals(
|
||||||
|
((AttachmentItem) o).header.getMessageId()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,29 +1,56 @@
|
|||||||
package org.briarproject.briar.android.attachment;
|
package org.briarproject.briar.android.attachment;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.sync.MessageId;
|
import org.briarproject.bramble.api.sync.MessageId;
|
||||||
import org.briarproject.briar.api.messaging.Attachment;
|
import org.briarproject.briar.api.messaging.Attachment;
|
||||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||||
|
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||||
|
import org.briarproject.briar.api.messaging.event.AttachmentReceivedEvent;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.lifecycle.LiveData;
|
||||||
|
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public interface AttachmentRetriever {
|
public interface AttachmentRetriever {
|
||||||
|
|
||||||
void cachePut(MessageId messageId, List<AttachmentItem> attachments);
|
@DatabaseExecutor
|
||||||
|
|
||||||
@Nullable
|
|
||||||
List<AttachmentItem> cacheGet(MessageId messageId);
|
|
||||||
|
|
||||||
Attachment getMessageAttachment(AttachmentHeader h) throws DbException;
|
Attachment getMessageAttachment(AttachmentHeader h) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of observable {@link LiveData}
|
||||||
|
* that get updated as the state of their {@link AttachmentItem}s changes.
|
||||||
|
*/
|
||||||
|
List<LiveData<AttachmentItem>> getAttachmentItems(
|
||||||
|
PrivateMessageHeader messageHeader);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves item size and adds the item to the cache, if available.
|
||||||
|
* <p>
|
||||||
|
* Use this to eagerly load the attachment size before it gets displayed.
|
||||||
|
* This is needed for messages containing a single attachment.
|
||||||
|
* Messages with more than one attachment use a standard size.
|
||||||
|
*/
|
||||||
|
@DatabaseExecutor
|
||||||
|
void cacheAttachmentItemWithSize(MessageId conversationMessageId,
|
||||||
|
AttachmentHeader h) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an {@link AttachmentItem} from the {@link Attachment}'s
|
* Creates an {@link AttachmentItem} from the {@link Attachment}'s
|
||||||
* {@link InputStream} which will be closed when this method returns.
|
* {@link InputStream} which will be closed when this method returns.
|
||||||
*/
|
*/
|
||||||
AttachmentItem getAttachmentItem(Attachment a, boolean needsSize);
|
AttachmentItem createAttachmentItem(Attachment a, boolean needsSize);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads an {@link AttachmentItem}
|
||||||
|
* that arrived via an {@link AttachmentReceivedEvent}
|
||||||
|
* and notifies the associated {@link LiveData}.
|
||||||
|
*/
|
||||||
|
@DatabaseExecutor
|
||||||
|
void loadAttachmentItem(MessageId attachmentId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,39 @@
|
|||||||
package org.briarproject.briar.android.attachment;
|
package org.briarproject.briar.android.attachment;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
|
import org.briarproject.bramble.api.db.NoSuchMessageException;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.sync.MessageId;
|
import org.briarproject.bramble.api.sync.MessageId;
|
||||||
|
import org.briarproject.briar.android.attachment.AttachmentItem.State;
|
||||||
import org.briarproject.briar.api.messaging.Attachment;
|
import org.briarproject.briar.api.messaging.Attachment;
|
||||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||||
|
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.lifecycle.LiveData;
|
||||||
|
import androidx.lifecycle.MutableLiveData;
|
||||||
|
|
||||||
|
import static java.util.Objects.requireNonNull;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
|
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
import static org.briarproject.briar.android.attachment.AttachmentItem.State.AVAILABLE;
|
||||||
|
import static org.briarproject.briar.android.attachment.AttachmentItem.State.ERROR;
|
||||||
|
import static org.briarproject.briar.android.attachment.AttachmentItem.State.LOADING;
|
||||||
|
import static org.briarproject.briar.android.attachment.AttachmentItem.State.MISSING;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class AttachmentRetrieverImpl implements AttachmentRetriever {
|
class AttachmentRetrieverImpl implements AttachmentRetriever {
|
||||||
@@ -27,6 +41,8 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
|
|||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(AttachmentRetrieverImpl.class.getName());
|
getLogger(AttachmentRetrieverImpl.class.getName());
|
||||||
|
|
||||||
|
@DatabaseExecutor
|
||||||
|
private final Executor dbExecutor;
|
||||||
private final MessagingManager messagingManager;
|
private final MessagingManager messagingManager;
|
||||||
private final ImageHelper imageHelper;
|
private final ImageHelper imageHelper;
|
||||||
private final ImageSizeCalculator imageSizeCalculator;
|
private final ImageSizeCalculator imageSizeCalculator;
|
||||||
@@ -34,13 +50,17 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
|
|||||||
private final int minWidth, maxWidth;
|
private final int minWidth, maxWidth;
|
||||||
private final int minHeight, maxHeight;
|
private final int minHeight, maxHeight;
|
||||||
|
|
||||||
private final Map<MessageId, List<AttachmentItem>> attachmentCache =
|
private final Map<MessageId, MutableLiveData<AttachmentItem>>
|
||||||
new ConcurrentHashMap<>();
|
itemsWithSize = new ConcurrentHashMap<>();
|
||||||
|
private final Map<MessageId, MutableLiveData<AttachmentItem>>
|
||||||
|
itemsWithoutSize = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
AttachmentRetrieverImpl(MessagingManager messagingManager,
|
AttachmentRetrieverImpl(@DatabaseExecutor Executor dbExecutor,
|
||||||
|
MessagingManager messagingManager,
|
||||||
AttachmentDimensions dimensions, ImageHelper imageHelper,
|
AttachmentDimensions dimensions, ImageHelper imageHelper,
|
||||||
ImageSizeCalculator imageSizeCalculator) {
|
ImageSizeCalculator imageSizeCalculator) {
|
||||||
|
this.dbExecutor = dbExecutor;
|
||||||
this.messagingManager = messagingManager;
|
this.messagingManager = messagingManager;
|
||||||
this.imageHelper = imageHelper;
|
this.imageHelper = imageHelper;
|
||||||
this.imageSizeCalculator = imageSizeCalculator;
|
this.imageSizeCalculator = imageSizeCalculator;
|
||||||
@@ -52,40 +72,130 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cachePut(MessageId messageId,
|
@DatabaseExecutor
|
||||||
List<AttachmentItem> attachments) {
|
|
||||||
attachmentCache.put(messageId, attachments);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Nullable
|
|
||||||
public List<AttachmentItem> cacheGet(MessageId messageId) {
|
|
||||||
return attachmentCache.get(messageId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Attachment getMessageAttachment(AttachmentHeader h)
|
public Attachment getMessageAttachment(AttachmentHeader h)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
return messagingManager.getAttachment(h);
|
return messagingManager.getAttachment(h);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AttachmentItem getAttachmentItem(Attachment a, boolean needsSize) {
|
public List<LiveData<AttachmentItem>> getAttachmentItems(
|
||||||
AttachmentHeader h = a.getHeader();
|
PrivateMessageHeader messageHeader) {
|
||||||
if (!needsSize) {
|
List<AttachmentHeader> headers = messageHeader.getAttachmentHeaders();
|
||||||
String extension =
|
List<LiveData<AttachmentItem>> items = new ArrayList<>(headers.size());
|
||||||
imageHelper.getExtensionFromMimeType(h.getContentType());
|
boolean needsSize = headers.size() == 1;
|
||||||
boolean hasError = false;
|
for (AttachmentHeader h : headers) {
|
||||||
if (extension == null) {
|
// try cache for existing item live data
|
||||||
extension = "";
|
MutableLiveData<AttachmentItem> liveData;
|
||||||
hasError = true;
|
if (needsSize) liveData = itemsWithSize.get(h.getMessageId());
|
||||||
|
else {
|
||||||
|
// try items with size first, as they work as well
|
||||||
|
liveData = itemsWithSize.get(h.getMessageId());
|
||||||
|
if (liveData == null)
|
||||||
|
liveData = itemsWithoutSize.get(h.getMessageId());
|
||||||
}
|
}
|
||||||
return new AttachmentItem(h, 0, 0, extension, 0, 0, hasError);
|
|
||||||
|
// create new live data with LOADING item if cache miss
|
||||||
|
if (liveData == null) {
|
||||||
|
AttachmentItem item = new AttachmentItem(h,
|
||||||
|
defaultSize, defaultSize, LOADING);
|
||||||
|
final MutableLiveData<AttachmentItem> finalLiveData =
|
||||||
|
new MutableLiveData<>(item);
|
||||||
|
// kick-off loading of attachment, will post to live data
|
||||||
|
dbExecutor.execute(
|
||||||
|
() -> loadAttachmentItem(h, needsSize, finalLiveData));
|
||||||
|
// add new LiveData to cache
|
||||||
|
liveData = finalLiveData;
|
||||||
|
if (needsSize) itemsWithSize.put(h.getMessageId(), liveData);
|
||||||
|
else itemsWithoutSize.put(h.getMessageId(), liveData);
|
||||||
|
}
|
||||||
|
items.add(liveData);
|
||||||
|
}
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@DatabaseExecutor
|
||||||
|
public void cacheAttachmentItemWithSize(MessageId conversationMessageId,
|
||||||
|
AttachmentHeader h) throws DbException {
|
||||||
|
try {
|
||||||
|
Attachment a = messagingManager.getAttachment(h);
|
||||||
|
AttachmentItem item = createAttachmentItem(a, true);
|
||||||
|
MutableLiveData<AttachmentItem> liveData =
|
||||||
|
new MutableLiveData<>(item);
|
||||||
|
itemsWithSize.put(h.getMessageId(), liveData);
|
||||||
|
} catch (NoSuchMessageException e) {
|
||||||
|
LOG.info("Attachment not received yet");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@DatabaseExecutor
|
||||||
|
public void loadAttachmentItem(MessageId attachmentId) {
|
||||||
|
// try to find LiveData for attachment in both caches
|
||||||
|
MutableLiveData<AttachmentItem> liveData;
|
||||||
|
boolean needsSize = true;
|
||||||
|
liveData = itemsWithSize.get(attachmentId);
|
||||||
|
if (liveData == null) {
|
||||||
|
needsSize = false;
|
||||||
|
liveData = itemsWithoutSize.get(attachmentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
InputStream is = new BufferedInputStream(a.getStream());
|
// If no LiveData for the attachment exists,
|
||||||
Size size = imageSizeCalculator.getSize(is, h.getContentType());
|
// its message did not yet arrive and we can ignore it for now.
|
||||||
|
if (liveData == null) return;
|
||||||
|
|
||||||
|
// actually load the attachment item
|
||||||
|
AttachmentHeader h = requireNonNull(liveData.getValue()).getHeader();
|
||||||
|
loadAttachmentItem(h, needsSize, liveData);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads an {@link AttachmentItem} from the database
|
||||||
|
* and notifies the given {@link LiveData}.
|
||||||
|
*/
|
||||||
|
@DatabaseExecutor
|
||||||
|
private void loadAttachmentItem(AttachmentHeader h, boolean needsSize,
|
||||||
|
MutableLiveData<AttachmentItem> liveData) {
|
||||||
|
Attachment a;
|
||||||
|
AttachmentItem item;
|
||||||
|
try {
|
||||||
|
a = messagingManager.getAttachment(h);
|
||||||
|
item = createAttachmentItem(a, needsSize);
|
||||||
|
} catch (NoSuchMessageException e) {
|
||||||
|
LOG.info("Attachment not received yet");
|
||||||
|
item = new AttachmentItem(h, defaultSize, defaultSize, MISSING);
|
||||||
|
} catch (DbException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
item = new AttachmentItem(h, "", ERROR);
|
||||||
|
}
|
||||||
|
liveData.postValue(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AttachmentItem createAttachmentItem(Attachment a,
|
||||||
|
boolean needsSize) {
|
||||||
|
AttachmentItem item;
|
||||||
|
AttachmentHeader h = a.getHeader();
|
||||||
|
if (needsSize) {
|
||||||
|
InputStream is = new BufferedInputStream(a.getStream());
|
||||||
|
Size size = imageSizeCalculator.getSize(is, h.getContentType());
|
||||||
|
tryToClose(is, LOG, WARNING);
|
||||||
|
item = createAttachmentItem(h, size);
|
||||||
|
} else {
|
||||||
|
String extension =
|
||||||
|
imageHelper.getExtensionFromMimeType(h.getContentType());
|
||||||
|
State state = AVAILABLE;
|
||||||
|
if (extension == null) {
|
||||||
|
extension = "";
|
||||||
|
state = ERROR;
|
||||||
|
}
|
||||||
|
item = new AttachmentItem(h, extension, state);
|
||||||
|
}
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
private AttachmentItem createAttachmentItem(AttachmentHeader h, Size size) {
|
||||||
// calculate thumbnail size
|
// calculate thumbnail size
|
||||||
Size thumbnailSize = new Size(defaultSize, defaultSize, size.mimeType);
|
Size thumbnailSize = new Size(defaultSize, defaultSize, size.mimeType);
|
||||||
if (!size.error) {
|
if (!size.error) {
|
||||||
@@ -104,8 +214,9 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
|
|||||||
hasError = true;
|
hasError = true;
|
||||||
}
|
}
|
||||||
if (extension == null) extension = "";
|
if (extension == null) extension = "";
|
||||||
return new AttachmentItem(h, size.width, size.height, extension,
|
State state = hasError ? ERROR : AVAILABLE;
|
||||||
thumbnailSize.width, thumbnailSize.height, hasError);
|
return new AttachmentItem(h, size.width, size.height,
|
||||||
|
extension, thumbnailSize.width, thumbnailSize.height, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Size getThumbnailSize(int width, int height, String mimeType) {
|
private Size getThumbnailSize(int width, int height, String mimeType) {
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package org.briarproject.briar.android.attachment;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.sync.MessageId;
|
||||||
|
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
|
@Immutable
|
||||||
|
@NotNullByDefault
|
||||||
|
class UnavailableItem {
|
||||||
|
|
||||||
|
private final MessageId conversationMessageId;
|
||||||
|
private final AttachmentHeader header;
|
||||||
|
private final boolean needsSize;
|
||||||
|
|
||||||
|
UnavailableItem(MessageId conversationMessageId,
|
||||||
|
AttachmentHeader header, boolean needsSize) {
|
||||||
|
this.conversationMessageId = conversationMessageId;
|
||||||
|
this.header = header;
|
||||||
|
this.needsSize = needsSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageId getConversationMessageId() {
|
||||||
|
return conversationMessageId;
|
||||||
|
}
|
||||||
|
|
||||||
|
AttachmentHeader getHeader() {
|
||||||
|
return header;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean needsSize() {
|
||||||
|
return needsSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import org.briarproject.briar.android.controller.handler.ResultExceptionHandler;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.UiThread;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
@@ -17,10 +18,7 @@ public interface BlogController extends BaseController {
|
|||||||
void setGroupId(GroupId g);
|
void setGroupId(GroupId g);
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
void setBlogSharingListener(BlogSharingListener listener);
|
void setBlogSharingListener(@Nullable BlogSharingListener listener);
|
||||||
|
|
||||||
@UiThread
|
|
||||||
void unsetBlogSharingListener(BlogSharingListener listener);
|
|
||||||
|
|
||||||
void loadBlogPosts(
|
void loadBlogPosts(
|
||||||
ResultExceptionHandler<Collection<BlogPostItem>, DbException> handler);
|
ResultExceptionHandler<Collection<BlogPostItem>, DbException> handler);
|
||||||
|
|||||||
@@ -96,15 +96,10 @@ class BlogControllerImpl extends BaseControllerImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setBlogSharingListener(BlogSharingListener listener) {
|
public void setBlogSharingListener(@Nullable BlogSharingListener listener) {
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void unsetBlogSharingListener(BlogSharingListener listener) {
|
|
||||||
if (this.listener == listener) this.listener = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eventOccurred(Event e) {
|
public void eventOccurred(Event e) {
|
||||||
if (groupId == null || listener == null)
|
if (groupId == null || listener == null)
|
||||||
|
|||||||
@@ -141,8 +141,7 @@ public class BlogFragment extends BaseFragment
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
blogController.unsetBlogSharingListener(this);
|
blogController.setBlogSharingListener(null);
|
||||||
sharingController.unsetSharingListener(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import org.briarproject.briar.api.blog.Blog;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.UiThread;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
@@ -18,10 +19,7 @@ public interface FeedController extends BaseController {
|
|||||||
void loadPersonalBlog(ResultExceptionHandler<Blog, DbException> handler);
|
void loadPersonalBlog(ResultExceptionHandler<Blog, DbException> handler);
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
void setFeedListener(FeedListener listener);
|
void setFeedListener(@Nullable FeedListener listener);
|
||||||
|
|
||||||
@UiThread
|
|
||||||
void unsetFeedListener(FeedListener listener);
|
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
interface FeedListener extends BlogListener {
|
interface FeedListener extends BlogListener {
|
||||||
|
|||||||
@@ -69,15 +69,10 @@ class FeedControllerImpl extends BaseControllerImpl implements FeedController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setFeedListener(FeedListener listener) {
|
public void setFeedListener(@Nullable FeedListener listener) {
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void unsetFeedListener(FeedListener listener) {
|
|
||||||
if (this.listener == listener) this.listener = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eventOccurred(Event e) {
|
public void eventOccurred(Event e) {
|
||||||
if (listener == null) throw new IllegalStateException();
|
if (listener == null) throw new IllegalStateException();
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ public class FeedFragment extends BaseFragment implements
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
feedController.unsetFeedListener(this);
|
feedController.setFeedListener(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -2,38 +2,35 @@ package org.briarproject.briar.android.contact;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.contact.Contact;
|
import org.briarproject.bramble.api.contact.Contact;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
|
||||||
|
|
||||||
import javax.annotation.concurrent.NotThreadSafe;
|
import javax.annotation.concurrent.NotThreadSafe;
|
||||||
|
|
||||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.DISCONNECTED;
|
|
||||||
|
|
||||||
@NotThreadSafe
|
@NotThreadSafe
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class ContactItem {
|
public class ContactItem {
|
||||||
|
|
||||||
private final Contact contact;
|
private final Contact contact;
|
||||||
private ConnectionStatus status;
|
private boolean connected;
|
||||||
|
|
||||||
public ContactItem(Contact contact) {
|
public ContactItem(Contact contact) {
|
||||||
this(contact, DISCONNECTED);
|
this(contact, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ContactItem(Contact contact, ConnectionStatus status) {
|
public ContactItem(Contact contact, boolean connected) {
|
||||||
this.contact = contact;
|
this.contact = contact;
|
||||||
this.status = status;
|
this.connected = connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Contact getContact() {
|
public Contact getContact() {
|
||||||
return contact;
|
return contact;
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionStatus getConnectionStatus() {
|
boolean isConnected() {
|
||||||
return status;
|
return connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setConnectionStatus(ConnectionStatus status) {
|
void setConnected(boolean connected) {
|
||||||
this.status = status;
|
this.connected = connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import android.widget.TextView;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
import org.briarproject.bramble.api.identity.Author;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||||
|
|
||||||
@@ -17,8 +16,6 @@ import androidx.annotation.UiThread;
|
|||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
import im.delight.android.identicons.IdenticonDrawable;
|
import im.delight.android.identicons.IdenticonDrawable;
|
||||||
|
|
||||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.CONNECTED;
|
|
||||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.RECENTLY_CONNECTED;
|
|
||||||
import static org.briarproject.briar.android.util.UiUtils.getContactDisplayName;
|
import static org.briarproject.briar.android.util.UiUtils.getContactDisplayName;
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
@@ -30,7 +27,7 @@ public class ContactItemViewHolder<I extends ContactItem>
|
|||||||
protected final ImageView avatar;
|
protected final ImageView avatar;
|
||||||
protected final TextView name;
|
protected final TextView name;
|
||||||
@Nullable
|
@Nullable
|
||||||
private final ImageView bulb;
|
protected final ImageView bulb;
|
||||||
|
|
||||||
public ContactItemViewHolder(View v) {
|
public ContactItemViewHolder(View v) {
|
||||||
super(v);
|
super(v);
|
||||||
@@ -50,13 +47,10 @@ public class ContactItemViewHolder<I extends ContactItem>
|
|||||||
|
|
||||||
if (bulb != null) {
|
if (bulb != null) {
|
||||||
// online/offline
|
// online/offline
|
||||||
ConnectionStatus status = item.getConnectionStatus();
|
if (item.isConnected()) {
|
||||||
if (status == CONNECTED) {
|
bulb.setImageResource(R.drawable.contact_connected);
|
||||||
bulb.setImageResource(R.drawable.ic_connected);
|
|
||||||
} else if (status == RECENTLY_CONNECTED) {
|
|
||||||
bulb.setImageResource(R.drawable.ic_recently_connected);
|
|
||||||
} else {
|
} else {
|
||||||
bulb.setImageResource(R.drawable.ic_disconnected);
|
bulb.setImageResource(R.drawable.contact_disconnected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public class ContactListAdapter extends
|
|||||||
if (c1.getTimestamp() != c2.getTimestamp()) {
|
if (c1.getTimestamp() != c2.getTimestamp()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return c1.getConnectionStatus() == c2.getConnectionStatus();
|
return c1.isConnected() == c2.isConnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ import org.briarproject.bramble.api.event.EventListener;
|
|||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionStatusChangedEvent;
|
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||||
@@ -53,6 +53,7 @@ import javax.inject.Inject;
|
|||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.UiThread;
|
||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
import androidx.core.app.ActivityOptionsCompat;
|
import androidx.core.app.ActivityOptionsCompat;
|
||||||
|
import androidx.core.util.Pair;
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
import io.github.kobakei.materialfabspeeddial.FabSpeedDial;
|
import io.github.kobakei.materialfabspeeddial.FabSpeedDial;
|
||||||
import io.github.kobakei.materialfabspeeddial.FabSpeedDial.OnMenuItemClickListener;
|
import io.github.kobakei.materialfabspeeddial.FabSpeedDial.OnMenuItemClickListener;
|
||||||
@@ -60,7 +61,7 @@ import io.github.kobakei.materialfabspeeddial.FabSpeedDial.OnMenuItemClickListen
|
|||||||
import static android.os.Build.VERSION.SDK_INT;
|
import static android.os.Build.VERSION.SDK_INT;
|
||||||
import static androidx.core.app.ActivityOptionsCompat.makeSceneTransitionAnimation;
|
import static androidx.core.app.ActivityOptionsCompat.makeSceneTransitionAnimation;
|
||||||
import static androidx.core.view.ViewCompat.getTransitionName;
|
import static androidx.core.view.ViewCompat.getTransitionName;
|
||||||
import static com.google.android.material.snackbar.BaseTransientBottomBar.LENGTH_INDEFINITE;
|
import static com.google.android.material.snackbar.Snackbar.LENGTH_INDEFINITE;
|
||||||
import static java.util.Objects.requireNonNull;
|
import static java.util.Objects.requireNonNull;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||||
@@ -86,12 +87,7 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
|||||||
|
|
||||||
private ContactListAdapter adapter;
|
private ContactListAdapter adapter;
|
||||||
private BriarRecyclerView list;
|
private BriarRecyclerView list;
|
||||||
/**
|
private Snackbar snackbar;
|
||||||
* The Snackbar is non-null when shown and null otherwise.
|
|
||||||
* Use {@link #showSnackBar()} and {@link #dismissSnackBar()} to interact.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
private Snackbar snackbar = null;
|
|
||||||
|
|
||||||
// Fields that are accessed from background threads must be volatile
|
// Fields that are accessed from background threads must be volatile
|
||||||
@Inject
|
@Inject
|
||||||
@@ -136,15 +132,26 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
|||||||
ContactId contactId = item.getContact().getId();
|
ContactId contactId = item.getContact().getId();
|
||||||
i.putExtra(CONTACT_ID, contactId.getInt());
|
i.putExtra(CONTACT_ID, contactId.getInt());
|
||||||
|
|
||||||
Bundle options = null;
|
|
||||||
// work-around for android bug #224270
|
|
||||||
if (SDK_INT >= 23 && !isSamsung7()) {
|
if (SDK_INT >= 23 && !isSamsung7()) {
|
||||||
options = makeTransitionOptions(view);
|
ContactListItemViewHolder holder =
|
||||||
}
|
(ContactListItemViewHolder) list
|
||||||
if (options == null) {
|
.getRecyclerView()
|
||||||
startActivity(i);
|
.findViewHolderForAdapterPosition(
|
||||||
|
adapter.findItemPosition(item));
|
||||||
|
Pair<View, String> avatar =
|
||||||
|
Pair.create(holder.avatar,
|
||||||
|
getTransitionName(holder.avatar));
|
||||||
|
Pair<View, String> bulb =
|
||||||
|
Pair.create(holder.bulb,
|
||||||
|
getTransitionName(holder.bulb));
|
||||||
|
ActivityOptionsCompat options =
|
||||||
|
makeSceneTransitionAnimation(getActivity(),
|
||||||
|
avatar, bulb);
|
||||||
|
ActivityCompat.startActivity(getActivity(), i,
|
||||||
|
options.toBundle());
|
||||||
} else {
|
} else {
|
||||||
ActivityCompat.startActivity(getActivity(), i, options);
|
// work-around for android bug #224270
|
||||||
|
startActivity(i);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
adapter = new ContactListAdapter(requireContext(),
|
adapter = new ContactListAdapter(requireContext(),
|
||||||
@@ -156,16 +163,14 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
|||||||
list.setEmptyText(getString(R.string.no_contacts));
|
list.setEmptyText(getString(R.string.no_contacts));
|
||||||
list.setEmptyAction(getString(R.string.no_contacts_action));
|
list.setEmptyAction(getString(R.string.no_contacts_action));
|
||||||
|
|
||||||
return contentView;
|
snackbar = new BriarSnackbarBuilder()
|
||||||
}
|
.setAction(R.string.show, v ->
|
||||||
|
startActivity(new Intent(getContext(),
|
||||||
|
PendingContactListActivity.class)))
|
||||||
|
.make(contentView, R.string.pending_contact_requests_snackbar,
|
||||||
|
LENGTH_INDEFINITE);
|
||||||
|
|
||||||
@Nullable
|
return contentView;
|
||||||
private Bundle makeTransitionOptions(View view) {
|
|
||||||
View avatar = view.findViewById(R.id.avatarView);
|
|
||||||
String name = requireNonNull(getTransitionName(avatar));
|
|
||||||
ActivityOptionsCompat options = makeSceneTransitionAnimation(
|
|
||||||
requireActivity(), view, name);
|
|
||||||
return options.toBundle();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -198,9 +203,9 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
|||||||
listener.runOnDbThread(() -> {
|
listener.runOnDbThread(() -> {
|
||||||
try {
|
try {
|
||||||
if (contactManager.getPendingContacts().isEmpty()) {
|
if (contactManager.getPendingContacts().isEmpty()) {
|
||||||
runOnUiThreadUnlessDestroyed(this::dismissSnackBar);
|
runOnUiThreadUnlessDestroyed(() -> snackbar.dismiss());
|
||||||
} else {
|
} else {
|
||||||
runOnUiThreadUnlessDestroyed(this::showSnackBar);
|
runOnUiThreadUnlessDestroyed(() -> snackbar.show());
|
||||||
}
|
}
|
||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
@@ -215,7 +220,6 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
|||||||
adapter.clear();
|
adapter.clear();
|
||||||
list.showProgressBar();
|
list.showProgressBar();
|
||||||
list.stopPeriodicUpdate();
|
list.stopPeriodicUpdate();
|
||||||
dismissSnackBar();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadContacts() {
|
private void loadContacts() {
|
||||||
@@ -229,9 +233,9 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
|||||||
ContactId id = c.getId();
|
ContactId id = c.getId();
|
||||||
GroupCount count =
|
GroupCount count =
|
||||||
conversationManager.getGroupCount(id);
|
conversationManager.getGroupCount(id);
|
||||||
ConnectionStatus status = connectionRegistry
|
boolean connected =
|
||||||
.getConnectionStatus(c.getId());
|
connectionRegistry.isConnected(c.getId());
|
||||||
contacts.add(new ContactListItem(c, status, count));
|
contacts.add(new ContactListItem(c, connected, count));
|
||||||
} catch (NoSuchContactException e) {
|
} catch (NoSuchContactException e) {
|
||||||
// Continue
|
// Continue
|
||||||
}
|
}
|
||||||
@@ -262,9 +266,10 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
|||||||
if (e instanceof ContactAddedEvent) {
|
if (e instanceof ContactAddedEvent) {
|
||||||
LOG.info("Contact added, reloading");
|
LOG.info("Contact added, reloading");
|
||||||
loadContacts();
|
loadContacts();
|
||||||
} else if (e instanceof ConnectionStatusChangedEvent) {
|
} else if (e instanceof ContactConnectedEvent) {
|
||||||
ConnectionStatusChangedEvent c = (ConnectionStatusChangedEvent) e;
|
setConnected(((ContactConnectedEvent) e).getContactId(), true);
|
||||||
setConnectionStatus(c.getContactId(), c.getConnectionStatus());
|
} else if (e instanceof ContactDisconnectedEvent) {
|
||||||
|
setConnected(((ContactDisconnectedEvent) e).getContactId(), false);
|
||||||
} else if (e instanceof ContactRemovedEvent) {
|
} else if (e instanceof ContactRemovedEvent) {
|
||||||
LOG.info("Contact removed, removing item");
|
LOG.info("Contact removed, removing item");
|
||||||
removeItem(((ContactRemovedEvent) e).getContactId());
|
removeItem(((ContactRemovedEvent) e).getContactId());
|
||||||
@@ -300,37 +305,14 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
|||||||
}
|
}
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
private void setConnectionStatus(ContactId c, ConnectionStatus status) {
|
private void setConnected(ContactId c, boolean connected) {
|
||||||
adapter.incrementRevision();
|
adapter.incrementRevision();
|
||||||
int position = adapter.findItemPosition(c);
|
int position = adapter.findItemPosition(c);
|
||||||
ContactListItem item = adapter.getItemAt(position);
|
ContactListItem item = adapter.getItemAt(position);
|
||||||
if (item != null) {
|
if (item != null) {
|
||||||
item.setConnectionStatus(status);
|
item.setConnected(connected);
|
||||||
adapter.updateItemAt(position, item);
|
adapter.updateItemAt(position, item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@UiThread
|
|
||||||
private void showSnackBar() {
|
|
||||||
if (snackbar != null) return;
|
|
||||||
View v = requireNonNull(getView());
|
|
||||||
int stringRes = R.string.pending_contact_requests_snackbar;
|
|
||||||
snackbar = new BriarSnackbarBuilder()
|
|
||||||
.setAction(R.string.show, view -> showPendingContactList())
|
|
||||||
.make(v, stringRes, LENGTH_INDEFINITE);
|
|
||||||
snackbar.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
private void dismissSnackBar() {
|
|
||||||
if (snackbar == null) return;
|
|
||||||
snackbar.dismiss();
|
|
||||||
snackbar = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showPendingContactList() {
|
|
||||||
Intent i = new Intent(getContext(), PendingContactListActivity.class);
|
|
||||||
startActivity(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package org.briarproject.briar.android.contact;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.contact.Contact;
|
import org.briarproject.bramble.api.contact.Contact;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
|
||||||
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
|
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
|
||||||
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
||||||
|
|
||||||
@@ -16,9 +15,9 @@ public class ContactListItem extends ContactItem {
|
|||||||
private long timestamp;
|
private long timestamp;
|
||||||
private int unread;
|
private int unread;
|
||||||
|
|
||||||
public ContactListItem(Contact contact, ConnectionStatus status,
|
public ContactListItem(Contact contact, boolean connected,
|
||||||
GroupCount count) {
|
GroupCount count) {
|
||||||
super(contact, status);
|
super(contact, connected);
|
||||||
this.empty = count.getMsgCount() == 0;
|
this.empty = count.getMsgCount() == 0;
|
||||||
this.unread = count.getUnreadCount();
|
this.unread = count.getUnreadCount();
|
||||||
this.timestamp = count.getLatestMsgTime();
|
this.timestamp = count.getLatestMsgTime();
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import org.briarproject.bramble.api.contact.ContactId;
|
|||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||||
|
import org.briarproject.briar.android.util.UiUtils;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
@@ -14,11 +15,8 @@ import javax.annotation.Nullable;
|
|||||||
|
|
||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.UiThread;
|
||||||
|
|
||||||
import static android.view.View.INVISIBLE;
|
|
||||||
import static android.view.View.VISIBLE;
|
|
||||||
import static androidx.core.view.ViewCompat.setTransitionName;
|
import static androidx.core.view.ViewCompat.setTransitionName;
|
||||||
import static org.briarproject.briar.android.util.UiUtils.formatDate;
|
import static org.briarproject.briar.android.util.UiUtils.formatDate;
|
||||||
import static org.briarproject.briar.android.util.UiUtils.getAvatarTransitionName;
|
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
@@ -41,11 +39,10 @@ class ContactListItemViewHolder extends ContactItemViewHolder<ContactListItem> {
|
|||||||
// unread count
|
// unread count
|
||||||
int unreadCount = item.getUnreadCount();
|
int unreadCount = item.getUnreadCount();
|
||||||
if (unreadCount > 0) {
|
if (unreadCount > 0) {
|
||||||
unread.setText(String.format(Locale.getDefault(), "%d",
|
unread.setText(String.format(Locale.getDefault(), "%d", unreadCount));
|
||||||
unreadCount));
|
unread.setVisibility(View.VISIBLE);
|
||||||
unread.setVisibility(VISIBLE);
|
|
||||||
} else {
|
} else {
|
||||||
unread.setVisibility(INVISIBLE);
|
unread.setVisibility(View.INVISIBLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// date of last message
|
// date of last message
|
||||||
@@ -57,7 +54,8 @@ class ContactListItemViewHolder extends ContactItemViewHolder<ContactListItem> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ContactId c = item.getContact().getId();
|
ContactId c = item.getContact().getId();
|
||||||
setTransitionName(avatar, getAvatarTransitionName(c));
|
setTransitionName(avatar, UiUtils.getAvatarTransitionName(c));
|
||||||
|
setTransitionName(bulb, UiUtils.getBulbTransitionName(c));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ public class PendingContactListViewModel extends AndroidViewModel
|
|||||||
Collection<Pair<PendingContact, PendingContactState>> pairs =
|
Collection<Pair<PendingContact, PendingContactState>> pairs =
|
||||||
contactManager.getPendingContacts();
|
contactManager.getPendingContacts();
|
||||||
List<PendingContactItem> items = new ArrayList<>(pairs.size());
|
List<PendingContactItem> items = new ArrayList<>(pairs.size());
|
||||||
boolean online = pairs.isEmpty();
|
boolean online = items.isEmpty();
|
||||||
for (Pair<PendingContact, PendingContactState> pair : pairs) {
|
for (Pair<PendingContact, PendingContactState> pair : pairs) {
|
||||||
PendingContact p = pair.getFirst();
|
PendingContact p = pair.getFirst();
|
||||||
PendingContactState state = pair.getSecond();
|
PendingContactState state = pair.getSecond();
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ class PendingContactViewHolder extends ViewHolder {
|
|||||||
status.setText(R.string.waiting_for_contact_to_come_online);
|
status.setText(R.string.waiting_for_contact_to_come_online);
|
||||||
break;
|
break;
|
||||||
case OFFLINE:
|
case OFFLINE:
|
||||||
|
color = ContextCompat
|
||||||
|
.getColor(status.getContext(), R.color.briar_yellow);
|
||||||
status.setText("");
|
status.setText("");
|
||||||
break;
|
break;
|
||||||
case CONNECTING:
|
case CONNECTING:
|
||||||
|
|||||||
@@ -16,12 +16,6 @@ public interface SharingController {
|
|||||||
@UiThread
|
@UiThread
|
||||||
void setSharingListener(SharingListener listener);
|
void setSharingListener(SharingListener listener);
|
||||||
|
|
||||||
/**
|
|
||||||
* Unsets the listener.
|
|
||||||
*/
|
|
||||||
@UiThread
|
|
||||||
void unsetSharingListener(SharingListener listener);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call this when your lifecycle starts,
|
* Call this when your lifecycle starts,
|
||||||
* so the listener will be called when information changes.
|
* so the listener will be called when information changes.
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ import org.briarproject.bramble.api.event.EventBus;
|
|||||||
import org.briarproject.bramble.api.event.EventListener;
|
import org.briarproject.bramble.api.event.EventListener;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionStatusChangedEvent;
|
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@@ -17,8 +18,6 @@ import javax.inject.Inject;
|
|||||||
|
|
||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.UiThread;
|
||||||
|
|
||||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.CONNECTED;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class SharingControllerImpl implements SharingController, EventListener {
|
public class SharingControllerImpl implements SharingController, EventListener {
|
||||||
|
|
||||||
@@ -44,11 +43,6 @@ public class SharingControllerImpl implements SharingController, EventListener {
|
|||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void unsetSharingListener(SharingListener listener) {
|
|
||||||
if (this.listener == listener) this.listener = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
eventBus.addListener(this);
|
eventBus.addListener(this);
|
||||||
@@ -61,14 +55,15 @@ public class SharingControllerImpl implements SharingController, EventListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eventOccurred(Event e) {
|
public void eventOccurred(Event e) {
|
||||||
if (e instanceof ConnectionStatusChangedEvent) {
|
if (e instanceof ContactConnectedEvent) {
|
||||||
ConnectionStatusChangedEvent c = (ConnectionStatusChangedEvent) e;
|
setConnected(((ContactConnectedEvent) e).getContactId());
|
||||||
setConnectionStatus(c.getContactId());
|
} else if (e instanceof ContactDisconnectedEvent) {
|
||||||
|
setConnected(((ContactDisconnectedEvent) e).getContactId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
private void setConnectionStatus(ContactId c) {
|
private void setConnected(ContactId c) {
|
||||||
if (listener == null) throw new IllegalStateException();
|
if (listener == null) throw new IllegalStateException();
|
||||||
if (contacts.contains(c)) {
|
if (contacts.contains(c)) {
|
||||||
int online = getOnlineCount();
|
int online = getOnlineCount();
|
||||||
@@ -95,9 +90,7 @@ public class SharingControllerImpl implements SharingController, EventListener {
|
|||||||
public int getOnlineCount() {
|
public int getOnlineCount() {
|
||||||
int online = 0;
|
int online = 0;
|
||||||
for (ContactId c : contacts) {
|
for (ContactId c : contacts) {
|
||||||
if (connectionRegistry.getConnectionStatus(c) == CONNECTED) {
|
if (connectionRegistry.isConnected(c)) online++;
|
||||||
online++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return online;
|
return online;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import android.view.Menu;
|
|||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
@@ -26,15 +27,14 @@ import org.briarproject.bramble.api.contact.event.ContactRemovedEvent;
|
|||||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.db.NoSuchContactException;
|
import org.briarproject.bramble.api.db.NoSuchContactException;
|
||||||
import org.briarproject.bramble.api.db.NoSuchMessageException;
|
|
||||||
import org.briarproject.bramble.api.event.Event;
|
import org.briarproject.bramble.api.event.Event;
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.event.EventListener;
|
import org.briarproject.bramble.api.event.EventListener;
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionStatusChangedEvent;
|
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||||
import org.briarproject.bramble.api.sync.ClientId;
|
import org.briarproject.bramble.api.sync.ClientId;
|
||||||
import org.briarproject.bramble.api.sync.MessageId;
|
import org.briarproject.bramble.api.sync.MessageId;
|
||||||
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent;
|
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent;
|
||||||
@@ -64,13 +64,13 @@ import org.briarproject.briar.api.client.ProtocolStateException;
|
|||||||
import org.briarproject.briar.api.client.SessionId;
|
import org.briarproject.briar.api.client.SessionId;
|
||||||
import org.briarproject.briar.api.conversation.ConversationManager;
|
import org.briarproject.briar.api.conversation.ConversationManager;
|
||||||
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
||||||
|
import org.briarproject.briar.api.conversation.ConversationMessageVisitor;
|
||||||
import org.briarproject.briar.api.conversation.ConversationRequest;
|
import org.briarproject.briar.api.conversation.ConversationRequest;
|
||||||
import org.briarproject.briar.api.conversation.ConversationResponse;
|
import org.briarproject.briar.api.conversation.ConversationResponse;
|
||||||
import org.briarproject.briar.api.conversation.DeletionResult;
|
import org.briarproject.briar.api.conversation.DeletionResult;
|
||||||
import org.briarproject.briar.api.conversation.event.ConversationMessageReceivedEvent;
|
import org.briarproject.briar.api.conversation.event.ConversationMessageReceivedEvent;
|
||||||
import org.briarproject.briar.api.forum.ForumSharingManager;
|
import org.briarproject.briar.api.forum.ForumSharingManager;
|
||||||
import org.briarproject.briar.api.introduction.IntroductionManager;
|
import org.briarproject.briar.api.introduction.IntroductionManager;
|
||||||
import org.briarproject.briar.api.messaging.Attachment;
|
|
||||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||||
@@ -96,6 +96,7 @@ import androidx.appcompat.widget.Toolbar;
|
|||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
import androidx.core.app.ActivityOptionsCompat;
|
import androidx.core.app.ActivityOptionsCompat;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
|
import androidx.lifecycle.LiveData;
|
||||||
import androidx.lifecycle.Observer;
|
import androidx.lifecycle.Observer;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
import androidx.lifecycle.ViewModelProviders;
|
import androidx.lifecycle.ViewModelProviders;
|
||||||
@@ -117,15 +118,11 @@ import static androidx.core.app.ActivityOptionsCompat.makeSceneTransitionAnimati
|
|||||||
import static androidx.core.view.ViewCompat.setTransitionName;
|
import static androidx.core.view.ViewCompat.setTransitionName;
|
||||||
import static androidx.lifecycle.Lifecycle.State.STARTED;
|
import static androidx.lifecycle.Lifecycle.State.STARTED;
|
||||||
import static androidx.recyclerview.widget.SortedList.INVALID_POSITION;
|
import static androidx.recyclerview.widget.SortedList.INVALID_POSITION;
|
||||||
import static java.util.Collections.emptyList;
|
|
||||||
import static java.util.Collections.singletonList;
|
|
||||||
import static java.util.Collections.sort;
|
import static java.util.Collections.sort;
|
||||||
import static java.util.Objects.requireNonNull;
|
import static java.util.Objects.requireNonNull;
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.CONNECTED;
|
|
||||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.RECENTLY_CONNECTED;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
import static org.briarproject.bramble.util.LogUtils.now;
|
import static org.briarproject.bramble.util.LogUtils.now;
|
||||||
@@ -137,8 +134,10 @@ import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_INTRO
|
|||||||
import static org.briarproject.briar.android.conversation.ImageActivity.ATTACHMENTS;
|
import static org.briarproject.briar.android.conversation.ImageActivity.ATTACHMENTS;
|
||||||
import static org.briarproject.briar.android.conversation.ImageActivity.ATTACHMENT_POSITION;
|
import static org.briarproject.briar.android.conversation.ImageActivity.ATTACHMENT_POSITION;
|
||||||
import static org.briarproject.briar.android.conversation.ImageActivity.DATE;
|
import static org.briarproject.briar.android.conversation.ImageActivity.DATE;
|
||||||
|
import static org.briarproject.briar.android.conversation.ImageActivity.ITEM_ID;
|
||||||
import static org.briarproject.briar.android.conversation.ImageActivity.NAME;
|
import static org.briarproject.briar.android.conversation.ImageActivity.NAME;
|
||||||
import static org.briarproject.briar.android.util.UiUtils.getAvatarTransitionName;
|
import static org.briarproject.briar.android.util.UiUtils.getAvatarTransitionName;
|
||||||
|
import static org.briarproject.briar.android.util.UiUtils.getBulbTransitionName;
|
||||||
import static org.briarproject.briar.android.util.UiUtils.observeOnce;
|
import static org.briarproject.briar.android.util.UiUtils.observeOnce;
|
||||||
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_ATTACHMENTS_PER_MESSAGE;
|
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_ATTACHMENTS_PER_MESSAGE;
|
||||||
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_TEXT_LENGTH;
|
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_TEXT_LENGTH;
|
||||||
@@ -185,8 +184,6 @@ public class ConversationActivity extends BriarActivity
|
|||||||
volatile GroupInvitationManager groupInvitationManager;
|
volatile GroupInvitationManager groupInvitationManager;
|
||||||
|
|
||||||
private final Map<MessageId, String> textCache = new ConcurrentHashMap<>();
|
private final Map<MessageId, String> textCache = new ConcurrentHashMap<>();
|
||||||
private final Map<MessageId, PrivateMessageHeader> missingAttachments =
|
|
||||||
new ConcurrentHashMap<>();
|
|
||||||
private final Observer<String> contactNameObserver = name -> {
|
private final Observer<String> contactNameObserver = name -> {
|
||||||
requireNonNull(name);
|
requireNonNull(name);
|
||||||
loadMessages();
|
loadMessages();
|
||||||
@@ -198,8 +195,8 @@ public class ConversationActivity extends BriarActivity
|
|||||||
private ConversationAdapter adapter;
|
private ConversationAdapter adapter;
|
||||||
private Toolbar toolbar;
|
private Toolbar toolbar;
|
||||||
private CircleImageView toolbarAvatar;
|
private CircleImageView toolbarAvatar;
|
||||||
|
private ImageView toolbarStatus;
|
||||||
private TextView toolbarTitle;
|
private TextView toolbarTitle;
|
||||||
private TextView toolbarStatus;
|
|
||||||
private BriarRecyclerView list;
|
private BriarRecyclerView list;
|
||||||
private LinearLayoutManager layoutManager;
|
private LinearLayoutManager layoutManager;
|
||||||
private TextInputView textInputView;
|
private TextInputView textInputView;
|
||||||
@@ -237,8 +234,8 @@ public class ConversationActivity extends BriarActivity
|
|||||||
// Custom Toolbar
|
// Custom Toolbar
|
||||||
toolbar = requireNonNull(setUpCustomToolbar(true));
|
toolbar = requireNonNull(setUpCustomToolbar(true));
|
||||||
toolbarAvatar = toolbar.findViewById(R.id.contactAvatar);
|
toolbarAvatar = toolbar.findViewById(R.id.contactAvatar);
|
||||||
toolbarTitle = toolbar.findViewById(R.id.contactName);
|
|
||||||
toolbarStatus = toolbar.findViewById(R.id.contactStatus);
|
toolbarStatus = toolbar.findViewById(R.id.contactStatus);
|
||||||
|
toolbarTitle = toolbar.findViewById(R.id.contactName);
|
||||||
|
|
||||||
observeOnce(viewModel.getContactAuthorId(), this, authorId -> {
|
observeOnce(viewModel.getContactAuthorId(), this, authorId -> {
|
||||||
requireNonNull(authorId);
|
requireNonNull(authorId);
|
||||||
@@ -257,12 +254,14 @@ public class ConversationActivity extends BriarActivity
|
|||||||
this::onAddedPrivateMessage);
|
this::onAddedPrivateMessage);
|
||||||
|
|
||||||
setTransitionName(toolbarAvatar, getAvatarTransitionName(contactId));
|
setTransitionName(toolbarAvatar, getAvatarTransitionName(contactId));
|
||||||
|
setTransitionName(toolbarStatus, getBulbTransitionName(contactId));
|
||||||
|
|
||||||
visitor = new ConversationVisitor(this, this, this,
|
visitor = new ConversationVisitor(this, this, this,
|
||||||
viewModel.getContactDisplayName());
|
viewModel.getContactDisplayName());
|
||||||
adapter = new ConversationAdapter(this, this);
|
adapter = new ConversationAdapter(this, this);
|
||||||
list = findViewById(R.id.conversationView);
|
list = findViewById(R.id.conversationView);
|
||||||
layoutManager = new LinearLayoutManager(this);
|
layoutManager = new LinearLayoutManager(this);
|
||||||
|
layoutManager.setStackFromEnd(true);
|
||||||
list.setLayoutManager(layoutManager);
|
list.setLayoutManager(layoutManager);
|
||||||
list.setAdapter(adapter);
|
list.setAdapter(adapter);
|
||||||
list.setEmptyText(getString(R.string.no_private_messages));
|
list.setEmptyText(getString(R.string.no_private_messages));
|
||||||
@@ -498,14 +497,14 @@ public class ConversationActivity extends BriarActivity
|
|||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
private void displayContactOnlineStatus() {
|
private void displayContactOnlineStatus() {
|
||||||
ConnectionStatus status =
|
if (connectionRegistry.isConnected(contactId)) {
|
||||||
connectionRegistry.getConnectionStatus(contactId);
|
toolbarStatus.setImageDrawable(ContextCompat.getDrawable(
|
||||||
if (status == CONNECTED) {
|
ConversationActivity.this, R.drawable.contact_online));
|
||||||
toolbarStatus.setText(R.string.online);
|
toolbarStatus.setContentDescription(getString(R.string.online));
|
||||||
} else if (status == RECENTLY_CONNECTED) {
|
|
||||||
toolbarStatus.setText(R.string.recently_online);
|
|
||||||
} else {
|
} else {
|
||||||
toolbarStatus.setText(R.string.offline);
|
toolbarStatus.setImageDrawable(ContextCompat.getDrawable(
|
||||||
|
ConversationActivity.this, R.drawable.contact_offline));
|
||||||
|
toolbarStatus.setContentDescription(getString(R.string.offline));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -539,6 +538,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DatabaseExecutor
|
||||||
private void eagerlyLoadMessageSize(PrivateMessageHeader h) {
|
private void eagerlyLoadMessageSize(PrivateMessageHeader h) {
|
||||||
try {
|
try {
|
||||||
MessageId id = h.getId();
|
MessageId id = h.getId();
|
||||||
@@ -555,21 +555,11 @@ public class ConversationActivity extends BriarActivity
|
|||||||
// images we use a grid so the size is fixed
|
// images we use a grid so the size is fixed
|
||||||
List<AttachmentHeader> headers = h.getAttachmentHeaders();
|
List<AttachmentHeader> headers = h.getAttachmentHeaders();
|
||||||
if (headers.size() == 1) {
|
if (headers.size() == 1) {
|
||||||
List<AttachmentItem> items = attachmentRetriever.cacheGet(id);
|
LOG.info("Eagerly loading image size for latest message");
|
||||||
if (items == null) {
|
AttachmentHeader header = headers.get(0);
|
||||||
LOG.info("Eagerly loading image size for latest message");
|
// get the item to retrieve its size
|
||||||
AttachmentHeader header = headers.get(0);
|
attachmentRetriever
|
||||||
try {
|
.cacheAttachmentItemWithSize(h.getId(), header);
|
||||||
Attachment a = attachmentRetriever
|
|
||||||
.getMessageAttachment(header);
|
|
||||||
AttachmentItem item =
|
|
||||||
attachmentRetriever.getAttachmentItem(a, true);
|
|
||||||
attachmentRetriever.cachePut(id, singletonList(item));
|
|
||||||
} catch (NoSuchMessageException e) {
|
|
||||||
LOG.info("Attachment not received yet");
|
|
||||||
missingAttachments.put(header.getMessageId(), h);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
@@ -650,44 +640,12 @@ public class ConversationActivity extends BriarActivity
|
|||||||
&& adapter.isScrolledToBottom(layoutManager);
|
&& adapter.isScrolledToBottom(layoutManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadMessageAttachments(PrivateMessageHeader h) {
|
private void updateMessageAttachment(MessageId m, AttachmentItem item) {
|
||||||
// TODO: Use placeholders for missing/invalid attachments
|
|
||||||
runOnDbThread(() -> {
|
|
||||||
try {
|
|
||||||
// TODO move getting the items off to IoExecutor, if size == 1
|
|
||||||
List<AttachmentHeader> headers = h.getAttachmentHeaders();
|
|
||||||
boolean needsSize = headers.size() == 1;
|
|
||||||
List<AttachmentItem> items = new ArrayList<>(headers.size());
|
|
||||||
for (AttachmentHeader header : headers) {
|
|
||||||
try {
|
|
||||||
Attachment a = attachmentRetriever
|
|
||||||
.getMessageAttachment(header);
|
|
||||||
AttachmentItem item = attachmentRetriever
|
|
||||||
.getAttachmentItem(a, needsSize);
|
|
||||||
items.add(item);
|
|
||||||
} catch (NoSuchMessageException e) {
|
|
||||||
LOG.info("Attachment not received yet");
|
|
||||||
missingAttachments.put(header.getMessageId(), h);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Don't cache items unless all are present and valid
|
|
||||||
attachmentRetriever.cachePut(h.getId(), items);
|
|
||||||
displayMessageAttachments(h.getId(), items);
|
|
||||||
} catch (DbException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void displayMessageAttachments(MessageId m,
|
|
||||||
List<AttachmentItem> items) {
|
|
||||||
runOnUiThreadUnlessDestroyed(() -> {
|
runOnUiThreadUnlessDestroyed(() -> {
|
||||||
Pair<Integer, ConversationMessageItem> pair =
|
Pair<Integer, ConversationMessageItem> pair =
|
||||||
adapter.getMessageItem(m);
|
adapter.getMessageItem(m);
|
||||||
if (pair != null) {
|
if (pair != null && pair.getSecond().updateAttachments(item)) {
|
||||||
boolean scroll = shouldScrollWhenUpdatingMessage();
|
boolean scroll = shouldScrollWhenUpdatingMessage();
|
||||||
pair.getSecond().setAttachments(items);
|
|
||||||
adapter.notifyItemChanged(pair.getFirst());
|
adapter.notifyItemChanged(pair.getFirst());
|
||||||
if (scroll) scrollToBottom();
|
if (scroll) scrollToBottom();
|
||||||
}
|
}
|
||||||
@@ -728,10 +686,16 @@ public class ConversationActivity extends BriarActivity
|
|||||||
LOG.info("Messages acked");
|
LOG.info("Messages acked");
|
||||||
markMessages(m.getMessageIds(), true, true);
|
markMessages(m.getMessageIds(), true, true);
|
||||||
}
|
}
|
||||||
} else if (e instanceof ConnectionStatusChangedEvent) {
|
} else if (e instanceof ContactConnectedEvent) {
|
||||||
ConnectionStatusChangedEvent c = (ConnectionStatusChangedEvent) e;
|
ContactConnectedEvent c = (ContactConnectedEvent) e;
|
||||||
if (c.getContactId().equals(contactId)) {
|
if (c.getContactId().equals(contactId)) {
|
||||||
LOG.info("Connection status changed");
|
LOG.info("Contact connected");
|
||||||
|
displayContactOnlineStatus();
|
||||||
|
}
|
||||||
|
} else if (e instanceof ContactDisconnectedEvent) {
|
||||||
|
ContactDisconnectedEvent c = (ContactDisconnectedEvent) e;
|
||||||
|
if (c.getContactId().equals(contactId)) {
|
||||||
|
LOG.info("Contact disconnected");
|
||||||
displayContactOnlineStatus();
|
displayContactOnlineStatus();
|
||||||
}
|
}
|
||||||
} else if (e instanceof ClientVersionUpdatedEvent) {
|
} else if (e instanceof ClientVersionUpdatedEvent) {
|
||||||
@@ -758,11 +722,8 @@ public class ConversationActivity extends BriarActivity
|
|||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
private void onAttachmentReceived(MessageId attachmentId) {
|
private void onAttachmentReceived(MessageId attachmentId) {
|
||||||
PrivateMessageHeader h = missingAttachments.remove(attachmentId);
|
runOnDbThread(
|
||||||
if (h != null) {
|
() -> attachmentRetriever.loadAttachmentItem(attachmentId));
|
||||||
LOG.info("Missing attachment received");
|
|
||||||
loadMessageAttachments(h);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
@@ -773,7 +734,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
observeOnce(viewModel.getContactDisplayName(), this,
|
observeOnce(viewModel.getContactDisplayName(), this,
|
||||||
name -> addConversationItem(h.accept(visitor)));
|
name -> addConversationItem(h.accept(visitor)));
|
||||||
} else {
|
} else {
|
||||||
// visitor also loads message text (if existing)
|
// visitor also loads message text and attachments (if existing)
|
||||||
addConversationItem(h.accept(visitor));
|
addConversationItem(h.accept(visitor));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1100,8 +1061,9 @@ public class ConversationActivity extends BriarActivity
|
|||||||
i.putExtra(ATTACHMENT_POSITION, attachments.indexOf(item));
|
i.putExtra(ATTACHMENT_POSITION, attachments.indexOf(item));
|
||||||
i.putExtra(NAME, name);
|
i.putExtra(NAME, name);
|
||||||
i.putExtra(DATE, messageItem.getTime());
|
i.putExtra(DATE, messageItem.getTime());
|
||||||
|
i.putExtra(ITEM_ID, messageItem.getId().getBytes());
|
||||||
// restoring list position should not trigger android bug #224270
|
// restoring list position should not trigger android bug #224270
|
||||||
String transitionName = item.getTransitionName();
|
String transitionName = item.getTransitionName(messageItem.getId());
|
||||||
ActivityOptionsCompat options =
|
ActivityOptionsCompat options =
|
||||||
makeSceneTransitionAnimation(this, view, transitionName);
|
makeSceneTransitionAnimation(this, view, transitionName);
|
||||||
ActivityCompat.startActivity(this, i, options.toBundle());
|
ActivityCompat.startActivity(this, i, options.toBundle());
|
||||||
@@ -1140,15 +1102,37 @@ public class ConversationActivity extends BriarActivity
|
|||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called by {@link PrivateMessageHeader#accept(ConversationMessageVisitor)}
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<AttachmentItem> getAttachmentItems(PrivateMessageHeader h) {
|
public List<AttachmentItem> getAttachmentItems(PrivateMessageHeader h) {
|
||||||
List<AttachmentItem> attachments =
|
List<LiveData<AttachmentItem>> liveDataList =
|
||||||
attachmentRetriever.cacheGet(h.getId());
|
attachmentRetriever.getAttachmentItems(h);
|
||||||
if (attachments == null) {
|
List<AttachmentItem> items = new ArrayList<>(liveDataList.size());
|
||||||
loadMessageAttachments(h);
|
for (LiveData<AttachmentItem> liveData : liveDataList) {
|
||||||
return emptyList();
|
liveData.observe(this, new AttachmentObserver(h.getId(), liveData));
|
||||||
|
items.add(requireNonNull(liveData.getValue()));
|
||||||
|
}
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class AttachmentObserver implements Observer<AttachmentItem> {
|
||||||
|
private final MessageId conversationMessageId;
|
||||||
|
private final LiveData<AttachmentItem> liveData;
|
||||||
|
|
||||||
|
private AttachmentObserver(MessageId conversationMessageId,
|
||||||
|
LiveData<AttachmentItem> liveData) {
|
||||||
|
this.conversationMessageId = conversationMessageId;
|
||||||
|
this.liveData = liveData;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChanged(AttachmentItem attachmentItem) {
|
||||||
|
updateMessageAttachment(conversationMessageId, attachmentItem);
|
||||||
|
if (attachmentItem.getState().isFinal())
|
||||||
|
liveData.removeObserver(this);
|
||||||
}
|
}
|
||||||
return attachments;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,12 +9,13 @@ import java.util.List;
|
|||||||
import javax.annotation.concurrent.NotThreadSafe;
|
import javax.annotation.concurrent.NotThreadSafe;
|
||||||
|
|
||||||
import androidx.annotation.LayoutRes;
|
import androidx.annotation.LayoutRes;
|
||||||
|
import androidx.annotation.UiThread;
|
||||||
|
|
||||||
@NotThreadSafe
|
@NotThreadSafe
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class ConversationMessageItem extends ConversationItem {
|
class ConversationMessageItem extends ConversationItem {
|
||||||
|
|
||||||
private List<AttachmentItem> attachments;
|
private final List<AttachmentItem> attachments;
|
||||||
|
|
||||||
ConversationMessageItem(@LayoutRes int layoutRes, PrivateMessageHeader h,
|
ConversationMessageItem(@LayoutRes int layoutRes, PrivateMessageHeader h,
|
||||||
List<AttachmentItem> attachments) {
|
List<AttachmentItem> attachments) {
|
||||||
@@ -26,8 +27,14 @@ class ConversationMessageItem extends ConversationItem {
|
|||||||
return attachments;
|
return attachments;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setAttachments(List<AttachmentItem> attachments) {
|
@UiThread
|
||||||
this.attachments = attachments;
|
boolean updateAttachments(AttachmentItem item) {
|
||||||
|
int pos = attachments.indexOf(item);
|
||||||
|
if (pos != -1 && attachments.get(pos).getState() != item.getState()) {
|
||||||
|
attachments.set(pos, item);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import com.google.android.material.appbar.AppBarLayout;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.sync.MessageId;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
import org.briarproject.briar.android.activity.BriarActivity;
|
import org.briarproject.briar.android.activity.BriarActivity;
|
||||||
@@ -69,6 +70,7 @@ public class ImageActivity extends BriarActivity
|
|||||||
final static String ATTACHMENT_POSITION = "position";
|
final static String ATTACHMENT_POSITION = "position";
|
||||||
final static String NAME = "name";
|
final static String NAME = "name";
|
||||||
final static String DATE = "date";
|
final static String DATE = "date";
|
||||||
|
final static String ITEM_ID = "itemId";
|
||||||
|
|
||||||
@RequiresApi(api = 16)
|
@RequiresApi(api = 16)
|
||||||
private final static int UI_FLAGS_DEFAULT =
|
private final static int UI_FLAGS_DEFAULT =
|
||||||
@@ -82,6 +84,7 @@ public class ImageActivity extends BriarActivity
|
|||||||
private AppBarLayout appBarLayout;
|
private AppBarLayout appBarLayout;
|
||||||
private ViewPager viewPager;
|
private ViewPager viewPager;
|
||||||
private List<AttachmentItem> attachments;
|
private List<AttachmentItem> attachments;
|
||||||
|
private MessageId conversationMessageId;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void injectActivity(ActivityComponent component) {
|
public void injectActivity(ActivityComponent component) {
|
||||||
@@ -136,6 +139,7 @@ public class ImageActivity extends BriarActivity
|
|||||||
String date = formatDateAbsolute(this, time);
|
String date = formatDateAbsolute(this, time);
|
||||||
contactName.setText(name);
|
contactName.setText(name);
|
||||||
dateView.setText(date);
|
dateView.setText(date);
|
||||||
|
conversationMessageId = new MessageId(i.getByteArrayExtra(ITEM_ID));
|
||||||
|
|
||||||
// Set up image ViewPager
|
// Set up image ViewPager
|
||||||
viewPager = findViewById(R.id.viewPager);
|
viewPager = findViewById(R.id.viewPager);
|
||||||
@@ -325,8 +329,8 @@ public class ImageActivity extends BriarActivity
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Fragment getItem(int position) {
|
public Fragment getItem(int position) {
|
||||||
Fragment f = ImageFragment
|
Fragment f = ImageFragment.newInstance(
|
||||||
.newInstance(attachments.get(position), isFirst);
|
attachments.get(position), conversationMessageId, isFirst);
|
||||||
isFirst = false;
|
isFirst = false;
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,8 @@ class ImageAdapter extends Adapter<ImageViewHolder> {
|
|||||||
public ImageViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
|
public ImageViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
|
||||||
View v = LayoutInflater.from(viewGroup.getContext()).inflate(
|
View v = LayoutInflater.from(viewGroup.getContext()).inflate(
|
||||||
R.layout.list_item_image, viewGroup, false);
|
R.layout.list_item_image, viewGroup, false);
|
||||||
return new ImageViewHolder(v, imageSize);
|
requireNonNull(conversationItem);
|
||||||
|
return new ImageViewHolder(v, imageSize, conversationItem.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -58,7 +59,7 @@ class ImageAdapter extends Adapter<ImageViewHolder> {
|
|||||||
// get item
|
// get item
|
||||||
requireNonNull(conversationItem);
|
requireNonNull(conversationItem);
|
||||||
AttachmentItem item = items.get(position);
|
AttachmentItem item = items.get(position);
|
||||||
// set onClick listener
|
// set onClick listener, if not missing or error
|
||||||
imageViewHolder.itemView.setOnClickListener(v ->
|
imageViewHolder.itemView.setOnClickListener(v ->
|
||||||
listener.onAttachmentClicked(v, conversationItem, item)
|
listener.onAttachmentClicked(v, conversationItem, item)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import com.bumptech.glide.request.target.Target;
|
|||||||
import com.github.chrisbanes.photoview.PhotoView;
|
import com.github.chrisbanes.photoview.PhotoView;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.sync.MessageId;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.BaseActivity;
|
import org.briarproject.briar.android.activity.BaseActivity;
|
||||||
import org.briarproject.briar.android.attachment.AttachmentItem;
|
import org.briarproject.briar.android.attachment.AttachmentItem;
|
||||||
@@ -23,6 +24,7 @@ import org.briarproject.briar.android.conversation.glide.GlideApp;
|
|||||||
import javax.annotation.ParametersAreNonnullByDefault;
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import androidx.annotation.DrawableRes;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
@@ -32,27 +34,36 @@ import static android.os.Build.VERSION.SDK_INT;
|
|||||||
import static android.widget.ImageView.ScaleType.FIT_START;
|
import static android.widget.ImageView.ScaleType.FIT_START;
|
||||||
import static com.bumptech.glide.load.engine.DiskCacheStrategy.NONE;
|
import static com.bumptech.glide.load.engine.DiskCacheStrategy.NONE;
|
||||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
||||||
|
import static org.briarproject.briar.android.attachment.AttachmentItem.State.AVAILABLE;
|
||||||
|
import static org.briarproject.briar.android.attachment.AttachmentItem.State.ERROR;
|
||||||
import static org.briarproject.briar.android.conversation.ImageActivity.ATTACHMENT_POSITION;
|
import static org.briarproject.briar.android.conversation.ImageActivity.ATTACHMENT_POSITION;
|
||||||
|
import static org.briarproject.briar.android.conversation.ImageActivity.ITEM_ID;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersAreNonnullByDefault
|
@ParametersAreNonnullByDefault
|
||||||
public class ImageFragment extends Fragment {
|
public class ImageFragment extends Fragment
|
||||||
|
implements RequestListener<Drawable> {
|
||||||
|
|
||||||
private final static String IS_FIRST = "isFirst";
|
private final static String IS_FIRST = "isFirst";
|
||||||
|
@DrawableRes
|
||||||
|
private static final int ERROR_RES = R.drawable.ic_image_broken;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ViewModelProvider.Factory viewModelFactory;
|
ViewModelProvider.Factory viewModelFactory;
|
||||||
|
|
||||||
private AttachmentItem attachment;
|
private AttachmentItem attachment;
|
||||||
private boolean isFirst;
|
private boolean isFirst;
|
||||||
|
private MessageId conversationItemId;
|
||||||
private ImageViewModel viewModel;
|
private ImageViewModel viewModel;
|
||||||
private PhotoView photoView;
|
private PhotoView photoView;
|
||||||
|
|
||||||
static ImageFragment newInstance(AttachmentItem a, boolean isFirst) {
|
static ImageFragment newInstance(AttachmentItem a,
|
||||||
|
MessageId conversationMessageId, boolean isFirst) {
|
||||||
ImageFragment f = new ImageFragment();
|
ImageFragment f = new ImageFragment();
|
||||||
Bundle args = new Bundle();
|
Bundle args = new Bundle();
|
||||||
args.putParcelable(ATTACHMENT_POSITION, a);
|
args.putParcelable(ATTACHMENT_POSITION, a);
|
||||||
args.putBoolean(IS_FIRST, isFirst);
|
args.putBoolean(IS_FIRST, isFirst);
|
||||||
|
args.putByteArray(ITEM_ID, conversationMessageId.getBytes());
|
||||||
f.setArguments(args);
|
f.setArguments(args);
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
@@ -70,6 +81,8 @@ public class ImageFragment extends Fragment {
|
|||||||
Bundle args = requireNonNull(getArguments());
|
Bundle args = requireNonNull(getArguments());
|
||||||
attachment = requireNonNull(args.getParcelable(ATTACHMENT_POSITION));
|
attachment = requireNonNull(args.getParcelable(ATTACHMENT_POSITION));
|
||||||
isFirst = args.getBoolean(IS_FIRST);
|
isFirst = args.getBoolean(IS_FIRST);
|
||||||
|
conversationItemId =
|
||||||
|
new MessageId(requireNonNull(args.getByteArray(ITEM_ID)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -82,55 +95,72 @@ public class ImageFragment extends Fragment {
|
|||||||
|
|
||||||
viewModel = ViewModelProviders.of(requireNonNull(getActivity()),
|
viewModel = ViewModelProviders.of(requireNonNull(getActivity()),
|
||||||
viewModelFactory).get(ImageViewModel.class);
|
viewModelFactory).get(ImageViewModel.class);
|
||||||
|
viewModel.getOnAttachmentLoaded()
|
||||||
|
.observeEvent(this, this::onAttachmentLoaded);
|
||||||
|
|
||||||
photoView = v.findViewById(R.id.photoView);
|
photoView = v.findViewById(R.id.photoView);
|
||||||
photoView.setScaleLevels(1, 2, 4);
|
photoView.setScaleLevels(1, 2, 4);
|
||||||
photoView.setOnClickListener(view -> viewModel.clickImage());
|
photoView.setOnClickListener(view -> viewModel.clickImage());
|
||||||
|
|
||||||
// Request Listener
|
if (attachment.getState() == AVAILABLE) {
|
||||||
RequestListener<Drawable> listener = new RequestListener<Drawable>() {
|
loadImage();
|
||||||
|
// postponed transition will be started when Image was loaded
|
||||||
@Override
|
} else if (attachment.getState() == ERROR) {
|
||||||
public boolean onLoadFailed(@Nullable GlideException e,
|
photoView.setImageResource(ERROR_RES);
|
||||||
Object model, Target<Drawable> target,
|
startPostponedTransition();
|
||||||
boolean isFirstResource) {
|
} else {
|
||||||
if (getActivity() != null && isFirst)
|
photoView.setImageResource(R.drawable.ic_image_missing);
|
||||||
getActivity().supportStartPostponedEnterTransition();
|
startPostponedTransition();
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onResourceReady(Drawable resource, Object model,
|
|
||||||
Target<Drawable> target, DataSource dataSource,
|
|
||||||
boolean isFirstResource) {
|
|
||||||
if (SDK_INT >= 21 && !(resource instanceof Animatable)) {
|
|
||||||
// set transition name only when not animatable,
|
|
||||||
// because the animation won't start otherwise
|
|
||||||
photoView.setTransitionName(
|
|
||||||
attachment.getTransitionName());
|
|
||||||
}
|
|
||||||
// Move image to the top if overlapping toolbar
|
|
||||||
if (viewModel.isOverlappingToolbar(photoView, resource)) {
|
|
||||||
photoView.setScaleType(FIT_START);
|
|
||||||
}
|
|
||||||
if (getActivity() != null && isFirst) {
|
|
||||||
getActivity().supportStartPostponedEnterTransition();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Load Image
|
|
||||||
GlideApp.with(this)
|
|
||||||
.load(attachment)
|
|
||||||
// TODO allow if size < maxTextureSize ?
|
|
||||||
// .override(SIZE_ORIGINAL)
|
|
||||||
.diskCacheStrategy(NONE)
|
|
||||||
.error(R.drawable.ic_image_broken)
|
|
||||||
.addListener(listener)
|
|
||||||
.into(photoView);
|
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void loadImage() {
|
||||||
|
GlideApp.with(this)
|
||||||
|
.load(attachment)
|
||||||
|
// TODO allow if size < maxTextureSize ?
|
||||||
|
// .override(SIZE_ORIGINAL)
|
||||||
|
.diskCacheStrategy(NONE)
|
||||||
|
.error(ERROR_RES)
|
||||||
|
.addListener(this)
|
||||||
|
.into(photoView);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onAttachmentLoaded(MessageId messageId) {
|
||||||
|
if (attachment.getMessageId().equals(messageId)) loadImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onLoadFailed(@Nullable GlideException e,
|
||||||
|
Object model, Target<Drawable> target,
|
||||||
|
boolean isFirstResource) {
|
||||||
|
startPostponedTransition();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onResourceReady(Drawable resource, Object model,
|
||||||
|
Target<Drawable> target, DataSource dataSource,
|
||||||
|
boolean isFirstResource) {
|
||||||
|
if (SDK_INT >= 21 && !(resource instanceof Animatable)) {
|
||||||
|
// set transition name only when not animatable,
|
||||||
|
// because the animation won't start otherwise
|
||||||
|
photoView.setTransitionName(
|
||||||
|
attachment.getTransitionName(conversationItemId));
|
||||||
|
}
|
||||||
|
// Move image to the top if overlapping toolbar
|
||||||
|
if (viewModel.isOverlappingToolbar(photoView, resource)) {
|
||||||
|
photoView.setScaleType(FIT_START);
|
||||||
|
}
|
||||||
|
startPostponedTransition();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startPostponedTransition() {
|
||||||
|
if (getActivity() != null && isFirst) {
|
||||||
|
getActivity().supportStartPostponedEnterTransition();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import android.widget.ImageView;
|
|||||||
import com.bumptech.glide.load.Transformation;
|
import com.bumptech.glide.load.Transformation;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.sync.MessageId;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.attachment.AttachmentItem;
|
import org.briarproject.briar.android.attachment.AttachmentItem;
|
||||||
import org.briarproject.briar.android.conversation.glide.BriarImageTransformation;
|
import org.briarproject.briar.android.conversation.glide.BriarImageTransformation;
|
||||||
@@ -18,8 +19,12 @@ import androidx.recyclerview.widget.RecyclerView.ViewHolder;
|
|||||||
import androidx.recyclerview.widget.StaggeredGridLayoutManager.LayoutParams;
|
import androidx.recyclerview.widget.StaggeredGridLayoutManager.LayoutParams;
|
||||||
|
|
||||||
import static android.os.Build.VERSION.SDK_INT;
|
import static android.os.Build.VERSION.SDK_INT;
|
||||||
|
import static android.widget.ImageView.ScaleType.CENTER_CROP;
|
||||||
|
import static android.widget.ImageView.ScaleType.FIT_CENTER;
|
||||||
import static com.bumptech.glide.load.engine.DiskCacheStrategy.NONE;
|
import static com.bumptech.glide.load.engine.DiskCacheStrategy.NONE;
|
||||||
import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;
|
import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;
|
||||||
|
import static org.briarproject.briar.android.attachment.AttachmentItem.State.AVAILABLE;
|
||||||
|
import static org.briarproject.briar.android.attachment.AttachmentItem.State.ERROR;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class ImageViewHolder extends ViewHolder {
|
class ImageViewHolder extends ViewHolder {
|
||||||
@@ -29,25 +34,33 @@ class ImageViewHolder extends ViewHolder {
|
|||||||
|
|
||||||
protected final ImageView imageView;
|
protected final ImageView imageView;
|
||||||
private final int imageSize;
|
private final int imageSize;
|
||||||
|
private final MessageId conversationItemId;
|
||||||
|
|
||||||
ImageViewHolder(View v, int imageSize) {
|
ImageViewHolder(View v, int imageSize, MessageId conversationItemId) {
|
||||||
super(v);
|
super(v);
|
||||||
imageView = v.findViewById(R.id.imageView);
|
imageView = v.findViewById(R.id.imageView);
|
||||||
this.imageSize = imageSize;
|
this.imageSize = imageSize;
|
||||||
|
this.conversationItemId = conversationItemId;
|
||||||
}
|
}
|
||||||
|
|
||||||
void bind(AttachmentItem attachment, Radii r, boolean single,
|
void bind(AttachmentItem attachment, Radii r, boolean single,
|
||||||
boolean needsStretch) {
|
boolean needsStretch) {
|
||||||
if (attachment.hasError()) {
|
setImageViewDimensions(attachment, single, needsStretch);
|
||||||
GlideApp.with(imageView)
|
if (attachment.getState() != AVAILABLE) {
|
||||||
.clear(imageView);
|
GlideApp.with(imageView).clear(imageView);
|
||||||
imageView.setImageResource(ERROR_RES);
|
if (attachment.getState() == ERROR) {
|
||||||
} else {
|
imageView.setImageResource(ERROR_RES);
|
||||||
setImageViewDimensions(attachment, single, needsStretch);
|
} else {
|
||||||
loadImage(attachment, r);
|
imageView.setImageResource(R.drawable.ic_image_missing);
|
||||||
if (SDK_INT >= 21) {
|
|
||||||
imageView.setTransitionName(attachment.getTransitionName());
|
|
||||||
}
|
}
|
||||||
|
imageView.setScaleType(FIT_CENTER);
|
||||||
|
} else {
|
||||||
|
loadImage(attachment, r);
|
||||||
|
imageView.setScaleType(CENTER_CROP);
|
||||||
|
}
|
||||||
|
if (SDK_INT >= 21) {
|
||||||
|
imageView.setTransitionName(
|
||||||
|
attachment.getTransitionName(conversationItemId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,13 +7,18 @@ import android.view.View;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
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.lifecycle.IoExecutor;
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.sync.MessageId;
|
||||||
import org.briarproject.briar.android.attachment.AttachmentItem;
|
import org.briarproject.briar.android.attachment.AttachmentItem;
|
||||||
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
||||||
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
||||||
import org.briarproject.briar.api.messaging.Attachment;
|
import org.briarproject.briar.api.messaging.Attachment;
|
||||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||||
|
import org.briarproject.briar.api.messaging.event.AttachmentReceivedEvent;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
@@ -41,16 +46,19 @@ import static org.briarproject.bramble.util.IoUtils.copyAndClose;
|
|||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class ImageViewModel extends AndroidViewModel {
|
public class ImageViewModel extends AndroidViewModel implements EventListener {
|
||||||
|
|
||||||
private static Logger LOG = getLogger(ImageViewModel.class.getName());
|
private static Logger LOG = getLogger(ImageViewModel.class.getName());
|
||||||
|
|
||||||
private final MessagingManager messagingManager;
|
private final MessagingManager messagingManager;
|
||||||
|
private final EventBus eventBus;
|
||||||
@DatabaseExecutor
|
@DatabaseExecutor
|
||||||
private final Executor dbExecutor;
|
private final Executor dbExecutor;
|
||||||
@IoExecutor
|
@IoExecutor
|
||||||
private final Executor ioExecutor;
|
private final Executor ioExecutor;
|
||||||
|
|
||||||
|
private final MutableLiveEvent<MessageId> attachmentLoaded =
|
||||||
|
new MutableLiveEvent<>();
|
||||||
/**
|
/**
|
||||||
* true means there was an error saving the image, false if image was saved.
|
* true means there was an error saving the image, false if image was saved.
|
||||||
*/
|
*/
|
||||||
@@ -62,13 +70,34 @@ public class ImageViewModel extends AndroidViewModel {
|
|||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ImageViewModel(Application application,
|
ImageViewModel(Application application,
|
||||||
MessagingManager messagingManager,
|
MessagingManager messagingManager, EventBus eventBus,
|
||||||
@DatabaseExecutor Executor dbExecutor,
|
@DatabaseExecutor Executor dbExecutor,
|
||||||
@IoExecutor Executor ioExecutor) {
|
@IoExecutor Executor ioExecutor) {
|
||||||
super(application);
|
super(application);
|
||||||
this.messagingManager = messagingManager;
|
this.messagingManager = messagingManager;
|
||||||
|
this.eventBus = eventBus;
|
||||||
this.dbExecutor = dbExecutor;
|
this.dbExecutor = dbExecutor;
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
|
|
||||||
|
eventBus.addListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCleared() {
|
||||||
|
super.onCleared();
|
||||||
|
eventBus.removeListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void eventOccurred(Event e) {
|
||||||
|
if (e instanceof AttachmentReceivedEvent) {
|
||||||
|
attachmentLoaded
|
||||||
|
.postEvent(((AttachmentReceivedEvent) e).getMessageId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LiveEvent<MessageId> getOnAttachmentLoaded() {
|
||||||
|
return attachmentLoaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clickImage() {
|
void clickImage() {
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import org.briarproject.bramble.api.db.DbException;
|
|||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||||
@@ -74,8 +73,7 @@ public class ContactChooserFragment extends BaseFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater,
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
|
||||||
@Nullable ViewGroup container,
|
|
||||||
@Nullable Bundle savedInstanceState) {
|
@Nullable Bundle savedInstanceState) {
|
||||||
|
|
||||||
View contentView = inflater.inflate(R.layout.list, container, false);
|
View contentView = inflater.inflate(R.layout.list, container, false);
|
||||||
@@ -129,9 +127,9 @@ public class ContactChooserFragment extends BaseFragment {
|
|||||||
ContactId id = c.getId();
|
ContactId id = c.getId();
|
||||||
GroupCount count =
|
GroupCount count =
|
||||||
conversationManager.getGroupCount(id);
|
conversationManager.getGroupCount(id);
|
||||||
ConnectionStatus status = connectionRegistry
|
boolean connected =
|
||||||
.getConnectionStatus(c.getId());
|
connectionRegistry.isConnected(c.getId());
|
||||||
contacts.add(new ContactListItem(c, status, count));
|
contacts.add(new ContactListItem(c, connected, count));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
displayContacts(contacts);
|
displayContacts(contacts);
|
||||||
|
|||||||
@@ -15,33 +15,27 @@ import android.widget.Toast;
|
|||||||
|
|
||||||
import com.google.android.material.textfield.TextInputLayout;
|
import com.google.android.material.textfield.TextInputLayout;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.crypto.DecryptionResult;
|
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
import org.briarproject.briar.android.activity.BriarActivity;
|
import org.briarproject.briar.android.activity.BriarActivity;
|
||||||
|
import org.briarproject.briar.android.controller.handler.UiResultHandler;
|
||||||
|
import org.briarproject.briar.android.util.UiUtils;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
|
||||||
import androidx.lifecycle.ViewModelProviders;
|
|
||||||
|
|
||||||
import static android.view.View.INVISIBLE;
|
import static android.view.View.INVISIBLE;
|
||||||
import static android.view.View.VISIBLE;
|
import static android.view.View.VISIBLE;
|
||||||
import static android.widget.Toast.LENGTH_LONG;
|
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.KEY_STRENGTHENER_ERROR;
|
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.SUCCESS;
|
|
||||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.QUITE_WEAK;
|
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.QUITE_WEAK;
|
||||||
import static org.briarproject.briar.android.login.LoginUtils.createKeyStrengthenerErrorDialog;
|
|
||||||
import static org.briarproject.briar.android.util.UiUtils.hideSoftKeyboard;
|
import static org.briarproject.briar.android.util.UiUtils.hideSoftKeyboard;
|
||||||
import static org.briarproject.briar.android.util.UiUtils.setError;
|
|
||||||
import static org.briarproject.briar.android.util.UiUtils.showSoftKeyboard;
|
import static org.briarproject.briar.android.util.UiUtils.showSoftKeyboard;
|
||||||
|
|
||||||
public class ChangePasswordActivity extends BriarActivity
|
public class ChangePasswordActivity extends BriarActivity
|
||||||
implements OnClickListener, OnEditorActionListener {
|
implements OnClickListener, OnEditorActionListener {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ViewModelProvider.Factory viewModelFactory;
|
protected ChangePasswordController passwordController;
|
||||||
|
|
||||||
private TextInputLayout currentPasswordEntryWrapper;
|
private TextInputLayout currentPasswordEntryWrapper;
|
||||||
private TextInputLayout newPasswordEntryWrapper;
|
private TextInputLayout newPasswordEntryWrapper;
|
||||||
@@ -53,17 +47,11 @@ public class ChangePasswordActivity extends BriarActivity
|
|||||||
private Button changePasswordButton;
|
private Button changePasswordButton;
|
||||||
private ProgressBar progress;
|
private ProgressBar progress;
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
ChangePasswordViewModel viewModel;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle state) {
|
public void onCreate(Bundle state) {
|
||||||
super.onCreate(state);
|
super.onCreate(state);
|
||||||
setContentView(R.layout.activity_change_password);
|
setContentView(R.layout.activity_change_password);
|
||||||
|
|
||||||
viewModel = ViewModelProviders.of(this, viewModelFactory)
|
|
||||||
.get(ChangePasswordViewModel.class);
|
|
||||||
|
|
||||||
currentPasswordEntryWrapper =
|
currentPasswordEntryWrapper =
|
||||||
findViewById(R.id.current_password_entry_wrapper);
|
findViewById(R.id.current_password_entry_wrapper);
|
||||||
newPasswordEntryWrapper = findViewById(R.id.new_password_entry_wrapper);
|
newPasswordEntryWrapper = findViewById(R.id.new_password_entry_wrapper);
|
||||||
@@ -114,12 +102,13 @@ public class ChangePasswordActivity extends BriarActivity
|
|||||||
String firstPassword = newPassword.getText().toString();
|
String firstPassword = newPassword.getText().toString();
|
||||||
String secondPassword = newPasswordConfirmation.getText().toString();
|
String secondPassword = newPasswordConfirmation.getText().toString();
|
||||||
boolean passwordsMatch = firstPassword.equals(secondPassword);
|
boolean passwordsMatch = firstPassword.equals(secondPassword);
|
||||||
float strength = viewModel.estimatePasswordStrength(firstPassword);
|
float strength =
|
||||||
|
passwordController.estimatePasswordStrength(firstPassword);
|
||||||
strengthMeter.setStrength(strength);
|
strengthMeter.setStrength(strength);
|
||||||
setError(newPasswordEntryWrapper,
|
UiUtils.setError(newPasswordEntryWrapper,
|
||||||
getString(R.string.password_too_weak),
|
getString(R.string.password_too_weak),
|
||||||
firstPassword.length() > 0 && strength < QUITE_WEAK);
|
firstPassword.length() > 0 && strength < QUITE_WEAK);
|
||||||
setError(newPasswordConfirmationWrapper,
|
UiUtils.setError(newPasswordConfirmationWrapper,
|
||||||
getString(R.string.passwords_do_not_match),
|
getString(R.string.passwords_do_not_match),
|
||||||
secondPassword.length() > 0 && !passwordsMatch);
|
secondPassword.length() > 0 && !passwordsMatch);
|
||||||
changePasswordButton.setEnabled(
|
changePasswordButton.setEnabled(
|
||||||
@@ -138,34 +127,32 @@ public class ChangePasswordActivity extends BriarActivity
|
|||||||
// Replace the button with a progress bar
|
// Replace the button with a progress bar
|
||||||
changePasswordButton.setVisibility(INVISIBLE);
|
changePasswordButton.setVisibility(INVISIBLE);
|
||||||
progress.setVisibility(VISIBLE);
|
progress.setVisibility(VISIBLE);
|
||||||
|
passwordController.changePassword(currentPassword.getText().toString(),
|
||||||
String curPwd = currentPassword.getText().toString();
|
newPassword.getText().toString(),
|
||||||
String newPwd = newPassword.getText().toString();
|
new UiResultHandler<Boolean>(this) {
|
||||||
viewModel.changePassword(curPwd, newPwd).observeEvent(this, result -> {
|
@Override
|
||||||
if (result == SUCCESS) {
|
public void onResultUi(@NonNull Boolean result) {
|
||||||
Toast.makeText(ChangePasswordActivity.this,
|
if (result) {
|
||||||
R.string.password_changed,
|
Toast.makeText(ChangePasswordActivity.this,
|
||||||
LENGTH_LONG).show();
|
R.string.password_changed,
|
||||||
setResult(RESULT_OK);
|
Toast.LENGTH_LONG).show();
|
||||||
supportFinishAfterTransition();
|
setResult(RESULT_OK);
|
||||||
} else {
|
supportFinishAfterTransition();
|
||||||
tryAgain(result);
|
} else {
|
||||||
|
tryAgain();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tryAgain(DecryptionResult result) {
|
private void tryAgain() {
|
||||||
|
UiUtils.setError(currentPasswordEntryWrapper,
|
||||||
|
getString(R.string.try_again), true);
|
||||||
changePasswordButton.setVisibility(VISIBLE);
|
changePasswordButton.setVisibility(VISIBLE);
|
||||||
progress.setVisibility(INVISIBLE);
|
progress.setVisibility(INVISIBLE);
|
||||||
if (result == KEY_STRENGTHENER_ERROR) {
|
currentPassword.setText("");
|
||||||
createKeyStrengthenerErrorDialog(this).show();
|
|
||||||
} else {
|
// show the keyboard again
|
||||||
setError(currentPasswordEntryWrapper,
|
showSoftKeyboard(currentPassword);
|
||||||
getString(R.string.try_again), true);
|
|
||||||
currentPassword.setText("");
|
|
||||||
// show the keyboard again
|
|
||||||
showSoftKeyboard(currentPassword);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package org.briarproject.briar.android.login;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.account.AccountManager;
|
||||||
|
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
|
||||||
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.briar.android.controller.handler.ResultHandler;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
|
public class ChangePasswordControllerImpl implements ChangePasswordController {
|
||||||
|
|
||||||
|
protected final AccountManager accountManager;
|
||||||
|
protected final Executor ioExecutor;
|
||||||
|
private final PasswordStrengthEstimator strengthEstimator;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
ChangePasswordControllerImpl(AccountManager accountManager,
|
||||||
|
@IoExecutor Executor ioExecutor,
|
||||||
|
PasswordStrengthEstimator strengthEstimator) {
|
||||||
|
this.accountManager = accountManager;
|
||||||
|
this.ioExecutor = ioExecutor;
|
||||||
|
this.strengthEstimator = strengthEstimator;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float estimatePasswordStrength(String password) {
|
||||||
|
return strengthEstimator.estimateStrength(password);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void changePassword(String oldPassword, String newPassword,
|
||||||
|
ResultHandler<Boolean> resultHandler) {
|
||||||
|
ioExecutor.execute(() -> {
|
||||||
|
boolean changed =
|
||||||
|
accountManager.changePassword(oldPassword, newPassword);
|
||||||
|
resultHandler.onResult(changed);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
package org.briarproject.briar.android.login;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.account.AccountManager;
|
|
||||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
|
||||||
import org.briarproject.bramble.api.crypto.DecryptionResult;
|
|
||||||
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
|
|
||||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
|
||||||
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import androidx.lifecycle.ViewModel;
|
|
||||||
|
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.SUCCESS;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
public class ChangePasswordViewModel extends ViewModel {
|
|
||||||
|
|
||||||
private final AccountManager accountManager;
|
|
||||||
private final Executor ioExecutor;
|
|
||||||
private final PasswordStrengthEstimator strengthEstimator;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
ChangePasswordViewModel(AccountManager accountManager,
|
|
||||||
@IoExecutor Executor ioExecutor,
|
|
||||||
PasswordStrengthEstimator strengthEstimator) {
|
|
||||||
this.accountManager = accountManager;
|
|
||||||
this.ioExecutor = ioExecutor;
|
|
||||||
this.strengthEstimator = strengthEstimator;
|
|
||||||
}
|
|
||||||
|
|
||||||
float estimatePasswordStrength(String password) {
|
|
||||||
return strengthEstimator.estimateStrength(password);
|
|
||||||
}
|
|
||||||
|
|
||||||
LiveEvent<DecryptionResult> changePassword(String oldPassword,
|
|
||||||
String newPassword) {
|
|
||||||
MutableLiveEvent<DecryptionResult> result = new MutableLiveEvent<>();
|
|
||||||
ioExecutor.execute(() -> {
|
|
||||||
try {
|
|
||||||
accountManager.changePassword(oldPassword, newPassword);
|
|
||||||
result.postEvent(SUCCESS);
|
|
||||||
} catch (DecryptionException e) {
|
|
||||||
result.postEvent(e.getDecryptionResult());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package org.briarproject.briar.android.login;
|
|
||||||
|
|
||||||
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 LoginModule {
|
|
||||||
|
|
||||||
@Binds
|
|
||||||
@IntoMap
|
|
||||||
@ViewModelKey(StartupViewModel.class)
|
|
||||||
abstract ViewModel bindStartupViewModel(StartupViewModel viewModel);
|
|
||||||
|
|
||||||
@Binds
|
|
||||||
@IntoMap
|
|
||||||
@ViewModelKey(ChangePasswordViewModel.class)
|
|
||||||
abstract ViewModel bindChangePasswordViewModel(
|
|
||||||
ChangePasswordViewModel viewModel);
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
package org.briarproject.briar.android.login;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.graphics.drawable.Drawable;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
import org.briarproject.briar.R;
|
|
||||||
|
|
||||||
import androidx.appcompat.app.AlertDialog;
|
|
||||||
|
|
||||||
import static androidx.core.content.ContextCompat.getColor;
|
|
||||||
import static androidx.core.content.ContextCompat.getDrawable;
|
|
||||||
import static androidx.core.graphics.drawable.DrawableCompat.setTint;
|
|
||||||
import static java.util.Objects.requireNonNull;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
class LoginUtils {
|
|
||||||
|
|
||||||
static AlertDialog createKeyStrengthenerErrorDialog(Context ctx) {
|
|
||||||
AlertDialog.Builder builder =
|
|
||||||
new AlertDialog.Builder(ctx, R.style.BriarDialogTheme);
|
|
||||||
Drawable icon = getDrawable(ctx, R.drawable.alerts_and_states_error);
|
|
||||||
setTint(requireNonNull(icon), getColor(ctx, R.color.color_primary));
|
|
||||||
builder.setIcon(icon);
|
|
||||||
builder.setTitle(R.string.dialog_title_cannot_check_password);
|
|
||||||
builder.setMessage(R.string.dialog_message_cannot_check_password);
|
|
||||||
builder.setPositiveButton(R.string.ok, null);
|
|
||||||
return builder.create();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -12,7 +12,6 @@ import android.widget.ProgressBar;
|
|||||||
import com.google.android.material.textfield.TextInputEditText;
|
import com.google.android.material.textfield.TextInputEditText;
|
||||||
import com.google.android.material.textfield.TextInputLayout;
|
import com.google.android.material.textfield.TextInputLayout;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.crypto.DecryptionResult;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
@@ -29,9 +28,6 @@ import androidx.lifecycle.ViewModelProviders;
|
|||||||
import static android.view.View.INVISIBLE;
|
import static android.view.View.INVISIBLE;
|
||||||
import static android.view.View.VISIBLE;
|
import static android.view.View.VISIBLE;
|
||||||
import static android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
|
import static android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.KEY_STRENGTHENER_ERROR;
|
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.SUCCESS;
|
|
||||||
import static org.briarproject.briar.android.login.LoginUtils.createKeyStrengthenerErrorDialog;
|
|
||||||
import static org.briarproject.briar.android.util.UiUtils.enterPressed;
|
import static org.briarproject.briar.android.util.UiUtils.enterPressed;
|
||||||
import static org.briarproject.briar.android.util.UiUtils.hideSoftKeyboard;
|
import static org.briarproject.briar.android.util.UiUtils.hideSoftKeyboard;
|
||||||
import static org.briarproject.briar.android.util.UiUtils.setError;
|
import static org.briarproject.briar.android.util.UiUtils.setError;
|
||||||
@@ -62,13 +58,12 @@ public class PasswordFragment extends BaseFragment implements TextWatcher {
|
|||||||
@Nullable ViewGroup container,
|
@Nullable ViewGroup container,
|
||||||
@Nullable Bundle savedInstanceState) {
|
@Nullable Bundle savedInstanceState) {
|
||||||
View v = inflater.inflate(R.layout.fragment_password, container,
|
View v = inflater.inflate(R.layout.fragment_password, container,
|
||||||
false);
|
false);
|
||||||
|
|
||||||
viewModel = ViewModelProviders.of(requireActivity(), viewModelFactory)
|
viewModel = ViewModelProviders.of(requireActivity(), viewModelFactory)
|
||||||
.get(StartupViewModel.class);
|
.get(StartupViewModel.class);
|
||||||
|
viewModel.getPasswordValidated().observeEvent(this, valid -> {
|
||||||
viewModel.getPasswordValidated().observeEvent(this, result -> {
|
if (!valid) onPasswordInvalid();
|
||||||
if (result != SUCCESS) onPasswordInvalid(result);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
signInButton = v.findViewById(R.id.btn_sign_in);
|
signInButton = v.findViewById(R.id.btn_sign_in);
|
||||||
@@ -112,20 +107,18 @@ public class PasswordFragment extends BaseFragment implements TextWatcher {
|
|||||||
viewModel.validatePassword(password.getText().toString());
|
viewModel.validatePassword(password.getText().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onPasswordInvalid(DecryptionResult result) {
|
private void onPasswordInvalid() {
|
||||||
|
setError(input, getString(R.string.try_again), true);
|
||||||
signInButton.setVisibility(VISIBLE);
|
signInButton.setVisibility(VISIBLE);
|
||||||
progress.setVisibility(INVISIBLE);
|
progress.setVisibility(INVISIBLE);
|
||||||
if (result == KEY_STRENGTHENER_ERROR) {
|
password.setText(null);
|
||||||
createKeyStrengthenerErrorDialog(requireContext()).show();
|
|
||||||
} else {
|
// show the keyboard again
|
||||||
setError(input, getString(R.string.try_again), true);
|
showSoftKeyboard(password);
|
||||||
password.setText(null);
|
|
||||||
// show the keyboard again
|
|
||||||
showSoftKeyboard(password);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onForgottenPasswordClick() {
|
public void onForgottenPasswordClick() {
|
||||||
|
// TODO Encapsulate the dialog in a re-usable fragment
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext(),
|
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext(),
|
||||||
R.style.BriarDialogTheme);
|
R.style.BriarDialogTheme);
|
||||||
builder.setTitle(R.string.dialog_title_lost_password);
|
builder.setTitle(R.string.dialog_title_lost_password);
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ package org.briarproject.briar.android.login;
|
|||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.account.AccountManager;
|
import org.briarproject.bramble.api.account.AccountManager;
|
||||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
|
||||||
import org.briarproject.bramble.api.crypto.DecryptionResult;
|
|
||||||
import org.briarproject.bramble.api.event.Event;
|
import org.briarproject.bramble.api.event.Event;
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.event.EventListener;
|
import org.briarproject.bramble.api.event.EventListener;
|
||||||
@@ -26,7 +24,6 @@ import androidx.lifecycle.AndroidViewModel;
|
|||||||
import androidx.lifecycle.LiveData;
|
import androidx.lifecycle.LiveData;
|
||||||
import androidx.lifecycle.MutableLiveData;
|
import androidx.lifecycle.MutableLiveData;
|
||||||
|
|
||||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.SUCCESS;
|
|
||||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.COMPACTING_DATABASE;
|
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.COMPACTING_DATABASE;
|
||||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.MIGRATING_DATABASE;
|
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.MIGRATING_DATABASE;
|
||||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.STARTING_SERVICES;
|
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.STARTING_SERVICES;
|
||||||
@@ -49,7 +46,7 @@ public class StartupViewModel extends AndroidViewModel
|
|||||||
@IoExecutor
|
@IoExecutor
|
||||||
private final Executor ioExecutor;
|
private final Executor ioExecutor;
|
||||||
|
|
||||||
private final MutableLiveEvent<DecryptionResult> passwordValidated =
|
private final MutableLiveEvent<Boolean> passwordValidated =
|
||||||
new MutableLiveEvent<>();
|
new MutableLiveEvent<>();
|
||||||
private final MutableLiveEvent<Boolean> accountDeleted =
|
private final MutableLiveEvent<Boolean> accountDeleted =
|
||||||
new MutableLiveEvent<>();
|
new MutableLiveEvent<>();
|
||||||
@@ -108,17 +105,13 @@ public class StartupViewModel extends AndroidViewModel
|
|||||||
|
|
||||||
void validatePassword(String password) {
|
void validatePassword(String password) {
|
||||||
ioExecutor.execute(() -> {
|
ioExecutor.execute(() -> {
|
||||||
try {
|
boolean signedIn = accountManager.signIn(password);
|
||||||
accountManager.signIn(password);
|
passwordValidated.postEvent(signedIn);
|
||||||
passwordValidated.postEvent(SUCCESS);
|
if (signedIn) state.postValue(SIGNED_IN);
|
||||||
state.postValue(SIGNED_IN);
|
|
||||||
} catch (DecryptionException e) {
|
|
||||||
passwordValidated.postEvent(e.getDecryptionResult());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
LiveEvent<DecryptionResult> getPasswordValidated() {
|
LiveEvent<Boolean> getPasswordValidated() {
|
||||||
return passwordValidated;
|
return passwordValidated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import org.briarproject.briar.api.privategroup.GroupMessageHeader;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.UiThread;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
@@ -19,10 +21,7 @@ interface GroupListController extends DbController {
|
|||||||
* The listener must be set right after the controller was injected
|
* The listener must be set right after the controller was injected
|
||||||
*/
|
*/
|
||||||
@UiThread
|
@UiThread
|
||||||
void setGroupListListener(GroupListListener listener);
|
void setGroupListListener(@Nullable GroupListListener listener);
|
||||||
|
|
||||||
@UiThread
|
|
||||||
void unsetGroupListListener(GroupListListener listener);
|
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
void onStart();
|
void onStart();
|
||||||
|
|||||||
@@ -80,15 +80,10 @@ class GroupListControllerImpl extends DbControllerImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setGroupListListener(GroupListListener listener) {
|
public void setGroupListListener(@Nullable GroupListListener listener) {
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void unsetGroupListListener(GroupListListener listener) {
|
|
||||||
if (this.listener == listener) this.listener = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@CallSuper
|
@CallSuper
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ public class GroupListFragment extends BaseFragment implements
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
controller.unsetGroupListListener(this);
|
controller.setGroupListListener(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ public class GroupMemberListActivity extends BriarActivity
|
|||||||
supportFinishAfterTransition();
|
supportFinishAfterTransition();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO ConnectionStatusChangedEvent
|
// TODO ContactConnectedEvent and ContactDisconnectedEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import org.briarproject.bramble.api.db.DatabaseExecutor;
|
|||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
|
||||||
import org.briarproject.bramble.api.sync.GroupId;
|
import org.briarproject.bramble.api.sync.GroupId;
|
||||||
import org.briarproject.briar.android.controller.DbControllerImpl;
|
import org.briarproject.briar.android.controller.DbControllerImpl;
|
||||||
import org.briarproject.briar.android.controller.handler.ResultExceptionHandler;
|
import org.briarproject.briar.android.controller.handler.ResultExceptionHandler;
|
||||||
@@ -20,7 +19,6 @@ import java.util.logging.Logger;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.DISCONNECTED;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
|
||||||
class GroupMemberListControllerImpl extends DbControllerImpl
|
class GroupMemberListControllerImpl extends DbControllerImpl
|
||||||
@@ -52,11 +50,10 @@ class GroupMemberListControllerImpl extends DbControllerImpl
|
|||||||
privateGroupManager.getMembers(groupId);
|
privateGroupManager.getMembers(groupId);
|
||||||
for (GroupMember m : members) {
|
for (GroupMember m : members) {
|
||||||
ContactId c = m.getContactId();
|
ContactId c = m.getContactId();
|
||||||
ConnectionStatus status = DISCONNECTED;
|
boolean online = false;
|
||||||
if (c != null) {
|
if (c != null)
|
||||||
status = connectionRegistry.getConnectionStatus(c);
|
online = connectionRegistry.isConnected(c);
|
||||||
}
|
items.add(new MemberListItem(m, online));
|
||||||
items.add(new MemberListItem(m, status));
|
|
||||||
}
|
}
|
||||||
handler.onResult(items);
|
handler.onResult(items);
|
||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ class MemberListAdapter extends
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean areContentsTheSame(MemberListItem m1, MemberListItem m2) {
|
public boolean areContentsTheSame(MemberListItem m1, MemberListItem m2) {
|
||||||
if (m1.getConnectionStatus() != m2.getConnectionStatus()) return false;
|
if (m1.isOnline() != m2.isOnline()) return false;
|
||||||
if (m1.getContactId() != m2.getContactId()) return false;
|
if (m1.getContactId() != m2.getContactId()) return false;
|
||||||
if (m1.getStatus() != m2.getStatus()) return false;
|
if (m1.getStatus() != m2.getStatus()) return false;
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import org.briarproject.bramble.api.identity.Author;
|
|||||||
import org.briarproject.bramble.api.identity.AuthorInfo;
|
import org.briarproject.bramble.api.identity.AuthorInfo;
|
||||||
import org.briarproject.bramble.api.identity.AuthorInfo.Status;
|
import org.briarproject.bramble.api.identity.AuthorInfo.Status;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
|
||||||
import org.briarproject.briar.api.privategroup.GroupMember;
|
import org.briarproject.briar.api.privategroup.GroupMember;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
@@ -16,11 +15,11 @@ import javax.annotation.concurrent.NotThreadSafe;
|
|||||||
class MemberListItem {
|
class MemberListItem {
|
||||||
|
|
||||||
private final GroupMember groupMember;
|
private final GroupMember groupMember;
|
||||||
private ConnectionStatus status;
|
private boolean online;
|
||||||
|
|
||||||
MemberListItem(GroupMember groupMember, ConnectionStatus status) {
|
MemberListItem(GroupMember groupMember, boolean online) {
|
||||||
this.groupMember = groupMember;
|
this.groupMember = groupMember;
|
||||||
this.status = status;
|
this.online = online;
|
||||||
}
|
}
|
||||||
|
|
||||||
Author getMember() {
|
Author getMember() {
|
||||||
@@ -44,12 +43,12 @@ class MemberListItem {
|
|||||||
return groupMember.getContactId();
|
return groupMember.getContactId();
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionStatus getConnectionStatus() {
|
boolean isOnline() {
|
||||||
return status;
|
return online;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setConnectionStatus(ConnectionStatus status) {
|
void setOnline(boolean online) {
|
||||||
this.status = status;
|
this.online = online;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import android.widget.ImageView;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.view.AuthorView;
|
import org.briarproject.briar.android.view.AuthorView;
|
||||||
|
|
||||||
@@ -15,8 +14,6 @@ import androidx.recyclerview.widget.RecyclerView;
|
|||||||
import static android.view.View.GONE;
|
import static android.view.View.GONE;
|
||||||
import static android.view.View.VISIBLE;
|
import static android.view.View.VISIBLE;
|
||||||
import static org.briarproject.bramble.api.identity.AuthorInfo.Status.OURSELVES;
|
import static org.briarproject.bramble.api.identity.AuthorInfo.Status.OURSELVES;
|
||||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.CONNECTED;
|
|
||||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.RECENTLY_CONNECTED;
|
|
||||||
import static org.briarproject.briar.android.util.UiUtils.getContactDisplayName;
|
import static org.briarproject.briar.android.util.UiUtils.getContactDisplayName;
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
@@ -41,13 +38,10 @@ class MemberListItemHolder extends RecyclerView.ViewHolder {
|
|||||||
// online status of visible contacts
|
// online status of visible contacts
|
||||||
if (item.getContactId() != null) {
|
if (item.getContactId() != null) {
|
||||||
bulb.setVisibility(VISIBLE);
|
bulb.setVisibility(VISIBLE);
|
||||||
ConnectionStatus status = item.getConnectionStatus();
|
if (item.isOnline()) {
|
||||||
if (status == CONNECTED) {
|
bulb.setImageResource(R.drawable.contact_connected);
|
||||||
bulb.setImageResource(R.drawable.ic_connected);
|
|
||||||
} else if (status == RECENTLY_CONNECTED) {
|
|
||||||
bulb.setImageResource(R.drawable.ic_recently_connected);
|
|
||||||
} else {
|
} else {
|
||||||
bulb.setImageResource(R.drawable.ic_disconnected);
|
bulb.setImageResource(R.drawable.contact_disconnected);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bulb.setVisibility(GONE);
|
bulb.setVisibility(GONE);
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import org.briarproject.bramble.api.event.EventListener;
|
|||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
|
||||||
import org.briarproject.bramble.api.sync.GroupId;
|
import org.briarproject.bramble.api.sync.GroupId;
|
||||||
import org.briarproject.bramble.api.sync.event.GroupRemovedEvent;
|
import org.briarproject.bramble.api.sync.event.GroupRemovedEvent;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
@@ -105,7 +104,7 @@ abstract class SharingStatusActivity extends BriarActivity
|
|||||||
supportFinishAfterTransition();
|
supportFinishAfterTransition();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO ConnectionStatusChangedEvent
|
// TODO ContactConnectedEvent and ContactDisconnectedEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -135,9 +134,8 @@ abstract class SharingStatusActivity extends BriarActivity
|
|||||||
try {
|
try {
|
||||||
List<ContactItem> contactItems = new ArrayList<>();
|
List<ContactItem> contactItems = new ArrayList<>();
|
||||||
for (Contact c : getSharedWith()) {
|
for (Contact c : getSharedWith()) {
|
||||||
ConnectionStatus status =
|
boolean online = connectionRegistry.isConnected(c.getId());
|
||||||
connectionRegistry.getConnectionStatus(c.getId());
|
ContactItem item = new ContactItem(c, online);
|
||||||
ContactItem item = new ContactItem(c, status);
|
|
||||||
contactItems.add(item);
|
contactItems.add(item);
|
||||||
}
|
}
|
||||||
displaySharedWith(contactItems);
|
displaySharedWith(contactItems);
|
||||||
|
|||||||
@@ -238,6 +238,10 @@ public class UiUtils {
|
|||||||
return "avatar" + c.getInt();
|
return "avatar" + c.getInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getBulbTransitionName(ContactId c) {
|
||||||
|
return "bulb" + c.getInt();
|
||||||
|
}
|
||||||
|
|
||||||
public static OnClickListener getGoToSettingsListener(Context context) {
|
public static OnClickListener getGoToSettingsListener(Context context) {
|
||||||
return (dialog, which) -> {
|
return (dialog, which) -> {
|
||||||
Intent i = new Intent();
|
Intent i = new Intent();
|
||||||
@@ -377,7 +381,7 @@ public class UiUtils {
|
|||||||
/**
|
/**
|
||||||
* Same as {@link #observeOnce(LiveData, LifecycleOwner, Observer)},
|
* Same as {@link #observeOnce(LiveData, LifecycleOwner, Observer)},
|
||||||
* but without a {@link LifecycleOwner}.
|
* but without a {@link LifecycleOwner}.
|
||||||
* <p>
|
*
|
||||||
* Warning: Do NOT call from objects that have a lifecycle.
|
* Warning: Do NOT call from objects that have a lifecycle.
|
||||||
*/
|
*/
|
||||||
@UiThread
|
@UiThread
|
||||||
@@ -397,4 +401,5 @@ public class UiUtils {
|
|||||||
return ctx.getResources().getConfiguration().getLayoutDirection() ==
|
return ctx.getResources().getConfiguration().getLayoutDirection() ==
|
||||||
LAYOUT_DIRECTION_RTL;
|
LAYOUT_DIRECTION_RTL;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import org.briarproject.briar.android.contact.add.remote.AddContactViewModel;
|
|||||||
import org.briarproject.briar.android.contact.add.remote.PendingContactListViewModel;
|
import org.briarproject.briar.android.contact.add.remote.PendingContactListViewModel;
|
||||||
import org.briarproject.briar.android.conversation.ConversationViewModel;
|
import org.briarproject.briar.android.conversation.ConversationViewModel;
|
||||||
import org.briarproject.briar.android.conversation.ImageViewModel;
|
import org.briarproject.briar.android.conversation.ImageViewModel;
|
||||||
|
import org.briarproject.briar.android.login.StartupViewModel;
|
||||||
|
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
@@ -16,6 +17,11 @@ import dagger.multibindings.IntoMap;
|
|||||||
@Module
|
@Module
|
||||||
public abstract class ViewModelModule {
|
public abstract class ViewModelModule {
|
||||||
|
|
||||||
|
@Binds
|
||||||
|
@IntoMap
|
||||||
|
@ViewModelKey(StartupViewModel.class)
|
||||||
|
abstract ViewModel bindStartupViewModel(StartupViewModel startupViewModel);
|
||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@IntoMap
|
@IntoMap
|
||||||
@ViewModelKey(ConversationViewModel.class)
|
@ViewModelKey(ConversationViewModel.class)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package org.briarproject.briar.android.widget;
|
package org.briarproject.briar.android.widget;
|
||||||
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@@ -11,7 +10,6 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
@@ -24,7 +22,6 @@ import androidx.fragment.app.DialogFragment;
|
|||||||
|
|
||||||
import static android.content.Intent.ACTION_VIEW;
|
import static android.content.Intent.ACTION_VIEW;
|
||||||
import static android.content.pm.PackageManager.MATCH_DEFAULT_ONLY;
|
import static android.content.pm.PackageManager.MATCH_DEFAULT_ONLY;
|
||||||
import static android.widget.Toast.LENGTH_SHORT;
|
|
||||||
import static java.util.Objects.requireNonNull;
|
import static java.util.Objects.requireNonNull;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@@ -67,23 +64,18 @@ public class LinkDialogFragment extends DialogFragment {
|
|||||||
urlView.setText(url);
|
urlView.setText(url);
|
||||||
|
|
||||||
// prepare normal intent or intent chooser
|
// prepare normal intent or intent chooser
|
||||||
Context ctx = requireContext();
|
|
||||||
Intent i = new Intent(ACTION_VIEW, Uri.parse(url));
|
Intent i = new Intent(ACTION_VIEW, Uri.parse(url));
|
||||||
PackageManager packageManager = ctx.getPackageManager();
|
PackageManager packageManager =
|
||||||
List activities =
|
requireNonNull(getContext()).getPackageManager();
|
||||||
packageManager.queryIntentActivities(i, MATCH_DEFAULT_ONLY);
|
List activities = packageManager.queryIntentActivities(i,
|
||||||
|
MATCH_DEFAULT_ONLY);
|
||||||
boolean choice = activities.size() > 1;
|
boolean choice = activities.size() > 1;
|
||||||
Intent intent = choice ? Intent.createChooser(i,
|
Intent intent = choice ? Intent.createChooser(i,
|
||||||
getString(R.string.link_warning_open_link)) : i;
|
getString(R.string.link_warning_open_link)) : i;
|
||||||
|
|
||||||
Button openButton = v.findViewById(R.id.openButton);
|
Button openButton = v.findViewById(R.id.openButton);
|
||||||
openButton.setOnClickListener(v1 -> {
|
openButton.setOnClickListener(v1 -> {
|
||||||
if (intent.resolveActivity(packageManager) != null) {
|
startActivity(intent);
|
||||||
startActivity(intent);
|
|
||||||
} else {
|
|
||||||
Toast.makeText(ctx, R.string.error_start_activity, LENGTH_SHORT)
|
|
||||||
.show();
|
|
||||||
}
|
|
||||||
getDialog().dismiss();
|
getDialog().dismiss();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportHeight="24"
|
||||||
|
android:viewportWidth="24">
|
||||||
|
|
||||||
|
<path
|
||||||
|
android:fillColor="#abffffff"
|
||||||
|
android:pathData="M12,2 C6.48,2,2,6.48,2,12 S6.48,22,12,22 S22,17.52,22,12 S17.52,2,12,2 Z M12,20
|
||||||
|
C7.58,20,4,16.42,4,12 S7.58,4,12,4 S20,7.58,20,12 S16.42,20,12,20 Z"/>
|
||||||
|
|
||||||
|
<path
|
||||||
|
android:fillColor="#95d220"
|
||||||
|
android:pathData="M10.8972,19.9503 C6.5514,19.3493,3.43091,15.2154,4.0625,10.896
|
||||||
|
C4.55452,7.53099,7.09451,4.8236,10.394,4.14714
|
||||||
|
C14.2569,3.35517,18.1698,5.54347,19.5236,9.25295
|
||||||
|
C20.0698,10.7495,20.1616,12.4612,19.777,13.9758
|
||||||
|
C19.5457,14.8864,18.8106,16.3388,18.2072,17.0771
|
||||||
|
C16.4904,19.1779,13.581,20.3215,10.8973,19.9503 Z"
|
||||||
|
android:strokeLineCap="round"
|
||||||
|
android:strokeLineJoin="round"
|
||||||
|
android:strokeWidth="0.76779664"/>
|
||||||
|
</vector>
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user