mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Save recovered shards in sharedPreferences
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package org.briarproject.briar.android.socialbackup.recover;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
@@ -14,6 +16,7 @@ import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||
import org.briarproject.briar.android.contact.add.nearby.QrCodeUtils;
|
||||
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
||||
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
||||
import org.briarproject.briar.api.socialbackup.MessageEncoder;
|
||||
import org.briarproject.briar.api.socialbackup.ReturnShardPayload;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.RestoreAccount;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.SecretOwnerTask;
|
||||
@@ -22,6 +25,7 @@ import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.HashSet;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -50,6 +54,7 @@ class OwnerReturnShardViewModel extends AndroidViewModel
|
||||
private final Executor ioExecutor;
|
||||
private final SecretOwnerTask task;
|
||||
private final RestoreAccount restoreAccount;
|
||||
private final SharedPreferences prefs;
|
||||
|
||||
private final MutableLiveEvent<Boolean> errorTryAgain =
|
||||
new MutableLiveEvent<>();
|
||||
@@ -65,18 +70,25 @@ class OwnerReturnShardViewModel extends AndroidViewModel
|
||||
private Bitmap qrCodeBitmap;
|
||||
private WifiManager wifiManager;
|
||||
private SecretKey secretKey;
|
||||
private final MessageEncoder messageEncoder;
|
||||
|
||||
@Inject
|
||||
OwnerReturnShardViewModel(Application app,
|
||||
AndroidExecutor androidExecutor,
|
||||
SecretOwnerTask task,
|
||||
RestoreAccount restoreAccount,
|
||||
@IoExecutor Executor ioExecutor) {
|
||||
@IoExecutor Executor ioExecutor,
|
||||
MessageEncoder messageEncoder) {
|
||||
super(app);
|
||||
this.androidExecutor = androidExecutor;
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.restoreAccount = restoreAccount;
|
||||
this.messageEncoder = messageEncoder;
|
||||
this.task = task;
|
||||
this.prefs = app.getSharedPreferences("account-recovery",
|
||||
Context.MODE_PRIVATE);
|
||||
restoreAccount.restoreFromPrevious(prefs.getStringSet("Recover", new HashSet<>()));
|
||||
|
||||
wifiManager = (WifiManager) app.getSystemService(WIFI_SERVICE);
|
||||
|
||||
// IntentFilter filter = new IntentFilter(ACTION_SCAN_MODE_CHANGED);
|
||||
@@ -227,7 +239,11 @@ class OwnerReturnShardViewModel extends AndroidViewModel
|
||||
}
|
||||
|
||||
public RestoreAccount.AddReturnShardPayloadResult addToShardSet(ReturnShardPayload toAdd) {
|
||||
return restoreAccount.addReturnShardPayload(toAdd);
|
||||
RestoreAccount.AddReturnShardPayloadResult result = restoreAccount.addReturnShardPayload(toAdd);
|
||||
if (result == RestoreAccount.AddReturnShardPayloadResult.OK) {
|
||||
prefs.edit().putStringSet("recovered", restoreAccount.getEncodedShards()).apply();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean canRecover() {
|
||||
|
||||
@@ -6,17 +6,21 @@ import org.briarproject.briar.api.socialbackup.ReturnShardPayload;
|
||||
import org.briarproject.briar.api.socialbackup.SocialBackup;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.Set;
|
||||
|
||||
public interface RestoreAccount {
|
||||
|
||||
enum AddReturnShardPayloadResult {
|
||||
DUPLICATE,
|
||||
MISMATCH,
|
||||
OK
|
||||
OK,
|
||||
RECOVERED
|
||||
}
|
||||
|
||||
int getNumberOfShards();
|
||||
|
||||
Set<String> getEncodedShards();
|
||||
|
||||
AddReturnShardPayloadResult addReturnShardPayload(ReturnShardPayload toAdd);
|
||||
|
||||
boolean canRecover();
|
||||
@@ -26,4 +30,6 @@ public interface RestoreAccount {
|
||||
SocialBackup getSocialBackup();
|
||||
|
||||
void addContactsToDb() throws DbException;
|
||||
|
||||
void restoreFromPrevious(Set<String> previousShards);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.briarproject.briar.socialbackup.recovery;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.client.ClientHelper;
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.contact.ContactManager;
|
||||
@@ -14,6 +15,8 @@ import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
||||
import org.briarproject.briar.api.socialbackup.BackupPayload;
|
||||
import org.briarproject.briar.api.socialbackup.ContactData;
|
||||
import org.briarproject.briar.api.socialbackup.DarkCrystal;
|
||||
import org.briarproject.briar.api.socialbackup.MessageEncoder;
|
||||
import org.briarproject.briar.api.socialbackup.MessageParser;
|
||||
import org.briarproject.briar.api.socialbackup.ReturnShardPayload;
|
||||
import org.briarproject.briar.api.socialbackup.Shard;
|
||||
import org.briarproject.briar.api.socialbackup.SocialBackup;
|
||||
@@ -23,6 +26,8 @@ import org.briarproject.briar.socialbackup.BackupPayloadDecoder;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -31,15 +36,18 @@ import javax.inject.Inject;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
|
||||
public class RestoreAccountImpl implements RestoreAccount {
|
||||
private final ArrayList<ReturnShardPayload> recoveredShards = new ArrayList<>();
|
||||
private final Set<ReturnShardPayload> recoveredShards = new HashSet<>();
|
||||
private final DarkCrystal darkCrystal;
|
||||
private final Executor ioExecutor;
|
||||
private final DatabaseComponent db;
|
||||
private final ContactManager contactManager;
|
||||
private final MessageEncoder messageEncoder;
|
||||
private final MessageParser messageParser;
|
||||
private final TransportPropertyManager transportPropertyManager;
|
||||
private final LifecycleManager lifecycleManager;
|
||||
private SecretKey secretKey;
|
||||
private final BackupPayloadDecoder backupPayloadDecoder;
|
||||
private final ClientHelper clientHelper;
|
||||
private SocialBackup socialBackup;
|
||||
private byte[] secretId;
|
||||
|
||||
@@ -52,7 +60,10 @@ public class RestoreAccountImpl implements RestoreAccount {
|
||||
@IoExecutor Executor ioExecutor,
|
||||
ContactManager contactManager,
|
||||
LifecycleManager lifecycleManager,
|
||||
TransportPropertyManager transportPropertyManager) {
|
||||
TransportPropertyManager transportPropertyManager,
|
||||
MessageEncoder messageEncoder,
|
||||
MessageParser messageParser,
|
||||
ClientHelper clientHelper) {
|
||||
this.darkCrystal = darkCrystal;
|
||||
this.backupPayloadDecoder = backupPayloadDecoder;
|
||||
this.db = db;
|
||||
@@ -60,6 +71,9 @@ public class RestoreAccountImpl implements RestoreAccount {
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.contactManager = contactManager;
|
||||
this.transportPropertyManager = transportPropertyManager;
|
||||
this.messageEncoder = messageEncoder;
|
||||
this.messageParser = messageParser;
|
||||
this.clientHelper = clientHelper;
|
||||
}
|
||||
|
||||
public int getNumberOfShards() {
|
||||
@@ -80,7 +94,7 @@ public class RestoreAccountImpl implements RestoreAccount {
|
||||
return AddReturnShardPayloadResult.MISMATCH;
|
||||
}
|
||||
recoveredShards.add(toAdd);
|
||||
return AddReturnShardPayloadResult.OK;
|
||||
return canRecover() ? AddReturnShardPayloadResult.RECOVERED : AddReturnShardPayloadResult.OK;
|
||||
}
|
||||
|
||||
public boolean canRecover() {
|
||||
@@ -148,4 +162,22 @@ public class RestoreAccountImpl implements RestoreAccount {
|
||||
LOG.info("Added all contacts");
|
||||
});
|
||||
}
|
||||
|
||||
public Set<String> getEncodedShards() {
|
||||
Set<String> s = new HashSet();
|
||||
for (ReturnShardPayload r : recoveredShards) {
|
||||
s.add(new String(messageEncoder.encodeReturnShardPayload(r)));
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public void restoreFromPrevious(Set<String> previousShards) {
|
||||
for (String s : previousShards) {
|
||||
try {
|
||||
addReturnShardPayload(messageParser.parseReturnShardPayload(clientHelper.toList(s.getBytes())));
|
||||
} catch (FormatException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user