use of org.matrix.androidsdk.crypto.data.MXUsersDevicesMap in project matrix-android-sdk by matrix-org.
the class CryptoTest method test07_testAliceAndBobInACryptedRoom.
@Test
public void test07_testAliceAndBobInACryptedRoom() throws Exception {
Log.e(LOG_TAG, "test07_testAliceAndBobInACryptedRoom");
Context context = InstrumentationRegistry.getContext();
final HashMap<String, Object> results = new HashMap<>();
doE2ETestWithAliceAndBobInARoom(true);
final String messageFromAlice = "Hello I'm Alice!";
Room roomFromBobPOV = mBobSession.getDataHandler().getRoom(mRoomId);
Room roomFromAlicePOV = mAliceSession.getDataHandler().getRoom(mRoomId);
assertTrue(roomFromBobPOV.isEncrypted());
assertTrue(roomFromAlicePOV.isEncrypted());
final CountDownLatch lock1 = new CountDownLatch(1);
roomFromAlicePOV.sendEvent(buildTextEvent(messageFromAlice, mAliceSession), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
lock1.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock1.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
results.put("sendEventError", e);
lock1.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock1.countDown();
}
});
lock1.await(2000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("sendEventError"));
MXCryptoError error = (MXCryptoError) results.get("sendEventError");
assertTrue(TextUtils.equals(error.errcode, MXCryptoError.UNKNOWN_DEVICES_CODE));
MXUsersDevicesMap<MXDeviceInfo> unknownDevices = (MXUsersDevicesMap<MXDeviceInfo>) error.mExceptionData;
List<String> deviceInfos = unknownDevices.getUserDeviceIds(mBobSession.getMyUserId());
assertTrue(1 == deviceInfos.size());
assertTrue(TextUtils.equals(deviceInfos.get(0), mBobSession.getCrypto().getMyDevice().deviceId));
final CountDownLatch lock2 = new CountDownLatch(1);
mAliceSession.getCrypto().setDevicesKnown(Arrays.asList(mBobSession.getCrypto().getMyDevice()), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("setDevicesKnown", "setDevicesKnown");
lock2.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock2.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock2.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock2.countDown();
}
});
lock2.await(2000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("setDevicesKnown"));
final CountDownLatch lock3 = new CountDownLatch(3);
MXEventListener eventListener = new MXEventListener() {
@Override
public void onLiveEvent(Event event, RoomState roomState) {
try {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
if (checkEncryptedEvent(event, mRoomId, messageFromAlice, mAliceSession)) {
results.put("onLiveEvent", "onLiveEvent");
lock3.countDown();
}
}
} catch (Exception e) {
}
}
};
mBobSession.getDataHandler().addListener(new MXEventListener() {
@Override
public void onToDeviceEvent(Event event) {
results.put("onToDeviceEvent", event);
lock3.countDown();
}
});
roomFromBobPOV.addEventListener(eventListener);
roomFromAlicePOV.sendEvent(buildTextEvent(messageFromAlice, mAliceSession), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
lock3.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock3.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock3.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock3.countDown();
}
});
lock3.await(2000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onToDeviceEvent"));
assertTrue(results.containsKey("onLiveEvent"));
assertTrue(mBobSession.getCrypto().getDeviceTrackingStatus(mBobSession.getMyUserId()) == MXDeviceList.TRACKING_STATUS_UP_TO_DATE);
assertTrue(mBobSession.getCrypto().getDeviceTrackingStatus(mAliceSession.getMyUserId()) == MXDeviceList.TRACKING_STATUS_UP_TO_DATE);
assertTrue(mAliceSession.getCrypto().getDeviceTrackingStatus(mBobSession.getMyUserId()) == MXDeviceList.TRACKING_STATUS_UP_TO_DATE);
assertTrue(mAliceSession.getCrypto().getDeviceTrackingStatus(mAliceSession.getMyUserId()) == MXDeviceList.TRACKING_STATUS_UP_TO_DATE);
mBobSession.clear(context);
}
use of org.matrix.androidsdk.crypto.data.MXUsersDevicesMap in project matrix-android-sdk by matrix-org.
the class CryptoTest method test03_testKeysUploadAndDownload.
@Test
public void test03_testKeysUploadAndDownload() throws Exception {
Log.e(LOG_TAG, "test03_testKeysUploadAndDownload");
Context context = InstrumentationRegistry.getContext();
final HashMap<String, Object> results = new HashMap<>();
createAliceAccount();
mAliceSession.getCredentials().deviceId = "AliceDevice";
final CountDownLatch lock0 = new CountDownLatch(1);
mAliceSession.enableCrypto(true, new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("enableCrypto", "enableCrypto");
lock0.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock0.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock0.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock0.countDown();
}
});
lock0.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("enableCrypto"));
createBobAccount();
final CountDownLatch lock2 = new CountDownLatch(1);
mBobSession.getCredentials().deviceId = "BobDevice";
mBobSession.enableCrypto(true, new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("enableCrypto2", "enableCrypto2");
lock2.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock2.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock2.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock2.countDown();
}
});
lock2.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("enableCrypto2"));
final CountDownLatch lock3 = new CountDownLatch(1);
mBobSession.getCrypto().getDeviceList().downloadKeys(Arrays.asList(mBobSession.getMyUserId(), mAliceSession.getMyUserId()), false, new ApiCallback<MXUsersDevicesMap<MXDeviceInfo>>() {
@Override
public void onSuccess(MXUsersDevicesMap<MXDeviceInfo> info) {
results.put("downloadKeys", info);
lock3.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock3.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock3.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock3.countDown();
}
});
lock3.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("downloadKeys"));
MXUsersDevicesMap<MXDeviceInfo> usersDevicesInfoMap = (MXUsersDevicesMap<MXDeviceInfo>) results.get("downloadKeys");
assertTrue(2 == usersDevicesInfoMap.getUserIds().size());
assertTrue(1 == usersDevicesInfoMap.getUserDeviceIds(mAliceSession.getMyUserId()).size());
MXDeviceInfo aliceDeviceFromBobPOV = usersDevicesInfoMap.getObject("AliceDevice", mAliceSession.getMyUserId());
assertTrue(null != aliceDeviceFromBobPOV);
assertTrue(TextUtils.equals(aliceDeviceFromBobPOV.fingerprint(), mAliceSession.getCrypto().getOlmDevice().getDeviceEd25519Key()));
// Continue testing other methods
assertTrue(null != mBobSession.getCrypto().deviceWithIdentityKey(mAliceSession.getCrypto().getOlmDevice().getDeviceCurve25519Key(), mAliceSession.getMyUserId(), MXCryptoAlgorithms.MXCRYPTO_ALGORITHM_OLM));
assertTrue(aliceDeviceFromBobPOV.isUnknown());
final CountDownLatch lock3a = new CountDownLatch(1);
mBobSession.getCrypto().setDevicesKnown(Arrays.asList(aliceDeviceFromBobPOV), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("setDevicesKnown", info);
lock3a.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock3a.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock3a.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock3a.countDown();
}
});
lock3a.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("setDevicesKnown"));
assertTrue(aliceDeviceFromBobPOV.isUnverified());
final CountDownLatch lock3b = new CountDownLatch(1);
mBobSession.getCrypto().setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_BLOCKED, aliceDeviceFromBobPOV.deviceId, mAliceSession.getMyUserId(), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("setDeviceVerification1", info);
lock3b.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock3b.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock3b.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock3b.countDown();
}
});
lock3b.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("setDeviceVerification1"));
assertTrue(aliceDeviceFromBobPOV.isBlocked());
Credentials bobCredentials = mBobSession.getCredentials();
Uri uri = Uri.parse(CryptoTestHelper.TESTS_HOME_SERVER_URL);
HomeServerConnectionConfig hs = new HomeServerConnectionConfig(uri);
hs.setCredentials(bobCredentials);
IMXStore store = new MXFileStore(hs, context);
MXSession bobSession2 = new MXSession(hs, new MXDataHandler(store, bobCredentials), context);
final CountDownLatch lock4 = new CountDownLatch(1);
MXStoreListener listener = new MXStoreListener() {
@Override
public void postProcess(String accountId) {
}
@Override
public void onStoreReady(String accountId) {
results.put("onStoreReady", "onStoreReady");
lock4.countDown();
}
@Override
public void onStoreCorrupted(String accountId, String description) {
lock4.countDown();
}
@Override
public void onStoreOOM(String accountId, String description) {
lock4.countDown();
}
};
bobSession2.getDataHandler().getStore().addMXStoreListener(listener);
bobSession2.getDataHandler().getStore().open();
lock4.await(2000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onStoreReady"));
final CountDownLatch lock4b = new CountDownLatch(2);
MXEventListener eventListener = new MXEventListener() {
@Override
public void onInitialSyncComplete(String toToken) {
results.put("onInitialSyncComplete", "onInitialSyncComplete");
lock4b.countDown();
}
@Override
public void onCryptoSyncComplete() {
results.put("onCryptoSyncComplete", "onCryptoSyncComplete");
lock4b.countDown();
}
};
bobSession2.getDataHandler().addListener(eventListener);
bobSession2.startEventStream(null);
lock4b.await(2000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onInitialSyncComplete"));
assertTrue(results.containsKey("onCryptoSyncComplete"));
MXDeviceInfo aliceDeviceFromBobPOV2 = bobSession2.getCrypto().deviceWithIdentityKey(mAliceSession.getCrypto().getOlmDevice().getDeviceCurve25519Key(), mAliceSession.getMyUserId(), MXCryptoAlgorithms.MXCRYPTO_ALGORITHM_OLM);
assertTrue(null != aliceDeviceFromBobPOV2);
assertTrue(TextUtils.equals(aliceDeviceFromBobPOV2.fingerprint(), mAliceSession.getCrypto().getOlmDevice().getDeviceEd25519Key()));
assertTrue(aliceDeviceFromBobPOV2.mVerified + " instead of " + MXDeviceInfo.DEVICE_VERIFICATION_BLOCKED, aliceDeviceFromBobPOV2.mVerified == MXDeviceInfo.DEVICE_VERIFICATION_BLOCKED);
// Download again alice device
final CountDownLatch lock5 = new CountDownLatch(1);
bobSession2.getCrypto().getDeviceList().downloadKeys(Arrays.asList(mAliceSession.getMyUserId()), true, new ApiCallback<MXUsersDevicesMap<MXDeviceInfo>>() {
@Override
public void onSuccess(MXUsersDevicesMap<MXDeviceInfo> info) {
results.put("downloadKeys2", info);
lock5.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock5.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock5.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock5.countDown();
}
});
lock5.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("downloadKeys2"));
MXDeviceInfo aliceDeviceFromBobPOV3 = bobSession2.getCrypto().deviceWithIdentityKey(mAliceSession.getCrypto().getOlmDevice().getDeviceCurve25519Key(), mAliceSession.getMyUserId(), MXCryptoAlgorithms.MXCRYPTO_ALGORITHM_OLM);
assertTrue(null != aliceDeviceFromBobPOV3);
assertTrue(TextUtils.equals(aliceDeviceFromBobPOV3.fingerprint(), mAliceSession.getCrypto().getOlmDevice().getDeviceEd25519Key()));
assertTrue(aliceDeviceFromBobPOV3.isBlocked());
mAliceSession.clear(context);
mBobSession.clear(context);
bobSession2.clear(context);
}
use of org.matrix.androidsdk.crypto.data.MXUsersDevicesMap in project matrix-android-sdk by matrix-org.
the class CryptoTest method test22_testDownloadKeysForUserWithNoDevice.
@Test
public void test22_testDownloadKeysForUserWithNoDevice() throws Exception {
Log.e(LOG_TAG, "test22_testDownloadKeysForUserWithNoDevice");
final HashMap<String, Object> results = new HashMap<>();
doE2ETestWithAliceAndBobInARoom(false);
mAliceSession.getCrypto().setWarnOnUnknownDevices(false);
final CountDownLatch lock1 = new CountDownLatch(1);
mAliceSession.getCrypto().getDeviceList().downloadKeys(Arrays.asList(mBobSession.getMyUserId()), false, new ApiCallback<MXUsersDevicesMap<MXDeviceInfo>>() {
@Override
public void onSuccess(MXUsersDevicesMap<MXDeviceInfo> info) {
results.put("downloadKeys", info);
lock1.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock1.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock1.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock1.countDown();
}
});
lock1.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("downloadKeys"));
MXUsersDevicesMap<MXDeviceInfo> usersDevicesInfoMap = (MXUsersDevicesMap<MXDeviceInfo>) results.get("downloadKeys");
// MXCrypto.downloadKeys should return @[] for Bob to distinguish him from an unknown user
List<String> bobDevices = usersDevicesInfoMap.getUserDeviceIds(mBobSession.getMyUserId());
assertTrue(null != bobDevices);
assertTrue(0 == bobDevices.size());
// try again
// it should not failed
final CountDownLatch lock2 = new CountDownLatch(1);
mAliceSession.getCrypto().getDeviceList().downloadKeys(Arrays.asList(mBobSession.getMyUserId()), false, new ApiCallback<MXUsersDevicesMap<MXDeviceInfo>>() {
@Override
public void onSuccess(MXUsersDevicesMap<MXDeviceInfo> info) {
results.put("downloadKeys2", info);
lock2.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock2.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock2.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock2.countDown();
}
});
lock2.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("downloadKeys2"));
}
use of org.matrix.androidsdk.crypto.data.MXUsersDevicesMap in project matrix-android-sdk by matrix-org.
the class MXCrypto method getUnknownDevices.
/**
* Provides the list of unknown devices
*
* @param devicesInRoom the devices map
* @return the unknown devices map
*/
public static MXUsersDevicesMap<MXDeviceInfo> getUnknownDevices(MXUsersDevicesMap<MXDeviceInfo> devicesInRoom) {
MXUsersDevicesMap<MXDeviceInfo> unknownDevices = new MXUsersDevicesMap<>();
List<String> userIds = devicesInRoom.getUserIds();
for (String userId : userIds) {
List<String> deviceIds = devicesInRoom.getUserDeviceIds(userId);
for (String deviceId : deviceIds) {
MXDeviceInfo deviceInfo = devicesInRoom.getObject(deviceId, userId);
if (deviceInfo.isUnknown()) {
unknownDevices.setObject(deviceInfo, userId, deviceId);
}
}
}
return unknownDevices;
}
use of org.matrix.androidsdk.crypto.data.MXUsersDevicesMap in project matrix-android-sdk by matrix-org.
the class MXFileCryptoStore method preloadCryptoData.
/**
* Preload the crypto data
*/
private void preloadCryptoData() {
Log.d(LOG_TAG, "## preloadCryptoData() starts");
long t0 = System.currentTimeMillis();
Object olmAccountAsVoid;
if (mAccountFileTmp.exists()) {
olmAccountAsVoid = loadObject(mAccountFileTmp, "preloadCryptoData - mAccountFile - tmp");
} else {
olmAccountAsVoid = loadObject(mAccountFile, "preloadCryptoData - mAccountFile");
}
if (null != olmAccountAsVoid) {
try {
mOlmAccount = (OlmAccount) olmAccountAsVoid;
} catch (Exception e) {
mIsCorrupted = true;
Log.e(LOG_TAG, "## preloadCryptoData() - invalid mAccountFile " + e.getMessage());
}
}
Log.d(LOG_TAG, "## preloadCryptoData() : load mOlmAccount in " + (System.currentTimeMillis() - t0) + " ms");
// previous store format
if (!mDevicesFolder.exists()) {
Object usersDevicesInfoMapAsVoid;
// if the tmp exists, it means that the latest file backup has been killed / stopped
if (mDevicesFileTmp.exists()) {
usersDevicesInfoMapAsVoid = loadObject(mDevicesFileTmp, "preloadCryptoData - mUsersDevicesInfoMap - tmp");
} else {
usersDevicesInfoMapAsVoid = loadObject(mDevicesFile, "preloadCryptoData - mUsersDevicesInfoMap");
}
if (null != usersDevicesInfoMapAsVoid) {
try {
MXUsersDevicesMap objectAsMap = (MXUsersDevicesMap) usersDevicesInfoMapAsVoid;
mUsersDevicesInfoMap = new MXUsersDevicesMap<>(objectAsMap.getMap());
} catch (Exception e) {
mIsCorrupted = true;
Log.e(LOG_TAG, "## preloadCryptoData() - invalid mUsersDevicesInfoMap " + e.getMessage());
}
} else {
mIsCorrupted = false;
}
mDevicesFolder.mkdirs();
if (null != mUsersDevicesInfoMap) {
HashMap<String, HashMap<String, MXDeviceInfo>> map = mUsersDevicesInfoMap.getMap();
Set<String> userIds = map.keySet();
for (String userId : userIds) {
storeObject(map.get(userId), mDevicesFolder, userId, "convert devices map of " + userId);
}
mDevicesFileTmp.delete();
mDevicesFile.delete();
}
} else {
// the user devices are loaded on demand
mUsersDevicesInfoMap = new MXUsersDevicesMap<>();
}
long t2 = System.currentTimeMillis();
int algoSize = 0;
Object algorithmsAsVoid;
if (mAlgorithmsFileTmp.exists()) {
algorithmsAsVoid = loadObject(mAlgorithmsFileTmp, "preloadCryptoData - mRoomsAlgorithms - tmp");
} else {
algorithmsAsVoid = loadObject(mAlgorithmsFile, "preloadCryptoData - mRoomsAlgorithms");
}
if (null != algorithmsAsVoid) {
try {
Map<String, String> algorithmsMap = (Map<String, String>) algorithmsAsVoid;
mRoomsAlgorithms = new HashMap<>(algorithmsMap);
algoSize = mRoomsAlgorithms.size();
} catch (Exception e) {
mIsCorrupted = true;
Log.e(LOG_TAG, "## preloadCryptoData() - invalid mAlgorithmsFile " + e.getMessage());
}
}
Log.d(LOG_TAG, "## preloadCryptoData() : load mRoomsAlgorithms (" + algoSize + " algos) in " + (System.currentTimeMillis() - t2) + " ms");
Object trackingStatusesAsVoid;
if (mTrackingStatusesFileTmp.exists()) {
trackingStatusesAsVoid = loadObject(mTrackingStatusesFileTmp, "preloadCryptoData - mTrackingStatuses - tmp");
} else {
trackingStatusesAsVoid = loadObject(mTrackingStatusesFile, "preloadCryptoData - mTrackingStatuses");
}
if (null != trackingStatusesAsVoid) {
try {
mTrackingStatuses = new HashMap<>((Map<String, Integer>) trackingStatusesAsVoid);
} catch (Exception e) {
Log.e(LOG_TAG, "## preloadCryptoData() - invalid mTrackingStatuses " + e.getMessage());
}
}
File outgoingRequestFile;
if (mOutgoingRoomKeyRequestsFileTmp.exists()) {
outgoingRequestFile = mOutgoingRoomKeyRequestsFileTmp;
} else {
outgoingRequestFile = mOutgoingRoomKeyRequestsFile;
}
if (outgoingRequestFile.exists()) {
Object requestsAsVoid = loadObject(outgoingRequestFile, "get outgoing key request");
try {
if (null != requestsAsVoid) {
mOutgoingRoomKeyRequests.putAll((Map<Map<String, String>, OutgoingRoomKeyRequest>) requestsAsVoid);
}
} catch (Exception e) {
Log.e(LOG_TAG, "## preloadCryptoData() : mOutgoingRoomKeyRequests init failed " + e.getMessage());
}
}
if (mOlmSessionsFolder.exists()) {
long t3 = System.currentTimeMillis();
mOlmSessions = new HashMap<>();
String[] olmSessionFiles = mOlmSessionsFolder.list();
if (null != olmSessionFiles) {
// build mOlmSessions for the file system
for (int i = 0; i < olmSessionFiles.length; i++) {
String deviceKey = olmSessionFiles[i];
HashMap<String, OlmSession> olmSessionSubMap = new HashMap<>();
File sessionsDeviceFolder = new File(mOlmSessionsFolder, deviceKey);
String[] sessionIds = sessionsDeviceFolder.list();
if (null != sessionIds) {
for (int j = 0; j < sessionIds.length; j++) {
String sessionId = sessionIds[j];
OlmSession olmSession = (OlmSession) loadObject(new File(sessionsDeviceFolder, sessionId), "load the olmSession " + deviceKey + " " + sessionId);
if (null != olmSession) {
olmSessionSubMap.put(decodeFilename(sessionId), olmSession);
}
}
}
mOlmSessions.put(decodeFilename(deviceKey), olmSessionSubMap);
}
Log.d(LOG_TAG, "## preloadCryptoData() : load " + olmSessionFiles.length + " olmsessions in " + (System.currentTimeMillis() - t3) + " ms");
}
} else {
Object olmSessionsAsVoid;
if (mOlmSessionsFileTmp.exists()) {
olmSessionsAsVoid = loadObject(mOlmSessionsFileTmp, "preloadCryptoData - mOlmSessions - tmp");
} else {
olmSessionsAsVoid = loadObject(mOlmSessionsFile, "preloadCryptoData - mOlmSessions");
}
if (null != olmSessionsAsVoid) {
try {
Map<String, Map<String, OlmSession>> olmSessionMap = (Map<String, Map<String, OlmSession>>) olmSessionsAsVoid;
mOlmSessions = new HashMap<>();
for (String key : olmSessionMap.keySet()) {
mOlmSessions.put(key, new HashMap<>(olmSessionMap.get(key)));
}
// convert to the new format
if (!mOlmSessionsFolder.mkdir()) {
Log.e(LOG_TAG, "Cannot create the folder " + mOlmSessionsFolder);
}
for (String key : olmSessionMap.keySet()) {
Map<String, OlmSession> submap = olmSessionMap.get(key);
File submapFolder = new File(mOlmSessionsFolder, encodeFilename(key));
if (!submapFolder.mkdir()) {
Log.e(LOG_TAG, "Cannot create the folder " + submapFolder);
}
for (String sessionId : submap.keySet()) {
storeObject(submap.get(sessionId), submapFolder, encodeFilename(sessionId), "Convert olmSession " + key + " " + sessionId);
}
}
} catch (Exception e) {
mIsCorrupted = true;
Log.e(LOG_TAG, "## preloadCryptoData() - invalid mSessionsFile " + e.getMessage());
}
mOlmSessionsFileTmp.delete();
mOlmSessionsFile.delete();
}
}
if (mInboundGroupSessionsFolder.exists()) {
long t4 = System.currentTimeMillis();
mInboundGroupSessions = new HashMap<>();
int count = 0;
String[] keysFolder = mInboundGroupSessionsFolder.list();
if (null != keysFolder) {
for (int i = 0; i < keysFolder.length; i++) {
File keyFolder = new File(mInboundGroupSessionsFolder, keysFolder[i]);
HashMap<String, MXOlmInboundGroupSession2> submap = new HashMap<>();
String[] sessionIds = keyFolder.list();
if (null != sessionIds) {
for (int j = 0; j < sessionIds.length; j++) {
File inboundSessionFile = new File(keyFolder, sessionIds[j]);
try {
Object inboundSessionAsVoid = loadObject(inboundSessionFile, "load inboundsession " + sessionIds[j] + " ");
MXOlmInboundGroupSession2 inboundSession;
if ((null != inboundSessionAsVoid) && (inboundSessionAsVoid instanceof MXOlmInboundGroupSession)) {
inboundSession = new MXOlmInboundGroupSession2((MXOlmInboundGroupSession) inboundSessionAsVoid);
} else {
inboundSession = (MXOlmInboundGroupSession2) inboundSessionAsVoid;
}
if (null != inboundSession) {
submap.put(decodeFilename(sessionIds[j]), inboundSession);
} else {
Log.e(LOG_TAG, "## preloadCryptoData() : delete " + inboundSessionFile);
inboundSessionFile.delete();
mIsCorrupted = false;
}
count++;
} catch (Exception e) {
Log.e(LOG_TAG, "## preloadCryptoData() - invalid mInboundGroupSessions " + e.getMessage());
}
}
}
mInboundGroupSessions.put(decodeFilename(keysFolder[i]), submap);
}
}
Log.d(LOG_TAG, "## preloadCryptoData() : load " + count + " inboundGroupSessions in " + (System.currentTimeMillis() - t4) + " ms");
} else {
Object inboundGroupSessionsAsVoid;
if (mInboundGroupSessionsFileTmp.exists()) {
inboundGroupSessionsAsVoid = loadObject(mInboundGroupSessionsFileTmp, "preloadCryptoData - mInboundGroupSessions - tmp");
} else {
inboundGroupSessionsAsVoid = loadObject(mInboundGroupSessionsFile, "preloadCryptoData - mInboundGroupSessions");
}
if (null != inboundGroupSessionsAsVoid) {
try {
Map<String, Map<String, MXOlmInboundGroupSession2>> inboundGroupSessionsMap = (Map<String, Map<String, MXOlmInboundGroupSession2>>) inboundGroupSessionsAsVoid;
mInboundGroupSessions = new HashMap<>();
for (String key : inboundGroupSessionsMap.keySet()) {
mInboundGroupSessions.put(key, new HashMap<>(inboundGroupSessionsMap.get(key)));
}
} catch (Exception e) {
mIsCorrupted = true;
Log.e(LOG_TAG, "## preloadCryptoData() - invalid mInboundGroupSessions " + e.getMessage());
}
if (!mInboundGroupSessionsFolder.mkdirs()) {
Log.e(LOG_TAG, "Cannot create the folder " + mInboundGroupSessionsFolder);
}
// convert to the new format
for (String key : mInboundGroupSessions.keySet()) {
File keyFolder = new File(mInboundGroupSessionsFolder, encodeFilename(key));
if (!keyFolder.mkdirs()) {
Log.e(LOG_TAG, "Cannot create the folder " + keyFolder);
}
Map<String, MXOlmInboundGroupSession2> inboundMaps = mInboundGroupSessions.get(key);
for (String sessionId : inboundMaps.keySet()) {
storeObject(inboundMaps.get(sessionId), keyFolder, encodeFilename(sessionId), "Convert inboundsession");
}
}
}
mInboundGroupSessionsFileTmp.delete();
mInboundGroupSessionsFile.delete();
}
if ((null == mOlmAccount) && (mUsersDevicesInfoMap.getMap().size() > 0)) {
mIsCorrupted = true;
Log.e(LOG_TAG, "## preloadCryptoData() - there is no account but some devices are defined");
}
}
Aggregations