mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-24 16:49:55 +01:00
Compare commits
1 Commits
social-bac
...
social-bac
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
995a053241 |
9
.idea/codeStyles/Project.xml
generated
9
.idea/codeStyles/Project.xml
generated
@@ -31,6 +31,15 @@
|
|||||||
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
|
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
|
||||||
<value />
|
<value />
|
||||||
</option>
|
</option>
|
||||||
|
<option name="PACKAGES_IMPORT_LAYOUT">
|
||||||
|
<value>
|
||||||
|
<package name="" alias="false" withSubpackages="true" />
|
||||||
|
<package name="java" alias="false" withSubpackages="true" />
|
||||||
|
<package name="javax" alias="false" withSubpackages="true" />
|
||||||
|
<package name="kotlin" alias="false" withSubpackages="true" />
|
||||||
|
<package name="" alias="true" withSubpackages="true" />
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" />
|
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" />
|
||||||
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" />
|
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" />
|
||||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||||
|
|||||||
@@ -8,15 +8,11 @@ android {
|
|||||||
compileSdkVersion 30
|
compileSdkVersion 30
|
||||||
buildToolsVersion '30.0.2'
|
buildToolsVersion '30.0.2'
|
||||||
|
|
||||||
packagingOptions {
|
|
||||||
doNotStrip '**/*.so'
|
|
||||||
}
|
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
minSdkVersion 16
|
minSdkVersion 16
|
||||||
targetSdkVersion 29
|
targetSdkVersion 29
|
||||||
versionCode 10218
|
versionCode 10216
|
||||||
versionName "1.2.18"
|
versionName "1.2.16"
|
||||||
consumerProguardFiles 'proguard-rules.txt'
|
consumerProguardFiles 'proguard-rules.txt'
|
||||||
|
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
|
|||||||
@@ -175,11 +175,6 @@ class AndroidBluetoothPlugin
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
IoUtils.tryToClose(s, LOG, WARNING);
|
IoUtils.tryToClose(s, LOG, WARNING);
|
||||||
throw e;
|
throw e;
|
||||||
} catch (NullPointerException e) {
|
|
||||||
// BluetoothSocket#connect() may throw an NPE under unknown
|
|
||||||
// circumstances
|
|
||||||
IoUtils.tryToClose(s, LOG, WARNING);
|
|
||||||
throw new IOException(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -114,8 +114,12 @@ public class AndroidUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an array of supported content types for image attachments.
|
* Returns an array of supported content types for image attachments.
|
||||||
|
* GIFs can't be compressed on API < 24 so they're not supported.
|
||||||
|
* <p>
|
||||||
|
* TODO: Remove this restriction when large message support is added
|
||||||
*/
|
*/
|
||||||
public static String[] getSupportedImageContentTypes() {
|
public static String[] getSupportedImageContentTypes() {
|
||||||
return new String[] {"image/jpeg", "image/png", "image/gif"};
|
if (SDK_INT < 24) return new String[] {"image/jpeg", "image/png"};
|
||||||
|
else return new String[] {"image/jpeg", "image/png", "image/gif"};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package org.briarproject.bramble.api.account;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
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.Identity;
|
|
||||||
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;
|
||||||
|
|
||||||
@@ -44,17 +43,6 @@ public interface AccountManager {
|
|||||||
*/
|
*/
|
||||||
boolean createAccount(String name, String password);
|
boolean createAccount(String name, String password);
|
||||||
|
|
||||||
/**
|
|
||||||
* Restores a given identity by registering it with the
|
|
||||||
* {@link IdentityManager}. Creates a database key, encrypts it with the
|
|
||||||
* given password and stores it on disk. {@link #accountExists()} will
|
|
||||||
* return true after this method returns true.
|
|
||||||
* @param identity
|
|
||||||
* @param password
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
boolean restoreAccount(Identity identity, String password);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes all account state from disk. {@link #accountExists()} will
|
* Deletes all account state from disk. {@link #accountExists()} will
|
||||||
* return false after this method returns.
|
* return false after this method returns.
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.contact;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Record types for the contact exchange protocol.
|
|
||||||
*/
|
|
||||||
public interface ContactExchangeRecordTypes {
|
|
||||||
|
|
||||||
byte CONTACT_INFO = 0;
|
|
||||||
}
|
|
||||||
@@ -3,7 +3,6 @@ package org.briarproject.bramble.api.contact;
|
|||||||
import org.briarproject.bramble.api.FormatException;
|
import org.briarproject.bramble.api.FormatException;
|
||||||
import org.briarproject.bramble.api.Pair;
|
import org.briarproject.bramble.api.Pair;
|
||||||
import org.briarproject.bramble.api.UnsupportedVersionException;
|
import org.briarproject.bramble.api.UnsupportedVersionException;
|
||||||
import org.briarproject.bramble.api.crypto.PublicKey;
|
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
import org.briarproject.bramble.api.db.ContactExistsException;
|
import org.briarproject.bramble.api.db.ContactExistsException;
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
@@ -49,10 +48,6 @@ public interface ContactManager {
|
|||||||
SecretKey rootKey, long timestamp, boolean alice, boolean verified,
|
SecretKey rootKey, long timestamp, boolean alice, boolean verified,
|
||||||
boolean active) throws DbException;
|
boolean active) throws DbException;
|
||||||
|
|
||||||
ContactId addContact(Transaction txn, Author remote, AuthorId local,
|
|
||||||
PublicKey handshake, boolean verified)
|
|
||||||
throws DbException, GeneralSecurityException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores a contact associated with the given local and remote pseudonyms,
|
* Stores a contact associated with the given local and remote pseudonyms,
|
||||||
* replacing the given pending contact, derives and stores handshake mode
|
* replacing the given pending contact, derives and stores handshake mode
|
||||||
@@ -210,18 +205,6 @@ public interface ContactManager {
|
|||||||
void setContactAlias(ContactId c, @Nullable String alias)
|
void setContactAlias(ContactId c, @Nullable String alias)
|
||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the contact's handshake public key
|
|
||||||
*/
|
|
||||||
void setHandshakePublicKey(Transaction txn, ContactId c,
|
|
||||||
PublicKey handshakePublicKey) throws DbException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the contact's handshake public key
|
|
||||||
*/
|
|
||||||
void setHandshakePublicKey(ContactId c, PublicKey handshakePublicKey)
|
|
||||||
throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if a contact with this {@code remoteAuthorId} belongs to
|
* Returns true if a contact with this {@code remoteAuthorId} belongs to
|
||||||
* the local pseudonym with this {@code localAuthorId}.
|
* the local pseudonym with this {@code localAuthorId}.
|
||||||
|
|||||||
@@ -24,19 +24,6 @@ public interface HandshakeManager {
|
|||||||
HandshakeResult handshake(PendingContactId p, InputStream in,
|
HandshakeResult handshake(PendingContactId p, InputStream in,
|
||||||
StreamWriter out) throws DbException, IOException;
|
StreamWriter out) throws DbException, IOException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Handshakes with the given contact. Returns an ephemeral master key
|
|
||||||
* authenticated with both parties' handshake key pairs and a flag
|
|
||||||
* indicating whether the local peer is Alice or Bob.
|
|
||||||
*
|
|
||||||
* @param in An incoming stream for the handshake, which must be secured in
|
|
||||||
* handshake mode
|
|
||||||
* @param out An outgoing stream for the handshake, which must be secured
|
|
||||||
* in handshake mode
|
|
||||||
*/
|
|
||||||
HandshakeResult handshake(ContactId c, InputStream in, StreamWriter out)
|
|
||||||
throws DbException, IOException;
|
|
||||||
|
|
||||||
class HandshakeResult {
|
class HandshakeResult {
|
||||||
|
|
||||||
private final SecretKey masterKey;
|
private final SecretKey masterKey;
|
||||||
|
|||||||
@@ -546,11 +546,6 @@ public interface DatabaseComponent extends TransactionManager {
|
|||||||
void setContactAlias(Transaction txn, ContactId c, @Nullable String alias)
|
void setContactAlias(Transaction txn, ContactId c, @Nullable String alias)
|
||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the remote handshake public key for a given contact
|
|
||||||
*/
|
|
||||||
void setHandshakePublicKey(Transaction txn, ContactId c, PublicKey handshakePublicKey) throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the given group's visibility to the given contact.
|
* Sets the given group's visibility to the given contact.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -35,20 +35,6 @@ public interface KeyManager {
|
|||||||
ContactId c, SecretKey rootKey, long timestamp, boolean alice,
|
ContactId c, SecretKey rootKey, long timestamp, boolean alice,
|
||||||
boolean active) throws DbException;
|
boolean active) throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Derives and stores a set of rotation mode transport keys for
|
|
||||||
* communicating with the given contact over each transport and returns the
|
|
||||||
* key set IDs.
|
|
||||||
* <p/>
|
|
||||||
* {@link StreamContext StreamContexts} for the contact can be created
|
|
||||||
* after this method has returned.
|
|
||||||
*
|
|
||||||
* @param alice True if the local party is Alice
|
|
||||||
* @param active Whether the derived keys can be used for outgoing streams
|
|
||||||
*/
|
|
||||||
Map<TransportId, KeySetId> addRotationKeys(ContactId c, SecretKey rootKey,
|
|
||||||
long timestamp, boolean alice, boolean active) throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Informs the key manager that a new contact has been added. Derives and
|
* Informs the key manager that a new contact has been added. Derives and
|
||||||
* stores a set of handshake mode transport keys for communicating with the
|
* stores a set of handshake mode transport keys for communicating with the
|
||||||
|
|||||||
@@ -176,18 +176,6 @@ class AccountManagerImpl implements AccountManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean restoreAccount(Identity identity, String password) {
|
|
||||||
synchronized (stateChangeLock) {
|
|
||||||
if (hasDatabaseKey())
|
|
||||||
throw new AssertionError("Already have a database key");
|
|
||||||
identityManager.registerIdentity(identity);
|
|
||||||
SecretKey key = crypto.generateSecretKey();
|
|
||||||
if (!encryptAndStoreDatabaseKey(key, password)) return false;
|
|
||||||
databaseKey = key;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@GuardedBy("stateChangeLock")
|
@GuardedBy("stateChangeLock")
|
||||||
private boolean encryptAndStoreDatabaseKey(SecretKey key, String password) {
|
private boolean encryptAndStoreDatabaseKey(SecretKey key, String password) {
|
||||||
byte[] plaintext = key.getBytes();
|
byte[] plaintext = key.getBytes();
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ class ConnectionManagerImpl implements ConnectionManager {
|
|||||||
ioExecutor.execute(new IncomingDuplexSyncConnection(keyManager,
|
ioExecutor.execute(new IncomingDuplexSyncConnection(keyManager,
|
||||||
connectionRegistry, streamReaderFactory, streamWriterFactory,
|
connectionRegistry, streamReaderFactory, streamWriterFactory,
|
||||||
syncSessionFactory, transportPropertyManager, ioExecutor,
|
syncSessionFactory, transportPropertyManager, ioExecutor,
|
||||||
t, d, handshakeManager));
|
t, d));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -101,7 +101,7 @@ class ConnectionManagerImpl implements ConnectionManager {
|
|||||||
ioExecutor.execute(new OutgoingDuplexSyncConnection(keyManager,
|
ioExecutor.execute(new OutgoingDuplexSyncConnection(keyManager,
|
||||||
connectionRegistry, streamReaderFactory, streamWriterFactory,
|
connectionRegistry, streamReaderFactory, streamWriterFactory,
|
||||||
syncSessionFactory, transportPropertyManager, ioExecutor,
|
syncSessionFactory, transportPropertyManager, ioExecutor,
|
||||||
secureRandom, handshakeManager, c, t, d));
|
secureRandom, c, t, d));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package org.briarproject.bramble.connection;
|
|||||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||||
import org.briarproject.bramble.api.connection.InterruptibleConnection;
|
import org.briarproject.bramble.api.connection.InterruptibleConnection;
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
import org.briarproject.bramble.api.contact.HandshakeManager;
|
|
||||||
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;
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package org.briarproject.bramble.connection;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
import org.briarproject.bramble.api.contact.HandshakeManager;
|
|
||||||
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.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
@@ -14,11 +13,9 @@ import org.briarproject.bramble.api.sync.SyncSessionFactory;
|
|||||||
import org.briarproject.bramble.api.transport.KeyManager;
|
import org.briarproject.bramble.api.transport.KeyManager;
|
||||||
import org.briarproject.bramble.api.transport.StreamContext;
|
import org.briarproject.bramble.api.transport.StreamContext;
|
||||||
import org.briarproject.bramble.api.transport.StreamReaderFactory;
|
import org.briarproject.bramble.api.transport.StreamReaderFactory;
|
||||||
import org.briarproject.bramble.api.transport.StreamWriter;
|
|
||||||
import org.briarproject.bramble.api.transport.StreamWriterFactory;
|
import org.briarproject.bramble.api.transport.StreamWriterFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
@@ -27,10 +24,6 @@ import static org.briarproject.bramble.util.LogUtils.logException;
|
|||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class IncomingDuplexSyncConnection extends DuplexSyncConnection
|
class IncomingDuplexSyncConnection extends DuplexSyncConnection
|
||||||
implements Runnable {
|
implements Runnable {
|
||||||
private final HandshakeManager handshakeManager;
|
|
||||||
|
|
||||||
// FIXME: Exchange timestamp as part of handshake protocol?
|
|
||||||
private static final long TIMESTAMP = 1617235200; // 1 April 2021 00:00 UTC
|
|
||||||
|
|
||||||
IncomingDuplexSyncConnection(KeyManager keyManager,
|
IncomingDuplexSyncConnection(KeyManager keyManager,
|
||||||
ConnectionRegistry connectionRegistry,
|
ConnectionRegistry connectionRegistry,
|
||||||
@@ -39,18 +32,14 @@ class IncomingDuplexSyncConnection extends DuplexSyncConnection
|
|||||||
SyncSessionFactory syncSessionFactory,
|
SyncSessionFactory syncSessionFactory,
|
||||||
TransportPropertyManager transportPropertyManager,
|
TransportPropertyManager transportPropertyManager,
|
||||||
Executor ioExecutor, TransportId transportId,
|
Executor ioExecutor, TransportId transportId,
|
||||||
DuplexTransportConnection connection,
|
DuplexTransportConnection connection) {
|
||||||
HandshakeManager handshakeManager) {
|
|
||||||
super(keyManager, connectionRegistry, streamReaderFactory,
|
super(keyManager, connectionRegistry, streamReaderFactory,
|
||||||
streamWriterFactory, syncSessionFactory,
|
streamWriterFactory, syncSessionFactory,
|
||||||
transportPropertyManager, ioExecutor, transportId, connection);
|
transportPropertyManager, ioExecutor, transportId, connection);
|
||||||
this.handshakeManager = handshakeManager;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
LOG.info("Running IncomingDuplexSyncConnection on transport " +
|
|
||||||
transportId.getString());
|
|
||||||
// Read and recognise the tag
|
// Read and recognise the tag
|
||||||
StreamContext ctx = recogniseTag(reader, transportId);
|
StreamContext ctx = recogniseTag(reader, transportId);
|
||||||
if (ctx == null) {
|
if (ctx == null) {
|
||||||
@@ -65,22 +54,10 @@ class IncomingDuplexSyncConnection extends DuplexSyncConnection
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (ctx.isHandshakeMode()) {
|
if (ctx.isHandshakeMode()) {
|
||||||
if (!performHandshake(ctx, contactId)) {
|
// TODO: Support handshake mode for contacts
|
||||||
LOG.warning("Handshake failed");
|
LOG.warning("Received handshake tag, expected rotation mode");
|
||||||
return;
|
onReadError(true);
|
||||||
}
|
return;
|
||||||
// Allocate a rotation mode stream context
|
|
||||||
ctx = allocateStreamContext(contactId, transportId);
|
|
||||||
if (ctx == null) {
|
|
||||||
LOG.warning("Could not allocate stream context");
|
|
||||||
onWriteError();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (ctx.isHandshakeMode()) {
|
|
||||||
LOG.warning("Got handshake mode context after handshaking");
|
|
||||||
onWriteError();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
connectionRegistry.registerIncomingConnection(contactId, transportId,
|
connectionRegistry.registerIncomingConnection(contactId, transportId,
|
||||||
this);
|
this);
|
||||||
@@ -126,35 +103,5 @@ class IncomingDuplexSyncConnection extends DuplexSyncConnection
|
|||||||
onWriteError();
|
onWriteError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean performHandshake(StreamContext ctxIn, ContactId contactId) {
|
|
||||||
LOG.info("Performing handshake (Incoming)");
|
|
||||||
// Allocate the outgoing stream context
|
|
||||||
StreamContext ctxOut =
|
|
||||||
allocateStreamContext(contactId, transportId);
|
|
||||||
if (ctxOut == null) {
|
|
||||||
LOG.warning("Could not allocate stream context");
|
|
||||||
onReadError(true);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
InputStream in = streamReaderFactory.createStreamReader(
|
|
||||||
reader.getInputStream(), ctxIn);
|
|
||||||
// Flush the output stream to send the outgoing stream header
|
|
||||||
StreamWriter out = streamWriterFactory.createStreamWriter(
|
|
||||||
writer.getOutputStream(), ctxOut);
|
|
||||||
out.getOutputStream().flush();
|
|
||||||
HandshakeManager.HandshakeResult result =
|
|
||||||
handshakeManager.handshake(contactId, in, out);
|
|
||||||
keyManager.addRotationKeys(contactId, result.getMasterKey(),
|
|
||||||
TIMESTAMP, result.isAlice(), true);
|
|
||||||
LOG.info("Rotation keys added");
|
|
||||||
return true;
|
|
||||||
} catch (IOException | DbException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
onReadError(true);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package org.briarproject.bramble.connection;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
import org.briarproject.bramble.api.contact.HandshakeManager;
|
|
||||||
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.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
@@ -15,11 +14,9 @@ import org.briarproject.bramble.api.sync.SyncSessionFactory;
|
|||||||
import org.briarproject.bramble.api.transport.KeyManager;
|
import org.briarproject.bramble.api.transport.KeyManager;
|
||||||
import org.briarproject.bramble.api.transport.StreamContext;
|
import org.briarproject.bramble.api.transport.StreamContext;
|
||||||
import org.briarproject.bramble.api.transport.StreamReaderFactory;
|
import org.briarproject.bramble.api.transport.StreamReaderFactory;
|
||||||
import org.briarproject.bramble.api.transport.StreamWriter;
|
|
||||||
import org.briarproject.bramble.api.transport.StreamWriterFactory;
|
import org.briarproject.bramble.api.transport.StreamWriterFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
@@ -31,38 +28,26 @@ import static org.briarproject.bramble.util.LogUtils.logException;
|
|||||||
class OutgoingDuplexSyncConnection extends DuplexSyncConnection
|
class OutgoingDuplexSyncConnection extends DuplexSyncConnection
|
||||||
implements Runnable {
|
implements Runnable {
|
||||||
|
|
||||||
// FIXME: Exchange timestamp as part of handshake protocol?
|
|
||||||
private static final long TIMESTAMP = 1617235200; // 1 April 2021 00:00 UTC
|
|
||||||
|
|
||||||
private final SecureRandom secureRandom;
|
private final SecureRandom secureRandom;
|
||||||
private final HandshakeManager handshakeManager;
|
|
||||||
private final ContactId contactId;
|
private final ContactId contactId;
|
||||||
|
|
||||||
OutgoingDuplexSyncConnection(
|
OutgoingDuplexSyncConnection(KeyManager keyManager,
|
||||||
KeyManager keyManager,
|
|
||||||
ConnectionRegistry connectionRegistry,
|
ConnectionRegistry connectionRegistry,
|
||||||
StreamReaderFactory streamReaderFactory,
|
StreamReaderFactory streamReaderFactory,
|
||||||
StreamWriterFactory streamWriterFactory,
|
StreamWriterFactory streamWriterFactory,
|
||||||
SyncSessionFactory syncSessionFactory,
|
SyncSessionFactory syncSessionFactory,
|
||||||
TransportPropertyManager transportPropertyManager,
|
TransportPropertyManager transportPropertyManager,
|
||||||
Executor ioExecutor,
|
Executor ioExecutor, SecureRandom secureRandom, ContactId contactId,
|
||||||
SecureRandom secureRandom,
|
TransportId transportId, DuplexTransportConnection connection) {
|
||||||
HandshakeManager handshakeManager,
|
|
||||||
ContactId contactId,
|
|
||||||
TransportId transportId,
|
|
||||||
DuplexTransportConnection connection) {
|
|
||||||
super(keyManager, connectionRegistry, streamReaderFactory,
|
super(keyManager, connectionRegistry, streamReaderFactory,
|
||||||
streamWriterFactory, syncSessionFactory,
|
streamWriterFactory, syncSessionFactory,
|
||||||
transportPropertyManager, ioExecutor, transportId, connection);
|
transportPropertyManager, ioExecutor, transportId, connection);
|
||||||
this.secureRandom = secureRandom;
|
this.secureRandom = secureRandom;
|
||||||
this.handshakeManager = handshakeManager;
|
|
||||||
this.contactId = contactId;
|
this.contactId = contactId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
LOG.info("Running OutgoingDuplexSyncConnection on transport " +
|
|
||||||
transportId.getString());
|
|
||||||
// Allocate a stream context
|
// Allocate a stream context
|
||||||
StreamContext ctx = allocateStreamContext(contactId, transportId);
|
StreamContext ctx = allocateStreamContext(contactId, transportId);
|
||||||
if (ctx == null) {
|
if (ctx == null) {
|
||||||
@@ -71,22 +56,10 @@ class OutgoingDuplexSyncConnection extends DuplexSyncConnection
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (ctx.isHandshakeMode()) {
|
if (ctx.isHandshakeMode()) {
|
||||||
if (!performHandshake(ctx)) {
|
// TODO: Support handshake mode for contacts
|
||||||
LOG.warning("Handshake failed");
|
LOG.warning("Cannot use handshake mode stream context");
|
||||||
return;
|
onWriteError();
|
||||||
}
|
return;
|
||||||
// Allocate a rotation mode stream context
|
|
||||||
ctx = allocateStreamContext(contactId, transportId);
|
|
||||||
if (ctx == null) {
|
|
||||||
LOG.warning("Could not allocate stream context");
|
|
||||||
onWriteError();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (ctx.isHandshakeMode()) {
|
|
||||||
LOG.warning("Got handshake mode context after handshaking");
|
|
||||||
onWriteError();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Start the incoming session on another thread
|
// Start the incoming session on another thread
|
||||||
Priority priority = generatePriority();
|
Priority priority = generatePriority();
|
||||||
@@ -154,59 +127,6 @@ class OutgoingDuplexSyncConnection extends DuplexSyncConnection
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean performHandshake(StreamContext ctxOut) {
|
|
||||||
LOG.info("Performing handshake (Outgoing)");
|
|
||||||
// Flush the output stream to send the outgoing stream header
|
|
||||||
StreamWriter out;
|
|
||||||
try {
|
|
||||||
out = streamWriterFactory.createStreamWriter(
|
|
||||||
writer.getOutputStream(), ctxOut);
|
|
||||||
out.getOutputStream().flush();
|
|
||||||
} catch (IOException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
onWriteError();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Read and recognise the tag
|
|
||||||
StreamContext ctxIn = recogniseTag(reader, transportId);
|
|
||||||
// Unrecognised tags are suspicious in this case
|
|
||||||
if (ctxIn == null) {
|
|
||||||
LOG.warning("Unrecognised tag for returning stream");
|
|
||||||
onReadError();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Check that the stream comes from the expected contact
|
|
||||||
ContactId inContactId = ctxIn.getContactId();
|
|
||||||
if (contactId == null) {
|
|
||||||
LOG.warning("Expected contact tag, got rendezvous tag");
|
|
||||||
onReadError();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!inContactId.equals(contactId)) {
|
|
||||||
LOG.warning("Wrong contact ID for returning stream");
|
|
||||||
onReadError();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// TODO: Register the connection, close it if it's redundant
|
|
||||||
// Handshake and exchange contacts
|
|
||||||
try {
|
|
||||||
InputStream in = streamReaderFactory.createStreamReader(
|
|
||||||
reader.getInputStream(), ctxIn);
|
|
||||||
HandshakeManager.HandshakeResult result =
|
|
||||||
handshakeManager.handshake(contactId, in, out);
|
|
||||||
keyManager.addRotationKeys(contactId, result.getMasterKey(),
|
|
||||||
TIMESTAMP, result.isAlice(), true);
|
|
||||||
LOG.info("Rotation keys added");
|
|
||||||
return true;
|
|
||||||
} catch (IOException | DbException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
onWriteError();
|
|
||||||
return false;
|
|
||||||
} finally {
|
|
||||||
// TODO: Unregister the connection
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onReadError() {
|
private void onReadError() {
|
||||||
// 'Recognised' is always true for outgoing connections
|
// 'Recognised' is always true for outgoing connections
|
||||||
onReadError(true);
|
onReadError(true);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package org.briarproject.bramble.api.contact;
|
package org.briarproject.bramble.contact;
|
||||||
|
|
||||||
public interface ContactExchangeConstants {
|
interface ContactExchangeConstants {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The current version of the contact exchange protocol.
|
* The current version of the contact exchange protocol.
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.briarproject.bramble.api.contact;
|
package org.briarproject.bramble.contact;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.crypto.PrivateKey;
|
import org.briarproject.bramble.api.crypto.PrivateKey;
|
||||||
import org.briarproject.bramble.api.crypto.PublicKey;
|
import org.briarproject.bramble.api.crypto.PublicKey;
|
||||||
@@ -6,7 +6,7 @@ import org.briarproject.bramble.api.crypto.SecretKey;
|
|||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public interface ContactExchangeCrypto {
|
interface ContactExchangeCrypto {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Derives the header key for a contact exchange stream from the master key.
|
* Derives the header key for a contact exchange stream from the master key.
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.briarproject.bramble.contact;
|
package org.briarproject.bramble.contact;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.contact.ContactExchangeCrypto;
|
|
||||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||||
import org.briarproject.bramble.api.crypto.PrivateKey;
|
import org.briarproject.bramble.api.crypto.PrivateKey;
|
||||||
import org.briarproject.bramble.api.crypto.PublicKey;
|
import org.briarproject.bramble.api.crypto.PublicKey;
|
||||||
@@ -11,12 +10,12 @@ import java.security.GeneralSecurityException;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static org.briarproject.bramble.api.contact.ContactExchangeConstants.ALICE_KEY_LABEL;
|
import static org.briarproject.bramble.contact.ContactExchangeConstants.ALICE_KEY_LABEL;
|
||||||
import static org.briarproject.bramble.api.contact.ContactExchangeConstants.ALICE_NONCE_LABEL;
|
import static org.briarproject.bramble.contact.ContactExchangeConstants.ALICE_NONCE_LABEL;
|
||||||
import static org.briarproject.bramble.api.contact.ContactExchangeConstants.BOB_KEY_LABEL;
|
import static org.briarproject.bramble.contact.ContactExchangeConstants.BOB_KEY_LABEL;
|
||||||
import static org.briarproject.bramble.api.contact.ContactExchangeConstants.BOB_NONCE_LABEL;
|
import static org.briarproject.bramble.contact.ContactExchangeConstants.BOB_NONCE_LABEL;
|
||||||
import static org.briarproject.bramble.api.contact.ContactExchangeConstants.PROTOCOL_VERSION;
|
import static org.briarproject.bramble.contact.ContactExchangeConstants.PROTOCOL_VERSION;
|
||||||
import static org.briarproject.bramble.api.contact.ContactExchangeConstants.SIGNING_LABEL;
|
import static org.briarproject.bramble.contact.ContactExchangeConstants.SIGNING_LABEL;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class ContactExchangeCryptoImpl implements ContactExchangeCrypto {
|
class ContactExchangeCryptoImpl implements ContactExchangeCrypto {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import org.briarproject.bramble.api.FormatException;
|
|||||||
import org.briarproject.bramble.api.Predicate;
|
import org.briarproject.bramble.api.Predicate;
|
||||||
import org.briarproject.bramble.api.client.ClientHelper;
|
import org.briarproject.bramble.api.client.ClientHelper;
|
||||||
import org.briarproject.bramble.api.contact.Contact;
|
import org.briarproject.bramble.api.contact.Contact;
|
||||||
import org.briarproject.bramble.api.contact.ContactExchangeCrypto;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactExchangeManager;
|
import org.briarproject.bramble.api.contact.ContactExchangeManager;
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
@@ -48,8 +47,8 @@ import javax.inject.Inject;
|
|||||||
|
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH;
|
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH;
|
||||||
import static org.briarproject.bramble.api.contact.ContactExchangeConstants.PROTOCOL_VERSION;
|
import static org.briarproject.bramble.contact.ContactExchangeConstants.PROTOCOL_VERSION;
|
||||||
import static org.briarproject.bramble.api.contact.ContactExchangeRecordTypes.CONTACT_INFO;
|
import static org.briarproject.bramble.contact.ContactExchangeRecordTypes.CONTACT_INFO;
|
||||||
import static org.briarproject.bramble.util.ValidationUtils.checkLength;
|
import static org.briarproject.bramble.util.ValidationUtils.checkLength;
|
||||||
import static org.briarproject.bramble.util.ValidationUtils.checkSize;
|
import static org.briarproject.bramble.util.ValidationUtils.checkSize;
|
||||||
|
|
||||||
@@ -82,8 +81,7 @@ class ContactExchangeManagerImpl implements ContactExchangeManager {
|
|||||||
private final ContactManager contactManager;
|
private final ContactManager contactManager;
|
||||||
private final IdentityManager identityManager;
|
private final IdentityManager identityManager;
|
||||||
private final TransportPropertyManager transportPropertyManager;
|
private final TransportPropertyManager transportPropertyManager;
|
||||||
private final org.briarproject.bramble.api.contact.ContactExchangeCrypto
|
private final ContactExchangeCrypto contactExchangeCrypto;
|
||||||
contactExchangeCrypto;
|
|
||||||
private final StreamReaderFactory streamReaderFactory;
|
private final StreamReaderFactory streamReaderFactory;
|
||||||
private final StreamWriterFactory streamWriterFactory;
|
private final StreamWriterFactory streamWriterFactory;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package org.briarproject.bramble.contact;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Record types for the contact exchange protocol.
|
||||||
|
*/
|
||||||
|
interface ContactExchangeRecordTypes {
|
||||||
|
|
||||||
|
byte CONTACT_INFO = 0;
|
||||||
|
}
|
||||||
@@ -9,7 +9,6 @@ import org.briarproject.bramble.api.contact.PendingContact;
|
|||||||
import org.briarproject.bramble.api.contact.PendingContactId;
|
import org.briarproject.bramble.api.contact.PendingContactId;
|
||||||
import org.briarproject.bramble.api.contact.PendingContactState;
|
import org.briarproject.bramble.api.contact.PendingContactState;
|
||||||
import org.briarproject.bramble.api.contact.event.PendingContactStateChangedEvent;
|
import org.briarproject.bramble.api.contact.event.PendingContactStateChangedEvent;
|
||||||
import org.briarproject.bramble.api.crypto.CryptoConstants;
|
|
||||||
import org.briarproject.bramble.api.crypto.KeyPair;
|
import org.briarproject.bramble.api.crypto.KeyPair;
|
||||||
import org.briarproject.bramble.api.crypto.PublicKey;
|
import org.briarproject.bramble.api.crypto.PublicKey;
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
@@ -120,18 +119,6 @@ class ContactManagerImpl implements ContactManager, EventListener {
|
|||||||
verified, active));
|
verified, active));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public ContactId addContact(Transaction txn, Author remote, AuthorId local,
|
|
||||||
PublicKey handshake, boolean verified)
|
|
||||||
throws DbException, GeneralSecurityException {
|
|
||||||
ContactId c = db.addContact(txn, remote, local, handshake, verified);
|
|
||||||
Contact contact = db.getContact(txn, c);
|
|
||||||
KeyPair ourKeyPair = identityManager.getHandshakeKeys(txn);
|
|
||||||
keyManager.addContact(txn, c, handshake, ourKeyPair);
|
|
||||||
for (ContactHook hook : hooks) hook.addingContact(txn, contact);
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getHandshakeLink() throws DbException {
|
public String getHandshakeLink() throws DbException {
|
||||||
KeyPair keyPair = db.transactionWithResult(true,
|
KeyPair keyPair = db.transactionWithResult(true,
|
||||||
@@ -247,23 +234,6 @@ class ContactManagerImpl implements ContactManager, EventListener {
|
|||||||
db.transaction(false, txn -> setContactAlias(txn, c, alias));
|
db.transaction(false, txn -> setContactAlias(txn, c, alias));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setHandshakePublicKey(Transaction txn, ContactId c,
|
|
||||||
PublicKey handshakePublicKey) throws DbException {
|
|
||||||
if (handshakePublicKey.getKeyType() !=
|
|
||||||
CryptoConstants.KEY_TYPE_AGREEMENT) {
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
}
|
|
||||||
db.setHandshakePublicKey(txn, c, handshakePublicKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setHandshakePublicKey(ContactId c, PublicKey handshakePublicKey)
|
|
||||||
throws DbException {
|
|
||||||
db.transaction(false,
|
|
||||||
txn -> setHandshakePublicKey(txn, c, handshakePublicKey));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contactExists(Transaction txn, AuthorId remoteAuthorId,
|
public boolean contactExists(Transaction txn, AuthorId remoteAuthorId,
|
||||||
AuthorId localAuthorId) throws DbException {
|
AuthorId localAuthorId) throws DbException {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.briarproject.bramble.contact;
|
package org.briarproject.bramble.contact;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.contact.ContactExchangeCrypto;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactExchangeManager;
|
import org.briarproject.bramble.api.contact.ContactExchangeManager;
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
import org.briarproject.bramble.api.contact.HandshakeManager;
|
import org.briarproject.bramble.api.contact.HandshakeManager;
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ package org.briarproject.bramble.contact;
|
|||||||
import org.briarproject.bramble.api.FormatException;
|
import org.briarproject.bramble.api.FormatException;
|
||||||
import org.briarproject.bramble.api.Pair;
|
import org.briarproject.bramble.api.Pair;
|
||||||
import org.briarproject.bramble.api.Predicate;
|
import org.briarproject.bramble.api.Predicate;
|
||||||
import org.briarproject.bramble.api.contact.Contact;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
import org.briarproject.bramble.api.contact.HandshakeManager;
|
import org.briarproject.bramble.api.contact.HandshakeManager;
|
||||||
import org.briarproject.bramble.api.contact.PendingContact;
|
import org.briarproject.bramble.api.contact.PendingContact;
|
||||||
@@ -90,27 +88,6 @@ class HandshakeManagerImpl implements HandshakeManager {
|
|||||||
});
|
});
|
||||||
PublicKey theirStaticPublicKey = keys.getFirst();
|
PublicKey theirStaticPublicKey = keys.getFirst();
|
||||||
KeyPair ourStaticKeyPair = keys.getSecond();
|
KeyPair ourStaticKeyPair = keys.getSecond();
|
||||||
return handshake(theirStaticPublicKey, ourStaticKeyPair, in, out);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public HandshakeResult handshake(ContactId c, InputStream in,
|
|
||||||
StreamWriter out) throws DbException, IOException {
|
|
||||||
Pair<PublicKey, KeyPair> keys = db.transactionWithResult(true, txn -> {
|
|
||||||
Contact contact = contactManager.getContact(txn, c);
|
|
||||||
PublicKey handshakePublicKey = contact.getHandshakePublicKey();
|
|
||||||
if (handshakePublicKey == null) throw new DbException();
|
|
||||||
KeyPair keyPair = identityManager.getHandshakeKeys(txn);
|
|
||||||
return new Pair<>(handshakePublicKey, keyPair);
|
|
||||||
});
|
|
||||||
PublicKey theirStaticPublicKey = keys.getFirst();
|
|
||||||
KeyPair ourStaticKeyPair = keys.getSecond();
|
|
||||||
return handshake(theirStaticPublicKey, ourStaticKeyPair, in, out);
|
|
||||||
}
|
|
||||||
|
|
||||||
private HandshakeResult handshake(PublicKey theirStaticPublicKey,
|
|
||||||
KeyPair ourStaticKeyPair, InputStream in, StreamWriter out)
|
|
||||||
throws IOException {
|
|
||||||
boolean alice = transportCrypto.isAlice(theirStaticPublicKey,
|
boolean alice = transportCrypto.isAlice(theirStaticPublicKey,
|
||||||
ourStaticKeyPair);
|
ourStaticKeyPair);
|
||||||
RecordReader recordReader = recordReaderFactory.createRecordReader(in);
|
RecordReader recordReader = recordReaderFactory.createRecordReader(in);
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ import org.briarproject.bramble.api.transport.KeySetId;
|
|||||||
import org.briarproject.bramble.api.transport.TransportKeySet;
|
import org.briarproject.bramble.api.transport.TransportKeySet;
|
||||||
import org.briarproject.bramble.api.transport.TransportKeys;
|
import org.briarproject.bramble.api.transport.TransportKeys;
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -696,11 +695,6 @@ interface Database<T> {
|
|||||||
void setHandshakeKeyPair(T txn, AuthorId local, PublicKey publicKey,
|
void setHandshakeKeyPair(T txn, AuthorId local, PublicKey publicKey,
|
||||||
PrivateKey privateKey) throws DbException;
|
PrivateKey privateKey) throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the handshake public key for a given contact
|
|
||||||
*/
|
|
||||||
void setHandshakePublicKey(T txn, ContactId c, PublicKey handshakePublicKey) throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Marks the given message as permanent, i.e. not temporary.
|
* Marks the given message as permanent, i.e. not temporary.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1040,15 +1040,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setHandshakePublicKey(Transaction transaction, ContactId c, PublicKey handshakePublicKey) throws DbException {
|
|
||||||
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
|
||||||
T txn = unbox(transaction);
|
|
||||||
if (!db.containsContact(txn, c))
|
|
||||||
throw new NoSuchContactException();
|
|
||||||
db.setHandshakePublicKey(txn, c, handshakePublicKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setHandshakeKeyPair(Transaction transaction, AuthorId local,
|
public void setHandshakeKeyPair(Transaction transaction, AuthorId local,
|
||||||
PublicKey publicKey, PrivateKey privateKey) throws DbException {
|
PublicKey publicKey, PrivateKey privateKey) throws DbException {
|
||||||
|
|||||||
@@ -3058,23 +3058,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setHandshakePublicKey(Connection txn, ContactId c, PublicKey handshakePublicKey) throws DbException {
|
|
||||||
PreparedStatement ps = null;
|
|
||||||
try {
|
|
||||||
String sql = "UPDATE contacts SET handshakePublicKey = ? WHERE contactId = ?";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
ps.setBytes(1, handshakePublicKey.getEncoded());
|
|
||||||
ps.setInt(2, c.getInt());
|
|
||||||
int affected = ps.executeUpdate();
|
|
||||||
if (affected < 0 || affected > 1) throw new DbStateException();
|
|
||||||
ps.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
tryToClose(ps, LOG, WARNING);
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setGroupVisibility(Connection txn, ContactId c, GroupId g,
|
public void setGroupVisibility(Connection txn, ContactId c, GroupId g,
|
||||||
boolean shared) throws DbException {
|
boolean shared) throws DbException {
|
||||||
|
|||||||
@@ -127,7 +127,6 @@ class KeyAgreementConnector {
|
|||||||
List<Pair<DuplexPlugin, BdfList>> transports = new ArrayList<>();
|
List<Pair<DuplexPlugin, BdfList>> transports = new ArrayList<>();
|
||||||
for (TransportId id : PREFERRED_TRANSPORTS) {
|
for (TransportId id : PREFERRED_TRANSPORTS) {
|
||||||
TransportDescriptor d = descriptors.get(id);
|
TransportDescriptor d = descriptors.get(id);
|
||||||
LOG.info("id: " + id + " d: " + d);
|
|
||||||
Plugin p = pluginManager.getPlugin(id);
|
Plugin p = pluginManager.getPlugin(id);
|
||||||
if (d != null && p instanceof DuplexPlugin) {
|
if (d != null && p instanceof DuplexPlugin) {
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
@@ -138,9 +137,6 @@ class KeyAgreementConnector {
|
|||||||
|
|
||||||
// TODO: If we don't have any transports in common with the peer,
|
// TODO: If we don't have any transports in common with the peer,
|
||||||
// warn the user and give up (#1224)
|
// warn the user and give up (#1224)
|
||||||
if (transports.isEmpty()) {
|
|
||||||
LOG.info("No transports found");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!transports.isEmpty()) {
|
if (!transports.isEmpty()) {
|
||||||
byte[] commitment = remotePayload.getCommitment();
|
byte[] commitment = remotePayload.getCommitment();
|
||||||
|
|||||||
@@ -34,12 +34,12 @@ class KeyAgreementTransport {
|
|||||||
Logger.getLogger(KeyAgreementTransport.class.getName());
|
Logger.getLogger(KeyAgreementTransport.class.getName());
|
||||||
|
|
||||||
// Accept records with current protocol version, known record type
|
// Accept records with current protocol version, known record type
|
||||||
private static final Predicate<Record> ACCEPT = r ->
|
private static Predicate<Record> ACCEPT = r ->
|
||||||
r.getProtocolVersion() == PROTOCOL_VERSION &&
|
r.getProtocolVersion() == PROTOCOL_VERSION &&
|
||||||
isKnownRecordType(r.getRecordType());
|
isKnownRecordType(r.getRecordType());
|
||||||
|
|
||||||
// Ignore records with current protocol version, unknown record type
|
// Ignore records with current protocol version, unknown record type
|
||||||
private static final Predicate<Record> IGNORE = r ->
|
private static Predicate<Record> IGNORE = r ->
|
||||||
r.getProtocolVersion() == PROTOCOL_VERSION &&
|
r.getProtocolVersion() == PROTOCOL_VERSION &&
|
||||||
!isKnownRecordType(r.getRecordType());
|
!isKnownRecordType(r.getRecordType());
|
||||||
|
|
||||||
|
|||||||
@@ -101,9 +101,9 @@ class KeyManagerImpl implements KeyManager, Service, EventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<TransportId, KeySetId> addRotationKeys(Transaction txn,
|
public Map<TransportId, KeySetId> addRotationKeys(
|
||||||
ContactId c, SecretKey rootKey, long timestamp, boolean alice,
|
Transaction txn, ContactId c, SecretKey rootKey, long timestamp,
|
||||||
boolean active) throws DbException {
|
boolean alice, boolean active) throws DbException {
|
||||||
Map<TransportId, KeySetId> ids = new HashMap<>();
|
Map<TransportId, KeySetId> ids = new HashMap<>();
|
||||||
for (Entry<TransportId, TransportKeyManager> e : managers.entrySet()) {
|
for (Entry<TransportId, TransportKeyManager> e : managers.entrySet()) {
|
||||||
TransportId t = e.getKey();
|
TransportId t = e.getKey();
|
||||||
@@ -114,14 +114,6 @@ class KeyManagerImpl implements KeyManager, Service, EventListener {
|
|||||||
return ids;
|
return ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<TransportId, KeySetId> addRotationKeys(ContactId c,
|
|
||||||
SecretKey rootKey, long timestamp, boolean alice, boolean active)
|
|
||||||
throws DbException {
|
|
||||||
return db.transactionWithResult(false, txn ->
|
|
||||||
addRotationKeys(txn, c, rootKey, timestamp, alice, active));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<TransportId, KeySetId> addContact(Transaction txn, ContactId c,
|
public Map<TransportId, KeySetId> addContact(Transaction txn, ContactId c,
|
||||||
PublicKey theirPublicKey, KeyPair ourKeyPair)
|
PublicKey theirPublicKey, KeyPair ourKeyPair)
|
||||||
@@ -145,7 +137,7 @@ class KeyManagerImpl implements KeyManager, Service, EventListener {
|
|||||||
PendingContactId p, PublicKey theirPublicKey, KeyPair ourKeyPair)
|
PendingContactId p, PublicKey theirPublicKey, KeyPair ourKeyPair)
|
||||||
throws DbException, GeneralSecurityException {
|
throws DbException, GeneralSecurityException {
|
||||||
SecretKey staticMasterKey = transportCrypto
|
SecretKey staticMasterKey = transportCrypto
|
||||||
.deriveStaticMasterKey(theirPublicKey, ourKeyPair);
|
.deriveStaticMasterKey(theirPublicKey, ourKeyPair);
|
||||||
SecretKey rootKey =
|
SecretKey rootKey =
|
||||||
transportCrypto.deriveHandshakeRootKey(staticMasterKey, true);
|
transportCrypto.deriveHandshakeRootKey(staticMasterKey, true);
|
||||||
boolean alice = transportCrypto.isAlice(theirPublicKey, ourKeyPair);
|
boolean alice = transportCrypto.isAlice(theirPublicKey, ourKeyPair);
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ Bridge obfs4 192.95.36.142:443 CDF2E852BF539B82BD10E27E9115A31734E378C2 cert=qUV
|
|||||||
Bridge obfs4 38.229.1.78:80 C8CBDB2464FC9804A69531437BCF2BE31FDD2EE4 cert=Hmyfd2ev46gGY7NoVxA9ngrPF2zCZtzskRTzoWXbxNkzeVnGFPWmrTtILRyqCTjHR+s9dg iat-mode=1
|
Bridge obfs4 38.229.1.78:80 C8CBDB2464FC9804A69531437BCF2BE31FDD2EE4 cert=Hmyfd2ev46gGY7NoVxA9ngrPF2zCZtzskRTzoWXbxNkzeVnGFPWmrTtILRyqCTjHR+s9dg iat-mode=1
|
||||||
Bridge obfs4 37.218.245.14:38224 D9A82D2F9C2F65A18407B1D2B764F130847F8B5D cert=bjRaMrr1BRiAW8IE9U5z27fQaYgOhX1UCmOpg2pFpoMvo6ZgQMzLsaTzzQNTlm7hNcb+Sg iat-mode=0
|
Bridge obfs4 37.218.245.14:38224 D9A82D2F9C2F65A18407B1D2B764F130847F8B5D cert=bjRaMrr1BRiAW8IE9U5z27fQaYgOhX1UCmOpg2pFpoMvo6ZgQMzLsaTzzQNTlm7hNcb+Sg iat-mode=0
|
||||||
Bridge obfs4 85.31.186.98:443 011F2599C0E9B27EE74B353155E244813763C3E5 cert=ayq0XzCwhpdysn5o0EyDUbmSOx3X/oTEbzDMvczHOdBJKlvIdHHLJGkZARtT4dcBFArPPg iat-mode=0
|
Bridge obfs4 85.31.186.98:443 011F2599C0E9B27EE74B353155E244813763C3E5 cert=ayq0XzCwhpdysn5o0EyDUbmSOx3X/oTEbzDMvczHOdBJKlvIdHHLJGkZARtT4dcBFArPPg iat-mode=0
|
||||||
Bridge obfs4 144.217.20.138:80 FB70B257C162BF1038CA669D568D76F5B7F0BABB cert=vYIV5MgrghGQvZPIi1tJwnzorMgqgmlKaB77Y3Z9Q/v94wZBOAXkW+fdx4aSxLVnKO+xNw iat-mode=0
|
Bridge obfs4 85.31.186.26:443 91A6354697E6B02A386312F68D82CF86824D3606 cert=PBwr+S8JTVZo6MPdHnkTwXJPILWADLqfMGoVvhZClMq/Urndyd42BwX9YFJHZnBB3H0XCw iat-mode=0
|
||||||
Bridge obfs4 193.11.166.194:27015 2D82C2E354D531A68469ADF7F878FA6060C6BACA cert=4TLQPJrTSaDffMK7Nbao6LC7G9OW/NHkUwIdjLSS3KYf0Nv4/nQiiI8dY2TcsQx01NniOg iat-mode=0
|
Bridge obfs4 193.11.166.194:27015 2D82C2E354D531A68469ADF7F878FA6060C6BACA cert=4TLQPJrTSaDffMK7Nbao6LC7G9OW/NHkUwIdjLSS3KYf0Nv4/nQiiI8dY2TcsQx01NniOg iat-mode=0
|
||||||
Bridge obfs4 193.11.166.194:27020 86AC7B8D430DAC4117E9F42C9EAED18133863AAF cert=0LDeJH4JzMDtkJJrFphJCiPqKx7loozKN7VNfuukMGfHO0Z8OGdzHVkhVAOfo1mUdv9cMg iat-mode=0
|
Bridge obfs4 193.11.166.194:27020 86AC7B8D430DAC4117E9F42C9EAED18133863AAF cert=0LDeJH4JzMDtkJJrFphJCiPqKx7loozKN7VNfuukMGfHO0Z8OGdzHVkhVAOfo1mUdv9cMg iat-mode=0
|
||||||
Bridge obfs4 193.11.166.194:27025 1AE2C08904527FEA90C4C4F8C1083EA59FBC6FAF cert=ItvYZzW5tn6v3G4UnQa6Qz04Npro6e81AP70YujmK/KXwDFPTs3aHXcHp4n8Vt6w/bv8cA iat-mode=0
|
Bridge obfs4 193.11.166.194:27025 1AE2C08904527FEA90C4C4F8C1083EA59FBC6FAF cert=ItvYZzW5tn6v3G4UnQa6Qz04Npro6e81AP70YujmK/KXwDFPTs3aHXcHp4n8Vt6w/bv8cA iat-mode=0
|
||||||
Bridge obfs4 209.148.46.65:443 74FAD13168806246602538555B5521A0383A1875 cert=ssH+9rP8dG2NLDN2XuFw63hIO/9MNNinLmxQDpVa+7kTOa9/m+tGWT1SmSYpQ9uTBGa6Hw iat-mode=0
|
Bridge obfs4 209.148.46.65:443 74FAD13168806246602538555B5521A0383A1875 cert=ssH+9rP8dG2NLDN2XuFw63hIO/9MNNinLmxQDpVa+7kTOa9/m+tGWT1SmSYpQ9uTBGa6Hw iat-mode=0
|
||||||
|
Bridge obfs4 146.57.248.225:22 10A6CD36A537FCE513A322361547444B393989F0 cert=K1gDtDAIcUfeLqbstggjIw2rtgIKqdIhUlHp82XRqNSq/mtAjp1BIC9vHKJ2FAEpGssTPw iat-mode=0
|
||||||
Bridge obfs4 45.145.95.6:27015 C5B7CD6946FF10C5B3E89691A7D3F2C122D2117C cert=TD7PbUO0/0k6xYHMPW3vJxICfkMZNdkRrb63Zhl5j9dW3iRGiCx0A7mPhe5T2EDzQ35+Zw iat-mode=0
|
Bridge obfs4 45.145.95.6:27015 C5B7CD6946FF10C5B3E89691A7D3F2C122D2117C cert=TD7PbUO0/0k6xYHMPW3vJxICfkMZNdkRrb63Zhl5j9dW3iRGiCx0A7mPhe5T2EDzQ35+Zw iat-mode=0
|
||||||
Bridge obfs4 51.222.13.177:80 5EDAC3B810E12B01F6FD8050D2FD3E277B289A08 cert=2uplIpLQ0q9+0qMFrK5pkaYRDOe460LL9WHBvatgkuRr/SL31wBOEupaMMJ6koRE6Ld0ew iat-mode=0
|
Bridge obfs4 51.222.13.177:80 5EDAC3B810E12B01F6FD8050D2FD3E277B289A08 cert=2uplIpLQ0q9+0qMFrK5pkaYRDOe460LL9WHBvatgkuRr/SL31wBOEupaMMJ6koRE6Ld0ew iat-mode=0
|
||||||
Bridge obfs4 78.46.188.239:37356 5A2D2F4158D0453E00C7C176978D3F41D69C45DB cert=3c0SwxpOisbohNxEc4tb875RVW8eOu1opRTVXJhafaKA/PNNtI7ElQIVOVZg1AdL5bxGCw iat-mode=0
|
Bridge obfs4 78.46.188.239:37356 5A2D2F4158D0453E00C7C176978D3F41D69C45DB cert=3c0SwxpOisbohNxEc4tb875RVW8eOu1opRTVXJhafaKA/PNNtI7ElQIVOVZg1AdL5bxGCw iat-mode=0
|
||||||
|
|||||||
@@ -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.13-1@zip'
|
tor 'org.briarproject:tor:0.3.5.13@zip'
|
||||||
tor 'org.briarproject:obfs4proxy:0.0.12-dev-40245c4a@zip'
|
tor 'org.briarproject:obfs4proxy:0.0.12-dev-40245c4a@zip'
|
||||||
|
|
||||||
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
|
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import javax.annotation.concurrent.Immutable;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.net.SocketFactory;
|
import javax.net.SocketFactory;
|
||||||
|
|
||||||
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.util.OsUtils.isLinux;
|
import static org.briarproject.bramble.util.OsUtils.isLinux;
|
||||||
|
|
||||||
@@ -97,15 +96,8 @@ public class UnixTorPluginFactory implements DuplexPluginFactory {
|
|||||||
String architecture = null;
|
String architecture = null;
|
||||||
if (isLinux()) {
|
if (isLinux()) {
|
||||||
String arch = System.getProperty("os.arch");
|
String arch = System.getProperty("os.arch");
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("System's os.arch is " + arch);
|
|
||||||
}
|
|
||||||
if (arch.equals("amd64")) {
|
if (arch.equals("amd64")) {
|
||||||
architecture = "linux-x86_64";
|
architecture = "linux-x86_64";
|
||||||
} else if (arch.equals("aarch64")) {
|
|
||||||
architecture = "linux-aarch64";
|
|
||||||
} else if (arch.equals("arm")) {
|
|
||||||
architecture = "linux-armhf";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (architecture == null) {
|
if (architecture == null) {
|
||||||
@@ -113,10 +105,6 @@ public class UnixTorPluginFactory implements DuplexPluginFactory {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("The selected architecture for Tor is " + architecture);
|
|
||||||
}
|
|
||||||
|
|
||||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||||
TorRendezvousCrypto torRendezvousCrypto = new TorRendezvousCryptoImpl();
|
TorRendezvousCrypto torRendezvousCrypto = new TorRendezvousCryptoImpl();
|
||||||
|
|||||||
@@ -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.12-dev-40245c4a:obfs4proxy-0.0.12-dev-40245c4a.zip:172029e7058b3a83ac93ac4991a44bf76e16ce8d46f558f5836d57da3cb3a766',
|
'org.briarproject:obfs4proxy:0.0.12-dev-40245c4a:obfs4proxy-0.0.12-dev-40245c4a.zip:172029e7058b3a83ac93ac4991a44bf76e16ce8d46f558f5836d57da3cb3a766',
|
||||||
'org.briarproject:tor:0.3.5.13-1:tor-0.3.5.13-1.zip:ef35c16bf8dc1f4c75ed71d9f55e4514f383d124ec96b859aca647c990927c99',
|
'org.briarproject:tor:0.3.5.13:tor-0.3.5.13.zip:1c5f0b821ee2aadb0ea04aa96caab3ca0a08370cce8de81c2dfe04d172f8a2a0',
|
||||||
'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',
|
||||||
|
|||||||
@@ -19,15 +19,11 @@ android {
|
|||||||
compileSdkVersion 30
|
compileSdkVersion 30
|
||||||
buildToolsVersion '30.0.2'
|
buildToolsVersion '30.0.2'
|
||||||
|
|
||||||
packagingOptions {
|
|
||||||
doNotStrip '**/*.so'
|
|
||||||
}
|
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
minSdkVersion 16
|
minSdkVersion 16
|
||||||
targetSdkVersion 29
|
targetSdkVersion 29
|
||||||
versionCode 10218
|
versionCode 10216
|
||||||
versionName "1.2.18"
|
versionName "1.2.16"
|
||||||
applicationId "org.briarproject.briar.socialbackup"
|
applicationId "org.briarproject.briar.socialbackup"
|
||||||
|
|
||||||
vectorDrawables.useSupportLibrary = true
|
vectorDrawables.useSupportLibrary = true
|
||||||
@@ -99,7 +95,6 @@ dependencies {
|
|||||||
implementation project(path: ':bramble-core', configuration: 'default')
|
implementation project(path: ':bramble-core', configuration: 'default')
|
||||||
implementation project(':bramble-android')
|
implementation project(':bramble-android')
|
||||||
|
|
||||||
implementation 'androidx.fragment:fragment:1.3.0'
|
|
||||||
implementation 'androidx.preference:preference:1.1.1'
|
implementation 'androidx.preference:preference:1.1.1'
|
||||||
implementation 'androidx.exifinterface:exifinterface:1.3.1'
|
implementation 'androidx.exifinterface:exifinterface:1.3.1'
|
||||||
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
|
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
|
||||||
|
|||||||
@@ -27,10 +27,7 @@
|
|||||||
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
|
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
|
||||||
<uses-permission android:name="android.permission.VIBRATE" />
|
<uses-permission android:name="android.permission.VIBRATE" />
|
||||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||||
<uses-permission
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
|
||||||
android:maxSdkVersion="18"
|
|
||||||
tools:ignore="ScopedStorage" />
|
|
||||||
|
|
||||||
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION" />
|
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||||
<uses-permission-sdk-23 android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
<uses-permission-sdk-23 android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||||
@@ -41,7 +38,6 @@
|
|||||||
android:name="org.briarproject.briar.android.BriarApplicationImpl"
|
android:name="org.briarproject.briar.android.BriarApplicationImpl"
|
||||||
android:allowBackup="false"
|
android:allowBackup="false"
|
||||||
android:banner="@mipmap/tv_banner"
|
android:banner="@mipmap/tv_banner"
|
||||||
android:fullBackupContent="@xml/backup_rules"
|
|
||||||
android:icon="@mipmap/ic_launcher_round"
|
android:icon="@mipmap/ic_launcher_round"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:logo="@mipmap/ic_launcher_round"
|
android:logo="@mipmap/ic_launcher_round"
|
||||||
@@ -157,16 +153,7 @@
|
|||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name="org.briarproject.briar.android.socialbackup.recover.CustodianReturnShardActivity"
|
android:name="org.briarproject.briar.android.socialbackup.RecoverActivity"
|
||||||
android:label="@string/activity_name_custodian_help_recovery"
|
|
||||||
android:parentActivityName="org.briarproject.briar.android.conversation.ConversationActivity">
|
|
||||||
<meta-data
|
|
||||||
android:name="android.support.PARENT_ACTIVITY"
|
|
||||||
android:value="org.briarproject.briar.android.conversation.ConversationActivity" />
|
|
||||||
</activity>
|
|
||||||
|
|
||||||
<activity
|
|
||||||
android:name="org.briarproject.briar.android.socialbackup.recover.OwnerReturnShardActivity"
|
|
||||||
android:label="@string/activity_name_recovery"
|
android:label="@string/activity_name_recovery"
|
||||||
android:parentActivityName="org.briarproject.briar.android.account.NewOrRecoverActivity">
|
android:parentActivityName="org.briarproject.briar.android.account.NewOrRecoverActivity">
|
||||||
<meta-data
|
<meta-data
|
||||||
@@ -175,12 +162,12 @@
|
|||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name="org.briarproject.briar.android.socialbackup.recover.RestoreAccountActivity"
|
android:name="org.briarproject.briar.android.socialbackup.CustodianHelpRecoverActivity"
|
||||||
android:label="@string/activity_name_restore_account"
|
android:label="@string/activity_name_custodian_help_recovery"
|
||||||
android:parentActivityName="org.briarproject.briar.android.socialbackup.recover.OwnerReturnShardActivity">
|
android:parentActivityName="org.briarproject.briar.android.conversation.ConversationActivity">
|
||||||
<meta-data
|
<meta-data
|
||||||
android:name="android.support.PARENT_ACTIVITY"
|
android:name="android.support.PARENT_ACTIVITY"
|
||||||
android:value="org.briarproject.briar.android.socialbackup.recover.OwnerReturnShardActivity" />
|
android:value="org.briarproject.briar.android.conversation.ConversationActivity" />
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
@@ -387,7 +374,7 @@
|
|||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name="org.briarproject.briar.android.contact.add.nearby.AddNearbyContactActivity"
|
android:name="org.briarproject.briar.android.keyagreement.ContactExchangeActivity"
|
||||||
android:label="@string/add_contact_title"
|
android:label="@string/add_contact_title"
|
||||||
android:parentActivityName="org.briarproject.briar.android.navdrawer.NavDrawerActivity"
|
android:parentActivityName="org.briarproject.briar.android.navdrawer.NavDrawerActivity"
|
||||||
android:theme="@style/BriarTheme.NoActionBar">
|
android:theme="@style/BriarTheme.NoActionBar">
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import org.briarproject.bramble.BrambleCoreModule;
|
|||||||
import org.briarproject.bramble.account.BriarAccountModule;
|
import org.briarproject.bramble.account.BriarAccountModule;
|
||||||
import org.briarproject.bramble.api.FeatureFlags;
|
import org.briarproject.bramble.api.FeatureFlags;
|
||||||
import org.briarproject.bramble.api.account.AccountManager;
|
import org.briarproject.bramble.api.account.AccountManager;
|
||||||
import org.briarproject.bramble.api.client.ClientHelper;
|
|
||||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||||
import org.briarproject.bramble.api.contact.ContactExchangeManager;
|
import org.briarproject.bramble.api.contact.ContactExchangeManager;
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
@@ -63,6 +62,7 @@ import org.briarproject.briar.api.privategroup.invitation.GroupInvitationFactory
|
|||||||
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager;
|
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager;
|
||||||
import org.briarproject.briar.api.socialbackup.SocialBackupManager;
|
import org.briarproject.briar.api.socialbackup.SocialBackupManager;
|
||||||
import org.briarproject.briar.api.test.TestDataCreator;
|
import org.briarproject.briar.api.test.TestDataCreator;
|
||||||
|
import org.briarproject.briar.socialbackup.SocialBackupManagerImpl_Factory;
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
@@ -191,8 +191,6 @@ public interface AndroidComponent
|
|||||||
|
|
||||||
DatabaseComponent databaseComponent();
|
DatabaseComponent databaseComponent();
|
||||||
|
|
||||||
ClientHelper clientHelper();
|
|
||||||
|
|
||||||
void inject(SignInReminderReceiver briarService);
|
void inject(SignInReminderReceiver briarService);
|
||||||
|
|
||||||
void inject(BriarService briarService);
|
void inject(BriarService briarService);
|
||||||
|
|||||||
@@ -31,9 +31,8 @@ import org.briarproject.briar.android.account.DozeHelperModule;
|
|||||||
import org.briarproject.briar.android.account.LockManagerImpl;
|
import org.briarproject.briar.android.account.LockManagerImpl;
|
||||||
import org.briarproject.briar.android.account.SetupModule;
|
import org.briarproject.briar.android.account.SetupModule;
|
||||||
import org.briarproject.briar.android.contact.ContactListModule;
|
import org.briarproject.briar.android.contact.ContactListModule;
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddNearbyContactModule;
|
|
||||||
import org.briarproject.briar.android.forum.ForumModule;
|
import org.briarproject.briar.android.forum.ForumModule;
|
||||||
import org.briarproject.briar.android.introduction.IntroductionModule;
|
import org.briarproject.briar.android.keyagreement.ContactExchangeModule;
|
||||||
import org.briarproject.briar.android.logging.LoggingModule;
|
import org.briarproject.briar.android.logging.LoggingModule;
|
||||||
import org.briarproject.briar.android.login.LoginModule;
|
import org.briarproject.briar.android.login.LoginModule;
|
||||||
import org.briarproject.briar.android.navdrawer.NavDrawerModule;
|
import org.briarproject.briar.android.navdrawer.NavDrawerModule;
|
||||||
@@ -42,8 +41,6 @@ import org.briarproject.briar.android.privategroup.list.GroupListModule;
|
|||||||
import org.briarproject.briar.android.reporting.DevReportModule;
|
import org.briarproject.briar.android.reporting.DevReportModule;
|
||||||
import org.briarproject.briar.android.settings.SettingsModule;
|
import org.briarproject.briar.android.settings.SettingsModule;
|
||||||
import org.briarproject.briar.android.sharing.SharingModule;
|
import org.briarproject.briar.android.sharing.SharingModule;
|
||||||
import org.briarproject.briar.android.socialbackup.recover.CustodianReturnShardModule;
|
|
||||||
import org.briarproject.briar.android.socialbackup.recover.OwnerReturnShardModule;
|
|
||||||
import org.briarproject.briar.android.test.TestAvatarCreatorImpl;
|
import org.briarproject.briar.android.test.TestAvatarCreatorImpl;
|
||||||
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;
|
||||||
@@ -78,7 +75,7 @@ import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
|||||||
@Module(includes = {
|
@Module(includes = {
|
||||||
SetupModule.class,
|
SetupModule.class,
|
||||||
DozeHelperModule.class,
|
DozeHelperModule.class,
|
||||||
AddNearbyContactModule.class,
|
ContactExchangeModule.class,
|
||||||
LoggingModule.class,
|
LoggingModule.class,
|
||||||
LoginModule.class,
|
LoginModule.class,
|
||||||
NavDrawerModule.class,
|
NavDrawerModule.class,
|
||||||
@@ -87,14 +84,11 @@ import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
|||||||
DevReportModule.class,
|
DevReportModule.class,
|
||||||
ContactListModule.class,
|
ContactListModule.class,
|
||||||
AndroidDarkCrystalModule.class,
|
AndroidDarkCrystalModule.class,
|
||||||
IntroductionModule.class,
|
|
||||||
// below need to be within same scope as ViewModelProvider.Factory
|
// below need to be within same scope as ViewModelProvider.Factory
|
||||||
ForumModule.class,
|
ForumModule.class,
|
||||||
GroupListModule.class,
|
GroupListModule.class,
|
||||||
GroupConversationModule.class,
|
GroupConversationModule.class,
|
||||||
SharingModule.class,
|
SharingModule.class,
|
||||||
OwnerReturnShardModule.class,
|
|
||||||
CustodianReturnShardModule.class
|
|
||||||
})
|
})
|
||||||
public class AppModule {
|
public class AppModule {
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ public class DozeFragment extends SetupFragment
|
|||||||
private DozeView dozeView;
|
private DozeView dozeView;
|
||||||
private HuaweiView huaweiView;
|
private HuaweiView huaweiView;
|
||||||
private Button next;
|
private Button next;
|
||||||
|
private ProgressBar progressBar;
|
||||||
private boolean secondAttempt = false;
|
private boolean secondAttempt = false;
|
||||||
|
|
||||||
public static DozeFragment newInstance() {
|
public static DozeFragment newInstance() {
|
||||||
@@ -57,19 +58,11 @@ public class DozeFragment extends SetupFragment
|
|||||||
huaweiView = v.findViewById(R.id.huaweiView);
|
huaweiView = v.findViewById(R.id.huaweiView);
|
||||||
huaweiView.setOnCheckedChangedListener(this);
|
huaweiView.setOnCheckedChangedListener(this);
|
||||||
next = v.findViewById(R.id.next);
|
next = v.findViewById(R.id.next);
|
||||||
ProgressBar progressBar = v.findViewById(R.id.progress);
|
progressBar = v.findViewById(R.id.progress);
|
||||||
|
|
||||||
dozeView.setOnButtonClickListener(this::askForDozeWhitelisting);
|
dozeView.setOnButtonClickListener(this::askForDozeWhitelisting);
|
||||||
next.setOnClickListener(this);
|
next.setOnClickListener(this);
|
||||||
|
|
||||||
viewModel.getIsCreatingAccount()
|
|
||||||
.observe(getViewLifecycleOwner(), isCreatingAccount -> {
|
|
||||||
if (isCreatingAccount) {
|
|
||||||
next.setVisibility(INVISIBLE);
|
|
||||||
progressBar.setVisibility(VISIBLE);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,6 +104,15 @@ public class DozeFragment extends SetupFragment
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
|
setNextClicked();
|
||||||
viewModel.dozeExceptionConfirmed();
|
viewModel.dozeExceptionConfirmed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void setNextClicked() {
|
||||||
|
super.setNextClicked();
|
||||||
|
|
||||||
|
next.setVisibility(INVISIBLE);
|
||||||
|
progressBar.setVisibility(VISIBLE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package org.briarproject.briar.android.account;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
public interface DozeHelper {
|
interface DozeHelper {
|
||||||
|
|
||||||
boolean needToShowDozeFragment(Context context);
|
boolean needToShowDozeFragment(Context context);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import static org.briarproject.briar.android.util.UiUtils.needsDozeWhitelisting;
|
|||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class DozeView extends PowerView {
|
class DozeView extends PowerView {
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private Runnable onButtonClickListener;
|
private Runnable onButtonClickListener;
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import static android.os.Build.VERSION.SDK_INT;
|
|||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class HuaweiView extends PowerView {
|
class HuaweiView extends PowerView {
|
||||||
|
|
||||||
private final static String PACKAGE_NAME = "com.huawei.systemmanager";
|
private final static String PACKAGE_NAME = "com.huawei.systemmanager";
|
||||||
private final static String CLASS_NAME =
|
private final static String CLASS_NAME =
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ 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.BaseActivity;
|
import org.briarproject.briar.android.activity.BaseActivity;
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||||
import org.briarproject.briar.android.socialbackup.recover.OwnerReturnShardActivity;
|
import org.briarproject.briar.android.socialbackup.RecoverActivity;
|
||||||
|
|
||||||
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
|
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
|
||||||
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
|
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
|
||||||
@@ -27,7 +27,6 @@ public class NewOrRecoverActivity extends BaseActivity implements
|
|||||||
public void onCreate(Bundle state) {
|
public void onCreate(Bundle state) {
|
||||||
super.onCreate(state);
|
super.onCreate(state);
|
||||||
// fade-in after splash screen instead of default animation
|
// fade-in after splash screen instead of default animation
|
||||||
// TODO the fade in is not working
|
|
||||||
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
|
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
|
||||||
setContentView(R.layout.activity_fragment_container);
|
setContentView(R.layout.activity_fragment_container);
|
||||||
NewOrRecoverFragment fragment = NewOrRecoverFragment.newInstance();
|
NewOrRecoverFragment fragment = NewOrRecoverFragment.newInstance();
|
||||||
@@ -46,7 +45,7 @@ public class NewOrRecoverActivity extends BaseActivity implements
|
|||||||
@Override
|
@Override
|
||||||
public void recoverAccountChosen() {
|
public void recoverAccountChosen() {
|
||||||
finish();
|
finish();
|
||||||
Intent i = new Intent(this, OwnerReturnShardActivity.class);
|
Intent i = new Intent(this, RecoverActivity.class);
|
||||||
i.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP |
|
i.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP |
|
||||||
FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_TASK_ON_HOME);
|
FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_TASK_ON_HOME);
|
||||||
startActivity(i);
|
startActivity(i);
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import static org.briarproject.briar.android.util.UiUtils.showOnboardingDialog;
|
|||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public abstract class PowerView extends ConstraintLayout {
|
abstract class PowerView extends ConstraintLayout {
|
||||||
|
|
||||||
private final TextView textView;
|
private final TextView textView;
|
||||||
private final ImageView checkImage;
|
private final ImageView checkImage;
|
||||||
@@ -156,7 +156,7 @@ public abstract class PowerView extends ConstraintLayout {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface OnCheckedChangedListener {
|
interface OnCheckedChangedListener {
|
||||||
void onCheckedChanged();
|
void onCheckedChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ public class SetPasswordFragment extends SetupFragment {
|
|||||||
private TextInputEditText passwordConfirmation;
|
private TextInputEditText passwordConfirmation;
|
||||||
private StrengthMeter strengthMeter;
|
private StrengthMeter strengthMeter;
|
||||||
private Button nextButton;
|
private Button nextButton;
|
||||||
|
private ProgressBar progressBar;
|
||||||
|
|
||||||
public static SetPasswordFragment newInstance() {
|
public static SetPasswordFragment newInstance() {
|
||||||
return new SetPasswordFragment();
|
return new SetPasswordFragment();
|
||||||
@@ -63,7 +64,7 @@ public class SetPasswordFragment extends SetupFragment {
|
|||||||
v.findViewById(R.id.password_confirm_wrapper);
|
v.findViewById(R.id.password_confirm_wrapper);
|
||||||
passwordConfirmation = v.findViewById(R.id.password_confirm);
|
passwordConfirmation = v.findViewById(R.id.password_confirm);
|
||||||
nextButton = v.findViewById(R.id.next);
|
nextButton = v.findViewById(R.id.next);
|
||||||
ProgressBar progressBar = v.findViewById(R.id.progress);
|
progressBar = v.findViewById(R.id.progress);
|
||||||
|
|
||||||
passwordEntry.addTextChangedListener(this);
|
passwordEntry.addTextChangedListener(this);
|
||||||
passwordConfirmation.addTextChangedListener(this);
|
passwordConfirmation.addTextChangedListener(this);
|
||||||
@@ -74,17 +75,6 @@ public class SetPasswordFragment extends SetupFragment {
|
|||||||
passwordConfirmation.setImeOptions(IME_ACTION_DONE);
|
passwordConfirmation.setImeOptions(IME_ACTION_DONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
viewModel.getIsCreatingAccount()
|
|
||||||
.observe(getViewLifecycleOwner(), isCreatingAccount -> {
|
|
||||||
if (isCreatingAccount) {
|
|
||||||
nextButton.setVisibility(INVISIBLE);
|
|
||||||
progressBar.setVisibility(VISIBLE);
|
|
||||||
// this also avoids the keyboard popping up
|
|
||||||
passwordEntry.setFocusable(false);
|
|
||||||
passwordConfirmation.setFocusable(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,6 +116,20 @@ public class SetPasswordFragment extends SetupFragment {
|
|||||||
IBinder token = passwordEntry.getWindowToken();
|
IBinder token = passwordEntry.getWindowToken();
|
||||||
Object o = getContext().getSystemService(INPUT_METHOD_SERVICE);
|
Object o = getContext().getSystemService(INPUT_METHOD_SERVICE);
|
||||||
((InputMethodManager) o).hideSoftInputFromWindow(token, 0);
|
((InputMethodManager) o).hideSoftInputFromWindow(token, 0);
|
||||||
|
|
||||||
|
setNextClicked();
|
||||||
viewModel.setPassword(passwordEntry.getText().toString());
|
viewModel.setPassword(passwordEntry.getText().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void setNextClicked() {
|
||||||
|
super.setNextClicked();
|
||||||
|
|
||||||
|
passwordEntry.setFocusable(false);
|
||||||
|
passwordConfirmation.setFocusable(false);
|
||||||
|
if (!viewModel.needToShowDozeFragment()) {
|
||||||
|
nextButton.setVisibility(INVISIBLE);
|
||||||
|
progressBar.setVisibility(VISIBLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import android.view.KeyEvent;
|
|||||||
import android.view.Menu;
|
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.OnClickListener;
|
import android.view.View.OnClickListener;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.TextView.OnEditorActionListener;
|
import android.widget.TextView.OnEditorActionListener;
|
||||||
@@ -18,6 +19,8 @@ import org.briarproject.briar.android.fragment.BaseFragment;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import androidx.annotation.CallSuper;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
|
||||||
@@ -32,6 +35,7 @@ abstract class SetupFragment extends BaseFragment implements TextWatcher,
|
|||||||
OnEditorActionListener, OnClickListener {
|
OnEditorActionListener, OnClickListener {
|
||||||
|
|
||||||
private final static String STATE_KEY_CLICKED = "setupFragmentClicked";
|
private final static String STATE_KEY_CLICKED = "setupFragmentClicked";
|
||||||
|
private boolean clicked = false;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ViewModelProvider.Factory viewModelFactory;
|
ViewModelProvider.Factory viewModelFactory;
|
||||||
@@ -44,6 +48,27 @@ abstract class SetupFragment extends BaseFragment implements TextWatcher,
|
|||||||
.get(SetupViewModel.class);
|
.get(SetupViewModel.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
||||||
|
if (savedInstanceState != null) {
|
||||||
|
clicked = savedInstanceState.getBoolean(STATE_KEY_CLICKED);
|
||||||
|
}
|
||||||
|
if (clicked) {
|
||||||
|
setNextClicked();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||||
|
super.onSaveInstanceState(outState);
|
||||||
|
outState.putBoolean(STATE_KEY_CLICKED, clicked);
|
||||||
|
}
|
||||||
|
|
||||||
|
@CallSuper
|
||||||
|
void setNextClicked() {
|
||||||
|
this.clicked = true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||||
inflater.inflate(R.menu.help_action, menu);
|
inflater.inflate(R.menu.help_action, menu);
|
||||||
@@ -89,4 +114,5 @@ abstract class SetupFragment extends BaseFragment implements TextWatcher,
|
|||||||
public void afterTextChanged(Editable editable) {
|
public void afterTextChanged(Editable editable) {
|
||||||
// noop
|
// noop
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ import javax.inject.Inject;
|
|||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.lifecycle.AndroidViewModel;
|
import androidx.lifecycle.AndroidViewModel;
|
||||||
import androidx.lifecycle.LiveData;
|
|
||||||
import androidx.lifecycle.MutableLiveData;
|
|
||||||
|
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.briar.android.account.SetupViewModel.State.AUTHOR_NAME;
|
import static org.briarproject.briar.android.account.SetupViewModel.State.AUTHOR_NAME;
|
||||||
@@ -29,7 +27,6 @@ import static org.briarproject.briar.android.account.SetupViewModel.State.SET_PA
|
|||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
public
|
|
||||||
class SetupViewModel extends AndroidViewModel {
|
class SetupViewModel extends AndroidViewModel {
|
||||||
enum State {AUTHOR_NAME, SET_PASSWORD, DOZE, CREATED, FAILED}
|
enum State {AUTHOR_NAME, SET_PASSWORD, DOZE, CREATED, FAILED}
|
||||||
|
|
||||||
@@ -39,8 +36,6 @@ class SetupViewModel extends AndroidViewModel {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private String authorName, password;
|
private String authorName, password;
|
||||||
private final MutableLiveEvent<State> state = new MutableLiveEvent<>();
|
private final MutableLiveEvent<State> state = new MutableLiveEvent<>();
|
||||||
private final MutableLiveData<Boolean> isCreatingAccount =
|
|
||||||
new MutableLiveData<>(false);
|
|
||||||
|
|
||||||
private final AccountManager accountManager;
|
private final AccountManager accountManager;
|
||||||
private final Executor ioExecutor;
|
private final Executor ioExecutor;
|
||||||
@@ -72,10 +67,6 @@ class SetupViewModel extends AndroidViewModel {
|
|||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
LiveData<Boolean> getIsCreatingAccount() {
|
|
||||||
return isCreatingAccount;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setAuthorName(String authorName) {
|
void setAuthorName(String authorName) {
|
||||||
this.authorName = authorName;
|
this.authorName = authorName;
|
||||||
state.setEvent(SET_PASSWORD);
|
state.setEvent(SET_PASSWORD);
|
||||||
@@ -106,7 +97,6 @@ class SetupViewModel extends AndroidViewModel {
|
|||||||
private void createAccount() {
|
private void createAccount() {
|
||||||
if (authorName == null) throw new IllegalStateException();
|
if (authorName == null) throw new IllegalStateException();
|
||||||
if (password == null) throw new IllegalStateException();
|
if (password == null) throw new IllegalStateException();
|
||||||
isCreatingAccount.setValue(true);
|
|
||||||
ioExecutor.execute(() -> {
|
ioExecutor.execute(() -> {
|
||||||
if (accountManager.createAccount(authorName, password)) {
|
if (accountManager.createAccount(authorName, password)) {
|
||||||
LOG.info("Created account");
|
LOG.info("Created account");
|
||||||
|
|||||||
@@ -24,10 +24,6 @@ import org.briarproject.briar.android.blog.RssFeedImportActivity;
|
|||||||
import org.briarproject.briar.android.blog.RssFeedManageActivity;
|
import org.briarproject.briar.android.blog.RssFeedManageActivity;
|
||||||
import org.briarproject.briar.android.blog.WriteBlogPostActivity;
|
import org.briarproject.briar.android.blog.WriteBlogPostActivity;
|
||||||
import org.briarproject.briar.android.contact.ContactListFragment;
|
import org.briarproject.briar.android.contact.ContactListFragment;
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddNearbyContactActivity;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddNearbyContactErrorFragment;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddNearbyContactFragment;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddNearbyContactIntroFragment;
|
|
||||||
import org.briarproject.briar.android.contact.add.remote.AddContactActivity;
|
import org.briarproject.briar.android.contact.add.remote.AddContactActivity;
|
||||||
import org.briarproject.briar.android.contact.add.remote.LinkExchangeFragment;
|
import org.briarproject.briar.android.contact.add.remote.LinkExchangeFragment;
|
||||||
import org.briarproject.briar.android.contact.add.remote.NicknameFragment;
|
import org.briarproject.briar.android.contact.add.remote.NicknameFragment;
|
||||||
@@ -43,6 +39,10 @@ import org.briarproject.briar.android.fragment.ScreenFilterDialogFragment;
|
|||||||
import org.briarproject.briar.android.introduction.ContactChooserFragment;
|
import org.briarproject.briar.android.introduction.ContactChooserFragment;
|
||||||
import org.briarproject.briar.android.introduction.IntroductionActivity;
|
import org.briarproject.briar.android.introduction.IntroductionActivity;
|
||||||
import org.briarproject.briar.android.introduction.IntroductionMessageFragment;
|
import org.briarproject.briar.android.introduction.IntroductionMessageFragment;
|
||||||
|
import org.briarproject.briar.android.keyagreement.ContactExchangeActivity;
|
||||||
|
import org.briarproject.briar.android.keyagreement.ContactExchangeErrorFragment;
|
||||||
|
import org.briarproject.briar.android.keyagreement.KeyAgreementActivity;
|
||||||
|
import org.briarproject.briar.android.keyagreement.KeyAgreementFragment;
|
||||||
import org.briarproject.briar.android.login.ChangePasswordActivity;
|
import org.briarproject.briar.android.login.ChangePasswordActivity;
|
||||||
import org.briarproject.briar.android.login.OpenDatabaseFragment;
|
import org.briarproject.briar.android.login.OpenDatabaseFragment;
|
||||||
import org.briarproject.briar.android.login.PasswordFragment;
|
import org.briarproject.briar.android.login.PasswordFragment;
|
||||||
@@ -80,29 +80,19 @@ import org.briarproject.briar.android.sharing.ShareBlogFragment;
|
|||||||
import org.briarproject.briar.android.sharing.ShareForumActivity;
|
import org.briarproject.briar.android.sharing.ShareForumActivity;
|
||||||
import org.briarproject.briar.android.sharing.ShareForumFragment;
|
import org.briarproject.briar.android.sharing.ShareForumFragment;
|
||||||
import org.briarproject.briar.android.sharing.SharingModule;
|
import org.briarproject.briar.android.sharing.SharingModule;
|
||||||
import org.briarproject.briar.android.socialbackup.recover.CustodianRecoveryModeExplainerFragment;
|
//import org.briarproject.briar.android.socialbackup.CustodianDisplayFragment;
|
||||||
|
import org.briarproject.briar.android.socialbackup.CustodianHelpRecoverActivity;
|
||||||
import org.briarproject.briar.android.socialbackup.CustodianSelectorFragment;
|
import org.briarproject.briar.android.socialbackup.CustodianSelectorFragment;
|
||||||
import org.briarproject.briar.android.socialbackup.DistributedBackupActivity;
|
import org.briarproject.briar.android.socialbackup.DistributedBackupActivity;
|
||||||
import org.briarproject.briar.android.socialbackup.ExistingBackupFragment;
|
import org.briarproject.briar.android.socialbackup.ExistingBackupFragment;
|
||||||
import org.briarproject.briar.android.socialbackup.recover.CustodianReturnShardActivity;
|
import org.briarproject.briar.android.socialbackup.OwnerRecoveryModeExplainerFragment;
|
||||||
import org.briarproject.briar.android.socialbackup.recover.CustodianReturnShardErrorFragment;
|
import org.briarproject.briar.android.socialbackup.RecoverActivity;
|
||||||
import org.briarproject.briar.android.socialbackup.recover.CustodianReturnShardFragment;
|
import org.briarproject.briar.android.socialbackup.ShardQrCodeFragment;
|
||||||
import org.briarproject.briar.android.socialbackup.recover.CustodianReturnShardSuccessFragment;
|
|
||||||
import org.briarproject.briar.android.socialbackup.recover.OwnerRecoveryModeErrorFragment;
|
|
||||||
import org.briarproject.briar.android.socialbackup.recover.OwnerRecoveryModeExplainerFragment;
|
|
||||||
import org.briarproject.briar.android.socialbackup.recover.OwnerRecoveryModeMainFragment;
|
|
||||||
import org.briarproject.briar.android.socialbackup.recover.OwnerReturnShardActivity;
|
|
||||||
import org.briarproject.briar.android.socialbackup.recover.OwnerReturnShardFragment;
|
|
||||||
import org.briarproject.briar.android.socialbackup.ShardsSentFragment;
|
import org.briarproject.briar.android.socialbackup.ShardsSentFragment;
|
||||||
import org.briarproject.briar.android.socialbackup.ThresholdSelectorFragment;
|
import org.briarproject.briar.android.socialbackup.ThresholdSelectorFragment;
|
||||||
import org.briarproject.briar.android.socialbackup.creation.CreateBackupModule;
|
import org.briarproject.briar.android.socialbackup.creation.CreateBackupModule;
|
||||||
import org.briarproject.briar.android.socialbackup.recover.OwnerReturnShardSuccessFragment;
|
|
||||||
import org.briarproject.briar.android.socialbackup.recover.RestoreAccountActivity;
|
|
||||||
import org.briarproject.briar.android.socialbackup.recover.RestoreAccountDozeFragment;
|
|
||||||
import org.briarproject.briar.android.socialbackup.recover.RestoreAccountSetPasswordFragment;
|
|
||||||
import org.briarproject.briar.android.splash.SplashScreenActivity;
|
import org.briarproject.briar.android.splash.SplashScreenActivity;
|
||||||
import org.briarproject.briar.android.test.TestDataActivity;
|
import org.briarproject.briar.android.test.TestDataActivity;
|
||||||
import org.briarproject.briar.api.socialbackup.recovery.RestoreAccount;
|
|
||||||
|
|
||||||
import dagger.Component;
|
import dagger.Component;
|
||||||
|
|
||||||
@@ -133,7 +123,9 @@ public interface ActivityComponent {
|
|||||||
|
|
||||||
void inject(PanicPreferencesActivity activity);
|
void inject(PanicPreferencesActivity activity);
|
||||||
|
|
||||||
void inject(AddNearbyContactActivity activity);
|
void inject(ContactExchangeActivity activity);
|
||||||
|
|
||||||
|
void inject(KeyAgreementActivity activity);
|
||||||
|
|
||||||
void inject(ConversationActivity activity);
|
void inject(ConversationActivity activity);
|
||||||
|
|
||||||
@@ -207,13 +199,7 @@ public interface ActivityComponent {
|
|||||||
|
|
||||||
void inject(NewOrRecoverActivity newOrRecoverActivity);
|
void inject(NewOrRecoverActivity newOrRecoverActivity);
|
||||||
|
|
||||||
void inject(CustodianReturnShardActivity custodianReturnShardActivity);
|
void inject(CustodianHelpRecoverActivity custodianHelpRecoverActivity);
|
||||||
|
|
||||||
void inject(OwnerReturnShardActivity ownerReturnShardActivity);
|
|
||||||
|
|
||||||
void inject(OwnerRecoveryModeMainFragment ownerRecoveryModeMainFragment);
|
|
||||||
|
|
||||||
void inject(RestoreAccountActivity restoreAccountActivity);
|
|
||||||
|
|
||||||
// Fragments
|
// Fragments
|
||||||
|
|
||||||
@@ -241,9 +227,7 @@ public interface ActivityComponent {
|
|||||||
|
|
||||||
void inject(FeedFragment fragment);
|
void inject(FeedFragment fragment);
|
||||||
|
|
||||||
void inject(AddNearbyContactIntroFragment fragment);
|
void inject(KeyAgreementFragment fragment);
|
||||||
|
|
||||||
void inject(AddNearbyContactFragment fragment);
|
|
||||||
|
|
||||||
void inject(LinkExchangeFragment fragment);
|
void inject(LinkExchangeFragment fragment);
|
||||||
|
|
||||||
@@ -261,7 +245,7 @@ public interface ActivityComponent {
|
|||||||
|
|
||||||
void inject(ScreenFilterDialogFragment fragment);
|
void inject(ScreenFilterDialogFragment fragment);
|
||||||
|
|
||||||
void inject(AddNearbyContactErrorFragment fragment);
|
void inject(ContactExchangeErrorFragment fragment);
|
||||||
|
|
||||||
void inject(AliasDialogFragment aliasDialogFragment);
|
void inject(AliasDialogFragment aliasDialogFragment);
|
||||||
|
|
||||||
@@ -275,8 +259,12 @@ public interface ActivityComponent {
|
|||||||
|
|
||||||
void inject(ThresholdSelectorFragment thresholdSelectorFragment);
|
void inject(ThresholdSelectorFragment thresholdSelectorFragment);
|
||||||
|
|
||||||
|
void inject(ShardQrCodeFragment shardQrCodeFragment);
|
||||||
|
|
||||||
void inject(DistributedBackupActivity distributedBackupActivity);
|
void inject(DistributedBackupActivity distributedBackupActivity);
|
||||||
|
|
||||||
|
void inject(RecoverActivity recoverActivity);
|
||||||
|
|
||||||
void inject(DatabaseComponent databaseComponent);
|
void inject(DatabaseComponent databaseComponent);
|
||||||
|
|
||||||
void inject(CustodianSelectorFragment custodianSelectorFragment);
|
void inject(CustodianSelectorFragment custodianSelectorFragment);
|
||||||
@@ -288,22 +276,4 @@ public interface ActivityComponent {
|
|||||||
void inject(ExistingBackupFragment existingBackupFragment);
|
void inject(ExistingBackupFragment existingBackupFragment);
|
||||||
|
|
||||||
void inject(NewOrRecoverFragment newOrRecoverFragment);
|
void inject(NewOrRecoverFragment newOrRecoverFragment);
|
||||||
|
|
||||||
void inject(CustodianRecoveryModeExplainerFragment custodianRecoveryModeExplainerFragment);
|
|
||||||
|
|
||||||
void inject(CustodianReturnShardFragment custodianReturnShardFragment);
|
|
||||||
|
|
||||||
void inject(OwnerReturnShardFragment ownerReturnShardFragment);
|
|
||||||
|
|
||||||
void inject(CustodianReturnShardSuccessFragment custodianReturnShardSuccessFragment);
|
|
||||||
|
|
||||||
void inject(RestoreAccountSetPasswordFragment restoreAccountSetPasswordFragment);
|
|
||||||
|
|
||||||
void inject(RestoreAccountDozeFragment restoreAccountDozeFragment);
|
|
||||||
|
|
||||||
void inject(OwnerReturnShardSuccessFragment ownerReturnShardSuccessFragment);
|
|
||||||
|
|
||||||
void inject(OwnerRecoveryModeErrorFragment ownerRecoveryModeErrorFragment);
|
|
||||||
|
|
||||||
void inject(CustodianReturnShardErrorFragment custodianReturnShardErrorFragment);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ public interface RequestCodes {
|
|||||||
int REQUEST_WRITE_BLOG_POST = 5;
|
int REQUEST_WRITE_BLOG_POST = 5;
|
||||||
int REQUEST_SHARE_BLOG = 6;
|
int REQUEST_SHARE_BLOG = 6;
|
||||||
int REQUEST_RINGTONE = 7;
|
int REQUEST_RINGTONE = 7;
|
||||||
|
int REQUEST_PERMISSION_CAMERA_LOCATION = 8;
|
||||||
int REQUEST_DOZE_WHITELISTING = 9;
|
int REQUEST_DOZE_WHITELISTING = 9;
|
||||||
|
int REQUEST_BLUETOOTH_DISCOVERABLE = 10;
|
||||||
int REQUEST_UNLOCK = 11;
|
int REQUEST_UNLOCK = 11;
|
||||||
int REQUEST_KEYGUARD_UNLOCK = 12;
|
int REQUEST_KEYGUARD_UNLOCK = 12;
|
||||||
int REQUEST_ATTACH_IMAGE = 13;
|
int REQUEST_ATTACH_IMAGE = 13;
|
||||||
|
|||||||
@@ -28,12 +28,9 @@ import javax.inject.Inject;
|
|||||||
import androidx.lifecycle.LiveData;
|
import androidx.lifecycle.LiveData;
|
||||||
import androidx.lifecycle.MutableLiveData;
|
import androidx.lifecycle.MutableLiveData;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
|
||||||
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.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.AndroidUtils.getSupportedImageContentTypes;
|
|
||||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||||
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.AVAILABLE;
|
import static org.briarproject.briar.android.attachment.AttachmentItem.State.AVAILABLE;
|
||||||
@@ -89,17 +86,7 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
|
|||||||
List<AttachmentHeader> headers = messageHeader.getAttachmentHeaders();
|
List<AttachmentHeader> headers = messageHeader.getAttachmentHeaders();
|
||||||
List<LiveData<AttachmentItem>> items = new ArrayList<>(headers.size());
|
List<LiveData<AttachmentItem>> items = new ArrayList<>(headers.size());
|
||||||
boolean needsSize = headers.size() == 1;
|
boolean needsSize = headers.size() == 1;
|
||||||
List<String> supported = asList(getSupportedImageContentTypes());
|
|
||||||
for (AttachmentHeader h : headers) {
|
for (AttachmentHeader h : headers) {
|
||||||
// Fail early if we don't support the content type
|
|
||||||
if (!supported.contains(h.getContentType())) {
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Unsupported content type " + h.getContentType());
|
|
||||||
}
|
|
||||||
AttachmentItem item = new AttachmentItem(h, "", ERROR);
|
|
||||||
items.add(new MutableLiveData<>(item));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// try cache for existing item live data
|
// try cache for existing item live data
|
||||||
MutableLiveData<AttachmentItem> liveData =
|
MutableLiveData<AttachmentItem> liveData =
|
||||||
itemsWithSize.get(h.getMessageId());
|
itemsWithSize.get(h.getMessageId());
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package org.briarproject.briar.android.contact;
|
package org.briarproject.briar.android.contact;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
import org.briarproject.briar.android.util.BriarAdapter;
|
import org.briarproject.briar.android.util.BriarAdapter;
|
||||||
|
|
||||||
@@ -44,4 +45,8 @@ public abstract class BaseContactListAdapter<I extends ContactItem, VH extends C
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface OnContactClickListener<I> {
|
||||||
|
void onItemClick(View view, I item);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import android.widget.TextView;
|
|||||||
|
|
||||||
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 javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import android.view.ViewGroup;
|
|||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.NullSafety;
|
import org.briarproject.bramble.api.nullsafety.NullSafety;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
|
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||||
|
|
||||||
import androidx.recyclerview.widget.DiffUtil.ItemCallback;
|
import androidx.recyclerview.widget.DiffUtil.ItemCallback;
|
||||||
import androidx.recyclerview.widget.ListAdapter;
|
import androidx.recyclerview.widget.ListAdapter;
|
||||||
@@ -15,6 +16,9 @@ import androidx.recyclerview.widget.ListAdapter;
|
|||||||
public class ContactListAdapter extends
|
public class ContactListAdapter extends
|
||||||
ListAdapter<ContactListItem, ContactListItemViewHolder> {
|
ListAdapter<ContactListItem, ContactListItemViewHolder> {
|
||||||
|
|
||||||
|
// TODO: using the click listener interface from BaseContactListAdapter on
|
||||||
|
// purpose here because it is entangled with ContactListItemViewHolder. At
|
||||||
|
// some point we probably want to change that.
|
||||||
protected final OnContactClickListener<ContactListItem> listener;
|
protected final OnContactClickListener<ContactListItem> listener;
|
||||||
|
|
||||||
public ContactListAdapter(
|
public ContactListAdapter(
|
||||||
|
|||||||
@@ -15,11 +15,12 @@ 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;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddNearbyContactActivity;
|
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||||
import org.briarproject.briar.android.contact.add.remote.AddContactActivity;
|
import org.briarproject.briar.android.contact.add.remote.AddContactActivity;
|
||||||
import org.briarproject.briar.android.contact.add.remote.PendingContactListActivity;
|
import org.briarproject.briar.android.contact.add.remote.PendingContactListActivity;
|
||||||
import org.briarproject.briar.android.conversation.ConversationActivity;
|
import org.briarproject.briar.android.conversation.ConversationActivity;
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||||
|
import org.briarproject.briar.android.keyagreement.ContactExchangeActivity;
|
||||||
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
|
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
|
||||||
import org.briarproject.briar.android.view.BriarRecyclerView;
|
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||||
|
|
||||||
@@ -33,6 +34,7 @@ import io.github.kobakei.materialfabspeeddial.FabSpeedDial;
|
|||||||
import io.github.kobakei.materialfabspeeddial.FabSpeedDial.OnMenuItemClickListener;
|
import io.github.kobakei.materialfabspeeddial.FabSpeedDial.OnMenuItemClickListener;
|
||||||
|
|
||||||
import static com.google.android.material.snackbar.BaseTransientBottomBar.LENGTH_INDEFINITE;
|
import static com.google.android.material.snackbar.BaseTransientBottomBar.LENGTH_INDEFINITE;
|
||||||
|
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
||||||
import static org.briarproject.briar.android.conversation.ConversationActivity.CONTACT_ID;
|
import static org.briarproject.briar.android.conversation.ConversationActivity.CONTACT_ID;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@@ -100,6 +102,7 @@ public class ContactListFragment extends BaseFragment
|
|||||||
.observe(getViewLifecycleOwner(), result -> {
|
.observe(getViewLifecycleOwner(), result -> {
|
||||||
result.onError(this::handleException).onSuccess(items -> {
|
result.onError(this::handleException).onSuccess(items -> {
|
||||||
adapter.submitList(items);
|
adapter.submitList(items);
|
||||||
|
if (requireNonNull(items).size() == 0) list.showData();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
viewModel.getHasPendingContacts()
|
viewModel.getHasPendingContacts()
|
||||||
@@ -125,8 +128,7 @@ public class ContactListFragment extends BaseFragment
|
|||||||
switch (itemId) {
|
switch (itemId) {
|
||||||
case R.id.action_add_contact_nearby:
|
case R.id.action_add_contact_nearby:
|
||||||
Intent intent =
|
Intent intent =
|
||||||
new Intent(getContext(),
|
new Intent(getContext(), ContactExchangeActivity.class);
|
||||||
AddNearbyContactActivity.class);
|
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
return;
|
return;
|
||||||
case R.id.action_add_contact_remotely:
|
case R.id.action_add_contact_remotely:
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import android.widget.TextView;
|
|||||||
|
|
||||||
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 java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
|
|||||||
@@ -3,41 +3,70 @@ package org.briarproject.briar.android.contact;
|
|||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||||
|
import org.briarproject.bramble.api.contact.Contact;
|
||||||
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
|
import org.briarproject.bramble.api.contact.event.ContactAddedEvent;
|
||||||
|
import org.briarproject.bramble.api.contact.event.ContactRemovedEvent;
|
||||||
import org.briarproject.bramble.api.contact.event.PendingContactAddedEvent;
|
import org.briarproject.bramble.api.contact.event.PendingContactAddedEvent;
|
||||||
import org.briarproject.bramble.api.contact.event.PendingContactRemovedEvent;
|
import org.briarproject.bramble.api.contact.event.PendingContactRemovedEvent;
|
||||||
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.Transaction;
|
||||||
import org.briarproject.bramble.api.db.TransactionManager;
|
import org.briarproject.bramble.api.db.TransactionManager;
|
||||||
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.lifecycle.LifecycleManager;
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||||
|
import org.briarproject.briar.android.viewmodel.DbViewModel;
|
||||||
|
import org.briarproject.briar.android.viewmodel.LiveResult;
|
||||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||||
|
import org.briarproject.briar.api.avatar.event.AvatarUpdatedEvent;
|
||||||
|
import org.briarproject.briar.api.client.MessageTracker;
|
||||||
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.event.ConversationMessageReceivedEvent;
|
||||||
|
import org.briarproject.briar.api.identity.AuthorInfo;
|
||||||
import org.briarproject.briar.api.identity.AuthorManager;
|
import org.briarproject.briar.api.identity.AuthorManager;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.Executor;
|
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.arch.core.util.Function;
|
||||||
import androidx.lifecycle.LiveData;
|
import androidx.lifecycle.LiveData;
|
||||||
import androidx.lifecycle.MutableLiveData;
|
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.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;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class ContactListViewModel extends ContactsViewModel {
|
class ContactListViewModel extends DbViewModel implements EventListener {
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(ContactListViewModel.class.getName());
|
getLogger(ContactListViewModel.class.getName());
|
||||||
|
|
||||||
|
private final ContactManager contactManager;
|
||||||
|
private final AuthorManager authorManager;
|
||||||
|
private final ConversationManager conversationManager;
|
||||||
|
private final ConnectionRegistry connectionRegistry;
|
||||||
|
private final EventBus eventBus;
|
||||||
private final AndroidNotificationManager notificationManager;
|
private final AndroidNotificationManager notificationManager;
|
||||||
|
|
||||||
|
private final MutableLiveData<LiveResult<List<ContactListItem>>>
|
||||||
|
contactListItems = new MutableLiveData<>();
|
||||||
|
|
||||||
private final MutableLiveData<Boolean> hasPendingContacts =
|
private final MutableLiveData<Boolean> hasPendingContacts =
|
||||||
new MutableLiveData<>();
|
new MutableLiveData<>();
|
||||||
|
|
||||||
@@ -50,25 +79,99 @@ class ContactListViewModel extends ContactsViewModel {
|
|||||||
ConversationManager conversationManager,
|
ConversationManager conversationManager,
|
||||||
ConnectionRegistry connectionRegistry, EventBus eventBus,
|
ConnectionRegistry connectionRegistry, EventBus eventBus,
|
||||||
AndroidNotificationManager notificationManager) {
|
AndroidNotificationManager notificationManager) {
|
||||||
super(application, dbExecutor, lifecycleManager, db, androidExecutor,
|
super(application, dbExecutor, lifecycleManager, db, androidExecutor);
|
||||||
contactManager, authorManager, conversationManager,
|
this.contactManager = contactManager;
|
||||||
connectionRegistry, eventBus);
|
this.authorManager = authorManager;
|
||||||
|
this.conversationManager = conversationManager;
|
||||||
|
this.connectionRegistry = connectionRegistry;
|
||||||
|
this.eventBus = eventBus;
|
||||||
this.notificationManager = notificationManager;
|
this.notificationManager = notificationManager;
|
||||||
|
this.eventBus.addListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCleared() {
|
||||||
|
super.onCleared();
|
||||||
|
eventBus.removeListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadContacts() {
|
||||||
|
loadList(this::loadContacts, contactListItems::setValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<ContactListItem> loadContacts(Transaction txn)
|
||||||
|
throws DbException {
|
||||||
|
long start = now();
|
||||||
|
List<ContactListItem> contacts = new ArrayList<>();
|
||||||
|
for (Contact c : contactManager.getContacts(txn)) {
|
||||||
|
ContactId id = c.getId();
|
||||||
|
AuthorInfo authorInfo = authorManager.getAuthorInfo(txn, c);
|
||||||
|
MessageTracker.GroupCount count =
|
||||||
|
conversationManager.getGroupCount(txn, id);
|
||||||
|
boolean connected = connectionRegistry.isConnected(c.getId());
|
||||||
|
contacts.add(new ContactListItem(c, authorInfo, connected, count));
|
||||||
|
}
|
||||||
|
Collections.sort(contacts);
|
||||||
|
logDuration(LOG, "Full load", start);
|
||||||
|
return contacts;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eventOccurred(Event e) {
|
public void eventOccurred(Event e) {
|
||||||
super.eventOccurred(e);
|
if (e instanceof ContactAddedEvent) {
|
||||||
if (e instanceof PendingContactAddedEvent ||
|
LOG.info("Contact added, reloading");
|
||||||
|
loadContacts();
|
||||||
|
} else if (e instanceof ContactConnectedEvent) {
|
||||||
|
updateItem(((ContactConnectedEvent) e).getContactId(),
|
||||||
|
item -> new ContactListItem(item, true), false);
|
||||||
|
} else if (e instanceof ContactDisconnectedEvent) {
|
||||||
|
updateItem(((ContactDisconnectedEvent) e).getContactId(),
|
||||||
|
item -> new ContactListItem(item, false), false);
|
||||||
|
} else if (e instanceof ContactRemovedEvent) {
|
||||||
|
LOG.info("Contact removed, removing item");
|
||||||
|
removeItem(((ContactRemovedEvent) e).getContactId());
|
||||||
|
} else if (e instanceof ConversationMessageReceivedEvent) {
|
||||||
|
LOG.info("Conversation message received, updating item");
|
||||||
|
ConversationMessageReceivedEvent<?> p =
|
||||||
|
(ConversationMessageReceivedEvent<?>) e;
|
||||||
|
ConversationMessageHeader h = p.getMessageHeader();
|
||||||
|
updateItem(p.getContactId(), item -> new ContactListItem(item, h),
|
||||||
|
true);
|
||||||
|
} else if (e instanceof PendingContactAddedEvent ||
|
||||||
e instanceof PendingContactRemovedEvent) {
|
e instanceof PendingContactRemovedEvent) {
|
||||||
checkForPendingContacts();
|
checkForPendingContacts();
|
||||||
|
} else if (e instanceof AvatarUpdatedEvent) {
|
||||||
|
AvatarUpdatedEvent a = (AvatarUpdatedEvent) e;
|
||||||
|
updateItem(a.getContactId(), item -> new ContactListItem(item,
|
||||||
|
a.getAttachmentHeader()), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LiveData<LiveResult<List<ContactListItem>>> getContactListItems() {
|
||||||
|
return contactListItems;
|
||||||
|
}
|
||||||
|
|
||||||
LiveData<Boolean> getHasPendingContacts() {
|
LiveData<Boolean> getHasPendingContacts() {
|
||||||
return hasPendingContacts;
|
return hasPendingContacts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateItem(ContactId c,
|
||||||
|
Function<ContactListItem, ContactListItem> replacer, boolean sort) {
|
||||||
|
List<ContactListItem> list = updateListItems(contactListItems,
|
||||||
|
itemToTest -> itemToTest.getContact().getId().equals(c),
|
||||||
|
replacer);
|
||||||
|
if (list == null) return;
|
||||||
|
if (sort) Collections.sort(list);
|
||||||
|
contactListItems.setValue(new LiveResult<>(list));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeItem(ContactId c) {
|
||||||
|
List<ContactListItem> list = removeListItems(contactListItems,
|
||||||
|
itemToTest -> itemToTest.getContact().getId().equals(c));
|
||||||
|
if (list == null) return;
|
||||||
|
contactListItems.setValue(new LiveResult<>(list));
|
||||||
|
}
|
||||||
|
|
||||||
void checkForPendingContacts() {
|
void checkForPendingContacts() {
|
||||||
runOnDbThread(() -> {
|
runOnDbThread(() -> {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,170 +0,0 @@
|
|||||||
package org.briarproject.briar.android.contact;
|
|
||||||
|
|
||||||
import android.app.Application;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
|
||||||
import org.briarproject.bramble.api.contact.Contact;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
|
||||||
import org.briarproject.bramble.api.contact.event.ContactAddedEvent;
|
|
||||||
import org.briarproject.bramble.api.contact.event.ContactRemovedEvent;
|
|
||||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
|
||||||
import org.briarproject.bramble.api.db.Transaction;
|
|
||||||
import org.briarproject.bramble.api.db.TransactionManager;
|
|
||||||
import org.briarproject.bramble.api.event.Event;
|
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
|
||||||
import org.briarproject.bramble.api.event.EventListener;
|
|
||||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
|
||||||
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
|
||||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
|
||||||
import org.briarproject.briar.android.viewmodel.DbViewModel;
|
|
||||||
import org.briarproject.briar.android.viewmodel.LiveResult;
|
|
||||||
import org.briarproject.briar.api.avatar.event.AvatarUpdatedEvent;
|
|
||||||
import org.briarproject.briar.api.client.MessageTracker;
|
|
||||||
import org.briarproject.briar.api.conversation.ConversationManager;
|
|
||||||
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
|
||||||
import org.briarproject.briar.api.conversation.event.ConversationMessageReceivedEvent;
|
|
||||||
import org.briarproject.briar.api.identity.AuthorInfo;
|
|
||||||
import org.briarproject.briar.api.identity.AuthorManager;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import androidx.annotation.UiThread;
|
|
||||||
import androidx.arch.core.util.Function;
|
|
||||||
import androidx.lifecycle.LiveData;
|
|
||||||
import androidx.lifecycle.MutableLiveData;
|
|
||||||
|
|
||||||
import static java.util.logging.Logger.getLogger;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.now;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
public class ContactsViewModel extends DbViewModel implements EventListener {
|
|
||||||
|
|
||||||
private static final Logger LOG =
|
|
||||||
getLogger(ContactsViewModel.class.getName());
|
|
||||||
|
|
||||||
protected final ContactManager contactManager;
|
|
||||||
private final AuthorManager authorManager;
|
|
||||||
private final ConversationManager conversationManager;
|
|
||||||
private final ConnectionRegistry connectionRegistry;
|
|
||||||
private final EventBus eventBus;
|
|
||||||
|
|
||||||
private final MutableLiveData<LiveResult<List<ContactListItem>>>
|
|
||||||
contactListItems = new MutableLiveData<>();
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public ContactsViewModel(Application application,
|
|
||||||
@DatabaseExecutor Executor dbExecutor,
|
|
||||||
LifecycleManager lifecycleManager, TransactionManager db,
|
|
||||||
AndroidExecutor androidExecutor, ContactManager contactManager,
|
|
||||||
AuthorManager authorManager,
|
|
||||||
ConversationManager conversationManager,
|
|
||||||
ConnectionRegistry connectionRegistry, EventBus eventBus) {
|
|
||||||
super(application, dbExecutor, lifecycleManager, db, androidExecutor);
|
|
||||||
this.contactManager = contactManager;
|
|
||||||
this.authorManager = authorManager;
|
|
||||||
this.conversationManager = conversationManager;
|
|
||||||
this.connectionRegistry = connectionRegistry;
|
|
||||||
this.eventBus = eventBus;
|
|
||||||
this.eventBus.addListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCleared() {
|
|
||||||
super.onCleared();
|
|
||||||
eventBus.removeListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void loadContacts() {
|
|
||||||
loadList(this::loadContacts, contactListItems::setValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<ContactListItem> loadContacts(Transaction txn)
|
|
||||||
throws DbException {
|
|
||||||
long start = now();
|
|
||||||
List<ContactListItem> contacts = new ArrayList<>();
|
|
||||||
for (Contact c : contactManager.getContacts(txn)) {
|
|
||||||
ContactId id = c.getId();
|
|
||||||
if (!displayContact(id)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
AuthorInfo authorInfo = authorManager.getAuthorInfo(txn, c);
|
|
||||||
MessageTracker.GroupCount count =
|
|
||||||
conversationManager.getGroupCount(txn, id);
|
|
||||||
boolean connected = connectionRegistry.isConnected(c.getId());
|
|
||||||
contacts.add(new ContactListItem(c, authorInfo, connected, count));
|
|
||||||
}
|
|
||||||
Collections.sort(contacts);
|
|
||||||
logDuration(LOG, "Full load", start);
|
|
||||||
return contacts;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Override this method to display only a subset of contacts.
|
|
||||||
*/
|
|
||||||
protected boolean displayContact(ContactId contactId) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void eventOccurred(Event e) {
|
|
||||||
if (e instanceof ContactAddedEvent) {
|
|
||||||
LOG.info("Contact added, reloading");
|
|
||||||
loadContacts();
|
|
||||||
} else if (e instanceof ContactConnectedEvent) {
|
|
||||||
updateItem(((ContactConnectedEvent) e).getContactId(),
|
|
||||||
item -> new ContactListItem(item, true), false);
|
|
||||||
} else if (e instanceof ContactDisconnectedEvent) {
|
|
||||||
updateItem(((ContactDisconnectedEvent) e).getContactId(),
|
|
||||||
item -> new ContactListItem(item, false), false);
|
|
||||||
} else if (e instanceof ContactRemovedEvent) {
|
|
||||||
LOG.info("Contact removed, removing item");
|
|
||||||
removeItem(((ContactRemovedEvent) e).getContactId());
|
|
||||||
} else if (e instanceof ConversationMessageReceivedEvent) {
|
|
||||||
LOG.info("Conversation message received, updating item");
|
|
||||||
ConversationMessageReceivedEvent<?> p =
|
|
||||||
(ConversationMessageReceivedEvent<?>) e;
|
|
||||||
ConversationMessageHeader h = p.getMessageHeader();
|
|
||||||
updateItem(p.getContactId(), item -> new ContactListItem(item, h),
|
|
||||||
true);
|
|
||||||
} else if (e instanceof AvatarUpdatedEvent) {
|
|
||||||
AvatarUpdatedEvent a = (AvatarUpdatedEvent) e;
|
|
||||||
updateItem(a.getContactId(), item -> new ContactListItem(item,
|
|
||||||
a.getAttachmentHeader()), false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public LiveData<LiveResult<List<ContactListItem>>> getContactListItems() {
|
|
||||||
return contactListItems;
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
private void updateItem(ContactId c,
|
|
||||||
Function<ContactListItem, ContactListItem> replacer, boolean sort) {
|
|
||||||
List<ContactListItem> list = updateListItems(contactListItems,
|
|
||||||
itemToTest -> itemToTest.getContact().getId().equals(c),
|
|
||||||
replacer);
|
|
||||||
if (list == null) return;
|
|
||||||
if (sort) Collections.sort(list);
|
|
||||||
contactListItems.setValue(new LiveResult<>(list));
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
private void removeItem(ContactId c) {
|
|
||||||
List<ContactListItem> list = removeListItems(contactListItems,
|
|
||||||
itemToTest -> itemToTest.getContact().getId().equals(c));
|
|
||||||
if (list == null) return;
|
|
||||||
contactListItems.setValue(new LiveResult<>(list));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package org.briarproject.briar.android.contact;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.briar.R;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
|
public class LegacyContactListAdapter extends
|
||||||
|
BaseContactListAdapter<ContactListItem, ContactListItemViewHolder> {
|
||||||
|
|
||||||
|
public LegacyContactListAdapter(Context context,
|
||||||
|
OnContactClickListener<ContactListItem> listener) {
|
||||||
|
super(context, ContactListItem.class, listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ContactListItemViewHolder onCreateViewHolder(ViewGroup viewGroup,
|
||||||
|
int i) {
|
||||||
|
View v = LayoutInflater.from(viewGroup.getContext()).inflate(
|
||||||
|
R.layout.list_item_contact, viewGroup, false);
|
||||||
|
|
||||||
|
return new ContactListItemViewHolder(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean areContentsTheSame(ContactListItem c1, ContactListItem c2) {
|
||||||
|
// check for all properties that influence visual
|
||||||
|
// representation of contact
|
||||||
|
if (c1.isEmpty() != c2.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (c1.getUnreadCount() != c2.getUnreadCount()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (c1.getTimestamp() != c2.getTimestamp()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return c1.isConnected() == c2.isConnected();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compare(ContactListItem c1, ContactListItem c2) {
|
||||||
|
return Long.compare(c2.getTimestamp(), c1.getTimestamp());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package org.briarproject.briar.android.contact;
|
|
||||||
|
|
||||||
import android.view.View;
|
|
||||||
|
|
||||||
public interface OnContactClickListener<I> {
|
|
||||||
|
|
||||||
void onItemClick(View view, I item);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
package org.briarproject.briar.android.contact.add.nearby;
|
|
||||||
|
|
||||||
import android.graphics.Bitmap;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
|
|
||||||
abstract class AddContactState {
|
|
||||||
|
|
||||||
static class KeyAgreementListening extends AddContactState {
|
|
||||||
final Bitmap qrCode;
|
|
||||||
|
|
||||||
KeyAgreementListening(Bitmap qrCode) {
|
|
||||||
this.qrCode = qrCode;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static class QrCodeScanned extends AddContactState {
|
|
||||||
}
|
|
||||||
|
|
||||||
static class KeyAgreementWaiting extends AddContactState {
|
|
||||||
}
|
|
||||||
|
|
||||||
static class KeyAgreementStarted extends AddContactState {
|
|
||||||
}
|
|
||||||
|
|
||||||
static class ContactExchangeStarted extends AddContactState {
|
|
||||||
}
|
|
||||||
|
|
||||||
static class ContactExchangeFinished extends AddContactState {
|
|
||||||
final ContactExchangeResult result;
|
|
||||||
|
|
||||||
ContactExchangeFinished(ContactExchangeResult result) {
|
|
||||||
this.result = result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static class Failed extends AddContactState {
|
|
||||||
/**
|
|
||||||
* Non-null if failed due to the scanned QR code version.
|
|
||||||
* True if the app producing the code is too old.
|
|
||||||
* False if the scanning app is too old.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
final Boolean qrCodeTooOld;
|
|
||||||
|
|
||||||
Failed(@Nullable Boolean qrCodeTooOld) {
|
|
||||||
this.qrCodeTooOld = qrCodeTooOld;
|
|
||||||
}
|
|
||||||
|
|
||||||
Failed() {
|
|
||||||
this(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract static class ContactExchangeResult {
|
|
||||||
static class Success extends ContactExchangeResult {
|
|
||||||
final Author remoteAuthor;
|
|
||||||
|
|
||||||
Success(Author remoteAuthor) {
|
|
||||||
this.remoteAuthor = remoteAuthor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static class Error extends ContactExchangeResult {
|
|
||||||
@Nullable
|
|
||||||
final Author duplicateAuthor;
|
|
||||||
|
|
||||||
Error(@Nullable Author duplicateAuthor) {
|
|
||||||
this.duplicateAuthor = duplicateAuthor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} // end ContactExchangeResult
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,204 +0,0 @@
|
|||||||
package org.briarproject.briar.android.contact.add.nearby;
|
|
||||||
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
|
||||||
import org.briarproject.briar.R;
|
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
|
||||||
import org.briarproject.briar.android.activity.BriarActivity;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.ContactExchangeFinished;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.ContactExchangeResult;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.Failed;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddNearbyContactViewModel.BluetoothDecision;
|
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment.BaseFragmentListener;
|
|
||||||
import org.briarproject.briar.android.util.RequestBluetoothDiscoverable;
|
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import androidx.activity.result.ActivityResultLauncher;
|
|
||||||
import androidx.appcompat.widget.Toolbar;
|
|
||||||
import androidx.fragment.app.FragmentManager;
|
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
|
||||||
|
|
||||||
import static android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
|
|
||||||
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
|
|
||||||
import static android.widget.Toast.LENGTH_LONG;
|
|
||||||
import static java.util.Objects.requireNonNull;
|
|
||||||
import static java.util.logging.Logger.getLogger;
|
|
||||||
import static org.briarproject.briar.android.contact.add.nearby.AddNearbyContactViewModel.BluetoothDecision.ACCEPTED;
|
|
||||||
import static org.briarproject.briar.android.contact.add.nearby.AddNearbyContactViewModel.BluetoothDecision.REFUSED;
|
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
|
||||||
@ParametersNotNullByDefault
|
|
||||||
public class AddNearbyContactActivity extends BriarActivity
|
|
||||||
implements BaseFragmentListener {
|
|
||||||
|
|
||||||
private static final Logger LOG =
|
|
||||||
getLogger(AddNearbyContactActivity.class.getName());
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
ViewModelProvider.Factory viewModelFactory;
|
|
||||||
|
|
||||||
private AddNearbyContactViewModel viewModel;
|
|
||||||
private final ActivityResultLauncher<Integer> bluetoothLauncher =
|
|
||||||
registerForActivityResult(new RequestBluetoothDiscoverable(),
|
|
||||||
this::onBluetoothDiscoverableResult);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void injectActivity(ActivityComponent component) {
|
|
||||||
component.inject(this);
|
|
||||||
viewModel = new ViewModelProvider(this, viewModelFactory)
|
|
||||||
.get(AddNearbyContactViewModel.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCreate(@Nullable Bundle state) {
|
|
||||||
super.onCreate(state);
|
|
||||||
setContentView(R.layout.activity_fragment_container_toolbar);
|
|
||||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
|
||||||
setSupportActionBar(toolbar);
|
|
||||||
requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
|
|
||||||
if (state == null) {
|
|
||||||
showInitialFragment(AddNearbyContactIntroFragment.newInstance());
|
|
||||||
}
|
|
||||||
viewModel.getRequestBluetoothDiscoverable().observeEvent(this, r ->
|
|
||||||
requestBluetoothDiscoverable()); // never false
|
|
||||||
viewModel.getShowQrCodeFragment().observeEvent(this, show -> {
|
|
||||||
if (show) showQrCodeFragment();
|
|
||||||
});
|
|
||||||
requireNonNull(getSupportActionBar())
|
|
||||||
.setTitle(R.string.add_contact_title);
|
|
||||||
viewModel.getState()
|
|
||||||
.observe(this, this::onAddContactStateChanged);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPostResume() {
|
|
||||||
super.onPostResume();
|
|
||||||
viewModel.setIsActivityResumed(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPause() {
|
|
||||||
super.onPause();
|
|
||||||
viewModel.setIsActivityResumed(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onBluetoothDiscoverableResult(boolean discoverable) {
|
|
||||||
if (discoverable) {
|
|
||||||
LOG.info("Bluetooth discoverability was accepted");
|
|
||||||
viewModel.setBluetoothDecision(ACCEPTED);
|
|
||||||
} else {
|
|
||||||
LOG.info("Bluetooth discoverability was refused");
|
|
||||||
viewModel.setBluetoothDecision(REFUSED);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
|
||||||
if (item.getItemId() == android.R.id.home) {
|
|
||||||
onBackPressed();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return super.onOptionsItemSelected(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onBackPressed() {
|
|
||||||
if (viewModel.getState().getValue() instanceof Failed) {
|
|
||||||
// re-create this activity when going back in failed state
|
|
||||||
Intent i = new Intent(this, AddNearbyContactActivity.class);
|
|
||||||
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
|
|
||||||
startActivity(i);
|
|
||||||
} else {
|
|
||||||
super.onBackPressed();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void requestBluetoothDiscoverable() {
|
|
||||||
Intent i = new Intent(ACTION_REQUEST_DISCOVERABLE);
|
|
||||||
if (i.resolveActivity(getPackageManager()) != null) {
|
|
||||||
LOG.info("Asking for Bluetooth discoverability");
|
|
||||||
viewModel.setBluetoothDecision(BluetoothDecision.WAITING);
|
|
||||||
bluetoothLauncher.launch(120); // 2min discoverable
|
|
||||||
} else {
|
|
||||||
viewModel.setBluetoothDecision(BluetoothDecision.NO_ADAPTER);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showQrCodeFragment() {
|
|
||||||
// FIXME #824
|
|
||||||
FragmentManager fm = getSupportFragmentManager();
|
|
||||||
if (fm.findFragmentByTag(AddNearbyContactFragment.TAG) == null) {
|
|
||||||
BaseFragment f = AddNearbyContactFragment.newInstance();
|
|
||||||
fm.beginTransaction()
|
|
||||||
.replace(R.id.fragmentContainer, f, f.getUniqueTag())
|
|
||||||
.addToBackStack(f.getUniqueTag())
|
|
||||||
.commit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onAddContactStateChanged(@Nullable AddContactState state) {
|
|
||||||
if (state instanceof ContactExchangeFinished) {
|
|
||||||
ContactExchangeResult result =
|
|
||||||
((ContactExchangeFinished) state).result;
|
|
||||||
onContactExchangeResult(result);
|
|
||||||
} else if (state instanceof Failed) {
|
|
||||||
Boolean qrCodeTooOld = ((Failed) state).qrCodeTooOld;
|
|
||||||
onAddingContactFailed(qrCodeTooOld);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onContactExchangeResult(ContactExchangeResult result) {
|
|
||||||
if (result instanceof ContactExchangeResult.Success) {
|
|
||||||
Author remoteAuthor =
|
|
||||||
((ContactExchangeResult.Success) result).remoteAuthor;
|
|
||||||
String contactName = remoteAuthor.getName();
|
|
||||||
String text = getString(R.string.contact_added_toast, contactName);
|
|
||||||
Toast.makeText(this, text, LENGTH_LONG).show();
|
|
||||||
supportFinishAfterTransition();
|
|
||||||
} else if (result instanceof ContactExchangeResult.Error) {
|
|
||||||
Author duplicateAuthor =
|
|
||||||
((ContactExchangeResult.Error) result).duplicateAuthor;
|
|
||||||
if (duplicateAuthor == null) {
|
|
||||||
showErrorFragment();
|
|
||||||
} else {
|
|
||||||
String contactName = duplicateAuthor.getName();
|
|
||||||
String text =
|
|
||||||
getString(R.string.contact_already_exists, contactName);
|
|
||||||
Toast.makeText(this, text, LENGTH_LONG).show();
|
|
||||||
supportFinishAfterTransition();
|
|
||||||
}
|
|
||||||
} else throw new AssertionError();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onAddingContactFailed(@Nullable Boolean qrCodeTooOld) {
|
|
||||||
if (qrCodeTooOld == null) {
|
|
||||||
showErrorFragment();
|
|
||||||
} else {
|
|
||||||
String msg;
|
|
||||||
if (qrCodeTooOld) {
|
|
||||||
msg = getString(R.string.qr_code_too_old,
|
|
||||||
getString(R.string.app_name));
|
|
||||||
} else {
|
|
||||||
msg = getString(R.string.qr_code_too_new,
|
|
||||||
getString(R.string.app_name));
|
|
||||||
}
|
|
||||||
showNextFragment(AddNearbyContactErrorFragment.newInstance(msg));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showErrorFragment() {
|
|
||||||
showNextFragment(new AddNearbyContactErrorFragment());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,202 +0,0 @@
|
|||||||
package org.briarproject.briar.android.contact.add.nearby;
|
|
||||||
|
|
||||||
import android.graphics.Bitmap;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.LinearLayout;
|
|
||||||
import android.widget.LinearLayout.LayoutParams;
|
|
||||||
import android.widget.TextView;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
|
||||||
import org.briarproject.briar.R;
|
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.ContactExchangeStarted;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.Failed;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.KeyAgreementStarted;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.KeyAgreementWaiting;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.QrCodeScanned;
|
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
|
||||||
import org.briarproject.briar.android.view.QrCodeView;
|
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import androidx.annotation.UiThread;
|
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
|
||||||
|
|
||||||
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
|
|
||||||
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
|
|
||||||
import static android.view.View.INVISIBLE;
|
|
||||||
import static android.view.View.VISIBLE;
|
|
||||||
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
|
|
||||||
import static android.widget.LinearLayout.HORIZONTAL;
|
|
||||||
import static android.widget.Toast.LENGTH_LONG;
|
|
||||||
import static java.util.logging.Level.WARNING;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
|
||||||
@ParametersNotNullByDefault
|
|
||||||
public class AddNearbyContactFragment extends BaseFragment
|
|
||||||
implements QrCodeView.FullscreenListener {
|
|
||||||
|
|
||||||
public static final String TAG = AddNearbyContactFragment.class.getName();
|
|
||||||
|
|
||||||
private static final Logger LOG = Logger.getLogger(TAG);
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
ViewModelProvider.Factory viewModelFactory;
|
|
||||||
|
|
||||||
private AddNearbyContactViewModel viewModel;
|
|
||||||
private CameraView cameraView;
|
|
||||||
private LinearLayout cameraOverlay;
|
|
||||||
private View statusView;
|
|
||||||
private QrCodeView qrCodeView;
|
|
||||||
private TextView status;
|
|
||||||
|
|
||||||
public static AddNearbyContactFragment newInstance() {
|
|
||||||
Bundle args = new Bundle();
|
|
||||||
AddNearbyContactFragment fragment = new AddNearbyContactFragment();
|
|
||||||
fragment.setArguments(args);
|
|
||||||
return fragment;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void injectFragment(ActivityComponent component) {
|
|
||||||
component.inject(this);
|
|
||||||
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory)
|
|
||||||
.get(AddNearbyContactViewModel.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public View onCreateView(LayoutInflater inflater,
|
|
||||||
@Nullable ViewGroup container,
|
|
||||||
@Nullable Bundle savedInstanceState) {
|
|
||||||
return inflater.inflate(R.layout.fragment_keyagreement_qr, container,
|
|
||||||
false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
|
||||||
super.onViewCreated(view, savedInstanceState);
|
|
||||||
cameraView = view.findViewById(R.id.camera_view);
|
|
||||||
cameraOverlay = view.findViewById(R.id.camera_overlay);
|
|
||||||
statusView = view.findViewById(R.id.status_container);
|
|
||||||
status = view.findViewById(R.id.connect_status);
|
|
||||||
qrCodeView = view.findViewById(R.id.qr_code_view);
|
|
||||||
qrCodeView.setFullscreenListener(this);
|
|
||||||
|
|
||||||
viewModel.getState().observe(getViewLifecycleOwner(),
|
|
||||||
this::onAddContactStateChanged);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
||||||
super.onActivityCreated(savedInstanceState);
|
|
||||||
requireActivity().setRequestedOrientation(SCREEN_ORIENTATION_NOSENSOR);
|
|
||||||
cameraView.setPreviewConsumer(viewModel.getQrCodeDecoder());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStart() {
|
|
||||||
super.onStart();
|
|
||||||
try {
|
|
||||||
cameraView.start();
|
|
||||||
} catch (CameraException e) {
|
|
||||||
logCameraExceptionAndFinish(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStop() {
|
|
||||||
super.onStop();
|
|
||||||
try {
|
|
||||||
cameraView.stop();
|
|
||||||
} catch (CameraException e) {
|
|
||||||
logCameraExceptionAndFinish(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
requireActivity()
|
|
||||||
.setRequestedOrientation(SCREEN_ORIENTATION_UNSPECIFIED);
|
|
||||||
super.onDestroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setFullscreen(boolean fullscreen) {
|
|
||||||
LinearLayout.LayoutParams statusParams, qrCodeParams;
|
|
||||||
if (fullscreen) {
|
|
||||||
// Grow the QR code view to fill its parent
|
|
||||||
statusParams = new LayoutParams(0, 0, 0f);
|
|
||||||
qrCodeParams = new LayoutParams(MATCH_PARENT, MATCH_PARENT, 1f);
|
|
||||||
} else {
|
|
||||||
// Shrink the QR code view to fill half its parent
|
|
||||||
if (cameraOverlay.getOrientation() == HORIZONTAL) {
|
|
||||||
statusParams = new LayoutParams(0, MATCH_PARENT, 1f);
|
|
||||||
qrCodeParams = new LayoutParams(0, MATCH_PARENT, 1f);
|
|
||||||
} else {
|
|
||||||
statusParams = new LayoutParams(MATCH_PARENT, 0, 1f);
|
|
||||||
qrCodeParams = new LayoutParams(MATCH_PARENT, 0, 1f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
statusView.setLayoutParams(statusParams);
|
|
||||||
qrCodeView.setLayoutParams(qrCodeParams);
|
|
||||||
cameraOverlay.invalidate();
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
private void onAddContactStateChanged(@Nullable AddContactState state) {
|
|
||||||
if (state instanceof AddContactState.KeyAgreementListening) {
|
|
||||||
Bitmap qrCode =
|
|
||||||
((AddContactState.KeyAgreementListening) state).qrCode;
|
|
||||||
qrCodeView.setQrCode(qrCode);
|
|
||||||
} else if (state instanceof QrCodeScanned) {
|
|
||||||
try {
|
|
||||||
cameraView.stop();
|
|
||||||
} catch (CameraException e) {
|
|
||||||
logCameraExceptionAndFinish(e);
|
|
||||||
}
|
|
||||||
cameraView.setVisibility(INVISIBLE);
|
|
||||||
statusView.setVisibility(VISIBLE);
|
|
||||||
status.setText(R.string.connecting_to_device);
|
|
||||||
} else if (state instanceof KeyAgreementWaiting) {
|
|
||||||
status.setText(R.string.waiting_for_contact_to_scan);
|
|
||||||
} else if (state instanceof KeyAgreementStarted) {
|
|
||||||
qrCodeView.setVisibility(INVISIBLE);
|
|
||||||
status.setText(R.string.authenticating_with_device);
|
|
||||||
} else if (state instanceof ContactExchangeStarted) {
|
|
||||||
status.setText(R.string.exchanging_contact_details);
|
|
||||||
} else if (state instanceof Failed) {
|
|
||||||
// the activity will replace this fragment with an error fragment
|
|
||||||
statusView.setVisibility(INVISIBLE);
|
|
||||||
cameraView.setVisibility(INVISIBLE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getUniqueTag() {
|
|
||||||
return TAG;
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
private void logCameraExceptionAndFinish(CameraException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
Toast.makeText(getActivity(), R.string.camera_error,
|
|
||||||
LENGTH_LONG).show();
|
|
||||||
finish();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void finish() {
|
|
||||||
requireActivity().getSupportFragmentManager().popBackStack();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
package org.briarproject.briar.android.contact.add.nearby;
|
|
||||||
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.ScrollView;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
|
||||||
import org.briarproject.briar.R;
|
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import androidx.activity.result.ActivityResultLauncher;
|
|
||||||
import androidx.activity.result.contract.ActivityResultContracts.RequestMultiplePermissions;
|
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
|
||||||
|
|
||||||
import static android.view.View.FOCUS_DOWN;
|
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
|
||||||
@ParametersNotNullByDefault
|
|
||||||
public class AddNearbyContactIntroFragment extends BaseFragment {
|
|
||||||
|
|
||||||
public static final String TAG =
|
|
||||||
AddNearbyContactIntroFragment.class.getName();
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
ViewModelProvider.Factory viewModelFactory;
|
|
||||||
|
|
||||||
private AddNearbyContactViewModel viewModel;
|
|
||||||
private AddNearbyContactPermissionManager permissionManager;
|
|
||||||
|
|
||||||
private ScrollView scrollView;
|
|
||||||
|
|
||||||
private final ActivityResultLauncher<String[]> permissionLauncher =
|
|
||||||
registerForActivityResult(new RequestMultiplePermissions(), r ->
|
|
||||||
permissionManager.onRequestPermissionResult(r,
|
|
||||||
viewModel::showQrCodeFragmentIfAllowed));
|
|
||||||
|
|
||||||
public static AddNearbyContactIntroFragment newInstance() {
|
|
||||||
Bundle args = new Bundle();
|
|
||||||
AddNearbyContactIntroFragment
|
|
||||||
fragment = new AddNearbyContactIntroFragment();
|
|
||||||
fragment.setArguments(args);
|
|
||||||
return fragment;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void injectFragment(ActivityComponent component) {
|
|
||||||
component.inject(this);
|
|
||||||
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory)
|
|
||||||
.get(AddNearbyContactViewModel.class);
|
|
||||||
permissionManager = new AddNearbyContactPermissionManager(
|
|
||||||
requireActivity(), permissionLauncher::launch,
|
|
||||||
viewModel.isBluetoothSupported());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public View onCreateView(LayoutInflater inflater,
|
|
||||||
@Nullable ViewGroup container,
|
|
||||||
@Nullable Bundle savedInstanceState) {
|
|
||||||
|
|
||||||
View v = inflater.inflate(R.layout.fragment_keyagreement_id, container,
|
|
||||||
false);
|
|
||||||
scrollView = v.findViewById(R.id.scrollView);
|
|
||||||
View button = v.findViewById(R.id.continueButton);
|
|
||||||
button.setOnClickListener(view -> viewModel.onContinueClicked(() ->
|
|
||||||
permissionManager.checkPermissions()
|
|
||||||
));
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStart() {
|
|
||||||
super.onStart();
|
|
||||||
// Permissions may have been granted manually while we were stopped
|
|
||||||
permissionManager.resetPermissions();
|
|
||||||
scrollView.post(() -> scrollView.fullScroll(FOCUS_DOWN));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getUniqueTag() {
|
|
||||||
return TAG;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,209 +0,0 @@
|
|||||||
package org.briarproject.briar.android.contact.add.nearby;
|
|
||||||
|
|
||||||
import android.content.ActivityNotFoundException;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.location.LocationManager;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import org.briarproject.briar.R;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import androidx.annotation.StringRes;
|
|
||||||
import androidx.appcompat.app.AlertDialog;
|
|
||||||
import androidx.core.util.Consumer;
|
|
||||||
import androidx.fragment.app.FragmentActivity;
|
|
||||||
|
|
||||||
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
|
|
||||||
import static android.Manifest.permission.CAMERA;
|
|
||||||
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
|
|
||||||
import static android.os.Build.VERSION.SDK_INT;
|
|
||||||
import static android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS;
|
|
||||||
import static android.widget.Toast.LENGTH_LONG;
|
|
||||||
import static androidx.core.app.ActivityCompat.shouldShowRequestPermissionRationale;
|
|
||||||
import static androidx.core.content.ContextCompat.checkSelfPermission;
|
|
||||||
import static org.briarproject.briar.android.util.UiUtils.getGoToSettingsListener;
|
|
||||||
|
|
||||||
public class AddNearbyContactPermissionManager {
|
|
||||||
|
|
||||||
private enum Permission {
|
|
||||||
UNKNOWN, GRANTED, SHOW_RATIONALE, PERMANENTLY_DENIED
|
|
||||||
}
|
|
||||||
|
|
||||||
private Permission cameraPermission = Permission.UNKNOWN;
|
|
||||||
private Permission locationPermission = Permission.UNKNOWN;
|
|
||||||
|
|
||||||
private final FragmentActivity ctx;
|
|
||||||
private final Consumer<String[]> requestPermissions;
|
|
||||||
private final boolean isBluetoothSupported;
|
|
||||||
|
|
||||||
public AddNearbyContactPermissionManager(FragmentActivity ctx,
|
|
||||||
Consumer<String[]> requestPermissions,
|
|
||||||
boolean isBluetoothSupported) {
|
|
||||||
this.ctx = ctx;
|
|
||||||
this.requestPermissions = requestPermissions;
|
|
||||||
this.isBluetoothSupported = isBluetoothSupported;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void resetPermissions() {
|
|
||||||
cameraPermission = Permission.UNKNOWN;
|
|
||||||
locationPermission = Permission.UNKNOWN;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return true if location is enabled,
|
|
||||||
* or it isn't required due to this being a SDK < 28 device.
|
|
||||||
*/
|
|
||||||
static boolean isLocationEnabled(Context ctx) {
|
|
||||||
if (SDK_INT >= 28) {
|
|
||||||
LocationManager lm = ctx.getSystemService(LocationManager.class);
|
|
||||||
return lm.isLocationEnabled();
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean areEssentialPermissionsGranted(Context ctx,
|
|
||||||
boolean isBluetoothSupported) {
|
|
||||||
int ok = PERMISSION_GRANTED;
|
|
||||||
return checkSelfPermission(ctx, CAMERA) == ok &&
|
|
||||||
(SDK_INT < 23 ||
|
|
||||||
checkSelfPermission(ctx, ACCESS_FINE_LOCATION) == ok ||
|
|
||||||
!isBluetoothSupported);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean areEssentialPermissionsGranted() {
|
|
||||||
return cameraPermission == Permission.GRANTED &&
|
|
||||||
(SDK_INT < 23 || locationPermission == Permission.GRANTED ||
|
|
||||||
!isBluetoothSupported);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean checkPermissions() {
|
|
||||||
boolean locationEnabled = isLocationEnabled(ctx);
|
|
||||||
if (locationEnabled && areEssentialPermissionsGranted()) return true;
|
|
||||||
// If an essential permission has been permanently denied, ask the
|
|
||||||
// user to change the setting
|
|
||||||
if (cameraPermission == Permission.PERMANENTLY_DENIED) {
|
|
||||||
showDenialDialog(R.string.permission_camera_title,
|
|
||||||
R.string.permission_camera_denied_body);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (isBluetoothSupported &&
|
|
||||||
locationPermission == Permission.PERMANENTLY_DENIED) {
|
|
||||||
showDenialDialog(R.string.permission_location_title,
|
|
||||||
R.string.permission_location_denied_body);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Should we show the rationale for one or both permissions?
|
|
||||||
if (cameraPermission == Permission.SHOW_RATIONALE &&
|
|
||||||
locationPermission == Permission.SHOW_RATIONALE) {
|
|
||||||
showRationale(R.string.permission_camera_location_title,
|
|
||||||
R.string.permission_camera_location_request_body);
|
|
||||||
} else if (cameraPermission == Permission.SHOW_RATIONALE) {
|
|
||||||
showRationale(R.string.permission_camera_title,
|
|
||||||
R.string.permission_camera_request_body);
|
|
||||||
} else if (locationPermission == Permission.SHOW_RATIONALE) {
|
|
||||||
showRationale(R.string.permission_location_title,
|
|
||||||
R.string.permission_location_request_body);
|
|
||||||
} else if (isLocationEnabled(ctx)) {
|
|
||||||
requestPermissions();
|
|
||||||
} else {
|
|
||||||
showLocationDialog(ctx);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showDenialDialog(@StringRes int title, @StringRes int body) {
|
|
||||||
AlertDialog.Builder builder =
|
|
||||||
new AlertDialog.Builder(ctx, R.style.BriarDialogTheme);
|
|
||||||
builder.setTitle(title);
|
|
||||||
builder.setMessage(body);
|
|
||||||
builder.setPositiveButton(R.string.ok, getGoToSettingsListener(ctx));
|
|
||||||
builder.setNegativeButton(R.string.cancel,
|
|
||||||
(dialog, which) -> ctx.supportFinishAfterTransition());
|
|
||||||
builder.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showRationale(@StringRes int title, @StringRes int body) {
|
|
||||||
AlertDialog.Builder builder =
|
|
||||||
new AlertDialog.Builder(ctx, R.style.BriarDialogTheme);
|
|
||||||
builder.setTitle(title);
|
|
||||||
builder.setMessage(body);
|
|
||||||
builder.setNeutralButton(R.string.continue_button,
|
|
||||||
(dialog, which) -> requestPermissions());
|
|
||||||
builder.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void showLocationDialog(Context ctx) {
|
|
||||||
AlertDialog.Builder builder =
|
|
||||||
new AlertDialog.Builder(ctx, R.style.BriarDialogTheme);
|
|
||||||
builder.setTitle(R.string.permission_location_setting_title);
|
|
||||||
builder.setMessage(R.string.permission_location_setting_body);
|
|
||||||
builder.setNegativeButton(R.string.cancel, null);
|
|
||||||
builder.setPositiveButton(R.string.permission_location_setting_button,
|
|
||||||
(dialog, which) -> {
|
|
||||||
Intent i = new Intent(ACTION_LOCATION_SOURCE_SETTINGS);
|
|
||||||
try {
|
|
||||||
ctx.startActivity(i);
|
|
||||||
} catch (ActivityNotFoundException e) {
|
|
||||||
Toast.makeText(ctx, R.string.error_start_activity,
|
|
||||||
LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
builder.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void requestPermissions() {
|
|
||||||
String[] permissions;
|
|
||||||
if (isBluetoothSupported) {
|
|
||||||
permissions = new String[] {CAMERA, ACCESS_FINE_LOCATION};
|
|
||||||
} else {
|
|
||||||
permissions = new String[] {CAMERA};
|
|
||||||
}
|
|
||||||
requestPermissions.accept(permissions);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onRequestPermissionResult(Map<String, Boolean> result,
|
|
||||||
Runnable onPermissionsGranted) {
|
|
||||||
if (gotPermission(CAMERA, result)) {
|
|
||||||
cameraPermission = Permission.GRANTED;
|
|
||||||
} else if (shouldShowRationale(CAMERA)) {
|
|
||||||
cameraPermission = Permission.SHOW_RATIONALE;
|
|
||||||
} else {
|
|
||||||
cameraPermission = Permission.PERMANENTLY_DENIED;
|
|
||||||
}
|
|
||||||
if (isBluetoothSupported) {
|
|
||||||
if (gotPermission(ACCESS_FINE_LOCATION, result)) {
|
|
||||||
locationPermission = Permission.GRANTED;
|
|
||||||
} else if (shouldShowRationale(ACCESS_FINE_LOCATION)) {
|
|
||||||
locationPermission = Permission.SHOW_RATIONALE;
|
|
||||||
} else {
|
|
||||||
locationPermission = Permission.PERMANENTLY_DENIED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// If a permission dialog has been shown, showing the QR code fragment
|
|
||||||
// on this call path would cause a crash due to
|
|
||||||
// https://code.google.com/p/android/issues/detail?id=190966.
|
|
||||||
// In that case the isResumed flag prevents the fragment from being
|
|
||||||
// shown here, and showQrCodeFragmentIfAllowed() will be called again
|
|
||||||
// from onPostResume().
|
|
||||||
if (checkPermissions()) onPermissionsGranted.run();
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean gotPermission(String permission,
|
|
||||||
Map<String, Boolean> result) {
|
|
||||||
Boolean permissionResult = result.get(permission);
|
|
||||||
return permissionResult == null ?
|
|
||||||
isGranted(permission) : permissionResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isGranted(String permission) {
|
|
||||||
return checkSelfPermission(ctx, permission) == PERMISSION_GRANTED;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean shouldShowRationale(String permission) {
|
|
||||||
return shouldShowRequestPermissionRationale(ctx, permission);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,532 +0,0 @@
|
|||||||
package org.briarproject.briar.android.contact.add.nearby;
|
|
||||||
|
|
||||||
import android.app.Application;
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
|
||||||
import android.content.BroadcastReceiver;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.IntentFilter;
|
|
||||||
import android.graphics.Bitmap;
|
|
||||||
import android.util.DisplayMetrics;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import com.google.zxing.Result;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.UnsupportedVersionException;
|
|
||||||
import org.briarproject.bramble.api.connection.ConnectionManager;
|
|
||||||
import org.briarproject.bramble.api.contact.Contact;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactExchangeManager;
|
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
|
||||||
import org.briarproject.bramble.api.db.ContactExistsException;
|
|
||||||
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.keyagreement.KeyAgreementResult;
|
|
||||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementTask;
|
|
||||||
import org.briarproject.bramble.api.keyagreement.Payload;
|
|
||||||
import org.briarproject.bramble.api.keyagreement.PayloadEncoder;
|
|
||||||
import org.briarproject.bramble.api.keyagreement.PayloadParser;
|
|
||||||
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementAbortedEvent;
|
|
||||||
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementFailedEvent;
|
|
||||||
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementFinishedEvent;
|
|
||||||
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementListeningEvent;
|
|
||||||
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementStartedEvent;
|
|
||||||
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementWaitingEvent;
|
|
||||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
import org.briarproject.bramble.api.plugin.BluetoothConstants;
|
|
||||||
import org.briarproject.bramble.api.plugin.LanTcpConstants;
|
|
||||||
import org.briarproject.bramble.api.plugin.Plugin;
|
|
||||||
import org.briarproject.bramble.api.plugin.Plugin.State;
|
|
||||||
import org.briarproject.bramble.api.plugin.PluginManager;
|
|
||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
|
||||||
import org.briarproject.bramble.api.plugin.event.TransportStateEvent;
|
|
||||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
|
||||||
import org.briarproject.briar.R;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.ContactExchangeFinished;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.ContactExchangeResult.Error;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.ContactExchangeResult.Success;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.ContactExchangeStarted;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.KeyAgreementListening;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.KeyAgreementStarted;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.AddContactState.KeyAgreementWaiting;
|
|
||||||
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
|
||||||
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Provider;
|
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.annotation.UiThread;
|
|
||||||
import androidx.core.util.Supplier;
|
|
||||||
import androidx.lifecycle.AndroidViewModel;
|
|
||||||
import androidx.lifecycle.LiveData;
|
|
||||||
import androidx.lifecycle.MutableLiveData;
|
|
||||||
|
|
||||||
import static android.bluetooth.BluetoothAdapter.ACTION_SCAN_MODE_CHANGED;
|
|
||||||
import static android.bluetooth.BluetoothAdapter.EXTRA_SCAN_MODE;
|
|
||||||
import static android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
|
|
||||||
import static android.widget.Toast.LENGTH_LONG;
|
|
||||||
import static java.util.Objects.requireNonNull;
|
|
||||||
import static java.util.logging.Level.INFO;
|
|
||||||
import static java.util.logging.Level.WARNING;
|
|
||||||
import static java.util.logging.Logger.getLogger;
|
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
|
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
|
||||||
import static org.briarproject.briar.android.contact.add.nearby.AddNearbyContactPermissionManager.areEssentialPermissionsGranted;
|
|
||||||
import static org.briarproject.briar.android.contact.add.nearby.AddNearbyContactPermissionManager.isLocationEnabled;
|
|
||||||
import static org.briarproject.briar.android.contact.add.nearby.AddNearbyContactViewModel.BluetoothDecision.NO_ADAPTER;
|
|
||||||
import static org.briarproject.briar.android.contact.add.nearby.AddNearbyContactViewModel.BluetoothDecision.REFUSED;
|
|
||||||
import static org.briarproject.briar.android.contact.add.nearby.AddNearbyContactViewModel.BluetoothDecision.UNKNOWN;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
class AddNearbyContactViewModel extends AndroidViewModel
|
|
||||||
implements EventListener, QrCodeDecoder.ResultCallback {
|
|
||||||
|
|
||||||
private static final Logger LOG =
|
|
||||||
getLogger(AddNearbyContactViewModel.class.getName());
|
|
||||||
|
|
||||||
enum BluetoothDecision {
|
|
||||||
/**
|
|
||||||
* We haven't asked the user about Bluetooth discoverability.
|
|
||||||
*/
|
|
||||||
UNKNOWN,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The device doesn't have a Bluetooth adapter.
|
|
||||||
*/
|
|
||||||
NO_ADAPTER,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* We're waiting for the user to accept or refuse discoverability.
|
|
||||||
*/
|
|
||||||
WAITING,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The user has accepted discoverability.
|
|
||||||
*/
|
|
||||||
ACCEPTED,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The user has refused discoverability.
|
|
||||||
*/
|
|
||||||
REFUSED
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("CharsetObjectCanBeUsed") // Requires minSdkVersion >= 19
|
|
||||||
private static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
|
|
||||||
|
|
||||||
private final EventBus eventBus;
|
|
||||||
private final AndroidExecutor androidExecutor;
|
|
||||||
private final Executor ioExecutor;
|
|
||||||
private final PluginManager pluginManager;
|
|
||||||
private final PayloadEncoder payloadEncoder;
|
|
||||||
private final PayloadParser payloadParser;
|
|
||||||
private final Provider<KeyAgreementTask> keyAgreementTaskProvider;
|
|
||||||
private final ContactExchangeManager contactExchangeManager;
|
|
||||||
private final ConnectionManager connectionManager;
|
|
||||||
|
|
||||||
private final MutableLiveEvent<Boolean> requestBluetoothDiscoverable =
|
|
||||||
new MutableLiveEvent<>();
|
|
||||||
private final MutableLiveEvent<Boolean> showQrCodeFragment =
|
|
||||||
new MutableLiveEvent<>();
|
|
||||||
private final MutableLiveData<AddContactState> state =
|
|
||||||
new MutableLiveData<>();
|
|
||||||
|
|
||||||
private final QrCodeDecoder qrCodeDecoder;
|
|
||||||
private final BroadcastReceiver bluetoothReceiver =
|
|
||||||
new BluetoothStateReceiver();
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private final BluetoothAdapter bt;
|
|
||||||
@Nullable
|
|
||||||
private final Plugin wifiPlugin, bluetoothPlugin;
|
|
||||||
|
|
||||||
// UiThread
|
|
||||||
private BluetoothDecision bluetoothDecision = BluetoothDecision.UNKNOWN;
|
|
||||||
|
|
||||||
private boolean wasContinueClicked = false;
|
|
||||||
private boolean isActivityResumed = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Records whether we've enabled the wifi plugin so we don't enable it more
|
|
||||||
* than once.
|
|
||||||
*/
|
|
||||||
private boolean hasEnabledWifi = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Records whether we've enabled the Bluetooth plugin so we don't enable it
|
|
||||||
* more than once.
|
|
||||||
*/
|
|
||||||
private boolean hasEnabledBluetooth = false;
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private volatile KeyAgreementTask task;
|
|
||||||
private volatile boolean gotLocalPayload = false, gotRemotePayload = false;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
AddNearbyContactViewModel(Application app,
|
|
||||||
EventBus eventBus,
|
|
||||||
AndroidExecutor androidExecutor,
|
|
||||||
@IoExecutor Executor ioExecutor,
|
|
||||||
PluginManager pluginManager,
|
|
||||||
PayloadEncoder payloadEncoder,
|
|
||||||
PayloadParser payloadParser,
|
|
||||||
Provider<KeyAgreementTask> keyAgreementTaskProvider,
|
|
||||||
ContactExchangeManager contactExchangeManager,
|
|
||||||
ConnectionManager connectionManager) {
|
|
||||||
super(app);
|
|
||||||
this.eventBus = eventBus;
|
|
||||||
this.androidExecutor = androidExecutor;
|
|
||||||
this.ioExecutor = ioExecutor;
|
|
||||||
this.pluginManager = pluginManager;
|
|
||||||
this.payloadEncoder = payloadEncoder;
|
|
||||||
this.payloadParser = payloadParser;
|
|
||||||
this.keyAgreementTaskProvider = keyAgreementTaskProvider;
|
|
||||||
this.contactExchangeManager = contactExchangeManager;
|
|
||||||
this.connectionManager = connectionManager;
|
|
||||||
bt = BluetoothAdapter.getDefaultAdapter();
|
|
||||||
wifiPlugin = pluginManager.getPlugin(LanTcpConstants.ID);
|
|
||||||
bluetoothPlugin = pluginManager.getPlugin(BluetoothConstants.ID);
|
|
||||||
qrCodeDecoder = new QrCodeDecoder(androidExecutor, ioExecutor, this);
|
|
||||||
eventBus.addListener(this);
|
|
||||||
IntentFilter filter = new IntentFilter(ACTION_SCAN_MODE_CHANGED);
|
|
||||||
getApplication().registerReceiver(bluetoothReceiver, filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCleared() {
|
|
||||||
super.onCleared();
|
|
||||||
getApplication().unregisterReceiver(bluetoothReceiver);
|
|
||||||
eventBus.removeListener(this);
|
|
||||||
stopListening();
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
void onContinueClicked(Supplier<Boolean> checkPermissions) {
|
|
||||||
if (bluetoothDecision == REFUSED) {
|
|
||||||
bluetoothDecision = UNKNOWN; // Ask again
|
|
||||||
}
|
|
||||||
wasContinueClicked = true;
|
|
||||||
if (checkPermissions.get()) showQrCodeFragmentIfAllowed();
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
boolean isBluetoothSupported() {
|
|
||||||
return bt != null && bluetoothPlugin != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
private boolean isWifiReady() {
|
|
||||||
if (wifiPlugin == null) return true; // Continue without wifi
|
|
||||||
State state = wifiPlugin.getState();
|
|
||||||
// Wait for plugin to become enabled
|
|
||||||
return state == ACTIVE || state == INACTIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
private boolean isBluetoothReady() {
|
|
||||||
if (bt == null || bluetoothPlugin == null) {
|
|
||||||
// Continue without Bluetooth
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (bluetoothDecision == BluetoothDecision.UNKNOWN ||
|
|
||||||
bluetoothDecision == BluetoothDecision.WAITING ||
|
|
||||||
bluetoothDecision == BluetoothDecision.REFUSED) {
|
|
||||||
// Wait for user to accept
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (bt.getScanMode() != SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
|
|
||||||
// Wait for adapter to become discoverable
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Wait for plugin to become active
|
|
||||||
return bluetoothPlugin.getState() == ACTIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
private void enableWifiIfWeShould() {
|
|
||||||
if (hasEnabledWifi) return;
|
|
||||||
if (wifiPlugin == null) return;
|
|
||||||
State state = wifiPlugin.getState();
|
|
||||||
if (state == STARTING_STOPPING || state == DISABLED) {
|
|
||||||
LOG.info("Enabling wifi plugin");
|
|
||||||
hasEnabledWifi = true;
|
|
||||||
pluginManager.setPluginEnabled(LanTcpConstants.ID, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
private void enableBluetoothIfWeShould() {
|
|
||||||
if (bluetoothDecision != BluetoothDecision.ACCEPTED) return;
|
|
||||||
if (hasEnabledBluetooth) return;
|
|
||||||
if (bluetoothPlugin == null || !isBluetoothSupported()) return;
|
|
||||||
State state = bluetoothPlugin.getState();
|
|
||||||
if (state == STARTING_STOPPING || state == DISABLED) {
|
|
||||||
LOG.info("Enabling Bluetooth plugin");
|
|
||||||
hasEnabledBluetooth = true;
|
|
||||||
pluginManager.setPluginEnabled(BluetoothConstants.ID, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
private void startAddingContact() {
|
|
||||||
// If we return to the intro fragment, the continue button needs to be
|
|
||||||
// clicked again before showing the QR code fragment
|
|
||||||
wasContinueClicked = false;
|
|
||||||
// If we return to the intro fragment, ask for Bluetooth
|
|
||||||
// discoverability again before showing the QR code fragment
|
|
||||||
bluetoothDecision = UNKNOWN;
|
|
||||||
// If we return to the intro fragment, we may need to enable wifi and
|
|
||||||
// Bluetooth again
|
|
||||||
hasEnabledWifi = false;
|
|
||||||
hasEnabledBluetooth = false;
|
|
||||||
// reset state, so we don't show an old QR code again
|
|
||||||
state.setValue(null);
|
|
||||||
// start to listen with a KeyAgreementTask
|
|
||||||
startListening();
|
|
||||||
showQrCodeFragment.setEvent(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Call this once Bluetooth and Wi-Fi are ready to be used.
|
|
||||||
* It is possible to call this more than once over the ViewModel's lifetime.
|
|
||||||
*/
|
|
||||||
@UiThread
|
|
||||||
private void startListening() {
|
|
||||||
KeyAgreementTask oldTask = task;
|
|
||||||
KeyAgreementTask newTask = keyAgreementTaskProvider.get();
|
|
||||||
task = newTask;
|
|
||||||
ioExecutor.execute(() -> {
|
|
||||||
if (oldTask != null) oldTask.stopListening();
|
|
||||||
newTask.listen();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
private void stopListening() {
|
|
||||||
KeyAgreementTask oldTask = task;
|
|
||||||
ioExecutor.execute(() -> {
|
|
||||||
if (oldTask != null) oldTask.stopListening();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void eventOccurred(Event e) {
|
|
||||||
if (e instanceof TransportStateEvent) {
|
|
||||||
TransportStateEvent t = (TransportStateEvent) e;
|
|
||||||
if (t.getTransportId().equals(BluetoothConstants.ID)) {
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Bluetooth state changed to " + t.getState());
|
|
||||||
}
|
|
||||||
showQrCodeFragmentIfAllowed();
|
|
||||||
} else if (t.getTransportId().equals(LanTcpConstants.ID)) {
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Wifi state changed to " + t.getState());
|
|
||||||
}
|
|
||||||
showQrCodeFragmentIfAllowed();
|
|
||||||
}
|
|
||||||
} else if (e instanceof KeyAgreementListeningEvent) {
|
|
||||||
LOG.info("KeyAgreementListeningEvent received");
|
|
||||||
KeyAgreementListeningEvent event = (KeyAgreementListeningEvent) e;
|
|
||||||
onLocalPayloadReceived(event.getLocalPayload());
|
|
||||||
} else if (e instanceof KeyAgreementWaitingEvent) {
|
|
||||||
LOG.info("KeyAgreementWaitingEvent received");
|
|
||||||
state.setValue(new KeyAgreementWaiting());
|
|
||||||
} else if (e instanceof KeyAgreementStartedEvent) {
|
|
||||||
LOG.info("KeyAgreementStartedEvent received");
|
|
||||||
state.setValue(new KeyAgreementStarted());
|
|
||||||
} else if (e instanceof KeyAgreementFinishedEvent) {
|
|
||||||
LOG.info("KeyAgreementFinishedEvent received");
|
|
||||||
KeyAgreementResult result =
|
|
||||||
((KeyAgreementFinishedEvent) e).getResult();
|
|
||||||
startContactExchange(result);
|
|
||||||
state.setValue(new ContactExchangeStarted());
|
|
||||||
} else if (e instanceof KeyAgreementAbortedEvent) {
|
|
||||||
LOG.info("KeyAgreementAbortedEvent received");
|
|
||||||
resetPayloadFlags();
|
|
||||||
state.setValue(new AddContactState.Failed());
|
|
||||||
} else if (e instanceof KeyAgreementFailedEvent) {
|
|
||||||
LOG.info("KeyAgreementFailedEvent received");
|
|
||||||
resetPayloadFlags();
|
|
||||||
state.setValue(new AddContactState.Failed());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("StatementWithEmptyBody")
|
|
||||||
@UiThread
|
|
||||||
void showQrCodeFragmentIfAllowed() {
|
|
||||||
boolean permissionsGranted = areEssentialPermissionsGranted(
|
|
||||||
getApplication(), isBluetoothSupported());
|
|
||||||
boolean locationEnabled = isLocationEnabled(getApplication());
|
|
||||||
if (isActivityResumed && wasContinueClicked && permissionsGranted &&
|
|
||||||
locationEnabled) {
|
|
||||||
if (isWifiReady() && isBluetoothReady()) {
|
|
||||||
LOG.info("Wifi and Bluetooth are ready");
|
|
||||||
startAddingContact();
|
|
||||||
} else {
|
|
||||||
enableWifiIfWeShould();
|
|
||||||
if (bluetoothDecision == UNKNOWN) {
|
|
||||||
if (isBluetoothSupported()) {
|
|
||||||
requestBluetoothDiscoverable.setEvent(true);
|
|
||||||
} else {
|
|
||||||
bluetoothDecision = NO_ADAPTER;
|
|
||||||
}
|
|
||||||
} else if (bluetoothDecision == REFUSED) {
|
|
||||||
// Ask again when the user clicks "continue"
|
|
||||||
} else {
|
|
||||||
enableBluetoothIfWeShould();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This sets the QR code by setting the state to KeyAgreementListening.
|
|
||||||
*/
|
|
||||||
private void onLocalPayloadReceived(Payload localPayload) {
|
|
||||||
if (gotLocalPayload) return;
|
|
||||||
DisplayMetrics dm = getApplication().getResources().getDisplayMetrics();
|
|
||||||
ioExecutor.execute(() -> {
|
|
||||||
byte[] payloadBytes = payloadEncoder.encode(localPayload);
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Local payload is " + payloadBytes.length
|
|
||||||
+ " bytes");
|
|
||||||
}
|
|
||||||
// Use ISO 8859-1 to encode bytes directly as a string
|
|
||||||
String content = new String(payloadBytes, ISO_8859_1);
|
|
||||||
Bitmap qrCode = QrCodeUtils.createQrCode(dm, content);
|
|
||||||
gotLocalPayload = true;
|
|
||||||
state.postValue(new KeyAgreementListening(qrCode));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@IoExecutor
|
|
||||||
public void onQrCodeDecoded(Result result) {
|
|
||||||
LOG.info("Got result from decoder");
|
|
||||||
// Ignore results until the KeyAgreementTask is ready
|
|
||||||
if (!gotLocalPayload || gotRemotePayload) return;
|
|
||||||
try {
|
|
||||||
byte[] payloadBytes = result.getText().getBytes(ISO_8859_1);
|
|
||||||
if (LOG.isLoggable(INFO))
|
|
||||||
LOG.info("Remote payload is " + payloadBytes.length + " bytes");
|
|
||||||
Payload remotePayload = payloadParser.parse(payloadBytes);
|
|
||||||
gotRemotePayload = true;
|
|
||||||
requireNonNull(task).connectAndRunProtocol(remotePayload);
|
|
||||||
state.postValue(new AddContactState.QrCodeScanned());
|
|
||||||
} catch (UnsupportedVersionException e) {
|
|
||||||
resetPayloadFlags();
|
|
||||||
state.postValue(new AddContactState.Failed(e.isTooOld()));
|
|
||||||
} catch (IOException | IllegalArgumentException e) {
|
|
||||||
LOG.log(WARNING, "QR Code Invalid", e);
|
|
||||||
androidExecutor.runOnUiThread(() -> Toast.makeText(getApplication(),
|
|
||||||
R.string.qr_code_invalid, LENGTH_LONG).show());
|
|
||||||
resetPayloadFlags();
|
|
||||||
state.postValue(new AddContactState.Failed());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void resetPayloadFlags() {
|
|
||||||
gotRemotePayload = false;
|
|
||||||
gotLocalPayload = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
private void startContactExchange(KeyAgreementResult result) {
|
|
||||||
TransportId t = result.getTransportId();
|
|
||||||
DuplexTransportConnection conn = result.getConnection();
|
|
||||||
SecretKey masterKey = result.getMasterKey();
|
|
||||||
boolean alice = result.wasAlice();
|
|
||||||
ioExecutor.execute(() -> {
|
|
||||||
try {
|
|
||||||
Contact contact = contactExchangeManager.exchangeContacts(conn,
|
|
||||||
masterKey, alice, true);
|
|
||||||
// Reuse the connection as a transport connection
|
|
||||||
connectionManager
|
|
||||||
.manageOutgoingConnection(contact.getId(), t, conn);
|
|
||||||
Success success = new Success(contact.getAuthor());
|
|
||||||
state.postValue(new ContactExchangeFinished(success));
|
|
||||||
} catch (ContactExistsException e) {
|
|
||||||
tryToClose(conn);
|
|
||||||
Error error = new Error(e.getRemoteAuthor());
|
|
||||||
state.postValue(new ContactExchangeFinished(error));
|
|
||||||
} catch (DbException | IOException e) {
|
|
||||||
tryToClose(conn);
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
Error error = new Error(null);
|
|
||||||
state.postValue(new ContactExchangeFinished(error));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private class BluetoothStateReceiver extends BroadcastReceiver {
|
|
||||||
@UiThread
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
int scanMode = intent.getIntExtra(EXTRA_SCAN_MODE, -1);
|
|
||||||
LOG.info("Bluetooth scan mode changed: " + scanMode);
|
|
||||||
showQrCodeFragmentIfAllowed();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void tryToClose(DuplexTransportConnection conn) {
|
|
||||||
try {
|
|
||||||
conn.getReader().dispose(true, true);
|
|
||||||
conn.getWriter().dispose(true);
|
|
||||||
} catch (IOException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set to true in onPostResume() and false in onPause(). This prevents the
|
|
||||||
* QR code fragment from being shown if onRequestPermissionsResult() is
|
|
||||||
* called while the activity is paused, which could cause a crash due to
|
|
||||||
* https://issuetracker.google.com/issues/37067655.
|
|
||||||
* TODO check if this is still happening with new permission requesting
|
|
||||||
*/
|
|
||||||
@UiThread
|
|
||||||
void setIsActivityResumed(boolean resumed) {
|
|
||||||
isActivityResumed = resumed;
|
|
||||||
// Workaround for
|
|
||||||
// https://code.google.com/p/android/issues/detail?id=190966
|
|
||||||
showQrCodeFragmentIfAllowed();
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
void setBluetoothDecision(BluetoothDecision decision) {
|
|
||||||
bluetoothDecision = decision;
|
|
||||||
showQrCodeFragmentIfAllowed();
|
|
||||||
}
|
|
||||||
|
|
||||||
QrCodeDecoder getQrCodeDecoder() {
|
|
||||||
return qrCodeDecoder;
|
|
||||||
}
|
|
||||||
|
|
||||||
LiveEvent<Boolean> getRequestBluetoothDiscoverable() {
|
|
||||||
return requestBluetoothDiscoverable;
|
|
||||||
}
|
|
||||||
|
|
||||||
LiveEvent<Boolean> getShowQrCodeFragment() {
|
|
||||||
return showQrCodeFragment;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This LiveData will be null initially.
|
|
||||||
*/
|
|
||||||
LiveData<AddContactState> getState() {
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -6,7 +6,6 @@ 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.android.contact.BaseContactListAdapter;
|
import org.briarproject.briar.android.contact.BaseContactListAdapter;
|
||||||
import org.briarproject.briar.android.contact.ContactItemViewHolder;
|
import org.briarproject.briar.android.contact.ContactItemViewHolder;
|
||||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ 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.GroupId;
|
import org.briarproject.bramble.api.sync.GroupId;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
|
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||||
import org.briarproject.briar.android.contact.ContactItemViewHolder;
|
import org.briarproject.briar.android.contact.ContactItemViewHolder;
|
||||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
|
||||||
import org.briarproject.briar.android.controller.handler.UiResultExceptionHandler;
|
import org.briarproject.briar.android.controller.handler.UiResultExceptionHandler;
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||||
import org.briarproject.briar.android.view.BriarRecyclerView;
|
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import android.widget.TextView;
|
|||||||
|
|
||||||
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.ContactItemViewHolder;
|
import org.briarproject.briar.android.contact.ContactItemViewHolder;
|
||||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import android.view.ViewGroup;
|
|||||||
|
|
||||||
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.OnContactClickListener;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class ContactSelectorAdapter extends
|
class ContactSelectorAdapter extends
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import android.view.MenuItem;
|
|||||||
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;
|
||||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package org.briarproject.briar.android.contactselection;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ import org.briarproject.briar.android.conversation.ConversationVisitor.TextCache
|
|||||||
import org.briarproject.briar.android.forum.ForumActivity;
|
import org.briarproject.briar.android.forum.ForumActivity;
|
||||||
import org.briarproject.briar.android.introduction.IntroductionActivity;
|
import org.briarproject.briar.android.introduction.IntroductionActivity;
|
||||||
import org.briarproject.briar.android.privategroup.conversation.GroupActivity;
|
import org.briarproject.briar.android.privategroup.conversation.GroupActivity;
|
||||||
import org.briarproject.briar.android.socialbackup.recover.CustodianReturnShardActivity;
|
import org.briarproject.briar.android.socialbackup.CustodianHelpRecoverActivity;
|
||||||
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
|
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
|
||||||
import org.briarproject.briar.android.view.BriarRecyclerView;
|
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||||
import org.briarproject.briar.android.view.ImagePreview;
|
import org.briarproject.briar.android.view.ImagePreview;
|
||||||
@@ -399,7 +399,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
return true;
|
return true;
|
||||||
case R.id.action_help_recover_account:
|
case R.id.action_help_recover_account:
|
||||||
if (contactId == null) return false;
|
if (contactId == null) return false;
|
||||||
Intent i = new Intent(this, CustodianReturnShardActivity.class);
|
Intent i = new Intent(this, CustodianHelpRecoverActivity.class);
|
||||||
i.putExtra(CONTACT_ID, contactId.getInt());
|
i.putExtra(CONTACT_ID, contactId.getInt());
|
||||||
startActivity(i);
|
startActivity(i);
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -296,13 +296,11 @@ class ConversationVisitor implements
|
|||||||
@Override
|
@Override
|
||||||
public ConversationItem visitShardMessage(ShardMessageHeader r) {
|
public ConversationItem visitShardMessage(ShardMessageHeader r) {
|
||||||
if (r.isLocal()) {
|
if (r.isLocal()) {
|
||||||
String text = ctx.getString(R.string.social_backup_shard_sent,
|
String text = ctx.getString(R.string.social_backup_shard_sent);
|
||||||
contactName.getValue());
|
|
||||||
return new ConversationNoticeItem(
|
return new ConversationNoticeItem(
|
||||||
R.layout.list_item_conversation_notice_out, text, r);
|
R.layout.list_item_conversation_notice_out, text, r);
|
||||||
} else {
|
} else {
|
||||||
String text = ctx.getString(R.string.social_backup_shard_received,
|
String text = ctx.getString(R.string.social_backup_shard_received);
|
||||||
contactName.getValue());
|
|
||||||
return new ConversationNoticeItem(
|
return new ConversationNoticeItem(
|
||||||
R.layout.list_item_conversation_notice_in, text, r);
|
R.layout.list_item_conversation_notice_in, text, r);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,10 @@ import org.briarproject.briar.android.attachment.AttachmentItem;
|
|||||||
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
|
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
|
||||||
import org.briarproject.briar.android.view.PullDownLayout;
|
import org.briarproject.briar.android.view.PullDownLayout;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
@@ -290,10 +293,13 @@ public class ImageActivity extends BriarActivity
|
|||||||
|
|
||||||
@RequiresApi(api = 19)
|
@RequiresApi(api = 19)
|
||||||
private Intent getCreationIntent() {
|
private Intent getCreationIntent() {
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",
|
||||||
|
Locale.getDefault());
|
||||||
|
String fileName = sdf.format(new Date());
|
||||||
Intent intent = new Intent(ACTION_CREATE_DOCUMENT);
|
Intent intent = new Intent(ACTION_CREATE_DOCUMENT);
|
||||||
intent.addCategory(CATEGORY_OPENABLE);
|
intent.addCategory(CATEGORY_OPENABLE);
|
||||||
intent.setType(getVisibleAttachment().getMimeType());
|
intent.setType(getVisibleAttachment().getMimeType());
|
||||||
intent.putExtra(EXTRA_TITLE, viewModel.getFileName());
|
intent.putExtra(EXTRA_TITLE, fileName);
|
||||||
return intent;
|
return intent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -225,8 +225,8 @@ public class ImageViewModel extends DbViewModel implements EventListener {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
String getFileName() {
|
private String getFileName() {
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",
|
||||||
Locale.getDefault());
|
Locale.getDefault());
|
||||||
return sdf.format(new Date());
|
return sdf.format(new Date());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,9 @@ import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
|||||||
import org.briarproject.briar.android.DestroyableContext;
|
import org.briarproject.briar.android.DestroyableContext;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import androidx.annotation.CallSuper;
|
import androidx.annotation.CallSuper;
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.UiThread;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.fragment.app.FragmentActivity;
|
import androidx.fragment.app.FragmentActivity;
|
||||||
@@ -46,11 +47,13 @@ public abstract class BaseFragment extends Fragment
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
if (item.getItemId() == android.R.id.home) {
|
switch (item.getItemId()) {
|
||||||
requireActivity().onBackPressed();
|
case android.R.id.home:
|
||||||
return true;
|
listener.onBackPressed();
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
return super.onOptionsItemSelected(item);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
@@ -76,7 +79,6 @@ public abstract class BaseFragment extends Fragment
|
|||||||
void handleException(Exception e);
|
void handleException(Exception e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
@CallSuper
|
@CallSuper
|
||||||
@Override
|
@Override
|
||||||
public void runOnUiThreadUnlessDestroyed(Runnable r) {
|
public void runOnUiThreadUnlessDestroyed(Runnable r) {
|
||||||
|
|||||||
@@ -5,41 +5,74 @@ import android.view.LayoutInflater;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||||
|
import org.briarproject.bramble.api.contact.Contact;
|
||||||
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
|
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.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.ContactListAdapter;
|
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||||
import org.briarproject.briar.android.contact.ContactListItem;
|
import org.briarproject.briar.android.contact.ContactListItem;
|
||||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
import org.briarproject.briar.android.contact.LegacyContactListAdapter;
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||||
import org.briarproject.briar.android.view.BriarRecyclerView;
|
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||||
|
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
|
||||||
|
import org.briarproject.briar.api.conversation.ConversationManager;
|
||||||
|
import org.briarproject.briar.api.identity.AuthorInfo;
|
||||||
|
import org.briarproject.briar.api.identity.AuthorManager;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
import androidx.annotation.UiThread;
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
|
|
||||||
|
import static java.util.logging.Level.WARNING;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
import static org.briarproject.briar.android.conversation.ConversationActivity.CONTACT_ID;
|
||||||
|
|
||||||
|
@UiThread
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
public class ContactChooserFragment extends BaseFragment
|
public class ContactChooserFragment extends BaseFragment {
|
||||||
implements OnContactClickListener<ContactListItem> {
|
|
||||||
|
|
||||||
private static final String TAG = ContactChooserFragment.class.getName();
|
public static final String TAG = ContactChooserFragment.class.getName();
|
||||||
|
private static final Logger LOG = Logger.getLogger(TAG);
|
||||||
|
|
||||||
@Inject
|
|
||||||
ViewModelProvider.Factory viewModelFactory;
|
|
||||||
|
|
||||||
private IntroductionViewModel viewModel;
|
|
||||||
private final ContactListAdapter adapter = new ContactListAdapter(this);
|
|
||||||
private BriarRecyclerView list;
|
private BriarRecyclerView list;
|
||||||
|
private LegacyContactListAdapter adapter;
|
||||||
|
private ContactId contactId;
|
||||||
|
|
||||||
|
// Fields that are accessed from background threads must be volatile
|
||||||
|
private volatile Contact c1;
|
||||||
|
@Inject
|
||||||
|
volatile ContactManager contactManager;
|
||||||
|
@Inject
|
||||||
|
volatile AuthorManager authorManager;
|
||||||
|
@Inject
|
||||||
|
volatile ConversationManager conversationManager;
|
||||||
|
@Inject
|
||||||
|
volatile ConnectionRegistry connectionRegistry;
|
||||||
|
|
||||||
|
public static ContactChooserFragment newInstance(ContactId id) {
|
||||||
|
Bundle args = new Bundle();
|
||||||
|
|
||||||
|
ContactChooserFragment fragment = new ContactChooserFragment();
|
||||||
|
args.putInt(CONTACT_ID, id.getInt());
|
||||||
|
fragment.setArguments(args);
|
||||||
|
return fragment;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void injectFragment(ActivityComponent component) {
|
public void injectFragment(ActivityComponent component) {
|
||||||
component.inject(this);
|
component.inject(this);
|
||||||
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory)
|
|
||||||
.get(IntroductionViewModel.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -47,20 +80,23 @@ public class ContactChooserFragment extends BaseFragment
|
|||||||
@Nullable ViewGroup container,
|
@Nullable ViewGroup container,
|
||||||
@Nullable Bundle savedInstanceState) {
|
@Nullable Bundle savedInstanceState) {
|
||||||
|
|
||||||
// change toolbar text (relevant when navigating back to this fragment)
|
|
||||||
requireActivity().setTitle(R.string.introduction_activity_title);
|
|
||||||
|
|
||||||
View contentView = inflater.inflate(R.layout.list, container, false);
|
View contentView = inflater.inflate(R.layout.list, container, false);
|
||||||
|
|
||||||
|
OnContactClickListener<ContactListItem> onContactClickListener =
|
||||||
|
(view, item) -> {
|
||||||
|
if (c1 == null) throw new IllegalStateException();
|
||||||
|
Contact c2 = item.getContact();
|
||||||
|
showMessageScreen(c1, c2);
|
||||||
|
};
|
||||||
|
adapter = new LegacyContactListAdapter(requireActivity(),
|
||||||
|
onContactClickListener);
|
||||||
|
|
||||||
list = contentView.findViewById(R.id.list);
|
list = contentView.findViewById(R.id.list);
|
||||||
list.setLayoutManager(new LinearLayoutManager(getActivity()));
|
list.setLayoutManager(new LinearLayoutManager(getActivity()));
|
||||||
list.setAdapter(adapter);
|
list.setAdapter(adapter);
|
||||||
list.setEmptyText(R.string.no_contacts);
|
list.setEmptyText(R.string.no_contacts);
|
||||||
|
|
||||||
viewModel.getContactListItems().observe(getViewLifecycleOwner(),
|
contactId = new ContactId(requireArguments().getInt(CONTACT_ID));
|
||||||
result -> result.onError(this::handleException)
|
|
||||||
.onSuccess(adapter::submitList)
|
|
||||||
);
|
|
||||||
|
|
||||||
return contentView;
|
return contentView;
|
||||||
}
|
}
|
||||||
@@ -68,13 +104,14 @@ public class ContactChooserFragment extends BaseFragment
|
|||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
list.startPeriodicUpdate();
|
loadContacts();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStop() {
|
public void onStop() {
|
||||||
super.onStop();
|
super.onStop();
|
||||||
list.stopPeriodicUpdate();
|
adapter.clear();
|
||||||
|
list.showProgressBar();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -82,9 +119,43 @@ public class ContactChooserFragment extends BaseFragment
|
|||||||
return TAG;
|
return TAG;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void loadContacts() {
|
||||||
public void onItemClick(View view, ContactListItem item) {
|
listener.runOnDbThread(() -> {
|
||||||
viewModel.setSecondContactId(item.getContact().getId());
|
try {
|
||||||
viewModel.triggerContactSelected();
|
List<ContactListItem> contacts = new ArrayList<>();
|
||||||
|
for (Contact c : contactManager.getContacts()) {
|
||||||
|
if (c.getId().equals(contactId)) {
|
||||||
|
c1 = c;
|
||||||
|
} else {
|
||||||
|
AuthorInfo authorInfo = authorManager.getAuthorInfo(c);
|
||||||
|
ContactId id = c.getId();
|
||||||
|
GroupCount count =
|
||||||
|
conversationManager.getGroupCount(id);
|
||||||
|
boolean connected =
|
||||||
|
connectionRegistry.isConnected(c.getId());
|
||||||
|
contacts.add(new ContactListItem(c, authorInfo,
|
||||||
|
connected, count));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
displayContacts(contacts);
|
||||||
|
} catch (DbException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void displayContacts(List<ContactListItem> contacts) {
|
||||||
|
runOnUiThreadUnlessDestroyed(() -> {
|
||||||
|
if (contacts.isEmpty()) list.showData();
|
||||||
|
else adapter.addAll(contacts);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showMessageScreen(Contact c1, Contact c2) {
|
||||||
|
IntroductionMessageFragment messageFragment =
|
||||||
|
IntroductionMessageFragment
|
||||||
|
.newInstance(c1.getId().getInt(), c2.getId().getInt());
|
||||||
|
showNextFragment(messageFragment);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,67 +9,30 @@ 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.fragment.BaseFragment.BaseFragmentListener;
|
import org.briarproject.briar.android.fragment.BaseFragment.BaseFragmentListener;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
|
||||||
|
|
||||||
import static org.briarproject.briar.android.conversation.ConversationActivity.CONTACT_ID;
|
import static org.briarproject.briar.android.conversation.ConversationActivity.CONTACT_ID;
|
||||||
|
|
||||||
public class IntroductionActivity extends BriarActivity
|
public class IntroductionActivity extends BriarActivity
|
||||||
implements BaseFragmentListener {
|
implements BaseFragmentListener {
|
||||||
|
|
||||||
@Inject
|
|
||||||
ViewModelProvider.Factory viewModelFactory;
|
|
||||||
|
|
||||||
private IntroductionViewModel viewModel;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void injectActivity(ActivityComponent component) {
|
|
||||||
component.inject(this);
|
|
||||||
viewModel = new ViewModelProvider(this, viewModelFactory)
|
|
||||||
.get(IntroductionViewModel.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String BUNDLE_CONTACT2 = "contact2";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
int contactId1 = intent.getIntExtra(CONTACT_ID, -1);
|
int id = intent.getIntExtra(CONTACT_ID, -1);
|
||||||
if (contactId1 == -1)
|
if (id == -1) throw new IllegalStateException("No ContactId");
|
||||||
throw new IllegalStateException("No ContactId");
|
ContactId contactId = new ContactId(id);
|
||||||
ContactId firstContactId = new ContactId(contactId1);
|
|
||||||
|
|
||||||
viewModel.setFirstContactId(firstContactId);
|
|
||||||
|
|
||||||
setContentView(R.layout.activity_fragment_container);
|
setContentView(R.layout.activity_fragment_container);
|
||||||
|
|
||||||
if (savedInstanceState == null) {
|
if (savedInstanceState == null) {
|
||||||
showInitialFragment(new ContactChooserFragment());
|
showInitialFragment(ContactChooserFragment.newInstance(contactId));
|
||||||
} else {
|
|
||||||
int contactId2 = savedInstanceState.getInt(BUNDLE_CONTACT2);
|
|
||||||
ContactId secondContactId = new ContactId(contactId2);
|
|
||||||
viewModel.setSecondContactId(secondContactId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
viewModel.getSecondContactSelected().observeEvent(this, e -> {
|
|
||||||
IntroductionMessageFragment fragment =
|
|
||||||
new IntroductionMessageFragment();
|
|
||||||
showNextFragment(fragment);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSaveInstanceState(@NonNull Bundle outState) {
|
public void injectActivity(ActivityComponent component) {
|
||||||
super.onSaveInstanceState(outState);
|
component.inject(this);
|
||||||
|
|
||||||
ContactId secondContactId = viewModel.getSecondContactId();
|
|
||||||
if (secondContactId != null) {
|
|
||||||
outState.putInt(BUNDLE_CONTACT2, secondContactId.getInt());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
package org.briarproject.briar.android.introduction;
|
|
||||||
|
|
||||||
import org.briarproject.briar.android.contact.ContactItem;
|
|
||||||
|
|
||||||
class IntroductionInfo {
|
|
||||||
private final ContactItem c1;
|
|
||||||
private final ContactItem c2;
|
|
||||||
private final boolean possible;
|
|
||||||
|
|
||||||
IntroductionInfo(ContactItem c1, ContactItem c2,
|
|
||||||
boolean possible) {
|
|
||||||
this.c1 = c1;
|
|
||||||
this.c2 = c2;
|
|
||||||
this.possible = possible;
|
|
||||||
}
|
|
||||||
|
|
||||||
ContactItem getContact1() {
|
|
||||||
return c1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ContactItem getContact2() {
|
|
||||||
return c2;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean isPossible() {
|
|
||||||
return possible;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.briarproject.briar.android.introduction;
|
package org.briarproject.briar.android.introduction;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
@@ -7,7 +8,12 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.contact.Contact;
|
||||||
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
|
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.briar.R;
|
import org.briarproject.briar.R;
|
||||||
@@ -18,19 +24,25 @@ import org.briarproject.briar.android.view.TextInputView;
|
|||||||
import org.briarproject.briar.android.view.TextSendController;
|
import org.briarproject.briar.android.view.TextSendController;
|
||||||
import org.briarproject.briar.android.view.TextSendController.SendListener;
|
import org.briarproject.briar.android.view.TextSendController.SendListener;
|
||||||
import org.briarproject.briar.api.attachment.AttachmentHeader;
|
import org.briarproject.briar.api.attachment.AttachmentHeader;
|
||||||
|
import org.briarproject.briar.api.identity.AuthorInfo;
|
||||||
|
import org.briarproject.briar.api.identity.AuthorManager;
|
||||||
|
import org.briarproject.briar.api.introduction.IntroductionManager;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.fragment.app.FragmentActivity;
|
import androidx.appcompat.app.ActionBar;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
|
||||||
import de.hdodenhof.circleimageview.CircleImageView;
|
import de.hdodenhof.circleimageview.CircleImageView;
|
||||||
|
|
||||||
import static android.app.Activity.RESULT_OK;
|
import static android.app.Activity.RESULT_OK;
|
||||||
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 android.widget.Toast.LENGTH_SHORT;
|
||||||
|
import static java.util.logging.Level.WARNING;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
import static org.briarproject.briar.android.util.UiUtils.getContactDisplayName;
|
import static org.briarproject.briar.android.util.UiUtils.getContactDisplayName;
|
||||||
import static org.briarproject.briar.android.util.UiUtils.hideSoftKeyboard;
|
import static org.briarproject.briar.android.util.UiUtils.hideSoftKeyboard;
|
||||||
import static org.briarproject.briar.android.view.AuthorView.setAvatar;
|
import static org.briarproject.briar.android.view.AuthorView.setAvatar;
|
||||||
@@ -41,21 +53,45 @@ import static org.briarproject.briar.api.introduction.IntroductionConstants.MAX_
|
|||||||
public class IntroductionMessageFragment extends BaseFragment
|
public class IntroductionMessageFragment extends BaseFragment
|
||||||
implements SendListener {
|
implements SendListener {
|
||||||
|
|
||||||
private static final String TAG =
|
public static final String TAG =
|
||||||
IntroductionMessageFragment.class.getName();
|
IntroductionMessageFragment.class.getName();
|
||||||
|
private static final Logger LOG = Logger.getLogger(TAG);
|
||||||
|
|
||||||
@Inject
|
private final static String CONTACT_ID_1 = "contact1";
|
||||||
ViewModelProvider.Factory viewModelFactory;
|
private final static String CONTACT_ID_2 = "contact2";
|
||||||
|
|
||||||
private IntroductionViewModel viewModel;
|
|
||||||
|
|
||||||
|
private IntroductionActivity introductionActivity;
|
||||||
private ViewHolder ui;
|
private ViewHolder ui;
|
||||||
|
private Contact contact1, contact2;
|
||||||
|
|
||||||
|
// Fields that are accessed from background threads must be volatile
|
||||||
|
@Inject
|
||||||
|
protected volatile ContactManager contactManager;
|
||||||
|
@Inject
|
||||||
|
protected volatile AuthorManager authorManager;
|
||||||
|
@Inject
|
||||||
|
protected volatile IntroductionManager introductionManager;
|
||||||
|
|
||||||
|
public static IntroductionMessageFragment newInstance(int contactId1,
|
||||||
|
int contactId2) {
|
||||||
|
Bundle args = new Bundle();
|
||||||
|
args.putInt(CONTACT_ID_1, contactId1);
|
||||||
|
args.putInt(CONTACT_ID_2, contactId2);
|
||||||
|
IntroductionMessageFragment fragment =
|
||||||
|
new IntroductionMessageFragment();
|
||||||
|
fragment.setArguments(args);
|
||||||
|
return fragment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAttach(Context context) {
|
||||||
|
super.onAttach(context);
|
||||||
|
introductionActivity = (IntroductionActivity) context;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void injectFragment(ActivityComponent component) {
|
public void injectFragment(ActivityComponent component) {
|
||||||
component.inject(this);
|
component.inject(this);
|
||||||
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory)
|
|
||||||
.get(IntroductionViewModel.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -64,7 +100,18 @@ public class IntroductionMessageFragment extends BaseFragment
|
|||||||
@Nullable Bundle savedInstanceState) {
|
@Nullable Bundle savedInstanceState) {
|
||||||
|
|
||||||
// change toolbar text
|
// change toolbar text
|
||||||
requireActivity().setTitle(R.string.introduction_message_title);
|
ActionBar actionBar = introductionActivity.getSupportActionBar();
|
||||||
|
if (actionBar != null) {
|
||||||
|
actionBar.setTitle(R.string.introduction_message_title);
|
||||||
|
}
|
||||||
|
|
||||||
|
// get contact IDs from fragment arguments
|
||||||
|
Bundle args = requireArguments();
|
||||||
|
int contactId1 = args.getInt(CONTACT_ID_1, -1);
|
||||||
|
int contactId2 = args.getInt(CONTACT_ID_2, -1);
|
||||||
|
if (contactId1 == -1 || contactId2 == -1) {
|
||||||
|
throw new AssertionError("Use newInstance() to instantiate");
|
||||||
|
}
|
||||||
|
|
||||||
// inflate view
|
// inflate view
|
||||||
View v = inflater.inflate(R.layout.introduction_message, container,
|
View v = inflater.inflate(R.layout.introduction_message, container,
|
||||||
@@ -76,44 +123,69 @@ public class IntroductionMessageFragment extends BaseFragment
|
|||||||
ui.message.setMaxTextLength(MAX_INTRODUCTION_TEXT_LENGTH);
|
ui.message.setMaxTextLength(MAX_INTRODUCTION_TEXT_LENGTH);
|
||||||
ui.message.setReady(false);
|
ui.message.setReady(false);
|
||||||
|
|
||||||
viewModel.getIntroductionInfo().observe(getViewLifecycleOwner(), ii -> {
|
// get contacts and then show view
|
||||||
if (ii == null) {
|
prepareToSetUpViews(contactId1, contactId2);
|
||||||
return;
|
|
||||||
}
|
|
||||||
setUpViews(ii.getContact1(), ii.getContact2(),
|
|
||||||
ii.isPossible());
|
|
||||||
});
|
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUniqueTag() {
|
public String getUniqueTag() {
|
||||||
return TAG;
|
return TAG;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void prepareToSetUpViews(int contactId1, int contactId2) {
|
||||||
|
introductionActivity.runOnDbThread(() -> {
|
||||||
|
try {
|
||||||
|
Contact contact1 =
|
||||||
|
contactManager.getContact(new ContactId(contactId1));
|
||||||
|
Contact contact2 =
|
||||||
|
contactManager.getContact(new ContactId(contactId2));
|
||||||
|
AuthorInfo a1 = authorManager.getAuthorInfo(contact1);
|
||||||
|
AuthorInfo a2 = authorManager.getAuthorInfo(contact2);
|
||||||
|
boolean possible =
|
||||||
|
introductionManager.canIntroduce(contact1, contact2);
|
||||||
|
ContactItem c1 = new ContactItem(contact1, a1);
|
||||||
|
ContactItem c2 = new ContactItem(contact2, a2);
|
||||||
|
setUpViews(c1, c2, possible);
|
||||||
|
} catch (DbException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void setUpViews(ContactItem c1, ContactItem c2, boolean possible) {
|
private void setUpViews(ContactItem c1, ContactItem c2, boolean possible) {
|
||||||
// set avatars
|
introductionActivity.runOnUiThreadUnlessDestroyed(() -> {
|
||||||
setAvatar(ui.avatar1, c1);
|
contact1 = c1.getContact();
|
||||||
setAvatar(ui.avatar2, c2);
|
contact2 = c2.getContact();
|
||||||
|
|
||||||
// set contact names
|
// set avatars
|
||||||
ui.contactName1.setText(getContactDisplayName(c1.getContact()));
|
setAvatar(ui.avatar1, c1);
|
||||||
ui.contactName2.setText(getContactDisplayName(c2.getContact()));
|
setAvatar(ui.avatar2, c2);
|
||||||
|
|
||||||
// hide progress bar
|
// set contact names
|
||||||
ui.progressBar.setVisibility(GONE);
|
ui.contactName1.setText(getContactDisplayName(c1.getContact()));
|
||||||
|
ui.contactName2.setText(getContactDisplayName(c2.getContact()));
|
||||||
|
|
||||||
if (possible) {
|
// hide progress bar
|
||||||
// show views
|
ui.progressBar.setVisibility(GONE);
|
||||||
ui.notPossible.setVisibility(GONE);
|
|
||||||
ui.message.setVisibility(VISIBLE);
|
if (possible) {
|
||||||
ui.message.setReady(true);
|
// show views
|
||||||
ui.message.showSoftKeyboard();
|
ui.notPossible.setVisibility(GONE);
|
||||||
} else {
|
ui.message.setVisibility(VISIBLE);
|
||||||
ui.notPossible.setVisibility(VISIBLE);
|
ui.message.setReady(true);
|
||||||
ui.message.setVisibility(GONE);
|
ui.message.showSoftKeyboard();
|
||||||
}
|
} else {
|
||||||
|
ui.notPossible.setVisibility(VISIBLE);
|
||||||
|
ui.message.setVisibility(GONE);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -121,7 +193,7 @@ public class IntroductionMessageFragment extends BaseFragment
|
|||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
case android.R.id.home:
|
case android.R.id.home:
|
||||||
hideSoftKeyboard(ui.message);
|
hideSoftKeyboard(ui.message);
|
||||||
requireActivity().onBackPressed();
|
introductionActivity.onBackPressed();
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
@@ -134,13 +206,32 @@ public class IntroductionMessageFragment extends BaseFragment
|
|||||||
// disable button to prevent accidental double invitations
|
// disable button to prevent accidental double invitations
|
||||||
ui.message.setReady(false);
|
ui.message.setReady(false);
|
||||||
|
|
||||||
viewModel.makeIntroduction(text);
|
makeIntroduction(contact1, contact2, text);
|
||||||
|
|
||||||
// don't wait for the introduction to be made before finishing activity
|
// don't wait for the introduction to be made before finishing activity
|
||||||
hideSoftKeyboard(ui.message);
|
hideSoftKeyboard(ui.message);
|
||||||
FragmentActivity activity = requireActivity();
|
introductionActivity.setResult(RESULT_OK);
|
||||||
activity.setResult(RESULT_OK);
|
introductionActivity.supportFinishAfterTransition();
|
||||||
activity.supportFinishAfterTransition();
|
}
|
||||||
|
|
||||||
|
private void makeIntroduction(Contact c1, Contact c2,
|
||||||
|
@Nullable String text) {
|
||||||
|
introductionActivity.runOnDbThread(() -> {
|
||||||
|
// actually make the introduction
|
||||||
|
try {
|
||||||
|
long timestamp = System.currentTimeMillis();
|
||||||
|
introductionManager.makeIntroduction(c1, c2, text, timestamp);
|
||||||
|
} catch (DbException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
introductionError();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void introductionError() {
|
||||||
|
introductionActivity.runOnUiThreadUnlessDestroyed(
|
||||||
|
() -> Toast.makeText(introductionActivity,
|
||||||
|
R.string.introduction_error, LENGTH_SHORT).show());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ViewHolder {
|
private static class ViewHolder {
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
package org.briarproject.briar.android.introduction;
|
|
||||||
|
|
||||||
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 IntroductionModule {
|
|
||||||
|
|
||||||
@Binds
|
|
||||||
@IntoMap
|
|
||||||
@ViewModelKey(IntroductionViewModel.class)
|
|
||||||
abstract ViewModel bindIntroductionViewModel(
|
|
||||||
IntroductionViewModel introductionViewModel);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,181 +0,0 @@
|
|||||||
package org.briarproject.briar.android.introduction;
|
|
||||||
|
|
||||||
import android.app.Application;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
|
||||||
import org.briarproject.bramble.api.contact.Contact;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
|
||||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
|
||||||
import org.briarproject.bramble.api.db.TransactionManager;
|
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
|
||||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
|
||||||
import org.briarproject.briar.R;
|
|
||||||
import org.briarproject.briar.android.contact.ContactItem;
|
|
||||||
import org.briarproject.briar.android.contact.ContactsViewModel;
|
|
||||||
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
|
||||||
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
|
||||||
import org.briarproject.briar.api.conversation.ConversationManager;
|
|
||||||
import org.briarproject.briar.api.identity.AuthorInfo;
|
|
||||||
import org.briarproject.briar.api.identity.AuthorManager;
|
|
||||||
import org.briarproject.briar.api.introduction.IntroductionManager;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.lifecycle.LiveData;
|
|
||||||
import androidx.lifecycle.MutableLiveData;
|
|
||||||
|
|
||||||
import static android.widget.Toast.LENGTH_SHORT;
|
|
||||||
import static java.util.Objects.requireNonNull;
|
|
||||||
import static java.util.logging.Level.WARNING;
|
|
||||||
import static java.util.logging.Logger.getLogger;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
class IntroductionViewModel extends ContactsViewModel {
|
|
||||||
|
|
||||||
private static final Logger LOG =
|
|
||||||
getLogger(IntroductionViewModel.class.getName());
|
|
||||||
|
|
||||||
private final ContactManager contactManager;
|
|
||||||
private final AuthorManager authorManager;
|
|
||||||
private final IntroductionManager introductionManager;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
IntroductionViewModel(Application application,
|
|
||||||
@DatabaseExecutor Executor dbExecutor,
|
|
||||||
LifecycleManager lifecycleManager, TransactionManager db,
|
|
||||||
AndroidExecutor androidExecutor, ContactManager contactManager,
|
|
||||||
AuthorManager authorManager,
|
|
||||||
ConversationManager conversationManager,
|
|
||||||
ConnectionRegistry connectionRegistry, EventBus eventBus,
|
|
||||||
IntroductionManager introductionManager) {
|
|
||||||
super(application, dbExecutor, lifecycleManager, db, androidExecutor,
|
|
||||||
contactManager, authorManager, conversationManager,
|
|
||||||
connectionRegistry, eventBus);
|
|
||||||
this.contactManager = contactManager;
|
|
||||||
this.authorManager = authorManager;
|
|
||||||
this.introductionManager = introductionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This is the contact from whose conversation we started the introduction
|
|
||||||
* using the menu item.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
private ContactId firstContactId;
|
|
||||||
/*
|
|
||||||
* This is the contact we selected from the list of contacts as a second
|
|
||||||
* contact for the introduction.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
private ContactId secondContactId;
|
|
||||||
|
|
||||||
private final MutableLiveEvent<Boolean> secondContactSelected =
|
|
||||||
new MutableLiveEvent<>();
|
|
||||||
|
|
||||||
private final MutableLiveData<IntroductionInfo> introductionInfo =
|
|
||||||
new MutableLiveData<>();
|
|
||||||
|
|
||||||
void setFirstContactId(ContactId contactId) {
|
|
||||||
this.firstContactId = contactId;
|
|
||||||
loadContacts();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
ContactId getSecondContactId() {
|
|
||||||
return secondContactId;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setSecondContactId(ContactId contactId) {
|
|
||||||
secondContactId = contactId;
|
|
||||||
// Setting this to null here so that IntroductionMessageFragment can
|
|
||||||
// tell whether the correct value has been loaded from the database when
|
|
||||||
// selecting a second contact repeatedly.
|
|
||||||
introductionInfo.setValue(null);
|
|
||||||
loadIntroductionInfo();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Trigger the event that the second contact has been selected from the
|
|
||||||
* contact list by the user.
|
|
||||||
*/
|
|
||||||
void triggerContactSelected() {
|
|
||||||
secondContactSelected.setEvent(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This event will be triggered once the second contact has been selected
|
|
||||||
* from the list of contacts displayed. It is not fired when the second
|
|
||||||
* contact gets restored from the saved instance state.
|
|
||||||
*/
|
|
||||||
LiveEvent<Boolean> getSecondContactSelected() {
|
|
||||||
return secondContactSelected;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Holder for the introduction info object with data about both contacts
|
|
||||||
* and whether the introduction is possible. May wrap null if the data
|
|
||||||
* is not available yet. This happens when it is reset by selecting a
|
|
||||||
* contact with the same view model instance more than once.
|
|
||||||
*/
|
|
||||||
LiveData<IntroductionInfo> getIntroductionInfo() {
|
|
||||||
return introductionInfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean displayContact(ContactId contactId) {
|
|
||||||
return !requireNonNull(firstContactId).equals(contactId);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void loadIntroductionInfo() {
|
|
||||||
final ContactId firstContactId = requireNonNull(this.firstContactId);
|
|
||||||
final ContactId secondContactId = requireNonNull(this.secondContactId);
|
|
||||||
runOnDbThread(() -> {
|
|
||||||
try {
|
|
||||||
Contact firstContact =
|
|
||||||
contactManager.getContact(firstContactId);
|
|
||||||
Contact secondContact =
|
|
||||||
contactManager.getContact(secondContactId);
|
|
||||||
AuthorInfo a1 = authorManager.getAuthorInfo(firstContact);
|
|
||||||
AuthorInfo a2 = authorManager.getAuthorInfo(secondContact);
|
|
||||||
boolean possible = introductionManager
|
|
||||||
.canIntroduce(firstContact, secondContact);
|
|
||||||
ContactItem c1 = new ContactItem(firstContact, a1);
|
|
||||||
ContactItem c2 = new ContactItem(secondContact, a2);
|
|
||||||
introductionInfo.postValue(
|
|
||||||
new IntroductionInfo(c1, c2, possible));
|
|
||||||
} catch (DbException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void makeIntroduction(@Nullable String text) {
|
|
||||||
final IntroductionInfo info =
|
|
||||||
requireNonNull(introductionInfo.getValue());
|
|
||||||
runOnDbThread(() -> {
|
|
||||||
// actually make the introduction
|
|
||||||
try {
|
|
||||||
long timestamp = System.currentTimeMillis();
|
|
||||||
introductionManager.makeIntroduction(
|
|
||||||
info.getContact1().getContact(),
|
|
||||||
info.getContact2().getContact(), text, timestamp);
|
|
||||||
} catch (DbException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
androidExecutor.runOnUiThread(() -> Toast.makeText(
|
|
||||||
getApplication(), R.string.introduction_error,
|
|
||||||
LENGTH_SHORT).show());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.briarproject.briar.android.contact.add.nearby;
|
package org.briarproject.briar.android.keyagreement;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.briarproject.briar.android.contact.add.nearby;
|
package org.briarproject.briar.android.keyagreement;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.hardware.Camera;
|
import android.hardware.Camera;
|
||||||
@@ -40,6 +40,7 @@ import static java.util.logging.Level.INFO;
|
|||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
public class CameraView extends SurfaceView implements SurfaceHolder.Callback,
|
public class CameraView extends SurfaceView implements SurfaceHolder.Callback,
|
||||||
@@ -125,14 +126,8 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback,
|
|||||||
throw new CameraException(e);
|
throw new CameraException(e);
|
||||||
}
|
}
|
||||||
setDisplayOrientation(getScreenRotationDegrees());
|
setDisplayOrientation(getScreenRotationDegrees());
|
||||||
if (camera == null) throw new CameraException("No camera found");
|
|
||||||
// Use barcode scene mode if it's available
|
// Use barcode scene mode if it's available
|
||||||
Parameters params;
|
Parameters params = camera.getParameters();
|
||||||
try {
|
|
||||||
params = camera.getParameters();
|
|
||||||
} catch (RuntimeException e) {
|
|
||||||
throw new CameraException(e);
|
|
||||||
}
|
|
||||||
params = setSceneMode(camera, params);
|
params = setSceneMode(camera, params);
|
||||||
if (SCENE_MODE_BARCODE.equals(params.getSceneMode())) {
|
if (SCENE_MODE_BARCODE.equals(params.getSceneMode())) {
|
||||||
// If the scene mode enabled the flash, try to disable it
|
// If the scene mode enabled the flash, try to disable it
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
package org.briarproject.briar.android.keyagreement;
|
||||||
|
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.identity.Author;
|
||||||
|
import org.briarproject.bramble.api.keyagreement.KeyAgreementResult;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
|
import org.briarproject.briar.R;
|
||||||
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import androidx.annotation.UiThread;
|
||||||
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
import androidx.lifecycle.ViewModelProviders;
|
||||||
|
|
||||||
|
import static android.widget.Toast.LENGTH_LONG;
|
||||||
|
import static java.util.Objects.requireNonNull;
|
||||||
|
|
||||||
|
@MethodsNotNullByDefault
|
||||||
|
@ParametersNotNullByDefault
|
||||||
|
public class ContactExchangeActivity extends KeyAgreementActivity {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
ViewModelProvider.Factory viewModelFactory;
|
||||||
|
|
||||||
|
private ContactExchangeViewModel viewModel;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void injectActivity(ActivityComponent component) {
|
||||||
|
component.inject(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(@Nullable Bundle state) {
|
||||||
|
super.onCreate(state);
|
||||||
|
requireNonNull(getSupportActionBar())
|
||||||
|
.setTitle(R.string.add_contact_title);
|
||||||
|
viewModel = ViewModelProviders.of(this, viewModelFactory)
|
||||||
|
.get(ContactExchangeViewModel.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startContactExchange(KeyAgreementResult result) {
|
||||||
|
viewModel.getSucceeded().observe(this, succeeded -> {
|
||||||
|
if (succeeded == null) return;
|
||||||
|
if (succeeded) {
|
||||||
|
Author remote = requireNonNull(viewModel.getRemoteAuthor());
|
||||||
|
contactExchangeSucceeded(remote);
|
||||||
|
} else {
|
||||||
|
Author duplicate = viewModel.getDuplicateAuthor();
|
||||||
|
if (duplicate == null) contactExchangeFailed();
|
||||||
|
else duplicateContact(duplicate);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
viewModel.startContactExchange(result.getTransportId(),
|
||||||
|
result.getConnection(), result.getMasterKey(),
|
||||||
|
result.wasAlice());
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void contactExchangeSucceeded(Author remoteAuthor) {
|
||||||
|
String contactName = remoteAuthor.getName();
|
||||||
|
String text = getString(R.string.contact_added_toast, contactName);
|
||||||
|
Toast.makeText(this, text, LENGTH_LONG).show();
|
||||||
|
supportFinishAfterTransition();
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void duplicateContact(Author remoteAuthor) {
|
||||||
|
String contactName = remoteAuthor.getName();
|
||||||
|
String format = getString(R.string.contact_already_exists);
|
||||||
|
String text = String.format(format, contactName);
|
||||||
|
Toast.makeText(this, text, LENGTH_LONG).show();
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void contactExchangeFailed() {
|
||||||
|
showErrorFragment();
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
@Override
|
||||||
|
public void keyAgreementFailed() {
|
||||||
|
showErrorFragment();
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
@Override
|
||||||
|
public String keyAgreementWaiting() {
|
||||||
|
return getString(R.string.waiting_for_contact_to_scan);
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
@Override
|
||||||
|
public String keyAgreementStarted() {
|
||||||
|
return getString(R.string.authenticating_with_device);
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
@Override
|
||||||
|
public void keyAgreementAborted(boolean remoteAborted) {
|
||||||
|
showErrorFragment();
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
@Override
|
||||||
|
public String keyAgreementFinished(KeyAgreementResult result) {
|
||||||
|
startContactExchange(result);
|
||||||
|
return getString(R.string.exchanging_contact_details);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showErrorFragment() {
|
||||||
|
showNextFragment(new ContactExchangeErrorFragment());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.briarproject.briar.android.contact.add.nearby;
|
package org.briarproject.briar.android.keyagreement;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
@@ -24,14 +24,14 @@ import static org.briarproject.briar.android.util.UiUtils.onSingleLinkClick;
|
|||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
public class AddNearbyContactErrorFragment extends BaseFragment {
|
public class ContactExchangeErrorFragment extends BaseFragment {
|
||||||
|
|
||||||
public static final String TAG =
|
public static final String TAG =
|
||||||
AddNearbyContactErrorFragment.class.getName();
|
ContactExchangeErrorFragment.class.getName();
|
||||||
private static final String ERROR_MSG = "errorMessage";
|
private static final String ERROR_MSG = "errorMessage";
|
||||||
|
|
||||||
public static AddNearbyContactErrorFragment newInstance(String errorMsg) {
|
public static ContactExchangeErrorFragment newInstance(String errorMsg) {
|
||||||
AddNearbyContactErrorFragment f = new AddNearbyContactErrorFragment();
|
ContactExchangeErrorFragment f = new ContactExchangeErrorFragment();
|
||||||
Bundle args = new Bundle();
|
Bundle args = new Bundle();
|
||||||
args.putString(ERROR_MSG, errorMsg);
|
args.putString(ERROR_MSG, errorMsg);
|
||||||
f.setArguments(args);
|
f.setArguments(args);
|
||||||
@@ -72,7 +72,7 @@ public class AddNearbyContactErrorFragment extends BaseFragment {
|
|||||||
tryAgain.setOnClickListener(view -> {
|
tryAgain.setOnClickListener(view -> {
|
||||||
// Recreate the activity so we return to the intro fragment
|
// Recreate the activity so we return to the intro fragment
|
||||||
FragmentActivity activity = requireActivity();
|
FragmentActivity activity = requireActivity();
|
||||||
Intent i = new Intent(activity, AddNearbyContactActivity.class);
|
Intent i = new Intent(activity, ContactExchangeActivity.class);
|
||||||
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
|
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
activity.startActivity(i);
|
activity.startActivity(i);
|
||||||
});
|
});
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.briarproject.briar.android.contact.add.nearby;
|
package org.briarproject.briar.android.keyagreement;
|
||||||
|
|
||||||
import org.briarproject.briar.android.viewmodel.ViewModelKey;
|
import org.briarproject.briar.android.viewmodel.ViewModelKey;
|
||||||
|
|
||||||
@@ -8,12 +8,12 @@ import dagger.Module;
|
|||||||
import dagger.multibindings.IntoMap;
|
import dagger.multibindings.IntoMap;
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
public abstract class AddNearbyContactModule {
|
public abstract class ContactExchangeModule {
|
||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@IntoMap
|
@IntoMap
|
||||||
@ViewModelKey(AddNearbyContactViewModel.class)
|
@ViewModelKey(ContactExchangeViewModel.class)
|
||||||
abstract ViewModel bindContactExchangeViewModel(
|
abstract ViewModel bindContactExchangeViewModel(
|
||||||
AddNearbyContactViewModel addNearbyContactViewModel);
|
ContactExchangeViewModel contactExchangeViewModel);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
package org.briarproject.briar.android.keyagreement;
|
||||||
|
|
||||||
|
import android.app.Application;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.connection.ConnectionManager;
|
||||||
|
import org.briarproject.bramble.api.contact.Contact;
|
||||||
|
import org.briarproject.bramble.api.contact.ContactExchangeManager;
|
||||||
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
|
import org.briarproject.bramble.api.db.ContactExistsException;
|
||||||
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
|
import org.briarproject.bramble.api.identity.Author;
|
||||||
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
|
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import androidx.annotation.UiThread;
|
||||||
|
import androidx.lifecycle.AndroidViewModel;
|
||||||
|
import androidx.lifecycle.LiveData;
|
||||||
|
import androidx.lifecycle.MutableLiveData;
|
||||||
|
|
||||||
|
import static java.util.logging.Level.WARNING;
|
||||||
|
import static java.util.logging.Logger.getLogger;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
|
class ContactExchangeViewModel extends AndroidViewModel {
|
||||||
|
|
||||||
|
private static final Logger LOG =
|
||||||
|
getLogger(ContactExchangeViewModel.class.getName());
|
||||||
|
|
||||||
|
private final Executor ioExecutor;
|
||||||
|
private final ContactExchangeManager contactExchangeManager;
|
||||||
|
private final ConnectionManager connectionManager;
|
||||||
|
private final MutableLiveData<Boolean> succeeded = new MutableLiveData<>();
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private volatile Author remoteAuthor, duplicateAuthor;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
ContactExchangeViewModel(Application app, @IoExecutor Executor ioExecutor,
|
||||||
|
ContactExchangeManager contactExchangeManager,
|
||||||
|
ConnectionManager connectionManager) {
|
||||||
|
super(app);
|
||||||
|
this.ioExecutor = ioExecutor;
|
||||||
|
this.contactExchangeManager = contactExchangeManager;
|
||||||
|
this.connectionManager = connectionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
void startContactExchange(TransportId t, DuplexTransportConnection conn,
|
||||||
|
SecretKey masterKey, boolean alice) {
|
||||||
|
ioExecutor.execute(() -> {
|
||||||
|
try {
|
||||||
|
Contact contact = contactExchangeManager.exchangeContacts(conn,
|
||||||
|
masterKey, alice, true);
|
||||||
|
// Reuse the connection as a transport connection
|
||||||
|
connectionManager.manageOutgoingConnection(contact.getId(),
|
||||||
|
t, conn);
|
||||||
|
remoteAuthor = contact.getAuthor();
|
||||||
|
succeeded.postValue(true);
|
||||||
|
} catch (ContactExistsException e) {
|
||||||
|
tryToClose(conn);
|
||||||
|
duplicateAuthor = e.getRemoteAuthor();
|
||||||
|
succeeded.postValue(false);
|
||||||
|
} catch (DbException | IOException e) {
|
||||||
|
tryToClose(conn);
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
succeeded.postValue(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void tryToClose(DuplexTransportConnection conn) {
|
||||||
|
try {
|
||||||
|
conn.getReader().dispose(true, true);
|
||||||
|
conn.getWriter().dispose(true);
|
||||||
|
} catch (IOException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
@Nullable
|
||||||
|
Author getRemoteAuthor() {
|
||||||
|
return remoteAuthor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
@Nullable
|
||||||
|
Author getDuplicateAuthor() {
|
||||||
|
return duplicateAuthor;
|
||||||
|
}
|
||||||
|
|
||||||
|
LiveData<Boolean> getSucceeded() {
|
||||||
|
return succeeded;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package org.briarproject.briar.android.keyagreement;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.ScrollView;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
|
import org.briarproject.briar.R;
|
||||||
|
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import static android.view.View.FOCUS_DOWN;
|
||||||
|
|
||||||
|
@MethodsNotNullByDefault
|
||||||
|
@ParametersNotNullByDefault
|
||||||
|
public class IntroFragment extends BaseFragment {
|
||||||
|
|
||||||
|
interface IntroScreenSeenListener {
|
||||||
|
void showNextScreen();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String TAG = IntroFragment.class.getName();
|
||||||
|
|
||||||
|
private IntroScreenSeenListener screenSeenListener;
|
||||||
|
private ScrollView scrollView;
|
||||||
|
|
||||||
|
public static IntroFragment newInstance() {
|
||||||
|
|
||||||
|
Bundle args = new Bundle();
|
||||||
|
|
||||||
|
IntroFragment fragment = new IntroFragment();
|
||||||
|
fragment.setArguments(args);
|
||||||
|
return fragment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAttach(Context context) {
|
||||||
|
super.onAttach(context);
|
||||||
|
screenSeenListener = (IntroScreenSeenListener) context;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUniqueTag() {
|
||||||
|
return TAG;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public View onCreateView(LayoutInflater inflater,
|
||||||
|
@Nullable ViewGroup container,
|
||||||
|
@Nullable Bundle savedInstanceState) {
|
||||||
|
|
||||||
|
View v = inflater.inflate(R.layout.fragment_keyagreement_id, container,
|
||||||
|
false);
|
||||||
|
scrollView = v.findViewById(R.id.scrollView);
|
||||||
|
View button = v.findViewById(R.id.continueButton);
|
||||||
|
button.setOnClickListener(view -> screenSeenListener.showNextScreen());
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
scrollView.post(() -> scrollView.fullScroll(FOCUS_DOWN));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,482 @@
|
|||||||
|
package org.briarproject.briar.android.keyagreement;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.view.MenuItem;
|
||||||
|
|
||||||
|
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.nullsafety.MethodsNotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.plugin.BluetoothConstants;
|
||||||
|
import org.briarproject.bramble.api.plugin.LanTcpConstants;
|
||||||
|
import org.briarproject.bramble.api.plugin.Plugin;
|
||||||
|
import org.briarproject.bramble.api.plugin.Plugin.State;
|
||||||
|
import org.briarproject.bramble.api.plugin.PluginManager;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.TransportStateEvent;
|
||||||
|
import org.briarproject.briar.R;
|
||||||
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
|
import org.briarproject.briar.android.activity.BriarActivity;
|
||||||
|
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||||
|
import org.briarproject.briar.android.fragment.BaseFragment.BaseFragmentListener;
|
||||||
|
import org.briarproject.briar.android.keyagreement.IntroFragment.IntroScreenSeenListener;
|
||||||
|
import org.briarproject.briar.android.keyagreement.KeyAgreementFragment.KeyAgreementEventListener;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import androidx.annotation.StringRes;
|
||||||
|
import androidx.annotation.UiThread;
|
||||||
|
import androidx.appcompat.app.AlertDialog.Builder;
|
||||||
|
import androidx.appcompat.widget.Toolbar;
|
||||||
|
import androidx.core.app.ActivityCompat;
|
||||||
|
import androidx.fragment.app.FragmentManager;
|
||||||
|
|
||||||
|
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
|
||||||
|
import static android.Manifest.permission.CAMERA;
|
||||||
|
import static android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
|
||||||
|
import static android.bluetooth.BluetoothAdapter.ACTION_SCAN_MODE_CHANGED;
|
||||||
|
import static android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
|
||||||
|
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
|
||||||
|
import static android.os.Build.VERSION.SDK_INT;
|
||||||
|
import static java.util.logging.Level.INFO;
|
||||||
|
import static java.util.logging.Logger.getLogger;
|
||||||
|
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
||||||
|
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||||
|
import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
|
||||||
|
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
||||||
|
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
||||||
|
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_BLUETOOTH_DISCOVERABLE;
|
||||||
|
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_PERMISSION_CAMERA_LOCATION;
|
||||||
|
import static org.briarproject.briar.android.util.UiUtils.getGoToSettingsListener;
|
||||||
|
|
||||||
|
@MethodsNotNullByDefault
|
||||||
|
@ParametersNotNullByDefault
|
||||||
|
public abstract class KeyAgreementActivity extends BriarActivity implements
|
||||||
|
BaseFragmentListener, IntroScreenSeenListener,
|
||||||
|
KeyAgreementEventListener, EventListener {
|
||||||
|
|
||||||
|
private enum BluetoothDecision {
|
||||||
|
/**
|
||||||
|
* We haven't asked the user about Bluetooth discoverability.
|
||||||
|
*/
|
||||||
|
UNKNOWN,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The device doesn't have a Bluetooth adapter.
|
||||||
|
*/
|
||||||
|
NO_ADAPTER,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We're waiting for the user to accept or refuse discoverability.
|
||||||
|
*/
|
||||||
|
WAITING,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The user has accepted discoverability.
|
||||||
|
*/
|
||||||
|
ACCEPTED,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The user has refused discoverability.
|
||||||
|
*/
|
||||||
|
REFUSED
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum Permission {
|
||||||
|
UNKNOWN, GRANTED, SHOW_RATIONALE, PERMANENTLY_DENIED
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Logger LOG =
|
||||||
|
getLogger(KeyAgreementActivity.class.getName());
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
EventBus eventBus;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
PluginManager pluginManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set to true in onPostResume() and false in onPause(). This prevents the
|
||||||
|
* QR code fragment from being shown if onRequestPermissionsResult() is
|
||||||
|
* called while the activity is paused, which could cause a crash due to
|
||||||
|
* https://issuetracker.google.com/issues/37067655.
|
||||||
|
*/
|
||||||
|
private boolean isResumed = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set to true when the continue button is clicked, and false when the QR
|
||||||
|
* code fragment is shown. This prevents the QR code fragment from being
|
||||||
|
* shown automatically before the continue button has been clicked.
|
||||||
|
*/
|
||||||
|
private boolean continueClicked = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Records whether we've enabled the wifi plugin so we don't enable it more
|
||||||
|
* than once.
|
||||||
|
*/
|
||||||
|
private boolean hasEnabledWifi = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Records whether we've enabled the Bluetooth plugin so we don't enable it
|
||||||
|
* more than once.
|
||||||
|
*/
|
||||||
|
private boolean hasEnabledBluetooth = false;
|
||||||
|
|
||||||
|
private Permission cameraPermission = Permission.UNKNOWN;
|
||||||
|
private Permission locationPermission = Permission.UNKNOWN;
|
||||||
|
private BluetoothDecision bluetoothDecision = BluetoothDecision.UNKNOWN;
|
||||||
|
private BroadcastReceiver bluetoothReceiver = null;
|
||||||
|
private Plugin wifiPlugin = null, bluetoothPlugin = null;
|
||||||
|
private BluetoothAdapter bt = null;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void injectActivity(ActivityComponent component) {
|
||||||
|
component.inject(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(@Nullable Bundle state) {
|
||||||
|
super.onCreate(state);
|
||||||
|
setContentView(R.layout.activity_fragment_container_toolbar);
|
||||||
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||||
|
setSupportActionBar(toolbar);
|
||||||
|
requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
|
||||||
|
if (state == null) {
|
||||||
|
showInitialFragment(IntroFragment.newInstance());
|
||||||
|
}
|
||||||
|
IntentFilter filter = new IntentFilter(ACTION_SCAN_MODE_CHANGED);
|
||||||
|
bluetoothReceiver = new BluetoothStateReceiver();
|
||||||
|
registerReceiver(bluetoothReceiver, filter);
|
||||||
|
wifiPlugin = pluginManager.getPlugin(LanTcpConstants.ID);
|
||||||
|
bluetoothPlugin = pluginManager.getPlugin(BluetoothConstants.ID);
|
||||||
|
bt = BluetoothAdapter.getDefaultAdapter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
|
if (bluetoothReceiver != null) unregisterReceiver(bluetoothReceiver);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
|
if (item.getItemId() == android.R.id.home) {
|
||||||
|
onBackPressed();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return super.onOptionsItemSelected(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
eventBus.addListener(this);
|
||||||
|
// Permissions may have been granted manually while we were stopped
|
||||||
|
cameraPermission = Permission.UNKNOWN;
|
||||||
|
locationPermission = Permission.UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostResume() {
|
||||||
|
super.onPostResume();
|
||||||
|
isResumed = true;
|
||||||
|
// Workaround for
|
||||||
|
// https://code.google.com/p/android/issues/detail?id=190966
|
||||||
|
showQrCodeFragmentIfAllowed();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("StatementWithEmptyBody")
|
||||||
|
private void showQrCodeFragmentIfAllowed() {
|
||||||
|
if (isResumed && continueClicked && areEssentialPermissionsGranted()) {
|
||||||
|
if (isWifiReady() && isBluetoothReady()) {
|
||||||
|
LOG.info("Wifi and Bluetooth are ready");
|
||||||
|
showQrCodeFragment();
|
||||||
|
} else {
|
||||||
|
if (shouldEnableWifi()) {
|
||||||
|
LOG.info("Enabling wifi plugin");
|
||||||
|
hasEnabledWifi = true;
|
||||||
|
pluginManager.setPluginEnabled(LanTcpConstants.ID, true);
|
||||||
|
}
|
||||||
|
if (bluetoothDecision == BluetoothDecision.UNKNOWN) {
|
||||||
|
requestBluetoothDiscoverable();
|
||||||
|
} else if (bluetoothDecision == BluetoothDecision.REFUSED) {
|
||||||
|
// Ask again when the user clicks "continue"
|
||||||
|
} else if (shouldEnableBluetooth()) {
|
||||||
|
LOG.info("Enabling Bluetooth plugin");
|
||||||
|
hasEnabledBluetooth = true;
|
||||||
|
pluginManager.setPluginEnabled(BluetoothConstants.ID, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean areEssentialPermissionsGranted() {
|
||||||
|
return cameraPermission == Permission.GRANTED &&
|
||||||
|
(SDK_INT < 23 || locationPermission == Permission.GRANTED ||
|
||||||
|
!isBluetoothSupported());
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isBluetoothSupported() {
|
||||||
|
return bt != null && bluetoothPlugin != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isWifiReady() {
|
||||||
|
if (wifiPlugin == null) return true; // Continue without wifi
|
||||||
|
State state = wifiPlugin.getState();
|
||||||
|
// Wait for plugin to become enabled
|
||||||
|
return state == ACTIVE || state == INACTIVE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isBluetoothReady() {
|
||||||
|
if (!isBluetoothSupported()) {
|
||||||
|
// Continue without Bluetooth
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (bluetoothDecision == BluetoothDecision.UNKNOWN ||
|
||||||
|
bluetoothDecision == BluetoothDecision.WAITING ||
|
||||||
|
bluetoothDecision == BluetoothDecision.REFUSED) {
|
||||||
|
// Wait for user to accept
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (bt.getScanMode() != SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
|
||||||
|
// Wait for adapter to become discoverable
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Wait for plugin to become active
|
||||||
|
return bluetoothPlugin.getState() == ACTIVE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean shouldEnableWifi() {
|
||||||
|
if (hasEnabledWifi) return false;
|
||||||
|
if (wifiPlugin == null) return false;
|
||||||
|
State state = wifiPlugin.getState();
|
||||||
|
return state == STARTING_STOPPING || state == DISABLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void requestBluetoothDiscoverable() {
|
||||||
|
if (!isBluetoothSupported()) {
|
||||||
|
bluetoothDecision = BluetoothDecision.NO_ADAPTER;
|
||||||
|
showQrCodeFragmentIfAllowed();
|
||||||
|
} else {
|
||||||
|
Intent i = new Intent(ACTION_REQUEST_DISCOVERABLE);
|
||||||
|
if (i.resolveActivity(getPackageManager()) != null) {
|
||||||
|
LOG.info("Asking for Bluetooth discoverability");
|
||||||
|
bluetoothDecision = BluetoothDecision.WAITING;
|
||||||
|
startActivityForResult(i, REQUEST_BLUETOOTH_DISCOVERABLE);
|
||||||
|
} else {
|
||||||
|
bluetoothDecision = BluetoothDecision.NO_ADAPTER;
|
||||||
|
showQrCodeFragmentIfAllowed();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean shouldEnableBluetooth() {
|
||||||
|
if (bluetoothDecision != BluetoothDecision.ACCEPTED) return false;
|
||||||
|
if (hasEnabledBluetooth) return false;
|
||||||
|
if (!isBluetoothSupported()) return false;
|
||||||
|
State state = bluetoothPlugin.getState();
|
||||||
|
return state == STARTING_STOPPING || state == DISABLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
isResumed = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStop() {
|
||||||
|
super.onStop();
|
||||||
|
eventBus.removeListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showNextScreen() {
|
||||||
|
continueClicked = true;
|
||||||
|
if (bluetoothDecision == BluetoothDecision.REFUSED) {
|
||||||
|
bluetoothDecision = BluetoothDecision.UNKNOWN; // Ask again
|
||||||
|
}
|
||||||
|
if (checkPermissions()) showQrCodeFragmentIfAllowed();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onActivityResult(int request, int result,
|
||||||
|
@Nullable Intent data) {
|
||||||
|
if (request == REQUEST_BLUETOOTH_DISCOVERABLE) {
|
||||||
|
if (result == RESULT_CANCELED) {
|
||||||
|
LOG.info("Bluetooth discoverability was refused");
|
||||||
|
bluetoothDecision = BluetoothDecision.REFUSED;
|
||||||
|
} else {
|
||||||
|
LOG.info("Bluetooth discoverability was accepted");
|
||||||
|
bluetoothDecision = BluetoothDecision.ACCEPTED;
|
||||||
|
}
|
||||||
|
showQrCodeFragmentIfAllowed();
|
||||||
|
} else super.onActivityResult(request, result, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showQrCodeFragment() {
|
||||||
|
// If we return to the intro fragment, the continue button needs to be
|
||||||
|
// clicked again before showing the QR code fragment
|
||||||
|
continueClicked = false;
|
||||||
|
// If we return to the intro fragment, ask for Bluetooth
|
||||||
|
// discoverability again before showing the QR code fragment
|
||||||
|
bluetoothDecision = BluetoothDecision.UNKNOWN;
|
||||||
|
// If we return to the intro fragment, we may need to enable wifi and
|
||||||
|
// Bluetooth again
|
||||||
|
hasEnabledWifi = false;
|
||||||
|
hasEnabledBluetooth = false;
|
||||||
|
|
||||||
|
// FIXME #824
|
||||||
|
FragmentManager fm = getSupportFragmentManager();
|
||||||
|
if (fm.findFragmentByTag(KeyAgreementFragment.TAG) == null) {
|
||||||
|
BaseFragment f = KeyAgreementFragment.newInstance();
|
||||||
|
fm.beginTransaction()
|
||||||
|
.replace(R.id.fragmentContainer, f, f.getUniqueTag())
|
||||||
|
.addToBackStack(f.getUniqueTag())
|
||||||
|
.commit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkPermissions() {
|
||||||
|
if (areEssentialPermissionsGranted()) return true;
|
||||||
|
// If an essential permission has been permanently denied, ask the
|
||||||
|
// user to change the setting
|
||||||
|
if (cameraPermission == Permission.PERMANENTLY_DENIED) {
|
||||||
|
showDenialDialog(R.string.permission_camera_title,
|
||||||
|
R.string.permission_camera_denied_body);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (isBluetoothSupported() &&
|
||||||
|
locationPermission == Permission.PERMANENTLY_DENIED) {
|
||||||
|
showDenialDialog(R.string.permission_location_title,
|
||||||
|
R.string.permission_location_denied_body);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Should we show the rationale for one or both permissions?
|
||||||
|
if (cameraPermission == Permission.SHOW_RATIONALE &&
|
||||||
|
locationPermission == Permission.SHOW_RATIONALE) {
|
||||||
|
showRationale(R.string.permission_camera_location_title,
|
||||||
|
R.string.permission_camera_location_request_body);
|
||||||
|
} else if (cameraPermission == Permission.SHOW_RATIONALE) {
|
||||||
|
showRationale(R.string.permission_camera_title,
|
||||||
|
R.string.permission_camera_request_body);
|
||||||
|
} else if (locationPermission == Permission.SHOW_RATIONALE) {
|
||||||
|
showRationale(R.string.permission_location_title,
|
||||||
|
R.string.permission_location_request_body);
|
||||||
|
} else {
|
||||||
|
requestPermissions();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showDenialDialog(@StringRes int title, @StringRes int body) {
|
||||||
|
Builder builder = new Builder(this, R.style.BriarDialogTheme);
|
||||||
|
builder.setTitle(title);
|
||||||
|
builder.setMessage(body);
|
||||||
|
builder.setPositiveButton(R.string.ok, getGoToSettingsListener(this));
|
||||||
|
builder.setNegativeButton(R.string.cancel,
|
||||||
|
(dialog, which) -> supportFinishAfterTransition());
|
||||||
|
builder.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showRationale(@StringRes int title, @StringRes int body) {
|
||||||
|
Builder builder = new Builder(this, R.style.BriarDialogTheme);
|
||||||
|
builder.setTitle(title);
|
||||||
|
builder.setMessage(body);
|
||||||
|
builder.setNeutralButton(R.string.continue_button,
|
||||||
|
(dialog, which) -> requestPermissions());
|
||||||
|
builder.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void requestPermissions() {
|
||||||
|
String[] permissions;
|
||||||
|
if (isBluetoothSupported()) {
|
||||||
|
permissions = new String[] {CAMERA, ACCESS_FINE_LOCATION};
|
||||||
|
} else {
|
||||||
|
permissions = new String[] {CAMERA};
|
||||||
|
}
|
||||||
|
ActivityCompat.requestPermissions(this, permissions,
|
||||||
|
REQUEST_PERMISSION_CAMERA_LOCATION);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@UiThread
|
||||||
|
public void onRequestPermissionsResult(int requestCode,
|
||||||
|
String[] permissions, int[] grantResults) {
|
||||||
|
if (requestCode != REQUEST_PERMISSION_CAMERA_LOCATION)
|
||||||
|
throw new AssertionError();
|
||||||
|
if (gotPermission(CAMERA, permissions, grantResults)) {
|
||||||
|
cameraPermission = Permission.GRANTED;
|
||||||
|
} else if (shouldShowRationale(CAMERA)) {
|
||||||
|
cameraPermission = Permission.SHOW_RATIONALE;
|
||||||
|
} else {
|
||||||
|
cameraPermission = Permission.PERMANENTLY_DENIED;
|
||||||
|
}
|
||||||
|
if (isBluetoothSupported()) {
|
||||||
|
if (gotPermission(ACCESS_FINE_LOCATION, permissions,
|
||||||
|
grantResults)) {
|
||||||
|
locationPermission = Permission.GRANTED;
|
||||||
|
} else if (shouldShowRationale(ACCESS_FINE_LOCATION)) {
|
||||||
|
locationPermission = Permission.SHOW_RATIONALE;
|
||||||
|
} else {
|
||||||
|
locationPermission = Permission.PERMANENTLY_DENIED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If a permission dialog has been shown, showing the QR code fragment
|
||||||
|
// on this call path would cause a crash due to
|
||||||
|
// https://code.google.com/p/android/issues/detail?id=190966.
|
||||||
|
// In that case the isResumed flag prevents the fragment from being
|
||||||
|
// shown here, and showQrCodeFragmentIfAllowed() will be called again
|
||||||
|
// from onPostResume().
|
||||||
|
if (checkPermissions()) showQrCodeFragmentIfAllowed();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean gotPermission(String permission, String[] permissions,
|
||||||
|
int[] grantResults) {
|
||||||
|
for (int i = 0; i < permissions.length; i++) {
|
||||||
|
if (permission.equals(permissions[i]))
|
||||||
|
return grantResults[i] == PERMISSION_GRANTED;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean shouldShowRationale(String permission) {
|
||||||
|
return ActivityCompat.shouldShowRequestPermissionRationale(this,
|
||||||
|
permission);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void eventOccurred(Event e) {
|
||||||
|
if (e instanceof TransportStateEvent) {
|
||||||
|
TransportStateEvent t = (TransportStateEvent) e;
|
||||||
|
if (t.getTransportId().equals(BluetoothConstants.ID)) {
|
||||||
|
if (LOG.isLoggable(INFO)) {
|
||||||
|
LOG.info("Bluetooth state changed to " + t.getState());
|
||||||
|
}
|
||||||
|
showQrCodeFragmentIfAllowed();
|
||||||
|
} else if (t.getTransportId().equals(LanTcpConstants.ID)) {
|
||||||
|
if (LOG.isLoggable(INFO)) {
|
||||||
|
LOG.info("Wifi state changed to " + t.getState());
|
||||||
|
}
|
||||||
|
showQrCodeFragmentIfAllowed();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class BluetoothStateReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
LOG.info("Bluetooth scan mode changed");
|
||||||
|
showQrCodeFragmentIfAllowed();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,375 @@
|
|||||||
|
package org.briarproject.briar.android.keyagreement;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.util.DisplayMetrics;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.LinearLayout;
|
||||||
|
import android.widget.LinearLayout.LayoutParams;
|
||||||
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.google.zxing.Result;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.UnsupportedVersionException;
|
||||||
|
import org.briarproject.bramble.api.event.Event;
|
||||||
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
|
import org.briarproject.bramble.api.keyagreement.KeyAgreementResult;
|
||||||
|
import org.briarproject.bramble.api.keyagreement.KeyAgreementTask;
|
||||||
|
import org.briarproject.bramble.api.keyagreement.Payload;
|
||||||
|
import org.briarproject.bramble.api.keyagreement.PayloadEncoder;
|
||||||
|
import org.briarproject.bramble.api.keyagreement.PayloadParser;
|
||||||
|
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementAbortedEvent;
|
||||||
|
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementFailedEvent;
|
||||||
|
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementFinishedEvent;
|
||||||
|
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementListeningEvent;
|
||||||
|
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementStartedEvent;
|
||||||
|
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementWaitingEvent;
|
||||||
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
|
import org.briarproject.briar.R;
|
||||||
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
|
import org.briarproject.briar.android.fragment.BaseEventFragment;
|
||||||
|
import org.briarproject.briar.android.view.QrCodeView;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.inject.Provider;
|
||||||
|
|
||||||
|
import androidx.annotation.UiThread;
|
||||||
|
|
||||||
|
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
|
||||||
|
import static android.view.View.INVISIBLE;
|
||||||
|
import static android.view.View.VISIBLE;
|
||||||
|
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
|
||||||
|
import static android.widget.LinearLayout.HORIZONTAL;
|
||||||
|
import static android.widget.Toast.LENGTH_LONG;
|
||||||
|
import static java.util.logging.Level.INFO;
|
||||||
|
import static java.util.logging.Level.WARNING;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
|
||||||
|
@MethodsNotNullByDefault
|
||||||
|
@ParametersNotNullByDefault
|
||||||
|
public class KeyAgreementFragment extends BaseEventFragment
|
||||||
|
implements QrCodeDecoder.ResultCallback, QrCodeView.FullscreenListener {
|
||||||
|
|
||||||
|
static final String TAG = KeyAgreementFragment.class.getName();
|
||||||
|
|
||||||
|
private static final Logger LOG = Logger.getLogger(TAG);
|
||||||
|
@SuppressWarnings("CharsetObjectCanBeUsed") // Requires minSdkVersion >= 19
|
||||||
|
private static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Provider<KeyAgreementTask> keyAgreementTaskProvider;
|
||||||
|
@Inject
|
||||||
|
PayloadEncoder payloadEncoder;
|
||||||
|
@Inject
|
||||||
|
PayloadParser payloadParser;
|
||||||
|
@Inject
|
||||||
|
@IoExecutor
|
||||||
|
Executor ioExecutor;
|
||||||
|
@Inject
|
||||||
|
EventBus eventBus;
|
||||||
|
|
||||||
|
private CameraView cameraView;
|
||||||
|
private LinearLayout cameraOverlay;
|
||||||
|
private View statusView;
|
||||||
|
private QrCodeView qrCodeView;
|
||||||
|
private TextView status;
|
||||||
|
|
||||||
|
private boolean gotRemotePayload;
|
||||||
|
private volatile boolean gotLocalPayload;
|
||||||
|
private KeyAgreementTask task;
|
||||||
|
private KeyAgreementEventListener listener;
|
||||||
|
|
||||||
|
public static KeyAgreementFragment newInstance() {
|
||||||
|
Bundle args = new Bundle();
|
||||||
|
KeyAgreementFragment fragment = new KeyAgreementFragment();
|
||||||
|
fragment.setArguments(args);
|
||||||
|
return fragment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAttach(Context context) {
|
||||||
|
super.onAttach(context);
|
||||||
|
listener = (KeyAgreementEventListener) context;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void injectFragment(ActivityComponent component) {
|
||||||
|
component.inject(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUniqueTag() {
|
||||||
|
return TAG;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public View onCreateView(LayoutInflater inflater,
|
||||||
|
@Nullable ViewGroup container,
|
||||||
|
@Nullable Bundle savedInstanceState) {
|
||||||
|
return inflater.inflate(R.layout.fragment_keyagreement_qr, container,
|
||||||
|
false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
||||||
|
super.onViewCreated(view, savedInstanceState);
|
||||||
|
cameraView = view.findViewById(R.id.camera_view);
|
||||||
|
cameraOverlay = view.findViewById(R.id.camera_overlay);
|
||||||
|
statusView = view.findViewById(R.id.status_container);
|
||||||
|
status = view.findViewById(R.id.connect_status);
|
||||||
|
qrCodeView = view.findViewById(R.id.qr_code_view);
|
||||||
|
qrCodeView.setFullscreenListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
||||||
|
super.onActivityCreated(savedInstanceState);
|
||||||
|
requireActivity().setRequestedOrientation(SCREEN_ORIENTATION_NOSENSOR);
|
||||||
|
cameraView.setPreviewConsumer(new QrCodeDecoder(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
try {
|
||||||
|
cameraView.start();
|
||||||
|
} catch (CameraException e) {
|
||||||
|
logCameraExceptionAndFinish(e);
|
||||||
|
}
|
||||||
|
startListening();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setFullscreen(boolean fullscreen) {
|
||||||
|
LinearLayout.LayoutParams statusParams, qrCodeParams;
|
||||||
|
if (fullscreen) {
|
||||||
|
// Grow the QR code view to fill its parent
|
||||||
|
statusParams = new LayoutParams(0, 0, 0f);
|
||||||
|
qrCodeParams = new LayoutParams(MATCH_PARENT, MATCH_PARENT, 1f);
|
||||||
|
} else {
|
||||||
|
// Shrink the QR code view to fill half its parent
|
||||||
|
if (cameraOverlay.getOrientation() == HORIZONTAL) {
|
||||||
|
statusParams = new LayoutParams(0, MATCH_PARENT, 1f);
|
||||||
|
qrCodeParams = new LayoutParams(0, MATCH_PARENT, 1f);
|
||||||
|
} else {
|
||||||
|
statusParams = new LayoutParams(MATCH_PARENT, 0, 1f);
|
||||||
|
qrCodeParams = new LayoutParams(MATCH_PARENT, 0, 1f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
statusView.setLayoutParams(statusParams);
|
||||||
|
qrCodeView.setLayoutParams(qrCodeParams);
|
||||||
|
cameraOverlay.invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStop() {
|
||||||
|
super.onStop();
|
||||||
|
stopListening();
|
||||||
|
try {
|
||||||
|
cameraView.stop();
|
||||||
|
} catch (CameraException e) {
|
||||||
|
logCameraExceptionAndFinish(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void logCameraExceptionAndFinish(CameraException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
Toast.makeText(getActivity(), R.string.camera_error,
|
||||||
|
LENGTH_LONG).show();
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void startListening() {
|
||||||
|
KeyAgreementTask oldTask = task;
|
||||||
|
KeyAgreementTask newTask = keyAgreementTaskProvider.get();
|
||||||
|
task = newTask;
|
||||||
|
ioExecutor.execute(() -> {
|
||||||
|
if (oldTask != null) oldTask.stopListening();
|
||||||
|
newTask.listen();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void stopListening() {
|
||||||
|
KeyAgreementTask oldTask = task;
|
||||||
|
ioExecutor.execute(() -> {
|
||||||
|
if (oldTask != null) oldTask.stopListening();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void reset() {
|
||||||
|
// If we've stopped the camera view, restart it
|
||||||
|
if (gotRemotePayload) {
|
||||||
|
try {
|
||||||
|
cameraView.start();
|
||||||
|
} catch (CameraException e) {
|
||||||
|
logCameraExceptionAndFinish(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
statusView.setVisibility(INVISIBLE);
|
||||||
|
cameraView.setVisibility(VISIBLE);
|
||||||
|
gotRemotePayload = false;
|
||||||
|
gotLocalPayload = false;
|
||||||
|
startListening();
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void qrCodeScanned(String content) {
|
||||||
|
try {
|
||||||
|
byte[] payloadBytes = content.getBytes(ISO_8859_1);
|
||||||
|
if (LOG.isLoggable(INFO))
|
||||||
|
LOG.info("Remote payload is " + payloadBytes.length + " bytes");
|
||||||
|
Payload remotePayload = payloadParser.parse(payloadBytes);
|
||||||
|
gotRemotePayload = true;
|
||||||
|
cameraView.stop();
|
||||||
|
cameraView.setVisibility(INVISIBLE);
|
||||||
|
statusView.setVisibility(VISIBLE);
|
||||||
|
status.setText(R.string.connecting_to_device);
|
||||||
|
task.connectAndRunProtocol(remotePayload);
|
||||||
|
} catch (UnsupportedVersionException e) {
|
||||||
|
reset();
|
||||||
|
String msg;
|
||||||
|
if (e.isTooOld()) {
|
||||||
|
msg = getString(R.string.qr_code_too_old,
|
||||||
|
getString(R.string.app_name));
|
||||||
|
} else {
|
||||||
|
msg = getString(R.string.qr_code_too_new,
|
||||||
|
getString(R.string.app_name));
|
||||||
|
}
|
||||||
|
showNextFragment(ContactExchangeErrorFragment.newInstance(msg));
|
||||||
|
} catch (CameraException e) {
|
||||||
|
logCameraExceptionAndFinish(e);
|
||||||
|
} catch (IOException | IllegalArgumentException e) {
|
||||||
|
LOG.log(WARNING, "QR Code Invalid", e);
|
||||||
|
reset();
|
||||||
|
Toast.makeText(getActivity(), R.string.qr_code_invalid,
|
||||||
|
LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void eventOccurred(Event e) {
|
||||||
|
if (e instanceof KeyAgreementListeningEvent) {
|
||||||
|
KeyAgreementListeningEvent event = (KeyAgreementListeningEvent) e;
|
||||||
|
gotLocalPayload = true;
|
||||||
|
setQrCode(event.getLocalPayload());
|
||||||
|
} else if (e instanceof KeyAgreementFailedEvent) {
|
||||||
|
keyAgreementFailed();
|
||||||
|
} else if (e instanceof KeyAgreementWaitingEvent) {
|
||||||
|
keyAgreementWaiting();
|
||||||
|
} else if (e instanceof KeyAgreementStartedEvent) {
|
||||||
|
keyAgreementStarted();
|
||||||
|
} else if (e instanceof KeyAgreementAbortedEvent) {
|
||||||
|
KeyAgreementAbortedEvent event = (KeyAgreementAbortedEvent) e;
|
||||||
|
keyAgreementAborted(event.didRemoteAbort());
|
||||||
|
} else if (e instanceof KeyAgreementFinishedEvent) {
|
||||||
|
keyAgreementFinished(((KeyAgreementFinishedEvent) e).getResult());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void keyAgreementFailed() {
|
||||||
|
reset();
|
||||||
|
listener.keyAgreementFailed();
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void keyAgreementWaiting() {
|
||||||
|
status.setText(listener.keyAgreementWaiting());
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void keyAgreementStarted() {
|
||||||
|
qrCodeView.setVisibility(INVISIBLE);
|
||||||
|
statusView.setVisibility(VISIBLE);
|
||||||
|
status.setText(listener.keyAgreementStarted());
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void keyAgreementAborted(boolean remoteAborted) {
|
||||||
|
reset();
|
||||||
|
listener.keyAgreementAborted(remoteAborted);
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
private void keyAgreementFinished(KeyAgreementResult result) {
|
||||||
|
statusView.setVisibility(VISIBLE);
|
||||||
|
status.setText(listener.keyAgreementFinished(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setQrCode(Payload localPayload) {
|
||||||
|
Context context = getContext();
|
||||||
|
if (context == null) return;
|
||||||
|
DisplayMetrics dm = context.getResources().getDisplayMetrics();
|
||||||
|
ioExecutor.execute(() -> {
|
||||||
|
byte[] payloadBytes = payloadEncoder.encode(localPayload);
|
||||||
|
if (LOG.isLoggable(INFO)) {
|
||||||
|
LOG.info("Local payload is " + payloadBytes.length
|
||||||
|
+ " bytes");
|
||||||
|
}
|
||||||
|
// Use ISO 8859-1 to encode bytes directly as a string
|
||||||
|
String content = new String(payloadBytes, ISO_8859_1);
|
||||||
|
Bitmap qrCode = QrCodeUtils.createQrCode(dm, content);
|
||||||
|
runOnUiThreadUnlessDestroyed(() -> qrCodeView.setQrCode(qrCode));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleResult(Result result) {
|
||||||
|
runOnUiThreadUnlessDestroyed(() -> {
|
||||||
|
LOG.info("Got result from decoder");
|
||||||
|
// Ignore results until the KeyAgreementTask is ready
|
||||||
|
if (!gotLocalPayload) return;
|
||||||
|
if (!gotRemotePayload) qrCodeScanned(result.getText());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void finish() {
|
||||||
|
getActivity().getSupportFragmentManager().popBackStack();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
|
interface KeyAgreementEventListener {
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
void keyAgreementFailed();
|
||||||
|
|
||||||
|
// Should return a string to be displayed as status.
|
||||||
|
@UiThread
|
||||||
|
@Nullable
|
||||||
|
String keyAgreementWaiting();
|
||||||
|
|
||||||
|
// Should return a string to be displayed as status.
|
||||||
|
@UiThread
|
||||||
|
@Nullable
|
||||||
|
String keyAgreementStarted();
|
||||||
|
|
||||||
|
// Will show an error fragment.
|
||||||
|
@UiThread
|
||||||
|
void keyAgreementAborted(boolean remoteAborted);
|
||||||
|
|
||||||
|
// Should return a string to be displayed as status.
|
||||||
|
@UiThread
|
||||||
|
@Nullable
|
||||||
|
String keyAgreementFinished(KeyAgreementResult result);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.briarproject.briar.android.contact.add.nearby;
|
package org.briarproject.briar.android.keyagreement;
|
||||||
|
|
||||||
import android.hardware.Camera;
|
import android.hardware.Camera;
|
||||||
|
|
||||||
@@ -6,6 +6,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|||||||
|
|
||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.UiThread;
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
interface PreviewConsumer {
|
interface PreviewConsumer {
|
||||||
|
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
package org.briarproject.briar.android.contact.add.nearby;
|
package org.briarproject.briar.android.keyagreement;
|
||||||
|
|
||||||
import android.hardware.Camera;
|
import android.hardware.Camera;
|
||||||
import android.hardware.Camera.CameraInfo;
|
import android.hardware.Camera.CameraInfo;
|
||||||
import android.hardware.Camera.PreviewCallback;
|
import android.hardware.Camera.PreviewCallback;
|
||||||
import android.hardware.Camera.Size;
|
import android.hardware.Camera.Size;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
|
||||||
import com.google.zxing.BinaryBitmap;
|
import com.google.zxing.BinaryBitmap;
|
||||||
import com.google.zxing.LuminanceSource;
|
import com.google.zxing.LuminanceSource;
|
||||||
@@ -14,13 +15,10 @@ import com.google.zxing.Result;
|
|||||||
import com.google.zxing.common.HybridBinarizer;
|
import com.google.zxing.common.HybridBinarizer;
|
||||||
import com.google.zxing.qrcode.QRCodeReader;
|
import com.google.zxing.qrcode.QRCodeReader;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.UiThread;
|
||||||
@@ -28,26 +26,22 @@ import androidx.annotation.UiThread;
|
|||||||
import static com.google.zxing.DecodeHintType.CHARACTER_SET;
|
import static com.google.zxing.DecodeHintType.CHARACTER_SET;
|
||||||
import static java.util.Collections.singletonMap;
|
import static java.util.Collections.singletonMap;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
public class QrCodeDecoder implements PreviewConsumer, PreviewCallback {
|
public class QrCodeDecoder implements PreviewConsumer, PreviewCallback {
|
||||||
|
|
||||||
private static final Logger LOG = getLogger(QrCodeDecoder.class.getName());
|
private static final Logger LOG =
|
||||||
|
Logger.getLogger(QrCodeDecoder.class.getName());
|
||||||
|
|
||||||
private final AndroidExecutor androidExecutor;
|
|
||||||
private final Executor ioExecutor;
|
|
||||||
private final Reader reader = new QRCodeReader();
|
private final Reader reader = new QRCodeReader();
|
||||||
private final ResultCallback callback;
|
private final ResultCallback callback;
|
||||||
|
|
||||||
private Camera camera = null;
|
private Camera camera = null;
|
||||||
private int cameraIndex = 0;
|
private int cameraIndex = 0;
|
||||||
|
|
||||||
public QrCodeDecoder(AndroidExecutor androidExecutor,
|
public QrCodeDecoder(ResultCallback callback) {
|
||||||
@IoExecutor Executor ioExecutor, ResultCallback callback) {
|
|
||||||
this.androidExecutor = androidExecutor;
|
|
||||||
this.ioExecutor = ioExecutor;
|
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +74,8 @@ public class QrCodeDecoder implements PreviewConsumer, PreviewCallback {
|
|||||||
if (data.length == size.width * size.height * 3 / 2) {
|
if (data.length == size.width * size.height * 3 / 2) {
|
||||||
CameraInfo info = new CameraInfo();
|
CameraInfo info = new CameraInfo();
|
||||||
Camera.getCameraInfo(cameraIndex, info);
|
Camera.getCameraInfo(cameraIndex, info);
|
||||||
decode(data, size.width, size.height, info.orientation);
|
new DecoderTask(data, size.width, size.height,
|
||||||
|
info.orientation).execute();
|
||||||
} else {
|
} else {
|
||||||
// Camera parameters have changed - ask for a new preview
|
// Camera parameters have changed - ask for a new preview
|
||||||
LOG.info("Preview size does not match camera parameters");
|
LOG.info("Preview size does not match camera parameters");
|
||||||
@@ -94,23 +89,43 @@ public class QrCodeDecoder implements PreviewConsumer, PreviewCallback {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void decode(byte[] data, int width, int height, int orientation) {
|
private class DecoderTask extends AsyncTask<Void, Void, Void> {
|
||||||
ioExecutor.execute(() -> {
|
|
||||||
|
private final byte[] data;
|
||||||
|
private final int width, height, orientation;
|
||||||
|
|
||||||
|
private DecoderTask(byte[] data, int width, int height,
|
||||||
|
int orientation) {
|
||||||
|
this.data = data;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
this.orientation = orientation;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(Void... params) {
|
||||||
BinaryBitmap bitmap = binarize(data, width, height, orientation);
|
BinaryBitmap bitmap = binarize(data, width, height, orientation);
|
||||||
Result result;
|
Result result;
|
||||||
try {
|
try {
|
||||||
result = reader.decode(bitmap,
|
result = reader.decode(bitmap,
|
||||||
singletonMap(CHARACTER_SET, "ISO8859_1"));
|
singletonMap(CHARACTER_SET, "ISO8859_1"));
|
||||||
callback.onQrCodeDecoded(result);
|
|
||||||
} catch (ReaderException e) {
|
} catch (ReaderException e) {
|
||||||
// No barcode found
|
// No barcode found
|
||||||
|
return null;
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
LOG.warning("Invalid preview frame");
|
LOG.warning("Invalid preview frame");
|
||||||
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
reader.reset();
|
reader.reset();
|
||||||
androidExecutor.runOnUiThread(this::askForPreviewFrame);
|
|
||||||
}
|
}
|
||||||
});
|
callback.handleResult(result);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(Void result) {
|
||||||
|
askForPreviewFrame();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BinaryBitmap binarize(byte[] data, int width, int height,
|
private static BinaryBitmap binarize(byte[] data, int width, int height,
|
||||||
@@ -128,7 +143,7 @@ public class QrCodeDecoder implements PreviewConsumer, PreviewCallback {
|
|||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public interface ResultCallback {
|
public interface ResultCallback {
|
||||||
@IoExecutor
|
|
||||||
void onQrCodeDecoded(Result result);
|
void handleResult(Result result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.briarproject.briar.android.contact.add.nearby;
|
package org.briarproject.briar.android.keyagreement;
|
||||||
|
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.util.DisplayMetrics;
|
import android.util.DisplayMetrics;
|
||||||
@@ -18,13 +18,13 @@ import static android.graphics.Color.BLACK;
|
|||||||
import static android.graphics.Color.WHITE;
|
import static android.graphics.Color.WHITE;
|
||||||
import static com.google.zxing.BarcodeFormat.QR_CODE;
|
import static com.google.zxing.BarcodeFormat.QR_CODE;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class QrCodeUtils {
|
public class QrCodeUtils {
|
||||||
|
|
||||||
private static final Logger LOG = getLogger(QrCodeUtils.class.getName());
|
private static final Logger LOG =
|
||||||
|
Logger.getLogger(QrCodeUtils.class.getName());
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static Bitmap createQrCode(DisplayMetrics dm, String input) {
|
public static Bitmap createQrCode(DisplayMetrics dm, String input) {
|
||||||
@@ -49,12 +49,14 @@ import androidx.annotation.DrawableRes;
|
|||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.StringRes;
|
import androidx.annotation.StringRes;
|
||||||
|
import androidx.appcompat.app.ActionBar;
|
||||||
import androidx.appcompat.app.ActionBarDrawerToggle;
|
import androidx.appcompat.app.ActionBarDrawerToggle;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.drawerlayout.widget.DrawerLayout;
|
import androidx.drawerlayout.widget.DrawerLayout;
|
||||||
import androidx.fragment.app.FragmentManager;
|
import androidx.fragment.app.FragmentManager;
|
||||||
|
import androidx.fragment.app.FragmentTransaction;
|
||||||
import androidx.lifecycle.MutableLiveData;
|
import androidx.lifecycle.MutableLiveData;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
import androidx.lifecycle.ViewModelProviders;
|
import androidx.lifecycle.ViewModelProviders;
|
||||||
@@ -65,7 +67,9 @@ import static android.view.View.GONE;
|
|||||||
import static android.view.View.VISIBLE;
|
import static android.view.View.VISIBLE;
|
||||||
import static androidx.core.view.GravityCompat.START;
|
import static androidx.core.view.GravityCompat.START;
|
||||||
import static androidx.drawerlayout.widget.DrawerLayout.LOCK_MODE_LOCKED_CLOSED;
|
import static androidx.drawerlayout.widget.DrawerLayout.LOCK_MODE_LOCKED_CLOSED;
|
||||||
|
import static androidx.fragment.app.FragmentManager.POP_BACK_STACK_INCLUSIVE;
|
||||||
import static androidx.lifecycle.Lifecycle.State.STARTED;
|
import static androidx.lifecycle.Lifecycle.State.STARTED;
|
||||||
|
import static java.util.Objects.requireNonNull;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.RUNNING;
|
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.RUNNING;
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||||
@@ -142,7 +146,7 @@ public class NavDrawerActivity extends BriarActivity implements
|
|||||||
if (ask) showDozeDialog(getString(R.string.setup_doze_intro));
|
if (ask) showDozeDialog(getString(R.string.setup_doze_intro));
|
||||||
});
|
});
|
||||||
|
|
||||||
Toolbar toolbar = setUpCustomToolbar(false);
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||||
drawerLayout = findViewById(R.id.drawer_layout);
|
drawerLayout = findViewById(R.id.drawer_layout);
|
||||||
navigation = findViewById(R.id.navigation);
|
navigation = findViewById(R.id.navigation);
|
||||||
GridView transportsView = findViewById(R.id.transportsView);
|
GridView transportsView = findViewById(R.id.transportsView);
|
||||||
@@ -152,6 +156,11 @@ public class NavDrawerActivity extends BriarActivity implements
|
|||||||
startActivity(new Intent(this, TransportsActivity.class));
|
startActivity(new Intent(this, TransportsActivity.class));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setSupportActionBar(toolbar);
|
||||||
|
ActionBar actionBar = requireNonNull(getSupportActionBar());
|
||||||
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||||
|
actionBar.setHomeButtonEnabled(true);
|
||||||
|
|
||||||
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
|
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
|
||||||
R.string.nav_drawer_open_description,
|
R.string.nav_drawer_open_description,
|
||||||
R.string.nav_drawer_close_description) {
|
R.string.nav_drawer_close_description) {
|
||||||
@@ -175,6 +184,9 @@ public class NavDrawerActivity extends BriarActivity implements
|
|||||||
|
|
||||||
if (lifecycleManager.getLifecycleState().isAfter(RUNNING)) {
|
if (lifecycleManager.getLifecycleState().isAfter(RUNNING)) {
|
||||||
showSignOutFragment();
|
showSignOutFragment();
|
||||||
|
} else if (state == null) {
|
||||||
|
startFragment(ContactListFragment.newInstance(),
|
||||||
|
R.id.nav_btn_contacts);
|
||||||
}
|
}
|
||||||
if (state == null) {
|
if (state == null) {
|
||||||
// do not call this again when there's existing state
|
// do not call this again when there's existing state
|
||||||
@@ -264,6 +276,7 @@ public class NavDrawerActivity extends BriarActivity implements
|
|||||||
@Override
|
@Override
|
||||||
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
||||||
drawerLayout.closeDrawer(START);
|
drawerLayout.closeDrawer(START);
|
||||||
|
clearBackStack();
|
||||||
if (item.getItemId() == R.id.nav_btn_lock) {
|
if (item.getItemId() == R.id.nav_btn_lock) {
|
||||||
lockManager.setLocked(true);
|
lockManager.setLocked(true);
|
||||||
ActivityCompat.finishAfterTransition(this);
|
ActivityCompat.finishAfterTransition(this);
|
||||||
@@ -283,8 +296,8 @@ public class NavDrawerActivity extends BriarActivity implements
|
|||||||
FragmentManager fm = getSupportFragmentManager();
|
FragmentManager fm = getSupportFragmentManager();
|
||||||
if (fm.findFragmentByTag(SignOutFragment.TAG) != null) {
|
if (fm.findFragmentByTag(SignOutFragment.TAG) != null) {
|
||||||
finish();
|
finish();
|
||||||
} else if (fm.getBackStackEntryCount() == 0 &&
|
} else if (fm.getBackStackEntryCount() == 0
|
||||||
fm.findFragmentByTag(ContactListFragment.TAG) == null) {
|
&& fm.findFragmentByTag(ContactListFragment.TAG) == null) {
|
||||||
// don't start fragments in the wrong part of lifecycle (#1904)
|
// don't start fragments in the wrong part of lifecycle (#1904)
|
||||||
if (!getLifecycle().getCurrentState().isAtLeast(STARTED)) {
|
if (!getLifecycle().getCurrentState().isAtLeast(STARTED)) {
|
||||||
LOG.warning("Tried to start contacts fragment in state " +
|
LOG.warning("Tried to start contacts fragment in state " +
|
||||||
@@ -333,12 +346,30 @@ public class NavDrawerActivity extends BriarActivity implements
|
|||||||
startFragment(fragment);
|
startFragment(fragment);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startFragment(BaseFragment f) {
|
private void startFragment(BaseFragment fragment) {
|
||||||
getSupportFragmentManager().beginTransaction()
|
if (getSupportFragmentManager().getBackStackEntryCount() == 0)
|
||||||
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out,
|
startFragment(fragment, false);
|
||||||
R.anim.fade_in, R.anim.fade_out)
|
else startFragment(fragment, true);
|
||||||
.replace(R.id.fragmentContainer, f, f.getUniqueTag())
|
}
|
||||||
.commit();
|
|
||||||
|
private void startFragment(BaseFragment fragment,
|
||||||
|
boolean isAddedToBackStack) {
|
||||||
|
FragmentTransaction trans =
|
||||||
|
getSupportFragmentManager().beginTransaction()
|
||||||
|
.setCustomAnimations(R.anim.fade_in,
|
||||||
|
R.anim.fade_out, R.anim.fade_in,
|
||||||
|
R.anim.fade_out)
|
||||||
|
.replace(R.id.fragmentContainer, fragment,
|
||||||
|
fragment.getUniqueTag());
|
||||||
|
if (isAddedToBackStack) {
|
||||||
|
trans.addToBackStack(fragment.getUniqueTag());
|
||||||
|
}
|
||||||
|
trans.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void clearBackStack() {
|
||||||
|
getSupportFragmentManager().popBackStackImmediate(null,
|
||||||
|
POP_BACK_STACK_INCLUSIVE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ 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.GroupId;
|
import org.briarproject.bramble.api.sync.GroupId;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||||
import org.briarproject.briar.android.contactselection.BaseContactSelectorFragment;
|
import org.briarproject.briar.android.contactselection.BaseContactSelectorFragment;
|
||||||
import org.briarproject.briar.android.contactselection.ContactSelectorController;
|
import org.briarproject.briar.android.contactselection.ContactSelectorController;
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import android.view.ViewGroup;
|
|||||||
import org.briarproject.bramble.api.contact.ContactId;
|
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.OnContactClickListener;
|
|
||||||
import org.briarproject.briar.android.contactselection.BaseContactSelectorAdapter;
|
import org.briarproject.briar.android.contactselection.BaseContactSelectorAdapter;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import android.widget.ImageView;
|
|||||||
|
|
||||||
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.OnContactClickListener;
|
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||||
import org.briarproject.briar.android.contactselection.BaseSelectableContactHolder;
|
import org.briarproject.briar.android.contactselection.BaseSelectableContactHolder;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|||||||
@@ -9,13 +9,10 @@ import android.view.View;
|
|||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
|
||||||
|
|
||||||
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;
|
||||||
import org.briarproject.briar.android.activity.BaseActivity;
|
import org.briarproject.briar.android.activity.BaseActivity;
|
||||||
import org.briarproject.briar.android.conversation.glide.GlideApp;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
@@ -35,7 +32,7 @@ public class ConfirmAvatarDialogFragment extends DialogFragment {
|
|||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ViewModelProvider.Factory viewModelFactory;
|
ViewModelProvider.Factory viewModelFactory;
|
||||||
private SettingsViewModel viewModel;
|
private SettingsViewModel settingsViewModel;
|
||||||
|
|
||||||
private static final String ARG_URI = "uri";
|
private static final String ARG_URI = "uri";
|
||||||
private Uri uri;
|
private Uri uri;
|
||||||
@@ -54,9 +51,6 @@ public class ConfirmAvatarDialogFragment extends DialogFragment {
|
|||||||
public void onAttach(Context ctx) {
|
public void onAttach(Context ctx) {
|
||||||
super.onAttach(ctx);
|
super.onAttach(ctx);
|
||||||
((BaseActivity) requireActivity()).getActivityComponent().inject(this);
|
((BaseActivity) requireActivity()).getActivityComponent().inject(this);
|
||||||
ViewModelProvider provider =
|
|
||||||
new ViewModelProvider(requireActivity(), viewModelFactory);
|
|
||||||
viewModel = provider.get(SettingsViewModel.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -66,34 +60,32 @@ public class ConfirmAvatarDialogFragment extends DialogFragment {
|
|||||||
uri = Uri.parse(argUri);
|
uri = Uri.parse(argUri);
|
||||||
|
|
||||||
FragmentActivity activity = requireActivity();
|
FragmentActivity activity = requireActivity();
|
||||||
LayoutInflater inflater = LayoutInflater.from(activity);
|
|
||||||
|
ViewModelProvider provider =
|
||||||
|
new ViewModelProvider(activity, viewModelFactory);
|
||||||
|
settingsViewModel = provider.get(SettingsViewModel.class);
|
||||||
|
|
||||||
|
AlertDialog.Builder builder =
|
||||||
|
new AlertDialog.Builder(activity, R.style.BriarDialogTheme);
|
||||||
|
|
||||||
|
LayoutInflater inflater = LayoutInflater.from(getContext());
|
||||||
final View view =
|
final View view =
|
||||||
inflater.inflate(R.layout.fragment_confirm_avatar_dialog, null);
|
inflater.inflate(R.layout.fragment_confirm_avatar_dialog, null);
|
||||||
|
builder.setView(view);
|
||||||
|
|
||||||
|
builder.setTitle(R.string.dialog_confirm_profile_picture_title);
|
||||||
|
builder.setNegativeButton(R.string.cancel, null);
|
||||||
|
builder.setPositiveButton(R.string.change,
|
||||||
|
(dialog, id) -> settingsViewModel.setAvatar(uri));
|
||||||
|
|
||||||
ImageView imageView = view.findViewById(R.id.image);
|
ImageView imageView = view.findViewById(R.id.image);
|
||||||
|
imageView.setImageURI(uri);
|
||||||
|
|
||||||
TextView textViewUserName = view.findViewById(R.id.username);
|
TextView textViewUserName = view.findViewById(R.id.username);
|
||||||
|
settingsViewModel.getOwnIdentityInfo().observe(activity,
|
||||||
|
us -> textViewUserName.setText(us.getLocalAuthor().getName()));
|
||||||
|
|
||||||
GlideApp.with(imageView)
|
return builder.create();
|
||||||
.load(uri)
|
|
||||||
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
|
||||||
.error(R.drawable.ic_image_broken)
|
|
||||||
.into(imageView)
|
|
||||||
.waitForLayout();
|
|
||||||
|
|
||||||
// we can't use getViewLifecycleOwner() here
|
|
||||||
// as this fragment technically doesn't have a view
|
|
||||||
viewModel.getOwnIdentityInfo().observe(activity, us ->
|
|
||||||
textViewUserName.setText(us.getLocalAuthor().getName())
|
|
||||||
);
|
|
||||||
|
|
||||||
int theme = R.style.BriarDialogTheme;
|
|
||||||
return new AlertDialog.Builder(activity, theme)
|
|
||||||
.setView(view)
|
|
||||||
.setTitle(R.string.dialog_confirm_profile_picture_title)
|
|
||||||
.setNegativeButton(R.string.cancel, null)
|
|
||||||
.setPositiveButton(R.string.change, (d, id) ->
|
|
||||||
viewModel.setAvatar(uri)
|
|
||||||
)
|
|
||||||
.create();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ class SettingsViewModel extends AndroidViewModel {
|
|||||||
return ownIdentityInfo;
|
return ownIdentityInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
LiveEvent<Boolean> getSetAvatarFailed() {
|
public LiveEvent<Boolean> getSetAvatarFailed() {
|
||||||
return setAvatarFailed;
|
return setAvatarFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user