mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Compare commits
115 Commits
1881-threa
...
1866-blog-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4f38e81ea | ||
|
|
4074ac8578 | ||
|
|
e97478a21a | ||
|
|
726ebcea3f | ||
|
|
2f969775d8 | ||
|
|
d3b855318c | ||
|
|
95104d3383 | ||
|
|
6860a04e8b | ||
|
|
33c24f8655 | ||
|
|
1fa4b78474 | ||
|
|
b678de7529 | ||
|
|
ab1ed0ff5a | ||
|
|
ad20e5230a | ||
|
|
ae923e5777 | ||
|
|
46b4204805 | ||
|
|
2257c005b3 | ||
|
|
eb9ff9c954 | ||
|
|
4f08f81779 | ||
|
|
2b0815aaac | ||
|
|
a9e83491d3 | ||
|
|
ee967c5d8f | ||
|
|
43740777d4 | ||
|
|
d5b0556ea2 | ||
|
|
227f00c10c | ||
|
|
8b4ff2dc8a | ||
|
|
4be2afb915 | ||
|
|
74447b8ec3 | ||
|
|
d95242bd7e | ||
|
|
51794424ce | ||
|
|
5db099bae6 | ||
|
|
a2faa3bd3b | ||
|
|
a3fb7b5680 | ||
|
|
264d110dbd | ||
|
|
839b871a45 | ||
|
|
2fb4825b8f | ||
|
|
3f9a66b1b6 | ||
|
|
d796916387 | ||
|
|
fe07b760ea | ||
|
|
b4a5fe6772 | ||
|
|
e21e6267d7 | ||
|
|
d7afbdf690 | ||
|
|
c5d2661c1d | ||
|
|
b738bdd14e | ||
|
|
629cff20a3 | ||
|
|
6cfb70db95 | ||
|
|
737ecfb620 | ||
|
|
5a424b178e | ||
|
|
59f4e7c34a | ||
|
|
2480824d69 | ||
|
|
a6c2000d81 | ||
|
|
a38a3139d9 | ||
|
|
4c8adaa02b | ||
|
|
8a534b4503 | ||
|
|
e5b2275c82 | ||
|
|
5159593825 | ||
|
|
a546fecc01 | ||
|
|
3e7e37f5f6 | ||
|
|
d095ba0b15 | ||
|
|
7fab97d26c | ||
|
|
6fbc82ee27 | ||
|
|
885b03cfd7 | ||
|
|
f81bfcafeb | ||
|
|
f36f1cf3d4 | ||
|
|
7d6a63d866 | ||
|
|
15ebdf8dd5 | ||
|
|
db2c235283 | ||
|
|
6b61725c6a | ||
|
|
e5bd43469e | ||
|
|
9366c184d8 | ||
|
|
73d2c964d4 | ||
|
|
fb2b4209cf | ||
|
|
a04b512497 | ||
|
|
3d9515e308 | ||
|
|
1b19b331b1 | ||
|
|
d151a2d7f7 | ||
|
|
9712a4b849 | ||
|
|
cf1ac5e3e5 | ||
|
|
cb859e998d | ||
|
|
0b9345f867 | ||
|
|
12988120d1 | ||
|
|
8d6c866e62 | ||
|
|
8f82cf3c73 | ||
|
|
21112ce092 | ||
|
|
21ee3ea00d | ||
|
|
bb964101b3 | ||
|
|
d796eff0f6 | ||
|
|
700ea2b387 | ||
|
|
e4a66615a7 | ||
|
|
6e3a7d8d0c | ||
|
|
166b5d4add | ||
|
|
0fd59a26f6 | ||
|
|
4162bf990a | ||
|
|
09cfadbf7e | ||
|
|
3fb27dbb12 | ||
|
|
ae4a04bada | ||
|
|
831c65b647 | ||
|
|
afcd38b84c | ||
|
|
d670179e30 | ||
|
|
998c435b13 | ||
|
|
4a0327a62b | ||
|
|
70532732c8 | ||
|
|
d69406dfe3 | ||
|
|
98619df867 | ||
|
|
f2eca0fdb6 | ||
|
|
c62a57e8b2 | ||
|
|
239c4a27ad | ||
|
|
e5d78a858d | ||
|
|
5c1bcdeb9d | ||
|
|
6c1f5450cb | ||
|
|
0d070cf422 | ||
|
|
d34d66c691 | ||
|
|
6005d156eb | ||
|
|
94dd75f24b | ||
|
|
c93e5441b0 | ||
|
|
8ec8cc927b |
@@ -2,6 +2,7 @@ image: briar/ci-image-android:latest
|
||||
|
||||
stages:
|
||||
- test
|
||||
- optional_tests
|
||||
- check_reproducibility
|
||||
|
||||
test:
|
||||
@@ -31,3 +32,33 @@ test_reproducible:
|
||||
- "curl -X POST -F token=${RELEASE_CHECK_TOKEN} -F ref=master -F variables[RELEASE_TAG]=${CI_COMMIT_REF_NAME} https://code.briarproject.org/api/v4/projects/61/trigger/pipeline"
|
||||
only:
|
||||
- tags
|
||||
|
||||
.optional_tests:
|
||||
stage: optional_tests
|
||||
before_script:
|
||||
- set -e
|
||||
- export GRADLE_USER_HOME=$PWD/.gradle
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .gradle/wrapper
|
||||
- .gradle/caches
|
||||
|
||||
script:
|
||||
- OPTIONAL_TESTS=org.briarproject.bramble.plugin.tor.BridgeTest ./gradlew --info bramble-java:test --tests BridgeTest
|
||||
|
||||
after_script:
|
||||
# these file change every time but should not be cached
|
||||
- rm -f $GRADLE_USER_HOME/caches/modules-2/modules-2.lock
|
||||
- rm -fr $GRADLE_USER_HOME/caches/*/plugin-resolution/
|
||||
|
||||
manual_tests:
|
||||
extends: .optional_tests
|
||||
when: manual
|
||||
except:
|
||||
- tags
|
||||
|
||||
pre_release_tests:
|
||||
extends: .optional_tests
|
||||
only:
|
||||
- tags
|
||||
|
||||
14
.idea/dictionaries/briar.xml
generated
Normal file
14
.idea/dictionaries/briar.xml
generated
Normal file
@@ -0,0 +1,14 @@
|
||||
<component name="ProjectDictionaryState">
|
||||
<dictionary name="briar">
|
||||
<words>
|
||||
<w>briar</w>
|
||||
<w>briarproject</w>
|
||||
<w>emoji</w>
|
||||
<w>encrypter</w>
|
||||
<w>identicon</w>
|
||||
<w>introducee</w>
|
||||
<w>introducer</w>
|
||||
<w>onboarding</w>
|
||||
</words>
|
||||
</dictionary>
|
||||
</component>
|
||||
24
.idea/runConfigurations/BridgeTest.xml
generated
Normal file
24
.idea/runConfigurations/BridgeTest.xml
generated
Normal file
@@ -0,0 +1,24 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="BridgeTest" type="AndroidJUnit" factoryName="Android JUnit" nameIsGenerated="true">
|
||||
<module name="briar.bramble-java" />
|
||||
<useClassPathOnly />
|
||||
<extension name="coverage">
|
||||
<pattern>
|
||||
<option name="PATTERN" value="org.briarproject.bramble.plugin.tor.*" />
|
||||
<option name="ENABLED" value="true" />
|
||||
</pattern>
|
||||
</extension>
|
||||
<option name="PACKAGE_NAME" value="org.briarproject.bramble.plugin.tor" />
|
||||
<option name="MAIN_CLASS_NAME" value="org.briarproject.bramble.plugin.tor.BridgeTest" />
|
||||
<option name="METHOD_NAME" value="" />
|
||||
<option name="TEST_OBJECT" value="class" />
|
||||
<option name="PARAMETERS" value="" />
|
||||
<option name="WORKING_DIRECTORY" value="$MODULE_DIR$" />
|
||||
<envs>
|
||||
<env name="OPTIONAL_TESTS" value="org.briarproject.bramble.plugin.tor.BridgeTest" />
|
||||
</envs>
|
||||
<method v="2">
|
||||
<option name="Android.Gradle.BeforeRunTask" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
||||
@@ -8,11 +8,15 @@ android {
|
||||
compileSdkVersion 30
|
||||
buildToolsVersion '30.0.2'
|
||||
|
||||
packagingOptions {
|
||||
doNotStrip '**/*.so'
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 16
|
||||
targetSdkVersion 29
|
||||
versionCode 10213
|
||||
versionName "1.2.13"
|
||||
versionCode 10218
|
||||
versionName "1.2.18"
|
||||
consumerProguardFiles 'proguard-rules.txt'
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
@@ -38,8 +42,8 @@ configurations {
|
||||
|
||||
dependencies {
|
||||
implementation project(path: ':bramble-core', configuration: 'default')
|
||||
tor 'org.briarproject:tor-android:0.3.5.12@zip'
|
||||
tor 'org.briarproject:obfs4proxy-android:0.0.11-2@zip'
|
||||
tor 'org.briarproject:tor-android:0.3.5.13@zip'
|
||||
tor 'org.briarproject:obfs4proxy-android:0.0.12-dev-40245c4a@zip'
|
||||
|
||||
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
|
||||
|
||||
|
||||
@@ -175,6 +175,11 @@ class AndroidBluetoothPlugin
|
||||
} catch (IOException e) {
|
||||
IoUtils.tryToClose(s, LOG, WARNING);
|
||||
throw e;
|
||||
} catch (NullPointerException e) {
|
||||
// BluetoothSocket#connect() may throw an NPE under unknown
|
||||
// circumstances
|
||||
IoUtils.tryToClose(s, LOG, WARNING);
|
||||
throw new IOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ public class AndroidUtils {
|
||||
private static final String FAKE_BLUETOOTH_ADDRESS = "02:00:00:00:00:00";
|
||||
|
||||
private static final String STORED_REPORTS = "dev-reports";
|
||||
private static final String STORED_LOGCAT = "dev-logcat";
|
||||
|
||||
public static Collection<String> getSupportedArchitectures() {
|
||||
List<String> abis = new ArrayList<>();
|
||||
@@ -107,14 +108,14 @@ public class AndroidUtils {
|
||||
return ctx.getDir(STORED_REPORTS, MODE_PRIVATE);
|
||||
}
|
||||
|
||||
public static File getLogcatFile(Context ctx) {
|
||||
return new File(ctx.getFilesDir(), STORED_LOGCAT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of supported content types for image attachments.
|
||||
* GIFs can't be compressed on API < 24 so they're not supported.
|
||||
* <p>
|
||||
* TODO: Remove this restriction when large message support is added
|
||||
*/
|
||||
public static String[] getSupportedImageContentTypes() {
|
||||
if (SDK_INT < 24) return new String[] {"image/jpeg", "image/png"};
|
||||
else return new String[] {"image/jpeg", "image/png", "image/gif"};
|
||||
return new String[] {"image/jpeg", "image/png", "image/gif"};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,8 +75,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.11-2:obfs4proxy-android-0.0.11-2.zip:57e55cbe87aa2aac210fdbb6cd8cdeafe15f825406a08ebf77a8b787aa2c6a8a',
|
||||
'org.briarproject:tor-android:0.3.5.12:tor-android-0.3.5.12.zip:db71fb3290acff79d572af0752570eaf6aad7c4d88c9b9aa0b4d5afe2b9ead9c',
|
||||
'org.briarproject:obfs4proxy-android:0.0.12-dev-40245c4a:obfs4proxy-android-0.0.12-dev-40245c4a.zip:8ab05a8f8391be2cb5ab2b665c281a06d9e3a756bd0f95a40a36ca927866ea82',
|
||||
'org.briarproject:tor-android:0.3.5.13:tor-android-0.3.5.13.zip:e0978db136731dae07774b722970cdae1e462fb5adc82845dd80a7e2d87f356c',
|
||||
'org.checkerframework:checker-compat-qual:2.5.3:checker-compat-qual-2.5.3.jar:d76b9afea61c7c082908023f0cbc1427fab9abd2df915c8b8a3e7a509bccbc6d',
|
||||
'org.checkerframework:checker-qual:2.5.2:checker-qual-2.5.2.jar:64b02691c8b9d4e7700f8ee2e742dce7ea2c6e81e662b7522c9ee3bf568c040a',
|
||||
'org.checkerframework:checker-qual:2.8.1:checker-qual-2.8.1.jar:9103499008bcecd4e948da29b17864abb64304e15706444ae209d17ebe0575df',
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package org.briarproject.bramble.api;
|
||||
|
||||
public interface ThrowingRunnable<T extends Throwable> {
|
||||
|
||||
void run() throws T;
|
||||
}
|
||||
@@ -19,4 +19,10 @@ public interface StreamDecrypterFactory {
|
||||
*/
|
||||
StreamDecrypter createContactExchangeStreamDecrypter(InputStream in,
|
||||
SecretKey headerKey);
|
||||
|
||||
/**
|
||||
* Creates a {@link StreamDecrypter} for decrypting a log stream.
|
||||
*/
|
||||
StreamDecrypter createLogStreamDecrypter(InputStream in,
|
||||
SecretKey headerKey);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,12 @@ public interface StreamEncrypterFactory {
|
||||
* Creates a {@link StreamEncrypter} for encrypting a contact exchange
|
||||
* stream.
|
||||
*/
|
||||
StreamEncrypter createContactExchangeStreamDecrypter(OutputStream out,
|
||||
StreamEncrypter createContactExchangeStreamEncrypter(OutputStream out,
|
||||
SecretKey headerKey);
|
||||
|
||||
/**
|
||||
* Creates a {@link StreamEncrypter} for encrypting a log stream.
|
||||
*/
|
||||
StreamEncrypter createLogStreamEncrypter(OutputStream out,
|
||||
SecretKey headerKey);
|
||||
}
|
||||
|
||||
@@ -13,4 +13,6 @@ public interface DevConfig {
|
||||
String getDevOnionAddress();
|
||||
|
||||
File getReportDir();
|
||||
|
||||
File getLogcatFile();
|
||||
}
|
||||
|
||||
@@ -16,8 +16,13 @@ public interface StreamReaderFactory {
|
||||
|
||||
/**
|
||||
* Creates an {@link InputStream InputStream} for reading from a contact
|
||||
* exchangestream.
|
||||
* exchange stream.
|
||||
*/
|
||||
InputStream createContactExchangeStreamReader(InputStream in,
|
||||
SecretKey headerKey);
|
||||
|
||||
/**
|
||||
* Creates an {@link InputStream} for reading from a log stream.
|
||||
*/
|
||||
InputStream createLogStreamReader(InputStream in, SecretKey headerKey);
|
||||
}
|
||||
|
||||
@@ -7,17 +7,19 @@ import java.io.OutputStream;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface StreamWriterFactory {
|
||||
|
||||
/**
|
||||
* Creates an {@link OutputStream OutputStream} for writing to a
|
||||
* transport stream
|
||||
* Creates a {@link StreamWriter} for writing to a transport stream.
|
||||
*/
|
||||
StreamWriter createStreamWriter(OutputStream out, StreamContext ctx);
|
||||
|
||||
/**
|
||||
* Creates an {@link OutputStream OutputStream} for writing to a contact
|
||||
* exchange stream.
|
||||
* Creates a {@link StreamWriter} for writing to a contact exchange stream.
|
||||
*/
|
||||
StreamWriter createContactExchangeStreamWriter(OutputStream out,
|
||||
SecretKey headerKey);
|
||||
|
||||
/**
|
||||
* Creates a {@link StreamWriter} for writing to a log stream.
|
||||
*/
|
||||
StreamWriter createLogStreamWriter(OutputStream out, SecretKey headerKey);
|
||||
}
|
||||
|
||||
@@ -9,14 +9,16 @@ import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.lang.Math.min;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
|
||||
class ScryptKdf implements PasswordBasedKdf {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ScryptKdf.class.getName());
|
||||
getLogger(ScryptKdf.class.getName());
|
||||
|
||||
private static final int MIN_COST = 256; // Min parameter N
|
||||
private static final int MAX_COST = 1024 * 1024; // Max parameter N
|
||||
@@ -33,10 +35,20 @@ class ScryptKdf implements PasswordBasedKdf {
|
||||
|
||||
@Override
|
||||
public int chooseCostParameter() {
|
||||
// Scrypt uses at least 128 * N * r bytes of memory. Don't use more
|
||||
// than half of the JVM's max heap size or we may run out of memory.
|
||||
// https://blog.filippo.io/the-scrypt-parameters/
|
||||
long maxMemory = Runtime.getRuntime().maxMemory();
|
||||
long maxCost = min(MAX_COST, maxMemory / BLOCK_SIZE / 256);
|
||||
if (LOG.isLoggable(INFO) && maxCost < MAX_COST) {
|
||||
LOG.info("Max cost capped at " + maxCost
|
||||
+ " due to max heap size " + maxMemory);
|
||||
}
|
||||
// Increase the cost from min to max while measuring performance
|
||||
int cost = MIN_COST;
|
||||
while (cost * 2 <= MAX_COST && measureDuration(cost) * 2 <= TARGET_MS)
|
||||
while (cost * 2 <= maxCost && measureDuration(cost) * 2 <= TARGET_MS) {
|
||||
cost *= 2;
|
||||
}
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("KDF cost parameter " + cost);
|
||||
return cost;
|
||||
|
||||
@@ -36,4 +36,10 @@ class StreamDecrypterFactoryImpl implements StreamDecrypterFactory {
|
||||
SecretKey headerKey) {
|
||||
return new StreamDecrypterImpl(in, cipherProvider.get(), 0, headerKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StreamDecrypter createLogStreamDecrypter(InputStream in,
|
||||
SecretKey headerKey) {
|
||||
return createContactExchangeStreamDecrypter(in, headerKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public StreamEncrypter createContactExchangeStreamDecrypter(
|
||||
public StreamEncrypter createContactExchangeStreamEncrypter(
|
||||
OutputStream out, SecretKey headerKey) {
|
||||
AuthenticatedCipher cipher = cipherProvider.get();
|
||||
byte[] streamHeaderNonce = new byte[STREAM_HEADER_NONCE_LENGTH];
|
||||
@@ -60,4 +60,10 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
|
||||
return new StreamEncrypterImpl(out, cipher, 0, null, streamHeaderNonce,
|
||||
headerKey, frameKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StreamEncrypter createLogStreamEncrypter(OutputStream out,
|
||||
SecretKey headerKey) {
|
||||
return createContactExchangeStreamEncrypter(out, headerKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.briarproject.bramble.sync;
|
||||
|
||||
import org.briarproject.bramble.api.ThrowingRunnable;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.contact.event.ContactRemovedEvent;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.briarproject.bramble.sync;
|
||||
|
||||
import org.briarproject.bramble.api.ThrowingRunnable;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.contact.event.ContactRemovedEvent;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.briarproject.bramble.sync;
|
||||
|
||||
interface ThrowingRunnable<T extends Throwable> {
|
||||
|
||||
void run() throws T;
|
||||
}
|
||||
@@ -24,15 +24,21 @@ class StreamReaderFactoryImpl implements StreamReaderFactory {
|
||||
|
||||
@Override
|
||||
public InputStream createStreamReader(InputStream in, StreamContext ctx) {
|
||||
return new StreamReaderImpl(
|
||||
streamDecrypterFactory.createStreamDecrypter(in, ctx));
|
||||
return new StreamReaderImpl(streamDecrypterFactory
|
||||
.createStreamDecrypter(in, ctx));
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream createContactExchangeStreamReader(InputStream in,
|
||||
SecretKey headerKey) {
|
||||
return new StreamReaderImpl(
|
||||
streamDecrypterFactory.createContactExchangeStreamDecrypter(in,
|
||||
headerKey));
|
||||
return new StreamReaderImpl(streamDecrypterFactory
|
||||
.createContactExchangeStreamDecrypter(in, headerKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream createLogStreamReader(InputStream in,
|
||||
SecretKey headerKey) {
|
||||
return new StreamReaderImpl(streamDecrypterFactory
|
||||
.createLogStreamDecrypter(in, headerKey));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,15 +26,21 @@ class StreamWriterFactoryImpl implements StreamWriterFactory {
|
||||
@Override
|
||||
public StreamWriter createStreamWriter(OutputStream out,
|
||||
StreamContext ctx) {
|
||||
return new StreamWriterImpl(
|
||||
streamEncrypterFactory.createStreamEncrypter(out, ctx));
|
||||
return new StreamWriterImpl(streamEncrypterFactory
|
||||
.createStreamEncrypter(out, ctx));
|
||||
}
|
||||
|
||||
@Override
|
||||
public StreamWriter createContactExchangeStreamWriter(OutputStream out,
|
||||
SecretKey headerKey) {
|
||||
return new StreamWriterImpl(
|
||||
streamEncrypterFactory.createContactExchangeStreamDecrypter(out,
|
||||
headerKey));
|
||||
return new StreamWriterImpl(streamEncrypterFactory
|
||||
.createContactExchangeStreamEncrypter(out, headerKey));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public StreamWriter createLogStreamWriter(OutputStream out,
|
||||
SecretKey headerKey) {
|
||||
return new StreamWriterImpl(streamEncrypterFactory
|
||||
.createLogStreamEncrypter(out, headerKey));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
Bridge obfs4 192.95.36.142:443 CDF2E852BF539B82BD10E27E9115A31734E378C2 cert=qUVQ0srL1JI/vO6V6m/24anYXiJD3QP2HgzUKQtQ7GRqqUvs7P+tG43RtAqdhLOALP7DJQ iat-mode=1
|
||||
Bridge obfs4 38.229.1.78:80 C8CBDB2464FC9804A69531437BCF2BE31FDD2EE4 cert=Hmyfd2ev46gGY7NoVxA9ngrPF2zCZtzskRTzoWXbxNkzeVnGFPWmrTtILRyqCTjHR+s9dg iat-mode=1
|
||||
Bridge obfs4 37.218.245.14:38224 D9A82D2F9C2F65A18407B1D2B764F130847F8B5D cert=bjRaMrr1BRiAW8IE9U5z27fQaYgOhX1UCmOpg2pFpoMvo6ZgQMzLsaTzzQNTlm7hNcb+Sg iat-mode=0
|
||||
Bridge obfs4 85.31.186.98:443 011F2599C0E9B27EE74B353155E244813763C3E5 cert=ayq0XzCwhpdysn5o0EyDUbmSOx3X/oTEbzDMvczHOdBJKlvIdHHLJGkZARtT4dcBFArPPg iat-mode=0
|
||||
Bridge obfs4 144.217.20.138:80 FB70B257C162BF1038CA669D568D76F5B7F0BABB cert=vYIV5MgrghGQvZPIi1tJwnzorMgqgmlKaB77Y3Z9Q/v94wZBOAXkW+fdx4aSxLVnKO+xNw iat-mode=0
|
||||
Bridge obfs4 193.11.166.194:27015 2D82C2E354D531A68469ADF7F878FA6060C6BACA cert=4TLQPJrTSaDffMK7Nbao6LC7G9OW/NHkUwIdjLSS3KYf0Nv4/nQiiI8dY2TcsQx01NniOg iat-mode=0
|
||||
Bridge obfs4 193.11.166.194:27020 86AC7B8D430DAC4117E9F42C9EAED18133863AAF cert=0LDeJH4JzMDtkJJrFphJCiPqKx7loozKN7VNfuukMGfHO0Z8OGdzHVkhVAOfo1mUdv9cMg iat-mode=0
|
||||
Bridge obfs4 193.11.166.194:27025 1AE2C08904527FEA90C4C4F8C1083EA59FBC6FAF cert=ItvYZzW5tn6v3G4UnQa6Qz04Npro6e81AP70YujmK/KXwDFPTs3aHXcHp4n8Vt6w/bv8cA iat-mode=0
|
||||
Bridge obfs4 209.148.46.65:443 74FAD13168806246602538555B5521A0383A1875 cert=ssH+9rP8dG2NLDN2XuFw63hIO/9MNNinLmxQDpVa+7kTOa9/m+tGWT1SmSYpQ9uTBGa6Hw iat-mode=0
|
||||
Bridge obfs4 45.145.95.6:27015 C5B7CD6946FF10C5B3E89691A7D3F2C122D2117C cert=TD7PbUO0/0k6xYHMPW3vJxICfkMZNdkRrb63Zhl5j9dW3iRGiCx0A7mPhe5T2EDzQ35+Zw iat-mode=0
|
||||
Bridge obfs4 51.222.13.177:80 5EDAC3B810E12B01F6FD8050D2FD3E277B289A08 cert=2uplIpLQ0q9+0qMFrK5pkaYRDOe460LL9WHBvatgkuRr/SL31wBOEupaMMJ6koRE6Ld0ew iat-mode=0
|
||||
Bridge obfs4 78.46.188.239:37356 5A2D2F4158D0453E00C7C176978D3F41D69C45DB cert=3c0SwxpOisbohNxEc4tb875RVW8eOu1opRTVXJhafaKA/PNNtI7ElQIVOVZg1AdL5bxGCw iat-mode=0
|
||||
Bridge obfs4 52.15.78.72:9443 02069A3C5362476936B62BA6F5ACC41ABD573A9B cert=ijYG/OKc7kqu2YzKNFfeXN7/BG2BOgfEP2KyYEiGDQthnHbsOiTWHeIG0WJVW+BckzDgKw iat-mode=0
|
||||
Bridge obfs4 13.58.29.242:9443 0C58939A77DA6B6B29D4B5236A75865659607AE0 cert=OylWIEHb/ezpq1zWxW0sgKRn+9ARH2eOcQOZ8/Gew+4l+oKOhQ2jUX/Y+FSl61JorXZUWA iat-mode=0
|
||||
Bridge obfs4 45.33.37.112:9443 60A609BB4ABE8D46E634AE81ED29ADAB7776B399 cert=t5v19WmNv5Sc2YPNr8RQids365W7MY8zJwQVkOxBjUMFomMWARDzsbYpcWLLcw0J9Gm+BQ iat-mode=0
|
||||
Bridge meek_lite 0.0.2.0:2 97700DFE9F483596DDA6264C4D7DF7641E1E39CE url=https://meek.azureedge.net/ front=ajax.aspnetcdn.com
|
||||
Bridge meek_lite 192.0.2.2:2 97700DFE9F483596DDA6264C4D7DF7641E1E39CE url=https://meek.azureedge.net/ front=ajax.aspnetcdn.com
|
||||
@@ -16,8 +16,8 @@ dependencies {
|
||||
implementation fileTree(dir: 'libs', include: '*.jar')
|
||||
implementation 'net.java.dev.jna:jna:4.5.2'
|
||||
implementation 'net.java.dev.jna:jna-platform:4.5.2'
|
||||
tor 'org.briarproject:tor:0.3.5.12@zip'
|
||||
tor 'org.briarproject:obfs4proxy:0.0.7@zip'
|
||||
tor 'org.briarproject:tor:0.3.5.13-1@zip'
|
||||
tor 'org.briarproject:obfs4proxy:0.0.12-dev-40245c4a@zip'
|
||||
|
||||
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.OsUtils.isLinux;
|
||||
|
||||
@@ -96,8 +97,15 @@ public class UnixTorPluginFactory implements DuplexPluginFactory {
|
||||
String architecture = null;
|
||||
if (isLinux()) {
|
||||
String arch = System.getProperty("os.arch");
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("System's os.arch is " + arch);
|
||||
}
|
||||
if (arch.equals("amd64")) {
|
||||
architecture = "linux-x86_64";
|
||||
} else if (arch.equals("aarch64")) {
|
||||
architecture = "linux-aarch64";
|
||||
} else if (arch.equals("arm")) {
|
||||
architecture = "linux-armhf";
|
||||
}
|
||||
}
|
||||
if (architecture == null) {
|
||||
@@ -105,6 +113,10 @@ public class UnixTorPluginFactory implements DuplexPluginFactory {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("The selected architecture for Tor is " + architecture);
|
||||
}
|
||||
|
||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
TorRendezvousCrypto torRendezvousCrypto = new TorRendezvousCryptoImpl();
|
||||
|
||||
@@ -53,7 +53,7 @@ public class BridgeTest extends BrambleTestCase {
|
||||
return component.getCircumventionProvider().getBridges(false);
|
||||
}
|
||||
|
||||
private final static long TIMEOUT = SECONDS.toMillis(30);
|
||||
private final static long TIMEOUT = SECONDS.toMillis(60);
|
||||
|
||||
private final static Logger LOG = getLogger(BridgeTest.class.getName());
|
||||
|
||||
|
||||
@@ -23,8 +23,8 @@ dependencyVerification {
|
||||
'org.apache.ant:ant-launcher:1.9.4:ant-launcher-1.9.4.jar:7bccea20b41801ca17bcbc909a78c835d0f443f12d639c77bd6ae3d05861608d',
|
||||
'org.apache.ant:ant:1.9.4:ant-1.9.4.jar:649ae0730251de07b8913f49286d46bba7b92d47c5f332610aa426c4f02161d8',
|
||||
'org.beanshell:bsh:1.3.0:bsh-1.3.0.jar:9b04edc75d19db54f1b4e8b5355e9364384c6cf71eb0a1b9724c159d779879f8',
|
||||
'org.briarproject:obfs4proxy:0.0.7:obfs4proxy-0.0.7.zip:5b2f693262ce43a7e130f7cc7d5d1617925330640a2eb6d71085e95df8ee0642',
|
||||
'org.briarproject:tor:0.3.5.12:tor-0.3.5.12.zip:2f542c4befd216f2226bf7c76e3b8b2d99af6f146a8cb28bf727f42014587006',
|
||||
'org.briarproject:obfs4proxy:0.0.12-dev-40245c4a:obfs4proxy-0.0.12-dev-40245c4a.zip:172029e7058b3a83ac93ac4991a44bf76e16ce8d46f558f5836d57da3cb3a766',
|
||||
'org.briarproject:tor:0.3.5.13-1:tor-0.3.5.13-1.zip:ef35c16bf8dc1f4c75ed71d9f55e4514f383d124ec96b859aca647c990927c99',
|
||||
'org.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.mojo:animal-sniffer-annotations:1.17:animal-sniffer-annotations-1.17.jar:92654f493ecfec52082e76354f0ebf87648dc3d5cec2e3c3cdb947c016747a53',
|
||||
|
||||
1
briar-android/artwork/notification_signout.svg
Normal file
1
briar-android/artwork/notification_signout.svg
Normal file
@@ -0,0 +1 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="24" height="24" viewBox="0 0 24 24"><path d="M16,17V14H9V10H16V7L21,12L16,17M14,2A2,2 0 0,1 16,4V6H14V4H5V20H14V18H16V20A2,2 0 0,1 14,22H5A2,2 0 0,1 3,20V4A2,2 0 0,1 5,2H14Z" /></svg>
|
||||
|
After Width: | Height: | Size: 423 B |
@@ -19,12 +19,18 @@ android {
|
||||
compileSdkVersion 30
|
||||
buildToolsVersion '30.0.2'
|
||||
|
||||
packagingOptions {
|
||||
doNotStrip '**/*.so'
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 16
|
||||
targetSdkVersion 29
|
||||
versionCode 10213
|
||||
versionName "1.2.13"
|
||||
versionCode 10218
|
||||
versionName "1.2.18"
|
||||
applicationId "org.briarproject.briar.android"
|
||||
|
||||
vectorDrawables.useSupportLibrary = true
|
||||
buildConfigField "String", "GitHash",
|
||||
"\"${getStdout(['git', 'rev-parse', '--short=7', 'HEAD'], 'No commit hash')}\""
|
||||
def now = (long) (System.currentTimeMillis() / 1000)
|
||||
@@ -93,6 +99,7 @@ dependencies {
|
||||
implementation project(path: ':bramble-core', configuration: 'default')
|
||||
implementation project(':bramble-android')
|
||||
|
||||
implementation 'androidx.fragment:fragment:1.3.0'
|
||||
implementation 'androidx.preference:preference:1.1.1'
|
||||
implementation 'androidx.exifinterface:exifinterface:1.3.1'
|
||||
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
|
||||
@@ -134,6 +141,7 @@ dependencies {
|
||||
testImplementation "org.jmock:jmock:$jmockVersion"
|
||||
testImplementation "org.jmock:jmock-junit4:$jmockVersion"
|
||||
testImplementation "org.jmock:jmock-legacy:$jmockVersion"
|
||||
testAnnotationProcessor "com.google.dagger:dagger-compiler:2.24"
|
||||
|
||||
androidTestImplementation project(path: ':bramble-api', configuration: 'testOutput')
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
|
||||
|
||||
@@ -27,7 +27,10 @@
|
||||
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
<uses-permission
|
||||
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||
android:maxSdkVersion="18"
|
||||
tools:ignore="ScopedStorage" />
|
||||
|
||||
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<uses-permission-sdk-23 android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||
@@ -38,6 +41,7 @@
|
||||
android:name="org.briarproject.briar.android.BriarApplicationImpl"
|
||||
android:allowBackup="false"
|
||||
android:banner="@mipmap/tv_banner"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
android:icon="@mipmap/ic_launcher_round"
|
||||
android:label="@string/app_name"
|
||||
android:logo="@mipmap/ic_launcher_round"
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.briarproject.briar.BriarCoreModule;
|
||||
import org.briarproject.briar.android.attachment.AttachmentModule;
|
||||
import org.briarproject.briar.android.attachment.media.MediaModule;
|
||||
import org.briarproject.briar.android.conversation.glide.BriarModelLoader;
|
||||
import org.briarproject.briar.android.logging.CachingLogHandler;
|
||||
import org.briarproject.briar.android.login.SignInReminderReceiver;
|
||||
import org.briarproject.briar.android.view.EmojiTextInputView;
|
||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||
@@ -179,6 +180,10 @@ public interface AndroidComponent
|
||||
|
||||
AndroidWakeLockManager wakeLockManager();
|
||||
|
||||
CachingLogHandler logHandler();
|
||||
|
||||
Thread.UncaughtExceptionHandler exceptionHandler();
|
||||
|
||||
void inject(SignInReminderReceiver briarService);
|
||||
|
||||
void inject(BriarService briarService);
|
||||
|
||||
@@ -255,7 +255,7 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
|
||||
R.string.ongoing_notification_title;
|
||||
int text = locked ? R.string.lock_tap_to_unlock :
|
||||
R.string.ongoing_notification_text;
|
||||
int icon = locked ? R.drawable.startup_lock :
|
||||
int icon = locked ? R.drawable.notification_lock :
|
||||
R.drawable.notification_ongoing;
|
||||
// Ongoing foreground notification that shows BriarService is running
|
||||
NotificationCompat.Builder b =
|
||||
@@ -624,7 +624,7 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
|
||||
|
||||
NotificationCompat.Builder b =
|
||||
new NotificationCompat.Builder(appContext, REMINDER_CHANNEL_ID);
|
||||
b.setSmallIcon(R.drawable.ic_signout);
|
||||
b.setSmallIcon(R.drawable.notification_signout);
|
||||
b.setColor(getColor(appContext, R.color.briar_primary));
|
||||
b.setContentTitle(
|
||||
appContext.getText(R.string.reminder_notification_title));
|
||||
|
||||
@@ -30,15 +30,18 @@ import org.briarproject.bramble.util.StringUtils;
|
||||
import org.briarproject.briar.android.account.DozeHelperModule;
|
||||
import org.briarproject.briar.android.account.LockManagerImpl;
|
||||
import org.briarproject.briar.android.account.SetupModule;
|
||||
import org.briarproject.briar.android.blog.BlogModule;
|
||||
import org.briarproject.briar.android.contact.ContactListModule;
|
||||
import org.briarproject.briar.android.forum.ForumModule;
|
||||
import org.briarproject.briar.android.introduction.IntroductionModule;
|
||||
import org.briarproject.briar.android.keyagreement.ContactExchangeModule;
|
||||
import org.briarproject.briar.android.logging.LoggingModule;
|
||||
import org.briarproject.briar.android.login.LoginModule;
|
||||
import org.briarproject.briar.android.navdrawer.NavDrawerModule;
|
||||
import org.briarproject.briar.android.privategroup.conversation.GroupConversationModule;
|
||||
import org.briarproject.briar.android.settings.SettingsModule;
|
||||
import org.briarproject.briar.android.privategroup.list.GroupListModule;
|
||||
import org.briarproject.briar.android.reporting.DevReportModule;
|
||||
import org.briarproject.briar.android.settings.SettingsModule;
|
||||
import org.briarproject.briar.android.sharing.SharingModule;
|
||||
import org.briarproject.briar.android.test.TestAvatarCreatorImpl;
|
||||
import org.briarproject.briar.android.viewmodel.ViewModelModule;
|
||||
@@ -74,13 +77,16 @@ import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
||||
SetupModule.class,
|
||||
DozeHelperModule.class,
|
||||
ContactExchangeModule.class,
|
||||
LoggingModule.class,
|
||||
LoginModule.class,
|
||||
NavDrawerModule.class,
|
||||
ViewModelModule.class,
|
||||
SettingsModule.class,
|
||||
DevReportModule.class,
|
||||
ContactListModule.class,
|
||||
IntroductionModule.class,
|
||||
// below need to be within same scope as ViewModelProvider.Factory
|
||||
BlogModule.class,
|
||||
ForumModule.class,
|
||||
GroupListModule.class,
|
||||
GroupConversationModule.class,
|
||||
@@ -192,6 +198,11 @@ public class AppModule {
|
||||
public File getReportDir() {
|
||||
return AndroidUtils.getReportDir(app.getApplicationContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getLogcatFile() {
|
||||
return AndroidUtils.getLogcatFile(app.getApplicationContext());
|
||||
}
|
||||
};
|
||||
return devConfig;
|
||||
}
|
||||
|
||||
@@ -6,9 +6,6 @@ import android.content.SharedPreferences;
|
||||
import org.briarproject.bramble.BrambleApplication;
|
||||
import org.briarproject.briar.android.navdrawer.NavDrawerActivity;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
/**
|
||||
* This exists so that the Application object will not necessarily be cast
|
||||
* directly to the Briar application object.
|
||||
@@ -17,8 +14,6 @@ public interface BriarApplication extends BrambleApplication {
|
||||
|
||||
Class<? extends Activity> ENTRY_ACTIVITY = NavDrawerActivity.class;
|
||||
|
||||
Collection<LogRecord> getRecentLogRecords();
|
||||
|
||||
AndroidComponent getApplicationComponent();
|
||||
|
||||
SharedPreferences getDefaultSharedPreferences();
|
||||
|
||||
@@ -20,12 +20,10 @@ import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.briar.BriarCoreEagerSingletons;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.logging.CachingLogHandler;
|
||||
import org.briarproject.briar.android.reporting.BriarExceptionHandler;
|
||||
import org.briarproject.briar.android.util.UiUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.lang.Thread.UncaughtExceptionHandler;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
@@ -42,22 +40,18 @@ public class BriarApplicationImpl extends Application
|
||||
private static final Logger LOG =
|
||||
getLogger(BriarApplicationImpl.class.getName());
|
||||
|
||||
private final CachingLogHandler logHandler = new CachingLogHandler();
|
||||
private final BriarExceptionHandler exceptionHandler =
|
||||
new BriarExceptionHandler(this);
|
||||
|
||||
private AndroidComponent applicationComponent;
|
||||
private volatile SharedPreferences prefs;
|
||||
|
||||
@Override
|
||||
protected void attachBaseContext(Context base) {
|
||||
Thread.setDefaultUncaughtExceptionHandler(exceptionHandler);
|
||||
if (prefs == null)
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(base);
|
||||
// Loading the language needs to be done here.
|
||||
Localizer.initialize(prefs);
|
||||
super.attachBaseContext(
|
||||
Localizer.getInstance().setLocale(base));
|
||||
Localizer.getInstance().setLocale(this);
|
||||
setTheme(base, prefs);
|
||||
}
|
||||
|
||||
@@ -67,6 +61,11 @@ public class BriarApplicationImpl extends Application
|
||||
|
||||
if (IS_DEBUG_BUILD) enableStrictMode();
|
||||
|
||||
applicationComponent = createApplicationComponent();
|
||||
UncaughtExceptionHandler exceptionHandler =
|
||||
applicationComponent.exceptionHandler();
|
||||
Thread.setDefaultUncaughtExceptionHandler(exceptionHandler);
|
||||
|
||||
Logger rootLogger = getLogger("");
|
||||
Handler[] handlers = rootLogger.getHandlers();
|
||||
// Disable the Android logger for release builds
|
||||
@@ -78,12 +77,12 @@ public class BriarApplicationImpl extends Application
|
||||
// Restore the default handlers after the level raising handler
|
||||
for (Handler handler : handlers) rootLogger.addHandler(handler);
|
||||
}
|
||||
CachingLogHandler logHandler = applicationComponent.logHandler();
|
||||
rootLogger.addHandler(logHandler);
|
||||
rootLogger.setLevel(IS_DEBUG_BUILD ? FINE : INFO);
|
||||
|
||||
LOG.info("Created");
|
||||
|
||||
applicationComponent = createApplicationComponent();
|
||||
EmojiManager.install(new GoogleEmojiProvider());
|
||||
}
|
||||
|
||||
@@ -136,11 +135,6 @@ public class BriarApplicationImpl extends Application
|
||||
return applicationComponent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<LogRecord> getRecentLogRecords() {
|
||||
return logHandler.getRecentLogRecords();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AndroidComponent getApplicationComponent() {
|
||||
return applicationComponent;
|
||||
|
||||
@@ -37,7 +37,7 @@ import javax.inject.Inject;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
|
||||
import static android.app.NotificationManager.IMPORTANCE_NONE;
|
||||
import static android.app.NotificationManager.IMPORTANCE_LOW;
|
||||
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
|
||||
import static android.content.Intent.ACTION_SHUTDOWN;
|
||||
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
|
||||
@@ -46,6 +46,7 @@ import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
|
||||
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
|
||||
import static android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static android.os.Process.myPid;
|
||||
import static androidx.core.app.NotificationCompat.VISIBILITY_SECRET;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
@@ -56,8 +57,10 @@ import static org.briarproject.briar.android.BriarApplication.ENTRY_ACTIVITY;
|
||||
import static org.briarproject.briar.api.android.AndroidNotificationManager.FAILURE_CHANNEL_ID;
|
||||
import static org.briarproject.briar.api.android.AndroidNotificationManager.FAILURE_NOTIFICATION_ID;
|
||||
import static org.briarproject.briar.api.android.AndroidNotificationManager.ONGOING_CHANNEL_ID;
|
||||
import static org.briarproject.briar.api.android.AndroidNotificationManager.ONGOING_CHANNEL_OLD_ID;
|
||||
import static org.briarproject.briar.api.android.AndroidNotificationManager.ONGOING_NOTIFICATION_ID;
|
||||
import static org.briarproject.briar.api.android.LockManager.ACTION_LOCK;
|
||||
import static org.briarproject.briar.api.android.LockManager.EXTRA_PID;
|
||||
|
||||
public class BriarService extends Service {
|
||||
|
||||
@@ -120,11 +123,17 @@ public class BriarService extends Service {
|
||||
if (SDK_INT >= 26) {
|
||||
NotificationManager nm = (NotificationManager)
|
||||
requireNonNull(getSystemService(NOTIFICATION_SERVICE));
|
||||
// Delete the old notification channel, which had
|
||||
// IMPORTANCE_NONE and showed a badge
|
||||
nm.deleteNotificationChannel(ONGOING_CHANNEL_OLD_ID);
|
||||
// Use IMPORTANCE_LOW so the system doesn't show its own
|
||||
// notification on API 26-27
|
||||
NotificationChannel ongoingChannel = new NotificationChannel(
|
||||
ONGOING_CHANNEL_ID,
|
||||
getString(R.string.ongoing_notification_title),
|
||||
IMPORTANCE_NONE);
|
||||
IMPORTANCE_LOW);
|
||||
ongoingChannel.setLockscreenVisibility(VISIBILITY_SECRET);
|
||||
ongoingChannel.setShowBadge(false);
|
||||
nm.createNotificationChannel(ongoingChannel);
|
||||
NotificationChannel failureChannel = new NotificationChannel(
|
||||
FAILURE_CHANNEL_ID,
|
||||
@@ -170,6 +179,7 @@ public class BriarService extends Service {
|
||||
@Override
|
||||
protected void attachBaseContext(Context base) {
|
||||
super.attachBaseContext(Localizer.getInstance().setLocale(base));
|
||||
Localizer.getInstance().setLocale(this);
|
||||
}
|
||||
|
||||
private void showStartupFailureNotification(StartResult result) {
|
||||
@@ -202,7 +212,12 @@ public class BriarService extends Service {
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
if (ACTION_LOCK.equals(intent.getAction())) {
|
||||
lockManager.setLocked(true);
|
||||
int pid = intent.getIntExtra(EXTRA_PID, -1);
|
||||
if (pid == myPid()) lockManager.setLocked(true);
|
||||
else if (LOG.isLoggable(WARNING)) {
|
||||
LOG.warning("Tried to lock process " + pid + " but this is " +
|
||||
myPid());
|
||||
}
|
||||
}
|
||||
return START_NOT_STICKY; // Don't restart automatically if killed
|
||||
}
|
||||
|
||||
@@ -68,7 +68,21 @@ public class Localizer {
|
||||
return new Locale(tag);
|
||||
}
|
||||
|
||||
// Returns the localized version of context
|
||||
/*
|
||||
* Apply localization to the specified context.
|
||||
*
|
||||
* It updates the configuration of the context's resources object but can
|
||||
* also return a new context derived from the context parameter. Hence
|
||||
* make sure to work with the return value of this method instead of
|
||||
* the context you passed as a parameter.
|
||||
*
|
||||
* This method also has side-effects as it calls Locale#setDefault().
|
||||
*
|
||||
* When using this in attachBaseContext() of Application, Service or
|
||||
* Activity subclasses, it is important to not only apply this method to the
|
||||
* base Context parameter received in that method, but also apply it on the
|
||||
* class itself which also extends Context.
|
||||
*/
|
||||
public Context setLocale(Context context) {
|
||||
Resources res = context.getResources();
|
||||
Configuration conf = res.getConfiguration();
|
||||
@@ -82,7 +96,7 @@ public class Localizer {
|
||||
Locale.setDefault(locale);
|
||||
if (SDK_INT >= 17) {
|
||||
conf.setLocale(locale);
|
||||
context.createConfigurationContext(conf);
|
||||
context = context.createConfigurationContext(conf);
|
||||
} else
|
||||
conf.locale = locale;
|
||||
//noinspection deprecation
|
||||
|
||||
@@ -33,7 +33,6 @@ public class DozeFragment extends SetupFragment
|
||||
private DozeView dozeView;
|
||||
private HuaweiView huaweiView;
|
||||
private Button next;
|
||||
private ProgressBar progressBar;
|
||||
private boolean secondAttempt = false;
|
||||
|
||||
public static DozeFragment newInstance() {
|
||||
@@ -58,11 +57,19 @@ public class DozeFragment extends SetupFragment
|
||||
huaweiView = v.findViewById(R.id.huaweiView);
|
||||
huaweiView.setOnCheckedChangedListener(this);
|
||||
next = v.findViewById(R.id.next);
|
||||
progressBar = v.findViewById(R.id.progress);
|
||||
ProgressBar progressBar = v.findViewById(R.id.progress);
|
||||
|
||||
dozeView.setOnButtonClickListener(this::askForDozeWhitelisting);
|
||||
next.setOnClickListener(this);
|
||||
|
||||
viewModel.getIsCreatingAccount()
|
||||
.observe(getViewLifecycleOwner(), isCreatingAccount -> {
|
||||
if (isCreatingAccount) {
|
||||
next.setVisibility(INVISIBLE);
|
||||
progressBar.setVisibility(VISIBLE);
|
||||
}
|
||||
});
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -104,15 +111,6 @@ public class DozeFragment extends SetupFragment
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
setNextClicked();
|
||||
viewModel.dozeExceptionConfirmed();
|
||||
}
|
||||
|
||||
@Override
|
||||
void setNextClicked() {
|
||||
super.setNextClicked();
|
||||
|
||||
next.setVisibility(INVISIBLE);
|
||||
progressBar.setVisibility(VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,8 +32,10 @@ import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import static android.app.AlarmManager.ELAPSED_REALTIME;
|
||||
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
|
||||
import static android.app.PendingIntent.getService;
|
||||
import static android.content.Context.ALARM_SERVICE;
|
||||
import static android.os.Process.myPid;
|
||||
import static android.os.SystemClock.elapsedRealtime;
|
||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
@@ -75,23 +77,25 @@ public class LockManagerImpl implements LockManager, Service, EventListener {
|
||||
LockManagerImpl(Application app, SettingsManager settingsManager,
|
||||
AndroidNotificationManager notificationManager,
|
||||
@DatabaseExecutor Executor dbExecutor) {
|
||||
this.appContext = app.getApplicationContext();
|
||||
appContext = app.getApplicationContext();
|
||||
this.settingsManager = settingsManager;
|
||||
this.notificationManager = notificationManager;
|
||||
this.dbExecutor = dbExecutor;
|
||||
this.alarmManager =
|
||||
alarmManager =
|
||||
(AlarmManager) appContext.getSystemService(ALARM_SERVICE);
|
||||
Intent i =
|
||||
new Intent(ACTION_LOCK, null, appContext, BriarService.class);
|
||||
this.lockIntent = getService(appContext, 0, i, 0);
|
||||
this.timeoutNever = Integer.valueOf(
|
||||
i.putExtra(EXTRA_PID, myPid());
|
||||
// When not using FLAG_UPDATE_CURRENT, the intent might have no extras
|
||||
lockIntent = getService(appContext, 0, i, FLAG_UPDATE_CURRENT);
|
||||
timeoutNever = Integer.parseInt(
|
||||
appContext.getString(R.string.pref_lock_timeout_value_never));
|
||||
this.timeoutDefault = Integer.valueOf(
|
||||
timeoutDefault = Integer.parseInt(
|
||||
appContext.getString(R.string.pref_lock_timeout_value_default));
|
||||
this.timeoutMinutes = timeoutNever;
|
||||
timeoutMinutes = timeoutNever;
|
||||
|
||||
// setting this in the constructor makes #getValue() @NonNull
|
||||
this.lockable.setValue(false);
|
||||
lockable.setValue(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -148,7 +152,7 @@ public class LockManagerImpl implements LockManager, Service, EventListener {
|
||||
boolean oldValue = lockable.getValue();
|
||||
boolean newValue = hasScreenLock(appContext) && lockableSetting;
|
||||
if (oldValue != newValue) {
|
||||
this.lockable.setValue(newValue);
|
||||
lockable.setValue(newValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,6 @@ public class SetPasswordFragment extends SetupFragment {
|
||||
private TextInputEditText passwordConfirmation;
|
||||
private StrengthMeter strengthMeter;
|
||||
private Button nextButton;
|
||||
private ProgressBar progressBar;
|
||||
|
||||
public static SetPasswordFragment newInstance() {
|
||||
return new SetPasswordFragment();
|
||||
@@ -64,7 +63,7 @@ public class SetPasswordFragment extends SetupFragment {
|
||||
v.findViewById(R.id.password_confirm_wrapper);
|
||||
passwordConfirmation = v.findViewById(R.id.password_confirm);
|
||||
nextButton = v.findViewById(R.id.next);
|
||||
progressBar = v.findViewById(R.id.progress);
|
||||
ProgressBar progressBar = v.findViewById(R.id.progress);
|
||||
|
||||
passwordEntry.addTextChangedListener(this);
|
||||
passwordConfirmation.addTextChangedListener(this);
|
||||
@@ -75,6 +74,17 @@ public class SetPasswordFragment extends SetupFragment {
|
||||
passwordConfirmation.setImeOptions(IME_ACTION_DONE);
|
||||
}
|
||||
|
||||
viewModel.getIsCreatingAccount()
|
||||
.observe(getViewLifecycleOwner(), isCreatingAccount -> {
|
||||
if (isCreatingAccount) {
|
||||
nextButton.setVisibility(INVISIBLE);
|
||||
progressBar.setVisibility(VISIBLE);
|
||||
// this also avoids the keyboard popping up
|
||||
passwordEntry.setFocusable(false);
|
||||
passwordConfirmation.setFocusable(false);
|
||||
}
|
||||
});
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -116,20 +126,6 @@ public class SetPasswordFragment extends SetupFragment {
|
||||
IBinder token = passwordEntry.getWindowToken();
|
||||
Object o = getContext().getSystemService(INPUT_METHOD_SERVICE);
|
||||
((InputMethodManager) o).hideSoftInputFromWindow(token, 0);
|
||||
|
||||
setNextClicked();
|
||||
viewModel.setPassword(passwordEntry.getText().toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
void setNextClicked() {
|
||||
super.setNextClicked();
|
||||
|
||||
passwordEntry.setFocusable(false);
|
||||
passwordConfirmation.setFocusable(false);
|
||||
if (!viewModel.needToShowDozeFragment()) {
|
||||
nextButton.setVisibility(INVISIBLE);
|
||||
progressBar.setVisibility(VISIBLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import android.view.KeyEvent;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.TextView;
|
||||
import android.widget.TextView.OnEditorActionListener;
|
||||
@@ -19,8 +18,6 @@ import org.briarproject.briar.android.fragment.BaseFragment;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.CallSuper;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
@@ -35,7 +32,6 @@ abstract class SetupFragment extends BaseFragment implements TextWatcher,
|
||||
OnEditorActionListener, OnClickListener {
|
||||
|
||||
private final static String STATE_KEY_CLICKED = "setupFragmentClicked";
|
||||
private boolean clicked = false;
|
||||
|
||||
@Inject
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
@@ -48,27 +44,6 @@ abstract class SetupFragment extends BaseFragment implements TextWatcher,
|
||||
.get(SetupViewModel.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
||||
if (savedInstanceState != null) {
|
||||
clicked = savedInstanceState.getBoolean(STATE_KEY_CLICKED);
|
||||
}
|
||||
if (clicked) {
|
||||
setNextClicked();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putBoolean(STATE_KEY_CLICKED, clicked);
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
void setNextClicked() {
|
||||
this.clicked = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.help_action, menu);
|
||||
@@ -114,5 +89,4 @@ abstract class SetupFragment extends BaseFragment implements TextWatcher,
|
||||
public void afterTextChanged(Editable editable) {
|
||||
// noop
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.lifecycle.AndroidViewModel;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.briar.android.account.SetupViewModel.State.AUTHOR_NAME;
|
||||
@@ -36,6 +38,8 @@ class SetupViewModel extends AndroidViewModel {
|
||||
@Nullable
|
||||
private String authorName, password;
|
||||
private final MutableLiveEvent<State> state = new MutableLiveEvent<>();
|
||||
private final MutableLiveData<Boolean> isCreatingAccount =
|
||||
new MutableLiveData<>(false);
|
||||
|
||||
private final AccountManager accountManager;
|
||||
private final Executor ioExecutor;
|
||||
@@ -67,6 +71,10 @@ class SetupViewModel extends AndroidViewModel {
|
||||
return state;
|
||||
}
|
||||
|
||||
LiveData<Boolean> getIsCreatingAccount() {
|
||||
return isCreatingAccount;
|
||||
}
|
||||
|
||||
void setAuthorName(String authorName) {
|
||||
this.authorName = authorName;
|
||||
state.setEvent(SET_PASSWORD);
|
||||
@@ -97,6 +105,7 @@ class SetupViewModel extends AndroidViewModel {
|
||||
private void createAccount() {
|
||||
if (authorName == null) throw new IllegalStateException();
|
||||
if (password == null) throw new IllegalStateException();
|
||||
isCreatingAccount.setValue(true);
|
||||
ioExecutor.execute(() -> {
|
||||
if (accountManager.createAccount(authorName, password)) {
|
||||
LOG.info("Created account");
|
||||
|
||||
@@ -77,7 +77,7 @@ public class UnlockActivity extends BaseActivity {
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode,
|
||||
Intent data) {
|
||||
@Nullable Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == REQUEST_KEYGUARD_UNLOCK) {
|
||||
if (resultCode == RESULT_OK) unlock();
|
||||
|
||||
@@ -11,10 +11,8 @@ import org.briarproject.briar.android.account.SetupActivity;
|
||||
import org.briarproject.briar.android.account.UnlockActivity;
|
||||
import org.briarproject.briar.android.blog.BlogActivity;
|
||||
import org.briarproject.briar.android.blog.BlogFragment;
|
||||
import org.briarproject.briar.android.blog.BlogModule;
|
||||
import org.briarproject.briar.android.blog.BlogPostFragment;
|
||||
import org.briarproject.briar.android.blog.FeedFragment;
|
||||
import org.briarproject.briar.android.blog.FeedPostFragment;
|
||||
import org.briarproject.briar.android.blog.ReblogActivity;
|
||||
import org.briarproject.briar.android.blog.ReblogFragment;
|
||||
import org.briarproject.briar.android.blog.RssFeedImportActivity;
|
||||
@@ -85,7 +83,6 @@ import dagger.Component;
|
||||
@ActivityScope
|
||||
@Component(modules = {
|
||||
ActivityModule.class,
|
||||
BlogModule.class,
|
||||
CreateGroupModule.class,
|
||||
GroupInvitationModule.class,
|
||||
GroupMemberModule.class,
|
||||
@@ -152,8 +149,6 @@ public interface ActivityComponent {
|
||||
|
||||
void inject(BlogPostFragment fragment);
|
||||
|
||||
void inject(FeedPostFragment fragment);
|
||||
|
||||
void inject(ReblogFragment fragment);
|
||||
|
||||
void inject(ReblogActivity activity);
|
||||
|
||||
@@ -109,6 +109,7 @@ public abstract class BaseActivity extends AppCompatActivity
|
||||
protected void attachBaseContext(Context base) {
|
||||
super.attachBaseContext(
|
||||
Localizer.getInstance().setLocale(base));
|
||||
Localizer.getInstance().setLocale(this);
|
||||
}
|
||||
|
||||
public ActivityComponent getActivityComponent() {
|
||||
|
||||
@@ -28,9 +28,12 @@ import javax.inject.Inject;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
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.AndroidUtils.getSupportedImageContentTypes;
|
||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.briar.android.attachment.AttachmentItem.State.AVAILABLE;
|
||||
@@ -86,7 +89,17 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
|
||||
List<AttachmentHeader> headers = messageHeader.getAttachmentHeaders();
|
||||
List<LiveData<AttachmentItem>> items = new ArrayList<>(headers.size());
|
||||
boolean needsSize = headers.size() == 1;
|
||||
List<String> supported = asList(getSupportedImageContentTypes());
|
||||
for (AttachmentHeader h : headers) {
|
||||
// Fail early if we don't support the content type
|
||||
if (!supported.contains(h.getContentType())) {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Unsupported content type " + h.getContentType());
|
||||
}
|
||||
AttachmentItem item = new AttachmentItem(h, "", ERROR);
|
||||
items.add(new MutableLiveData<>(item));
|
||||
continue;
|
||||
}
|
||||
// try cache for existing item live data
|
||||
MutableLiveData<AttachmentItem> liveData =
|
||||
itemsWithSize.get(h.getMessageId());
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
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.android.controller.handler.ExceptionHandler;
|
||||
import org.briarproject.briar.android.controller.handler.ResultExceptionHandler;
|
||||
import org.briarproject.briar.api.blog.BlogPostHeader;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import androidx.annotation.UiThread;
|
||||
|
||||
@NotNullByDefault
|
||||
interface BaseController {
|
||||
|
||||
@UiThread
|
||||
void onStart();
|
||||
|
||||
@UiThread
|
||||
void onStop();
|
||||
|
||||
void loadBlogPosts(GroupId g,
|
||||
ResultExceptionHandler<Collection<BlogPostItem>, DbException> handler);
|
||||
|
||||
void loadBlogPost(BlogPostHeader header,
|
||||
ResultExceptionHandler<BlogPostItem, DbException> handler);
|
||||
|
||||
void loadBlogPost(GroupId g, MessageId m,
|
||||
ResultExceptionHandler<BlogPostItem, DbException> handler);
|
||||
|
||||
void repeatPost(BlogPostItem item, @Nullable String comment,
|
||||
ExceptionHandler<DbException> handler);
|
||||
|
||||
@NotNullByDefault
|
||||
interface BlogListener {
|
||||
|
||||
@UiThread
|
||||
void onBlogPostAdded(BlogPostHeader header, boolean local);
|
||||
|
||||
@UiThread
|
||||
void onBlogRemoved();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,209 +0,0 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.android.controller.DbControllerImpl;
|
||||
import org.briarproject.briar.android.controller.handler.ExceptionHandler;
|
||||
import org.briarproject.briar.android.controller.handler.ResultExceptionHandler;
|
||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||
import org.briarproject.briar.api.blog.Blog;
|
||||
import org.briarproject.briar.api.blog.BlogCommentHeader;
|
||||
import org.briarproject.briar.api.blog.BlogManager;
|
||||
import org.briarproject.briar.api.blog.BlogPostHeader;
|
||||
import org.briarproject.briar.util.HtmlUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import androidx.annotation.CallSuper;
|
||||
|
||||
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.util.HtmlUtils.ARTICLE;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
abstract class BaseControllerImpl extends DbControllerImpl
|
||||
implements BaseController, EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(BaseControllerImpl.class.getName());
|
||||
|
||||
protected final EventBus eventBus;
|
||||
protected final AndroidNotificationManager notificationManager;
|
||||
protected final IdentityManager identityManager;
|
||||
protected final BlogManager blogManager;
|
||||
|
||||
private final Map<MessageId, String> textCache = new ConcurrentHashMap<>();
|
||||
private final Map<MessageId, BlogPostHeader> headerCache =
|
||||
new ConcurrentHashMap<>();
|
||||
|
||||
BaseControllerImpl(@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager, EventBus eventBus,
|
||||
AndroidNotificationManager notificationManager,
|
||||
IdentityManager identityManager, BlogManager blogManager) {
|
||||
super(dbExecutor, lifecycleManager);
|
||||
this.eventBus = eventBus;
|
||||
this.notificationManager = notificationManager;
|
||||
this.identityManager = identityManager;
|
||||
this.blogManager = blogManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
@CallSuper
|
||||
public void onStart() {
|
||||
eventBus.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@CallSuper
|
||||
public void onStop() {
|
||||
eventBus.removeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadBlogPosts(GroupId groupId,
|
||||
ResultExceptionHandler<Collection<BlogPostItem>, DbException> handler) {
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
Collection<BlogPostItem> items = loadItems(groupId);
|
||||
handler.onResult(items);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
handler.onException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Collection<BlogPostItem> loadItems(GroupId groupId) throws DbException {
|
||||
long start = now();
|
||||
Collection<BlogPostHeader> headers =
|
||||
blogManager.getPostHeaders(groupId);
|
||||
logDuration(LOG, "Loading headers", start);
|
||||
Collection<BlogPostItem> items = new ArrayList<>(headers.size());
|
||||
start = now();
|
||||
for (BlogPostHeader h : headers) {
|
||||
headerCache.put(h.getId(), h);
|
||||
BlogPostItem item = getItem(h);
|
||||
items.add(item);
|
||||
}
|
||||
logDuration(LOG, "Loading bodies", start);
|
||||
return items;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadBlogPost(BlogPostHeader header,
|
||||
ResultExceptionHandler<BlogPostItem, DbException> handler) {
|
||||
|
||||
String text = textCache.get(header.getId());
|
||||
if (text != null) {
|
||||
LOG.info("Loaded text from cache");
|
||||
handler.onResult(new BlogPostItem(header, text));
|
||||
return;
|
||||
}
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
long start = now();
|
||||
BlogPostItem item = getItem(header);
|
||||
logDuration(LOG, "Loading text", start);
|
||||
handler.onResult(item);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
handler.onException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadBlogPost(GroupId g, MessageId m,
|
||||
ResultExceptionHandler<BlogPostItem, DbException> handler) {
|
||||
|
||||
BlogPostHeader header = headerCache.get(m);
|
||||
if (header != null) {
|
||||
LOG.info("Loaded header from cache");
|
||||
loadBlogPost(header, handler);
|
||||
return;
|
||||
}
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
long start = now();
|
||||
BlogPostHeader header1 = getPostHeader(g, m);
|
||||
BlogPostItem item = getItem(header1);
|
||||
logDuration(LOG, "Loading post", start);
|
||||
handler.onResult(item);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
handler.onException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void repeatPost(BlogPostItem item, @Nullable String comment,
|
||||
ExceptionHandler<DbException> handler) {
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
LocalAuthor a = identityManager.getLocalAuthor();
|
||||
Blog b = blogManager.getPersonalBlog(a);
|
||||
BlogPostHeader h = item.getHeader();
|
||||
blogManager.addLocalComment(a, b.getId(), comment, h);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
handler.onException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private BlogPostHeader getPostHeader(GroupId g, MessageId m)
|
||||
throws DbException {
|
||||
BlogPostHeader header = headerCache.get(m);
|
||||
if (header == null) {
|
||||
header = blogManager.getPostHeader(g, m);
|
||||
headerCache.put(m, header);
|
||||
}
|
||||
return header;
|
||||
}
|
||||
|
||||
@DatabaseExecutor
|
||||
private BlogPostItem getItem(BlogPostHeader h) throws DbException {
|
||||
String text;
|
||||
if (h instanceof BlogCommentHeader) {
|
||||
BlogCommentHeader c = (BlogCommentHeader) h;
|
||||
BlogCommentItem item = new BlogCommentItem(c);
|
||||
text = getPostText(item.getPostHeader().getId());
|
||||
item.setText(text);
|
||||
return item;
|
||||
} else {
|
||||
text = getPostText(h.getId());
|
||||
return new BlogPostItem(h, text);
|
||||
}
|
||||
}
|
||||
|
||||
@DatabaseExecutor
|
||||
private String getPostText(MessageId m) throws DbException {
|
||||
String text = textCache.get(m);
|
||||
if (text == null) {
|
||||
text = HtmlUtils.clean(blogManager.getPostText(m), ARTICLE);
|
||||
textCache.put(m, text);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ProgressBar;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import androidx.annotation.CallSuper;
|
||||
import androidx.annotation.UiThread;
|
||||
|
||||
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
|
||||
import static android.view.View.INVISIBLE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.briar.android.activity.BriarActivity.GROUP_ID;
|
||||
import static org.briarproject.briar.android.util.UiUtils.MIN_DATE_RESOLUTION;
|
||||
|
||||
@UiThread
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
abstract class BasePostFragment extends BaseFragment {
|
||||
|
||||
static final String POST_ID = "briar.POST_ID";
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(BasePostFragment.class.getName());
|
||||
|
||||
private final Handler handler = new Handler(Looper.getMainLooper());
|
||||
|
||||
protected MessageId postId;
|
||||
private ProgressBar progressBar;
|
||||
private BlogPostViewHolder ui;
|
||||
private BlogPostItem post;
|
||||
private Runnable refresher;
|
||||
|
||||
@CallSuper
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater,
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
// retrieve MessageId of blog post from arguments
|
||||
byte[] p = requireArguments().getByteArray(POST_ID);
|
||||
if (p == null) throw new IllegalStateException("No post ID in args");
|
||||
postId = new MessageId(p);
|
||||
|
||||
View view = inflater.inflate(R.layout.fragment_blog_post, container,
|
||||
false);
|
||||
progressBar = view.findViewById(R.id.progressBar);
|
||||
progressBar.setVisibility(VISIBLE);
|
||||
ui = new BlogPostViewHolder(view, true, new OnBlogPostClickListener() {
|
||||
@Override
|
||||
public void onBlogPostClick(BlogPostItem post) {
|
||||
// We're already there
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthorClick(BlogPostItem post) {
|
||||
if (getContext() == null) return;
|
||||
Intent i = new Intent(getContext(), BlogActivity.class);
|
||||
i.putExtra(GROUP_ID, post.getGroupId().getBytes());
|
||||
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
|
||||
getContext().startActivity(i);
|
||||
}
|
||||
}, getFragmentManager());
|
||||
return view;
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
startPeriodicUpdate();
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
stopPeriodicUpdate();
|
||||
}
|
||||
|
||||
@UiThread
|
||||
protected void onBlogPostLoaded(BlogPostItem post) {
|
||||
progressBar.setVisibility(INVISIBLE);
|
||||
this.post = post;
|
||||
ui.bindItem(post);
|
||||
}
|
||||
|
||||
private void startPeriodicUpdate() {
|
||||
refresher = () -> {
|
||||
LOG.info("Updating Content...");
|
||||
ui.updateDate(post.getTimestamp());
|
||||
handler.postDelayed(refresher, MIN_DATE_RESOLUTION);
|
||||
};
|
||||
LOG.info("Adding Handler Callback");
|
||||
handler.postDelayed(refresher, MIN_DATE_RESOLUTION);
|
||||
}
|
||||
|
||||
private void stopPeriodicUpdate() {
|
||||
if (refresher != null) {
|
||||
LOG.info("Removing Handler Callback");
|
||||
handler.removeCallbacks(refresher);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.db.TransactionManager;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||
import org.briarproject.briar.android.viewmodel.DbViewModel;
|
||||
import org.briarproject.briar.android.viewmodel.LiveResult;
|
||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||
import org.briarproject.briar.api.blog.Blog;
|
||||
import org.briarproject.briar.api.blog.BlogCommentHeader;
|
||||
import org.briarproject.briar.api.blog.BlogManager;
|
||||
import org.briarproject.briar.api.blog.BlogPostHeader;
|
||||
import org.briarproject.briar.util.HtmlUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
import static org.briarproject.briar.util.HtmlUtils.ARTICLE;
|
||||
|
||||
@NotNullByDefault
|
||||
abstract class BaseViewModel extends DbViewModel implements EventListener {
|
||||
|
||||
private static final Logger LOG = getLogger(BaseViewModel.class.getName());
|
||||
|
||||
private final EventBus eventBus;
|
||||
protected final IdentityManager identityManager;
|
||||
protected final AndroidNotificationManager notificationManager;
|
||||
protected final BlogManager blogManager;
|
||||
|
||||
protected final MutableLiveData<LiveResult<ListUpdate>> blogPosts =
|
||||
new MutableLiveData<>();
|
||||
|
||||
BaseViewModel(Application application,
|
||||
@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager,
|
||||
TransactionManager db,
|
||||
AndroidExecutor androidExecutor,
|
||||
EventBus eventBus,
|
||||
IdentityManager identityManager,
|
||||
AndroidNotificationManager notificationManager,
|
||||
BlogManager blogManager) {
|
||||
super(application, dbExecutor, lifecycleManager, db, androidExecutor);
|
||||
this.eventBus = eventBus;
|
||||
this.identityManager = identityManager;
|
||||
this.notificationManager = notificationManager;
|
||||
this.blogManager = blogManager;
|
||||
eventBus.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCleared() {
|
||||
super.onCleared();
|
||||
eventBus.removeListener(this);
|
||||
}
|
||||
|
||||
@DatabaseExecutor
|
||||
protected List<BlogPostItem> loadBlogPosts(Transaction txn, GroupId groupId)
|
||||
throws DbException {
|
||||
long start = now();
|
||||
List<BlogPostHeader> headers =
|
||||
blogManager.getPostHeaders(txn, groupId);
|
||||
logDuration(LOG, "Loading headers", start);
|
||||
List<BlogPostItem> items = new ArrayList<>(headers.size());
|
||||
start = now();
|
||||
for (BlogPostHeader h : headers) {
|
||||
BlogPostItem item = getItem(txn, h);
|
||||
items.add(item);
|
||||
}
|
||||
logDuration(LOG, "Loading bodies", start);
|
||||
return items;
|
||||
}
|
||||
|
||||
@DatabaseExecutor
|
||||
protected BlogPostItem getItem(Transaction txn, BlogPostHeader h)
|
||||
throws DbException {
|
||||
String text;
|
||||
if (h instanceof BlogCommentHeader) {
|
||||
BlogCommentHeader c = (BlogCommentHeader) h;
|
||||
BlogCommentItem item = new BlogCommentItem(c);
|
||||
text = getPostText(txn, item.getPostHeader().getId());
|
||||
item.setText(text);
|
||||
return item;
|
||||
} else {
|
||||
text = getPostText(txn, h.getId());
|
||||
return new BlogPostItem(h, text);
|
||||
}
|
||||
}
|
||||
|
||||
@DatabaseExecutor
|
||||
private String getPostText(Transaction txn, MessageId m)
|
||||
throws DbException {
|
||||
return HtmlUtils.clean(blogManager.getPostText(txn, m), ARTICLE);
|
||||
}
|
||||
|
||||
LiveData<LiveResult<BlogPostItem>> loadBlogPost(GroupId g, MessageId m) {
|
||||
MutableLiveData<LiveResult<BlogPostItem>> result =
|
||||
new MutableLiveData<>();
|
||||
runOnDbThread(true, txn -> {
|
||||
long start = now();
|
||||
BlogPostHeader header = blogManager.getPostHeader(txn, g, m);
|
||||
BlogPostItem item = getItem(txn, header);
|
||||
logDuration(LOG, "Loading post", start);
|
||||
result.postValue(new LiveResult<>(item));
|
||||
}, e -> {
|
||||
logException(LOG, WARNING, e);
|
||||
result.postValue(new LiveResult<>(e));
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
protected void onBlogPostAdded(BlogPostHeader header, boolean local) {
|
||||
runOnDbThread(true, txn -> {
|
||||
BlogPostItem item = getItem(txn, header);
|
||||
txn.attach(() -> onBlogPostItemAdded(item, local));
|
||||
}, this::handleException);
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void onBlogPostItemAdded(BlogPostItem item, boolean local) {
|
||||
List<BlogPostItem> items = addListItem(getBlogPostItems(), item);
|
||||
if (items != null) {
|
||||
Collections.sort(items);
|
||||
blogPosts.setValue(new LiveResult<>(new ListUpdate(local, items)));
|
||||
}
|
||||
}
|
||||
|
||||
void repeatPost(BlogPostItem item, @Nullable String comment) {
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
LocalAuthor a = identityManager.getLocalAuthor();
|
||||
Blog b = blogManager.getPersonalBlog(a);
|
||||
BlogPostHeader h = item.getHeader();
|
||||
blogManager.addLocalComment(a, b.getId(), comment, h);
|
||||
} catch (DbException e) {
|
||||
handleException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
LiveData<LiveResult<ListUpdate>> getBlogPosts() {
|
||||
return blogPosts;
|
||||
}
|
||||
|
||||
@UiThread
|
||||
protected List<BlogPostItem> getBlogPostItems() {
|
||||
LiveResult<ListUpdate> value = blogPosts.getValue();
|
||||
if (value == null) return null;
|
||||
ListUpdate result = value.getResultOrNull();
|
||||
return result == null ? null : result.getItems();
|
||||
}
|
||||
|
||||
static class ListUpdate {
|
||||
|
||||
@Nullable
|
||||
private final Boolean postAddedWasLocal;
|
||||
private final List<BlogPostItem> items;
|
||||
|
||||
ListUpdate(@Nullable Boolean postAddedWasLocal,
|
||||
List<BlogPostItem> items) {
|
||||
this.postAddedWasLocal = postAddedWasLocal;
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Boolean getPostAddedWasLocal() {
|
||||
return postAddedWasLocal;
|
||||
}
|
||||
|
||||
public List<BlogPostItem> getItems() {
|
||||
return items;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,9 @@ import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
@@ -23,7 +26,16 @@ public class BlogActivity extends BriarActivity
|
||||
implements BaseFragmentListener {
|
||||
|
||||
@Inject
|
||||
BlogController blogController;
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
|
||||
private BlogViewModel viewModel;
|
||||
|
||||
@Override
|
||||
public void injectActivity(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
viewModel = new ViewModelProvider(this, viewModelFactory)
|
||||
.get(BlogViewModel.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle state) {
|
||||
@@ -31,32 +43,36 @@ public class BlogActivity extends BriarActivity
|
||||
|
||||
// GroupId from Intent
|
||||
Intent i = getIntent();
|
||||
byte[] b = i.getByteArrayExtra(GROUP_ID);
|
||||
if (b == null) throw new IllegalStateException("No group ID in intent");
|
||||
GroupId groupId = new GroupId(b);
|
||||
blogController.setGroupId(groupId);
|
||||
GroupId groupId =
|
||||
new GroupId(requireNonNull(i.getByteArrayExtra(GROUP_ID)));
|
||||
viewModel.setGroupId(groupId);
|
||||
|
||||
setContentView(R.layout.activity_fragment_container_toolbar);
|
||||
Toolbar toolbar = setUpCustomToolbar(false);
|
||||
|
||||
// Open Sharing Status on Toolbar click
|
||||
if (toolbar != null) {
|
||||
toolbar.setOnClickListener(v -> {
|
||||
Intent i1 = new Intent(BlogActivity.this,
|
||||
BlogSharingStatusActivity.class);
|
||||
i1.putExtra(GROUP_ID, groupId.getBytes());
|
||||
startActivity(i1);
|
||||
});
|
||||
}
|
||||
toolbar.setOnClickListener(v -> {
|
||||
Intent i1 = new Intent(BlogActivity.this,
|
||||
BlogSharingStatusActivity.class);
|
||||
i1.putExtra(GROUP_ID, groupId.getBytes());
|
||||
startActivity(i1);
|
||||
});
|
||||
|
||||
viewModel.getBlog().observe(this, blog ->
|
||||
setTitle(blog.getBlog().getAuthor().getName())
|
||||
);
|
||||
viewModel.getSharingInfo().observe(this, info ->
|
||||
setToolbarSubTitle(info.total, info.online)
|
||||
);
|
||||
|
||||
if (state == null) {
|
||||
showInitialFragment(BlogFragment.newInstance(groupId));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectActivity(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
private void setToolbarSubTitle(int total, int online) {
|
||||
requireNonNull(getSupportActionBar())
|
||||
.setSubtitle(getString(R.string.shared_with, total, online));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,7 +8,9 @@ import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
// This class is not thread-safe
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
@NotThreadSafe
|
||||
class BlogCommentItem extends BlogPostItem {
|
||||
|
||||
private static final BlogCommentComparator COMPARATOR =
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
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.android.controller.handler.ResultExceptionHandler;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import androidx.annotation.UiThread;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface BlogController extends BaseController {
|
||||
|
||||
void setGroupId(GroupId g);
|
||||
|
||||
@UiThread
|
||||
void setBlogSharingListener(BlogSharingListener listener);
|
||||
|
||||
@UiThread
|
||||
void unsetBlogSharingListener(BlogSharingListener listener);
|
||||
|
||||
void loadBlogPosts(
|
||||
ResultExceptionHandler<Collection<BlogPostItem>, DbException> handler);
|
||||
|
||||
void loadBlogPost(MessageId m,
|
||||
ResultExceptionHandler<BlogPostItem, DbException> handler);
|
||||
|
||||
void loadBlog(ResultExceptionHandler<BlogItem, DbException> handler);
|
||||
|
||||
void deleteBlog(ResultExceptionHandler<Void, DbException> handler);
|
||||
|
||||
void loadSharingContacts(
|
||||
ResultExceptionHandler<Collection<ContactId>, DbException> handler);
|
||||
|
||||
interface BlogSharingListener extends BlogListener {
|
||||
@UiThread
|
||||
void onBlogInvitationAccepted(ContactId c);
|
||||
|
||||
@UiThread
|
||||
void onBlogLeft(ContactId c);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
@@ -12,33 +10,25 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.activity.BriarActivity;
|
||||
import org.briarproject.briar.android.blog.BlogController.BlogSharingListener;
|
||||
import org.briarproject.briar.android.controller.SharingController;
|
||||
import org.briarproject.briar.android.controller.handler.UiResultExceptionHandler;
|
||||
import org.briarproject.briar.android.blog.BaseViewModel.ListUpdate;
|
||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||
import org.briarproject.briar.android.sharing.BlogSharingStatusActivity;
|
||||
import org.briarproject.briar.android.sharing.ShareBlogActivity;
|
||||
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
|
||||
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||
import org.briarproject.briar.api.blog.BlogPostHeader;
|
||||
|
||||
import java.util.Collection;
|
||||
import org.briarproject.briar.android.widget.LinkDialogFragment;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView.LayoutManager;
|
||||
|
||||
@@ -49,30 +39,22 @@ import static com.google.android.material.snackbar.Snackbar.LENGTH_LONG;
|
||||
import static org.briarproject.briar.android.activity.BriarActivity.GROUP_ID;
|
||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_SHARE_BLOG;
|
||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_WRITE_BLOG_POST;
|
||||
import static org.briarproject.briar.android.controller.SharingController.SharingListener;
|
||||
|
||||
@UiThread
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class BlogFragment extends BaseFragment
|
||||
implements BlogSharingListener, SharingListener,
|
||||
OnBlogPostClickListener {
|
||||
implements OnBlogPostClickListener {
|
||||
|
||||
private final static String TAG = BlogFragment.class.getName();
|
||||
|
||||
@Inject
|
||||
BlogController blogController;
|
||||
@Inject
|
||||
SharingController sharingController;
|
||||
@Nullable
|
||||
private Parcelable layoutManagerState;
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
|
||||
private GroupId groupId;
|
||||
private BlogPostAdapter adapter;
|
||||
private LayoutManager layoutManager;
|
||||
private BlogViewModel viewModel;
|
||||
private final BlogPostAdapter adapter = new BlogPostAdapter(false, this);
|
||||
private BriarRecyclerView list;
|
||||
private MenuItem writeButton, deleteButton;
|
||||
private boolean isMyBlog = false, canDeleteBlog = false;
|
||||
|
||||
static BlogFragment newInstance(GroupId groupId) {
|
||||
BlogFragment f = new BlogFragment();
|
||||
@@ -87,8 +69,8 @@ public class BlogFragment extends BaseFragment
|
||||
@Override
|
||||
public void injectFragment(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
blogController.setBlogSharingListener(this);
|
||||
sharingController.setSharingListener(this);
|
||||
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory)
|
||||
.get(BlogViewModel.class);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -103,95 +85,75 @@ public class BlogFragment extends BaseFragment
|
||||
|
||||
View v = inflater.inflate(R.layout.fragment_blog, container, false);
|
||||
|
||||
adapter = new BlogPostAdapter(requireActivity(), this,
|
||||
getFragmentManager());
|
||||
list = v.findViewById(R.id.postList);
|
||||
layoutManager = new LinearLayoutManager(getActivity());
|
||||
LayoutManager layoutManager = new LinearLayoutManager(getActivity());
|
||||
list.setLayoutManager(layoutManager);
|
||||
list.setAdapter(adapter);
|
||||
list.showProgressBar();
|
||||
list.setEmptyText(getString(R.string.blogs_other_blog_empty_state));
|
||||
|
||||
if (savedInstanceState != null) {
|
||||
layoutManagerState =
|
||||
savedInstanceState.getParcelable("layoutManager");
|
||||
}
|
||||
|
||||
viewModel.getBlogPosts().observe(getViewLifecycleOwner(), result ->
|
||||
result.onError(this::handleException)
|
||||
.onSuccess(this::onBlogPostsLoaded)
|
||||
);
|
||||
viewModel.getBlogRemoved().observe(getViewLifecycleOwner(), removed -> {
|
||||
if (removed) finish();
|
||||
});
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
sharingController.onStart();
|
||||
loadBlog();
|
||||
loadSharedContacts();
|
||||
loadBlogPosts(false);
|
||||
viewModel.blockAndClearNotifications();
|
||||
list.startPeriodicUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
sharingController.onStop();
|
||||
viewModel.unblockNotifications();
|
||||
list.stopPeriodicUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
blogController.unsetBlogSharingListener(this);
|
||||
sharingController.unsetSharingListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
if (layoutManager != null) {
|
||||
layoutManagerState = layoutManager.onSaveInstanceState();
|
||||
outState.putParcelable("layoutManager", layoutManagerState);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.blogs_blog_actions, menu);
|
||||
writeButton = menu.findItem(R.id.action_write_blog_post);
|
||||
if (isMyBlog) writeButton.setVisible(true);
|
||||
deleteButton = menu.findItem(R.id.action_blog_delete);
|
||||
if (canDeleteBlog) deleteButton.setEnabled(true);
|
||||
|
||||
MenuItem writeButton = menu.findItem(R.id.action_write_blog_post);
|
||||
MenuItem deleteButton = menu.findItem(R.id.action_blog_delete);
|
||||
viewModel.getBlog().observe(getViewLifecycleOwner(), blog -> {
|
||||
if (blog.isOurs()) writeButton.setVisible(true);
|
||||
if (blog.canBeRemoved()) deleteButton.setEnabled(true);
|
||||
});
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_write_blog_post:
|
||||
Intent i = new Intent(getActivity(),
|
||||
WriteBlogPostActivity.class);
|
||||
i.putExtra(GROUP_ID, groupId.getBytes());
|
||||
startActivityForResult(i, REQUEST_WRITE_BLOG_POST);
|
||||
return true;
|
||||
case R.id.action_blog_share:
|
||||
Intent i2 = new Intent(getActivity(), ShareBlogActivity.class);
|
||||
i2.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
|
||||
i2.putExtra(GROUP_ID, groupId.getBytes());
|
||||
startActivityForResult(i2, REQUEST_SHARE_BLOG);
|
||||
return true;
|
||||
case R.id.action_blog_sharing_status:
|
||||
Intent i3 = new Intent(getActivity(),
|
||||
BlogSharingStatusActivity.class);
|
||||
i3.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
|
||||
i3.putExtra(GROUP_ID, groupId.getBytes());
|
||||
startActivity(i3);
|
||||
return true;
|
||||
case R.id.action_blog_delete:
|
||||
showDeleteDialog();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
int itemId = item.getItemId();
|
||||
if (itemId == R.id.action_write_blog_post) {
|
||||
Intent i = new Intent(getActivity(), WriteBlogPostActivity.class);
|
||||
i.putExtra(GROUP_ID, groupId.getBytes());
|
||||
startActivityForResult(i, REQUEST_WRITE_BLOG_POST);
|
||||
return true;
|
||||
} else if (itemId == R.id.action_blog_share) {
|
||||
Intent i = new Intent(getActivity(), ShareBlogActivity.class);
|
||||
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
|
||||
i.putExtra(GROUP_ID, groupId.getBytes());
|
||||
startActivityForResult(i, REQUEST_SHARE_BLOG);
|
||||
return true;
|
||||
} else if (itemId == R.id.action_blog_sharing_status) {
|
||||
Intent i =
|
||||
new Intent(getActivity(), BlogSharingStatusActivity.class);
|
||||
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
|
||||
i.putExtra(GROUP_ID, groupId.getBytes());
|
||||
startActivity(i);
|
||||
return true;
|
||||
} else if (itemId == R.id.action_blog_delete) {
|
||||
showDeleteDialog();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -201,7 +163,6 @@ public class BlogFragment extends BaseFragment
|
||||
|
||||
if (request == REQUEST_WRITE_BLOG_POST && result == RESULT_OK) {
|
||||
displaySnackbar(R.string.blogs_blog_post_created, true);
|
||||
loadBlogPosts(true);
|
||||
} else if (request == REQUEST_SHARE_BLOG && result == RESULT_OK) {
|
||||
displaySnackbar(R.string.blogs_sharing_snackbar, false);
|
||||
}
|
||||
@@ -212,35 +173,25 @@ public class BlogFragment extends BaseFragment
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlogPostAdded(BlogPostHeader header, boolean local) {
|
||||
blogController.loadBlogPost(header,
|
||||
new UiResultExceptionHandler<BlogPostItem, DbException>(
|
||||
this) {
|
||||
@Override
|
||||
public void onResultUi(BlogPostItem post) {
|
||||
adapter.add(post);
|
||||
if (local) {
|
||||
list.scrollToPosition(0);
|
||||
displaySnackbar(R.string.blogs_blog_post_created,
|
||||
false);
|
||||
} else {
|
||||
displaySnackbar(R.string.blogs_blog_post_received,
|
||||
true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExceptionUi(DbException exception) {
|
||||
handleException(exception);
|
||||
}
|
||||
}
|
||||
);
|
||||
private void onBlogPostsLoaded(ListUpdate update) {
|
||||
adapter.submitList(update.getItems(), () -> {
|
||||
Boolean wasLocal = update.getPostAddedWasLocal();
|
||||
if (wasLocal != null && wasLocal) {
|
||||
list.scrollToPosition(0);
|
||||
displaySnackbar(R.string.blogs_blog_post_created,
|
||||
false);
|
||||
} else if (wasLocal != null) {
|
||||
displaySnackbar(R.string.blogs_blog_post_received,
|
||||
true);
|
||||
}
|
||||
list.showData();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlogPostClick(BlogPostItem post) {
|
||||
BlogPostFragment f = BlogPostFragment.newInstance(post.getId());
|
||||
BlogPostFragment f =
|
||||
BlogPostFragment.newInstance(groupId, post.getId(), false);
|
||||
showNextFragment(f);
|
||||
}
|
||||
|
||||
@@ -256,111 +207,10 @@ public class BlogFragment extends BaseFragment
|
||||
getContext().startActivity(i);
|
||||
}
|
||||
|
||||
private void loadBlogPosts(boolean reload) {
|
||||
blogController.loadBlogPosts(
|
||||
new UiResultExceptionHandler<Collection<BlogPostItem>,
|
||||
DbException>(this) {
|
||||
@Override
|
||||
public void onResultUi(Collection<BlogPostItem> posts) {
|
||||
if (posts.isEmpty()) {
|
||||
list.showData();
|
||||
} else {
|
||||
adapter.addAll(posts);
|
||||
if (reload || layoutManagerState == null) {
|
||||
list.scrollToPosition(0);
|
||||
} else {
|
||||
layoutManager.onRestoreInstanceState(
|
||||
layoutManagerState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExceptionUi(DbException exception) {
|
||||
handleException(exception);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void loadBlog() {
|
||||
blogController.loadBlog(
|
||||
new UiResultExceptionHandler<BlogItem, DbException>(this) {
|
||||
@Override
|
||||
public void onResultUi(BlogItem blog) {
|
||||
setToolbarTitle(blog.getBlog().getAuthor());
|
||||
if (blog.isOurs())
|
||||
showWriteButton();
|
||||
if (blog.canBeRemoved())
|
||||
enableDeleteButton();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExceptionUi(DbException exception) {
|
||||
handleException(exception);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setToolbarTitle(Author a) {
|
||||
getActivity().setTitle(a.getName());
|
||||
}
|
||||
|
||||
private void loadSharedContacts() {
|
||||
blogController.loadSharingContacts(
|
||||
new UiResultExceptionHandler<Collection<ContactId>,
|
||||
DbException>(this) {
|
||||
@Override
|
||||
public void onResultUi(Collection<ContactId> contacts) {
|
||||
sharingController.addAll(contacts);
|
||||
int online = sharingController.getOnlineCount();
|
||||
setToolbarSubTitle(contacts.size(), online);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExceptionUi(DbException exception) {
|
||||
handleException(exception);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlogInvitationAccepted(ContactId c) {
|
||||
sharingController.add(c);
|
||||
setToolbarSubTitle(sharingController.getTotalCount(),
|
||||
sharingController.getOnlineCount());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlogLeft(ContactId c) {
|
||||
sharingController.remove(c);
|
||||
setToolbarSubTitle(sharingController.getTotalCount(),
|
||||
sharingController.getOnlineCount());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharingInfoUpdated(int total, int online) {
|
||||
setToolbarSubTitle(total, online);
|
||||
}
|
||||
|
||||
private void setToolbarSubTitle(int total, int online) {
|
||||
ActionBar actionBar =
|
||||
((BriarActivity) getActivity()).getSupportActionBar();
|
||||
if (actionBar != null) {
|
||||
actionBar.setSubtitle(
|
||||
getString(R.string.shared_with, total, online));
|
||||
}
|
||||
}
|
||||
|
||||
private void showWriteButton() {
|
||||
isMyBlog = true;
|
||||
if (writeButton != null)
|
||||
writeButton.setVisible(true);
|
||||
}
|
||||
|
||||
private void enableDeleteButton() {
|
||||
canDeleteBlog = true;
|
||||
if (deleteButton != null)
|
||||
deleteButton.setEnabled(true);
|
||||
public void onLinkClick(String url) {
|
||||
LinkDialogFragment f = LinkDialogFragment.newInstance(url);
|
||||
f.show(getParentFragmentManager(), f.getUniqueTag());
|
||||
}
|
||||
|
||||
private void displaySnackbar(int stringId, boolean scroll) {
|
||||
@@ -373,38 +223,21 @@ public class BlogFragment extends BaseFragment
|
||||
}
|
||||
|
||||
private void showDeleteDialog() {
|
||||
DialogInterface.OnClickListener okListener =
|
||||
(dialog, which) -> deleteBlog();
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext(),
|
||||
R.style.BriarDialogTheme);
|
||||
builder.setTitle(getString(R.string.blogs_remove_blog));
|
||||
builder.setMessage(
|
||||
getString(R.string.blogs_remove_blog_dialog_message));
|
||||
builder.setPositiveButton(R.string.cancel, null);
|
||||
builder.setNegativeButton(R.string.blogs_remove_blog_ok, okListener);
|
||||
builder.setNegativeButton(R.string.blogs_remove_blog_ok,
|
||||
(dialog, which) -> deleteBlog());
|
||||
builder.show();
|
||||
}
|
||||
|
||||
private void deleteBlog() {
|
||||
blogController.deleteBlog(
|
||||
new UiResultExceptionHandler<Void, DbException>(this) {
|
||||
@Override
|
||||
public void onResultUi(Void result) {
|
||||
Toast.makeText(getActivity(),
|
||||
R.string.blogs_blog_removed, LENGTH_SHORT)
|
||||
.show();
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExceptionUi(DbException exception) {
|
||||
handleException(exception);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlogRemoved() {
|
||||
viewModel.deleteBlog();
|
||||
Toast.makeText(getActivity(), R.string.blogs_blog_removed, LENGTH_SHORT)
|
||||
.show();
|
||||
finish();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,35 +1,23 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import org.briarproject.briar.android.activity.ActivityScope;
|
||||
import org.briarproject.briar.android.activity.BaseActivity;
|
||||
import org.briarproject.briar.android.controller.SharingController;
|
||||
import org.briarproject.briar.android.controller.SharingControllerImpl;
|
||||
import org.briarproject.briar.android.viewmodel.ViewModelKey;
|
||||
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import dagger.Binds;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import dagger.multibindings.IntoMap;
|
||||
|
||||
@Module
|
||||
public class BlogModule {
|
||||
public interface BlogModule {
|
||||
|
||||
@ActivityScope
|
||||
@Provides
|
||||
BlogController provideBlogController(BaseActivity activity,
|
||||
BlogControllerImpl blogController) {
|
||||
activity.addLifecycleController(blogController);
|
||||
return blogController;
|
||||
}
|
||||
@Binds
|
||||
@IntoMap
|
||||
@ViewModelKey(FeedViewModel.class)
|
||||
ViewModel bindFeedViewModel(FeedViewModel feedViewModel);
|
||||
|
||||
@ActivityScope
|
||||
@Provides
|
||||
FeedController provideFeedController(FeedControllerImpl feedController) {
|
||||
return feedController;
|
||||
}
|
||||
|
||||
@ActivityScope
|
||||
@Provides
|
||||
SharingController provideSharingController(
|
||||
SharingControllerImpl sharingController) {
|
||||
return sharingController;
|
||||
}
|
||||
@Binds
|
||||
@IntoMap
|
||||
@ViewModelKey(BlogViewModel.class)
|
||||
ViewModel bindBlogViewModel(BlogViewModel blogViewModel);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@@ -8,52 +7,44 @@ import android.view.ViewGroup;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.util.BriarAdapter;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.ListAdapter;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
class BlogPostAdapter extends BriarAdapter<BlogPostItem, BlogPostViewHolder> {
|
||||
class BlogPostAdapter extends ListAdapter<BlogPostItem, BlogPostViewHolder> {
|
||||
|
||||
private final boolean authorClickable;
|
||||
private final OnBlogPostClickListener listener;
|
||||
@Nullable
|
||||
private final FragmentManager fragmentManager;
|
||||
|
||||
BlogPostAdapter(Context ctx, OnBlogPostClickListener listener,
|
||||
@Nullable FragmentManager fragmentManager) {
|
||||
super(ctx, BlogPostItem.class);
|
||||
BlogPostAdapter(boolean authorClickable, OnBlogPostClickListener listener) {
|
||||
super(new DiffUtil.ItemCallback<BlogPostItem>() {
|
||||
@Override
|
||||
public boolean areItemsTheSame(BlogPostItem a, BlogPostItem b) {
|
||||
return a.getId().equals(b.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(BlogPostItem a, BlogPostItem b) {
|
||||
return a.isRead() == b.isRead();
|
||||
}
|
||||
});
|
||||
this.authorClickable = authorClickable;
|
||||
this.listener = listener;
|
||||
this.fragmentManager = fragmentManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlogPostViewHolder onCreateViewHolder(ViewGroup parent,
|
||||
int viewType) {
|
||||
View v = LayoutInflater.from(ctx).inflate(
|
||||
View v = LayoutInflater.from(parent.getContext()).inflate(
|
||||
R.layout.list_item_blog_post, parent, false);
|
||||
return new BlogPostViewHolder(v, false, listener, fragmentManager);
|
||||
return new BlogPostViewHolder(v, false, listener, authorClickable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(BlogPostViewHolder ui, int position) {
|
||||
ui.bindItem(getItemAt(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(BlogPostItem a, BlogPostItem b) {
|
||||
return a.compareTo(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(BlogPostItem a, BlogPostItem b) {
|
||||
return a.isRead() == b.isRead();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areItemsTheSame(BlogPostItem a, BlogPostItem b) {
|
||||
return a.getId().equals(b.getId());
|
||||
ui.bindItem(getItem(position));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,76 +1,163 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ProgressBar;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.blog.BaseController.BlogListener;
|
||||
import org.briarproject.briar.android.controller.handler.UiResultExceptionHandler;
|
||||
import org.briarproject.briar.api.blog.BlogPostHeader;
|
||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||
import org.briarproject.briar.android.widget.LinkDialogFragment;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
|
||||
import static android.view.View.INVISIBLE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.briar.android.activity.BriarActivity.GROUP_ID;
|
||||
import static org.briarproject.briar.android.util.UiUtils.MIN_DATE_RESOLUTION;
|
||||
|
||||
@UiThread
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class BlogPostFragment extends BasePostFragment implements BlogListener {
|
||||
public class BlogPostFragment extends BaseFragment
|
||||
implements OnBlogPostClickListener {
|
||||
|
||||
private static final String TAG = BlogPostFragment.class.getName();
|
||||
private static final Logger LOG = getLogger(TAG);
|
||||
|
||||
static final String POST_ID = "briar.POST_ID";
|
||||
static final String IS_FEED = "briar.IS_FEED";
|
||||
|
||||
protected BlogViewModel viewModel;
|
||||
private final Handler handler = new Handler(Looper.getMainLooper());
|
||||
|
||||
private ProgressBar progressBar;
|
||||
private BlogPostViewHolder ui;
|
||||
private BlogPostItem post;
|
||||
private Runnable refresher;
|
||||
|
||||
@Inject
|
||||
BlogController blogController;
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
|
||||
static BlogPostFragment newInstance(MessageId postId) {
|
||||
static BlogPostFragment newInstance(GroupId blogId, MessageId postId,
|
||||
boolean isFeed) {
|
||||
BlogPostFragment f = new BlogPostFragment();
|
||||
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putByteArray(GROUP_ID, blogId.getBytes());
|
||||
bundle.putByteArray(POST_ID, postId.getBytes());
|
||||
|
||||
bundle.putBoolean(IS_FEED, isFeed);
|
||||
f.setArguments(bundle);
|
||||
return f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectFragment(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory)
|
||||
.get(BlogViewModel.class);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater,
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
Bundle args = requireArguments();
|
||||
GroupId groupId =
|
||||
new GroupId(requireNonNull(args.getByteArray(GROUP_ID)));
|
||||
MessageId postId =
|
||||
new MessageId(requireNonNull(args.getByteArray(POST_ID)));
|
||||
boolean isFeed = args.getBoolean(IS_FEED);
|
||||
|
||||
View view = inflater.inflate(R.layout.fragment_blog_post, container,
|
||||
false);
|
||||
progressBar = view.findViewById(R.id.progressBar);
|
||||
progressBar.setVisibility(VISIBLE);
|
||||
ui = new BlogPostViewHolder(view, true, this, isFeed);
|
||||
LifecycleOwner owner = getViewLifecycleOwner();
|
||||
viewModel.loadBlogPost(groupId, postId).observe(owner, result ->
|
||||
result.onError(this::handleException)
|
||||
.onSuccess(this::onBlogPostLoaded)
|
||||
);
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
startPeriodicUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
stopPeriodicUpdate();
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void onBlogPostLoaded(BlogPostItem post) {
|
||||
progressBar.setVisibility(INVISIBLE);
|
||||
this.post = post;
|
||||
ui.bindItem(post);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlogPostClick(BlogPostItem post) {
|
||||
// We're already there
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthorClick(BlogPostItem post) {
|
||||
Intent i = new Intent(requireContext(), BlogActivity.class);
|
||||
i.putExtra(GROUP_ID, post.getGroupId().getBytes());
|
||||
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
|
||||
requireContext().startActivity(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLinkClick(String url) {
|
||||
LinkDialogFragment f = LinkDialogFragment.newInstance(url);
|
||||
f.show(getParentFragmentManager(), f.getUniqueTag());
|
||||
}
|
||||
|
||||
private void startPeriodicUpdate() {
|
||||
refresher = () -> {
|
||||
LOG.info("Updating Content...");
|
||||
ui.updateDate(post.getTimestamp());
|
||||
handler.postDelayed(refresher, MIN_DATE_RESOLUTION);
|
||||
};
|
||||
LOG.info("Adding Handler Callback");
|
||||
handler.postDelayed(refresher, MIN_DATE_RESOLUTION);
|
||||
}
|
||||
|
||||
private void stopPeriodicUpdate() {
|
||||
if (refresher != null) {
|
||||
LOG.info("Removing Handler Callback");
|
||||
handler.removeCallbacks(refresher);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUniqueTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectFragment(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
blogController.loadBlogPost(postId,
|
||||
new UiResultExceptionHandler<BlogPostItem, DbException>(
|
||||
this) {
|
||||
@Override
|
||||
public void onResultUi(BlogPostItem post) {
|
||||
onBlogPostLoaded(post);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExceptionUi(DbException exception) {
|
||||
handleException(exception);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlogPostAdded(BlogPostHeader header, boolean local) {
|
||||
// doesn't matter here
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlogRemoved() {
|
||||
finish();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ public class BlogPostItem implements Comparable<BlogPostItem> {
|
||||
private final BlogPostHeader header;
|
||||
@Nullable
|
||||
protected String text;
|
||||
private boolean read;
|
||||
private final boolean read;
|
||||
|
||||
BlogPostItem(BlogPostHeader header, @Nullable String text) {
|
||||
this.header = header;
|
||||
@@ -74,9 +74,6 @@ public class BlogPostItem implements Comparable<BlogPostItem> {
|
||||
|
||||
protected static int compare(BlogPostHeader h1, BlogPostHeader h2) {
|
||||
// The newest post comes first
|
||||
long aTime = h1.getTimeReceived(), bTime = h2.getTimeReceived();
|
||||
if (aTime > bTime) return -1;
|
||||
if (aTime < bTime) return 1;
|
||||
return 0;
|
||||
return Long.compare(h2.getTimeReceived(), h1.getTimeReceived());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import android.view.ViewGroup;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.view.AuthorView;
|
||||
@@ -17,23 +18,24 @@ import org.briarproject.briar.api.blog.BlogPostHeader;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static org.briarproject.briar.android.activity.BriarActivity.GROUP_ID;
|
||||
import static org.briarproject.briar.android.blog.BasePostFragment.POST_ID;
|
||||
import static org.briarproject.briar.android.blog.BlogPostFragment.POST_ID;
|
||||
import static org.briarproject.briar.android.util.UiUtils.TEASER_LENGTH;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getSpanned;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getTeaser;
|
||||
import static org.briarproject.briar.android.util.UiUtils.makeLinksClickable;
|
||||
import static org.briarproject.briar.api.blog.MessageType.POST;
|
||||
import static org.briarproject.briar.android.view.AuthorView.COMMENTER;
|
||||
import static org.briarproject.briar.android.view.AuthorView.REBLOGGER;
|
||||
import static org.briarproject.briar.android.view.AuthorView.RSS_FEED_REBLOGGED;
|
||||
|
||||
@UiThread
|
||||
@NotNullByDefault
|
||||
class BlogPostViewHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
private final Context ctx;
|
||||
@@ -43,20 +45,16 @@ class BlogPostViewHolder extends RecyclerView.ViewHolder {
|
||||
private final ImageButton reblogButton;
|
||||
private final TextView text;
|
||||
private final ViewGroup commentContainer;
|
||||
private final boolean fullText;
|
||||
private final boolean fullText, authorClickable;
|
||||
|
||||
@NonNull
|
||||
private final OnBlogPostClickListener listener;
|
||||
@Nullable
|
||||
private final FragmentManager fragmentManager;
|
||||
|
||||
BlogPostViewHolder(View v, boolean fullText,
|
||||
@NonNull OnBlogPostClickListener listener,
|
||||
@Nullable FragmentManager fragmentManager) {
|
||||
OnBlogPostClickListener listener, boolean authorClickable) {
|
||||
super(v);
|
||||
this.fullText = fullText;
|
||||
this.listener = listener;
|
||||
this.fragmentManager = fragmentManager;
|
||||
this.authorClickable = authorClickable;
|
||||
|
||||
ctx = v.getContext();
|
||||
layout = v.findViewById(R.id.postLayout);
|
||||
@@ -67,10 +65,6 @@ class BlogPostViewHolder extends RecyclerView.ViewHolder {
|
||||
commentContainer = v.findViewById(R.id.commentContainer);
|
||||
}
|
||||
|
||||
void setVisibility(int visibility) {
|
||||
layout.setVisibility(visibility);
|
||||
}
|
||||
|
||||
void hideReblogButton() {
|
||||
reblogButton.setVisibility(GONE);
|
||||
}
|
||||
@@ -103,7 +97,7 @@ class BlogPostViewHolder extends RecyclerView.ViewHolder {
|
||||
author.setPersona(
|
||||
item.isRssFeed() ? AuthorView.RSS_FEED : AuthorView.NORMAL);
|
||||
// TODO make author clickable more often #624
|
||||
if (!fullText && item.getHeader().getType() == POST) {
|
||||
if (authorClickable) {
|
||||
author.setAuthorClickable(v -> listener.onAuthorClick(item));
|
||||
} else {
|
||||
author.setAuthorNotClickable();
|
||||
@@ -114,7 +108,7 @@ class BlogPostViewHolder extends RecyclerView.ViewHolder {
|
||||
if (fullText) {
|
||||
text.setText(postText);
|
||||
text.setTextIsSelectable(true);
|
||||
makeLinksClickable(text, fragmentManager);
|
||||
makeLinksClickable(text, listener::onLinkClick);
|
||||
} else {
|
||||
text.setTextIsSelectable(false);
|
||||
if (postText.length() > TEASER_LENGTH)
|
||||
@@ -147,17 +141,16 @@ class BlogPostViewHolder extends RecyclerView.ViewHolder {
|
||||
reblogger.setAuthorClickable(v -> listener.onAuthorClick(item));
|
||||
}
|
||||
reblogger.setVisibility(VISIBLE);
|
||||
reblogger.setPersona(AuthorView.REBLOGGER);
|
||||
reblogger.setPersona(REBLOGGER);
|
||||
|
||||
author.setPersona(item.getHeader().getRootPost().isRssFeed() ?
|
||||
AuthorView.RSS_FEED_REBLOGGED :
|
||||
AuthorView.COMMENTER);
|
||||
RSS_FEED_REBLOGGED : COMMENTER);
|
||||
|
||||
// comments
|
||||
// TODO use nested RecyclerView instead like we do for Image Attachments
|
||||
for (BlogCommentHeader c : item.getComments()) {
|
||||
View v = LayoutInflater.from(ctx)
|
||||
.inflate(R.layout.list_item_blog_comment,
|
||||
commentContainer, false);
|
||||
View v = LayoutInflater.from(ctx).inflate(
|
||||
R.layout.list_item_blog_comment, commentContainer, false);
|
||||
|
||||
AuthorView author = v.findViewById(R.id.authorView);
|
||||
TextView text = v.findViewById(R.id.textView);
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Application;
|
||||
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.TransactionManager;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.api.sync.event.GroupRemovedEvent;
|
||||
import org.briarproject.briar.android.controller.ActivityLifecycleController;
|
||||
import org.briarproject.briar.android.controller.handler.ResultExceptionHandler;
|
||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||
import org.briarproject.briar.android.sharing.SharingController;
|
||||
import org.briarproject.briar.android.sharing.SharingController.SharingInfo;
|
||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||
import org.briarproject.briar.api.blog.Blog;
|
||||
import org.briarproject.briar.api.blog.BlogInvitationResponse;
|
||||
@@ -35,85 +35,54 @@ import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
class BlogControllerImpl extends BaseControllerImpl
|
||||
implements ActivityLifecycleController, BlogController, EventListener {
|
||||
class BlogViewModel extends BaseViewModel {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(BlogControllerImpl.class.getName());
|
||||
private static final Logger LOG = getLogger(BlogViewModel.class.getName());
|
||||
|
||||
private final BlogSharingManager blogSharingManager;
|
||||
private final SharingController sharingController;
|
||||
|
||||
// UI thread
|
||||
@Nullable
|
||||
private BlogSharingListener listener;
|
||||
private volatile GroupId groupId;
|
||||
|
||||
private volatile GroupId groupId = null;
|
||||
private final MutableLiveData<BlogItem> blog = new MutableLiveData<>();
|
||||
private final MutableLiveData<Boolean> blogRemoved =
|
||||
new MutableLiveData<>();
|
||||
|
||||
@Inject
|
||||
BlogControllerImpl(@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager, EventBus eventBus,
|
||||
BlogViewModel(Application application,
|
||||
@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager,
|
||||
TransactionManager db,
|
||||
AndroidExecutor androidExecutor,
|
||||
EventBus eventBus,
|
||||
IdentityManager identityManager,
|
||||
AndroidNotificationManager notificationManager,
|
||||
IdentityManager identityManager, BlogManager blogManager,
|
||||
BlogSharingManager blogSharingManager) {
|
||||
super(dbExecutor, lifecycleManager, eventBus, notificationManager,
|
||||
identityManager, blogManager);
|
||||
BlogManager blogManager,
|
||||
BlogSharingManager blogSharingManager,
|
||||
SharingController sharingController) {
|
||||
super(application, dbExecutor, lifecycleManager, db, androidExecutor,
|
||||
eventBus, identityManager, notificationManager, blogManager);
|
||||
this.blogSharingManager = blogSharingManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreate(Activity activity) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityStart() {
|
||||
super.onStart();
|
||||
notificationManager.blockNotification(groupId);
|
||||
notificationManager.clearBlogPostNotification(groupId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityStop() {
|
||||
super.onStop();
|
||||
notificationManager.unblockNotification(groupId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityDestroy() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGroupId(GroupId g) {
|
||||
groupId = g;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlogSharingListener(BlogSharingListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unsetBlogSharingListener(BlogSharingListener listener) {
|
||||
if (this.listener == listener) this.listener = null;
|
||||
this.sharingController = sharingController;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (groupId == null || listener == null)
|
||||
throw new IllegalStateException();
|
||||
if (e instanceof BlogPostAddedEvent) {
|
||||
BlogPostAddedEvent b = (BlogPostAddedEvent) e;
|
||||
if (b.getGroupId().equals(groupId)) {
|
||||
LOG.info("Blog post added");
|
||||
listener.onBlogPostAdded(b.getHeader(), b.isLocal());
|
||||
onBlogPostAdded(b.getHeader(), b.isLocal());
|
||||
}
|
||||
} else if (e instanceof BlogInvitationResponseReceivedEvent) {
|
||||
BlogInvitationResponseReceivedEvent b =
|
||||
@@ -121,41 +90,36 @@ class BlogControllerImpl extends BaseControllerImpl
|
||||
BlogInvitationResponse r = b.getMessageHeader();
|
||||
if (r.getShareableId().equals(groupId) && r.wasAccepted()) {
|
||||
LOG.info("Blog invitation accepted");
|
||||
listener.onBlogInvitationAccepted(b.getContactId());
|
||||
sharingController.add(b.getContactId());
|
||||
}
|
||||
} else if (e instanceof ContactLeftShareableEvent) {
|
||||
ContactLeftShareableEvent s = (ContactLeftShareableEvent) e;
|
||||
if (s.getGroupId().equals(groupId)) {
|
||||
LOG.info("Blog left by contact");
|
||||
listener.onBlogLeft(s.getContactId());
|
||||
sharingController.remove(s.getContactId());
|
||||
}
|
||||
} else if (e instanceof GroupRemovedEvent) {
|
||||
GroupRemovedEvent g = (GroupRemovedEvent) e;
|
||||
if (g.getGroup().getId().equals(groupId)) {
|
||||
LOG.info("Blog removed");
|
||||
listener.onBlogRemoved();
|
||||
blogRemoved.setValue(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadBlogPosts(
|
||||
ResultExceptionHandler<Collection<BlogPostItem>, DbException> handler) {
|
||||
if (groupId == null) throw new IllegalStateException();
|
||||
loadBlogPosts(groupId, handler);
|
||||
/**
|
||||
* Set this before calling any other methods.
|
||||
*/
|
||||
@UiThread
|
||||
public void setGroupId(GroupId groupId) {
|
||||
if (this.groupId == groupId) return; // configuration change
|
||||
this.groupId = groupId;
|
||||
loadBlog(groupId);
|
||||
loadBlogPosts(groupId);
|
||||
loadSharingContacts(groupId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadBlogPost(MessageId m,
|
||||
ResultExceptionHandler<BlogPostItem, DbException> handler) {
|
||||
if (groupId == null) throw new IllegalStateException();
|
||||
loadBlogPost(groupId, m, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadBlog(
|
||||
ResultExceptionHandler<BlogItem, DbException> handler) {
|
||||
if (groupId == null) throw new IllegalStateException();
|
||||
private void loadBlog(GroupId groupId) {
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
long start = now();
|
||||
@@ -163,50 +127,65 @@ class BlogControllerImpl extends BaseControllerImpl
|
||||
Blog b = blogManager.getBlog(groupId);
|
||||
boolean ours = a.getId().equals(b.getAuthor().getId());
|
||||
boolean removable = blogManager.canBeRemoved(b);
|
||||
BlogItem blog = new BlogItem(b, ours, removable);
|
||||
blog.postValue(new BlogItem(b, ours, removable));
|
||||
logDuration(LOG, "Loading blog", start);
|
||||
handler.onResult(blog);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
handler.onException(e);
|
||||
handleException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteBlog(ResultExceptionHandler<Void, DbException> handler) {
|
||||
if (groupId == null) throw new IllegalStateException();
|
||||
void blockAndClearNotifications() {
|
||||
notificationManager.blockNotification(groupId);
|
||||
notificationManager.clearBlogPostNotification(groupId);
|
||||
}
|
||||
|
||||
void unblockNotifications() {
|
||||
notificationManager.unblockNotification(groupId);
|
||||
}
|
||||
|
||||
private void loadBlogPosts(GroupId groupId) {
|
||||
loadList(txn -> new ListUpdate(null, loadBlogPosts(txn, groupId)),
|
||||
blogPosts::setValue);
|
||||
}
|
||||
|
||||
private void loadSharingContacts(GroupId groupId) {
|
||||
runOnDbThread(true, txn -> {
|
||||
Collection<Contact> contacts =
|
||||
blogSharingManager.getSharedWith(txn, groupId);
|
||||
txn.attach(() -> onSharingContactsLoaded(contacts));
|
||||
}, this::handleException);
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void onSharingContactsLoaded(Collection<Contact> contacts) {
|
||||
Collection<ContactId> contactIds = new ArrayList<>(contacts.size());
|
||||
for (Contact c : contacts) contactIds.add(c.getId());
|
||||
sharingController.addAll(contactIds);
|
||||
}
|
||||
|
||||
void deleteBlog() {
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
long start = now();
|
||||
Blog b = blogManager.getBlog(groupId);
|
||||
blogManager.removeBlog(b);
|
||||
logDuration(LOG, "Removing blog", start);
|
||||
handler.onResult(null);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
handler.onException(e);
|
||||
handleException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadSharingContacts(
|
||||
ResultExceptionHandler<Collection<ContactId>, DbException> handler) {
|
||||
if (groupId == null) throw new IllegalStateException();
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
Collection<Contact> contacts =
|
||||
blogSharingManager.getSharedWith(groupId);
|
||||
Collection<ContactId> contactIds =
|
||||
new ArrayList<>(contacts.size());
|
||||
for (Contact c : contacts) contactIds.add(c.getId());
|
||||
handler.onResult(contactIds);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
handler.onException(e);
|
||||
}
|
||||
});
|
||||
LiveData<BlogItem> getBlog() {
|
||||
return blog;
|
||||
}
|
||||
|
||||
LiveData<Boolean> getBlogRemoved() {
|
||||
return blogRemoved;
|
||||
}
|
||||
|
||||
LiveData<SharingInfo> getSharingInfo() {
|
||||
return sharingController.getSharingInfo();
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.android.controller.handler.ResultExceptionHandler;
|
||||
import org.briarproject.briar.api.blog.Blog;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import androidx.annotation.UiThread;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface FeedController extends BaseController {
|
||||
|
||||
void loadBlogPosts(
|
||||
ResultExceptionHandler<Collection<BlogPostItem>, DbException> handler);
|
||||
|
||||
void loadPersonalBlog(ResultExceptionHandler<Blog, DbException> handler);
|
||||
|
||||
@UiThread
|
||||
void setFeedListener(FeedListener listener);
|
||||
|
||||
@UiThread
|
||||
void unsetFeedListener(FeedListener listener);
|
||||
|
||||
@NotNullByDefault
|
||||
interface FeedListener extends BlogListener {
|
||||
|
||||
@UiThread
|
||||
void onBlogAdded();
|
||||
}
|
||||
}
|
||||
@@ -1,143 +0,0 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.NoSuchGroupException;
|
||||
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.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.event.GroupAddedEvent;
|
||||
import org.briarproject.bramble.api.sync.event.GroupRemovedEvent;
|
||||
import org.briarproject.briar.android.controller.handler.ResultExceptionHandler;
|
||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||
import org.briarproject.briar.api.blog.Blog;
|
||||
import org.briarproject.briar.api.blog.BlogManager;
|
||||
import org.briarproject.briar.api.blog.event.BlogPostAddedEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
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.api.blog.BlogManager.CLIENT_ID;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
class FeedControllerImpl extends BaseControllerImpl implements FeedController {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(FeedControllerImpl.class.getName());
|
||||
|
||||
// UI thread
|
||||
@Nullable
|
||||
private FeedListener listener;
|
||||
|
||||
@Inject
|
||||
FeedControllerImpl(@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager, EventBus eventBus,
|
||||
AndroidNotificationManager notificationManager,
|
||||
IdentityManager identityManager, BlogManager blogManager) {
|
||||
super(dbExecutor, lifecycleManager, eventBus, notificationManager,
|
||||
identityManager, blogManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
if (listener == null) throw new IllegalStateException();
|
||||
notificationManager.blockAllBlogPostNotifications();
|
||||
notificationManager.clearAllBlogPostNotifications();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
notificationManager.unblockAllBlogPostNotifications();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFeedListener(FeedListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unsetFeedListener(FeedListener listener) {
|
||||
if (this.listener == listener) this.listener = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (listener == null) throw new IllegalStateException();
|
||||
if (e instanceof BlogPostAddedEvent) {
|
||||
BlogPostAddedEvent b = (BlogPostAddedEvent) e;
|
||||
LOG.info("Blog post added");
|
||||
listener.onBlogPostAdded(b.getHeader(), b.isLocal());
|
||||
} else if (e instanceof GroupAddedEvent) {
|
||||
GroupAddedEvent g = (GroupAddedEvent) e;
|
||||
if (g.getGroup().getClientId().equals(CLIENT_ID)) {
|
||||
LOG.info("Blog added");
|
||||
listener.onBlogAdded();
|
||||
}
|
||||
} else if (e instanceof GroupRemovedEvent) {
|
||||
GroupRemovedEvent g = (GroupRemovedEvent) e;
|
||||
if (g.getGroup().getClientId().equals(CLIENT_ID)) {
|
||||
LOG.info("Blog removed");
|
||||
listener.onBlogRemoved();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadBlogPosts(
|
||||
ResultExceptionHandler<Collection<BlogPostItem>, DbException> handler) {
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
long start = now();
|
||||
Collection<BlogPostItem> posts = new ArrayList<>();
|
||||
for (Blog b : blogManager.getBlogs()) {
|
||||
try {
|
||||
posts.addAll(loadItems(b.getId()));
|
||||
} catch (NoSuchGroupException | NoSuchMessageException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
logDuration(LOG, "Loading all posts", start);
|
||||
handler.onResult(posts);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
handler.onException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadPersonalBlog(
|
||||
ResultExceptionHandler<Blog, DbException> handler) {
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
long start = now();
|
||||
Author a = identityManager.getLocalAuthor();
|
||||
Blog b = blogManager.getPersonalBlog(a);
|
||||
logDuration(LOG, "Loading personal blog", start);
|
||||
handler.onResult(b);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
handler.onException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package org.briarproject.briar.android.blog;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
@@ -10,53 +9,41 @@ import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.blog.FeedController.FeedListener;
|
||||
import org.briarproject.briar.android.controller.handler.UiResultExceptionHandler;
|
||||
import org.briarproject.briar.android.blog.BaseViewModel.ListUpdate;
|
||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
|
||||
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||
import org.briarproject.briar.android.widget.LinkDialogFragment;
|
||||
import org.briarproject.briar.api.blog.Blog;
|
||||
import org.briarproject.briar.api.blog.BlogPostHeader;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
|
||||
import static android.app.Activity.RESULT_OK;
|
||||
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
|
||||
import static com.google.android.material.snackbar.Snackbar.LENGTH_LONG;
|
||||
import static org.briarproject.briar.android.activity.BriarActivity.GROUP_ID;
|
||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_WRITE_BLOG_POST;
|
||||
|
||||
@UiThread
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class FeedFragment extends BaseFragment implements
|
||||
OnBlogPostClickListener, FeedListener {
|
||||
public class FeedFragment extends BaseFragment
|
||||
implements OnBlogPostClickListener {
|
||||
|
||||
public final static String TAG = FeedFragment.class.getName();
|
||||
private static final Logger LOG = Logger.getLogger(TAG);
|
||||
|
||||
@Inject
|
||||
FeedController feedController;
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
|
||||
private BlogPostAdapter adapter;
|
||||
private FeedViewModel viewModel;
|
||||
private final BlogPostAdapter adapter = new BlogPostAdapter(true, this);
|
||||
private LinearLayoutManager layoutManager;
|
||||
private BriarRecyclerView list;
|
||||
@Nullable
|
||||
private Blog personalBlog;
|
||||
@Nullable
|
||||
private Parcelable layoutManagerState;
|
||||
|
||||
public static FeedFragment newInstance() {
|
||||
FeedFragment f = new FeedFragment();
|
||||
@@ -70,7 +57,8 @@ public class FeedFragment extends BaseFragment implements
|
||||
@Override
|
||||
public void injectFragment(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
feedController.setFeedListener(this);
|
||||
viewModel = new ViewModelProvider(this, viewModelFactory)
|
||||
.get(FeedViewModel.class);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -82,9 +70,6 @@ public class FeedFragment extends BaseFragment implements
|
||||
|
||||
View v = inflater.inflate(R.layout.fragment_blog, container, false);
|
||||
|
||||
adapter =
|
||||
new BlogPostAdapter(getActivity(), this, getFragmentManager());
|
||||
|
||||
layoutManager = new LinearLayoutManager(getActivity());
|
||||
list = v.findViewById(R.id.postList);
|
||||
list.setLayoutManager(layoutManager);
|
||||
@@ -93,103 +78,38 @@ public class FeedFragment extends BaseFragment implements
|
||||
list.setEmptyText(R.string.blogs_feed_empty_state);
|
||||
list.setEmptyAction(R.string.blogs_feed_empty_state_action);
|
||||
|
||||
if (savedInstanceState != null) {
|
||||
layoutManagerState =
|
||||
savedInstanceState.getParcelable("layoutManager");
|
||||
}
|
||||
viewModel.getBlogPosts().observe(getViewLifecycleOwner(), result ->
|
||||
result.onError(this::handleException)
|
||||
.onSuccess(this::onBlogPostsLoaded)
|
||||
);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode,
|
||||
@Nullable Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
// The BlogPostAddedEvent arrives when the controller is not listening
|
||||
if (requestCode == REQUEST_WRITE_BLOG_POST && resultCode == RESULT_OK) {
|
||||
showSnackBar(R.string.blogs_blog_post_created);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
feedController.onStart();
|
||||
viewModel.blockAndClearAllBlogPostNotifications();
|
||||
list.startPeriodicUpdate();
|
||||
loadPersonalBlog();
|
||||
loadBlogPosts(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
feedController.onStop();
|
||||
adapter.clear();
|
||||
list.showProgressBar();
|
||||
viewModel.unblockAllBlogPostNotifications();
|
||||
list.stopPeriodicUpdate();
|
||||
// TODO save list position in database/preferences?
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
feedController.unsetFeedListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
if (layoutManager != null) {
|
||||
layoutManagerState = layoutManager.onSaveInstanceState();
|
||||
outState.putParcelable("layoutManager", layoutManagerState);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadPersonalBlog() {
|
||||
feedController.loadPersonalBlog(
|
||||
new UiResultExceptionHandler<Blog, DbException>(this) {
|
||||
@Override
|
||||
public void onResultUi(Blog b) {
|
||||
personalBlog = b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExceptionUi(DbException exception) {
|
||||
handleException(exception);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void loadBlogPosts(boolean clear) {
|
||||
int revision = adapter.getRevision();
|
||||
feedController.loadBlogPosts(
|
||||
new UiResultExceptionHandler<Collection<BlogPostItem>, DbException>(
|
||||
this) {
|
||||
@Override
|
||||
public void onResultUi(Collection<BlogPostItem> posts) {
|
||||
if (revision == adapter.getRevision()) {
|
||||
adapter.incrementRevision();
|
||||
if (clear) adapter.setItems(posts);
|
||||
else adapter.addAll(posts);
|
||||
if (posts.isEmpty()) list.showData();
|
||||
if (layoutManagerState == null) {
|
||||
list.scrollToPosition(0); // Scroll to the top
|
||||
} else {
|
||||
layoutManager.onRestoreInstanceState(
|
||||
layoutManagerState);
|
||||
}
|
||||
} else {
|
||||
LOG.info("Concurrent update, reloading");
|
||||
loadBlogPosts(clear);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExceptionUi(DbException exception) {
|
||||
handleException(exception);
|
||||
}
|
||||
});
|
||||
private void onBlogPostsLoaded(ListUpdate update) {
|
||||
adapter.submitList(update.getItems(), () -> {
|
||||
Boolean wasLocal = update.getPostAddedWasLocal();
|
||||
if (wasLocal != null && wasLocal) {
|
||||
showSnackBar(R.string.blogs_blog_post_created);
|
||||
} else if (wasLocal != null) {
|
||||
showSnackBar(R.string.blogs_blog_post_received);
|
||||
}
|
||||
list.showData();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -200,67 +120,48 @@ public class FeedFragment extends BaseFragment implements
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (personalBlog == null) return false;
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_write_blog_post:
|
||||
Intent i1 =
|
||||
new Intent(getActivity(), WriteBlogPostActivity.class);
|
||||
i1.putExtra(GROUP_ID, personalBlog.getId().getBytes());
|
||||
startActivityForResult(i1, REQUEST_WRITE_BLOG_POST);
|
||||
return true;
|
||||
case R.id.action_rss_feeds_import:
|
||||
Intent i2 =
|
||||
new Intent(getActivity(), RssFeedImportActivity.class);
|
||||
startActivity(i2);
|
||||
return true;
|
||||
case R.id.action_rss_feeds_manage:
|
||||
Intent i3 =
|
||||
new Intent(getActivity(), RssFeedManageActivity.class);
|
||||
i3.putExtra(GROUP_ID, personalBlog.getId().getBytes());
|
||||
startActivity(i3);
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
int itemId = item.getItemId();
|
||||
if (itemId == R.id.action_write_blog_post) {
|
||||
Blog personalBlog = viewModel.getPersonalBlog().getValue();
|
||||
if (personalBlog == null) return false;
|
||||
Intent i = new Intent(getActivity(), WriteBlogPostActivity.class);
|
||||
i.putExtra(GROUP_ID, personalBlog.getId().getBytes());
|
||||
startActivity(i);
|
||||
return true;
|
||||
} else if (itemId == R.id.action_rss_feeds_import) {
|
||||
Intent i = new Intent(getActivity(), RssFeedImportActivity.class);
|
||||
startActivity(i);
|
||||
return true;
|
||||
} else if (itemId == R.id.action_rss_feeds_manage) {
|
||||
Blog personalBlog = viewModel.getPersonalBlog().getValue();
|
||||
if (personalBlog == null) return false;
|
||||
Intent i = new Intent(getActivity(), RssFeedManageActivity.class);
|
||||
i.putExtra(GROUP_ID, personalBlog.getId().getBytes());
|
||||
startActivity(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlogPostAdded(BlogPostHeader header, boolean local) {
|
||||
feedController.loadBlogPost(header,
|
||||
new UiResultExceptionHandler<BlogPostItem, DbException>(
|
||||
this) {
|
||||
@Override
|
||||
public void onResultUi(BlogPostItem post) {
|
||||
adapter.incrementRevision();
|
||||
adapter.add(post);
|
||||
if (local) {
|
||||
showSnackBar(R.string.blogs_blog_post_created);
|
||||
} else {
|
||||
showSnackBar(R.string.blogs_blog_post_received);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExceptionUi(DbException exception) {
|
||||
handleException(exception);
|
||||
}
|
||||
}
|
||||
);
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlogPostClick(BlogPostItem post) {
|
||||
FeedPostFragment f =
|
||||
FeedPostFragment.newInstance(post.getGroupId(), post.getId());
|
||||
BaseFragment f = BlogPostFragment
|
||||
.newInstance(post.getGroupId(), post.getId(), true);
|
||||
showNextFragment(f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthorClick(BlogPostItem post) {
|
||||
Intent i = new Intent(getContext(), BlogActivity.class);
|
||||
Intent i = new Intent(requireContext(), BlogActivity.class);
|
||||
i.putExtra(GROUP_ID, post.getGroupId().getBytes());
|
||||
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
|
||||
getContext().startActivity(i);
|
||||
requireContext().startActivity(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLinkClick(String url) {
|
||||
LinkDialogFragment f = LinkDialogFragment.newInstance(url);
|
||||
f.show(getParentFragmentManager(), f.getUniqueTag());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -283,14 +184,4 @@ public class FeedFragment extends BaseFragment implements
|
||||
sb.make(list, stringRes, LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlogAdded() {
|
||||
loadBlogPosts(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlogRemoved() {
|
||||
loadBlogPosts(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.controller.handler.UiResultExceptionHandler;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.UiThread;
|
||||
|
||||
import static org.briarproject.briar.android.activity.BriarActivity.GROUP_ID;
|
||||
|
||||
@UiThread
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class FeedPostFragment extends BasePostFragment {
|
||||
|
||||
private static final String TAG = FeedPostFragment.class.getName();
|
||||
|
||||
private GroupId blogId;
|
||||
|
||||
@Inject
|
||||
FeedController feedController;
|
||||
|
||||
static FeedPostFragment newInstance(GroupId blogId, MessageId postId) {
|
||||
FeedPostFragment f = new FeedPostFragment();
|
||||
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putByteArray(GROUP_ID, blogId.getBytes());
|
||||
bundle.putByteArray(POST_ID, postId.getBytes());
|
||||
|
||||
f.setArguments(bundle);
|
||||
return f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectFragment(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater,
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
Bundle args = requireArguments();
|
||||
byte[] b = args.getByteArray(GROUP_ID);
|
||||
if (b == null) throw new IllegalStateException("No group ID in args");
|
||||
blogId = new GroupId(b);
|
||||
|
||||
return super.onCreateView(inflater, container, savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUniqueTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
feedController.loadBlogPost(blogId, postId,
|
||||
new UiResultExceptionHandler<BlogPostItem, DbException>(
|
||||
this) {
|
||||
@Override
|
||||
public void onResultUi(BlogPostItem post) {
|
||||
onBlogPostLoaded(post);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExceptionUi(DbException exception) {
|
||||
handleException(exception);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
package org.briarproject.briar.android.blog;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.db.TransactionManager;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
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.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.event.GroupRemovedEvent;
|
||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||
import org.briarproject.briar.android.viewmodel.LiveResult;
|
||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||
import org.briarproject.briar.api.blog.Blog;
|
||||
import org.briarproject.briar.api.blog.BlogManager;
|
||||
import org.briarproject.briar.api.blog.event.BlogPostAddedEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
import static org.briarproject.briar.api.blog.BlogManager.CLIENT_ID;
|
||||
|
||||
@NotNullByDefault
|
||||
class FeedViewModel extends BaseViewModel {
|
||||
|
||||
private static final Logger LOG = getLogger(FeedViewModel.class.getName());
|
||||
|
||||
private final MutableLiveData<Blog> personalBlog = new MutableLiveData<>();
|
||||
|
||||
@Inject
|
||||
FeedViewModel(Application application,
|
||||
@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager,
|
||||
TransactionManager db,
|
||||
AndroidExecutor androidExecutor,
|
||||
EventBus eventBus,
|
||||
IdentityManager identityManager,
|
||||
AndroidNotificationManager notificationManager,
|
||||
BlogManager blogManager) {
|
||||
super(application, dbExecutor, lifecycleManager, db, androidExecutor,
|
||||
eventBus, identityManager, notificationManager, blogManager);
|
||||
loadPersonalBlog();
|
||||
loadAllBlogPosts();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof BlogPostAddedEvent) {
|
||||
BlogPostAddedEvent b = (BlogPostAddedEvent) e;
|
||||
LOG.info("Blog post added");
|
||||
onBlogPostAdded(b.getHeader(), b.isLocal());
|
||||
}
|
||||
if (e instanceof GroupRemovedEvent) {
|
||||
GroupRemovedEvent g = (GroupRemovedEvent) e;
|
||||
if (g.getGroup().getClientId().equals(CLIENT_ID)) {
|
||||
LOG.info("Blog removed");
|
||||
onBlogRemoved(g.getGroup().getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void blockAndClearAllBlogPostNotifications() {
|
||||
notificationManager.blockAllBlogPostNotifications();
|
||||
notificationManager.clearAllBlogPostNotifications();
|
||||
}
|
||||
|
||||
void unblockAllBlogPostNotifications() {
|
||||
notificationManager.unblockAllBlogPostNotifications();
|
||||
}
|
||||
|
||||
private void loadPersonalBlog() {
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
long start = now();
|
||||
Author a = identityManager.getLocalAuthor();
|
||||
Blog b = blogManager.getPersonalBlog(a);
|
||||
logDuration(LOG, "Loading personal blog", start);
|
||||
personalBlog.postValue(b);
|
||||
} catch (DbException e) {
|
||||
handleException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
LiveData<Blog> getPersonalBlog() {
|
||||
return personalBlog;
|
||||
}
|
||||
|
||||
private void loadAllBlogPosts() {
|
||||
loadList(this::loadAllBlogPosts, blogPosts::setValue);
|
||||
}
|
||||
|
||||
@DatabaseExecutor
|
||||
private ListUpdate loadAllBlogPosts(Transaction txn)
|
||||
throws DbException {
|
||||
long start = now();
|
||||
List<BlogPostItem> posts = new ArrayList<>();
|
||||
for (GroupId g : blogManager.getBlogIds(txn)) {
|
||||
posts.addAll(loadBlogPosts(txn, g));
|
||||
}
|
||||
Collections.sort(posts);
|
||||
logDuration(LOG, "Loading all posts", start);
|
||||
return new ListUpdate(null, posts);
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void onBlogRemoved(GroupId g) {
|
||||
List<BlogPostItem> items = removeListItems(getBlogPostItems(),
|
||||
item -> item.getGroupId().equals(g)
|
||||
);
|
||||
if (items != null) {
|
||||
blogPosts.setValue(new LiveResult<>(new ListUpdate(null, items)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,4 +5,6 @@ interface OnBlogPostClickListener {
|
||||
void onBlogPostClick(BlogPostItem post);
|
||||
|
||||
void onAuthorClick(BlogPostItem post);
|
||||
|
||||
void onLinkClick(String url);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.activity.BriarActivity;
|
||||
import org.briarproject.briar.android.fragment.BaseFragment.BaseFragmentListener;
|
||||
|
||||
import static org.briarproject.briar.android.blog.BasePostFragment.POST_ID;
|
||||
import static org.briarproject.briar.android.blog.BlogPostFragment.POST_ID;
|
||||
|
||||
public class ReblogActivity extends BriarActivity implements
|
||||
BaseFragmentListener {
|
||||
@@ -39,13 +39,11 @@ public class ReblogActivity extends BriarActivity implements
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
onBackPressed();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
if (item.getItemId() == android.R.id.home) {
|
||||
onBackPressed();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,19 +7,17 @@ import android.view.ViewGroup;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.ScrollView;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.controller.handler.UiExceptionHandler;
|
||||
import org.briarproject.briar.android.controller.handler.UiResultExceptionHandler;
|
||||
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.android.widget.LinkDialogFragment;
|
||||
import org.briarproject.briar.api.attachment.AttachmentHeader;
|
||||
|
||||
import java.util.List;
|
||||
@@ -27,13 +25,15 @@ import java.util.List;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import static android.view.View.FOCUS_DOWN;
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.INVISIBLE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static org.briarproject.briar.android.activity.BriarActivity.GROUP_ID;
|
||||
import static org.briarproject.briar.android.blog.BasePostFragment.POST_ID;
|
||||
import static org.briarproject.briar.android.blog.BlogPostFragment.POST_ID;
|
||||
import static org.briarproject.briar.api.blog.BlogConstants.MAX_BLOG_POST_TEXT_LENGTH;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@@ -42,12 +42,13 @@ public class ReblogFragment extends BaseFragment implements SendListener {
|
||||
|
||||
public static final String TAG = ReblogFragment.class.getName();
|
||||
|
||||
@Inject
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
|
||||
private BlogViewModel viewModel;
|
||||
private ViewHolder ui;
|
||||
private BlogPostItem item;
|
||||
|
||||
@Inject
|
||||
FeedController feedController;
|
||||
|
||||
static ReblogFragment newInstance(GroupId groupId, MessageId messageId) {
|
||||
ReblogFragment f = new ReblogFragment();
|
||||
|
||||
@@ -67,6 +68,8 @@ public class ReblogFragment extends BaseFragment implements SendListener {
|
||||
@Override
|
||||
public void injectFragment(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory)
|
||||
.get(BlogViewModel.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -90,30 +93,20 @@ public class ReblogFragment extends BaseFragment implements SendListener {
|
||||
ui.input.setMaxTextLength(MAX_BLOG_POST_TEXT_LENGTH);
|
||||
showProgressBar();
|
||||
|
||||
feedController.loadBlogPost(blogId, postId,
|
||||
new UiResultExceptionHandler<BlogPostItem, DbException>(
|
||||
this) {
|
||||
@Override
|
||||
public void onResultUi(BlogPostItem result) {
|
||||
item = result;
|
||||
bindViewHolder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExceptionUi(DbException exception) {
|
||||
handleException(exception);
|
||||
}
|
||||
});
|
||||
viewModel.loadBlogPost(blogId, postId).observe(getViewLifecycleOwner(),
|
||||
result -> result.onError(this::handleException)
|
||||
.onSuccess(this::bindViewHolder)
|
||||
);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
private void bindViewHolder() {
|
||||
if (item == null) return;
|
||||
private void bindViewHolder(BlogPostItem item) {
|
||||
this.item = item;
|
||||
|
||||
hideProgressBar();
|
||||
|
||||
ui.post.bindItem(item);
|
||||
ui.post.bindItem(this.item);
|
||||
ui.post.hideReblogButton();
|
||||
|
||||
ui.input.setReady(true);
|
||||
@@ -124,13 +117,7 @@ public class ReblogFragment extends BaseFragment implements SendListener {
|
||||
public void onSendClick(@Nullable String text,
|
||||
List<AttachmentHeader> headers) {
|
||||
ui.input.hideSoftKeyboard();
|
||||
feedController.repeatPost(item, text,
|
||||
new UiExceptionHandler<DbException>(this) {
|
||||
@Override
|
||||
public void onExceptionUi(DbException exception) {
|
||||
handleException(exception);
|
||||
}
|
||||
});
|
||||
viewModel.repeatPost(item, text);
|
||||
finish();
|
||||
}
|
||||
|
||||
@@ -144,7 +131,7 @@ public class ReblogFragment extends BaseFragment implements SendListener {
|
||||
ui.input.setVisibility(VISIBLE);
|
||||
}
|
||||
|
||||
private class ViewHolder {
|
||||
private class ViewHolder implements OnBlogPostClickListener {
|
||||
|
||||
private final ScrollView scrollView;
|
||||
private final ProgressBar progressBar;
|
||||
@@ -155,18 +142,25 @@ public class ReblogFragment extends BaseFragment implements SendListener {
|
||||
scrollView = v.findViewById(R.id.scrollView);
|
||||
progressBar = v.findViewById(R.id.progressBar);
|
||||
post = new BlogPostViewHolder(v.findViewById(R.id.postLayout),
|
||||
true, new OnBlogPostClickListener() {
|
||||
@Override
|
||||
public void onBlogPostClick(BlogPostItem post) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthorClick(BlogPostItem post) {
|
||||
// probably don't want to allow author clicks here
|
||||
}
|
||||
}, getFragmentManager());
|
||||
true, this, false);
|
||||
input = v.findViewById(R.id.inputText);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlogPostClick(BlogPostItem post) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthorClick(BlogPostItem post) {
|
||||
// probably don't want to allow author clicks here
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLinkClick(String url) {
|
||||
LinkDialogFragment f = LinkDialogFragment.newInstance(url);
|
||||
f.show(getParentFragmentManager(), f.getUniqueTag());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.briarproject.briar.android.contact;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.View;
|
||||
|
||||
import org.briarproject.briar.android.util.BriarAdapter;
|
||||
|
||||
@@ -45,8 +44,4 @@ public abstract class BaseContactListAdapter<I extends ContactItem, VH extends C
|
||||
return true;
|
||||
}
|
||||
|
||||
public interface OnContactClickListener<I> {
|
||||
void onItemClick(View view, I item);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import android.widget.TextView;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ import android.view.ViewGroup;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.NullSafety;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
|
||||
import androidx.recyclerview.widget.DiffUtil.ItemCallback;
|
||||
import androidx.recyclerview.widget.ListAdapter;
|
||||
@@ -16,9 +15,6 @@ import androidx.recyclerview.widget.ListAdapter;
|
||||
public class ContactListAdapter extends
|
||||
ListAdapter<ContactListItem, ContactListItemViewHolder> {
|
||||
|
||||
// TODO: using the click listener interface from BaseContactListAdapter on
|
||||
// purpose here because it is entangled with ContactListItemViewHolder. At
|
||||
// some point we probably want to change that.
|
||||
protected final OnContactClickListener<ContactListItem> listener;
|
||||
|
||||
public ContactListAdapter(
|
||||
|
||||
@@ -15,7 +15,6 @@ import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
import org.briarproject.briar.android.contact.add.remote.AddContactActivity;
|
||||
import org.briarproject.briar.android.contact.add.remote.PendingContactListActivity;
|
||||
import org.briarproject.briar.android.conversation.ConversationActivity;
|
||||
@@ -34,7 +33,6 @@ import io.github.kobakei.materialfabspeeddial.FabSpeedDial;
|
||||
import io.github.kobakei.materialfabspeeddial.FabSpeedDial.OnMenuItemClickListener;
|
||||
|
||||
import static com.google.android.material.snackbar.BaseTransientBottomBar.LENGTH_INDEFINITE;
|
||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
||||
import static org.briarproject.briar.android.conversation.ConversationActivity.CONTACT_ID;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@@ -102,7 +100,6 @@ public class ContactListFragment extends BaseFragment
|
||||
.observe(getViewLifecycleOwner(), result -> {
|
||||
result.onError(this::handleException).onSuccess(items -> {
|
||||
adapter.submitList(items);
|
||||
if (requireNonNull(items).size() == 0) list.showData();
|
||||
});
|
||||
});
|
||||
viewModel.getHasPendingContacts()
|
||||
|
||||
@@ -5,7 +5,6 @@ import android.widget.TextView;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
|
||||
@@ -3,68 +3,41 @@ package org.briarproject.briar.android.contact;
|
||||
import android.app.Application;
|
||||
|
||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.contact.event.ContactAddedEvent;
|
||||
import org.briarproject.bramble.api.contact.event.ContactRemovedEvent;
|
||||
import org.briarproject.bramble.api.contact.event.PendingContactAddedEvent;
|
||||
import org.briarproject.bramble.api.contact.event.PendingContactRemovedEvent;
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.db.TransactionManager;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||
import org.briarproject.briar.android.viewmodel.DbViewModel;
|
||||
import org.briarproject.briar.android.viewmodel.LiveResult;
|
||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||
import org.briarproject.briar.api.avatar.event.AvatarUpdatedEvent;
|
||||
import org.briarproject.briar.api.client.MessageTracker;
|
||||
import org.briarproject.briar.api.conversation.ConversationManager;
|
||||
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
||||
import org.briarproject.briar.api.conversation.event.ConversationMessageReceivedEvent;
|
||||
import org.briarproject.briar.api.identity.AuthorInfo;
|
||||
import org.briarproject.briar.api.identity.AuthorManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.arch.core.util.Function;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@NotNullByDefault
|
||||
class ContactListViewModel extends DbViewModel implements EventListener {
|
||||
class ContactListViewModel extends ContactsViewModel {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(ContactListViewModel.class.getName());
|
||||
|
||||
private final ContactManager contactManager;
|
||||
private final AuthorManager authorManager;
|
||||
private final ConversationManager conversationManager;
|
||||
private final ConnectionRegistry connectionRegistry;
|
||||
private final EventBus eventBus;
|
||||
private final AndroidNotificationManager notificationManager;
|
||||
|
||||
private final MutableLiveData<LiveResult<List<ContactListItem>>>
|
||||
contactListItems = new MutableLiveData<>();
|
||||
|
||||
private final MutableLiveData<Boolean> hasPendingContacts =
|
||||
new MutableLiveData<>();
|
||||
|
||||
@@ -77,103 +50,34 @@ class ContactListViewModel extends DbViewModel implements EventListener {
|
||||
ConversationManager conversationManager,
|
||||
ConnectionRegistry connectionRegistry, EventBus eventBus,
|
||||
AndroidNotificationManager notificationManager) {
|
||||
super(application, dbExecutor, lifecycleManager, db, androidExecutor);
|
||||
this.contactManager = contactManager;
|
||||
this.authorManager = authorManager;
|
||||
this.conversationManager = conversationManager;
|
||||
this.connectionRegistry = connectionRegistry;
|
||||
this.eventBus = eventBus;
|
||||
super(application, dbExecutor, lifecycleManager, db, androidExecutor,
|
||||
contactManager, authorManager, conversationManager,
|
||||
connectionRegistry, eventBus);
|
||||
this.notificationManager = notificationManager;
|
||||
this.eventBus.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCleared() {
|
||||
super.onCleared();
|
||||
eventBus.removeListener(this);
|
||||
}
|
||||
|
||||
void loadContacts() {
|
||||
loadList(this::loadContacts, contactListItems::setValue);
|
||||
}
|
||||
|
||||
private List<ContactListItem> loadContacts(Transaction txn)
|
||||
throws DbException {
|
||||
long start = now();
|
||||
List<ContactListItem> contacts = new ArrayList<>();
|
||||
for (Contact c : contactManager.getContacts(txn)) {
|
||||
ContactId id = c.getId();
|
||||
AuthorInfo authorInfo = authorManager.getAuthorInfo(txn, c);
|
||||
MessageTracker.GroupCount count =
|
||||
conversationManager.getGroupCount(txn, id);
|
||||
boolean connected = connectionRegistry.isConnected(c.getId());
|
||||
contacts.add(new ContactListItem(c, authorInfo, connected, count));
|
||||
}
|
||||
Collections.sort(contacts);
|
||||
logDuration(LOG, "Full load", start);
|
||||
return contacts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof ContactAddedEvent) {
|
||||
LOG.info("Contact added, reloading");
|
||||
loadContacts();
|
||||
} else if (e instanceof ContactConnectedEvent) {
|
||||
updateItem(((ContactConnectedEvent) e).getContactId(),
|
||||
item -> new ContactListItem(item, true), false);
|
||||
} else if (e instanceof ContactDisconnectedEvent) {
|
||||
updateItem(((ContactDisconnectedEvent) e).getContactId(),
|
||||
item -> new ContactListItem(item, false), false);
|
||||
} else if (e instanceof ContactRemovedEvent) {
|
||||
LOG.info("Contact removed, removing item");
|
||||
removeItem(((ContactRemovedEvent) e).getContactId());
|
||||
} else if (e instanceof ConversationMessageReceivedEvent) {
|
||||
LOG.info("Conversation message received, updating item");
|
||||
ConversationMessageReceivedEvent<?> p =
|
||||
(ConversationMessageReceivedEvent<?>) e;
|
||||
ConversationMessageHeader h = p.getMessageHeader();
|
||||
updateItem(p.getContactId(), item -> new ContactListItem(item, h),
|
||||
true);
|
||||
} else if (e instanceof PendingContactAddedEvent ||
|
||||
super.eventOccurred(e);
|
||||
if (e instanceof PendingContactAddedEvent ||
|
||||
e instanceof PendingContactRemovedEvent) {
|
||||
checkForPendingContacts();
|
||||
} else if (e instanceof AvatarUpdatedEvent) {
|
||||
AvatarUpdatedEvent a = (AvatarUpdatedEvent) e;
|
||||
updateItem(a.getContactId(), item -> new ContactListItem(item,
|
||||
a.getAttachmentHeader()), false);
|
||||
}
|
||||
}
|
||||
|
||||
LiveData<LiveResult<List<ContactListItem>>> getContactListItems() {
|
||||
return contactListItems;
|
||||
}
|
||||
|
||||
LiveData<Boolean> getHasPendingContacts() {
|
||||
return hasPendingContacts;
|
||||
}
|
||||
|
||||
private void updateItem(ContactId c,
|
||||
Function<ContactListItem, ContactListItem> replacer, boolean sort) {
|
||||
List<ContactListItem> list = updateListItems(contactListItems,
|
||||
itemToTest -> itemToTest.getContact().getId().equals(c),
|
||||
replacer);
|
||||
if (list == null) return;
|
||||
if (sort) Collections.sort(list);
|
||||
contactListItems.setValue(new LiveResult<>(list));
|
||||
}
|
||||
|
||||
private void removeItem(ContactId c) {
|
||||
List<ContactListItem> list = removeListItems(contactListItems,
|
||||
itemToTest -> itemToTest.getContact().getId().equals(c));
|
||||
if (list == null) return;
|
||||
contactListItems.setValue(new LiveResult<>(list));
|
||||
}
|
||||
|
||||
void checkForPendingContacts() {
|
||||
runOnDbThreadOrLogException(() -> {
|
||||
boolean hasPending = !contactManager.getPendingContacts().isEmpty();
|
||||
hasPendingContacts.postValue(hasPending);
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
boolean hasPending =
|
||||
!contactManager.getPendingContacts().isEmpty();
|
||||
hasPendingContacts.postValue(hasPending);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
package org.briarproject.briar.android.contact;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.contact.event.ContactAddedEvent;
|
||||
import org.briarproject.bramble.api.contact.event.ContactRemovedEvent;
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.db.TransactionManager;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||
import org.briarproject.briar.android.viewmodel.DbViewModel;
|
||||
import org.briarproject.briar.android.viewmodel.LiveResult;
|
||||
import org.briarproject.briar.api.avatar.event.AvatarUpdatedEvent;
|
||||
import org.briarproject.briar.api.client.MessageTracker;
|
||||
import org.briarproject.briar.api.conversation.ConversationManager;
|
||||
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
||||
import org.briarproject.briar.api.conversation.event.ConversationMessageReceivedEvent;
|
||||
import org.briarproject.briar.api.identity.AuthorInfo;
|
||||
import org.briarproject.briar.api.identity.AuthorManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.arch.core.util.Function;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
|
||||
@NotNullByDefault
|
||||
public class ContactsViewModel extends DbViewModel implements EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(ContactsViewModel.class.getName());
|
||||
|
||||
protected final ContactManager contactManager;
|
||||
private final AuthorManager authorManager;
|
||||
private final ConversationManager conversationManager;
|
||||
private final ConnectionRegistry connectionRegistry;
|
||||
private final EventBus eventBus;
|
||||
|
||||
private final MutableLiveData<LiveResult<List<ContactListItem>>>
|
||||
contactListItems = new MutableLiveData<>();
|
||||
|
||||
@Inject
|
||||
public ContactsViewModel(Application application,
|
||||
@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager, TransactionManager db,
|
||||
AndroidExecutor androidExecutor, ContactManager contactManager,
|
||||
AuthorManager authorManager,
|
||||
ConversationManager conversationManager,
|
||||
ConnectionRegistry connectionRegistry, EventBus eventBus) {
|
||||
super(application, dbExecutor, lifecycleManager, db, androidExecutor);
|
||||
this.contactManager = contactManager;
|
||||
this.authorManager = authorManager;
|
||||
this.conversationManager = conversationManager;
|
||||
this.connectionRegistry = connectionRegistry;
|
||||
this.eventBus = eventBus;
|
||||
this.eventBus.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCleared() {
|
||||
super.onCleared();
|
||||
eventBus.removeListener(this);
|
||||
}
|
||||
|
||||
protected void loadContacts() {
|
||||
loadList(this::loadContacts, contactListItems::setValue);
|
||||
}
|
||||
|
||||
private List<ContactListItem> loadContacts(Transaction txn)
|
||||
throws DbException {
|
||||
long start = now();
|
||||
List<ContactListItem> contacts = new ArrayList<>();
|
||||
for (Contact c : contactManager.getContacts(txn)) {
|
||||
ContactId id = c.getId();
|
||||
if (!displayContact(id)) {
|
||||
continue;
|
||||
}
|
||||
AuthorInfo authorInfo = authorManager.getAuthorInfo(txn, c);
|
||||
MessageTracker.GroupCount count =
|
||||
conversationManager.getGroupCount(txn, id);
|
||||
boolean connected = connectionRegistry.isConnected(c.getId());
|
||||
contacts.add(new ContactListItem(c, authorInfo, connected, count));
|
||||
}
|
||||
Collections.sort(contacts);
|
||||
logDuration(LOG, "Full load", start);
|
||||
return contacts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this method to display only a subset of contacts.
|
||||
*/
|
||||
protected boolean displayContact(ContactId contactId) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof ContactAddedEvent) {
|
||||
LOG.info("Contact added, reloading");
|
||||
loadContacts();
|
||||
} else if (e instanceof ContactConnectedEvent) {
|
||||
updateItem(((ContactConnectedEvent) e).getContactId(),
|
||||
item -> new ContactListItem(item, true), false);
|
||||
} else if (e instanceof ContactDisconnectedEvent) {
|
||||
updateItem(((ContactDisconnectedEvent) e).getContactId(),
|
||||
item -> new ContactListItem(item, false), false);
|
||||
} else if (e instanceof ContactRemovedEvent) {
|
||||
LOG.info("Contact removed, removing item");
|
||||
removeItem(((ContactRemovedEvent) e).getContactId());
|
||||
} else if (e instanceof ConversationMessageReceivedEvent) {
|
||||
LOG.info("Conversation message received, updating item");
|
||||
ConversationMessageReceivedEvent<?> p =
|
||||
(ConversationMessageReceivedEvent<?>) e;
|
||||
ConversationMessageHeader h = p.getMessageHeader();
|
||||
updateItem(p.getContactId(), item -> new ContactListItem(item, h),
|
||||
true);
|
||||
} else if (e instanceof AvatarUpdatedEvent) {
|
||||
AvatarUpdatedEvent a = (AvatarUpdatedEvent) e;
|
||||
updateItem(a.getContactId(), item -> new ContactListItem(item,
|
||||
a.getAttachmentHeader()), false);
|
||||
}
|
||||
}
|
||||
|
||||
public LiveData<LiveResult<List<ContactListItem>>> getContactListItems() {
|
||||
return contactListItems;
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void updateItem(ContactId c,
|
||||
Function<ContactListItem, ContactListItem> replacer, boolean sort) {
|
||||
List<ContactListItem> list = updateListItems(getList(contactListItems),
|
||||
itemToTest -> itemToTest.getContact().getId().equals(c),
|
||||
replacer);
|
||||
if (list == null) return;
|
||||
if (sort) Collections.sort(list);
|
||||
contactListItems.setValue(new LiveResult<>(list));
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void removeItem(ContactId c) {
|
||||
removeAndUpdateListItems(contactListItems,
|
||||
itemToTest -> itemToTest.getContact().getId().equals(c));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package org.briarproject.briar.android.contact;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
|
||||
@NotNullByDefault
|
||||
public class LegacyContactListAdapter extends
|
||||
BaseContactListAdapter<ContactListItem, ContactListItemViewHolder> {
|
||||
|
||||
public LegacyContactListAdapter(Context context,
|
||||
OnContactClickListener<ContactListItem> listener) {
|
||||
super(context, ContactListItem.class, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContactListItemViewHolder onCreateViewHolder(ViewGroup viewGroup,
|
||||
int i) {
|
||||
View v = LayoutInflater.from(viewGroup.getContext()).inflate(
|
||||
R.layout.list_item_contact, viewGroup, false);
|
||||
|
||||
return new ContactListItemViewHolder(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(ContactListItem c1, ContactListItem c2) {
|
||||
// check for all properties that influence visual
|
||||
// representation of contact
|
||||
if (c1.isEmpty() != c2.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (c1.getUnreadCount() != c2.getUnreadCount()) {
|
||||
return false;
|
||||
}
|
||||
if (c1.getTimestamp() != c2.getTimestamp()) {
|
||||
return false;
|
||||
}
|
||||
return c1.isConnected() == c2.isConnected();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(ContactListItem c1, ContactListItem c2) {
|
||||
return Long.compare(c2.getTimestamp(), c1.getTimestamp());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.briarproject.briar.android.contact;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
public interface OnContactClickListener<I> {
|
||||
|
||||
void onItemClick(View view, I item);
|
||||
|
||||
}
|
||||
@@ -2,9 +2,12 @@ package org.briarproject.briar.android.contact.add.remote;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
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.db.TransactionManager;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
@@ -15,6 +18,7 @@ import org.briarproject.briar.android.viewmodel.LiveEvent;
|
||||
import org.briarproject.briar.android.viewmodel.LiveResult;
|
||||
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -62,10 +66,15 @@ public class AddContactViewModel extends DbViewModel {
|
||||
}
|
||||
|
||||
private void loadHandshakeLink() {
|
||||
// If an exception is thrown the UI should stay disabled,
|
||||
// leaving the user unable to proceed
|
||||
runOnDbThreadOrLogException(() ->
|
||||
handshakeLink.postValue(contactManager.getHandshakeLink()));
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
handshakeLink.postValue(contactManager.getHandshakeLink());
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
// the UI should stay disabled in this case,
|
||||
// leaving the user unable to proceed
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
LiveData<String> getHandshakeLink() {
|
||||
@@ -97,11 +106,17 @@ public class AddContactViewModel extends DbViewModel {
|
||||
void addContact(String nickname) {
|
||||
if (remoteHandshakeLink == null) throw new IllegalStateException();
|
||||
runOnDbThread(() -> {
|
||||
contactManager.addPendingContact(remoteHandshakeLink, nickname);
|
||||
addContactResult.postValue(new LiveResult<>(true));
|
||||
}, e -> {
|
||||
logException(LOG, WARNING, e);
|
||||
addContactResult.postValue(new LiveResult<>(e));
|
||||
try {
|
||||
contactManager.addPendingContact(remoteHandshakeLink, nickname);
|
||||
addContactResult.postValue(new LiveResult<>(true));
|
||||
} catch (UnsupportedVersionException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
addContactResult.postValue(new LiveResult<>(e));
|
||||
} catch (DbException | FormatException
|
||||
| GeneralSecurityException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
addContactResult.postValue(new LiveResult<>(e));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -109,15 +124,15 @@ public class AddContactViewModel extends DbViewModel {
|
||||
return addContactResult;
|
||||
}
|
||||
|
||||
public void updatePendingContact(String name, PendingContact p) {
|
||||
void updatePendingContact(String name, PendingContact p) {
|
||||
runOnDbThread(() -> {
|
||||
contactManager.removePendingContact(p.getId());
|
||||
addContact(name);
|
||||
}, e -> {
|
||||
if (e instanceof NoSuchPendingContactException) {
|
||||
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
|
||||
} else {
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
addContactResult.postValue(new LiveResult<>(e));
|
||||
}
|
||||
|
||||
@@ -29,11 +29,11 @@ import org.briarproject.briar.android.fragment.BaseFragment;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.appcompat.app.AlertDialog.Builder;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.lifecycle.ViewModelProviders;
|
||||
|
||||
import static android.view.View.INVISIBLE;
|
||||
import static android.view.View.VISIBLE;
|
||||
@@ -48,6 +48,7 @@ import static org.briarproject.briar.android.util.UiUtils.getDialogIcon;
|
||||
public class NicknameFragment extends BaseFragment {
|
||||
|
||||
private static final String TAG = NicknameFragment.class.getName();
|
||||
private static final String SAVED_LINK = "savedLink";
|
||||
|
||||
@Inject
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
@@ -67,6 +68,20 @@ public class NicknameFragment extends BaseFragment {
|
||||
@Override
|
||||
public void injectFragment(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory)
|
||||
.get(AddContactViewModel.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (savedInstanceState != null) {
|
||||
// When the activity (and the ViewModel) get destroyed,
|
||||
// the link will not be available anymore and needs to be restored.
|
||||
// TODO migrate to SavedStateViewModelFactory (once we can use it)
|
||||
String savedLink = savedInstanceState.getString(SAVED_LINK);
|
||||
if (savedLink != null) viewModel.setRemoteHandshakeLink(savedLink);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -74,14 +89,9 @@ public class NicknameFragment extends BaseFragment {
|
||||
public View onCreateView(LayoutInflater inflater,
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
if (getActivity() == null || getContext() == null) return null;
|
||||
|
||||
View v = inflater.inflate(R.layout.fragment_nickname,
|
||||
container, false);
|
||||
|
||||
viewModel = ViewModelProviders.of(getActivity(), viewModelFactory)
|
||||
.get(AddContactViewModel.class);
|
||||
|
||||
contactNameLayout = v.findViewById(R.id.contactNameLayout);
|
||||
contactNameInput = v.findViewById(R.id.contactNameInput);
|
||||
|
||||
@@ -93,6 +103,12 @@ public class NicknameFragment extends BaseFragment {
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putString(SAVED_LINK, viewModel.getRemoteHandshakeLink());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private String getNicknameOrNull() {
|
||||
Editable text = contactNameInput.getText();
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.briarproject.bramble.api.contact.PendingContactState;
|
||||
import org.briarproject.bramble.api.contact.event.PendingContactRemovedEvent;
|
||||
import org.briarproject.bramble.api.contact.event.PendingContactStateChangedEvent;
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.TransactionManager;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
@@ -32,8 +33,10 @@ import javax.inject.Inject;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.contact.PendingContactState.OFFLINE;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@NotNullByDefault
|
||||
public class PendingContactListViewModel extends DbViewModel
|
||||
@@ -87,20 +90,24 @@ public class PendingContactListViewModel extends DbViewModel
|
||||
}
|
||||
|
||||
private void loadPendingContacts() {
|
||||
runOnDbThreadOrLogException(() -> {
|
||||
Collection<Pair<PendingContact, PendingContactState>> pairs =
|
||||
contactManager.getPendingContacts();
|
||||
List<PendingContactItem> items = new ArrayList<>(pairs.size());
|
||||
boolean online = pairs.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, state, lastPoll));
|
||||
online = online || state != OFFLINE;
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
Collection<Pair<PendingContact, PendingContactState>> pairs =
|
||||
contactManager.getPendingContacts();
|
||||
List<PendingContactItem> items = new ArrayList<>(pairs.size());
|
||||
boolean online = pairs.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, state, lastPoll));
|
||||
online = online || state != OFFLINE;
|
||||
}
|
||||
pendingContacts.postValue(items);
|
||||
hasInternetConnection.postValue(online);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
pendingContacts.postValue(items);
|
||||
hasInternetConnection.postValue(online);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -109,8 +116,13 @@ public class PendingContactListViewModel extends DbViewModel
|
||||
}
|
||||
|
||||
void removePendingContact(PendingContactId id) {
|
||||
runOnDbThreadOrLogException(() ->
|
||||
contactManager.removePendingContact(id));
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
contactManager.removePendingContact(id);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
LiveData<Boolean> getHasInternetConnection() {
|
||||
|
||||
@@ -6,6 +6,7 @@ import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.android.contact.BaseContactListAdapter;
|
||||
import org.briarproject.briar.android.contact.ContactItemViewHolder;
|
||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -12,8 +12,8 @@ import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
import org.briarproject.briar.android.contact.ContactItemViewHolder;
|
||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||
import org.briarproject.briar.android.controller.handler.UiResultExceptionHandler;
|
||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||
|
||||
@@ -6,8 +6,8 @@ import android.widget.TextView;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
import org.briarproject.briar.android.contact.ContactItemViewHolder;
|
||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import android.view.ViewGroup;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||
|
||||
@NotNullByDefault
|
||||
class ContactSelectorAdapter extends
|
||||
|
||||
@@ -8,7 +8,7 @@ import android.view.MenuItem;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
|
||||
@@ -3,7 +3,7 @@ package org.briarproject.briar.android.contactselection;
|
||||
import android.view.View;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
package org.briarproject.briar.android.controller;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import androidx.annotation.UiThread;
|
||||
|
||||
@Deprecated
|
||||
@NotNullByDefault
|
||||
public interface SharingController {
|
||||
|
||||
/**
|
||||
* Sets the listener that is called when contacts go on or offline.
|
||||
*/
|
||||
@UiThread
|
||||
void setSharingListener(SharingListener listener);
|
||||
|
||||
/**
|
||||
* Unsets the listener.
|
||||
*/
|
||||
@UiThread
|
||||
void unsetSharingListener(SharingListener listener);
|
||||
|
||||
/**
|
||||
* Call this when your lifecycle starts,
|
||||
* so the listener will be called when information changes.
|
||||
*/
|
||||
@UiThread
|
||||
void onStart();
|
||||
|
||||
/**
|
||||
* Call this when your lifecycle stops,
|
||||
* so that the controller knows it can stops listening to events.
|
||||
*/
|
||||
@UiThread
|
||||
void onStop();
|
||||
|
||||
/**
|
||||
* Adds one contact to be tracked.
|
||||
*/
|
||||
@UiThread
|
||||
void add(ContactId c);
|
||||
|
||||
/**
|
||||
* Adds a collection of contacts to be tracked.
|
||||
*/
|
||||
@UiThread
|
||||
void addAll(Collection<ContactId> contacts);
|
||||
|
||||
/**
|
||||
* Call this when the contact identified by c is no longer sharing
|
||||
* the given group identified by GroupId g.
|
||||
*/
|
||||
@UiThread
|
||||
void remove(ContactId c);
|
||||
|
||||
/**
|
||||
* Returns the number of online contacts.
|
||||
*/
|
||||
@UiThread
|
||||
int getOnlineCount();
|
||||
|
||||
/**
|
||||
* Returns the total number of contacts that have been added.
|
||||
*/
|
||||
@UiThread
|
||||
int getTotalCount();
|
||||
|
||||
interface SharingListener {
|
||||
|
||||
@UiThread
|
||||
void onSharingInfoUpdated(int total, int online);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
package org.briarproject.briar.android.controller;
|
||||
|
||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.UiThread;
|
||||
|
||||
@Deprecated
|
||||
@NotNullByDefault
|
||||
public class SharingControllerImpl implements SharingController, EventListener {
|
||||
|
||||
private final EventBus eventBus;
|
||||
private final ConnectionRegistry connectionRegistry;
|
||||
|
||||
// UI thread
|
||||
private final Set<ContactId> contacts = new HashSet<>();
|
||||
|
||||
// UI thread
|
||||
@Nullable
|
||||
private SharingListener listener;
|
||||
|
||||
@Inject
|
||||
SharingControllerImpl(EventBus eventBus,
|
||||
ConnectionRegistry connectionRegistry) {
|
||||
this.eventBus = eventBus;
|
||||
this.connectionRegistry = connectionRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSharingListener(SharingListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unsetSharingListener(SharingListener listener) {
|
||||
if (this.listener == listener) this.listener = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
eventBus.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
eventBus.removeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof ContactConnectedEvent) {
|
||||
setConnected(((ContactConnectedEvent) e).getContactId());
|
||||
} else if (e instanceof ContactDisconnectedEvent) {
|
||||
setConnected(((ContactDisconnectedEvent) e).getContactId());
|
||||
}
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void setConnected(ContactId c) {
|
||||
if (listener == null) throw new IllegalStateException();
|
||||
if (contacts.contains(c)) {
|
||||
int online = getOnlineCount();
|
||||
listener.onSharingInfoUpdated(contacts.size(), online);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAll(Collection<ContactId> c) {
|
||||
contacts.addAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(ContactId c) {
|
||||
contacts.add(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(ContactId c) {
|
||||
contacts.remove(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOnlineCount() {
|
||||
int online = 0;
|
||||
for (ContactId c : contacts) {
|
||||
if (connectionRegistry.isConnected(c)) online++;
|
||||
}
|
||||
return online;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalCount() {
|
||||
return contacts.size();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package org.briarproject.briar.android.conversation;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
import android.transition.Slide;
|
||||
@@ -105,6 +106,7 @@ import androidx.recyclerview.selection.SelectionTracker.SelectionObserver;
|
||||
import androidx.recyclerview.selection.StorageStrategy;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
|
||||
import de.hdodenhof.circleimageview.CircleImageView;
|
||||
import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt;
|
||||
|
||||
@@ -479,12 +481,10 @@ public class ConversationActivity extends BriarActivity
|
||||
@UiThread
|
||||
private void displayContactOnlineStatus() {
|
||||
if (connectionRegistry.isConnected(contactId)) {
|
||||
toolbarStatus.setImageDrawable(ContextCompat.getDrawable(
|
||||
ConversationActivity.this, R.drawable.contact_online));
|
||||
toolbarStatus.setImageResource(R.drawable.contact_online);
|
||||
toolbarStatus.setContentDescription(getString(R.string.online));
|
||||
} else {
|
||||
toolbarStatus.setImageDrawable(ContextCompat.getDrawable(
|
||||
ConversationActivity.this, R.drawable.contact_offline));
|
||||
toolbarStatus.setImageResource(R.drawable.contact_offline);
|
||||
toolbarStatus.setContentDescription(getString(R.string.offline));
|
||||
}
|
||||
}
|
||||
@@ -935,13 +935,16 @@ public class ConversationActivity extends BriarActivity
|
||||
return;
|
||||
}
|
||||
|
||||
int color =
|
||||
ContextCompat.getColor(this, R.color.briar_primary);
|
||||
Drawable drawable = VectorDrawableCompat
|
||||
.create(getResources(), R.drawable.ic_more_vert_accent, null);
|
||||
new MaterialTapTargetPrompt.Builder(ConversationActivity.this,
|
||||
R.style.OnboardingDialogTheme).setTarget(target)
|
||||
.setPrimaryText(R.string.introduction_onboarding_title)
|
||||
.setSecondaryText(R.string.introduction_onboarding_text)
|
||||
.setIcon(R.drawable.ic_more_vert_accent)
|
||||
.setBackgroundColour(
|
||||
ContextCompat.getColor(this, R.color.briar_primary))
|
||||
.setIconDrawable(drawable)
|
||||
.setBackgroundColour(color)
|
||||
.show();
|
||||
}
|
||||
|
||||
|
||||
@@ -149,7 +149,7 @@ public class ConversationViewModel extends DbViewModel
|
||||
AttachmentReceivedEvent a = (AttachmentReceivedEvent) e;
|
||||
if (a.getContactId().equals(contactId)) {
|
||||
LOG.info("Attachment received");
|
||||
runOnDbThreadOrLogException(() -> attachmentRetriever
|
||||
runOnDbThread(() -> attachmentRetriever
|
||||
.loadAttachmentItem(a.getMessageId()));
|
||||
}
|
||||
} else if (e instanceof AvatarUpdatedEvent) {
|
||||
@@ -194,36 +194,44 @@ public class ConversationViewModel extends DbViewModel
|
||||
|
||||
private void loadContact(ContactId contactId) {
|
||||
runOnDbThread(() -> {
|
||||
long start = now();
|
||||
Contact c = contactManager.getContact(contactId);
|
||||
AuthorInfo authorInfo = authorManager.getAuthorInfo(c);
|
||||
contactItem.postValue(new ContactItem(c, authorInfo));
|
||||
logDuration(LOG, "Loading contact", start);
|
||||
start = now();
|
||||
checkFeaturesAndOnboarding(contactId);
|
||||
logDuration(LOG, "Checking for image support", start);
|
||||
}, e -> {
|
||||
if (e instanceof NoSuchContactException) {
|
||||
try {
|
||||
long start = now();
|
||||
Contact c = contactManager.getContact(contactId);
|
||||
AuthorInfo authorInfo = authorManager.getAuthorInfo(c);
|
||||
contactItem.postValue(new ContactItem(c, authorInfo));
|
||||
logDuration(LOG, "Loading contact", start);
|
||||
start = now();
|
||||
checkFeaturesAndOnboarding(contactId);
|
||||
logDuration(LOG, "Checking for image support", start);
|
||||
} catch (NoSuchContactException e) {
|
||||
contactDeleted.postValue(true);
|
||||
} else {
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void markMessageRead(GroupId g, MessageId m) {
|
||||
runOnDbThreadOrLogException(() -> {
|
||||
long start = now();
|
||||
messagingManager.setReadFlag(g, m, true);
|
||||
logDuration(LOG, "Marking read", start);
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
long start = now();
|
||||
messagingManager.setReadFlag(g, m, true);
|
||||
logDuration(LOG, "Marking read", start);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void setContactAlias(String alias) {
|
||||
runOnDbThreadOrLogException(() -> {
|
||||
contactManager.setContactAlias(requireNonNull(contactId),
|
||||
alias.isEmpty() ? null : alias);
|
||||
loadContact(contactId);
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
contactManager.setContactAlias(requireNonNull(contactId),
|
||||
alias.isEmpty() ? null : alias);
|
||||
loadContact(contactId);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -319,17 +327,21 @@ public class ConversationViewModel extends DbViewModel
|
||||
@UiThread
|
||||
private void storeMessage(PrivateMessage m) {
|
||||
attachmentCreator.onAttachmentsSent(m.getMessage().getId());
|
||||
runOnDbThreadOrLogException(() -> {
|
||||
long start = now();
|
||||
messagingManager.addLocalMessage(m);
|
||||
logDuration(LOG, "Storing message", start);
|
||||
Message message = m.getMessage();
|
||||
PrivateMessageHeader h = new PrivateMessageHeader(
|
||||
message.getId(), message.getGroupId(),
|
||||
message.getTimestamp(), true, true, false, false,
|
||||
m.hasText(), m.getAttachmentHeaders());
|
||||
// TODO add text to cache when available here
|
||||
addedHeader.postEvent(h);
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
long start = now();
|
||||
messagingManager.addLocalMessage(m);
|
||||
logDuration(LOG, "Storing message", start);
|
||||
Message message = m.getMessage();
|
||||
PrivateMessageHeader h = new PrivateMessageHeader(
|
||||
message.getId(), message.getGroupId(),
|
||||
message.getTimestamp(), true, true, false, false,
|
||||
m.hasText(), m.getAttachmentHeaders());
|
||||
// TODO add text to cache when available here
|
||||
addedHeader.postEvent(h);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -371,7 +383,12 @@ public class ConversationViewModel extends DbViewModel
|
||||
|
||||
@UiThread
|
||||
void recheckFeaturesAndOnboarding(ContactId contactId) {
|
||||
runOnDbThreadOrLogException(() ->
|
||||
checkFeaturesAndOnboarding(contactId));
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
checkFeaturesAndOnboarding(contactId);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,10 +24,7 @@ import org.briarproject.briar.android.attachment.AttachmentItem;
|
||||
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
|
||||
import org.briarproject.briar.android.view.PullDownLayout;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@@ -293,13 +290,10 @@ public class ImageActivity extends BriarActivity
|
||||
|
||||
@RequiresApi(api = 19)
|
||||
private Intent getCreationIntent() {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",
|
||||
Locale.getDefault());
|
||||
String fileName = sdf.format(new Date());
|
||||
Intent intent = new Intent(ACTION_CREATE_DOCUMENT);
|
||||
intent.addCategory(CATEGORY_OPENABLE);
|
||||
intent.setType(getVisibleAttachment().getMimeType());
|
||||
intent.putExtra(EXTRA_TITLE, fileName);
|
||||
intent.putExtra(EXTRA_TITLE, viewModel.getFileName());
|
||||
return intent;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.net.Uri;
|
||||
import android.view.View;
|
||||
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.TransactionManager;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
@@ -197,12 +198,14 @@ public class ImageViewModel extends DbViewModel implements EventListener {
|
||||
private void saveImage(AttachmentItem attachment, OutputStreamProvider osp,
|
||||
@Nullable Runnable afterCopy) {
|
||||
runOnDbThread(() -> {
|
||||
Attachment a =
|
||||
attachmentReader.getAttachment(attachment.getHeader());
|
||||
copyImageFromDb(a, osp, afterCopy);
|
||||
}, e -> {
|
||||
logException(LOG, WARNING, e);
|
||||
saveState.postEvent(true);
|
||||
try {
|
||||
Attachment a =
|
||||
attachmentReader.getAttachment(attachment.getHeader());
|
||||
copyImageFromDb(a, osp, afterCopy);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
saveState.postEvent(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -222,8 +225,8 @@ public class ImageViewModel extends DbViewModel implements EventListener {
|
||||
});
|
||||
}
|
||||
|
||||
private String getFileName() {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",
|
||||
String getFileName() {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",
|
||||
Locale.getDefault());
|
||||
return sdf.format(new Date());
|
||||
}
|
||||
|
||||
@@ -79,12 +79,6 @@ public class ForumActivity extends
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
viewModel.clearForumPostNotification();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int request, int result,
|
||||
@Nullable Intent data) {
|
||||
@@ -109,16 +103,16 @@ public class ForumActivity extends
|
||||
// Handle presses on the action bar items
|
||||
int itemId = item.getItemId();
|
||||
if (itemId == R.id.action_forum_share) {
|
||||
Intent i2 = new Intent(this, ShareForumActivity.class);
|
||||
i2.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
|
||||
i2.putExtra(GROUP_ID, groupId.getBytes());
|
||||
startActivityForResult(i2, REQUEST_SHARE_FORUM);
|
||||
Intent i = new Intent(this, ShareForumActivity.class);
|
||||
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
|
||||
i.putExtra(GROUP_ID, groupId.getBytes());
|
||||
startActivityForResult(i, REQUEST_SHARE_FORUM);
|
||||
return true;
|
||||
} else if (itemId == R.id.action_forum_sharing_status) {
|
||||
Intent i3 = new Intent(this, ForumSharingStatusActivity.class);
|
||||
i3.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
|
||||
i3.putExtra(GROUP_ID, groupId.getBytes());
|
||||
startActivity(i3);
|
||||
Intent i = new Intent(this, ForumSharingStatusActivity.class);
|
||||
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
|
||||
i.putExtra(GROUP_ID, groupId.getBytes());
|
||||
startActivity(i);
|
||||
return true;
|
||||
} else if (itemId == R.id.action_forum_delete) {
|
||||
showUnsubscribeDialog();
|
||||
|
||||
@@ -40,8 +40,10 @@ import androidx.annotation.UiThread;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
import static org.briarproject.briar.api.forum.ForumManager.CLIENT_ID;
|
||||
|
||||
@@ -143,7 +145,7 @@ class ForumListViewModel extends DbViewModel implements EventListener {
|
||||
|
||||
@UiThread
|
||||
private void onForumPostReceived(GroupId g, ForumPostHeader header) {
|
||||
List<ForumListItem> list = updateListItems(forumItems,
|
||||
List<ForumListItem> list = updateListItems(getList(forumItems),
|
||||
itemToTest -> itemToTest.getForum().getId().equals(g),
|
||||
itemToUpdate -> new ForumListItem(itemToUpdate, header));
|
||||
if (list == null) return;
|
||||
@@ -154,19 +156,21 @@ class ForumListViewModel extends DbViewModel implements EventListener {
|
||||
|
||||
@UiThread
|
||||
private void onGroupRemoved(GroupId groupId) {
|
||||
List<ForumListItem> list = removeListItems(forumItems, i ->
|
||||
removeAndUpdateListItems(forumItems, i ->
|
||||
i.getForum().getId().equals(groupId)
|
||||
);
|
||||
if (list == null) return;
|
||||
forumItems.setValue(new LiveResult<>(list));
|
||||
}
|
||||
|
||||
void loadForumInvitations() {
|
||||
runOnDbThreadOrLogException(() -> {
|
||||
long start = now();
|
||||
int available = forumSharingManager.getInvitations().size();
|
||||
logDuration(LOG, "Loading available", start);
|
||||
numInvitations.postValue(available);
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
long start = now();
|
||||
int available = forumSharingManager.getInvitations().size();
|
||||
logDuration(LOG, "Loading available", start);
|
||||
numInvitations.postValue(available);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ import org.briarproject.briar.android.threaded.ThreadListViewModel;
|
||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||
import org.briarproject.briar.api.client.MessageTracker;
|
||||
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
|
||||
import org.briarproject.briar.api.client.PostHeader;
|
||||
import org.briarproject.briar.api.forum.Forum;
|
||||
import org.briarproject.briar.api.forum.ForumInvitationResponse;
|
||||
import org.briarproject.briar.api.forum.ForumManager;
|
||||
@@ -51,8 +50,10 @@ import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import static android.widget.Toast.LENGTH_SHORT;
|
||||
import static java.lang.Math.max;
|
||||
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;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@@ -92,8 +93,9 @@ class ForumViewModel extends ThreadListViewModel<ForumPostItem> {
|
||||
ForumPostReceivedEvent f = (ForumPostReceivedEvent) e;
|
||||
if (f.getGroupId().equals(groupId)) {
|
||||
LOG.info("Forum post received, adding...");
|
||||
ForumPostItem item = buildItem(f.getHeader(), f.getText());
|
||||
addItem(item);
|
||||
ForumPostItem item =
|
||||
new ForumPostItem(f.getHeader(), f.getText());
|
||||
addItem(item, false);
|
||||
}
|
||||
} else if (e instanceof ForumInvitationResponseReceivedEvent) {
|
||||
ForumInvitationResponseReceivedEvent f =
|
||||
@@ -114,14 +116,20 @@ class ForumViewModel extends ThreadListViewModel<ForumPostItem> {
|
||||
}
|
||||
}
|
||||
|
||||
void clearForumPostNotification() {
|
||||
protected void clearNotifications() {
|
||||
notificationManager.clearForumPostNotification(groupId);
|
||||
}
|
||||
|
||||
LiveData<Forum> loadForum() {
|
||||
MutableLiveData<Forum> forum = new MutableLiveData<>();
|
||||
runOnDbThreadOrLogException(() ->
|
||||
forum.postValue(forumManager.getForum(groupId)));
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
Forum f = forumManager.getForum(groupId);
|
||||
forum.postValue(f);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
return forum;
|
||||
}
|
||||
|
||||
@@ -132,19 +140,35 @@ class ForumViewModel extends ThreadListViewModel<ForumPostItem> {
|
||||
List<ForumPostHeader> headers =
|
||||
forumManager.getPostHeaders(txn, groupId);
|
||||
logDuration(LOG, "Loading headers", start);
|
||||
return createItems(txn, headers, this::buildItem);
|
||||
start = now();
|
||||
List<ForumPostItem> items = new ArrayList<>();
|
||||
for (ForumPostHeader header : headers) {
|
||||
items.add(loadItem(txn, header));
|
||||
}
|
||||
logDuration(LOG, "Loading bodies and creating items", start);
|
||||
return items;
|
||||
}, this::setItems);
|
||||
}
|
||||
|
||||
private ForumPostItem loadItem(Transaction txn, ForumPostHeader header)
|
||||
throws DbException {
|
||||
String text = forumManager.getPostText(txn, header.getId());
|
||||
return new ForumPostItem(header, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createAndStoreMessage(String text,
|
||||
@Nullable MessageId parentId) {
|
||||
runOnDbThreadOrLogException(() -> {
|
||||
LocalAuthor author = identityManager.getLocalAuthor();
|
||||
GroupCount count = forumManager.getGroupCount(groupId);
|
||||
long timestamp = max(count.getLatestMsgTime() + 1,
|
||||
clock.currentTimeMillis());
|
||||
createMessage(text, timestamp, parentId, author);
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
LocalAuthor author = identityManager.getLocalAuthor();
|
||||
GroupCount count = forumManager.getGroupCount(groupId);
|
||||
long timestamp = max(count.getLatestMsgTime() + 1,
|
||||
clock.currentTimeMillis());
|
||||
createMessage(text, timestamp, parentId, author);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -159,43 +183,47 @@ class ForumViewModel extends ThreadListViewModel<ForumPostItem> {
|
||||
}
|
||||
|
||||
private void storePost(ForumPost msg, String text) {
|
||||
runOnDbThreadOrLogException(() -> {
|
||||
runOnDbThread(false, txn -> {
|
||||
long start = now();
|
||||
ForumPostHeader header = forumManager.addLocalPost(msg);
|
||||
addItemAsync(buildItem(header, text));
|
||||
ForumPostHeader header = forumManager.addLocalPost(txn, msg);
|
||||
logDuration(LOG, "Storing forum post", start);
|
||||
});
|
||||
}
|
||||
|
||||
private ForumPostItem buildItem(ForumPostHeader header, String text) {
|
||||
return new ForumPostItem(header, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String loadMessageText(Transaction txn, PostHeader header)
|
||||
throws DbException {
|
||||
return forumManager.getPostText(txn, header.getId());
|
||||
txn.attach(() -> {
|
||||
ForumPostItem item = new ForumPostItem(header, text);
|
||||
addItem(item, true);
|
||||
});
|
||||
}, e -> logException(LOG, WARNING, e));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void markItemRead(ForumPostItem item) {
|
||||
runOnDbThreadOrLogException(() ->
|
||||
forumManager.setReadFlag(groupId, item.getId(), true));
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
forumManager.setReadFlag(groupId, item.getId(), true);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void loadSharingContacts() {
|
||||
runOnDbThreadOrLogException(true, txn -> {
|
||||
runOnDbThread(true, txn -> {
|
||||
Collection<Contact> contacts =
|
||||
forumSharingManager.getSharedWith(txn, groupId);
|
||||
Collection<ContactId> contactIds = new ArrayList<>(contacts.size());
|
||||
for (Contact c : contacts) contactIds.add(c.getId());
|
||||
txn.attach(() -> sharingController.addAll(contactIds));
|
||||
});
|
||||
}, e -> logException(LOG, WARNING, e));
|
||||
}
|
||||
|
||||
void deleteForum() {
|
||||
runOnDbThreadOrLogException(() ->
|
||||
forumManager.removeForum(forumManager.getForum(groupId)));
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
Forum f = forumManager.getForum(groupId);
|
||||
forumManager.removeForum(f);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
Toast.makeText(getApplication(), R.string.forum_left_toast,
|
||||
LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@@ -10,9 +10,8 @@ import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.briar.android.DestroyableContext;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import androidx.annotation.CallSuper;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
@@ -47,13 +46,11 @@ public abstract class BaseFragment extends Fragment
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
listener.onBackPressed();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
if (item.getItemId() == android.R.id.home) {
|
||||
requireActivity().onBackPressed();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@UiThread
|
||||
@@ -79,6 +76,7 @@ public abstract class BaseFragment extends Fragment
|
||||
void handleException(Exception e);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@CallSuper
|
||||
@Override
|
||||
public void runOnUiThreadUnlessDestroyed(Runnable r) {
|
||||
|
||||
@@ -5,74 +5,41 @@ import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
import org.briarproject.briar.android.contact.ContactListAdapter;
|
||||
import org.briarproject.briar.android.contact.ContactListItem;
|
||||
import org.briarproject.briar.android.contact.LegacyContactListAdapter;
|
||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
|
||||
import org.briarproject.briar.api.conversation.ConversationManager;
|
||||
import org.briarproject.briar.api.identity.AuthorInfo;
|
||||
import org.briarproject.briar.api.identity.AuthorManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.briar.android.conversation.ConversationActivity.CONTACT_ID;
|
||||
|
||||
@UiThread
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class ContactChooserFragment extends BaseFragment {
|
||||
public class ContactChooserFragment extends BaseFragment
|
||||
implements OnContactClickListener<ContactListItem> {
|
||||
|
||||
public static final String TAG = ContactChooserFragment.class.getName();
|
||||
private static final Logger LOG = Logger.getLogger(TAG);
|
||||
private static final String TAG = ContactChooserFragment.class.getName();
|
||||
|
||||
@Inject
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
|
||||
private IntroductionViewModel viewModel;
|
||||
private final ContactListAdapter adapter = new ContactListAdapter(this);
|
||||
private BriarRecyclerView list;
|
||||
private LegacyContactListAdapter adapter;
|
||||
private ContactId contactId;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
private volatile Contact c1;
|
||||
@Inject
|
||||
volatile ContactManager contactManager;
|
||||
@Inject
|
||||
volatile AuthorManager authorManager;
|
||||
@Inject
|
||||
volatile ConversationManager conversationManager;
|
||||
@Inject
|
||||
volatile ConnectionRegistry connectionRegistry;
|
||||
|
||||
public static ContactChooserFragment newInstance(ContactId id) {
|
||||
Bundle args = new Bundle();
|
||||
|
||||
ContactChooserFragment fragment = new ContactChooserFragment();
|
||||
args.putInt(CONTACT_ID, id.getInt());
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectFragment(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory)
|
||||
.get(IntroductionViewModel.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -80,23 +47,20 @@ public class ContactChooserFragment extends BaseFragment {
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
|
||||
View contentView = inflater.inflate(R.layout.list, container, false);
|
||||
// change toolbar text (relevant when navigating back to this fragment)
|
||||
requireActivity().setTitle(R.string.introduction_activity_title);
|
||||
|
||||
OnContactClickListener<ContactListItem> onContactClickListener =
|
||||
(view, item) -> {
|
||||
if (c1 == null) throw new IllegalStateException();
|
||||
Contact c2 = item.getContact();
|
||||
showMessageScreen(c1, c2);
|
||||
};
|
||||
adapter = new LegacyContactListAdapter(requireActivity(),
|
||||
onContactClickListener);
|
||||
View contentView = inflater.inflate(R.layout.list, container, false);
|
||||
|
||||
list = contentView.findViewById(R.id.list);
|
||||
list.setLayoutManager(new LinearLayoutManager(getActivity()));
|
||||
list.setAdapter(adapter);
|
||||
list.setEmptyText(R.string.no_contacts);
|
||||
|
||||
contactId = new ContactId(requireArguments().getInt(CONTACT_ID));
|
||||
viewModel.getContactListItems().observe(getViewLifecycleOwner(),
|
||||
result -> result.onError(this::handleException)
|
||||
.onSuccess(adapter::submitList)
|
||||
);
|
||||
|
||||
return contentView;
|
||||
}
|
||||
@@ -104,14 +68,13 @@ public class ContactChooserFragment extends BaseFragment {
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
loadContacts();
|
||||
list.startPeriodicUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
adapter.clear();
|
||||
list.showProgressBar();
|
||||
list.stopPeriodicUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -119,43 +82,9 @@ public class ContactChooserFragment extends BaseFragment {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
private void loadContacts() {
|
||||
listener.runOnDbThread(() -> {
|
||||
try {
|
||||
List<ContactListItem> contacts = new ArrayList<>();
|
||||
for (Contact c : contactManager.getContacts()) {
|
||||
if (c.getId().equals(contactId)) {
|
||||
c1 = c;
|
||||
} else {
|
||||
AuthorInfo authorInfo = authorManager.getAuthorInfo(c);
|
||||
ContactId id = c.getId();
|
||||
GroupCount count =
|
||||
conversationManager.getGroupCount(id);
|
||||
boolean connected =
|
||||
connectionRegistry.isConnected(c.getId());
|
||||
contacts.add(new ContactListItem(c, authorInfo,
|
||||
connected, count));
|
||||
}
|
||||
}
|
||||
displayContacts(contacts);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public void onItemClick(View view, ContactListItem item) {
|
||||
viewModel.setSecondContactId(item.getContact().getId());
|
||||
viewModel.triggerContactSelected();
|
||||
}
|
||||
|
||||
private void displayContacts(List<ContactListItem> contacts) {
|
||||
runOnUiThreadUnlessDestroyed(() -> {
|
||||
if (contacts.isEmpty()) list.showData();
|
||||
else adapter.addAll(contacts);
|
||||
});
|
||||
}
|
||||
|
||||
private void showMessageScreen(Contact c1, Contact c2) {
|
||||
IntroductionMessageFragment messageFragment =
|
||||
IntroductionMessageFragment
|
||||
.newInstance(c1.getId().getInt(), c2.getId().getInt());
|
||||
showNextFragment(messageFragment);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,30 +9,67 @@ import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.activity.BriarActivity;
|
||||
import org.briarproject.briar.android.fragment.BaseFragment.BaseFragmentListener;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import static org.briarproject.briar.android.conversation.ConversationActivity.CONTACT_ID;
|
||||
|
||||
public class IntroductionActivity extends BriarActivity
|
||||
implements BaseFragmentListener {
|
||||
|
||||
@Inject
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
|
||||
private IntroductionViewModel viewModel;
|
||||
|
||||
@Override
|
||||
public void injectActivity(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
viewModel = new ViewModelProvider(this, viewModelFactory)
|
||||
.get(IntroductionViewModel.class);
|
||||
}
|
||||
|
||||
private static final String BUNDLE_CONTACT2 = "contact2";
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
Intent intent = getIntent();
|
||||
int id = intent.getIntExtra(CONTACT_ID, -1);
|
||||
if (id == -1) throw new IllegalStateException("No ContactId");
|
||||
ContactId contactId = new ContactId(id);
|
||||
int contactId1 = intent.getIntExtra(CONTACT_ID, -1);
|
||||
if (contactId1 == -1)
|
||||
throw new IllegalStateException("No ContactId");
|
||||
ContactId firstContactId = new ContactId(contactId1);
|
||||
|
||||
viewModel.setFirstContactId(firstContactId);
|
||||
|
||||
setContentView(R.layout.activity_fragment_container);
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
showInitialFragment(ContactChooserFragment.newInstance(contactId));
|
||||
showInitialFragment(new ContactChooserFragment());
|
||||
} else {
|
||||
int contactId2 = savedInstanceState.getInt(BUNDLE_CONTACT2);
|
||||
ContactId secondContactId = new ContactId(contactId2);
|
||||
viewModel.setSecondContactId(secondContactId);
|
||||
}
|
||||
|
||||
viewModel.getSecondContactSelected().observeEvent(this, e -> {
|
||||
IntroductionMessageFragment fragment =
|
||||
new IntroductionMessageFragment();
|
||||
showNextFragment(fragment);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectActivity(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
|
||||
ContactId secondContactId = viewModel.getSecondContactId();
|
||||
if (secondContactId != null) {
|
||||
outState.putInt(BUNDLE_CONTACT2, secondContactId.getInt());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.briarproject.briar.android.introduction;
|
||||
|
||||
import org.briarproject.briar.android.contact.ContactItem;
|
||||
|
||||
class IntroductionInfo {
|
||||
private final ContactItem c1;
|
||||
private final ContactItem c2;
|
||||
private final boolean possible;
|
||||
|
||||
IntroductionInfo(ContactItem c1, ContactItem c2,
|
||||
boolean possible) {
|
||||
this.c1 = c1;
|
||||
this.c2 = c2;
|
||||
this.possible = possible;
|
||||
}
|
||||
|
||||
ContactItem getContact1() {
|
||||
return c1;
|
||||
}
|
||||
|
||||
ContactItem getContact2() {
|
||||
return c2;
|
||||
}
|
||||
|
||||
boolean isPossible() {
|
||||
return possible;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.briarproject.briar.android.introduction;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
@@ -8,12 +7,7 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
@@ -24,25 +18,19 @@ 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.attachment.AttachmentHeader;
|
||||
import org.briarproject.briar.api.identity.AuthorInfo;
|
||||
import org.briarproject.briar.api.identity.AuthorManager;
|
||||
import org.briarproject.briar.api.introduction.IntroductionManager;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import de.hdodenhof.circleimageview.CircleImageView;
|
||||
|
||||
import static android.app.Activity.RESULT_OK;
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static android.widget.Toast.LENGTH_SHORT;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getContactDisplayName;
|
||||
import static org.briarproject.briar.android.util.UiUtils.hideSoftKeyboard;
|
||||
import static org.briarproject.briar.android.view.AuthorView.setAvatar;
|
||||
@@ -53,45 +41,21 @@ import static org.briarproject.briar.api.introduction.IntroductionConstants.MAX_
|
||||
public class IntroductionMessageFragment extends BaseFragment
|
||||
implements SendListener {
|
||||
|
||||
public static final String TAG =
|
||||
private static final String TAG =
|
||||
IntroductionMessageFragment.class.getName();
|
||||
private static final Logger LOG = Logger.getLogger(TAG);
|
||||
|
||||
private final static String CONTACT_ID_1 = "contact1";
|
||||
private final static String CONTACT_ID_2 = "contact2";
|
||||
@Inject
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
|
||||
private IntroductionViewModel viewModel;
|
||||
|
||||
private IntroductionActivity introductionActivity;
|
||||
private ViewHolder ui;
|
||||
private Contact contact1, contact2;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject
|
||||
protected volatile ContactManager contactManager;
|
||||
@Inject
|
||||
protected volatile AuthorManager authorManager;
|
||||
@Inject
|
||||
protected volatile IntroductionManager introductionManager;
|
||||
|
||||
public static IntroductionMessageFragment newInstance(int contactId1,
|
||||
int contactId2) {
|
||||
Bundle args = new Bundle();
|
||||
args.putInt(CONTACT_ID_1, contactId1);
|
||||
args.putInt(CONTACT_ID_2, contactId2);
|
||||
IntroductionMessageFragment fragment =
|
||||
new IntroductionMessageFragment();
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
introductionActivity = (IntroductionActivity) context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectFragment(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory)
|
||||
.get(IntroductionViewModel.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -100,18 +64,7 @@ public class IntroductionMessageFragment extends BaseFragment
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
|
||||
// change toolbar text
|
||||
ActionBar actionBar = introductionActivity.getSupportActionBar();
|
||||
if (actionBar != null) {
|
||||
actionBar.setTitle(R.string.introduction_message_title);
|
||||
}
|
||||
|
||||
// get contact IDs from fragment arguments
|
||||
Bundle args = requireArguments();
|
||||
int contactId1 = args.getInt(CONTACT_ID_1, -1);
|
||||
int contactId2 = args.getInt(CONTACT_ID_2, -1);
|
||||
if (contactId1 == -1 || contactId2 == -1) {
|
||||
throw new AssertionError("Use newInstance() to instantiate");
|
||||
}
|
||||
requireActivity().setTitle(R.string.introduction_message_title);
|
||||
|
||||
// inflate view
|
||||
View v = inflater.inflate(R.layout.introduction_message, container,
|
||||
@@ -123,69 +76,44 @@ public class IntroductionMessageFragment extends BaseFragment
|
||||
ui.message.setMaxTextLength(MAX_INTRODUCTION_TEXT_LENGTH);
|
||||
ui.message.setReady(false);
|
||||
|
||||
// get contacts and then show view
|
||||
prepareToSetUpViews(contactId1, contactId2);
|
||||
viewModel.getIntroductionInfo().observe(getViewLifecycleOwner(), ii -> {
|
||||
if (ii == null) {
|
||||
return;
|
||||
}
|
||||
setUpViews(ii.getContact1(), ii.getContact2(),
|
||||
ii.isPossible());
|
||||
});
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUniqueTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
private void prepareToSetUpViews(int contactId1, int contactId2) {
|
||||
introductionActivity.runOnDbThread(() -> {
|
||||
try {
|
||||
Contact contact1 =
|
||||
contactManager.getContact(new ContactId(contactId1));
|
||||
Contact contact2 =
|
||||
contactManager.getContact(new ContactId(contactId2));
|
||||
AuthorInfo a1 = authorManager.getAuthorInfo(contact1);
|
||||
AuthorInfo a2 = authorManager.getAuthorInfo(contact2);
|
||||
boolean possible =
|
||||
introductionManager.canIntroduce(contact1, contact2);
|
||||
ContactItem c1 = new ContactItem(contact1, a1);
|
||||
ContactItem c2 = new ContactItem(contact2, a2);
|
||||
setUpViews(c1, c2, possible);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setUpViews(ContactItem c1, ContactItem c2, boolean possible) {
|
||||
introductionActivity.runOnUiThreadUnlessDestroyed(() -> {
|
||||
contact1 = c1.getContact();
|
||||
contact2 = c2.getContact();
|
||||
// set avatars
|
||||
setAvatar(ui.avatar1, c1);
|
||||
setAvatar(ui.avatar2, c2);
|
||||
|
||||
// set avatars
|
||||
setAvatar(ui.avatar1, c1);
|
||||
setAvatar(ui.avatar2, c2);
|
||||
// set contact names
|
||||
ui.contactName1.setText(getContactDisplayName(c1.getContact()));
|
||||
ui.contactName2.setText(getContactDisplayName(c2.getContact()));
|
||||
|
||||
// set contact names
|
||||
ui.contactName1.setText(getContactDisplayName(c1.getContact()));
|
||||
ui.contactName2.setText(getContactDisplayName(c2.getContact()));
|
||||
// hide progress bar
|
||||
ui.progressBar.setVisibility(GONE);
|
||||
|
||||
// hide progress bar
|
||||
ui.progressBar.setVisibility(GONE);
|
||||
|
||||
if (possible) {
|
||||
// show views
|
||||
ui.notPossible.setVisibility(GONE);
|
||||
ui.message.setVisibility(VISIBLE);
|
||||
ui.message.setReady(true);
|
||||
ui.message.showSoftKeyboard();
|
||||
} else {
|
||||
ui.notPossible.setVisibility(VISIBLE);
|
||||
ui.message.setVisibility(GONE);
|
||||
}
|
||||
});
|
||||
if (possible) {
|
||||
// show views
|
||||
ui.notPossible.setVisibility(GONE);
|
||||
ui.message.setVisibility(VISIBLE);
|
||||
ui.message.setReady(true);
|
||||
ui.message.showSoftKeyboard();
|
||||
} else {
|
||||
ui.notPossible.setVisibility(VISIBLE);
|
||||
ui.message.setVisibility(GONE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -193,7 +121,7 @@ public class IntroductionMessageFragment extends BaseFragment
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
hideSoftKeyboard(ui.message);
|
||||
introductionActivity.onBackPressed();
|
||||
requireActivity().onBackPressed();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
@@ -206,32 +134,13 @@ public class IntroductionMessageFragment extends BaseFragment
|
||||
// disable button to prevent accidental double invitations
|
||||
ui.message.setReady(false);
|
||||
|
||||
makeIntroduction(contact1, contact2, text);
|
||||
viewModel.makeIntroduction(text);
|
||||
|
||||
// don't wait for the introduction to be made before finishing activity
|
||||
hideSoftKeyboard(ui.message);
|
||||
introductionActivity.setResult(RESULT_OK);
|
||||
introductionActivity.supportFinishAfterTransition();
|
||||
}
|
||||
|
||||
private void makeIntroduction(Contact c1, Contact c2,
|
||||
@Nullable String text) {
|
||||
introductionActivity.runOnDbThread(() -> {
|
||||
// actually make the introduction
|
||||
try {
|
||||
long timestamp = System.currentTimeMillis();
|
||||
introductionManager.makeIntroduction(c1, c2, text, timestamp);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
introductionError();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void introductionError() {
|
||||
introductionActivity.runOnUiThreadUnlessDestroyed(
|
||||
() -> Toast.makeText(introductionActivity,
|
||||
R.string.introduction_error, LENGTH_SHORT).show());
|
||||
FragmentActivity activity = requireActivity();
|
||||
activity.setResult(RESULT_OK);
|
||||
activity.supportFinishAfterTransition();
|
||||
}
|
||||
|
||||
private static class ViewHolder {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user