mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 02:39:05 +01:00
Compare commits
200 Commits
remote-con
...
beta-1.2.4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75dfa80541 | ||
|
|
41b59fbcfe | ||
|
|
98a4f5def1 | ||
|
|
aeefa35f38 | ||
|
|
4e7f33edfd | ||
|
|
f1e957ffed | ||
|
|
9e3fed6bc0 | ||
|
|
bf9a39cc6c | ||
|
|
72aa5397f8 | ||
|
|
21eaab3259 | ||
|
|
92d595da35 | ||
|
|
5e85566fc3 | ||
|
|
1574bf35fc | ||
|
|
533e01e881 | ||
|
|
383367f0c8 | ||
|
|
ca052ea7dd | ||
|
|
5147f6b7e6 | ||
|
|
84a8ff1dd8 | ||
|
|
6c489fbea3 | ||
|
|
c7200910c9 | ||
|
|
663e5c4b46 | ||
|
|
529eaceec7 | ||
|
|
f516dbe34f | ||
|
|
5b515d7e18 | ||
|
|
ef04a26cfc | ||
|
|
2e6fe42074 | ||
|
|
124e2f99b0 | ||
|
|
190a6bff96 | ||
|
|
01df141c08 | ||
|
|
d7c9bf80de | ||
|
|
3a5e51e248 | ||
|
|
a76e3dcec1 | ||
|
|
0fdc7199ed | ||
|
|
248f482fee | ||
|
|
4196d046a3 | ||
|
|
722ebb22f6 | ||
|
|
a4f561ca1a | ||
|
|
c7db0bf6fa | ||
|
|
ca6f458551 | ||
|
|
c85990408a | ||
|
|
3ed0204170 | ||
|
|
e2b3340734 | ||
|
|
78aac8de52 | ||
|
|
971ae3a20e | ||
|
|
622e7a775a | ||
|
|
103e8482b0 | ||
|
|
ddcfc11012 | ||
|
|
ab2e40abde | ||
|
|
1ddceaadd6 | ||
|
|
7a644f7d8b | ||
|
|
397afbfec0 | ||
|
|
0d4cb05ac0 | ||
|
|
aa0937e6aa | ||
|
|
4bf8d4c0e7 | ||
|
|
75fcd28071 | ||
|
|
5f29ab3b40 | ||
|
|
f45d00e23c | ||
|
|
2b589c2da6 | ||
|
|
67d15ec82e | ||
|
|
2d44d749ba | ||
|
|
6ef86c5638 | ||
|
|
131f9b9696 | ||
|
|
a876d4cfb7 | ||
|
|
fafcacf808 | ||
|
|
7a0d990f0b | ||
|
|
234bdf686e | ||
|
|
edb9da107f | ||
|
|
d1d4914c6a | ||
|
|
9261d23bba | ||
|
|
f4febe90c9 | ||
|
|
ecd766b204 | ||
|
|
ca4fc2dc26 | ||
|
|
c3ddcdffe0 | ||
|
|
2e37619357 | ||
|
|
c247d745df | ||
|
|
3a4de3d2cb | ||
|
|
04f1036dbf | ||
|
|
9736f9d31f | ||
|
|
440d5239b1 | ||
|
|
e4a8b10b94 | ||
|
|
41676065c5 | ||
|
|
1fcc83a0d0 | ||
|
|
249b85cd26 | ||
|
|
a23e0699d8 | ||
|
|
e3e47dae48 | ||
|
|
9660ff2fff | ||
|
|
ea810c817b | ||
|
|
876d50975e | ||
|
|
bf5bdc52b4 | ||
|
|
29320c410e | ||
|
|
d41472a18c | ||
|
|
c411065255 | ||
|
|
3ac5646355 | ||
|
|
c46fdce277 | ||
|
|
643ef593e1 | ||
|
|
eda17449be | ||
|
|
28f82a1507 | ||
|
|
8734825346 | ||
|
|
640f3d63b0 | ||
|
|
b1dfd867f0 | ||
|
|
ff76900d74 | ||
|
|
945fdb8ee4 | ||
|
|
53fe3e1592 | ||
|
|
be76c5b7db | ||
|
|
909e946e58 | ||
|
|
408d9ddee4 | ||
|
|
0e5027e725 | ||
|
|
2d4c97a69e | ||
|
|
7d62ae5fa8 | ||
|
|
bd616853cf | ||
|
|
32e1d6c748 | ||
|
|
6b022afa67 | ||
|
|
e8b454b25b | ||
|
|
54c05b5ffe | ||
|
|
d145a082f5 | ||
|
|
4fd012c31a | ||
|
|
95d06770bf | ||
|
|
428247b7b2 | ||
|
|
a921361a56 | ||
|
|
fe7dfa721e | ||
|
|
92eb06a9e9 | ||
|
|
5beed1a748 | ||
|
|
774047d856 | ||
|
|
fc28e7aa88 | ||
|
|
78459499b2 | ||
|
|
c2973608d7 | ||
|
|
be1c33cb42 | ||
|
|
c955466bda | ||
|
|
593a0c4632 | ||
|
|
ed20b2d8d6 | ||
|
|
34583e6d2d | ||
|
|
ea5a862242 | ||
|
|
9ab9e02f8a | ||
|
|
3f70ae3c8c | ||
|
|
3f60098099 | ||
|
|
e965021e3d | ||
|
|
7d9380d3d6 | ||
|
|
3c8c0e579e | ||
|
|
bd2bbe9268 | ||
|
|
89d24b1753 | ||
|
|
861dbe20b1 | ||
|
|
197800de8b | ||
|
|
07e824ad68 | ||
|
|
d210215bd1 | ||
|
|
00705447ec | ||
|
|
9095ccef85 | ||
|
|
3196204094 | ||
|
|
2bae639105 | ||
|
|
f73d298752 | ||
|
|
bc3a443276 | ||
|
|
2a29d33303 | ||
|
|
30e0be9f43 | ||
|
|
3828d16971 | ||
|
|
a54eb64eb5 | ||
|
|
ad2d3e70d6 | ||
|
|
1f91842c52 | ||
|
|
c07a0a2fd7 | ||
|
|
4ee4905e06 | ||
|
|
67b7517f2b | ||
|
|
cd3174a643 | ||
|
|
9d9bc4ca84 | ||
|
|
7358091699 | ||
|
|
11eefaedcf | ||
|
|
bb5a6c0241 | ||
|
|
70d29af2ba | ||
|
|
baedb14e2b | ||
|
|
2796926709 | ||
|
|
fc6275b037 | ||
|
|
f76f9be4ed | ||
|
|
6167ba5c46 | ||
|
|
55f4600a69 | ||
|
|
c73801c7e8 | ||
|
|
249e1e28fe | ||
|
|
f0cea28aeb | ||
|
|
32e8ea9888 | ||
|
|
5a1caed89f | ||
|
|
22f5c42fc1 | ||
|
|
aab46040a5 | ||
|
|
18fd238aa1 | ||
|
|
3a837b3c5a | ||
|
|
ac2597865c | ||
|
|
4a67cf3ce7 | ||
|
|
a5041e651e | ||
|
|
b0e97d787f | ||
|
|
0d8af780a3 | ||
|
|
9c20e6b333 | ||
|
|
ab14976c96 | ||
|
|
ec3f821ba6 | ||
|
|
1d546da781 | ||
|
|
f2c951b70b | ||
|
|
1e259c100d | ||
|
|
3636aeba9a | ||
|
|
132e20a6ce | ||
|
|
c228e5c219 | ||
|
|
ae1d1fc5a7 | ||
|
|
37f02a40e9 | ||
|
|
3c8b8c39e1 | ||
|
|
8f839e2c30 | ||
|
|
da4b63f20f | ||
|
|
cd40e771d2 |
80
.idea/codeStyles/Project.xml
generated
80
.idea/codeStyles/Project.xml
generated
@@ -1,16 +1,7 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<code_scheme name="Project" version="173">
|
||||
<option name="RIGHT_MARGIN" value="100" />
|
||||
<AndroidXmlCodeStyleSettings>
|
||||
<option name="USE_CUSTOM_SETTINGS" value="true" />
|
||||
</AndroidXmlCodeStyleSettings>
|
||||
<JavaCodeStyleSettings>
|
||||
<option name="ANNOTATION_PARAMETER_WRAP" value="1" />
|
||||
<option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="99" />
|
||||
<option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="99" />
|
||||
<option name="PACKAGES_TO_USE_IMPORT_ON_DEMAND">
|
||||
<value />
|
||||
</option>
|
||||
<option name="IMPORT_LAYOUT_TABLE">
|
||||
<value>
|
||||
<package name="android" withSubpackages="true" static="false" />
|
||||
@@ -77,7 +68,6 @@
|
||||
</indentOptions>
|
||||
</codeStyleSettings>
|
||||
<codeStyleSettings language="XML">
|
||||
<option name="FORCE_REARRANGE_MODE" value="1" />
|
||||
<indentOptions>
|
||||
<option name="CONTINUATION_INDENT_SIZE" value="4" />
|
||||
<option name="USE_TAB_CHARACTER" value="true" />
|
||||
@@ -90,7 +80,8 @@
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>xmlns:android</NAME>
|
||||
<XML_NAMESPACE>Namespace:</XML_NAMESPACE>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
</rule>
|
||||
@@ -100,7 +91,8 @@
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>xmlns:.*</NAME>
|
||||
<XML_NAMESPACE>Namespace:</XML_NAMESPACE>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
<order>BY_NAME</order>
|
||||
@@ -111,6 +103,7 @@
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*:id</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
@@ -121,6 +114,7 @@
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*:name</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
@@ -131,6 +125,7 @@
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>name</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
@@ -141,6 +136,7 @@
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>style</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
@@ -151,6 +147,7 @@
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
@@ -161,64 +158,12 @@
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*:layout_width</NAME>
|
||||
<NAME>.*</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
</rule>
|
||||
</section>
|
||||
<section>
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*:layout_height</NAME>
|
||||
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
</rule>
|
||||
</section>
|
||||
<section>
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*:layout_.*</NAME>
|
||||
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
<order>BY_NAME</order>
|
||||
</rule>
|
||||
</section>
|
||||
<section>
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*:width</NAME>
|
||||
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
<order>BY_NAME</order>
|
||||
</rule>
|
||||
</section>
|
||||
<section>
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*:height</NAME>
|
||||
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
<order>BY_NAME</order>
|
||||
</rule>
|
||||
</section>
|
||||
<section>
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*</NAME>
|
||||
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
<order>BY_NAME</order>
|
||||
<order>ANDROID_ATTRIBUTE_ORDER</order>
|
||||
</rule>
|
||||
</section>
|
||||
<section>
|
||||
@@ -226,6 +171,7 @@
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>.*</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
|
||||
@@ -9,10 +9,10 @@ android {
|
||||
buildToolsVersion '28.0.3'
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 14
|
||||
targetSdkVersion 26
|
||||
versionCode 10107
|
||||
versionName "1.1.7"
|
||||
minSdkVersion 16
|
||||
targetSdkVersion 28
|
||||
versionCode 10204
|
||||
versionName "1.2.4"
|
||||
consumerProguardFiles 'proguard-rules.txt'
|
||||
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
@@ -30,8 +30,8 @@ configurations {
|
||||
|
||||
dependencies {
|
||||
implementation project(path: ':bramble-core', configuration: 'default')
|
||||
tor 'org.briarproject:tor-android:0.3.5.8@zip'
|
||||
tor 'org.briarproject:obfs4proxy-android:0.0.9@zip'
|
||||
tor 'org.briarproject:tor-android:0.3.5.8-64@zip'
|
||||
tor 'org.briarproject:obfs4proxy-android:0.0.11-2@zip'
|
||||
|
||||
annotationProcessor 'com.google.dagger:dagger-compiler:2.22.1'
|
||||
|
||||
@@ -59,6 +59,8 @@ task unpackTorBinaries {
|
||||
copy {
|
||||
from configurations.tor.collect { zipTree(it) }
|
||||
into torBinariesDir
|
||||
// TODO: Remove after next Tor upgrade, which won't include non-PIE binaries
|
||||
include 'geoip.zip', '*_pie.zip'
|
||||
}
|
||||
}
|
||||
dependsOn cleanTorBinaries
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.briarproject.bramble.plugin.tor;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
|
||||
import org.briarproject.bramble.api.battery.BatteryManager;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
@@ -89,9 +88,15 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
|
||||
// Check that we have a Tor binary for this architecture
|
||||
String architecture = null;
|
||||
for (String abi : AndroidUtils.getSupportedArchitectures()) {
|
||||
if (abi.startsWith("x86")) {
|
||||
if (abi.startsWith("x86_64")) {
|
||||
architecture = "x86_64";
|
||||
break;
|
||||
} else if (abi.startsWith("x86")) {
|
||||
architecture = "x86";
|
||||
break;
|
||||
} else if (abi.startsWith("arm64")) {
|
||||
architecture = "arm64";
|
||||
break;
|
||||
} else if (abi.startsWith("armeabi")) {
|
||||
architecture = "arm";
|
||||
break;
|
||||
@@ -101,8 +106,8 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
|
||||
LOG.info("Tor is not supported on this architecture");
|
||||
return null;
|
||||
}
|
||||
// Use position-independent executable for SDK >= 16
|
||||
if (Build.VERSION.SDK_INT >= 16) architecture += "_pie";
|
||||
// Use position-independent executable
|
||||
architecture += "_pie";
|
||||
|
||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
|
||||
@@ -23,6 +23,7 @@ import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static android.content.Context.WIFI_SERVICE;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static android.provider.Settings.Secure.ANDROID_ID;
|
||||
|
||||
@Immutable
|
||||
@@ -74,8 +75,7 @@ class AndroidSecureRandomProvider extends UnixSecureRandomProvider {
|
||||
// Silence strict mode
|
||||
StrictMode.ThreadPolicy tp = StrictMode.allowThreadDiskWrites();
|
||||
super.writeSeed();
|
||||
if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT <= 18)
|
||||
applyOpenSslFix();
|
||||
if (SDK_INT <= 18) applyOpenSslFix();
|
||||
StrictMode.setThreadPolicy(tp);
|
||||
}
|
||||
|
||||
|
||||
@@ -66,8 +66,8 @@ dependencyVerification {
|
||||
'org.beanshell:bsh:1.3.0:bsh-1.3.0.jar:9b04edc75d19db54f1b4e8b5355e9364384c6cf71eb0a1b9724c159d779879f8',
|
||||
'org.bouncycastle:bcpkix-jdk15on:1.56:bcpkix-jdk15on-1.56.jar:7043dee4e9e7175e93e0b36f45b1ec1ecb893c5f755667e8b916eb8dd201c6ca',
|
||||
'org.bouncycastle:bcprov-jdk15on:1.56:bcprov-jdk15on-1.56.jar:963e1ee14f808ffb99897d848ddcdb28fa91ddda867eb18d303e82728f878349',
|
||||
'org.briarproject:obfs4proxy-android:0.0.9:obfs4proxy-android-0.0.9.zip:9b7e9181535ea8d8bbe8ae6338e08cf4c5fc1e357a779393e0ce49586d459ae0',
|
||||
'org.briarproject:tor-android:0.3.5.8:tor-android-0.3.5.8.zip:42a13a6f185be1a62f42e3f30ce66a3c099ac5ec890a65e7593111b65b44a54a',
|
||||
'org.briarproject:obfs4proxy-android:0.0.11-2:obfs4proxy-android-0.0.11-2.zip:57e55cbe87aa2aac210fdbb6cd8cdeafe15f825406a08ebf77a8b787aa2c6a8a',
|
||||
'org.briarproject:tor-android:0.3.5.8-64:tor-android-0.3.5.8-64.zip:9f144088c0fe845d1cf3232cdc2b51c68e6f9a22660592009f43a5633fca8824',
|
||||
'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.codehaus.groovy:groovy-all:2.4.15:groovy-all-2.4.15.jar:51d6c4e71782e85674239189499854359d380fb75e1a703756e3aaa5b98a5af0',
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.briarproject.bramble.api;
|
||||
|
||||
/**
|
||||
* Interface for specifying which features are enabled in a build.
|
||||
*/
|
||||
public interface FeatureFlags {
|
||||
|
||||
boolean shouldEnableImageAttachments();
|
||||
|
||||
boolean shouldEnablePrivateMessageDeletion();
|
||||
}
|
||||
@@ -25,7 +25,10 @@ public interface ClientHelper {
|
||||
throws DbException, FormatException;
|
||||
|
||||
void addLocalMessage(Transaction txn, Message m, BdfDictionary metadata,
|
||||
boolean shared) throws DbException, FormatException;
|
||||
boolean shared, boolean temporary)
|
||||
throws DbException, FormatException;
|
||||
|
||||
Message createMessage(GroupId g, long timestamp, byte[] body);
|
||||
|
||||
Message createMessage(GroupId g, long timestamp, BdfList body)
|
||||
throws FormatException;
|
||||
@@ -108,7 +111,7 @@ public interface ClientHelper {
|
||||
Author parseAndValidateAuthor(BdfList author) throws FormatException;
|
||||
|
||||
PublicKey parseAndValidateAgreementPublicKey(byte[] publicKeyBytes)
|
||||
throws FormatException;
|
||||
throws FormatException;
|
||||
|
||||
TransportProperties parseAndValidateTransportProperties(
|
||||
BdfDictionary properties) throws FormatException;
|
||||
|
||||
@@ -4,8 +4,10 @@ import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.Pair;
|
||||
import org.briarproject.bramble.api.UnsupportedVersionException;
|
||||
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.db.NoSuchContactException;
|
||||
import org.briarproject.bramble.api.db.PendingContactExistsException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
@@ -117,9 +119,14 @@ public interface ContactManager {
|
||||
* @throws FormatException If the link is invalid
|
||||
* @throws GeneralSecurityException If the pending contact's handshake
|
||||
* public key is invalid
|
||||
* @throws ContactExistsException If a contact with the same handshake
|
||||
* public key already exists
|
||||
* @throws PendingContactExistsException If a pending contact with the same
|
||||
* handshake public key already exists
|
||||
*/
|
||||
PendingContact addPendingContact(String link, String alias)
|
||||
throws DbException, FormatException, GeneralSecurityException;
|
||||
throws DbException, FormatException, GeneralSecurityException,
|
||||
ContactExistsException, PendingContactExistsException;
|
||||
|
||||
/**
|
||||
* Returns the pending contact with the given ID.
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.briarproject.bramble.api.contact;
|
||||
public enum PendingContactState {
|
||||
|
||||
WAITING_FOR_CONNECTION,
|
||||
OFFLINE,
|
||||
CONNECTING,
|
||||
ADDING_CONTACT,
|
||||
FAILED
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.briarproject.bramble.api.transport.TransportKeySet;
|
||||
import org.briarproject.bramble.api.transport.TransportKeys;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
@@ -77,12 +78,12 @@ public interface DatabaseComponent extends TransactionManager {
|
||||
* Stores a local message.
|
||||
*/
|
||||
void addLocalMessage(Transaction txn, Message m, Metadata meta,
|
||||
boolean shared) throws DbException;
|
||||
boolean shared, boolean temporary) throws DbException;
|
||||
|
||||
/**
|
||||
* Stores a pending contact.
|
||||
*/
|
||||
void addPendingContact(Transaction txn, PendingContact p)
|
||||
void addPendingContact(Transaction txn, PendingContact p, AuthorId local)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
@@ -427,6 +428,13 @@ public interface DatabaseComponent extends TransactionManager {
|
||||
*/
|
||||
Settings getSettings(Transaction txn, String namespace) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the versions of the sync protocol supported by the given contact.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
List<Byte> getSyncVersions(Transaction txn, ContactId c) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all transport keys for the given transport.
|
||||
* <p/>
|
||||
@@ -510,6 +518,12 @@ public interface DatabaseComponent extends TransactionManager {
|
||||
void removePendingContact(Transaction txn, PendingContactId p)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Removes all temporary messages (and all associated state) from the
|
||||
* database.
|
||||
*/
|
||||
void removeTemporaryMessages(Transaction txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Removes a transport (and all associated state) from the database.
|
||||
*/
|
||||
@@ -538,6 +552,11 @@ public interface DatabaseComponent extends TransactionManager {
|
||||
void setGroupVisibility(Transaction txn, ContactId c, GroupId g,
|
||||
Visibility v) throws DbException;
|
||||
|
||||
/**
|
||||
* Marks the given message as permanent, i.e. not temporary.
|
||||
*/
|
||||
void setMessagePermanent(Transaction txn, MessageId m) throws DbException;
|
||||
|
||||
/**
|
||||
* Marks the given message as shared.
|
||||
*/
|
||||
@@ -568,6 +587,12 @@ public interface DatabaseComponent extends TransactionManager {
|
||||
void setReorderingWindow(Transaction txn, KeySetId k, TransportId t,
|
||||
long timePeriod, long base, byte[] bitmap) throws DbException;
|
||||
|
||||
/**
|
||||
* Sets the versions of the sync protocol supported by the given contact.
|
||||
*/
|
||||
void setSyncVersions(Transaction txn, ContactId c, List<Byte> supported)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Marks the given transport keys as usable for outgoing streams.
|
||||
*/
|
||||
|
||||
@@ -1,9 +1,21 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
import org.briarproject.bramble.api.contact.PendingContact;
|
||||
|
||||
/**
|
||||
* Thrown when a duplicate pending contact is added to the database. This
|
||||
* exception may occur due to concurrent updates and does not indicate a
|
||||
* database error.
|
||||
*/
|
||||
public class PendingContactExistsException extends DbException {
|
||||
|
||||
private final PendingContact pendingContact;
|
||||
|
||||
public PendingContactExistsException(PendingContact pendingContact) {
|
||||
this.pendingContact = pendingContact;
|
||||
}
|
||||
|
||||
public PendingContact getPendingContact() {
|
||||
return pendingContact;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
package org.briarproject.bramble.api.sync;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* A record acknowledging receipt of one or more {@link Message Messages}.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class Ack {
|
||||
|
||||
private final Collection<MessageId> acked;
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
package org.briarproject.bramble.api.sync;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class Message {
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
package org.briarproject.bramble.api.sync;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* A record offering the recipient one or more {@link Message Messages}.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class Offer {
|
||||
|
||||
private final Collection<MessageId> offered;
|
||||
|
||||
@@ -9,5 +9,5 @@ public interface RecordTypes {
|
||||
byte MESSAGE = 1;
|
||||
byte OFFER = 2;
|
||||
byte REQUEST = 3;
|
||||
|
||||
byte VERSIONS = 4;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
package org.briarproject.bramble.api.sync;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* A record requesting one or more {@link Message Messages} from the recipient.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class Request {
|
||||
|
||||
private final Collection<MessageId> requested;
|
||||
|
||||
@@ -2,6 +2,9 @@ package org.briarproject.bramble.api.sync;
|
||||
|
||||
import org.briarproject.bramble.api.UniqueId;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.briarproject.bramble.api.record.Record.MAX_RECORD_PAYLOAD_BYTES;
|
||||
|
||||
public interface SyncConstants {
|
||||
@@ -11,6 +14,11 @@ public interface SyncConstants {
|
||||
*/
|
||||
byte PROTOCOL_VERSION = 0;
|
||||
|
||||
/**
|
||||
* The versions of the sync protocol this peer supports.
|
||||
*/
|
||||
List<Byte> SUPPORTED_VERSIONS = singletonList(PROTOCOL_VERSION);
|
||||
|
||||
/**
|
||||
* The maximum length of a group descriptor in bytes.
|
||||
*/
|
||||
@@ -35,4 +43,10 @@ public interface SyncConstants {
|
||||
* The maximum number of message IDs in an ack, offer or request record.
|
||||
*/
|
||||
int MAX_MESSAGE_IDS = MAX_RECORD_PAYLOAD_BYTES / UniqueId.LENGTH;
|
||||
|
||||
/**
|
||||
* The maximum number of versions of the sync protocol a peer may support
|
||||
* simultaneously.
|
||||
*/
|
||||
int MAX_SUPPORTED_VERSIONS = 10;
|
||||
}
|
||||
|
||||
@@ -25,4 +25,7 @@ public interface SyncRecordReader {
|
||||
|
||||
Request readRequest() throws IOException;
|
||||
|
||||
boolean hasVersions() throws IOException;
|
||||
|
||||
Versions readVersions() throws IOException;
|
||||
}
|
||||
|
||||
@@ -15,5 +15,7 @@ public interface SyncRecordWriter {
|
||||
|
||||
void writeRequest(Request r) throws IOException;
|
||||
|
||||
void writeVersions(Versions v) throws IOException;
|
||||
|
||||
void flush() throws IOException;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.briarproject.bramble.api.sync;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* A record telling the recipient which versions of the sync protocol the
|
||||
* sender supports.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class Versions {
|
||||
|
||||
private final List<Byte> supported;
|
||||
|
||||
public Versions(List<Byte> supported) {
|
||||
this.supported = supported;
|
||||
}
|
||||
|
||||
public List<Byte> getSupportedVersions() {
|
||||
return supported;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.briarproject.bramble.api.sync.event;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when the versions of the sync protocol supported
|
||||
* by a contact are updated.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class SyncVersionsUpdatedEvent extends Event {
|
||||
|
||||
private final ContactId contactId;
|
||||
private final List<Byte> supported;
|
||||
|
||||
public SyncVersionsUpdatedEvent(ContactId contactId, List<Byte> supported) {
|
||||
this.contactId = contactId;
|
||||
this.supported = supported;
|
||||
}
|
||||
|
||||
public ContactId getContactId() {
|
||||
return contactId;
|
||||
}
|
||||
|
||||
public List<Byte> getSupportedVersions() {
|
||||
return supported;
|
||||
}
|
||||
}
|
||||
@@ -85,14 +85,21 @@ class ClientHelperImpl implements ClientHelper {
|
||||
@Override
|
||||
public void addLocalMessage(Message m, BdfDictionary metadata,
|
||||
boolean shared) throws DbException, FormatException {
|
||||
db.transaction(false, txn -> addLocalMessage(txn, m, metadata, shared));
|
||||
db.transaction(false, txn -> addLocalMessage(txn, m, metadata, shared,
|
||||
false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLocalMessage(Transaction txn, Message m,
|
||||
BdfDictionary metadata, boolean shared)
|
||||
BdfDictionary metadata, boolean shared, boolean temporary)
|
||||
throws DbException, FormatException {
|
||||
db.addLocalMessage(txn, m, metadataEncoder.encode(metadata), shared);
|
||||
db.addLocalMessage(txn, m, metadataEncoder.encode(metadata), shared,
|
||||
temporary);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message createMessage(GroupId g, long timestamp, byte[] body) {
|
||||
return messageFactory.createMessage(g, timestamp, body);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -139,7 +139,8 @@ class ContactManagerImpl implements ContactManager, EventListener {
|
||||
pendingContactFactory.createPendingContact(link, alias);
|
||||
Transaction txn = db.startTransaction(false);
|
||||
try {
|
||||
db.addPendingContact(txn, p);
|
||||
AuthorId local = identityManager.getLocalAuthor(txn).getId();
|
||||
db.addPendingContact(txn, p, local);
|
||||
KeyPair ourKeyPair = identityManager.getHandshakeKeys(txn);
|
||||
keyManager.addPendingContact(txn, p.getId(), p.getPublicKey(),
|
||||
ourKeyPair);
|
||||
@@ -147,7 +148,6 @@ class ContactManagerImpl implements ContactManager, EventListener {
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
states.put(p.getId(), WAITING_FOR_CONNECTION);
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.briarproject.bramble.api.transport.TransportKeySet;
|
||||
import org.briarproject.bramble.api.transport.TransportKeys;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
@@ -115,7 +116,7 @@ interface Database<T> {
|
||||
* if the message was created locally.
|
||||
*/
|
||||
void addMessage(T txn, Message m, MessageState state, boolean shared,
|
||||
@Nullable ContactId sender) throws DbException;
|
||||
boolean temporary, @Nullable ContactId sender) throws DbException;
|
||||
|
||||
/**
|
||||
* Adds a dependency between two messages, where the dependent message is
|
||||
@@ -266,6 +267,16 @@ interface Database<T> {
|
||||
*/
|
||||
Collection<ContactId> getContacts(T txn, AuthorId local) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the contact with the given {@code handshakePublicKey}
|
||||
* for the given local pseudonym or {@code null} if none exists.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
@Nullable
|
||||
Contact getContact(T txn, PublicKey handshakePublicKey, AuthorId local)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the group with the given ID.
|
||||
* <p/>
|
||||
@@ -528,6 +539,13 @@ interface Database<T> {
|
||||
*/
|
||||
Settings getSettings(T txn, String namespace) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the versions of the sync protocol supported by the given contact.
|
||||
* <p/>
|
||||
* Read-only.
|
||||
*/
|
||||
List<Byte> getSyncVersions(T txn, ContactId c) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all transport keys for the given transport.
|
||||
* <p/>
|
||||
@@ -630,6 +648,12 @@ interface Database<T> {
|
||||
*/
|
||||
void removePendingContact(T txn, PendingContactId p) throws DbException;
|
||||
|
||||
/**
|
||||
* Removes all temporary messages (and all associated state) from the
|
||||
* database.
|
||||
*/
|
||||
void removeTemporaryMessages(T txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Removes a transport (and all associated state) from the database.
|
||||
*/
|
||||
@@ -671,6 +695,11 @@ interface Database<T> {
|
||||
void setHandshakeKeyPair(T txn, AuthorId local, PublicKey publicKey,
|
||||
PrivateKey privateKey) throws DbException;
|
||||
|
||||
/**
|
||||
* Marks the given message as permanent, i.e. not temporary.
|
||||
*/
|
||||
void setMessagePermanent(T txn, MessageId m) throws DbException;
|
||||
|
||||
/**
|
||||
* Marks the given message as shared.
|
||||
*/
|
||||
@@ -689,6 +718,12 @@ interface Database<T> {
|
||||
void setReorderingWindow(T txn, KeySetId k, TransportId t,
|
||||
long timePeriod, long base, byte[] bitmap) throws DbException;
|
||||
|
||||
/**
|
||||
* Sets the versions of the sync protocol supported by the given contact.
|
||||
*/
|
||||
void setSyncVersions(T txn, ContactId c, List<Byte> supported)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Marks the given transport keys as usable for outgoing streams.
|
||||
*/
|
||||
|
||||
@@ -65,6 +65,7 @@ import org.briarproject.bramble.api.sync.event.MessageToAckEvent;
|
||||
import org.briarproject.bramble.api.sync.event.MessageToRequestEvent;
|
||||
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent;
|
||||
import org.briarproject.bramble.api.sync.event.MessagesSentEvent;
|
||||
import org.briarproject.bramble.api.sync.event.SyncVersionsUpdatedEvent;
|
||||
import org.briarproject.bramble.api.sync.validation.MessageState;
|
||||
import org.briarproject.bramble.api.transport.KeySetId;
|
||||
import org.briarproject.bramble.api.transport.TransportKeySet;
|
||||
@@ -273,13 +274,14 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
|
||||
@Override
|
||||
public void addLocalMessage(Transaction transaction, Message m,
|
||||
Metadata meta, boolean shared) throws DbException {
|
||||
Metadata meta, boolean shared, boolean temporary)
|
||||
throws DbException {
|
||||
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
||||
T txn = unbox(transaction);
|
||||
if (!db.containsGroup(txn, m.getGroupId()))
|
||||
throw new NoSuchGroupException();
|
||||
if (!db.containsMessage(txn, m.getId())) {
|
||||
db.addMessage(txn, m, DELIVERED, shared, null);
|
||||
db.addMessage(txn, m, DELIVERED, shared, temporary, null);
|
||||
transaction.attach(new MessageAddedEvent(m, null));
|
||||
transaction.attach(new MessageStateChangedEvent(m.getId(), true,
|
||||
DELIVERED));
|
||||
@@ -289,12 +291,17 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPendingContact(Transaction transaction, PendingContact p)
|
||||
throws DbException {
|
||||
public void addPendingContact(Transaction transaction, PendingContact p,
|
||||
AuthorId local) throws DbException {
|
||||
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
||||
T txn = unbox(transaction);
|
||||
if (db.containsPendingContact(txn, p.getId()))
|
||||
throw new PendingContactExistsException();
|
||||
Contact contact = db.getContact(txn, p.getPublicKey(), local);
|
||||
if (contact != null)
|
||||
throw new ContactExistsException(local, contact.getAuthor());
|
||||
if (db.containsPendingContact(txn, p.getId())) {
|
||||
PendingContact existing = db.getPendingContact(txn, p.getId());
|
||||
throw new PendingContactExistsException(existing);
|
||||
}
|
||||
db.addPendingContact(txn, p);
|
||||
transaction.attach(new PendingContactAddedEvent(p));
|
||||
}
|
||||
@@ -715,6 +722,15 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
return db.getSettings(txn, namespace);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Byte> getSyncVersions(Transaction transaction, ContactId c)
|
||||
throws DbException {
|
||||
T txn = unbox(transaction);
|
||||
if (!db.containsContact(txn, c))
|
||||
throw new NoSuchContactException();
|
||||
return db.getSyncVersions(txn, c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<TransportKeySet> getTransportKeys(Transaction transaction,
|
||||
TransportId t) throws DbException {
|
||||
@@ -800,7 +816,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
db.raiseSeenFlag(txn, c, m.getId());
|
||||
db.raiseAckFlag(txn, c, m.getId());
|
||||
} else {
|
||||
db.addMessage(txn, m, UNKNOWN, false, c);
|
||||
db.addMessage(txn, m, UNKNOWN, false, false, c);
|
||||
transaction.attach(new MessageAddedEvent(m, c));
|
||||
}
|
||||
transaction.attach(new MessageToAckEvent(c));
|
||||
@@ -908,6 +924,14 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
transaction.attach(new PendingContactRemovedEvent(p));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeTemporaryMessages(Transaction transaction)
|
||||
throws DbException {
|
||||
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
||||
T txn = unbox(transaction);
|
||||
db.removeTemporaryMessages(txn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeTransport(Transaction transaction, TransportId t)
|
||||
throws DbException {
|
||||
@@ -967,6 +991,16 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
transaction.attach(new GroupVisibilityUpdatedEvent(affected));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMessagePermanent(Transaction transaction, MessageId m)
|
||||
throws DbException {
|
||||
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
||||
T txn = unbox(transaction);
|
||||
if (!db.containsMessage(txn, m))
|
||||
throw new NoSuchMessageException();
|
||||
db.setMessagePermanent(txn, m);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMessageShared(Transaction transaction, MessageId m)
|
||||
throws DbException {
|
||||
@@ -975,8 +1009,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
if (!db.containsMessage(txn, m))
|
||||
throw new NoSuchMessageException();
|
||||
if (db.getMessageState(txn, m) != DELIVERED)
|
||||
throw new IllegalArgumentException(
|
||||
"Shared undelivered message");
|
||||
throw new IllegalArgumentException("Shared undelivered message");
|
||||
db.setMessageShared(txn, m);
|
||||
transaction.attach(new MessageSharedEvent(m));
|
||||
}
|
||||
@@ -1028,6 +1061,17 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
db.setReorderingWindow(txn, k, t, timePeriod, base, bitmap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSyncVersions(Transaction transaction, ContactId c,
|
||||
List<Byte> supported) throws DbException {
|
||||
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
||||
T txn = unbox(transaction);
|
||||
if (!db.containsContact(txn, c))
|
||||
throw new NoSuchContactException();
|
||||
db.setSyncVersions(txn, c, supported);
|
||||
transaction.attach(new SyncVersionsUpdatedEvent(c, supported));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTransportKeysActive(Transaction transaction, TransportId t,
|
||||
KeySetId k) throws DbException {
|
||||
|
||||
@@ -62,6 +62,7 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
|
||||
import static java.sql.Types.BINARY;
|
||||
import static java.sql.Types.BOOLEAN;
|
||||
@@ -97,7 +98,7 @@ import static org.briarproject.bramble.util.LogUtils.now;
|
||||
abstract class JdbcDatabase implements Database<Connection> {
|
||||
|
||||
// Package access for testing
|
||||
static final int CODE_SCHEMA_VERSION = 45;
|
||||
static final int CODE_SCHEMA_VERSION = 47;
|
||||
|
||||
// Time period offsets for incoming transport keys
|
||||
private static final int OFFSET_PREV = -1;
|
||||
@@ -134,6 +135,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
+ " handshakePublicKey _BINARY," // Null if key is unknown
|
||||
+ " localAuthorId _HASH NOT NULL,"
|
||||
+ " verified BOOLEAN NOT NULL,"
|
||||
+ " syncVersions _BINARY DEFAULT '00' NOT NULL,"
|
||||
+ " PRIMARY KEY (contactId),"
|
||||
+ " FOREIGN KEY (localAuthorId)"
|
||||
+ " REFERENCES localAuthors (authorId)"
|
||||
@@ -177,6 +179,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
+ " timestamp BIGINT NOT NULL,"
|
||||
+ " state INT NOT NULL,"
|
||||
+ " shared BOOLEAN NOT NULL,"
|
||||
+ " temporary BOOLEAN NOT NULL,"
|
||||
+ " length INT NOT NULL,"
|
||||
+ " raw BLOB," // Null if message has been deleted
|
||||
+ " PRIMARY KEY (messageId),"
|
||||
@@ -336,25 +339,26 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
private static final Logger LOG =
|
||||
getLogger(JdbcDatabase.class.getName());
|
||||
|
||||
// Different database libraries use different names for certain types
|
||||
private final MessageFactory messageFactory;
|
||||
private final Clock clock;
|
||||
private final DatabaseTypes dbTypes;
|
||||
|
||||
// Locking: connectionsLock
|
||||
private final Lock connectionsLock = new ReentrantLock();
|
||||
private final Condition connectionsChanged = connectionsLock.newCondition();
|
||||
|
||||
@GuardedBy("connectionsLock")
|
||||
private final LinkedList<Connection> connections = new LinkedList<>();
|
||||
|
||||
private int openConnections = 0; // Locking: connectionsLock
|
||||
private boolean closed = false; // Locking: connectionsLock
|
||||
@GuardedBy("connectionsLock")
|
||||
private int openConnections = 0;
|
||||
@GuardedBy("connectionsLock")
|
||||
private boolean closed = false;
|
||||
|
||||
protected abstract Connection createConnection()
|
||||
throws DbException, SQLException;
|
||||
|
||||
protected abstract void compactAndClose() throws DbException;
|
||||
|
||||
private final Lock connectionsLock = new ReentrantLock();
|
||||
private final Condition connectionsChanged = connectionsLock.newCondition();
|
||||
|
||||
JdbcDatabase(DatabaseTypes databaseTypes, MessageFactory messageFactory,
|
||||
Clock clock) {
|
||||
this.dbTypes = databaseTypes;
|
||||
@@ -457,7 +461,9 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
new Migration41_42(dbTypes),
|
||||
new Migration42_43(dbTypes),
|
||||
new Migration43_44(dbTypes),
|
||||
new Migration44_45()
|
||||
new Migration44_45(),
|
||||
new Migration45_46(),
|
||||
new Migration46_47(dbTypes)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -777,22 +783,23 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
|
||||
@Override
|
||||
public void addMessage(Connection txn, Message m, MessageState state,
|
||||
boolean messageShared, @Nullable ContactId sender)
|
||||
boolean shared, boolean temporary, @Nullable ContactId sender)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
String sql = "INSERT INTO messages (messageId, groupId, timestamp,"
|
||||
+ " state, shared, length, raw)"
|
||||
+ " VALUES (?, ?, ?, ?, ?, ?, ?)";
|
||||
+ " state, shared, temporary, length, raw)"
|
||||
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setBytes(1, m.getId().getBytes());
|
||||
ps.setBytes(2, m.getGroupId().getBytes());
|
||||
ps.setLong(3, m.getTimestamp());
|
||||
ps.setInt(4, state.getValue());
|
||||
ps.setBoolean(5, messageShared);
|
||||
ps.setBoolean(5, shared);
|
||||
ps.setBoolean(6, temporary);
|
||||
byte[] raw = messageFactory.getRawMessage(m);
|
||||
ps.setInt(6, raw.length);
|
||||
ps.setBytes(7, raw);
|
||||
ps.setInt(7, raw.length);
|
||||
ps.setBytes(8, raw);
|
||||
int affected = ps.executeUpdate();
|
||||
if (affected != 1) throw new DbStateException();
|
||||
ps.close();
|
||||
@@ -804,8 +811,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
boolean offered = removeOfferedMessage(txn, c, m.getId());
|
||||
boolean seen = offered || c.equals(sender);
|
||||
addStatus(txn, m.getId(), c, m.getGroupId(), m.getTimestamp(),
|
||||
raw.length, state, e.getValue(), messageShared,
|
||||
false, seen);
|
||||
raw.length, state, e.getValue(), shared, false, seen);
|
||||
}
|
||||
// Update denormalised column in messageDependencies if dependency
|
||||
// is in same group as dependent
|
||||
@@ -1459,6 +1465,47 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Contact getContact(Connection txn, PublicKey handshakePublicKey,
|
||||
AuthorId localAuthorId) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT contactId, authorId, formatVersion, name,"
|
||||
+ " alias, publicKey, verified"
|
||||
+ " FROM contacts"
|
||||
+ " WHERE handshakePublicKey = ? AND localAuthorId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setBytes(1, handshakePublicKey.getEncoded());
|
||||
ps.setBytes(2, localAuthorId.getBytes());
|
||||
rs = ps.executeQuery();
|
||||
if (!rs.next()) {
|
||||
rs.close();
|
||||
ps.close();
|
||||
return null;
|
||||
}
|
||||
ContactId contactId = new ContactId(rs.getInt(1));
|
||||
AuthorId authorId = new AuthorId(rs.getBytes(2));
|
||||
int formatVersion = rs.getInt(3);
|
||||
String name = rs.getString(4);
|
||||
String alias = rs.getString(5);
|
||||
PublicKey publicKey = new SignaturePublicKey(rs.getBytes(6));
|
||||
boolean verified = rs.getBoolean(7);
|
||||
if (rs.next()) throw new DbStateException();
|
||||
rs.close();
|
||||
ps.close();
|
||||
Author author =
|
||||
new Author(authorId, formatVersion, name, publicKey);
|
||||
return new Contact(contactId, author, localAuthorId, alias,
|
||||
handshakePublicKey, verified);
|
||||
} catch (SQLException e) {
|
||||
tryToClose(rs, LOG, WARNING);
|
||||
tryToClose(ps, LOG, WARNING);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Group getGroup(Connection txn, GroupId g) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
@@ -2324,6 +2371,32 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Byte> getSyncVersions(Connection txn, ContactId c)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT syncVersions FROM contacts"
|
||||
+ " WHERE contactId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
rs = ps.executeQuery();
|
||||
if (!rs.next()) throw new DbStateException();
|
||||
byte[] bytes = rs.getBytes(1);
|
||||
List<Byte> supported = new ArrayList<>(bytes.length);
|
||||
for (byte b : bytes) supported.add(b);
|
||||
if (rs.next()) throw new DbStateException();
|
||||
rs.close();
|
||||
ps.close();
|
||||
return supported;
|
||||
} catch (SQLException e) {
|
||||
tryToClose(rs, LOG, WARNING);
|
||||
tryToClose(ps, LOG, WARNING);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<TransportKeySet> getTransportKeys(Connection txn,
|
||||
TransportId t) throws DbException {
|
||||
@@ -2876,6 +2949,21 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeTemporaryMessages(Connection txn) throws DbException {
|
||||
Statement s = null;
|
||||
try {
|
||||
String sql = "DELETE FROM messages WHERE temporary = TRUE";
|
||||
s = txn.createStatement();
|
||||
int affected = s.executeUpdate(sql);
|
||||
if (affected < 0) throw new DbStateException();
|
||||
s.close();
|
||||
} catch (SQLException e) {
|
||||
tryToClose(s, LOG, WARNING);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeTransport(Connection txn, TransportId t)
|
||||
throws DbException {
|
||||
@@ -3021,6 +3109,24 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMessagePermanent(Connection txn, MessageId m)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
String sql = "UPDATE messages SET temporary = FALSE"
|
||||
+ " WHERE messageId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setBytes(1, m.getBytes());
|
||||
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
|
||||
public void setMessageShared(Connection txn, MessageId m)
|
||||
throws DbException {
|
||||
@@ -3124,6 +3230,29 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSyncVersions(Connection txn, ContactId c,
|
||||
List<Byte> supported) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
String sql = "UPDATE contacts SET syncVersions = ?"
|
||||
+ " WHERE contactId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
byte[] bytes = new byte[supported.size()];
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
bytes[i] = supported.get(i);
|
||||
}
|
||||
ps.setBytes(1, bytes);
|
||||
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
|
||||
public void setTransportKeysActive(Connection txn, TransportId t,
|
||||
KeySetId k) throws DbException {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.briarproject.bramble.db;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
||||
|
||||
class Migration45_46 implements Migration<Connection> {
|
||||
|
||||
private static final Logger LOG = getLogger(Migration45_46.class.getName());
|
||||
|
||||
@Override
|
||||
public int getStartVersion() {
|
||||
return 45;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEndVersion() {
|
||||
return 46;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void migrate(Connection txn) throws DbException {
|
||||
Statement s = null;
|
||||
try {
|
||||
s = txn.createStatement();
|
||||
s.execute("ALTER TABLE messages"
|
||||
+ " ADD COLUMN temporary BOOLEAN DEFAULT FALSE NOT NULL");
|
||||
} catch (SQLException e) {
|
||||
tryToClose(s, LOG, WARNING);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.briarproject.bramble.db;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
||||
|
||||
class Migration46_47 implements Migration<Connection> {
|
||||
|
||||
private static final Logger LOG = getLogger(Migration46_47.class.getName());
|
||||
|
||||
private final DatabaseTypes dbTypes;
|
||||
|
||||
Migration46_47(DatabaseTypes dbTypes) {
|
||||
this.dbTypes = dbTypes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getStartVersion() {
|
||||
return 46;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEndVersion() {
|
||||
return 47;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void migrate(Connection txn) throws DbException {
|
||||
Statement s = null;
|
||||
try {
|
||||
s = txn.createStatement();
|
||||
s.execute(dbTypes.replaceTypes("ALTER TABLE contacts"
|
||||
+ " ADD COLUMN syncVersions"
|
||||
+ " _BINARY DEFAULT '00' NOT NULL"));
|
||||
} catch (SQLException e) {
|
||||
tryToClose(s, LOG, WARNING);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,8 +107,11 @@ class LifecycleManagerImpl implements LifecycleManager, MigrationListener {
|
||||
else logDuration(LOG, "Creating database", start);
|
||||
|
||||
db.transaction(false, txn -> {
|
||||
long start1 = now();
|
||||
db.removeTemporaryMessages(txn);
|
||||
logDuration(LOG, "Removing temporary messages", start1);
|
||||
for (OpenDatabaseHook hook : openDatabaseHooks) {
|
||||
long start1 = now();
|
||||
start1 = now();
|
||||
hook.onDatabaseOpened(txn);
|
||||
if (LOG.isLoggable(FINE)) {
|
||||
logDuration(LOG, "Calling open database hook "
|
||||
|
||||
@@ -195,8 +195,6 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
if (!assetsAreUpToDate()) installAssets();
|
||||
if (cookieFile.exists() && !cookieFile.delete())
|
||||
LOG.warning("Old auth cookie not deleted");
|
||||
// Migrate old settings before having a chance to stop
|
||||
migrateSettings();
|
||||
// Start a new Tor process
|
||||
LOG.info("Starting Tor");
|
||||
String torPath = torFile.getAbsolutePath();
|
||||
@@ -816,21 +814,6 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
controlConnection.setConf("ConnectionPadding", enable ? "1" : "0");
|
||||
}
|
||||
|
||||
// TODO remove when sufficient time has passed. Added 2018-08-15
|
||||
private void migrateSettings() {
|
||||
Settings sOld = callback.getSettings();
|
||||
int oldNetwork = sOld.getInt("network", -1);
|
||||
if (oldNetwork == -1) return;
|
||||
Settings s = new Settings();
|
||||
if (oldNetwork == 0) {
|
||||
s.putInt(PREF_TOR_NETWORK, PREF_TOR_NETWORK_NEVER);
|
||||
} else if (oldNetwork == 1) {
|
||||
s.putBoolean(PREF_TOR_MOBILE, false);
|
||||
}
|
||||
s.putInt("network", -1);
|
||||
callback.mergeSettings(s);
|
||||
}
|
||||
|
||||
private static class ConnectionStatus {
|
||||
|
||||
// All of the following are locking: this
|
||||
|
||||
@@ -284,7 +284,7 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
|
||||
meta.put("transportId", t.getString());
|
||||
meta.put("version", version);
|
||||
meta.put("local", local);
|
||||
clientHelper.addLocalMessage(txn, m, meta, shared);
|
||||
clientHelper.addLocalMessage(txn, m, meta, shared, false);
|
||||
} catch (FormatException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -66,6 +66,7 @@ import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.contact.PendingContactState.ADDING_CONTACT;
|
||||
import static org.briarproject.bramble.api.contact.PendingContactState.FAILED;
|
||||
import static org.briarproject.bramble.api.contact.PendingContactState.OFFLINE;
|
||||
import static org.briarproject.bramble.api.contact.PendingContactState.WAITING_FOR_CONNECTION;
|
||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNull;
|
||||
import static org.briarproject.bramble.rendezvous.RendezvousConstants.POLLING_INTERVAL_MS;
|
||||
@@ -158,9 +159,7 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
|
||||
private void addPendingContact(PendingContact p) {
|
||||
long now = clock.currentTimeMillis();
|
||||
long expiry = p.getTimestamp() + RENDEZVOUS_TIMEOUT_MS;
|
||||
if (expiry > now) {
|
||||
broadcastState(p.getId(), WAITING_FOR_CONNECTION);
|
||||
} else {
|
||||
if (expiry <= now) {
|
||||
broadcastState(p.getId(), FAILED);
|
||||
return;
|
||||
}
|
||||
@@ -180,9 +179,13 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
|
||||
for (PluginState ps : pluginStates.values()) {
|
||||
RendezvousEndpoint endpoint =
|
||||
createEndpoint(ps.plugin, p.getId(), cs);
|
||||
if (endpoint != null)
|
||||
if (endpoint != null) {
|
||||
requireNull(ps.endpoints.put(p.getId(), endpoint));
|
||||
cs.numEndpoints++;
|
||||
}
|
||||
}
|
||||
if (cs.numEndpoints == 0) broadcastState(p.getId(), OFFLINE);
|
||||
else broadcastState(p.getId(), WAITING_FOR_CONNECTION);
|
||||
} catch (DbException | GeneralSecurityException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
@@ -328,9 +331,14 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
|
||||
TransportId t = plugin.getId();
|
||||
Map<PendingContactId, RendezvousEndpoint> endpoints = new HashMap<>();
|
||||
for (Entry<PendingContactId, CryptoState> e : cryptoStates.entrySet()) {
|
||||
RendezvousEndpoint endpoint =
|
||||
createEndpoint(plugin, e.getKey(), e.getValue());
|
||||
if (endpoint != null) endpoints.put(e.getKey(), endpoint);
|
||||
PendingContactId p = e.getKey();
|
||||
CryptoState cs = e.getValue();
|
||||
RendezvousEndpoint endpoint = createEndpoint(plugin, p, cs);
|
||||
if (endpoint != null) {
|
||||
endpoints.put(p, endpoint);
|
||||
if (++cs.numEndpoints == 1)
|
||||
broadcastState(p, WAITING_FOR_CONNECTION);
|
||||
}
|
||||
}
|
||||
requireNull(pluginStates.put(t, new PluginState(plugin, endpoints)));
|
||||
}
|
||||
@@ -344,8 +352,11 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
|
||||
private void removeTransport(TransportId t) {
|
||||
PluginState ps = pluginStates.remove(t);
|
||||
if (ps != null) {
|
||||
for (RendezvousEndpoint endpoint : ps.endpoints.values()) {
|
||||
tryToClose(endpoint, LOG, INFO);
|
||||
for (Entry<PendingContactId, RendezvousEndpoint> e :
|
||||
ps.endpoints.entrySet()) {
|
||||
tryToClose(e.getValue(), LOG, INFO);
|
||||
CryptoState cs = cryptoStates.get(e.getKey());
|
||||
if (--cs.numEndpoints == 0) broadcastState(e.getKey(), OFFLINE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -391,6 +402,8 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
|
||||
private final boolean alice;
|
||||
private final long expiry;
|
||||
|
||||
private int numEndpoints = 0;
|
||||
|
||||
private CryptoState(SecretKey rendezvousKey, boolean alice,
|
||||
long expiry) {
|
||||
this.rendezvousKey = rendezvousKey;
|
||||
|
||||
@@ -17,6 +17,7 @@ import org.briarproject.bramble.api.sync.Offer;
|
||||
import org.briarproject.bramble.api.sync.Request;
|
||||
import org.briarproject.bramble.api.sync.SyncRecordWriter;
|
||||
import org.briarproject.bramble.api.sync.SyncSession;
|
||||
import org.briarproject.bramble.api.sync.Versions;
|
||||
import org.briarproject.bramble.api.sync.event.GroupVisibilityUpdatedEvent;
|
||||
import org.briarproject.bramble.api.sync.event.MessageRequestedEvent;
|
||||
import org.briarproject.bramble.api.sync.event.MessageSharedEvent;
|
||||
@@ -39,9 +40,11 @@ import javax.annotation.concurrent.ThreadSafe;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
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.lifecycle.LifecycleManager.LifecycleState.STOPPING;
|
||||
import static org.briarproject.bramble.api.record.Record.MAX_RECORD_PAYLOAD_BYTES;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_IDS;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.SUPPORTED_VERSIONS;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
/**
|
||||
@@ -55,7 +58,7 @@ import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
class DuplexOutgoingSession implements SyncSession, EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(DuplexOutgoingSession.class.getName());
|
||||
getLogger(DuplexOutgoingSession.class.getName());
|
||||
|
||||
private static final ThrowingRunnable<IOException> CLOSE = () -> {
|
||||
};
|
||||
@@ -103,6 +106,8 @@ class DuplexOutgoingSession implements SyncSession, EventListener {
|
||||
public void run() throws IOException {
|
||||
eventBus.addListener(this);
|
||||
try {
|
||||
// Send our supported protocol versions
|
||||
recordWriter.writeVersions(new Versions(SUPPORTED_VERSIONS));
|
||||
// Start a query for each type of record
|
||||
generateAck();
|
||||
generateBatch();
|
||||
|
||||
@@ -18,14 +18,17 @@ import org.briarproject.bramble.api.sync.Offer;
|
||||
import org.briarproject.bramble.api.sync.Request;
|
||||
import org.briarproject.bramble.api.sync.SyncRecordReader;
|
||||
import org.briarproject.bramble.api.sync.SyncSession;
|
||||
import org.briarproject.bramble.api.sync.Versions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.STOPPING;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@@ -37,7 +40,7 @@ import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
class IncomingSession implements SyncSession, EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(IncomingSession.class.getName());
|
||||
getLogger(IncomingSession.class.getName());
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final Executor dbExecutor;
|
||||
@@ -80,6 +83,9 @@ class IncomingSession implements SyncSession, EventListener {
|
||||
} else if (recordReader.hasRequest()) {
|
||||
Request r = recordReader.readRequest();
|
||||
dbExecutor.execute(new ReceiveRequest(r));
|
||||
} else if (recordReader.hasVersions()) {
|
||||
Versions v = recordReader.readVersions();
|
||||
dbExecutor.execute(new ReceiveVersions(v));
|
||||
} else {
|
||||
// unknown records are ignored in RecordReader#eof()
|
||||
throw new FormatException();
|
||||
@@ -190,4 +196,26 @@ class IncomingSession implements SyncSession, EventListener {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class ReceiveVersions implements Runnable {
|
||||
|
||||
private final Versions versions;
|
||||
|
||||
private ReceiveVersions(Versions versions) {
|
||||
this.versions = versions;
|
||||
}
|
||||
|
||||
@DatabaseExecutor
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
List<Byte> supported = versions.getSupportedVersions();
|
||||
db.transaction(false,
|
||||
txn -> db.setSyncVersions(txn, contactId, supported));
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.briarproject.bramble.api.sync.Ack;
|
||||
import org.briarproject.bramble.api.sync.Message;
|
||||
import org.briarproject.bramble.api.sync.SyncRecordWriter;
|
||||
import org.briarproject.bramble.api.sync.SyncSession;
|
||||
import org.briarproject.bramble.api.sync.Versions;
|
||||
import org.briarproject.bramble.api.transport.StreamWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -29,9 +30,11 @@ import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
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.lifecycle.LifecycleManager.LifecycleState.STOPPING;
|
||||
import static org.briarproject.bramble.api.record.Record.MAX_RECORD_PAYLOAD_BYTES;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_IDS;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.SUPPORTED_VERSIONS;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
/**
|
||||
@@ -44,7 +47,7 @@ import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
class SimplexOutgoingSession implements SyncSession, EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(SimplexOutgoingSession.class.getName());
|
||||
getLogger(SimplexOutgoingSession.class.getName());
|
||||
|
||||
private static final ThrowingRunnable<IOException> CLOSE = () -> {
|
||||
};
|
||||
@@ -80,6 +83,8 @@ class SimplexOutgoingSession implements SyncSession, EventListener {
|
||||
public void run() throws IOException {
|
||||
eventBus.addListener(this);
|
||||
try {
|
||||
// Send our supported protocol versions
|
||||
recordWriter.writeVersions(new Versions(SUPPORTED_VERSIONS));
|
||||
// Start a query for each type of record
|
||||
dbExecutor.execute(new GenerateAck());
|
||||
dbExecutor.execute(new GenerateBatch());
|
||||
|
||||
@@ -13,6 +13,7 @@ import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.api.sync.Offer;
|
||||
import org.briarproject.bramble.api.sync.Request;
|
||||
import org.briarproject.bramble.api.sync.SyncRecordReader;
|
||||
import org.briarproject.bramble.api.sync.Versions;
|
||||
import org.briarproject.bramble.util.ByteUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -26,6 +27,8 @@ import static org.briarproject.bramble.api.sync.RecordTypes.ACK;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.MESSAGE;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.OFFER;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.REQUEST;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.VERSIONS;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_SUPPORTED_VERSIONS;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION;
|
||||
|
||||
@@ -45,7 +48,7 @@ class SyncRecordReaderImpl implements SyncRecordReader {
|
||||
|
||||
private static boolean isKnownRecordType(byte type) {
|
||||
return type == ACK || type == MESSAGE || type == OFFER ||
|
||||
type == REQUEST;
|
||||
type == REQUEST || type == VERSIONS;
|
||||
}
|
||||
|
||||
private final MessageFactory messageFactory;
|
||||
@@ -148,4 +151,27 @@ class SyncRecordReaderImpl implements SyncRecordReader {
|
||||
if (!hasRequest()) throw new FormatException();
|
||||
return new Request(readMessageIds());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasVersions() throws IOException {
|
||||
return !eof() && getNextRecordType() == VERSIONS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Versions readVersions() throws IOException {
|
||||
if (!hasVersions()) throw new FormatException();
|
||||
return new Versions(readSupportedVersions());
|
||||
}
|
||||
|
||||
private List<Byte> readSupportedVersions() throws IOException {
|
||||
if (nextRecord == null) throw new AssertionError();
|
||||
byte[] payload = nextRecord.getPayload();
|
||||
if (payload.length == 0) throw new FormatException();
|
||||
if (payload.length > MAX_SUPPORTED_VERSIONS)
|
||||
throw new FormatException();
|
||||
List<Byte> supported = new ArrayList<>(payload.length);
|
||||
for (byte b : payload) supported.add(b);
|
||||
nextRecord = null;
|
||||
return supported;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.api.sync.Offer;
|
||||
import org.briarproject.bramble.api.sync.Request;
|
||||
import org.briarproject.bramble.api.sync.SyncRecordWriter;
|
||||
import org.briarproject.bramble.api.sync.Versions;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -20,6 +21,7 @@ import static org.briarproject.bramble.api.sync.RecordTypes.ACK;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.MESSAGE;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.OFFER;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.REQUEST;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.VERSIONS;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION;
|
||||
|
||||
@NotThreadSafe
|
||||
@@ -65,6 +67,12 @@ class SyncRecordWriterImpl implements SyncRecordWriter {
|
||||
writeRecord(REQUEST);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeVersions(Versions v) throws IOException {
|
||||
for (byte b : v.getSupportedVersions()) payload.write(b);
|
||||
writeRecord(VERSIONS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() throws IOException {
|
||||
writer.flush();
|
||||
|
||||
@@ -314,6 +314,7 @@ class ValidationManagerImpl implements ValidationManager, Service,
|
||||
try {
|
||||
shareMsg = hook.incomingMessage(txn, m, meta);
|
||||
} catch (InvalidMessageException e) {
|
||||
logException(LOG, INFO, e);
|
||||
invalidateMessage(txn, m.getId());
|
||||
return new DeliveryResult(false, false);
|
||||
}
|
||||
|
||||
@@ -243,7 +243,7 @@ class ClientVersioningManagerImpl implements ClientVersioningManager,
|
||||
try {
|
||||
Message m = clientHelper.createMessage(localGroup.getId(), now,
|
||||
body);
|
||||
db.addLocalMessage(txn, m, new Metadata(), false);
|
||||
db.addLocalMessage(txn, m, new Metadata(), false, false);
|
||||
} catch (FormatException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
@@ -438,7 +438,7 @@ class ClientVersioningManagerImpl implements ClientVersioningManager,
|
||||
BdfDictionary meta = new BdfDictionary();
|
||||
meta.put(MSG_KEY_UPDATE_VERSION, updateVersion);
|
||||
meta.put(MSG_KEY_LOCAL, true);
|
||||
clientHelper.addLocalMessage(txn, m, meta, true);
|
||||
clientHelper.addLocalMessage(txn, m, meta, true, false);
|
||||
} catch (FormatException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ public class ClientHelperImplTest extends BrambleTestCase {
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn));
|
||||
oneOf(metadataEncoder).encode(dictionary);
|
||||
will(returnValue(metadata));
|
||||
oneOf(db).addLocalMessage(txn, message, metadata, shared);
|
||||
oneOf(db).addLocalMessage(txn, message, metadata, shared, false);
|
||||
}});
|
||||
|
||||
clientHelper.addLocalMessage(message, dictionary, shared);
|
||||
|
||||
@@ -6,11 +6,15 @@ import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.contact.PendingContact;
|
||||
import org.briarproject.bramble.api.contact.PendingContactState;
|
||||
import org.briarproject.bramble.api.contact.event.ContactAddedEvent;
|
||||
import org.briarproject.bramble.api.contact.event.PendingContactStateChangedEvent;
|
||||
import org.briarproject.bramble.api.crypto.PublicKey;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.identity.Identity;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestDatabaseConfigModule;
|
||||
import org.briarproject.bramble.test.TestDuplexTransportConnection;
|
||||
@@ -27,7 +31,7 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static junit.framework.TestCase.assertNotNull;
|
||||
import static junit.framework.TestCase.assertNull;
|
||||
import static junit.framework.TestCase.fail;
|
||||
import static org.briarproject.bramble.api.contact.PendingContactState.WAITING_FOR_CONNECTION;
|
||||
import static org.briarproject.bramble.api.contact.PendingContactState.OFFLINE;
|
||||
import static org.briarproject.bramble.test.TestDuplexTransportConnection.createPair;
|
||||
import static org.briarproject.bramble.test.TestPluginConfigModule.DUPLEX_TRANSPORT_ID;
|
||||
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
||||
@@ -188,9 +192,14 @@ public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
||||
private PendingContact addPendingContact(
|
||||
ContactExchangeIntegrationTestComponent local,
|
||||
ContactExchangeIntegrationTestComponent remote) throws Exception {
|
||||
EventWaiter waiter = new EventWaiter();
|
||||
local.getEventBus().addListener(waiter);
|
||||
String link = remote.getContactManager().getHandshakeLink();
|
||||
String alias = remote.getIdentityManager().getLocalAuthor().getName();
|
||||
return local.getContactManager().addPendingContact(link, alias);
|
||||
PendingContact pendingContact =
|
||||
local.getContactManager().addPendingContact(link, alias);
|
||||
waiter.latch.await(TIMEOUT, MILLISECONDS);
|
||||
return pendingContact;
|
||||
}
|
||||
|
||||
private void assertContacts(boolean verified,
|
||||
@@ -237,7 +246,7 @@ public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
||||
assertEquals(1, pairs.size());
|
||||
Pair<PendingContact, PendingContactState> pair =
|
||||
pairs.iterator().next();
|
||||
assertEquals(WAITING_FOR_CONNECTION, pair.getSecond());
|
||||
assertEquals(OFFLINE, pair.getSecond());
|
||||
PendingContact pendingContact = pair.getFirst();
|
||||
assertEquals(expectedIdentity.getLocalAuthor().getName(),
|
||||
pendingContact.getAlias());
|
||||
@@ -261,4 +270,19 @@ public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
||||
tearDown(bob);
|
||||
deleteTestDirectory(testDir);
|
||||
}
|
||||
|
||||
@NotNullByDefault
|
||||
private static class EventWaiter implements EventListener {
|
||||
|
||||
private final CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof PendingContactStateChangedEvent) {
|
||||
PendingContactStateChangedEvent p =
|
||||
(PendingContactStateChangedEvent) e;
|
||||
if (p.getPendingContactState() == OFFLINE) latch.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,10 +61,12 @@ import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE;
|
||||
@@ -120,6 +122,9 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
private final Contact contact;
|
||||
private final KeySetId keySetId;
|
||||
private final PendingContactId pendingContactId;
|
||||
private final Random random = new Random();
|
||||
private final boolean shared = random.nextBoolean();
|
||||
private final boolean temporary = random.nextBoolean();
|
||||
|
||||
public DatabaseComponentImplTest() {
|
||||
clientId = getClientId();
|
||||
@@ -253,7 +258,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
eventExecutor, shutdownManager);
|
||||
|
||||
db.transaction(false, transaction ->
|
||||
db.addLocalMessage(transaction, message, metadata, true));
|
||||
db.addLocalMessage(transaction, message, metadata, shared,
|
||||
temporary));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -265,20 +271,23 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
will(returnValue(true));
|
||||
oneOf(database).containsMessage(txn, messageId);
|
||||
will(returnValue(false));
|
||||
oneOf(database).addMessage(txn, message, DELIVERED, true, null);
|
||||
oneOf(database).addMessage(txn, message, DELIVERED, shared,
|
||||
temporary, null);
|
||||
oneOf(database).mergeMessageMetadata(txn, messageId, metadata);
|
||||
oneOf(database).commitTransaction(txn);
|
||||
// The message was added, so the listeners should be called
|
||||
oneOf(eventBus).broadcast(with(any(MessageAddedEvent.class)));
|
||||
oneOf(eventBus)
|
||||
.broadcast(with(any(MessageStateChangedEvent.class)));
|
||||
oneOf(eventBus).broadcast(with(any(MessageSharedEvent.class)));
|
||||
oneOf(eventBus).broadcast(with(any(
|
||||
MessageStateChangedEvent.class)));
|
||||
if (shared)
|
||||
oneOf(eventBus).broadcast(with(any(MessageSharedEvent.class)));
|
||||
}});
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
eventExecutor, shutdownManager);
|
||||
|
||||
db.transaction(false, transaction ->
|
||||
db.addLocalMessage(transaction, message, metadata, true));
|
||||
db.addLocalMessage(transaction, message, metadata, shared,
|
||||
temporary));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -286,11 +295,11 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
// Check whether the contact is in the DB (which it's not)
|
||||
exactly(16).of(database).startTransaction();
|
||||
exactly(18).of(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
exactly(16).of(database).containsContact(txn, contactId);
|
||||
exactly(18).of(database).containsContact(txn, contactId);
|
||||
will(returnValue(false));
|
||||
exactly(16).of(database).abortTransaction(txn);
|
||||
exactly(18).of(database).abortTransaction(txn);
|
||||
}});
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
eventExecutor, shutdownManager);
|
||||
@@ -368,6 +377,14 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
// Expected
|
||||
}
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.getSyncVersions(transaction, contactId));
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
try {
|
||||
Ack a = new Ack(singletonList(messageId));
|
||||
db.transaction(false, transaction ->
|
||||
@@ -427,6 +444,14 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.setSyncVersions(transaction, contactId, emptyList()));
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -569,11 +594,11 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
// Check whether the message is in the DB (which it's not)
|
||||
exactly(11).of(database).startTransaction();
|
||||
exactly(12).of(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
exactly(11).of(database).containsMessage(txn, messageId);
|
||||
exactly(12).of(database).containsMessage(txn, messageId);
|
||||
will(returnValue(false));
|
||||
exactly(11).of(database).abortTransaction(txn);
|
||||
exactly(12).of(database).abortTransaction(txn);
|
||||
// Allow other checks to pass
|
||||
allowing(database).containsContact(txn, contactId);
|
||||
will(returnValue(true));
|
||||
@@ -637,6 +662,14 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
// Expected
|
||||
}
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.setMessagePermanent(transaction, message.getId()));
|
||||
fail();
|
||||
} catch (NoSuchMessageException expected) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
try {
|
||||
db.transaction(false, transaction ->
|
||||
db.setMessageShared(transaction, message.getId()));
|
||||
@@ -972,7 +1005,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
will(returnValue(VISIBLE));
|
||||
oneOf(database).containsMessage(txn, messageId);
|
||||
will(returnValue(false));
|
||||
oneOf(database).addMessage(txn, message, UNKNOWN, false, contactId);
|
||||
oneOf(database).addMessage(txn, message, UNKNOWN, false, false,
|
||||
contactId);
|
||||
// Second time
|
||||
oneOf(database).containsContact(txn, contactId);
|
||||
will(returnValue(true));
|
||||
@@ -1507,6 +1541,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
public void testMessageDependencies() throws Exception {
|
||||
int shutdownHandle = 12345;
|
||||
MessageId messageId2 = new MessageId(getRandomId());
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
// open()
|
||||
oneOf(database).open(key, null);
|
||||
@@ -1521,7 +1556,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
will(returnValue(true));
|
||||
oneOf(database).containsMessage(txn, messageId);
|
||||
will(returnValue(false));
|
||||
oneOf(database).addMessage(txn, message, DELIVERED, true, null);
|
||||
oneOf(database).addMessage(txn, message, DELIVERED, shared,
|
||||
temporary, null);
|
||||
oneOf(database).mergeMessageMetadata(txn, messageId, metadata);
|
||||
// addMessageDependencies()
|
||||
oneOf(database).containsMessage(txn, messageId);
|
||||
@@ -1544,7 +1580,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
oneOf(eventBus).broadcast(with(any(MessageAddedEvent.class)));
|
||||
oneOf(eventBus).broadcast(with(any(
|
||||
MessageStateChangedEvent.class)));
|
||||
oneOf(eventBus).broadcast(with(any(MessageSharedEvent.class)));
|
||||
if (shared)
|
||||
oneOf(eventBus).broadcast(with(any(MessageSharedEvent.class)));
|
||||
// endTransaction()
|
||||
oneOf(database).commitTransaction(txn);
|
||||
// close()
|
||||
@@ -1555,7 +1592,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
|
||||
|
||||
assertFalse(db.open(key, null));
|
||||
db.transaction(false, transaction -> {
|
||||
db.addLocalMessage(transaction, message, metadata, true);
|
||||
db.addLocalMessage(transaction, message, metadata, shared,
|
||||
temporary);
|
||||
Collection<MessageId> dependencies = new ArrayList<>(2);
|
||||
dependencies.add(messageId1);
|
||||
dependencies.add(messageId2);
|
||||
|
||||
@@ -567,8 +567,9 @@ public abstract class DatabasePerformanceTest extends BrambleTestCase {
|
||||
MessageState state =
|
||||
MessageState.fromValue(random.nextInt(4));
|
||||
boolean shared = random.nextBoolean();
|
||||
boolean temporary = random.nextBoolean();
|
||||
ContactId sender = random.nextBoolean() ? c : null;
|
||||
db.addMessage(txn, m, state, shared, sender);
|
||||
db.addMessage(txn, m, state, shared, temporary, sender);
|
||||
if (random.nextBoolean())
|
||||
db.raiseRequestedFlag(txn, c, m.getId());
|
||||
Metadata mm = getMetadata(METADATA_KEYS_PER_MESSAGE);
|
||||
@@ -597,7 +598,8 @@ public abstract class DatabasePerformanceTest extends BrambleTestCase {
|
||||
for (int j = 0; j < MESSAGES_PER_GROUP; j++) {
|
||||
Message m = getMessage(g.getId());
|
||||
messages.add(m);
|
||||
db.addMessage(txn, m, DELIVERED, false, null);
|
||||
boolean temporary = random.nextBoolean();
|
||||
db.addMessage(txn, m, DELIVERED, false, temporary, null);
|
||||
Metadata mm = getMetadata(METADATA_KEYS_PER_MESSAGE);
|
||||
messageMeta.get(g.getId()).add(mm);
|
||||
db.mergeMessageMetadata(txn, m.getId(), mm);
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.SettableClock;
|
||||
import org.briarproject.bramble.test.TestDatabaseConfig;
|
||||
import org.briarproject.bramble.test.TestMessageFactory;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -41,7 +42,6 @@ import org.junit.Test;
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -51,6 +51,7 @@ import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.singletonList;
|
||||
@@ -153,7 +154,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.addGroup(txn, group);
|
||||
assertTrue(db.containsGroup(txn, groupId));
|
||||
assertFalse(db.containsMessage(txn, messageId));
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
assertTrue(db.containsMessage(txn, messageId));
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
@@ -191,7 +192,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a group and a message
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
|
||||
// Removing the group should remove the message
|
||||
assertTrue(db.containsMessage(txn, messageId));
|
||||
@@ -213,7 +214,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.addContact(txn, author, localAuthor.getId(), null, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
|
||||
// The contact has not seen the message, so it should be sendable
|
||||
Collection<MessageId> ids =
|
||||
@@ -244,7 +245,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.addContact(txn, author, localAuthor.getId(), null, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
db.addMessage(txn, message, UNKNOWN, true, null);
|
||||
db.addMessage(txn, message, UNKNOWN, true, false, null);
|
||||
|
||||
// The message has not been validated, so it should not be sendable
|
||||
Collection<MessageId> ids = db.getMessagesToSend(txn, contactId,
|
||||
@@ -288,7 +289,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), null, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
|
||||
// The group is invisible, so the message should not be sendable
|
||||
Collection<MessageId> ids = db.getMessagesToSend(txn, contactId,
|
||||
@@ -340,7 +341,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.addContact(txn, author, localAuthor.getId(), null, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
db.addMessage(txn, message, DELIVERED, false, null);
|
||||
db.addMessage(txn, message, DELIVERED, false, false, null);
|
||||
|
||||
// The message is not shared, so it should not be sendable
|
||||
Collection<MessageId> ids = db.getMessagesToSend(txn, contactId,
|
||||
@@ -371,7 +372,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.addContact(txn, author, localAuthor.getId(), null, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
|
||||
// The message is sendable, but too large to send
|
||||
Collection<MessageId> ids =
|
||||
@@ -402,15 +403,15 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
// Add some messages to ack
|
||||
Message message1 = getMessage(groupId);
|
||||
MessageId messageId1 = message1.getId();
|
||||
db.addMessage(txn, message, DELIVERED, true, contactId);
|
||||
db.addMessage(txn, message1, DELIVERED, true, contactId);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, contactId);
|
||||
db.addMessage(txn, message1, DELIVERED, true, false, contactId);
|
||||
|
||||
// Both message IDs should be returned
|
||||
Collection<MessageId> ids = db.getMessagesToAck(txn, contactId, 1234);
|
||||
assertEquals(Arrays.asList(messageId, messageId1), ids);
|
||||
assertEquals(asList(messageId, messageId1), ids);
|
||||
|
||||
// Remove both message IDs
|
||||
db.lowerAckFlag(txn, contactId, Arrays.asList(messageId, messageId1));
|
||||
db.lowerAckFlag(txn, contactId, asList(messageId, messageId1));
|
||||
|
||||
// Both message IDs should have been removed
|
||||
assertEquals(emptyList(), db.getMessagesToAck(txn,
|
||||
@@ -422,7 +423,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Both message IDs should be returned
|
||||
ids = db.getMessagesToAck(txn, contactId, 1234);
|
||||
assertEquals(Arrays.asList(messageId, messageId1), ids);
|
||||
assertEquals(asList(messageId, messageId1), ids);
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
@@ -439,7 +440,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.addContact(txn, author, localAuthor.getId(), null, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
|
||||
// Retrieve the message from the database and mark it as sent
|
||||
Collection<MessageId> ids = db.getMessagesToSend(txn, contactId,
|
||||
@@ -608,7 +609,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), null, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
|
||||
// The group is not visible so the message should not be visible
|
||||
assertFalse(db.containsVisibleMessage(txn, contactId, messageId));
|
||||
@@ -1149,6 +1150,43 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetContactsByHandshakePublicKey() throws Exception {
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add an identity for a local author - no contacts should be
|
||||
// associated
|
||||
db.addIdentity(txn, identity);
|
||||
PublicKey handshakePublicKey = TestUtils.getSignaturePublicKey();
|
||||
Contact contact =
|
||||
db.getContact(txn, handshakePublicKey, localAuthor.getId());
|
||||
assertNull(contact);
|
||||
|
||||
// Add a contact associated with the local author
|
||||
assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(),
|
||||
handshakePublicKey, true));
|
||||
contact = db.getContact(txn, handshakePublicKey, localAuthor.getId());
|
||||
assertNotNull(contact);
|
||||
assertEquals(contactId, contact.getId());
|
||||
assertEquals(author, contact.getAuthor());
|
||||
assertNull(contact.getAlias());
|
||||
assertEquals(handshakePublicKey, contact.getHandshakePublicKey());
|
||||
assertTrue(contact.isVerified());
|
||||
assertEquals(author.getName(), contact.getAuthor().getName());
|
||||
assertEquals(author.getPublicKey(), contact.getAuthor().getPublicKey());
|
||||
assertEquals(author.getFormatVersion(),
|
||||
contact.getAuthor().getFormatVersion());
|
||||
|
||||
// Ensure no contacts are returned after contact was deleted
|
||||
db.removeContact(txn, contactId);
|
||||
contact = db.getContact(txn, handshakePublicKey, localAuthor.getId());
|
||||
assertNull(contact);
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOfferedMessages() throws Exception {
|
||||
Database<Connection> db = open(false);
|
||||
@@ -1223,7 +1261,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a group and a message
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
|
||||
// Attach some metadata to the message
|
||||
Metadata metadata = new Metadata();
|
||||
@@ -1294,7 +1332,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a group and a message
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
|
||||
// Attach some metadata to the message
|
||||
Metadata metadata = new Metadata();
|
||||
@@ -1355,8 +1393,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a group and two messages
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message1, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
db.addMessage(txn, message1, DELIVERED, true, false, null);
|
||||
|
||||
// Attach some metadata to the messages
|
||||
Metadata metadata = new Metadata();
|
||||
@@ -1459,8 +1497,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a group and two messages
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message1, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
db.addMessage(txn, message1, DELIVERED, true, false, null);
|
||||
|
||||
// Attach some metadata to the messages
|
||||
Metadata metadata = new Metadata();
|
||||
@@ -1536,9 +1574,9 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a group and some messages
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, PENDING, true, contactId);
|
||||
db.addMessage(txn, message1, PENDING, true, contactId);
|
||||
db.addMessage(txn, message2, INVALID, true, contactId);
|
||||
db.addMessage(txn, message, PENDING, true, false, contactId);
|
||||
db.addMessage(txn, message1, PENDING, true, false, contactId);
|
||||
db.addMessage(txn, message2, INVALID, true, false, contactId);
|
||||
|
||||
// Add dependencies
|
||||
db.addMessageDependency(txn, message, messageId1, PENDING);
|
||||
@@ -1589,7 +1627,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertEquals(0, dependents.size());
|
||||
|
||||
// Add message 3
|
||||
db.addMessage(txn, message3, UNKNOWN, false, contactId);
|
||||
db.addMessage(txn, message3, UNKNOWN, false, false, contactId);
|
||||
|
||||
// Message 3 has message 1 as a dependent
|
||||
dependents = db.getMessageDependents(txn, messageId3);
|
||||
@@ -1601,7 +1639,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertEquals(0, dependents.size());
|
||||
|
||||
// Add message 4
|
||||
db.addMessage(txn, message4, UNKNOWN, false, contactId);
|
||||
db.addMessage(txn, message4, UNKNOWN, false, false, contactId);
|
||||
|
||||
// Message 4 has message 2 as a dependent
|
||||
dependents = db.getMessageDependents(txn, messageId4);
|
||||
@@ -1619,7 +1657,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a group and a message
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, PENDING, true, contactId);
|
||||
db.addMessage(txn, message, PENDING, true, false, contactId);
|
||||
|
||||
// Add a second group
|
||||
Group group1 = getGroup(clientId, 123);
|
||||
@@ -1629,7 +1667,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
// Add a message to the second group
|
||||
Message message1 = getMessage(groupId1);
|
||||
MessageId messageId1 = message1.getId();
|
||||
db.addMessage(txn, message1, DELIVERED, true, contactId);
|
||||
db.addMessage(txn, message1, DELIVERED, true, false, contactId);
|
||||
|
||||
// Create an ID for a missing message
|
||||
MessageId messageId2 = new MessageId(getRandomId());
|
||||
@@ -1637,7 +1675,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
// Add another message to the first group
|
||||
Message message3 = getMessage(groupId);
|
||||
MessageId messageId3 = message3.getId();
|
||||
db.addMessage(txn, message3, DELIVERED, true, contactId);
|
||||
db.addMessage(txn, message3, DELIVERED, true, false, contactId);
|
||||
|
||||
// Add dependencies between the messages
|
||||
db.addMessageDependency(txn, message, messageId1, PENDING);
|
||||
@@ -1680,10 +1718,10 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a group and some messages with different states
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message1, UNKNOWN, true, contactId);
|
||||
db.addMessage(txn, message2, INVALID, true, contactId);
|
||||
db.addMessage(txn, message3, PENDING, true, contactId);
|
||||
db.addMessage(txn, message4, DELIVERED, true, contactId);
|
||||
db.addMessage(txn, message1, UNKNOWN, true, false, contactId);
|
||||
db.addMessage(txn, message2, INVALID, true, false, contactId);
|
||||
db.addMessage(txn, message3, PENDING, true, false, contactId);
|
||||
db.addMessage(txn, message4, DELIVERED, true, false, contactId);
|
||||
|
||||
Collection<MessageId> result;
|
||||
|
||||
@@ -1713,10 +1751,10 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a group and some messages
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message1, DELIVERED, true, contactId);
|
||||
db.addMessage(txn, message2, DELIVERED, false, contactId);
|
||||
db.addMessage(txn, message3, DELIVERED, false, contactId);
|
||||
db.addMessage(txn, message4, DELIVERED, true, contactId);
|
||||
db.addMessage(txn, message1, DELIVERED, true, false, contactId);
|
||||
db.addMessage(txn, message2, DELIVERED, false, false, contactId);
|
||||
db.addMessage(txn, message3, DELIVERED, false, false, contactId);
|
||||
db.addMessage(txn, message4, DELIVERED, true, false, contactId);
|
||||
|
||||
// Introduce dependencies between the messages
|
||||
db.addMessageDependency(txn, message1, message2.getId(), DELIVERED);
|
||||
@@ -1744,7 +1782,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.addContact(txn, author, localAuthor.getId(), null, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
|
||||
// The message should not be sent or seen
|
||||
MessageStatus status = db.getMessageStatus(txn, contactId, messageId);
|
||||
@@ -1878,7 +1916,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.addContact(txn, author, localAuthor.getId(), null, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
|
||||
// The message should be visible to the contact
|
||||
assertTrue(db.containsVisibleMessage(txn, contactId, messageId));
|
||||
@@ -1961,7 +1999,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
|
||||
// Add a group and a message
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, UNKNOWN, false, contactId);
|
||||
db.addMessage(txn, message, UNKNOWN, false, false, contactId);
|
||||
|
||||
// Walk the message through the validation and delivery states
|
||||
assertEquals(UNKNOWN, db.getMessageState(txn, messageId));
|
||||
@@ -1988,7 +2026,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), null, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, UNKNOWN, false, null);
|
||||
db.addMessage(txn, message, UNKNOWN, false, false, null);
|
||||
|
||||
// There should be no messages to send
|
||||
assertEquals(Long.MAX_VALUE, db.getNextSendTime(txn, contactId));
|
||||
@@ -2073,7 +2111,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.addContact(txn, author, localAuthor.getId(), null, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
|
||||
// Time: now
|
||||
// Retrieve the message from the database
|
||||
@@ -2118,7 +2156,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.addContact(txn, author, localAuthor.getId(), null, true));
|
||||
db.addGroup(txn, group);
|
||||
db.addGroupVisibility(txn, contactId, groupId, true);
|
||||
db.addMessage(txn, message, DELIVERED, true, null);
|
||||
db.addMessage(txn, message, DELIVERED, true, false, null);
|
||||
|
||||
// Time: now
|
||||
// Retrieve the message from the database
|
||||
@@ -2257,6 +2295,58 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
||||
db.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTemporaryMessages() throws Exception {
|
||||
Message message1 = getMessage(groupId);
|
||||
MessageId messageId1 = message1.getId();
|
||||
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a group and two temporary messages
|
||||
db.addGroup(txn, group);
|
||||
db.addMessage(txn, message, DELIVERED, false, true, null);
|
||||
db.addMessage(txn, message1, DELIVERED, false, true, null);
|
||||
|
||||
// Mark one of the messages as permanent
|
||||
db.setMessagePermanent(txn, messageId);
|
||||
|
||||
// Remove all temporary messages
|
||||
db.removeTemporaryMessages(txn);
|
||||
|
||||
// The permanent message should not have been removed
|
||||
assertTrue(db.containsMessage(txn, messageId));
|
||||
|
||||
// The temporary message should have been removed
|
||||
assertFalse(db.containsMessage(txn, messageId1));
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSyncVersions() throws Exception {
|
||||
Database<Connection> db = open(false);
|
||||
Connection txn = db.startTransaction();
|
||||
|
||||
// Add a contact
|
||||
db.addIdentity(txn, identity);
|
||||
assertEquals(contactId,
|
||||
db.addContact(txn, author, localAuthor.getId(), null, true));
|
||||
|
||||
// Only sync version 0 should be supported by default
|
||||
List<Byte> defaultSupported = singletonList((byte) 0);
|
||||
assertEquals(defaultSupported, db.getSyncVersions(txn, contactId));
|
||||
|
||||
// Set the supported versions and check that they're returned
|
||||
List<Byte> supported = asList((byte) 0, (byte) 1);
|
||||
db.setSyncVersions(txn, contactId, supported);
|
||||
assertEquals(supported, db.getSyncVersions(txn, contactId));
|
||||
|
||||
db.commitTransaction(txn);
|
||||
db.close();
|
||||
}
|
||||
|
||||
private Database<Connection> open(boolean resume) throws Exception {
|
||||
return open(resume, new TestMessageFactory(), new SystemClock());
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ public class LifecycleManagerImplTest extends BrambleMockTestCase {
|
||||
oneOf(db).open(dbKey, lifecycleManager);
|
||||
will(returnValue(false));
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn));
|
||||
oneOf(db).removeTemporaryMessages(txn);
|
||||
allowing(eventBus).broadcast(with(any(LifecycleEvent.class)));
|
||||
}});
|
||||
|
||||
|
||||
@@ -637,7 +637,8 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
will(returnValue(timestamp));
|
||||
oneOf(clientHelper).createMessage(g, timestamp, body);
|
||||
will(returnValue(message));
|
||||
oneOf(clientHelper).addLocalMessage(txn, message, meta, shared);
|
||||
oneOf(clientHelper).addLocalMessage(txn, message, meta, shared,
|
||||
false);
|
||||
}});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ import static java.util.Collections.singletonList;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static org.briarproject.bramble.api.contact.PendingContactState.ADDING_CONTACT;
|
||||
import static org.briarproject.bramble.api.contact.PendingContactState.FAILED;
|
||||
import static org.briarproject.bramble.api.contact.PendingContactState.OFFLINE;
|
||||
import static org.briarproject.bramble.api.contact.PendingContactState.WAITING_FOR_CONNECTION;
|
||||
import static org.briarproject.bramble.rendezvous.RendezvousConstants.POLLING_INTERVAL_MS;
|
||||
import static org.briarproject.bramble.rendezvous.RendezvousConstants.RENDEZVOUS_TIMEOUT_MS;
|
||||
@@ -120,7 +121,7 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
will(returnValue(beforeExpiry));
|
||||
oneOf(eventBus).broadcast(with(new PredicateMatcher<>(
|
||||
PendingContactStateChangedEvent.class, e ->
|
||||
e.getPendingContactState() == WAITING_FOR_CONNECTION)));
|
||||
e.getPendingContactState() == OFFLINE)));
|
||||
// Capture the poll task
|
||||
oneOf(scheduler).scheduleAtFixedRate(with(any(Runnable.class)),
|
||||
with(POLLING_INTERVAL_MS), with(POLLING_INTERVAL_MS),
|
||||
@@ -184,7 +185,7 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Add the pending contact - endpoint should be created and polled
|
||||
expectAddUnexpiredPendingContact(beforeExpiry);
|
||||
expectAddPendingContact(beforeExpiry, WAITING_FOR_CONNECTION);
|
||||
expectDeriveRendezvousKey();
|
||||
expectCreateEndpoint();
|
||||
|
||||
@@ -205,9 +206,7 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Remove the pending contact - endpoint should be closed
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(rendezvousEndpoint).close();
|
||||
}});
|
||||
expectCloseEndpoint();
|
||||
|
||||
rendezvousPoller.eventOccurred(
|
||||
new PendingContactRemovedEvent(pendingContact.getId()));
|
||||
@@ -238,7 +237,7 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Add the pending contact - endpoint should be created and polled
|
||||
expectAddUnexpiredPendingContact(beforeExpiry);
|
||||
expectAddPendingContact(beforeExpiry, WAITING_FOR_CONNECTION);
|
||||
expectDeriveRendezvousKey();
|
||||
expectCreateEndpoint();
|
||||
|
||||
@@ -260,10 +259,7 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
|
||||
// Run the poll task - pending contact expires, endpoint is closed
|
||||
expectPendingContactExpires(afterExpiry);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(rendezvousEndpoint).close();
|
||||
}});
|
||||
expectCloseEndpoint();
|
||||
|
||||
capturePollTask.get().run();
|
||||
context.assertIsSatisfied();
|
||||
@@ -289,7 +285,7 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Add the pending contact - no endpoints should be created yet
|
||||
expectAddUnexpiredPendingContact(beforeExpiry);
|
||||
expectAddPendingContact(beforeExpiry, OFFLINE);
|
||||
expectDeriveRendezvousKey();
|
||||
|
||||
rendezvousPoller.eventOccurred(
|
||||
@@ -299,14 +295,14 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
// Enable the transport - endpoint should be created
|
||||
expectGetPlugin();
|
||||
expectCreateEndpoint();
|
||||
expectStateChangedEvent(WAITING_FOR_CONNECTION);
|
||||
|
||||
rendezvousPoller.eventOccurred(new TransportEnabledEvent(transportId));
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Disable the transport - endpoint should be closed
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(rendezvousEndpoint).close();
|
||||
}});
|
||||
expectCloseEndpoint();
|
||||
expectStateChangedEvent(OFFLINE);
|
||||
|
||||
rendezvousPoller.eventOccurred(new TransportDisabledEvent(transportId));
|
||||
context.assertIsSatisfied();
|
||||
@@ -482,13 +478,14 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
return capturePollTask;
|
||||
}
|
||||
|
||||
private void expectAddUnexpiredPendingContact(long now) {
|
||||
private void expectAddPendingContact(long now,
|
||||
PendingContactState initialState) {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now));
|
||||
oneOf(eventBus).broadcast(with(new PredicateMatcher<>(
|
||||
PendingContactStateChangedEvent.class, e ->
|
||||
e.getPendingContactState() == WAITING_FOR_CONNECTION)));
|
||||
e.getPendingContactState() == initialState)));
|
||||
}});
|
||||
}
|
||||
|
||||
@@ -546,7 +543,7 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
will(returnValue(now));
|
||||
oneOf(eventBus).broadcast(with(new PredicateMatcher<>(
|
||||
PendingContactStateChangedEvent.class, e ->
|
||||
e.getPendingContactState() == WAITING_FOR_CONNECTION)));
|
||||
e.getPendingContactState() == OFFLINE)));
|
||||
// Capture the poll task
|
||||
oneOf(scheduler).scheduleAtFixedRate(with(any(Runnable.class)),
|
||||
with(POLLING_INTERVAL_MS), with(POLLING_INTERVAL_MS),
|
||||
@@ -576,4 +573,10 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
e.getPendingContactState() == state)));
|
||||
}});
|
||||
}
|
||||
|
||||
private void expectCloseEndpoint() throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(rendezvousEndpoint).close();
|
||||
}});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.Message;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.api.sync.SyncRecordWriter;
|
||||
import org.briarproject.bramble.api.sync.Versions;
|
||||
import org.briarproject.bramble.api.transport.StreamWriter;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.DbExpectations;
|
||||
@@ -49,6 +50,8 @@ public class SimplexOutgoingSessionTest extends BrambleMockTestCase {
|
||||
context.checking(new DbExpectations() {{
|
||||
// Add listener
|
||||
oneOf(eventBus).addListener(session);
|
||||
// Send the protocol versions
|
||||
oneOf(recordWriter).writeVersions(with(any(Versions.class)));
|
||||
// No acks to send
|
||||
oneOf(db).transactionWithNullableResult(with(false),
|
||||
withNullableDbCallable(noAckTxn));
|
||||
@@ -83,6 +86,8 @@ public class SimplexOutgoingSessionTest extends BrambleMockTestCase {
|
||||
context.checking(new DbExpectations() {{
|
||||
// Add listener
|
||||
oneOf(eventBus).addListener(session);
|
||||
// Send the protocol versions
|
||||
oneOf(recordWriter).writeVersions(with(any(Versions.class)));
|
||||
// One ack to send
|
||||
oneOf(db).transactionWithNullableResult(with(false),
|
||||
withNullableDbCallable(ackTxn));
|
||||
|
||||
@@ -10,11 +10,14 @@ import org.briarproject.bramble.api.sync.MessageFactory;
|
||||
import org.briarproject.bramble.api.sync.Offer;
|
||||
import org.briarproject.bramble.api.sync.Request;
|
||||
import org.briarproject.bramble.api.sync.SyncRecordReader;
|
||||
import org.briarproject.bramble.api.sync.Versions;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@@ -22,7 +25,9 @@ import static org.briarproject.bramble.api.record.Record.MAX_RECORD_PAYLOAD_BYTE
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.ACK;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.OFFER;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.REQUEST;
|
||||
import static org.briarproject.bramble.api.sync.RecordTypes.VERSIONS;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_IDS;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_SUPPORTED_VERSIONS;
|
||||
import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -35,12 +40,17 @@ public class SyncRecordReaderImplTest extends BrambleMockTestCase {
|
||||
context.mock(MessageFactory.class);
|
||||
private final RecordReader recordReader = context.mock(RecordReader.class);
|
||||
|
||||
private SyncRecordReader reader;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
reader = new SyncRecordReaderImpl(messageFactory, recordReader);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoFormatExceptionIfAckIsMaximumSize() throws Exception {
|
||||
expectReadRecord(createAck());
|
||||
|
||||
SyncRecordReader reader =
|
||||
new SyncRecordReaderImpl(messageFactory, recordReader);
|
||||
Ack ack = reader.readAck();
|
||||
assertEquals(MAX_MESSAGE_IDS, ack.getMessageIds().size());
|
||||
}
|
||||
@@ -49,8 +59,6 @@ public class SyncRecordReaderImplTest extends BrambleMockTestCase {
|
||||
public void testFormatExceptionIfAckIsEmpty() throws Exception {
|
||||
expectReadRecord(createEmptyAck());
|
||||
|
||||
SyncRecordReader reader =
|
||||
new SyncRecordReaderImpl(messageFactory, recordReader);
|
||||
reader.readAck();
|
||||
}
|
||||
|
||||
@@ -58,8 +66,6 @@ public class SyncRecordReaderImplTest extends BrambleMockTestCase {
|
||||
public void testNoFormatExceptionIfOfferIsMaximumSize() throws Exception {
|
||||
expectReadRecord(createOffer());
|
||||
|
||||
SyncRecordReader reader =
|
||||
new SyncRecordReaderImpl(messageFactory, recordReader);
|
||||
Offer offer = reader.readOffer();
|
||||
assertEquals(MAX_MESSAGE_IDS, offer.getMessageIds().size());
|
||||
}
|
||||
@@ -68,8 +74,6 @@ public class SyncRecordReaderImplTest extends BrambleMockTestCase {
|
||||
public void testFormatExceptionIfOfferIsEmpty() throws Exception {
|
||||
expectReadRecord(createEmptyOffer());
|
||||
|
||||
SyncRecordReader reader =
|
||||
new SyncRecordReaderImpl(messageFactory, recordReader);
|
||||
reader.readOffer();
|
||||
}
|
||||
|
||||
@@ -77,8 +81,6 @@ public class SyncRecordReaderImplTest extends BrambleMockTestCase {
|
||||
public void testNoFormatExceptionIfRequestIsMaximumSize() throws Exception {
|
||||
expectReadRecord(createRequest());
|
||||
|
||||
SyncRecordReader reader =
|
||||
new SyncRecordReaderImpl(messageFactory, recordReader);
|
||||
Request request = reader.readRequest();
|
||||
assertEquals(MAX_MESSAGE_IDS, request.getMessageIds().size());
|
||||
}
|
||||
@@ -87,11 +89,36 @@ public class SyncRecordReaderImplTest extends BrambleMockTestCase {
|
||||
public void testFormatExceptionIfRequestIsEmpty() throws Exception {
|
||||
expectReadRecord(createEmptyRequest());
|
||||
|
||||
SyncRecordReader reader =
|
||||
new SyncRecordReaderImpl(messageFactory, recordReader);
|
||||
reader.readRequest();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoFormatExceptionIfVersionsIsMaximumSize()
|
||||
throws Exception {
|
||||
expectReadRecord(createVersions(MAX_SUPPORTED_VERSIONS));
|
||||
|
||||
Versions versions = reader.readVersions();
|
||||
List<Byte> supported = versions.getSupportedVersions();
|
||||
assertEquals(MAX_SUPPORTED_VERSIONS, supported.size());
|
||||
for (int i = 0; i < supported.size(); i++) {
|
||||
assertEquals(i, (int) supported.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testFormatExceptionIfVersionsIsEmpty() throws Exception {
|
||||
expectReadRecord(createVersions(0));
|
||||
|
||||
reader.readVersions();
|
||||
}
|
||||
|
||||
@Test(expected = FormatException.class)
|
||||
public void testFormatExceptionIfVersionsIsTooLarge() throws Exception {
|
||||
expectReadRecord(createVersions(MAX_SUPPORTED_VERSIONS + 1));
|
||||
|
||||
reader.readVersions();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEofReturnsTrueWhenAtEndOfStream() throws Exception {
|
||||
expectReadRecord(createAck());
|
||||
@@ -140,6 +167,12 @@ public class SyncRecordReaderImplTest extends BrambleMockTestCase {
|
||||
return new Record(PROTOCOL_VERSION, REQUEST, new byte[0]);
|
||||
}
|
||||
|
||||
private Record createVersions(int numVersions) {
|
||||
byte[] payload = new byte[numVersions];
|
||||
for (int i = 0; i < payload.length; i++) payload[i] = (byte) i;
|
||||
return new Record(PROTOCOL_VERSION, VERSIONS, payload);
|
||||
}
|
||||
|
||||
private byte[] createPayload() throws Exception {
|
||||
ByteArrayOutputStream payload = new ByteArrayOutputStream();
|
||||
while (payload.size() + UniqueId.LENGTH <= MAX_RECORD_PAYLOAD_BYTES) {
|
||||
|
||||
@@ -28,7 +28,6 @@ import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.Collections.singletonMap;
|
||||
@@ -80,24 +79,9 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
|
||||
|
||||
@Test
|
||||
public void testStartAndStop() throws Exception {
|
||||
Transaction txn = new Transaction(null, true);
|
||||
Transaction txn1 = new Transaction(null, true);
|
||||
Transaction txn2 = new Transaction(null, true);
|
||||
|
||||
context.checking(new DbExpectations() {{
|
||||
// validateOutstandingMessages()
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||
oneOf(db).getMessagesToValidate(txn);
|
||||
will(returnValue(emptyList()));
|
||||
// deliverOutstandingMessages()
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn1));
|
||||
oneOf(db).getPendingMessages(txn1);
|
||||
will(returnValue(emptyList()));
|
||||
// shareOutstandingMessages()
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn2));
|
||||
oneOf(db).getMessagesToShare(txn2);
|
||||
will(returnValue(emptyList()));
|
||||
}});
|
||||
expectGetMessagesToValidate();
|
||||
expectGetPendingMessages();
|
||||
expectGetMessagesToShare();
|
||||
|
||||
vm.startService();
|
||||
vm.stopService();
|
||||
@@ -106,167 +90,134 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
|
||||
@Test
|
||||
public void testMessagesAreValidatedAtStartup() throws Exception {
|
||||
Transaction txn = new Transaction(null, true);
|
||||
Transaction txn1 = new Transaction(null, true);
|
||||
Transaction txn2 = new Transaction(null, false);
|
||||
Transaction txn3 = new Transaction(null, true);
|
||||
Transaction txn4 = new Transaction(null, false);
|
||||
Transaction txn5 = new Transaction(null, true);
|
||||
Transaction txn6 = new Transaction(null, true);
|
||||
Transaction txn1 = new Transaction(null, false);
|
||||
Transaction txn2 = new Transaction(null, true);
|
||||
Transaction txn3 = new Transaction(null, false);
|
||||
|
||||
expectGetMessagesToValidate(messageId, messageId1);
|
||||
|
||||
context.checking(new DbExpectations() {{
|
||||
// Get messages to validate
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||
oneOf(db).getMessagesToValidate(txn);
|
||||
will(returnValue(asList(messageId, messageId1)));
|
||||
// Load the first raw message and group
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn1));
|
||||
oneOf(db).getMessage(txn1, messageId);
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||
oneOf(db).getMessage(txn, messageId);
|
||||
will(returnValue(message));
|
||||
oneOf(db).getGroup(txn1, groupId);
|
||||
oneOf(db).getGroup(txn, groupId);
|
||||
will(returnValue(group));
|
||||
// Validate the first message: valid
|
||||
oneOf(validator).validateMessage(message, group);
|
||||
will(returnValue(validResult));
|
||||
// Store the validation result for the first message
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn2));
|
||||
oneOf(db).mergeMessageMetadata(txn2, messageId, metadata);
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn1));
|
||||
oneOf(db).mergeMessageMetadata(txn1, messageId, metadata);
|
||||
// Deliver the first message
|
||||
oneOf(hook).incomingMessage(txn2, message, metadata);
|
||||
oneOf(hook).incomingMessage(txn1, message, metadata);
|
||||
will(returnValue(false));
|
||||
oneOf(db).setMessageState(txn2, messageId, DELIVERED);
|
||||
oneOf(db).setMessageState(txn1, messageId, DELIVERED);
|
||||
// Get any pending dependents
|
||||
oneOf(db).getMessageDependents(txn2, messageId);
|
||||
oneOf(db).getMessageDependents(txn1, messageId);
|
||||
will(returnValue(emptyMap()));
|
||||
// Load the second raw message and group
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn3));
|
||||
oneOf(db).getMessage(txn3, messageId1);
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn2));
|
||||
oneOf(db).getMessage(txn2, messageId1);
|
||||
will(returnValue(message1));
|
||||
oneOf(db).getGroup(txn3, groupId);
|
||||
oneOf(db).getGroup(txn2, groupId);
|
||||
will(returnValue(group));
|
||||
// Validate the second message: invalid
|
||||
oneOf(validator).validateMessage(message1, group);
|
||||
will(throwException(new InvalidMessageException()));
|
||||
// Store the validation result for the second message
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn4));
|
||||
oneOf(db).getMessageState(txn4, messageId1);
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn3));
|
||||
oneOf(db).getMessageState(txn3, messageId1);
|
||||
will(returnValue(UNKNOWN));
|
||||
oneOf(db).setMessageState(txn4, messageId1, INVALID);
|
||||
oneOf(db).deleteMessage(txn4, messageId1);
|
||||
oneOf(db).deleteMessageMetadata(txn4, messageId1);
|
||||
oneOf(db).setMessageState(txn3, messageId1, INVALID);
|
||||
oneOf(db).deleteMessage(txn3, messageId1);
|
||||
oneOf(db).deleteMessageMetadata(txn3, messageId1);
|
||||
// Recursively invalidate any dependents
|
||||
oneOf(db).getMessageDependents(txn4, messageId1);
|
||||
oneOf(db).getMessageDependents(txn3, messageId1);
|
||||
will(returnValue(emptyMap()));
|
||||
// Get pending messages to deliver
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn5));
|
||||
oneOf(db).getPendingMessages(txn5);
|
||||
will(returnValue(emptyList()));
|
||||
// Get messages to share
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn6));
|
||||
oneOf(db).getMessagesToShare(txn6);
|
||||
will(returnValue(emptyList()));
|
||||
}});
|
||||
|
||||
expectGetPendingMessages();
|
||||
expectGetMessagesToShare();
|
||||
|
||||
vm.startService();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPendingMessagesAreDeliveredAtStartup() throws Exception {
|
||||
Transaction txn = new Transaction(null, true);
|
||||
Transaction txn1 = new Transaction(null, true);
|
||||
Transaction txn2 = new Transaction(null, false);
|
||||
Transaction txn3 = new Transaction(null, false);
|
||||
Transaction txn4 = new Transaction(null, true);
|
||||
Transaction txn = new Transaction(null, false);
|
||||
Transaction txn1 = new Transaction(null, false);
|
||||
|
||||
expectGetMessagesToValidate();
|
||||
expectGetPendingMessages(messageId);
|
||||
|
||||
context.checking(new DbExpectations() {{
|
||||
// Get messages to validate
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||
oneOf(db).getMessagesToValidate(txn);
|
||||
will(returnValue(emptyList()));
|
||||
// Get pending messages to deliver
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn1));
|
||||
oneOf(db).getPendingMessages(txn1);
|
||||
will(returnValue(singletonList(messageId)));
|
||||
// Check whether the message is ready to deliver
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn2));
|
||||
oneOf(db).getMessageState(txn2, messageId);
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn));
|
||||
oneOf(db).getMessageState(txn, messageId);
|
||||
will(returnValue(PENDING));
|
||||
oneOf(db).getMessageDependencies(txn2, messageId);
|
||||
oneOf(db).getMessageDependencies(txn, messageId);
|
||||
will(returnValue(singletonMap(messageId1, DELIVERED)));
|
||||
// Get the message and its metadata to deliver
|
||||
oneOf(db).getMessage(txn2, messageId);
|
||||
oneOf(db).getMessage(txn, messageId);
|
||||
will(returnValue(message));
|
||||
oneOf(db).getGroup(txn2, groupId);
|
||||
oneOf(db).getGroup(txn, groupId);
|
||||
will(returnValue(group));
|
||||
oneOf(db).getMessageMetadataForValidator(txn2, messageId);
|
||||
oneOf(db).getMessageMetadataForValidator(txn, messageId);
|
||||
will(returnValue(new Metadata()));
|
||||
// Deliver the message
|
||||
oneOf(hook).incomingMessage(txn2, message, metadata);
|
||||
oneOf(hook).incomingMessage(txn, message, metadata);
|
||||
will(returnValue(false));
|
||||
oneOf(db).setMessageState(txn2, messageId, DELIVERED);
|
||||
oneOf(db).setMessageState(txn, messageId, DELIVERED);
|
||||
// Get any pending dependents
|
||||
oneOf(db).getMessageDependents(txn2, messageId);
|
||||
oneOf(db).getMessageDependents(txn, messageId);
|
||||
will(returnValue(singletonMap(messageId2, PENDING)));
|
||||
// Check whether the dependent is ready to deliver
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn3));
|
||||
oneOf(db).getMessageState(txn3, messageId2);
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn1));
|
||||
oneOf(db).getMessageState(txn1, messageId2);
|
||||
will(returnValue(PENDING));
|
||||
oneOf(db).getMessageDependencies(txn3, messageId2);
|
||||
oneOf(db).getMessageDependencies(txn1, messageId2);
|
||||
will(returnValue(singletonMap(messageId1, DELIVERED)));
|
||||
// Get the dependent and its metadata to deliver
|
||||
oneOf(db).getMessage(txn3, messageId2);
|
||||
oneOf(db).getMessage(txn1, messageId2);
|
||||
will(returnValue(message2));
|
||||
oneOf(db).getGroup(txn3, groupId);
|
||||
oneOf(db).getGroup(txn1, groupId);
|
||||
will(returnValue(group));
|
||||
oneOf(db).getMessageMetadataForValidator(txn3, messageId2);
|
||||
oneOf(db).getMessageMetadataForValidator(txn1, messageId2);
|
||||
will(returnValue(metadata));
|
||||
// Deliver the dependent
|
||||
oneOf(hook).incomingMessage(txn3, message2, metadata);
|
||||
oneOf(hook).incomingMessage(txn1, message2, metadata);
|
||||
will(returnValue(false));
|
||||
oneOf(db).setMessageState(txn3, messageId2, DELIVERED);
|
||||
oneOf(db).setMessageState(txn1, messageId2, DELIVERED);
|
||||
// Get any pending dependents
|
||||
oneOf(db).getMessageDependents(txn3, messageId2);
|
||||
oneOf(db).getMessageDependents(txn1, messageId2);
|
||||
will(returnValue(emptyMap()));
|
||||
|
||||
// Get messages to share
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn4));
|
||||
oneOf(db).getMessagesToShare(txn4);
|
||||
will(returnValue(emptyList()));
|
||||
}});
|
||||
|
||||
expectGetMessagesToShare();
|
||||
|
||||
vm.startService();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessagesAreSharedAtStartup() throws Exception {
|
||||
Transaction txn = new Transaction(null, true);
|
||||
Transaction txn1 = new Transaction(null, true);
|
||||
Transaction txn2 = new Transaction(null, true);
|
||||
Transaction txn3 = new Transaction(null, false);
|
||||
Transaction txn4 = new Transaction(null, false);
|
||||
Transaction txn = new Transaction(null, false);
|
||||
Transaction txn1 = new Transaction(null, false);
|
||||
|
||||
expectGetMessagesToValidate();
|
||||
expectGetPendingMessages();
|
||||
expectGetMessagesToShare(messageId);
|
||||
|
||||
context.checking(new DbExpectations() {{
|
||||
// No messages to validate
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||
oneOf(db).getMessagesToValidate(txn);
|
||||
will(returnValue(emptyList()));
|
||||
// No pending messages to deliver
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn1));
|
||||
oneOf(db).getPendingMessages(txn1);
|
||||
will(returnValue(emptyList()));
|
||||
|
||||
// Get messages to share
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn2));
|
||||
oneOf(db).getMessagesToShare(txn2);
|
||||
will(returnValue(singletonList(messageId)));
|
||||
// Share message and get dependencies
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn3));
|
||||
oneOf(db).setMessageShared(txn3, messageId);
|
||||
oneOf(db).getMessageDependencies(txn3, messageId);
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn));
|
||||
oneOf(db).setMessageShared(txn, messageId);
|
||||
oneOf(db).getMessageDependencies(txn, messageId);
|
||||
will(returnValue(singletonMap(messageId2, DELIVERED)));
|
||||
// Share dependency
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn4));
|
||||
oneOf(db).setMessageShared(txn4, messageId2);
|
||||
oneOf(db).getMessageDependencies(txn4, messageId2);
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn1));
|
||||
oneOf(db).setMessageShared(txn1, messageId2);
|
||||
oneOf(db).getMessageDependencies(txn1, messageId2);
|
||||
will(returnValue(emptyMap()));
|
||||
}});
|
||||
|
||||
@@ -318,49 +269,39 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
|
||||
throws Exception {
|
||||
Transaction txn = new Transaction(null, true);
|
||||
Transaction txn1 = new Transaction(null, true);
|
||||
Transaction txn2 = new Transaction(null, true);
|
||||
Transaction txn3 = new Transaction(null, false);
|
||||
Transaction txn4 = new Transaction(null, true);
|
||||
Transaction txn5 = new Transaction(null, true);
|
||||
Transaction txn2 = new Transaction(null, false);
|
||||
|
||||
expectGetMessagesToValidate(messageId, messageId1);
|
||||
|
||||
context.checking(new DbExpectations() {{
|
||||
// Get messages to validate
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||
oneOf(db).getMessagesToValidate(txn);
|
||||
will(returnValue(asList(messageId, messageId1)));
|
||||
// Load the first raw message - *gasp* it's gone!
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn1));
|
||||
oneOf(db).getMessage(txn1, messageId);
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||
oneOf(db).getMessage(txn, messageId);
|
||||
will(throwException(new NoSuchMessageException()));
|
||||
// Load the second raw message and group
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn2));
|
||||
oneOf(db).getMessage(txn2, messageId1);
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn1));
|
||||
oneOf(db).getMessage(txn1, messageId1);
|
||||
will(returnValue(message1));
|
||||
oneOf(db).getGroup(txn2, groupId);
|
||||
oneOf(db).getGroup(txn1, groupId);
|
||||
will(returnValue(group));
|
||||
// Validate the second message: invalid
|
||||
oneOf(validator).validateMessage(message1, group);
|
||||
will(throwException(new InvalidMessageException()));
|
||||
// Invalidate the second message
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn3));
|
||||
oneOf(db).getMessageState(txn3, messageId1);
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn2));
|
||||
oneOf(db).getMessageState(txn2, messageId1);
|
||||
will(returnValue(UNKNOWN));
|
||||
oneOf(db).setMessageState(txn3, messageId1, INVALID);
|
||||
oneOf(db).deleteMessage(txn3, messageId1);
|
||||
oneOf(db).deleteMessageMetadata(txn3, messageId1);
|
||||
oneOf(db).setMessageState(txn2, messageId1, INVALID);
|
||||
oneOf(db).deleteMessage(txn2, messageId1);
|
||||
oneOf(db).deleteMessageMetadata(txn2, messageId1);
|
||||
// Recursively invalidate dependents
|
||||
oneOf(db).getMessageDependents(txn3, messageId1);
|
||||
oneOf(db).getMessageDependents(txn2, messageId1);
|
||||
will(returnValue(emptyMap()));
|
||||
// Get pending messages to deliver
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn4));
|
||||
oneOf(db).getPendingMessages(txn4);
|
||||
will(returnValue(emptyList()));
|
||||
// Get messages to share
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn5));
|
||||
oneOf(db).getMessagesToShare(txn5);
|
||||
will(returnValue(emptyList()));
|
||||
}});
|
||||
|
||||
expectGetPendingMessages();
|
||||
expectGetMessagesToShare();
|
||||
|
||||
vm.startService();
|
||||
}
|
||||
|
||||
@@ -369,52 +310,42 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
|
||||
throws Exception {
|
||||
Transaction txn = new Transaction(null, true);
|
||||
Transaction txn1 = new Transaction(null, true);
|
||||
Transaction txn2 = new Transaction(null, true);
|
||||
Transaction txn3 = new Transaction(null, false);
|
||||
Transaction txn4 = new Transaction(null, true);
|
||||
Transaction txn5 = new Transaction(null, true);
|
||||
Transaction txn2 = new Transaction(null, false);
|
||||
|
||||
expectGetMessagesToValidate(messageId, messageId1);
|
||||
|
||||
context.checking(new DbExpectations() {{
|
||||
// Get messages to validate
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||
oneOf(db).getMessagesToValidate(txn);
|
||||
will(returnValue(asList(messageId, messageId1)));
|
||||
// Load the first raw message
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn1));
|
||||
oneOf(db).getMessage(txn1, messageId);
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||
oneOf(db).getMessage(txn, messageId);
|
||||
will(returnValue(message));
|
||||
// Load the group - *gasp* it's gone!
|
||||
oneOf(db).getGroup(txn1, groupId);
|
||||
oneOf(db).getGroup(txn, groupId);
|
||||
will(throwException(new NoSuchGroupException()));
|
||||
// Load the second raw message and group
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn2));
|
||||
oneOf(db).getMessage(txn2, messageId1);
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn1));
|
||||
oneOf(db).getMessage(txn1, messageId1);
|
||||
will(returnValue(message1));
|
||||
oneOf(db).getGroup(txn2, groupId);
|
||||
oneOf(db).getGroup(txn1, groupId);
|
||||
will(returnValue(group));
|
||||
// Validate the second message: invalid
|
||||
oneOf(validator).validateMessage(message1, group);
|
||||
will(throwException(new InvalidMessageException()));
|
||||
// Store the validation result for the second message
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn3));
|
||||
oneOf(db).getMessageState(txn3, messageId1);
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn2));
|
||||
oneOf(db).getMessageState(txn2, messageId1);
|
||||
will(returnValue(UNKNOWN));
|
||||
oneOf(db).setMessageState(txn3, messageId1, INVALID);
|
||||
oneOf(db).deleteMessage(txn3, messageId1);
|
||||
oneOf(db).deleteMessageMetadata(txn3, messageId1);
|
||||
oneOf(db).setMessageState(txn2, messageId1, INVALID);
|
||||
oneOf(db).deleteMessage(txn2, messageId1);
|
||||
oneOf(db).deleteMessageMetadata(txn2, messageId1);
|
||||
// Recursively invalidate dependents
|
||||
oneOf(db).getMessageDependents(txn3, messageId1);
|
||||
oneOf(db).getMessageDependents(txn2, messageId1);
|
||||
will(returnValue(emptyMap()));
|
||||
// Get pending messages to deliver
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn4));
|
||||
oneOf(db).getPendingMessages(txn4);
|
||||
will(returnValue(emptyList()));
|
||||
// Get messages to share
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn5));
|
||||
oneOf(db).getMessagesToShare(txn5);
|
||||
will(returnValue(emptyList()));
|
||||
}});
|
||||
|
||||
expectGetPendingMessages();
|
||||
expectGetMessagesToShare();
|
||||
|
||||
vm.startService();
|
||||
}
|
||||
|
||||
@@ -801,4 +732,35 @@ public class ValidationManagerImplTest extends BrambleMockTestCase {
|
||||
|
||||
vm.eventOccurred(new MessageAddedEvent(message, contactId));
|
||||
}
|
||||
|
||||
private void expectGetMessagesToValidate(MessageId... ids)
|
||||
throws Exception {
|
||||
Transaction txn = new Transaction(null, true);
|
||||
|
||||
context.checking(new DbExpectations() {{
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||
oneOf(db).getMessagesToValidate(txn);
|
||||
will(returnValue(asList(ids)));
|
||||
}});
|
||||
}
|
||||
|
||||
private void expectGetPendingMessages(MessageId... ids) throws Exception {
|
||||
Transaction txn = new Transaction(null, true);
|
||||
|
||||
context.checking(new DbExpectations() {{
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||
oneOf(db).getPendingMessages(txn);
|
||||
will(returnValue(asList(ids)));
|
||||
}});
|
||||
}
|
||||
|
||||
private void expectGetMessagesToShare(MessageId... ids) throws Exception {
|
||||
Transaction txn = new Transaction(null, true);
|
||||
|
||||
context.checking(new DbExpectations() {{
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||
oneOf(db).getMessagesToShare(txn);
|
||||
will(returnValue(asList(ids)));
|
||||
}});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.api.FeatureFlags;
|
||||
import org.briarproject.bramble.battery.DefaultBatteryManagerModule;
|
||||
import org.briarproject.bramble.event.DefaultEventExecutorModule;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
@Module(includes = {
|
||||
DefaultBatteryManagerModule.class,
|
||||
@@ -13,4 +15,20 @@ import dagger.Module;
|
||||
TestSecureRandomModule.class
|
||||
})
|
||||
public class BrambleCoreIntegrationTestModule {
|
||||
|
||||
@Provides
|
||||
FeatureFlags provideFeatureFlags() {
|
||||
return new FeatureFlags() {
|
||||
|
||||
@Override
|
||||
public boolean shouldEnableImageAttachments() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldEnablePrivateMessageDeletion() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ public class ClientVersioningManagerImplTest extends BrambleMockTestCase {
|
||||
localUpdateBody);
|
||||
will(returnValue(localUpdate));
|
||||
oneOf(clientHelper).addLocalMessage(txn, localUpdate,
|
||||
localUpdateMeta, true);
|
||||
localUpdateMeta, true, false);
|
||||
}});
|
||||
}
|
||||
|
||||
@@ -172,7 +172,7 @@ public class ClientVersioningManagerImplTest extends BrambleMockTestCase {
|
||||
localVersionsBody);
|
||||
will(returnValue(localVersions));
|
||||
oneOf(db).addLocalMessage(txn, localVersions, new Metadata(),
|
||||
false);
|
||||
false, false);
|
||||
// Inform contacts that client versions have changed
|
||||
oneOf(db).getContacts(txn);
|
||||
will(returnValue(singletonList(contact)));
|
||||
@@ -259,7 +259,7 @@ public class ClientVersioningManagerImplTest extends BrambleMockTestCase {
|
||||
newLocalVersionsBody);
|
||||
will(returnValue(newLocalVersions));
|
||||
oneOf(db).addLocalMessage(txn, newLocalVersions, new Metadata(),
|
||||
false);
|
||||
false, false);
|
||||
// Inform contacts that client versions have changed
|
||||
oneOf(db).getContacts(txn);
|
||||
will(returnValue(singletonList(contact)));
|
||||
@@ -284,7 +284,7 @@ public class ClientVersioningManagerImplTest extends BrambleMockTestCase {
|
||||
newLocalUpdateBody);
|
||||
will(returnValue(newLocalUpdate));
|
||||
oneOf(clientHelper).addLocalMessage(txn, newLocalUpdate,
|
||||
newLocalUpdateMeta, true);
|
||||
newLocalUpdateMeta, true, false);
|
||||
// No visibilities have changed
|
||||
}});
|
||||
|
||||
@@ -355,7 +355,7 @@ public class ClientVersioningManagerImplTest extends BrambleMockTestCase {
|
||||
newLocalVersionsBody);
|
||||
will(returnValue(newLocalVersions));
|
||||
oneOf(db).addLocalMessage(txn, newLocalVersions, new Metadata(),
|
||||
false);
|
||||
false, false);
|
||||
// Inform contacts that client versions have changed
|
||||
oneOf(db).getContacts(txn);
|
||||
will(returnValue(singletonList(contact)));
|
||||
@@ -382,7 +382,7 @@ public class ClientVersioningManagerImplTest extends BrambleMockTestCase {
|
||||
newLocalUpdateBody);
|
||||
will(returnValue(newLocalUpdate));
|
||||
oneOf(clientHelper).addLocalMessage(txn, newLocalUpdate,
|
||||
newLocalUpdateMeta, true);
|
||||
newLocalUpdateMeta, true, false);
|
||||
// The client's visibility has changed
|
||||
oneOf(hook).onClientVisibilityChanging(txn, contact, visibility);
|
||||
}});
|
||||
@@ -567,7 +567,7 @@ public class ClientVersioningManagerImplTest extends BrambleMockTestCase {
|
||||
newLocalUpdateBody);
|
||||
will(returnValue(newLocalUpdate));
|
||||
oneOf(clientHelper).addLocalMessage(txn, newLocalUpdate,
|
||||
newLocalUpdateMeta, true);
|
||||
newLocalUpdateMeta, true, false);
|
||||
// The client's visibility has changed
|
||||
oneOf(clientHelper).getGroupMetadataAsDictionary(txn,
|
||||
contactGroup.getId());
|
||||
@@ -640,7 +640,7 @@ public class ClientVersioningManagerImplTest extends BrambleMockTestCase {
|
||||
newLocalUpdateBody);
|
||||
will(returnValue(newLocalUpdate));
|
||||
oneOf(clientHelper).addLocalMessage(txn, newLocalUpdate,
|
||||
newLocalUpdateMeta, true);
|
||||
newLocalUpdateMeta, true, false);
|
||||
// The client's visibility has changed
|
||||
oneOf(clientHelper).getGroupMetadataAsDictionary(txn,
|
||||
contactGroup.getId());
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[main]
|
||||
host = https://www.transifex.com
|
||||
lang_map = pt_BR: pt-rBR, nb_NO: nb, zh-Hans: zh-rCN
|
||||
lang_map = pt_BR: pt-rBR, nb_NO: nb, zh-Hans: zh-rCN, zh-Hant: zh-rTW
|
||||
|
||||
[briar.stringsxml-5]
|
||||
file_filter = src/main/res/values-<lang>/strings.xml
|
||||
|
||||
@@ -20,10 +20,10 @@ android {
|
||||
buildToolsVersion '28.0.3'
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 15
|
||||
targetSdkVersion 26
|
||||
versionCode 10107
|
||||
versionName "1.1.7"
|
||||
minSdkVersion 16
|
||||
targetSdkVersion 28
|
||||
versionCode 10204
|
||||
versionName "1.2.4"
|
||||
applicationId "org.briarproject.briar.android"
|
||||
buildConfigField "String", "GitHash",
|
||||
"\"${getStdout(['git', 'rev-parse', '--short=7', 'HEAD'], 'No commit hash')}\""
|
||||
@@ -117,7 +117,7 @@ dependencies {
|
||||
implementation 'de.hdodenhof:circleimageview:2.2.0'
|
||||
implementation 'com.google.zxing:core:3.3.3'
|
||||
implementation 'uk.co.samuelwall:material-tap-target-prompt:2.14.0'
|
||||
implementation 'com.vanniktech:emoji-google:0.5.1'
|
||||
implementation 'com.vanniktech:emoji-google:0.6.0' // later versions already use androidx
|
||||
implementation 'com.github.kobakei:MaterialFabSpeedDial:1.2.1' // later versions already use androidx
|
||||
def glideVersion = '4.9.0'
|
||||
implementation("com.github.bumptech.glide:glide:$glideVersion") {
|
||||
|
||||
@@ -5,6 +5,10 @@
|
||||
|
||||
# QR codes
|
||||
-keep class com.google.zxing.Result
|
||||
-keepclassmembers enum * {
|
||||
public static **[] values();
|
||||
public static ** valueOf(java.lang.String);
|
||||
}
|
||||
|
||||
# RSS libraries
|
||||
-keep,includedescriptorclasses class com.rometools.rome.feed.synd.impl.** { *; }
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.briarproject.bramble.BrambleAndroidModule;
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.bramble.account.BriarAccountModule;
|
||||
import org.briarproject.briar.BriarCoreModule;
|
||||
import org.briarproject.briar.android.attachment.AttachmentModule;
|
||||
import org.briarproject.briar.android.navdrawer.NavDrawerActivityTest;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
@@ -13,6 +14,7 @@ import dagger.Component;
|
||||
@Singleton
|
||||
@Component(modules = {
|
||||
AppModule.class,
|
||||
AttachmentModule.class,
|
||||
BriarCoreModule.class,
|
||||
BrambleAndroidModule.class,
|
||||
BriarAccountModule.class,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.briarproject.briar.android.conversation;
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.content.res.AssetManager;
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
@@ -21,7 +21,7 @@ import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class AttachmentControllerIntegrationTest {
|
||||
public class AttachmentRetrieverIntegrationTest {
|
||||
|
||||
private static final String smallKitten =
|
||||
"https://upload.wikimedia.org/wikipedia/commons/thumb/0/06/Kitten_in_Rizal_Park%2C_Manila.jpg/160px-Kitten_in_Rizal_Park%2C_Manila.jpg";
|
||||
@@ -47,15 +47,17 @@ public class AttachmentControllerIntegrationTest {
|
||||
);
|
||||
private final MessageId msgId = new MessageId(getRandomId());
|
||||
|
||||
private final AttachmentController controller =
|
||||
new AttachmentController(null, dimensions);
|
||||
private final ImageHelper imageHelper = new ImageHelperImpl();
|
||||
private final AttachmentRetriever retriever =
|
||||
new AttachmentRetrieverImpl(null, dimensions, imageHelper,
|
||||
new ImageSizeCalculator(imageHelper));
|
||||
|
||||
@Test
|
||||
public void testSmallJpegImage() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(smallKitten);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = controller.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(msgId, item.getMessageId());
|
||||
assertEquals(160, item.getWidth());
|
||||
assertEquals(240, item.getHeight());
|
||||
@@ -70,8 +72,8 @@ public class AttachmentControllerIntegrationTest {
|
||||
public void testBigJpegImage() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(originalKitten);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = controller.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(msgId, item.getMessageId());
|
||||
assertEquals(1728, item.getWidth());
|
||||
assertEquals(2592, item.getHeight());
|
||||
@@ -86,8 +88,8 @@ public class AttachmentControllerIntegrationTest {
|
||||
public void testSmallPngImage() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/png");
|
||||
InputStream is = getUrlInputStream(pngKitten);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = controller.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(msgId, item.getMessageId());
|
||||
assertEquals(737, item.getWidth());
|
||||
assertEquals(510, item.getHeight());
|
||||
@@ -102,8 +104,8 @@ public class AttachmentControllerIntegrationTest {
|
||||
public void testUberGif() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(uberGif);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = controller.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(1, item.getWidth());
|
||||
assertEquals(1, item.getHeight());
|
||||
assertEquals(dimensions.minHeight, item.getThumbnailWidth());
|
||||
@@ -117,8 +119,8 @@ public class AttachmentControllerIntegrationTest {
|
||||
public void testLottaPixels() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(lottaPixel);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = controller.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(64250, item.getWidth());
|
||||
assertEquals(64250, item.getHeight());
|
||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||
@@ -132,8 +134,8 @@ public class AttachmentControllerIntegrationTest {
|
||||
public void testImageIoCrash() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(imageIoCrash);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = controller.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(1184, item.getWidth());
|
||||
assertEquals(448, item.getHeight());
|
||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||
@@ -147,8 +149,8 @@ public class AttachmentControllerIntegrationTest {
|
||||
public void testGimpCrash() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(gimpCrash);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = controller.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(1, item.getWidth());
|
||||
assertEquals(1, item.getHeight());
|
||||
assertEquals(dimensions.minHeight, item.getThumbnailWidth());
|
||||
@@ -162,8 +164,8 @@ public class AttachmentControllerIntegrationTest {
|
||||
public void testOptiPngAfl() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(optiPngAfl);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = controller.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(32, item.getWidth());
|
||||
assertEquals(32, item.getHeight());
|
||||
assertEquals(dimensions.minHeight, item.getThumbnailWidth());
|
||||
@@ -177,8 +179,8 @@ public class AttachmentControllerIntegrationTest {
|
||||
public void testLibrawError() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(librawError);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = controller.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertTrue(item.hasError());
|
||||
}
|
||||
|
||||
@@ -186,8 +188,8 @@ public class AttachmentControllerIntegrationTest {
|
||||
public void testSmallAnimatedGifMaxDimensions() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
||||
InputStream is = getAssetInputStream("animated.gif");
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = controller.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(65535, item.getWidth());
|
||||
assertEquals(65535, item.getHeight());
|
||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||
@@ -201,8 +203,8 @@ public class AttachmentControllerIntegrationTest {
|
||||
public void testSmallAnimatedGifHugeDimensions() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
||||
InputStream is = getAssetInputStream("animated2.gif");
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = controller.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(10000, item.getWidth());
|
||||
assertEquals(10000, item.getHeight());
|
||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||
@@ -216,8 +218,8 @@ public class AttachmentControllerIntegrationTest {
|
||||
public void testSmallGifLargeDimensions() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
||||
InputStream is = getAssetInputStream("error_large.gif");
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = controller.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(16384, item.getWidth());
|
||||
assertEquals(16384, item.getHeight());
|
||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||
@@ -231,8 +233,8 @@ public class AttachmentControllerIntegrationTest {
|
||||
public void testHighError() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getAssetInputStream("error_high.jpg");
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = controller.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(1, item.getWidth());
|
||||
assertEquals(10000, item.getHeight());
|
||||
assertEquals(dimensions.minWidth, item.getThumbnailWidth());
|
||||
@@ -246,8 +248,8 @@ public class AttachmentControllerIntegrationTest {
|
||||
public void testWideError() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getAssetInputStream("error_wide.jpg");
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = controller.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(1920, item.getWidth());
|
||||
assertEquals(1, item.getHeight());
|
||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||
@@ -73,7 +73,7 @@
|
||||
android:label="@string/crash_report_title"
|
||||
android:launchMode="singleInstance"
|
||||
android:theme="@style/BriarTheme.NoActionBar"
|
||||
android:windowSoftInputMode="stateHidden">
|
||||
android:windowSoftInputMode="adjustResize|stateHidden">
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
@@ -89,7 +89,7 @@
|
||||
<activity
|
||||
android:name="org.briarproject.briar.android.account.SetupActivity"
|
||||
android:label="@string/setup_title"
|
||||
android:windowSoftInputMode="adjustResize">
|
||||
android:windowSoftInputMode="adjustResize|stateAlwaysVisible">
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
@@ -126,7 +126,7 @@
|
||||
android:label="@string/app_name"
|
||||
android:parentActivityName="org.briarproject.briar.android.navdrawer.NavDrawerActivity"
|
||||
android:theme="@style/BriarTheme.NoActionBar"
|
||||
android:windowSoftInputMode="stateHidden|adjustResize">
|
||||
android:windowSoftInputMode="adjustResize|stateUnchanged">
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="org.briarproject.briar.android.navdrawer.NavDrawerActivity"/>
|
||||
@@ -145,7 +145,7 @@
|
||||
android:name="org.briarproject.briar.android.privategroup.creation.CreateGroupActivity"
|
||||
android:label="@string/groups_create_group_title"
|
||||
android:parentActivityName="org.briarproject.briar.android.navdrawer.NavDrawerActivity"
|
||||
android:windowSoftInputMode="adjustResize">
|
||||
android:windowSoftInputMode="adjustResize|stateAlwaysVisible">
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="org.briarproject.briar.android.navdrawer.NavDrawerActivity"/>
|
||||
@@ -174,8 +174,7 @@
|
||||
<activity
|
||||
android:name="org.briarproject.briar.android.privategroup.memberlist.GroupMemberListActivity"
|
||||
android:label="@string/groups_member_list"
|
||||
android:parentActivityName="org.briarproject.briar.android.privategroup.conversation.GroupActivity"
|
||||
android:windowSoftInputMode="adjustResize|stateHidden">
|
||||
android:parentActivityName="org.briarproject.briar.android.privategroup.conversation.GroupActivity">
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="org.briarproject.briar.android.privategroup.conversation.GroupActivity"/>
|
||||
@@ -184,8 +183,7 @@
|
||||
<activity
|
||||
android:name="org.briarproject.briar.android.privategroup.reveal.RevealContactsActivity"
|
||||
android:label="@string/groups_reveal_contacts"
|
||||
android:parentActivityName="org.briarproject.briar.android.privategroup.conversation.GroupActivity"
|
||||
android:windowSoftInputMode="adjustResize|stateAlwaysHidden">
|
||||
android:parentActivityName="org.briarproject.briar.android.privategroup.conversation.GroupActivity">
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="org.briarproject.briar.android.privategroup.conversation.GroupActivity"/>
|
||||
@@ -223,7 +221,7 @@
|
||||
android:name="org.briarproject.briar.android.forum.CreateForumActivity"
|
||||
android:label="@string/create_forum_title"
|
||||
android:parentActivityName="org.briarproject.briar.android.navdrawer.NavDrawerActivity"
|
||||
android:windowSoftInputMode="adjustResize">
|
||||
android:windowSoftInputMode="adjustResize|stateAlwaysVisible">
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="org.briarproject.briar.android.navdrawer.NavDrawerActivity"/>
|
||||
@@ -292,7 +290,7 @@
|
||||
android:name="org.briarproject.briar.android.blog.WriteBlogPostActivity"
|
||||
android:label="@string/blogs_write_blog_post"
|
||||
android:parentActivityName="org.briarproject.briar.android.blog.BlogActivity"
|
||||
android:windowSoftInputMode="stateVisible|adjustResize">
|
||||
android:windowSoftInputMode="adjustResize|stateAlwaysVisible">
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="org.briarproject.briar.android.blog.BlogActivity"/>
|
||||
@@ -302,7 +300,7 @@
|
||||
android:name="org.briarproject.briar.android.blog.ReblogActivity"
|
||||
android:label="@string/blogs_reblog_button"
|
||||
android:parentActivityName="org.briarproject.briar.android.blog.BlogActivity"
|
||||
android:windowSoftInputMode="stateHidden">
|
||||
android:windowSoftInputMode="adjustResize|stateHidden">
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="org.briarproject.briar.android.blog.BlogActivity"/>
|
||||
@@ -312,7 +310,7 @@
|
||||
android:name="org.briarproject.briar.android.blog.RssFeedImportActivity"
|
||||
android:label="@string/blogs_rss_feeds_import"
|
||||
android:parentActivityName="org.briarproject.briar.android.navdrawer.NavDrawerActivity"
|
||||
android:windowSoftInputMode="stateVisible|adjustResize">
|
||||
android:windowSoftInputMode="adjustResize|stateAlwaysVisible">
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="org.briarproject.briar.android.navdrawer.NavDrawerActivity"/>
|
||||
@@ -341,7 +339,7 @@
|
||||
android:name="org.briarproject.briar.android.introduction.IntroductionActivity"
|
||||
android:label="@string/introduction_activity_title"
|
||||
android:parentActivityName="org.briarproject.briar.android.conversation.ConversationActivity"
|
||||
android:windowSoftInputMode="stateHidden|adjustResize">
|
||||
android:windowSoftInputMode="adjustResize|stateHidden">
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="org.briarproject.briar.android.conversation.ConversationActivity"/>
|
||||
@@ -369,7 +367,8 @@
|
||||
<activity
|
||||
android:name="org.briarproject.briar.android.login.ChangePasswordActivity"
|
||||
android:label="@string/change_password"
|
||||
android:parentActivityName="org.briarproject.briar.android.settings.SettingsActivity">
|
||||
android:parentActivityName="org.briarproject.briar.android.settings.SettingsActivity"
|
||||
android:windowSoftInputMode="adjustResize|stateAlwaysVisible">
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value="org.briarproject.briar.android.settings.SettingsActivity"/>
|
||||
@@ -424,7 +423,7 @@
|
||||
android:name=".android.contact.add.remote.AddContactActivity"
|
||||
android:label="@string/add_contact_remotely_title_case"
|
||||
android:theme="@style/BriarTheme"
|
||||
android:windowSoftInputMode="stateHidden|adjustResize"/>
|
||||
android:windowSoftInputMode="adjustResize|stateHidden"/>
|
||||
|
||||
<activity
|
||||
android:name=".android.contact.add.remote.PendingContactListActivity"
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.briarproject.bramble.BrambleAndroidModule;
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.bramble.account.BriarAccountModule;
|
||||
import org.briarproject.bramble.api.FeatureFlags;
|
||||
import org.briarproject.bramble.api.account.AccountManager;
|
||||
import org.briarproject.bramble.api.contact.ContactExchangeManager;
|
||||
import org.briarproject.bramble.api.contact.ContactManager;
|
||||
@@ -29,6 +30,7 @@ import org.briarproject.bramble.api.system.LocationUtils;
|
||||
import org.briarproject.bramble.plugin.tor.CircumventionProvider;
|
||||
import org.briarproject.briar.BriarCoreEagerSingletons;
|
||||
import org.briarproject.briar.BriarCoreModule;
|
||||
import org.briarproject.briar.android.attachment.AttachmentModule;
|
||||
import org.briarproject.briar.android.conversation.glide.BriarModelLoader;
|
||||
import org.briarproject.briar.android.login.SignInReminderReceiver;
|
||||
import org.briarproject.briar.android.reporting.BriarReportSender;
|
||||
@@ -67,7 +69,8 @@ import dagger.Component;
|
||||
BriarCoreModule.class,
|
||||
BrambleAndroidModule.class,
|
||||
BriarAccountModule.class,
|
||||
AppModule.class
|
||||
AppModule.class,
|
||||
AttachmentModule.class
|
||||
})
|
||||
public interface AndroidComponent
|
||||
extends BrambleCoreEagerSingletons, BrambleAndroidEagerSingletons,
|
||||
@@ -161,6 +164,8 @@ public interface AndroidComponent
|
||||
|
||||
ViewModelProvider.Factory viewModelFactory();
|
||||
|
||||
FeatureFlags featureFlags();
|
||||
|
||||
void inject(SignInReminderReceiver briarService);
|
||||
|
||||
void inject(BriarService briarService);
|
||||
|
||||
@@ -347,8 +347,10 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
|
||||
if (currentTime - lastSound > SOUND_DELAY) {
|
||||
boolean sound = settings.getBoolean(PREF_NOTIFY_SOUND, true);
|
||||
String ringtoneUri = settings.get(PREF_NOTIFY_RINGTONE_URI);
|
||||
if (sound && !StringUtils.isNullOrEmpty(ringtoneUri))
|
||||
b.setSound(Uri.parse(ringtoneUri));
|
||||
if (sound && !StringUtils.isNullOrEmpty(ringtoneUri)) {
|
||||
Uri uri = Uri.parse(ringtoneUri);
|
||||
if (!"file".equals(uri.getScheme())) b.setSound(uri);
|
||||
}
|
||||
b.setDefaults(getDefaults());
|
||||
lastSound = currentTime;
|
||||
}
|
||||
@@ -359,7 +361,8 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
|
||||
int defaults = DEFAULT_LIGHTS;
|
||||
boolean sound = settings.getBoolean(PREF_NOTIFY_SOUND, true);
|
||||
String ringtoneUri = settings.get(PREF_NOTIFY_RINGTONE_URI);
|
||||
if (sound && StringUtils.isNullOrEmpty(ringtoneUri))
|
||||
if (sound && (StringUtils.isNullOrEmpty(ringtoneUri) ||
|
||||
"file".equals(Uri.parse(ringtoneUri).getScheme())))
|
||||
defaults |= DEFAULT_SOUND;
|
||||
if (settings.getBoolean(PREF_NOTIFY_VIBRATION, true))
|
||||
defaults |= DEFAULT_VIBRATE;
|
||||
|
||||
@@ -7,6 +7,7 @@ import android.os.StrictMode;
|
||||
|
||||
import com.vanniktech.emoji.RecentEmoji;
|
||||
|
||||
import org.briarproject.bramble.api.FeatureFlags;
|
||||
import org.briarproject.bramble.api.battery.BatteryManager;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.PublicKey;
|
||||
@@ -59,6 +60,7 @@ import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static org.briarproject.bramble.api.reporting.ReportingConstants.DEV_ONION_ADDRESS;
|
||||
import static org.briarproject.bramble.api.reporting.ReportingConstants.DEV_PUBLIC_KEY_HEX;
|
||||
import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
||||
|
||||
@Module(includes = {ContactExchangeModule.class, ViewModelModule.class})
|
||||
public class AppModule {
|
||||
@@ -230,4 +232,20 @@ public class AppModule {
|
||||
lifecycleManager.registerOpenDatabaseHook(recentEmoji);
|
||||
return recentEmoji;
|
||||
}
|
||||
|
||||
@Provides
|
||||
FeatureFlags provideFeatureFlags() {
|
||||
return new FeatureFlags() {
|
||||
|
||||
@Override
|
||||
public boolean shouldEnableImageAttachments() {
|
||||
return IS_DEBUG_BUILD;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldEnablePrivateMessageDeletion() {
|
||||
return IS_DEBUG_BUILD;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
package org.briarproject.briar.android;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Application.ActivityLifecycleCallbacks;
|
||||
import android.os.Bundle;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
class BackgroundMonitor implements ActivityLifecycleCallbacks {
|
||||
|
||||
private final AtomicInteger foregroundActivities = new AtomicInteger(0);
|
||||
|
||||
boolean isRunningInBackground() {
|
||||
return foregroundActivities.get() == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Activity a, @Nullable Bundle state) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityStarted(Activity a) {
|
||||
foregroundActivities.incrementAndGet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResumed(Activity a) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityPaused(Activity a) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityStopped(Activity a) {
|
||||
foregroundActivities.decrementAndGet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivitySaveInstanceState(Activity a,
|
||||
@Nullable Bundle outState) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityDestroyed(Activity a) {
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import org.acra.annotation.ReportsCrashes;
|
||||
import org.briarproject.bramble.BrambleAndroidModule;
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.briar.BriarCoreModule;
|
||||
import org.briarproject.briar.BuildConfig;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.logging.CachingLogHandler;
|
||||
import org.briarproject.briar.android.reporting.BriarReportPrimer;
|
||||
@@ -33,9 +34,9 @@ import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static java.util.logging.Level.FINE;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.acra.ReportField.ANDROID_VERSION;
|
||||
import static org.acra.ReportField.APP_VERSION_CODE;
|
||||
import static org.acra.ReportField.APP_VERSION_NAME;
|
||||
@@ -64,6 +65,7 @@ import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
||||
reportDialogClass = DevReportActivity.class,
|
||||
resDialogOkToast = R.string.dev_report_saved,
|
||||
deleteOldUnsentReportsOnApplicationStart = false,
|
||||
buildConfigClass = BuildConfig.class,
|
||||
customReportContent = {
|
||||
REPORT_ID,
|
||||
APP_VERSION_CODE, APP_VERSION_NAME, PACKAGE_NAME,
|
||||
@@ -80,10 +82,9 @@ public class BriarApplicationImpl extends Application
|
||||
implements BriarApplication {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(BriarApplicationImpl.class.getName());
|
||||
getLogger(BriarApplicationImpl.class.getName());
|
||||
|
||||
private final CachingLogHandler logHandler = new CachingLogHandler();
|
||||
private final BackgroundMonitor backgroundMonitor = new BackgroundMonitor();
|
||||
|
||||
private AndroidComponent applicationComponent;
|
||||
private volatile SharedPreferences prefs;
|
||||
@@ -106,12 +107,16 @@ public class BriarApplicationImpl extends Application
|
||||
|
||||
if (IS_DEBUG_BUILD) enableStrictMode();
|
||||
|
||||
Logger rootLogger = Logger.getLogger("");
|
||||
if (!IS_DEBUG_BUILD && !IS_BETA_BUILD) {
|
||||
// Remove default log handlers so system log is not used
|
||||
for (Handler handler : rootLogger.getHandlers()) {
|
||||
rootLogger.removeHandler(handler);
|
||||
}
|
||||
Logger rootLogger = getLogger("");
|
||||
Handler[] handlers = rootLogger.getHandlers();
|
||||
// Disable the Android logger for release builds
|
||||
for (Handler handler : handlers) rootLogger.removeHandler(handler);
|
||||
if (IS_DEBUG_BUILD || IS_BETA_BUILD) {
|
||||
// We can't set the level of the Android logger at runtime, so
|
||||
// raise records to the logger's default level
|
||||
rootLogger.addHandler(new LevelRaisingHandler(FINE, INFO));
|
||||
// Restore the default handlers after the level raising handler
|
||||
for (Handler handler : handlers) rootLogger.addHandler(handler);
|
||||
}
|
||||
rootLogger.addHandler(logHandler);
|
||||
rootLogger.setLevel(IS_DEBUG_BUILD || IS_BETA_BUILD ? FINE : INFO);
|
||||
@@ -120,9 +125,6 @@ public class BriarApplicationImpl extends Application
|
||||
|
||||
applicationComponent = createApplicationComponent();
|
||||
EmojiManager.install(new GoogleEmojiProvider());
|
||||
|
||||
if (SDK_INT < 16)
|
||||
registerActivityLifecycleCallbacks(backgroundMonitor);
|
||||
}
|
||||
|
||||
protected AndroidComponent createApplicationComponent() {
|
||||
@@ -184,12 +186,8 @@ public class BriarApplicationImpl extends Application
|
||||
|
||||
@Override
|
||||
public boolean isRunningInBackground() {
|
||||
if (SDK_INT >= 16) {
|
||||
RunningAppProcessInfo info = new RunningAppProcessInfo();
|
||||
ActivityManager.getMyMemoryState(info);
|
||||
return (info.importance != IMPORTANCE_FOREGROUND);
|
||||
} else {
|
||||
return backgroundMonitor.isRunningInBackground();
|
||||
}
|
||||
RunningAppProcessInfo info = new RunningAppProcessInfo();
|
||||
ActivityManager.getMyMemoryState(info);
|
||||
return (info.importance != IMPORTANCE_FOREGROUND);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,8 +238,6 @@ public class BriarService extends Service {
|
||||
} else if (level == TRIM_MEMORY_RUNNING_LOW) {
|
||||
LOG.info("Trim memory: running low");
|
||||
} else if (level == TRIM_MEMORY_RUNNING_CRITICAL) {
|
||||
// This level may be received if SDK_INT < 16, although the
|
||||
// constant isn't declared until API level 16
|
||||
LOG.warning("Trim memory: running critically low");
|
||||
// If we're not in the foreground, clear the UI to save memory
|
||||
if (app.isRunningInBackground()) hideUi();
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.briarproject.briar.android;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* Log handler that raises all records at or above a given source level to a
|
||||
* given destination level. This affects the level seen by subsequent handlers.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
class LevelRaisingHandler extends Handler {
|
||||
|
||||
private final Level dest;
|
||||
private final int srcInt, destInt;
|
||||
|
||||
LevelRaisingHandler(Level src, Level dest) {
|
||||
this.dest = dest;
|
||||
srcInt = src.intValue();
|
||||
destInt = dest.intValue();
|
||||
if (srcInt > destInt) throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publish(LogRecord record) {
|
||||
int recordInt = record.getLevel().intValue();
|
||||
if (recordInt >= srcInt && recordInt < destInt) record.setLevel(dest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
}
|
||||
@@ -150,7 +150,7 @@ class ScreenFilterMonitorImpl implements ScreenFilterMonitor, Service {
|
||||
// Get permissions
|
||||
String[] requestedPermissions = packageInfo.requestedPermissions;
|
||||
if (requestedPermissions == null) return false;
|
||||
if (SDK_INT >= 16 && SDK_INT < 23) {
|
||||
if (SDK_INT < 23) {
|
||||
// Check whether the permission has been requested and granted
|
||||
int[] flags = packageInfo.requestedPermissionsFlags;
|
||||
for (int i = 0; i < requestedPermissions.length; i++) {
|
||||
|
||||
@@ -30,15 +30,4 @@ public interface TestingConstants {
|
||||
long EXPIRY_DATE = IS_DEBUG_BUILD || IS_BETA_BUILD ?
|
||||
BuildConfig.BuildTimestamp + 90 * 24 * 60 * 60 * 1000L :
|
||||
Long.MAX_VALUE;
|
||||
|
||||
/**
|
||||
* Feature flag for enabling image attachments.
|
||||
*/
|
||||
boolean FEATURE_FLAG_IMAGE_ATTACHMENTS = false;
|
||||
|
||||
/**
|
||||
* Feature flag for enabling adding contacts at a distance.
|
||||
*/
|
||||
boolean FEATURE_FLAG_REMOTE_CONTACTS = IS_DEBUG_BUILD;
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.briarproject.briar.android.account;
|
||||
import android.os.Bundle;
|
||||
import android.support.design.widget.TextInputEditText;
|
||||
import android.support.design.widget.TextInputLayout;
|
||||
import android.text.Editable;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@@ -10,18 +11,15 @@ import android.widget.Button;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static android.view.inputmethod.EditorInfo.IME_ACTION_NEXT;
|
||||
import static android.view.inputmethod.EditorInfo.IME_ACTION_NONE;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||
import static org.briarproject.bramble.util.StringUtils.toUtf8;
|
||||
import static org.briarproject.briar.android.util.UiUtils.setError;
|
||||
import static org.briarproject.briar.android.util.UiUtils.showSoftKeyboard;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
@@ -64,12 +62,6 @@ public class AuthorNameFragment extends SetupFragment {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
showSoftKeyboard(authorNameInput);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getHelpText() {
|
||||
return getString(R.string.setup_name_explanation);
|
||||
@@ -77,20 +69,21 @@ public class AuthorNameFragment extends SetupFragment {
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence authorName, int i, int i1, int i2) {
|
||||
int authorNameLength = StringUtils.toUtf8(authorName.toString()).length;
|
||||
int authorNameLength = toUtf8(authorName.toString().trim()).length;
|
||||
boolean error = authorNameLength > MAX_AUTHOR_NAME_LENGTH;
|
||||
setError(authorNameWrapper, getString(R.string.name_too_long), error);
|
||||
boolean enabled = authorNameLength > 0 && !error;
|
||||
authorNameInput
|
||||
.setImeOptions(enabled ? IME_ACTION_NEXT : IME_ACTION_NONE);
|
||||
authorNameInput.setOnEditorActionListener(enabled ? this : null);
|
||||
nextButton.setEnabled(enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
setupController.setAuthorName(authorNameInput.getText().toString());
|
||||
setupController.showPasswordFragment();
|
||||
Editable text = authorNameInput.getText();
|
||||
if (text != null) {
|
||||
setupController.setAuthorName(text.toString().trim());
|
||||
setupController.showPasswordFragment();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -61,7 +61,6 @@ public class SetPasswordFragment extends SetupFragment {
|
||||
strengthMeter = v.findViewById(R.id.strength_meter);
|
||||
passwordEntryWrapper = v.findViewById(R.id.password_entry_wrapper);
|
||||
passwordEntry = v.findViewById(R.id.password_entry);
|
||||
passwordEntry.requestFocus();
|
||||
passwordConfirmationWrapper =
|
||||
v.findViewById(R.id.password_confirm_wrapper);
|
||||
passwordConfirmation = v.findViewById(R.id.password_confirm);
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.briarproject.briar.android.activity;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.support.annotation.LayoutRes;
|
||||
import android.support.annotation.UiThread;
|
||||
import android.support.v4.app.Fragment;
|
||||
@@ -12,7 +11,6 @@ import android.support.v7.widget.Toolbar;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewGroup.LayoutParams;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
@@ -44,10 +42,10 @@ import javax.inject.Inject;
|
||||
import static android.arch.lifecycle.Lifecycle.State.STARTED;
|
||||
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
|
||||
import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
|
||||
import static android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.briar.android.TestingConstants.PREVENT_SCREENSHOTS;
|
||||
import static org.briarproject.briar.android.util.UiUtils.hideSoftKeyboard;
|
||||
|
||||
/**
|
||||
* Warning: Some activities don't extend {@link BaseActivity}.
|
||||
@@ -217,17 +215,6 @@ public abstract class BaseActivity extends AppCompatActivity
|
||||
});
|
||||
}
|
||||
|
||||
public void showSoftKeyboard(View view) {
|
||||
Object o = getSystemService(INPUT_METHOD_SERVICE);
|
||||
((InputMethodManager) o).showSoftInput(view, SHOW_IMPLICIT);
|
||||
}
|
||||
|
||||
public void hideSoftKeyboard(View view) {
|
||||
IBinder token = view.getWindowToken();
|
||||
Object o = getSystemService(INPUT_METHOD_SERVICE);
|
||||
((InputMethodManager) o).hideSoftInputFromWindow(token, 0);
|
||||
}
|
||||
|
||||
@UiThread
|
||||
public void handleDbException(DbException e) {
|
||||
supportFinishAfterTransition();
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory.Options;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.jsoup.UnsupportedMimeTypeException;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static android.graphics.Bitmap.CompressFormat.JPEG;
|
||||
import static android.graphics.BitmapFactory.decodeStream;
|
||||
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.util.IoUtils.tryToClose;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
import static org.briarproject.briar.api.messaging.MessagingConstants.IMAGE_MIME_TYPES;
|
||||
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_IMAGE_SIZE;
|
||||
|
||||
@NotNullByDefault
|
||||
class AttachmentCreationTask {
|
||||
|
||||
private static Logger LOG =
|
||||
getLogger(AttachmentCreationTask.class.getName());
|
||||
|
||||
private static final int MAX_ATTACHMENT_DIMENSION = 1000;
|
||||
|
||||
private final MessagingManager messagingManager;
|
||||
private final ContentResolver contentResolver;
|
||||
private final ImageSizeCalculator imageSizeCalculator;
|
||||
private final GroupId groupId;
|
||||
private final Collection<Uri> uris;
|
||||
private final boolean needsSize;
|
||||
@Nullable
|
||||
private volatile AttachmentCreator attachmentCreator;
|
||||
|
||||
private volatile boolean canceled = false;
|
||||
|
||||
AttachmentCreationTask(MessagingManager messagingManager,
|
||||
ContentResolver contentResolver,
|
||||
AttachmentCreator attachmentCreator,
|
||||
ImageSizeCalculator imageSizeCalculator,
|
||||
GroupId groupId, Collection<Uri> uris, boolean needsSize) {
|
||||
this.messagingManager = messagingManager;
|
||||
this.contentResolver = contentResolver;
|
||||
this.imageSizeCalculator = imageSizeCalculator;
|
||||
this.groupId = groupId;
|
||||
this.uris = uris;
|
||||
this.needsSize = needsSize;
|
||||
this.attachmentCreator = attachmentCreator;
|
||||
}
|
||||
|
||||
void cancel() {
|
||||
canceled = true;
|
||||
attachmentCreator = null;
|
||||
}
|
||||
|
||||
@IoExecutor
|
||||
void storeAttachments() {
|
||||
for (Uri uri : uris) processUri(uri);
|
||||
AttachmentCreator attachmentCreator = this.attachmentCreator;
|
||||
if (!canceled && attachmentCreator != null)
|
||||
attachmentCreator.onAttachmentCreationFinished();
|
||||
this.attachmentCreator = null;
|
||||
}
|
||||
|
||||
@IoExecutor
|
||||
private void processUri(Uri uri) {
|
||||
if (canceled) return;
|
||||
try {
|
||||
AttachmentHeader h = storeAttachment(uri);
|
||||
AttachmentCreator attachmentCreator = this.attachmentCreator;
|
||||
if (attachmentCreator != null) {
|
||||
attachmentCreator.onAttachmentHeaderReceived(uri, h, needsSize);
|
||||
}
|
||||
} catch (DbException | IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
AttachmentCreator attachmentCreator = this.attachmentCreator;
|
||||
if (attachmentCreator != null) {
|
||||
attachmentCreator.onAttachmentError(uri, e);
|
||||
}
|
||||
canceled = true;
|
||||
}
|
||||
}
|
||||
|
||||
@IoExecutor
|
||||
private AttachmentHeader storeAttachment(Uri uri)
|
||||
throws IOException, DbException {
|
||||
long start = now();
|
||||
String contentType = contentResolver.getType(uri);
|
||||
if (contentType == null) throw new IOException("null content type");
|
||||
if (!isValidMimeType(contentType)) {
|
||||
String uriString = uri.toString();
|
||||
throw new UnsupportedMimeTypeException("", contentType, uriString);
|
||||
}
|
||||
InputStream is = contentResolver.openInputStream(uri);
|
||||
if (is == null) throw new IOException();
|
||||
is = compressImage(is, contentType);
|
||||
contentType = "image/jpeg";
|
||||
long timestamp = System.currentTimeMillis();
|
||||
AttachmentHeader h = messagingManager
|
||||
.addLocalAttachment(groupId, timestamp, contentType, is);
|
||||
tryToClose(is, LOG, WARNING);
|
||||
logDuration(LOG, "Storing attachment", start);
|
||||
return h;
|
||||
}
|
||||
|
||||
private boolean isValidMimeType(String mimeType) {
|
||||
for (String supportedType : IMAGE_MIME_TYPES) {
|
||||
if (supportedType.equals(mimeType)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private InputStream compressImage(InputStream is, String contentType)
|
||||
throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try {
|
||||
Bitmap bitmap = createBitmap(is, contentType);
|
||||
for (int quality = 100; quality >= 0; quality -= 10) {
|
||||
if (!bitmap.compress(JPEG, quality, out))
|
||||
throw new IOException();
|
||||
if (out.size() <= MAX_IMAGE_SIZE) {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Compressed image to "
|
||||
+ out.size() + " bytes, quality " + quality);
|
||||
}
|
||||
return new ByteArrayInputStream(out.toByteArray());
|
||||
}
|
||||
out.reset();
|
||||
}
|
||||
throw new IOException();
|
||||
} finally {
|
||||
tryToClose(is, LOG, WARNING);
|
||||
}
|
||||
}
|
||||
|
||||
private Bitmap createBitmap(InputStream is, String contentType)
|
||||
throws IOException {
|
||||
is = new BufferedInputStream(is);
|
||||
Size size = imageSizeCalculator.getSize(is, contentType);
|
||||
if (size.error) throw new IOException();
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Original image size: " + size.width + "x" + size.height);
|
||||
int dimension = Math.max(size.width, size.height);
|
||||
int inSampleSize = 1;
|
||||
while (dimension > MAX_ATTACHMENT_DIMENSION) {
|
||||
inSampleSize *= 2;
|
||||
dimension /= 2;
|
||||
}
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Scaling attachment by factor of " + inSampleSize);
|
||||
Options options = new Options();
|
||||
options.inSampleSize = inSampleSize;
|
||||
Bitmap bitmap = decodeStream(is, null, options);
|
||||
if (bitmap == null) throw new IOException();
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.UiThread;
|
||||
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface AttachmentCreator {
|
||||
|
||||
@UiThread
|
||||
LiveData<AttachmentResult> storeAttachments(LiveData<GroupId> groupId,
|
||||
Collection<Uri> newUris);
|
||||
|
||||
/**
|
||||
* This should be only called after configuration changes.
|
||||
* In this case we should not create new attachments.
|
||||
* They are already being created and returned by the existing LiveData.
|
||||
*/
|
||||
@UiThread
|
||||
LiveData<AttachmentResult> getLiveAttachments();
|
||||
|
||||
@UiThread
|
||||
List<AttachmentHeader> getAttachmentHeadersForSending();
|
||||
|
||||
/**
|
||||
* Marks the attachments as sent and adds the items to the cache for display
|
||||
*
|
||||
* @param id The MessageId of the sent message.
|
||||
*/
|
||||
@UiThread
|
||||
void onAttachmentsSent(MessageId id);
|
||||
|
||||
/**
|
||||
* Needs to be called when created attachments will not be sent anymore.
|
||||
*/
|
||||
@UiThread
|
||||
void cancel();
|
||||
|
||||
@UiThread
|
||||
void deleteUnsentAttachments();
|
||||
|
||||
@IoExecutor
|
||||
void onAttachmentHeaderReceived(Uri uri, AttachmentHeader h,
|
||||
boolean needsSize);
|
||||
|
||||
@IoExecutor
|
||||
void onAttachmentError(Uri uri, Throwable t);
|
||||
|
||||
@IoExecutor
|
||||
void onAttachmentCreationFinished();
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
|
||||
import android.app.Application;
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.arch.lifecycle.MutableLiveData;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.UiThread;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.api.messaging.Attachment;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.FileTooBigException;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.jsoup.UnsupportedMimeTypeException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
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.briar.android.util.UiUtils.observeForeverOnce;
|
||||
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_IMAGE_SIZE;
|
||||
|
||||
@NotNullByDefault
|
||||
class AttachmentCreatorImpl implements AttachmentCreator {
|
||||
|
||||
private static Logger LOG =
|
||||
getLogger(AttachmentCreatorImpl.class.getName());
|
||||
|
||||
private final Application app;
|
||||
@IoExecutor
|
||||
private final Executor ioExecutor;
|
||||
private final MessagingManager messagingManager;
|
||||
private final AttachmentRetriever retriever;
|
||||
private final ImageSizeCalculator imageSizeCalculator;
|
||||
|
||||
private final CopyOnWriteArrayList<Uri> uris = new CopyOnWriteArrayList<>();
|
||||
private final CopyOnWriteArrayList<AttachmentItemResult> itemResults =
|
||||
new CopyOnWriteArrayList<>();
|
||||
|
||||
@Nullable
|
||||
private AttachmentCreationTask task;
|
||||
|
||||
@Nullable
|
||||
private volatile MutableLiveData<AttachmentResult> result;
|
||||
|
||||
@Inject
|
||||
AttachmentCreatorImpl(Application app, @IoExecutor Executor ioExecutor,
|
||||
MessagingManager messagingManager, AttachmentRetriever retriever,
|
||||
ImageSizeCalculator imageSizeCalculator) {
|
||||
this.app = app;
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.messagingManager = messagingManager;
|
||||
this.retriever = retriever;
|
||||
this.imageSizeCalculator = imageSizeCalculator;
|
||||
}
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public LiveData<AttachmentResult> storeAttachments(
|
||||
LiveData<GroupId> groupId, Collection<Uri> newUris) {
|
||||
if (task != null || result != null || !uris.isEmpty())
|
||||
throw new IllegalStateException();
|
||||
MutableLiveData<AttachmentResult> result = new MutableLiveData<>();
|
||||
this.result = result;
|
||||
uris.addAll(newUris);
|
||||
observeForeverOnce(groupId, id -> {
|
||||
if (id == null) throw new IllegalStateException();
|
||||
boolean needsSize = uris.size() == 1;
|
||||
task = new AttachmentCreationTask(messagingManager,
|
||||
app.getContentResolver(), this, imageSizeCalculator, id,
|
||||
uris, needsSize);
|
||||
ioExecutor.execute(() -> task.storeAttachments());
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public LiveData<AttachmentResult> getLiveAttachments() {
|
||||
MutableLiveData<AttachmentResult> result = this.result;
|
||||
if (task == null || result == null || uris.isEmpty())
|
||||
throw new IllegalStateException();
|
||||
// A task is already running. It will update the result LiveData.
|
||||
// So nothing more to do here.
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@IoExecutor
|
||||
public void onAttachmentHeaderReceived(Uri uri, AttachmentHeader h,
|
||||
boolean needsSize) {
|
||||
// get and cache AttachmentItem for ImagePreview
|
||||
try {
|
||||
Attachment a = retriever.getMessageAttachment(h);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, needsSize);
|
||||
if (item.hasError()) throw new IOException();
|
||||
AttachmentItemResult itemResult =
|
||||
new AttachmentItemResult(uri, item);
|
||||
itemResults.add(itemResult);
|
||||
MutableLiveData<AttachmentResult> result = this.result;
|
||||
if (result != null) result.postValue(getResult(false));
|
||||
} catch (IOException | DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
onAttachmentError(uri, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@IoExecutor
|
||||
public void onAttachmentError(Uri uri, Throwable t) {
|
||||
// get error message
|
||||
String errorMsg;
|
||||
if (t instanceof UnsupportedMimeTypeException) {
|
||||
String mimeType = ((UnsupportedMimeTypeException) t).getMimeType();
|
||||
errorMsg = app.getString(
|
||||
R.string.image_attach_error_invalid_mime_type, mimeType);
|
||||
} else if (t instanceof FileTooBigException) {
|
||||
int mb = MAX_IMAGE_SIZE / 1024 / 1024;
|
||||
errorMsg = app.getString(R.string.image_attach_error_too_big, mb);
|
||||
} else {
|
||||
errorMsg = null; // generic error
|
||||
}
|
||||
AttachmentItemResult itemResult =
|
||||
new AttachmentItemResult(uri, errorMsg);
|
||||
itemResults.add(itemResult);
|
||||
MutableLiveData<AttachmentResult> result = this.result;
|
||||
if (result != null) result.postValue(getResult(false));
|
||||
// expect to receive a cancel from the UI
|
||||
}
|
||||
|
||||
@Override
|
||||
@IoExecutor
|
||||
public void onAttachmentCreationFinished() {
|
||||
MutableLiveData<AttachmentResult> result = this.result;
|
||||
if (result != null) result.postValue(getResult(true));
|
||||
}
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public List<AttachmentHeader> getAttachmentHeadersForSending() {
|
||||
List<AttachmentHeader> headers = new ArrayList<>(itemResults.size());
|
||||
for (AttachmentItemResult itemResult : itemResults) {
|
||||
// check if we are trying to send attachment items with errors
|
||||
if (itemResult.getItem() == null) throw new IllegalStateException();
|
||||
headers.add(itemResult.getItem().getHeader());
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public void onAttachmentsSent(MessageId id) {
|
||||
List<AttachmentItem> items = new ArrayList<>(itemResults.size());
|
||||
for (AttachmentItemResult itemResult : itemResults) {
|
||||
// check if we are trying to send attachment items with errors
|
||||
if (itemResult.getItem() == null) throw new IllegalStateException();
|
||||
items.add(itemResult.getItem());
|
||||
}
|
||||
retriever.cachePut(id, items);
|
||||
resetState();
|
||||
}
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public void cancel() {
|
||||
if (task == null) throw new AssertionError();
|
||||
task.cancel();
|
||||
deleteUnsentAttachments();
|
||||
resetState();
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void resetState() {
|
||||
task = null;
|
||||
uris.clear();
|
||||
itemResults.clear();
|
||||
MutableLiveData<AttachmentResult> result = this.result;
|
||||
if (result != null) {
|
||||
result.setValue(null);
|
||||
this.result = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public void deleteUnsentAttachments() {
|
||||
// Make a copy for the IoExecutor as we clear the itemResults soon
|
||||
List<AttachmentHeader> headers = new ArrayList<>(itemResults.size());
|
||||
for (AttachmentItemResult itemResult : itemResults) {
|
||||
// check if we are trying to send attachment items with errors
|
||||
if (itemResult.getItem() != null)
|
||||
headers.add(itemResult.getItem().getHeader());
|
||||
}
|
||||
ioExecutor.execute(() -> {
|
||||
for (AttachmentHeader header : headers) {
|
||||
try {
|
||||
messagingManager.removeAttachment(header);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private AttachmentResult getResult(boolean finished) {
|
||||
// Make a copy of the list,
|
||||
// because our copy will continue to change in the background.
|
||||
// (As it's a CopyOnWriteArrayList,
|
||||
// the code that receives the result can safely do simple things
|
||||
// like iterating over the list,
|
||||
// but anything that involves calling more than one list method
|
||||
// is still unsafe.)
|
||||
Collection<AttachmentItemResult> items = new ArrayList<>(itemResults);
|
||||
return new AttachmentResult(items, finished);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,15 @@
|
||||
package org.briarproject.briar.android.conversation;
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
class AttachmentDimensions {
|
||||
|
||||
final int defaultSize;
|
||||
@@ -33,7 +38,7 @@ class AttachmentDimensions {
|
||||
int maxHeight = res.getDimensionPixelSize(
|
||||
R.dimen.message_bubble_image_max_height);
|
||||
return new AttachmentDimensions(defaultSize, minWidth, maxWidth,
|
||||
minHeight, minHeight);
|
||||
minHeight, maxHeight);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.briarproject.briar.android.conversation;
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
@@ -6,18 +6,21 @@ import android.support.annotation.Nullable;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class AttachmentItem implements Parcelable {
|
||||
|
||||
private final MessageId messageId;
|
||||
private final AttachmentHeader header;
|
||||
private final int width, height;
|
||||
private final String mimeType, extension;
|
||||
private final String extension;
|
||||
private final int thumbnailWidth, thumbnailHeight;
|
||||
private final boolean hasError;
|
||||
private final long instanceId;
|
||||
@@ -37,13 +40,12 @@ public class AttachmentItem implements Parcelable {
|
||||
|
||||
private static final AtomicLong NEXT_INSTANCE_ID = new AtomicLong(0);
|
||||
|
||||
AttachmentItem(MessageId messageId, int width, int height, String mimeType,
|
||||
AttachmentItem(AttachmentHeader header, int width, int height,
|
||||
String extension, int thumbnailWidth, int thumbnailHeight,
|
||||
boolean hasError) {
|
||||
this.messageId = messageId;
|
||||
this.header = header;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.mimeType = mimeType;
|
||||
this.extension = extension;
|
||||
this.thumbnailWidth = thumbnailWidth;
|
||||
this.thumbnailHeight = thumbnailHeight;
|
||||
@@ -54,19 +56,24 @@ public class AttachmentItem implements Parcelable {
|
||||
protected AttachmentItem(Parcel in) {
|
||||
byte[] messageIdByte = new byte[MessageId.LENGTH];
|
||||
in.readByteArray(messageIdByte);
|
||||
messageId = new MessageId(messageIdByte);
|
||||
MessageId messageId = new MessageId(messageIdByte);
|
||||
width = in.readInt();
|
||||
height = in.readInt();
|
||||
mimeType = in.readString();
|
||||
extension = in.readString();
|
||||
String mimeType = requireNonNull(in.readString());
|
||||
extension = requireNonNull(in.readString());
|
||||
thumbnailWidth = in.readInt();
|
||||
thumbnailHeight = in.readInt();
|
||||
hasError = in.readByte() != 0;
|
||||
instanceId = in.readLong();
|
||||
header = new AttachmentHeader(messageId, mimeType);
|
||||
}
|
||||
|
||||
public AttachmentHeader getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public MessageId getMessageId() {
|
||||
return messageId;
|
||||
return header.getMessageId();
|
||||
}
|
||||
|
||||
int getWidth() {
|
||||
@@ -77,27 +84,27 @@ public class AttachmentItem implements Parcelable {
|
||||
return height;
|
||||
}
|
||||
|
||||
String getMimeType() {
|
||||
return mimeType;
|
||||
public String getMimeType() {
|
||||
return header.getContentType();
|
||||
}
|
||||
|
||||
String getExtension() {
|
||||
public String getExtension() {
|
||||
return extension;
|
||||
}
|
||||
|
||||
int getThumbnailWidth() {
|
||||
public int getThumbnailWidth() {
|
||||
return thumbnailWidth;
|
||||
}
|
||||
|
||||
int getThumbnailHeight() {
|
||||
public int getThumbnailHeight() {
|
||||
return thumbnailHeight;
|
||||
}
|
||||
|
||||
boolean hasError() {
|
||||
public boolean hasError() {
|
||||
return hasError;
|
||||
}
|
||||
|
||||
String getTransitionName() {
|
||||
public String getTransitionName() {
|
||||
return String.valueOf(instanceId);
|
||||
}
|
||||
|
||||
@@ -108,10 +115,10 @@ public class AttachmentItem implements Parcelable {
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeByteArray(messageId.getBytes());
|
||||
dest.writeByteArray(header.getMessageId().getBytes());
|
||||
dest.writeInt(width);
|
||||
dest.writeInt(height);
|
||||
dest.writeString(mimeType);
|
||||
dest.writeString(header.getContentType());
|
||||
dest.writeString(extension);
|
||||
dest.writeInt(thumbnailWidth);
|
||||
dest.writeInt(thumbnailHeight);
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.net.Uri;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class AttachmentItemResult {
|
||||
|
||||
private final Uri uri;
|
||||
@Nullable
|
||||
private final AttachmentItem item;
|
||||
@Nullable
|
||||
private final String errorMsg;
|
||||
|
||||
AttachmentItemResult(Uri uri, AttachmentItem item) {
|
||||
this.uri = uri;
|
||||
this.item = item;
|
||||
this.errorMsg = null;
|
||||
}
|
||||
|
||||
AttachmentItemResult(Uri uri, @Nullable String errorMsg) {
|
||||
this.uri = uri;
|
||||
this.item = null;
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
|
||||
public Uri getUri() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public AttachmentItem getItem() {
|
||||
return item;
|
||||
}
|
||||
|
||||
public boolean hasError() {
|
||||
return item == null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getErrorMsg() {
|
||||
return errorMsg;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.UiThread;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@UiThread
|
||||
@NotNullByDefault
|
||||
public interface AttachmentManager {
|
||||
|
||||
LiveData<AttachmentResult> storeAttachments(Collection<Uri> uri,
|
||||
boolean restart);
|
||||
|
||||
List<AttachmentHeader> getAttachmentHeadersForSending();
|
||||
|
||||
void cancel();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
import static org.briarproject.briar.android.attachment.AttachmentDimensions.getAttachmentDimensions;
|
||||
|
||||
@Module
|
||||
public class AttachmentModule {
|
||||
|
||||
@Provides
|
||||
ImageHelper provideImageHelper(ImageHelperImpl imageHelper) {
|
||||
return imageHelper;
|
||||
}
|
||||
|
||||
@Provides
|
||||
ImageSizeCalculator provideImageSizeCalculator(ImageHelper imageHelper) {
|
||||
return new ImageSizeCalculator(imageHelper);
|
||||
}
|
||||
|
||||
@Provides
|
||||
AttachmentDimensions provideAttachmentDimensions(Application app) {
|
||||
return getAttachmentDimensions(app.getResources());
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
AttachmentRetriever provideAttachmentRetriever(
|
||||
AttachmentRetrieverImpl attachmentRetriever) {
|
||||
return attachmentRetriever;
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
AttachmentCreator provideAttachmentCreator(
|
||||
AttachmentCreatorImpl attachmentCreator) {
|
||||
return attachmentCreator;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class AttachmentResult {
|
||||
|
||||
private final Collection<AttachmentItemResult> itemResults;
|
||||
private final boolean finished;
|
||||
|
||||
public AttachmentResult(Collection<AttachmentItemResult> itemResults,
|
||||
boolean finished) {
|
||||
this.itemResults = itemResults;
|
||||
this.finished = finished;
|
||||
}
|
||||
|
||||
public Collection<AttachmentItemResult> getItemResults() {
|
||||
return itemResults;
|
||||
}
|
||||
|
||||
public boolean isFinished() {
|
||||
return finished;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.api.messaging.Attachment;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface AttachmentRetriever {
|
||||
|
||||
void cachePut(MessageId messageId, List<AttachmentItem> attachments);
|
||||
|
||||
@Nullable
|
||||
List<AttachmentItem> cacheGet(MessageId messageId);
|
||||
|
||||
Attachment getMessageAttachment(AttachmentHeader h) throws DbException;
|
||||
|
||||
/**
|
||||
* Creates an {@link AttachmentItem} from the {@link Attachment}'s
|
||||
* {@link InputStream} which will be closed when this method returns.
|
||||
*/
|
||||
AttachmentItem getAttachmentItem(Attachment a, boolean needsSize);
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.api.messaging.Attachment;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
|
||||
@NotNullByDefault
|
||||
class AttachmentRetrieverImpl implements AttachmentRetriever {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(AttachmentRetrieverImpl.class.getName());
|
||||
|
||||
private final MessagingManager messagingManager;
|
||||
private final ImageHelper imageHelper;
|
||||
private final ImageSizeCalculator imageSizeCalculator;
|
||||
private final int defaultSize;
|
||||
private final int minWidth, maxWidth;
|
||||
private final int minHeight, maxHeight;
|
||||
|
||||
private final Map<MessageId, List<AttachmentItem>> attachmentCache =
|
||||
new ConcurrentHashMap<>();
|
||||
|
||||
@Inject
|
||||
AttachmentRetrieverImpl(MessagingManager messagingManager,
|
||||
AttachmentDimensions dimensions, ImageHelper imageHelper,
|
||||
ImageSizeCalculator imageSizeCalculator) {
|
||||
this.messagingManager = messagingManager;
|
||||
this.imageHelper = imageHelper;
|
||||
this.imageSizeCalculator = imageSizeCalculator;
|
||||
defaultSize = dimensions.defaultSize;
|
||||
minWidth = dimensions.minWidth;
|
||||
maxWidth = dimensions.maxWidth;
|
||||
minHeight = dimensions.minHeight;
|
||||
maxHeight = dimensions.maxHeight;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cachePut(MessageId messageId,
|
||||
List<AttachmentItem> attachments) {
|
||||
attachmentCache.put(messageId, attachments);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public List<AttachmentItem> cacheGet(MessageId messageId) {
|
||||
return attachmentCache.get(messageId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Attachment getMessageAttachment(AttachmentHeader h)
|
||||
throws DbException {
|
||||
return messagingManager.getAttachment(h);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AttachmentItem getAttachmentItem(Attachment a, boolean needsSize) {
|
||||
AttachmentHeader h = a.getHeader();
|
||||
if (!needsSize) {
|
||||
String extension =
|
||||
imageHelper.getExtensionFromMimeType(h.getContentType());
|
||||
boolean hasError = false;
|
||||
if (extension == null) {
|
||||
extension = "";
|
||||
hasError = true;
|
||||
}
|
||||
return new AttachmentItem(h, 0, 0, extension, 0, 0, hasError);
|
||||
}
|
||||
|
||||
InputStream is = new BufferedInputStream(a.getStream());
|
||||
Size size = imageSizeCalculator.getSize(is, h.getContentType());
|
||||
|
||||
// calculate thumbnail size
|
||||
Size thumbnailSize = new Size(defaultSize, defaultSize, size.mimeType);
|
||||
if (!size.error) {
|
||||
thumbnailSize =
|
||||
getThumbnailSize(size.width, size.height, size.mimeType);
|
||||
}
|
||||
// get file extension
|
||||
String extension = imageHelper.getExtensionFromMimeType(size.mimeType);
|
||||
boolean hasError = extension == null || size.error;
|
||||
if (!h.getContentType().equals(size.mimeType)) {
|
||||
if (LOG.isLoggable(WARNING)) {
|
||||
LOG.warning("Header has different mime type (" +
|
||||
h.getContentType() + ") than image (" + size.mimeType +
|
||||
").");
|
||||
}
|
||||
hasError = true;
|
||||
}
|
||||
if (extension == null) extension = "";
|
||||
return new AttachmentItem(h, size.width, size.height, extension,
|
||||
thumbnailSize.width, thumbnailSize.height, hasError);
|
||||
}
|
||||
|
||||
private Size getThumbnailSize(int width, int height, String mimeType) {
|
||||
float widthPercentage = maxWidth / (float) width;
|
||||
float heightPercentage = maxHeight / (float) height;
|
||||
float scaleFactor = Math.min(widthPercentage, heightPercentage);
|
||||
if (scaleFactor > 1) scaleFactor = 1f;
|
||||
int thumbnailWidth = (int) (width * scaleFactor);
|
||||
int thumbnailHeight = (int) (height * scaleFactor);
|
||||
if (thumbnailWidth < minWidth || thumbnailHeight < minHeight) {
|
||||
widthPercentage = minWidth / (float) width;
|
||||
heightPercentage = minHeight / (float) height;
|
||||
scaleFactor = Math.max(widthPercentage, heightPercentage);
|
||||
thumbnailWidth = (int) (width * scaleFactor);
|
||||
thumbnailHeight = (int) (height * scaleFactor);
|
||||
if (thumbnailWidth > maxWidth) thumbnailWidth = maxWidth;
|
||||
if (thumbnailHeight > maxHeight) thumbnailHeight = maxHeight;
|
||||
}
|
||||
return new Size(thumbnailWidth, thumbnailHeight, mimeType);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.briarproject.briar.android.conversation;
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import java.io.InputStream;
|
||||
|
||||
@NotNullByDefault
|
||||
interface ImageHelper {
|
||||
public interface ImageHelper {
|
||||
|
||||
DecodeResult decodeStream(InputStream is);
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
class ImageHelperImpl implements ImageHelper {
|
||||
|
||||
@Inject
|
||||
ImageHelperImpl() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DecodeResult decodeStream(InputStream is) {
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeStream(is, null, options);
|
||||
String mimeType = options.outMimeType;
|
||||
if (mimeType == null) mimeType = "";
|
||||
return new DecodeResult(options.outWidth, options.outHeight,
|
||||
mimeType);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getExtensionFromMimeType(String mimeType) {
|
||||
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
||||
return mimeTypeMap.getExtensionFromMimeType(mimeType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.support.media.ExifInterface;
|
||||
|
||||
import com.bumptech.glide.util.MarkEnforcingInputStream;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.android.attachment.ImageHelper.DecodeResult;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static android.support.media.ExifInterface.ORIENTATION_ROTATE_270;
|
||||
import static android.support.media.ExifInterface.ORIENTATION_ROTATE_90;
|
||||
import static android.support.media.ExifInterface.ORIENTATION_TRANSPOSE;
|
||||
import static android.support.media.ExifInterface.ORIENTATION_TRANSVERSE;
|
||||
import static android.support.media.ExifInterface.TAG_IMAGE_LENGTH;
|
||||
import static android.support.media.ExifInterface.TAG_IMAGE_WIDTH;
|
||||
import static android.support.media.ExifInterface.TAG_ORIENTATION;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@NotNullByDefault
|
||||
class ImageSizeCalculator {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(ImageSizeCalculator.class.getName());
|
||||
|
||||
private static final int READ_LIMIT = 1024 * 8192;
|
||||
|
||||
private final ImageHelper imageHelper;
|
||||
|
||||
ImageSizeCalculator(ImageHelper imageHelper) {
|
||||
this.imageHelper = imageHelper;
|
||||
}
|
||||
|
||||
Size getSize(InputStream is, String contentType) {
|
||||
Size size = new Size();
|
||||
is = new MarkEnforcingInputStream(is);
|
||||
is.mark(READ_LIMIT);
|
||||
if (contentType.equals("image/jpeg")) {
|
||||
try {
|
||||
// use exif to get size
|
||||
size = getSizeFromExif(is);
|
||||
is.reset();
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
if (size.error) {
|
||||
// need to mark again to re-add read limit
|
||||
is.mark(READ_LIMIT);
|
||||
try {
|
||||
// use BitmapFactory to get size
|
||||
size = getSizeFromBitmap(is);
|
||||
is.reset();
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the size of a JPEG {@link InputStream} if EXIF info is available.
|
||||
*/
|
||||
private Size getSizeFromExif(InputStream is) throws IOException {
|
||||
ExifInterface exif = new ExifInterface(is);
|
||||
// these can return 0 independent of default value
|
||||
int width = exif.getAttributeInt(TAG_IMAGE_WIDTH, 0);
|
||||
int height = exif.getAttributeInt(TAG_IMAGE_LENGTH, 0);
|
||||
if (width == 0 || height == 0) return new Size();
|
||||
int orientation = exif.getAttributeInt(TAG_ORIENTATION, 0);
|
||||
if (orientation == ORIENTATION_ROTATE_90 ||
|
||||
orientation == ORIENTATION_ROTATE_270 ||
|
||||
orientation == ORIENTATION_TRANSVERSE ||
|
||||
orientation == ORIENTATION_TRANSPOSE) {
|
||||
//noinspection SuspiciousNameCombination
|
||||
return new Size(height, width, "image/jpeg");
|
||||
}
|
||||
return new Size(width, height, "image/jpeg");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the size of any image {@link InputStream}.
|
||||
*/
|
||||
private Size getSizeFromBitmap(InputStream is) {
|
||||
DecodeResult result = imageHelper.decodeStream(is);
|
||||
if (result.width < 1 || result.height < 1) return new Size();
|
||||
return new Size(result.width, result.height, result.mimeType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
class Size {
|
||||
|
||||
final int width;
|
||||
final int height;
|
||||
final String mimeType;
|
||||
final boolean error;
|
||||
|
||||
Size(int width, int height, String mimeType) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.mimeType = mimeType;
|
||||
this.error = false;
|
||||
}
|
||||
|
||||
Size() {
|
||||
this.width = 0;
|
||||
this.height = 0;
|
||||
this.mimeType = "";
|
||||
this.error = true;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
@@ -21,6 +20,7 @@ import org.briarproject.briar.android.fragment.BaseFragment;
|
||||
import org.briarproject.briar.android.view.TextInputView;
|
||||
import org.briarproject.briar.android.view.TextSendController;
|
||||
import org.briarproject.briar.android.view.TextSendController.SendListener;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -121,7 +121,8 @@ public class ReblogFragment extends BaseFragment implements SendListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSendClick(@Nullable String text, List<Uri> imageUris) {
|
||||
public void onSendClick(@Nullable String text,
|
||||
List<AttachmentHeader> headers) {
|
||||
ui.input.hideSoftKeyboard();
|
||||
feedController.repeatPost(item, text,
|
||||
new UiExceptionHandler<DbException>(this) {
|
||||
|
||||
@@ -32,6 +32,7 @@ import static android.view.View.VISIBLE;
|
||||
import static android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.briar.android.util.UiUtils.hideSoftKeyboard;
|
||||
|
||||
public class RssFeedImportActivity extends BriarActivity {
|
||||
|
||||
@@ -77,7 +78,6 @@ public class RssFeedImportActivity extends BriarActivity {
|
||||
if (actionId == IME_ACTION_DONE && importButton.isEnabled() &&
|
||||
importButton.getVisibility() == VISIBLE) {
|
||||
publish();
|
||||
hideSoftKeyboard(urlInput);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -123,6 +123,7 @@ public class RssFeedImportActivity extends BriarActivity {
|
||||
// hide import button, show progress bar
|
||||
importButton.setVisibility(GONE);
|
||||
progressBar.setVisibility(VISIBLE);
|
||||
hideSoftKeyboard(urlInput);
|
||||
|
||||
String url = validateAndNormaliseUrl(urlInput.getText().toString());
|
||||
if (url == null) throw new AssertionError();
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import android.widget.TextView.OnEditorActionListener;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
@@ -27,6 +23,7 @@ import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||
import org.briarproject.briar.api.blog.BlogManager;
|
||||
import org.briarproject.briar.api.blog.BlogPost;
|
||||
import org.briarproject.briar.api.blog.BlogPostFactory;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.List;
|
||||
@@ -44,7 +41,7 @@ import static org.briarproject.briar.api.blog.BlogConstants.MAX_BLOG_POST_TEXT_L
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class WriteBlogPostActivity extends BriarActivity
|
||||
implements OnEditorActionListener, SendListener {
|
||||
implements SendListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(WriteBlogPostActivity.class.getName());
|
||||
@@ -114,13 +111,8 @@ public class WriteBlogPostActivity extends BriarActivity
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) {
|
||||
input.requestFocus();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSendClick(@Nullable String text, List<Uri> imageUris) {
|
||||
public void onSendClick(@Nullable String text,
|
||||
List<AttachmentHeader> headers) {
|
||||
if (isNullOrEmpty(text)) throw new AssertionError();
|
||||
|
||||
// hide publish button, show progress bar
|
||||
|
||||
@@ -56,7 +56,6 @@ import javax.inject.Inject;
|
||||
|
||||
import io.github.kobakei.materialfabspeeddial.FabSpeedDial;
|
||||
import io.github.kobakei.materialfabspeeddial.FabSpeedDial.OnMenuItemClickListener;
|
||||
import io.github.kobakei.materialfabspeeddial.FabSpeedDialMenu;
|
||||
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static android.support.design.widget.Snackbar.LENGTH_INDEFINITE;
|
||||
@@ -67,7 +66,6 @@ import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
import static org.briarproject.briar.android.TestingConstants.FEATURE_FLAG_REMOTE_CONTACTS;
|
||||
import static org.briarproject.briar.android.conversation.ConversationActivity.CONTACT_ID;
|
||||
import static org.briarproject.briar.android.util.UiUtils.isSamsung7;
|
||||
|
||||
@@ -124,19 +122,7 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
||||
container, false);
|
||||
|
||||
FabSpeedDial speedDial = contentView.findViewById(R.id.speedDial);
|
||||
if (FEATURE_FLAG_REMOTE_CONTACTS) {
|
||||
speedDial.addOnMenuItemClickListener(this);
|
||||
} else {
|
||||
speedDial.setMenu(new FabSpeedDialMenu(contentView.getContext()));
|
||||
speedDial.addOnStateChangeListener(open -> {
|
||||
if (open) {
|
||||
Intent intent = new Intent(getContext(),
|
||||
ContactExchangeActivity.class);
|
||||
startActivity(intent);
|
||||
speedDial.closeMenu();
|
||||
}
|
||||
});
|
||||
}
|
||||
speedDial.addOnMenuItemClickListener(this);
|
||||
|
||||
OnContactClickListener<ContactListItem> onContactClickListener =
|
||||
(view, item) -> {
|
||||
@@ -167,9 +153,10 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
||||
startActivity(i);
|
||||
}
|
||||
};
|
||||
adapter = new ContactListAdapter(getContext(), onContactClickListener);
|
||||
adapter = new ContactListAdapter(requireContext(),
|
||||
onContactClickListener);
|
||||
list = contentView.findViewById(R.id.list);
|
||||
list.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
list.setLayoutManager(new LinearLayoutManager(requireContext()));
|
||||
list.setAdapter(adapter);
|
||||
list.setEmptyImage(R.drawable.ic_empty_state_contact_list);
|
||||
list.setEmptyText(getString(R.string.no_contacts));
|
||||
@@ -265,7 +252,7 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
||||
if (revision == adapter.getRevision()) {
|
||||
adapter.incrementRevision();
|
||||
if (contacts.isEmpty()) list.showData();
|
||||
else adapter.addAll(contacts);
|
||||
else adapter.replaceAll(contacts);
|
||||
} else {
|
||||
LOG.info("Concurrent update, reloading");
|
||||
loadContacts();
|
||||
|
||||
@@ -9,8 +9,10 @@ import android.support.annotation.Nullable;
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.UnsupportedVersionException;
|
||||
import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.contact.PendingContact;
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.NoSuchPendingContactException;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
||||
import org.briarproject.briar.android.viewmodel.LiveResult;
|
||||
@@ -118,4 +120,19 @@ public class AddContactViewModel extends AndroidViewModel {
|
||||
return addContactResult;
|
||||
}
|
||||
|
||||
public void updatePendingContact(String name, PendingContact p) {
|
||||
dbExecutor.execute(() -> {
|
||||
try {
|
||||
contactManager.removePendingContact(p.getId());
|
||||
addContact(name);
|
||||
} catch(NoSuchPendingContactException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
// no error in UI as pending contact was converted into contact
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
addContactResult.postValue(new LiveResult<>(e));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.briarproject.briar.android.contact.add.remote;
|
||||
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.arch.lifecycle.ViewModelProvider;
|
||||
import android.arch.lifecycle.ViewModelProviders;
|
||||
import android.content.ClipData;
|
||||
@@ -11,7 +12,9 @@ import android.support.v4.app.ShareCompat.IntentBuilder;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
|
||||
import android.widget.Button;
|
||||
import android.widget.ScrollView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
@@ -34,7 +37,8 @@ import static org.briarproject.briar.android.util.UiUtils.observeOnce;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class LinkExchangeFragment extends BaseFragment {
|
||||
public class LinkExchangeFragment extends BaseFragment
|
||||
implements OnGlobalLayoutListener {
|
||||
|
||||
private static final String TAG = LinkExchangeFragment.class.getName();
|
||||
|
||||
@@ -90,9 +94,30 @@ public class LinkExchangeFragment extends BaseFragment {
|
||||
observeOnce(viewModel.getHandshakeLink(), this,
|
||||
this::onHandshakeLinkLoaded);
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
ScrollView scrollView = (ScrollView) v;
|
||||
// we need to wait for views to be laid out to get the heights
|
||||
scrollView.getViewTreeObserver().addOnGlobalLayoutListener(this);
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGlobalLayout() {
|
||||
ScrollView scrollView = (ScrollView) requireNonNull(getView());
|
||||
View layout = scrollView.getChildAt(0);
|
||||
int scrollBy = layout.getHeight() - scrollView.getHeight();
|
||||
if (scrollBy > 0) {
|
||||
// smoothScrollTo() is too fast due to the transition animation
|
||||
ObjectAnimator animator = ObjectAnimator
|
||||
.ofInt(scrollView, "scrollY", scrollBy);
|
||||
animator.setDuration(1000);
|
||||
animator.start();
|
||||
}
|
||||
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
|
||||
}
|
||||
|
||||
private void onHandshakeLinkLoaded(String link) {
|
||||
View v = requireNonNull(getView());
|
||||
|
||||
|
||||
@@ -2,10 +2,15 @@ package org.briarproject.briar.android.contact.add.remote;
|
||||
|
||||
import android.arch.lifecycle.ViewModelProvider;
|
||||
import android.arch.lifecycle.ViewModelProviders;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface.OnClickListener;
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.StringRes;
|
||||
import android.support.design.widget.TextInputEditText;
|
||||
import android.support.design.widget.TextInputLayout;
|
||||
import android.support.v7.app.AlertDialog.Builder;
|
||||
import android.text.Editable;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
@@ -15,6 +20,10 @@ import android.widget.ProgressBar;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.briarproject.bramble.api.UnsupportedVersionException;
|
||||
import org.briarproject.bramble.api.contact.PendingContact;
|
||||
import org.briarproject.bramble.api.db.ContactExistsException;
|
||||
import org.briarproject.bramble.api.db.PendingContactExistsException;
|
||||
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;
|
||||
@@ -24,9 +33,13 @@ import org.briarproject.briar.android.fragment.BaseFragment;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static android.support.v4.content.ContextCompat.getColor;
|
||||
import static android.support.v4.content.ContextCompat.getDrawable;
|
||||
import static android.support.v4.graphics.drawable.DrawableCompat.setTint;
|
||||
import static android.view.View.INVISIBLE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static android.widget.Toast.LENGTH_LONG;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||
import static org.briarproject.bramble.util.StringUtils.utf8IsTooLong;
|
||||
|
||||
@@ -82,19 +95,20 @@ public class NicknameFragment extends BaseFragment {
|
||||
|
||||
@Nullable
|
||||
private String getNicknameOrNull() {
|
||||
Editable name = contactNameInput.getText();
|
||||
if (name == null || name.toString().trim().length() == 0) {
|
||||
Editable text = contactNameInput.getText();
|
||||
if (text == null || text.toString().trim().length() == 0) {
|
||||
contactNameLayout.setError(getString(R.string.nickname_missing));
|
||||
contactNameInput.requestFocus();
|
||||
return null;
|
||||
}
|
||||
if (utf8IsTooLong(name.toString(), MAX_AUTHOR_NAME_LENGTH)) {
|
||||
String name = text.toString().trim();
|
||||
if (utf8IsTooLong(name, MAX_AUTHOR_NAME_LENGTH)) {
|
||||
contactNameLayout.setError(getString(R.string.name_too_long));
|
||||
contactNameInput.requestFocus();
|
||||
return null;
|
||||
}
|
||||
contactNameLayout.setError(null);
|
||||
return name.toString().trim();
|
||||
return name;
|
||||
}
|
||||
|
||||
private void onAddButtonClicked() {
|
||||
@@ -106,23 +120,95 @@ public class NicknameFragment extends BaseFragment {
|
||||
|
||||
viewModel.getAddContactResult().observe(this, result -> {
|
||||
if (result == null) return;
|
||||
if (result.hasError()) {
|
||||
int stringRes;
|
||||
if (result
|
||||
.getException() instanceof UnsupportedVersionException) {
|
||||
stringRes = R.string.unsupported_link;
|
||||
} else {
|
||||
stringRes = R.string.adding_contact_error;
|
||||
}
|
||||
Toast.makeText(getContext(), stringRes, LENGTH_LONG).show();
|
||||
} else {
|
||||
Intent intent = new Intent(getActivity(),
|
||||
PendingContactListActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
finish();
|
||||
if (result.hasError())
|
||||
handleException(name, requireNonNull(result.getException()));
|
||||
else
|
||||
showPendingContactListActivity();
|
||||
});
|
||||
viewModel.addContact(name);
|
||||
}
|
||||
|
||||
private void showPendingContactListActivity() {
|
||||
Intent intent = new Intent(getActivity(),
|
||||
PendingContactListActivity.class);
|
||||
startActivity(intent);
|
||||
finish();
|
||||
}
|
||||
|
||||
private void handleException(String name, Exception e) {
|
||||
if (e instanceof ContactExistsException) {
|
||||
ContactExistsException ce = (ContactExistsException) e;
|
||||
handleExistingContact(name, ce.getRemoteAuthor());
|
||||
} else if (e instanceof PendingContactExistsException) {
|
||||
PendingContactExistsException pe =
|
||||
(PendingContactExistsException) e;
|
||||
handleExistingPendingContact(name, pe.getPendingContact());
|
||||
} else if (e instanceof UnsupportedVersionException) {
|
||||
int stringRes = R.string.unsupported_link;
|
||||
Toast.makeText(getContext(), stringRes, LENGTH_LONG).show();
|
||||
finish();
|
||||
} else {
|
||||
int stringRes = R.string.adding_contact_error;
|
||||
Toast.makeText(getContext(), stringRes, LENGTH_LONG).show();
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
private void handleExistingContact(String name, Author existing) {
|
||||
OnClickListener listener = (d, w) -> {
|
||||
d.dismiss();
|
||||
String str = getString(R.string.contact_already_exists, name);
|
||||
Toast.makeText(getContext(), str, LENGTH_LONG).show();
|
||||
finish();
|
||||
};
|
||||
showSameLinkDialog(existing.getName(), name,
|
||||
R.string.duplicate_link_dialog_text_1_contact, listener);
|
||||
}
|
||||
|
||||
private void handleExistingPendingContact(String name, PendingContact p) {
|
||||
OnClickListener listener = (d, w) -> {
|
||||
viewModel.updatePendingContact(name, p);
|
||||
Toast.makeText(getContext(), R.string.pending_contact_updated_toast,
|
||||
LENGTH_LONG).show();
|
||||
d.dismiss();
|
||||
showPendingContactListActivity();
|
||||
};
|
||||
showSameLinkDialog(p.getAlias(), name,
|
||||
R.string.duplicate_link_dialog_text_1, listener);
|
||||
}
|
||||
|
||||
private void showSameLinkDialog(String name1, String name2,
|
||||
@StringRes int existsRes, OnClickListener samePersonListener) {
|
||||
Context ctx = requireContext();
|
||||
Builder b = new Builder(ctx, R.style.BriarDialogTheme_Neutral);
|
||||
b.setTitle(getString(R.string.duplicate_link_dialog_title));
|
||||
String msg = getString(existsRes, name1) + "\n\n" +
|
||||
getString(R.string.duplicate_link_dialog_text_2, name2, name1);
|
||||
b.setMessage(msg);
|
||||
b.setPositiveButton(R.string.same_person_button, samePersonListener);
|
||||
b.setNegativeButton(R.string.different_person_button, (d, w) -> {
|
||||
d.dismiss();
|
||||
showWarningDialog(name1, name2);
|
||||
});
|
||||
b.setCancelable(false);
|
||||
b.show();
|
||||
}
|
||||
|
||||
private void showWarningDialog(String name1, String name2) {
|
||||
Context ctx = requireContext();
|
||||
Builder b = new Builder(ctx, R.style.BriarDialogTheme);
|
||||
Drawable icon = getDrawable(ctx, R.drawable.alerts_and_states_error);
|
||||
setTint(requireNonNull(icon), getColor(ctx, R.color.color_primary));
|
||||
b.setIcon(icon);
|
||||
b.setTitle(getString(R.string.duplicate_link_dialog_title));
|
||||
b.setMessage(
|
||||
getString(R.string.duplicate_link_dialog_text_3, name1, name2));
|
||||
b.setPositiveButton(R.string.ok, (d, w) -> {
|
||||
d.dismiss();
|
||||
finish();
|
||||
});
|
||||
b.setCancelable(false);
|
||||
b.show();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.arch.lifecycle.ViewModelProvider;
|
||||
import android.arch.lifecycle.ViewModelProviders;
|
||||
import android.content.DialogInterface.OnClickListener;
|
||||
import android.os.Bundle;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
@@ -15,6 +16,7 @@ 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.util.BriarSnackbarBuilder;
|
||||
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -22,6 +24,7 @@ import java.util.Collection;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static android.support.design.widget.Snackbar.LENGTH_INDEFINITE;
|
||||
import static org.briarproject.bramble.api.contact.PendingContactState.FAILED;
|
||||
import static org.briarproject.briar.android.contact.add.remote.PendingContactItem.POLL_DURATION_MS;
|
||||
|
||||
@@ -36,6 +39,7 @@ public class PendingContactListActivity extends BriarActivity
|
||||
private PendingContactListViewModel viewModel;
|
||||
private PendingContactListAdapter adapter;
|
||||
private BriarRecyclerView list;
|
||||
private Snackbar offlineSnackbar;
|
||||
|
||||
@Override
|
||||
public void injectActivity(ActivityComponent component) {
|
||||
@@ -58,6 +62,8 @@ public class PendingContactListActivity extends BriarActivity
|
||||
viewModel.onCreate();
|
||||
viewModel.getPendingContacts()
|
||||
.observe(this, this::onPendingContactsChanged);
|
||||
viewModel.getHasInternetConnection()
|
||||
.observe(this, this::onInternetConnectionChanged);
|
||||
|
||||
adapter = new PendingContactListAdapter(this, this,
|
||||
PendingContactItem.class);
|
||||
@@ -66,6 +72,10 @@ public class PendingContactListActivity extends BriarActivity
|
||||
list.setLayoutManager(new LinearLayoutManager(this));
|
||||
list.setAdapter(adapter);
|
||||
list.showProgressBar();
|
||||
|
||||
offlineSnackbar = new BriarSnackbarBuilder()
|
||||
.setBackgroundColor(R.color.briar_red)
|
||||
.make(list, R.string.offline_state, LENGTH_INDEFINITE);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -130,4 +140,9 @@ public class PendingContactListActivity extends BriarActivity
|
||||
}
|
||||
}
|
||||
|
||||
private void onInternetConnectionChanged(boolean online) {
|
||||
if (online) offlineSnackbar.dismiss();
|
||||
else offlineSnackbar.show();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.contact.PendingContactState.OFFLINE;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@NotNullByDefault
|
||||
@@ -48,6 +49,8 @@ public class PendingContactListViewModel extends AndroidViewModel
|
||||
|
||||
private final MutableLiveData<Collection<PendingContactItem>>
|
||||
pendingContacts = new MutableLiveData<>();
|
||||
private final MutableLiveData<Boolean> hasInternetConnection =
|
||||
new MutableLiveData<>();
|
||||
|
||||
@Inject
|
||||
PendingContactListViewModel(Application application,
|
||||
@@ -88,13 +91,16 @@ public class PendingContactListViewModel extends AndroidViewModel
|
||||
Collection<Pair<PendingContact, PendingContactState>> pairs =
|
||||
contactManager.getPendingContacts();
|
||||
List<PendingContactItem> items = new ArrayList<>(pairs.size());
|
||||
boolean online = items.isEmpty();
|
||||
for (Pair<PendingContact, PendingContactState> pair : pairs) {
|
||||
PendingContact p = pair.getFirst();
|
||||
PendingContactState state = pair.getSecond();
|
||||
long lastPoll = rendezvousPoller.getLastPollTime(p.getId());
|
||||
items.add(new PendingContactItem(p, pair.getSecond(),
|
||||
lastPoll));
|
||||
items.add(new PendingContactItem(p, state, lastPoll));
|
||||
online = online || state != OFFLINE;
|
||||
}
|
||||
pendingContacts.postValue(items);
|
||||
hasInternetConnection.postValue(online);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
@@ -115,4 +121,8 @@ public class PendingContactListViewModel extends AndroidViewModel
|
||||
});
|
||||
}
|
||||
|
||||
LiveData<Boolean> getHasInternetConnection() {
|
||||
return hasInternetConnection;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -52,6 +52,11 @@ class PendingContactViewHolder extends ViewHolder {
|
||||
.getColor(status.getContext(), R.color.briar_yellow);
|
||||
status.setText(R.string.waiting_for_contact_to_come_online);
|
||||
break;
|
||||
case OFFLINE:
|
||||
color = ContextCompat
|
||||
.getColor(status.getContext(), R.color.briar_yellow);
|
||||
status.setText("");
|
||||
break;
|
||||
case CONNECTING:
|
||||
status.setText(R.string.connecting);
|
||||
break;
|
||||
|
||||
@@ -21,9 +21,12 @@ import org.briarproject.briar.android.activity.BaseActivity;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||
import static org.briarproject.bramble.util.StringUtils.toUtf8;
|
||||
import static org.briarproject.briar.android.util.UiUtils.hideSoftKeyboard;
|
||||
import static org.briarproject.briar.android.util.UiUtils.showSoftKeyboard;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
@@ -76,13 +79,14 @@ public class AliasDialogFragment extends AppCompatDialogFragment {
|
||||
setButton.setOnClickListener(v1 -> onSetButtonClicked());
|
||||
|
||||
Button cancelButton = v.findViewById(R.id.cancelButton);
|
||||
cancelButton.setOnClickListener(v1 -> getDialog().cancel());
|
||||
cancelButton.setOnClickListener(v1 -> onCancelButtonClicked());
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
private void onSetButtonClicked() {
|
||||
String alias = aliasEditText.getText().toString();
|
||||
hideSoftKeyboard(aliasEditText);
|
||||
String alias = aliasEditText.getText().toString().trim();
|
||||
if (toUtf8(alias).length > MAX_AUTHOR_NAME_LENGTH) {
|
||||
aliasEditLayout.setError(getString(R.string.name_too_long));
|
||||
} else {
|
||||
@@ -91,4 +95,17 @@ public class AliasDialogFragment extends AppCompatDialogFragment {
|
||||
}
|
||||
}
|
||||
|
||||
private void onCancelButtonClicked() {
|
||||
hideSoftKeyboard(aliasEditText);
|
||||
getDialog().cancel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
requireNonNull(getDialog().getWindow())
|
||||
.setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_VISIBLE);
|
||||
showSoftKeyboard(aliasEditText);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,264 +0,0 @@
|
||||
package org.briarproject.briar.android.conversation;
|
||||
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.BitmapFactory.Options;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.media.ExifInterface;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import com.bumptech.glide.util.MarkEnforcingInputStream;
|
||||
|
||||
import org.briarproject.bramble.api.Pair;
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.android.conversation.ImageHelper.DecodeResult;
|
||||
import org.briarproject.briar.api.messaging.Attachment;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static android.support.media.ExifInterface.ORIENTATION_ROTATE_270;
|
||||
import static android.support.media.ExifInterface.ORIENTATION_ROTATE_90;
|
||||
import static android.support.media.ExifInterface.ORIENTATION_TRANSPOSE;
|
||||
import static android.support.media.ExifInterface.ORIENTATION_TRANSVERSE;
|
||||
import static android.support.media.ExifInterface.TAG_IMAGE_LENGTH;
|
||||
import static android.support.media.ExifInterface.TAG_IMAGE_WIDTH;
|
||||
import static android.support.media.ExifInterface.TAG_ORIENTATION;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
|
||||
@NotNullByDefault
|
||||
class AttachmentController {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(AttachmentController.class.getName());
|
||||
private static final int READ_LIMIT = 1024 * 8192;
|
||||
|
||||
private final MessagingManager messagingManager;
|
||||
private final ImageHelper imageHelper;
|
||||
private final int defaultSize;
|
||||
private final int minWidth, maxWidth;
|
||||
private final int minHeight, maxHeight;
|
||||
|
||||
private final Map<MessageId, List<AttachmentItem>> attachmentCache =
|
||||
new ConcurrentHashMap<>();
|
||||
|
||||
AttachmentController(MessagingManager messagingManager,
|
||||
AttachmentDimensions dimensions, ImageHelper imageHelper) {
|
||||
this.messagingManager = messagingManager;
|
||||
this.imageHelper = imageHelper;
|
||||
defaultSize = dimensions.defaultSize;
|
||||
minWidth = dimensions.minWidth;
|
||||
maxWidth = dimensions.maxWidth;
|
||||
minHeight = dimensions.minHeight;
|
||||
maxHeight = dimensions.maxHeight;
|
||||
}
|
||||
|
||||
AttachmentController(MessagingManager messagingManager,
|
||||
AttachmentDimensions dimensions) {
|
||||
this(messagingManager, dimensions, new ImageHelper() {
|
||||
@Override
|
||||
public DecodeResult decodeStream(InputStream is) {
|
||||
Options options = new Options();
|
||||
options.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeStream(is, null, options);
|
||||
String mimeType = options.outMimeType;
|
||||
if (mimeType == null) mimeType = "";
|
||||
return new DecodeResult(options.outWidth, options.outHeight,
|
||||
mimeType);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getExtensionFromMimeType(String mimeType) {
|
||||
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
||||
return mimeTypeMap.getExtensionFromMimeType(mimeType);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void put(MessageId messageId, List<AttachmentItem> attachments) {
|
||||
attachmentCache.put(messageId, attachments);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
List<AttachmentItem> get(MessageId messageId) {
|
||||
return attachmentCache.get(messageId);
|
||||
}
|
||||
|
||||
@DatabaseExecutor
|
||||
List<Pair<AttachmentHeader, Attachment>> getMessageAttachments(
|
||||
List<AttachmentHeader> headers) throws DbException {
|
||||
long start = now();
|
||||
List<Pair<AttachmentHeader, Attachment>> attachments =
|
||||
new ArrayList<>(headers.size());
|
||||
for (AttachmentHeader h : headers) {
|
||||
Attachment a = messagingManager.getAttachment(h.getMessageId());
|
||||
attachments.add(new Pair<>(h, a));
|
||||
}
|
||||
logDuration(LOG, "Loading attachment", start);
|
||||
return attachments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates {@link AttachmentItem}s from the passed headers and Attachments.
|
||||
* <p>
|
||||
* Note: This closes the {@link Attachment}'s {@link InputStream}.
|
||||
*/
|
||||
List<AttachmentItem> getAttachmentItems(
|
||||
List<Pair<AttachmentHeader, Attachment>> attachments) {
|
||||
boolean needsSize = attachments.size() == 1;
|
||||
List<AttachmentItem> items = new ArrayList<>(attachments.size());
|
||||
for (Pair<AttachmentHeader, Attachment> a : attachments) {
|
||||
AttachmentItem item =
|
||||
getAttachmentItem(a.getFirst(), a.getSecond(), needsSize);
|
||||
items.add(item);
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an {@link AttachmentItem} from the {@link Attachment}'s
|
||||
* {@link InputStream} which will be closed when this method returns.
|
||||
*/
|
||||
AttachmentItem getAttachmentItem(AttachmentHeader h, Attachment a,
|
||||
boolean needsSize) {
|
||||
MessageId messageId = h.getMessageId();
|
||||
if (!needsSize) {
|
||||
String mimeType = h.getContentType();
|
||||
String extension = imageHelper.getExtensionFromMimeType(mimeType);
|
||||
boolean hasError = false;
|
||||
if (extension == null) {
|
||||
extension = "";
|
||||
hasError = true;
|
||||
}
|
||||
return new AttachmentItem(messageId, 0, 0, mimeType, extension, 0,
|
||||
0, hasError);
|
||||
}
|
||||
|
||||
Size size = new Size();
|
||||
InputStream is = new MarkEnforcingInputStream(
|
||||
new BufferedInputStream(a.getStream()));
|
||||
is.mark(READ_LIMIT);
|
||||
try {
|
||||
// use exif to get size
|
||||
if (h.getContentType().equals("image/jpeg")) {
|
||||
size = getSizeFromExif(is);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
try {
|
||||
// use BitmapFactory to get size
|
||||
if (size.error) {
|
||||
is.reset();
|
||||
// need to mark again to re-add read limit
|
||||
is.mark(READ_LIMIT);
|
||||
size = getSizeFromBitmap(is);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
} finally {
|
||||
tryToClose(is, LOG, WARNING);
|
||||
}
|
||||
|
||||
// calculate thumbnail size
|
||||
Size thumbnailSize = new Size(defaultSize, defaultSize, size.mimeType);
|
||||
if (!size.error) {
|
||||
thumbnailSize =
|
||||
getThumbnailSize(size.width, size.height, size.mimeType);
|
||||
}
|
||||
// get file extension
|
||||
String extension = imageHelper.getExtensionFromMimeType(size.mimeType);
|
||||
boolean hasError = extension == null || size.error;
|
||||
if (extension == null) extension = "";
|
||||
return new AttachmentItem(messageId, size.width, size.height,
|
||||
size.mimeType, extension, thumbnailSize.width,
|
||||
thumbnailSize.height, hasError);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the size of a JPEG {@link InputStream} if EXIF info is available.
|
||||
*/
|
||||
private Size getSizeFromExif(InputStream is) throws IOException {
|
||||
ExifInterface exif = new ExifInterface(is);
|
||||
// these can return 0 independent of default value
|
||||
int width = exif.getAttributeInt(TAG_IMAGE_WIDTH, 0);
|
||||
int height = exif.getAttributeInt(TAG_IMAGE_LENGTH, 0);
|
||||
if (width == 0 || height == 0) return new Size();
|
||||
int orientation = exif.getAttributeInt(TAG_ORIENTATION, 0);
|
||||
if (orientation == ORIENTATION_ROTATE_90 ||
|
||||
orientation == ORIENTATION_ROTATE_270 ||
|
||||
orientation == ORIENTATION_TRANSVERSE ||
|
||||
orientation == ORIENTATION_TRANSPOSE) {
|
||||
//noinspection SuspiciousNameCombination
|
||||
return new Size(height, width, "image/jpeg");
|
||||
}
|
||||
return new Size(width, height, "image/jpeg");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the size of any image {@link InputStream}.
|
||||
*/
|
||||
private Size getSizeFromBitmap(InputStream is) {
|
||||
DecodeResult result = imageHelper.decodeStream(is);
|
||||
if (result.width < 1 || result.height < 1) return new Size();
|
||||
return new Size(result.width, result.height, result.mimeType);
|
||||
}
|
||||
|
||||
private Size getThumbnailSize(int width, int height, String mimeType) {
|
||||
float widthPercentage = maxWidth / (float) width;
|
||||
float heightPercentage = maxHeight / (float) height;
|
||||
float scaleFactor = Math.min(widthPercentage, heightPercentage);
|
||||
if (scaleFactor > 1) scaleFactor = 1f;
|
||||
int thumbnailWidth = (int) (width * scaleFactor);
|
||||
int thumbnailHeight = (int) (height * scaleFactor);
|
||||
if (thumbnailWidth < minWidth || thumbnailHeight < minHeight) {
|
||||
widthPercentage = minWidth / (float) width;
|
||||
heightPercentage = minHeight / (float) height;
|
||||
scaleFactor = Math.max(widthPercentage, heightPercentage);
|
||||
thumbnailWidth = (int) (width * scaleFactor);
|
||||
thumbnailHeight = (int) (height * scaleFactor);
|
||||
if (thumbnailWidth > maxWidth) thumbnailWidth = maxWidth;
|
||||
if (thumbnailHeight > maxHeight) thumbnailHeight = maxHeight;
|
||||
}
|
||||
return new Size(thumbnailWidth, thumbnailHeight, mimeType);
|
||||
}
|
||||
|
||||
private static class Size {
|
||||
|
||||
private final int width;
|
||||
private final int height;
|
||||
private final String mimeType;
|
||||
private final boolean error;
|
||||
|
||||
private Size(int width, int height, String mimeType) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.mimeType = mimeType;
|
||||
this.error = false;
|
||||
}
|
||||
|
||||
private Size() {
|
||||
this.width = 0;
|
||||
this.height = 0;
|
||||
this.mimeType = "";
|
||||
this.error = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import android.arch.lifecycle.ViewModelProvider;
|
||||
import android.arch.lifecycle.ViewModelProviders;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.Nullable;
|
||||
@@ -30,14 +29,15 @@ import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.briarproject.bramble.api.FeatureFlags;
|
||||
import org.briarproject.bramble.api.Pair;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.contact.event.ContactRemovedEvent;
|
||||
import org.briarproject.bramble.api.crypto.CryptoExecutor;
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.NoSuchContactException;
|
||||
import org.briarproject.bramble.api.db.NoSuchMessageException;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
@@ -46,13 +46,14 @@ import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||
import org.briarproject.bramble.api.settings.SettingsManager;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent;
|
||||
import org.briarproject.bramble.api.sync.event.MessagesSentEvent;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.activity.BriarActivity;
|
||||
import org.briarproject.briar.android.attachment.AttachmentItem;
|
||||
import org.briarproject.briar.android.attachment.AttachmentRetriever;
|
||||
import org.briarproject.briar.android.blog.BlogActivity;
|
||||
import org.briarproject.briar.android.conversation.ConversationVisitor.AttachmentCache;
|
||||
import org.briarproject.briar.android.conversation.ConversationVisitor.TextCache;
|
||||
@@ -63,10 +64,9 @@ import org.briarproject.briar.android.util.BriarSnackbarBuilder;
|
||||
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||
import org.briarproject.briar.android.view.ImagePreview;
|
||||
import org.briarproject.briar.android.view.TextAttachmentController;
|
||||
import org.briarproject.briar.android.view.TextAttachmentController.AttachImageListener;
|
||||
import org.briarproject.briar.android.view.TextAttachmentController.AttachmentListener;
|
||||
import org.briarproject.briar.android.view.TextInputView;
|
||||
import org.briarproject.briar.android.view.TextSendController;
|
||||
import org.briarproject.briar.android.view.TextSendController.SendListener;
|
||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||
import org.briarproject.briar.api.blog.BlogSharingManager;
|
||||
import org.briarproject.briar.api.client.ProtocolStateException;
|
||||
@@ -81,8 +81,8 @@ import org.briarproject.briar.api.introduction.IntroductionManager;
|
||||
import org.briarproject.briar.api.messaging.Attachment;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageFactory;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||
import org.briarproject.briar.api.messaging.event.AttachmentReceivedEvent;
|
||||
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -92,7 +92,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -110,15 +109,16 @@ import static android.support.v7.util.SortedList.INVALID_POSITION;
|
||||
import static android.view.Gravity.RIGHT;
|
||||
import static android.widget.Toast.LENGTH_SHORT;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.Collections.sort;
|
||||
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.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
import static org.briarproject.briar.android.TestingConstants.FEATURE_FLAG_IMAGE_ATTACHMENTS;
|
||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_ATTACH_IMAGE;
|
||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_INTRODUCTION;
|
||||
import static org.briarproject.briar.android.conversation.ImageActivity.ATTACHMENTS;
|
||||
@@ -128,6 +128,7 @@ import static org.briarproject.briar.android.conversation.ImageActivity.NAME;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getAvatarTransitionName;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getBulbTransitionName;
|
||||
import static org.briarproject.briar.android.util.UiUtils.observeOnce;
|
||||
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_ATTACHMENTS_PER_MESSAGE;
|
||||
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_TEXT_LENGTH;
|
||||
import static uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt.STATE_DISMISSED;
|
||||
import static uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt.STATE_FINISHED;
|
||||
@@ -135,13 +136,13 @@ import static uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt.S
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class ConversationActivity extends BriarActivity
|
||||
implements EventListener, ConversationListener, SendListener,
|
||||
TextCache, AttachmentCache, AttachImageListener {
|
||||
implements EventListener, ConversationListener, TextCache,
|
||||
AttachmentCache, AttachmentListener {
|
||||
|
||||
public static final String CONTACT_ID = "briar.CONTACT_ID";
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ConversationActivity.class.getName());
|
||||
getLogger(ConversationActivity.class.getName());
|
||||
|
||||
private static final int TRANSITION_DURATION_MS = 500;
|
||||
private static final int ONBOARDING_DELAY_MS = 250;
|
||||
@@ -151,10 +152,9 @@ public class ConversationActivity extends BriarActivity
|
||||
@Inject
|
||||
ConnectionRegistry connectionRegistry;
|
||||
@Inject
|
||||
@CryptoExecutor
|
||||
Executor cryptoExecutor;
|
||||
@Inject
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
@Inject
|
||||
FeatureFlags featureFlags;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject
|
||||
@@ -166,10 +166,6 @@ public class ConversationActivity extends BriarActivity
|
||||
@Inject
|
||||
volatile EventBus eventBus;
|
||||
@Inject
|
||||
volatile SettingsManager settingsManager;
|
||||
@Inject
|
||||
volatile PrivateMessageFactory privateMessageFactory;
|
||||
@Inject
|
||||
volatile IntroductionManager introductionManager;
|
||||
@Inject
|
||||
volatile ForumSharingManager forumSharingManager;
|
||||
@@ -179,12 +175,14 @@ public class ConversationActivity extends BriarActivity
|
||||
volatile GroupInvitationManager groupInvitationManager;
|
||||
|
||||
private final Map<MessageId, String> textCache = new ConcurrentHashMap<>();
|
||||
private final Map<MessageId, PrivateMessageHeader> missingAttachments =
|
||||
new ConcurrentHashMap<>();
|
||||
private final Observer<String> contactNameObserver = name -> {
|
||||
requireNonNull(name);
|
||||
loadMessages();
|
||||
};
|
||||
|
||||
private AttachmentController attachmentController;
|
||||
private AttachmentRetriever attachmentRetriever;
|
||||
private ConversationViewModel viewModel;
|
||||
private ConversationVisitor visitor;
|
||||
private ConversationAdapter adapter;
|
||||
@@ -219,7 +217,7 @@ public class ConversationActivity extends BriarActivity
|
||||
|
||||
viewModel = ViewModelProviders.of(this, viewModelFactory)
|
||||
.get(ConversationViewModel.class);
|
||||
attachmentController = viewModel.getAttachmentController();
|
||||
attachmentRetriever = viewModel.getAttachmentRetriever();
|
||||
|
||||
setContentView(R.layout.activity_conversation);
|
||||
|
||||
@@ -242,7 +240,7 @@ public class ConversationActivity extends BriarActivity
|
||||
requireNonNull(deleted);
|
||||
if (deleted) finish();
|
||||
});
|
||||
viewModel.getAddedPrivateMessage().observe(this,
|
||||
viewModel.getAddedPrivateMessage().observeEvent(this,
|
||||
this::onAddedPrivateMessage);
|
||||
|
||||
setTransitionName(toolbarAvatar, getAvatarTransitionName(contactId));
|
||||
@@ -261,13 +259,13 @@ public class ConversationActivity extends BriarActivity
|
||||
list.getRecyclerView().addOnScrollListener(scrollListener);
|
||||
|
||||
textInputView = findViewById(R.id.text_input_container);
|
||||
if (FEATURE_FLAG_IMAGE_ATTACHMENTS) {
|
||||
if (featureFlags.shouldEnableImageAttachments()) {
|
||||
ImagePreview imagePreview = findViewById(R.id.imagePreview);
|
||||
sendController = new TextAttachmentController(textInputView,
|
||||
imagePreview, this, this);
|
||||
imagePreview, this, viewModel);
|
||||
observeOnce(viewModel.hasImageSupport(), this, hasSupport -> {
|
||||
if (hasSupport != null && hasSupport) {
|
||||
// remove cast when removing FEATURE_FLAG_IMAGE_ATTACHMENTS
|
||||
// TODO: remove cast when removing feature flag
|
||||
((TextAttachmentController) sendController)
|
||||
.setImagesSupported();
|
||||
}
|
||||
@@ -278,7 +276,7 @@ public class ConversationActivity extends BriarActivity
|
||||
textInputView.setSendController(sendController);
|
||||
textInputView.setMaxTextLength(MAX_PRIVATE_MESSAGE_TEXT_LENGTH);
|
||||
textInputView.setReady(false);
|
||||
textInputView.addOnKeyboardShownListener(this::scrollToBottom);
|
||||
textInputView.setOnKeyboardShownListener(this::scrollToBottom);
|
||||
}
|
||||
|
||||
private void scrollToBottom() {
|
||||
@@ -302,7 +300,7 @@ public class ConversationActivity extends BriarActivity
|
||||
Snackbar.LENGTH_SHORT)
|
||||
.show();
|
||||
} else if (request == REQUEST_ATTACH_IMAGE && result == RESULT_OK) {
|
||||
// remove cast when removing FEATURE_FLAG_IMAGE_ATTACHMENTS
|
||||
// TODO: remove cast when removing feature flag
|
||||
((TextAttachmentController) sendController).onImageReceived(data);
|
||||
}
|
||||
}
|
||||
@@ -358,6 +356,11 @@ public class ConversationActivity extends BriarActivity
|
||||
MenuInflater inflater = getMenuInflater();
|
||||
inflater.inflate(R.menu.conversation_actions, menu);
|
||||
|
||||
// Hide private message deletion action if feature is not enabled
|
||||
if (!featureFlags.shouldEnablePrivateMessageDeletion()) {
|
||||
menu.removeItem(R.id.action_delete_all_messages);
|
||||
}
|
||||
|
||||
// enable introduction action if available
|
||||
observeOnce(viewModel.showIntroductionAction(), this, enable -> {
|
||||
if (enable != null && enable) {
|
||||
@@ -391,6 +394,9 @@ public class ConversationActivity extends BriarActivity
|
||||
AliasDialogFragment.newInstance().show(
|
||||
getSupportFragmentManager(), AliasDialogFragment.TAG);
|
||||
return true;
|
||||
case R.id.action_delete_all_messages:
|
||||
askToDeleteAllMessages();
|
||||
return true;
|
||||
case R.id.action_social_remove_person:
|
||||
askToRemoveContact();
|
||||
return true;
|
||||
@@ -442,29 +448,40 @@ public class ConversationActivity extends BriarActivity
|
||||
});
|
||||
}
|
||||
|
||||
private void eagerlyLoadMessageSize(PrivateMessageHeader h)
|
||||
throws DbException {
|
||||
MessageId id = h.getId();
|
||||
// If the message has text, load it
|
||||
if (h.hasText()) {
|
||||
String text = textCache.get(id);
|
||||
if (text == null) {
|
||||
LOG.info("Eagerly loading text for latest message");
|
||||
text = messagingManager.getMessageText(id);
|
||||
textCache.put(id, text);
|
||||
private void eagerlyLoadMessageSize(PrivateMessageHeader h) {
|
||||
try {
|
||||
MessageId id = h.getId();
|
||||
// If the message has text, load it
|
||||
if (h.hasText()) {
|
||||
String text = textCache.get(id);
|
||||
if (text == null) {
|
||||
LOG.info("Eagerly loading text for latest message");
|
||||
text = messagingManager.getMessageText(id);
|
||||
textCache.put(id, requireNonNull(text));
|
||||
}
|
||||
}
|
||||
}
|
||||
// If the message has a single image, load its size - for multiple
|
||||
// images we use a grid so the size is fixed
|
||||
if (h.getAttachmentHeaders().size() == 1) {
|
||||
List<AttachmentItem> items = attachmentController.get(id);
|
||||
if (items == null) {
|
||||
LOG.info("Eagerly loading image size for latest message");
|
||||
items = attachmentController.getAttachmentItems(
|
||||
attachmentController.getMessageAttachments(
|
||||
h.getAttachmentHeaders()));
|
||||
attachmentController.put(id, items);
|
||||
// If the message has a single image, load its size - for multiple
|
||||
// images we use a grid so the size is fixed
|
||||
List<AttachmentHeader> headers = h.getAttachmentHeaders();
|
||||
if (headers.size() == 1) {
|
||||
List<AttachmentItem> items = attachmentRetriever.cacheGet(id);
|
||||
if (items == null) {
|
||||
LOG.info("Eagerly loading image size for latest message");
|
||||
AttachmentHeader header = headers.get(0);
|
||||
try {
|
||||
Attachment a = attachmentRetriever
|
||||
.getMessageAttachment(header);
|
||||
AttachmentItem item =
|
||||
attachmentRetriever.getAttachmentItem(a, true);
|
||||
attachmentRetriever.cachePut(id, singletonList(item));
|
||||
} catch (NoSuchMessageException e) {
|
||||
LOG.info("Attachment not received yet");
|
||||
missingAttachments.put(header.getMessageId(), h);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -475,8 +492,10 @@ public class ConversationActivity extends BriarActivity
|
||||
adapter.incrementRevision();
|
||||
textInputView.setReady(true);
|
||||
// start observing onboarding after enabling
|
||||
viewModel.showImageOnboarding().observeEvent(this,
|
||||
this::showImageOnboarding);
|
||||
if (featureFlags.shouldEnableImageAttachments()) {
|
||||
viewModel.showImageOnboarding().observeEvent(this,
|
||||
this::showImageOnboarding);
|
||||
}
|
||||
List<ConversationItem> items = createItems(headers);
|
||||
adapter.addAll(items);
|
||||
list.showData();
|
||||
@@ -512,7 +531,7 @@ public class ConversationActivity extends BriarActivity
|
||||
long start = now();
|
||||
String text = messagingManager.getMessageText(m);
|
||||
logDuration(LOG, "Loading text", start);
|
||||
displayMessageText(m, text);
|
||||
displayMessageText(m, requireNonNull(text));
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
@@ -540,16 +559,30 @@ public class ConversationActivity extends BriarActivity
|
||||
&& adapter.isScrolledToBottom(layoutManager);
|
||||
}
|
||||
|
||||
private void loadMessageAttachments(MessageId messageId,
|
||||
List<AttachmentHeader> headers) {
|
||||
private void loadMessageAttachments(PrivateMessageHeader h) {
|
||||
// TODO: Use placeholders for missing/invalid attachments
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
List<Pair<AttachmentHeader, Attachment>> attachments =
|
||||
attachmentController.getMessageAttachments(headers);
|
||||
// TODO move getting the items off to IoExecutor, if size == 1
|
||||
List<AttachmentItem> items =
|
||||
attachmentController.getAttachmentItems(attachments);
|
||||
displayMessageAttachments(messageId, items);
|
||||
List<AttachmentHeader> headers = h.getAttachmentHeaders();
|
||||
boolean needsSize = headers.size() == 1;
|
||||
List<AttachmentItem> items = new ArrayList<>(headers.size());
|
||||
for (AttachmentHeader header : headers) {
|
||||
try {
|
||||
Attachment a = attachmentRetriever
|
||||
.getMessageAttachment(header);
|
||||
AttachmentItem item = attachmentRetriever
|
||||
.getAttachmentItem(a, needsSize);
|
||||
items.add(item);
|
||||
} catch (NoSuchMessageException e) {
|
||||
LOG.info("Attachment not received yet");
|
||||
missingAttachments.put(header.getMessageId(), h);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Don't cache items unless all are present and valid
|
||||
attachmentRetriever.cachePut(h.getId(), items);
|
||||
displayMessageAttachments(h.getId(), items);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
@@ -559,7 +592,6 @@ public class ConversationActivity extends BriarActivity
|
||||
private void displayMessageAttachments(MessageId m,
|
||||
List<AttachmentItem> items) {
|
||||
runOnUiThreadUnlessDestroyed(() -> {
|
||||
attachmentController.put(m, items);
|
||||
Pair<Integer, ConversationMessageItem> pair =
|
||||
adapter.getMessageItem(m);
|
||||
if (pair != null) {
|
||||
@@ -573,6 +605,13 @@ public class ConversationActivity extends BriarActivity
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof AttachmentReceivedEvent) {
|
||||
AttachmentReceivedEvent a = (AttachmentReceivedEvent) e;
|
||||
if (a.getContactId().equals(contactId)) {
|
||||
LOG.info("Attachment received");
|
||||
onAttachmentReceived(a.getMessageId());
|
||||
}
|
||||
}
|
||||
if (e instanceof ContactRemovedEvent) {
|
||||
ContactRemovedEvent c = (ContactRemovedEvent) e;
|
||||
if (c.getContactId().equals(contactId)) {
|
||||
@@ -623,6 +662,15 @@ public class ConversationActivity extends BriarActivity
|
||||
scrollToBottom();
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void onAttachmentReceived(MessageId attachmentId) {
|
||||
PrivateMessageHeader h = missingAttachments.remove(attachmentId);
|
||||
if (h != null) {
|
||||
LOG.info("Missing attachment received");
|
||||
loadMessageAttachments(h);
|
||||
}
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void onNewConversationMessage(ConversationMessageHeader h) {
|
||||
if (h instanceof ConversationRequest ||
|
||||
@@ -658,12 +706,21 @@ public class ConversationActivity extends BriarActivity
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSendClick(@Nullable String text, List<Uri> imageUris) {
|
||||
if (isNullOrEmpty(text) && imageUris.isEmpty())
|
||||
public void onTooManyAttachments() {
|
||||
String format = getResources().getString(
|
||||
R.string.messaging_too_many_attachments_toast);
|
||||
String warning = String.format(format, MAX_ATTACHMENTS_PER_MESSAGE);
|
||||
Toast.makeText(this, warning, LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSendClick(@Nullable String text,
|
||||
List<AttachmentHeader> attachmentHeaders) {
|
||||
if (isNullOrEmpty(text) && attachmentHeaders.isEmpty())
|
||||
throw new AssertionError();
|
||||
long timestamp = System.currentTimeMillis();
|
||||
timestamp = Math.max(timestamp, getMinTimestampForNewMessage());
|
||||
viewModel.sendMessage(text, imageUris, timestamp);
|
||||
viewModel.sendMessage(text, attachmentHeaders, timestamp);
|
||||
textInputView.clearText();
|
||||
}
|
||||
|
||||
@@ -676,7 +733,52 @@ public class ConversationActivity extends BriarActivity
|
||||
private void onAddedPrivateMessage(@Nullable PrivateMessageHeader h) {
|
||||
if (h == null) return;
|
||||
addConversationItem(h.accept(visitor));
|
||||
viewModel.onAddedPrivateMessageSeen();
|
||||
}
|
||||
|
||||
private void askToDeleteAllMessages() {
|
||||
AlertDialog.Builder builder =
|
||||
new AlertDialog.Builder(this, R.style.BriarDialogTheme);
|
||||
builder.setTitle(getString(R.string.dialog_title_delete_all_messages));
|
||||
builder.setMessage(
|
||||
getString(R.string.dialog_message_delete_all_messages));
|
||||
builder.setNegativeButton(R.string.delete,
|
||||
(dialog, which) -> deleteAllMessages());
|
||||
builder.setPositiveButton(R.string.cancel, null);
|
||||
builder.show();
|
||||
}
|
||||
|
||||
private void deleteAllMessages() {
|
||||
list.showProgressBar();
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
boolean allDeleted =
|
||||
conversationManager.deleteAllMessages(contactId);
|
||||
reloadConversationAfterDeletingAllMessages(allDeleted);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
runOnUiThreadUnlessDestroyed(() -> list.showData());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void reloadConversationAfterDeletingAllMessages(
|
||||
boolean allDeleted) {
|
||||
runOnUiThreadUnlessDestroyed(() -> {
|
||||
adapter.clear();
|
||||
loadMessages();
|
||||
if (!allDeleted) showNotAllDeletedDialog();
|
||||
});
|
||||
}
|
||||
|
||||
private void showNotAllDeletedDialog() {
|
||||
AlertDialog.Builder builder =
|
||||
new AlertDialog.Builder(this, R.style.BriarDialogTheme);
|
||||
builder.setTitle(
|
||||
getString(R.string.dialog_title_not_all_messages_deleted));
|
||||
builder.setMessage(
|
||||
getString(R.string.dialog_message_not_all_messages_deleted));
|
||||
builder.setPositiveButton(R.string.ok, null);
|
||||
builder.show();
|
||||
}
|
||||
|
||||
private void askToRemoveContact() {
|
||||
@@ -726,7 +828,7 @@ public class ConversationActivity extends BriarActivity
|
||||
}
|
||||
|
||||
private void showImageOnboarding() {
|
||||
// remove cast when removing FEATURE_FLAG_IMAGE_ATTACHMENTS
|
||||
// TODO: remove cast when removing feature flag
|
||||
((TextAttachmentController) sendController)
|
||||
.showImageOnboarding(this, () ->
|
||||
viewModel.onImageOnboardingSeen());
|
||||
@@ -901,11 +1003,11 @@ public class ConversationActivity extends BriarActivity
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AttachmentItem> getAttachmentItems(MessageId m,
|
||||
List<AttachmentHeader> headers) {
|
||||
List<AttachmentItem> attachments = attachmentController.get(m);
|
||||
public List<AttachmentItem> getAttachmentItems(PrivateMessageHeader h) {
|
||||
List<AttachmentItem> attachments =
|
||||
attachmentRetriever.cacheGet(h.getId());
|
||||
if (attachments == null) {
|
||||
loadMessageAttachments(m, headers);
|
||||
loadMessageAttachments(h);
|
||||
return emptyList();
|
||||
}
|
||||
return attachments;
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.support.annotation.UiThread;
|
||||
import android.view.View;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.android.attachment.AttachmentItem;
|
||||
|
||||
@UiThread
|
||||
@NotNullByDefault
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.briarproject.briar.android.conversation;
|
||||
import android.support.annotation.LayoutRes;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.android.attachment.AttachmentItem;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user