use of org.matrix.androidsdk.util.MXOsHandler in project matrix-android-sdk by matrix-org.
the class MXFileStore method open.
/**
* Open the store.
*/
@Override
public void open() {
super.open();
final long fLoadTimeT0 = System.currentTimeMillis();
// avoid concurrency call.
synchronized (this) {
if (!mIsReady && !mIsOpening && (null != mMetadata) && (null != mHandlerThread)) {
mIsOpening = true;
Log.e(LOG_TAG, "Open the store.");
// creation the background handler.
if (null == mFileStoreHandler) {
// never succeeded to reproduce but it was reported in GA.
try {
mHandlerThread.start();
} catch (IllegalThreadStateException e) {
Log.e(LOG_TAG, "mHandlerThread is already started.");
// already started
return;
}
mFileStoreHandler = new MXOsHandler(mHandlerThread.getLooper());
}
Runnable r = new Runnable() {
@Override
public void run() {
mFileStoreHandler.post(new Runnable() {
public void run() {
Log.e(LOG_TAG, "Open the store in the background thread.");
String errorDescription = null;
boolean succeed = (mMetadata.mVersion == MXFILE_VERSION) && TextUtils.equals(mMetadata.mUserId, mCredentials.userId) && TextUtils.equals(mMetadata.mAccessToken, mCredentials.accessToken);
if (!succeed) {
errorDescription = "Invalid store content";
Log.e(LOG_TAG, errorDescription);
}
if (succeed) {
succeed &= loadRoomsMessages();
if (!succeed) {
errorDescription = "loadRoomsMessages fails";
Log.e(LOG_TAG, errorDescription);
} else {
Log.e(LOG_TAG, "loadRoomsMessages succeeds");
}
}
if (succeed) {
succeed &= loadGroups();
if (!succeed) {
errorDescription = "loadGroups fails";
Log.e(LOG_TAG, errorDescription);
} else {
Log.e(LOG_TAG, "loadGroups succeeds");
}
}
if (succeed) {
succeed &= loadRoomsState();
if (!succeed) {
errorDescription = "loadRoomsState fails";
Log.e(LOG_TAG, errorDescription);
} else {
Log.e(LOG_TAG, "loadRoomsState succeeds");
long t0 = System.currentTimeMillis();
Log.e(LOG_TAG, "Retrieve the users from the roomstate");
Collection<Room> rooms = getRooms();
for (Room room : rooms) {
Collection<RoomMember> members = room.getLiveState().getMembers();
for (RoomMember member : members) {
updateUserWithRoomMemberEvent(member);
}
}
long delta = System.currentTimeMillis() - t0;
Log.e(LOG_TAG, "Retrieve " + mUsers.size() + " users with the room states in " + delta + " ms");
mStoreStats.put("Retrieve users", delta);
}
}
if (succeed) {
succeed &= loadSummaries();
if (!succeed) {
errorDescription = "loadSummaries fails";
Log.e(LOG_TAG, errorDescription);
} else {
Log.e(LOG_TAG, "loadSummaries succeeds");
for (String roomId : mRoomSummaries.keySet()) {
Room room = getRoom(roomId);
if (null == room) {
succeed = false;
Log.e(LOG_TAG, "loadSummaries : the room " + roomId + " does not exist");
} else if (null == room.getMember(mCredentials.userId)) {
// succeed = false;
Log.e(LOG_TAG, "loadSummaries) : a summary exists for the roomId " + roomId + " but the user is not anymore a member");
}
}
}
}
if (succeed) {
succeed &= loadRoomsAccountData();
if (!succeed) {
errorDescription = "loadRoomsAccountData fails";
Log.e(LOG_TAG, errorDescription);
} else {
Log.e(LOG_TAG, "loadRoomsAccountData succeeds");
}
}
// assume that something is corrupted
if (!succeed) {
Log.e(LOG_TAG, "Fail to open the store in background");
// delete all data set mMetadata to null
// backup it to restore it
// the behaviour should be the same as first login
MXFileStoreMetaData tmpMetadata = mMetadata;
deleteAllData(true);
mRoomsToCommitForMessages = new HashSet<>();
mRoomsToCommitForStates = new HashSet<>();
// mRoomsToCommitForStatesEvents = new HashSet<>();
mRoomsToCommitForSummaries = new HashSet<>();
mRoomsToCommitForReceipts = new HashSet<>();
mMetadata = tmpMetadata;
// mMetadata should only be null at file store loading
if (null == mMetadata) {
mMetadata = new MXFileStoreMetaData();
mMetadata.mUserId = mCredentials.userId;
mMetadata.mAccessToken = mCredentials.accessToken;
mMetaDataHasChanged = true;
} else {
mMetadata.mEventStreamToken = null;
}
mMetadata.mVersion = MXFILE_VERSION;
// the event stream token is put to zero to ensure ta
mEventStreamToken = null;
mAreReceiptsReady = true;
} else {
Log.d(LOG_TAG, "++ store stats");
Set<String> roomIds = mRoomEvents.keySet();
for (String roomId : roomIds) {
Room room = getRoom(roomId);
if ((null != room) && (null != room.getLiveState())) {
int membersCount = room.getLiveState().getMembers().size();
int eventsCount = mRoomEvents.get(roomId).size();
Log.d(LOG_TAG, " room " + roomId + " : membersCount " + membersCount + " - eventsCount " + eventsCount);
}
}
Log.d(LOG_TAG, "-- store stats");
}
// post processing
Log.d(LOG_TAG, "## open() : post processing.");
dispatchPostProcess(mCredentials.userId);
mIsPostProcessingDone = true;
synchronized (this) {
mIsReady = true;
}
mIsOpening = false;
if (!succeed && !mIsNewStorage) {
Log.e(LOG_TAG, "The store is corrupted.");
dispatchOnStoreCorrupted(mCredentials.userId, errorDescription);
} else {
// extract the room states
mRoomReceiptsToLoad.addAll(listFiles(mStoreRoomsMessagesReceiptsFolderFile.list()));
mPreloadTime = System.currentTimeMillis() - fLoadTimeT0;
Log.e(LOG_TAG, "The store is opened.");
dispatchOnStoreReady(mCredentials.userId);
// load the following items with delay
// theses items are not required to be ready
// load the receipts
loadReceipts();
// load the users
loadUsers();
}
}
});
}
};
Thread t = new Thread(r);
t.start();
} else if (mIsReady) {
Runnable r = new Runnable() {
@Override
public void run() {
mFileStoreHandler.post(new Runnable() {
@Override
public void run() {
// should never happen
if (!mIsPostProcessingDone && !mIsNewStorage) {
Log.e(LOG_TAG, "## open() : is ready but the post processing was not yet done : please wait....");
return;
} else {
if (!mIsPostProcessingDone) {
Log.e(LOG_TAG, "## open() : is ready but the post processing was not yet done.");
dispatchPostProcess(mCredentials.userId);
mIsPostProcessingDone = true;
} else {
Log.e(LOG_TAG, "## open() when ready : the post processing is already done.");
}
dispatchOnStoreReady(mCredentials.userId);
mPreloadTime = System.currentTimeMillis() - fLoadTimeT0;
}
}
});
}
};
Thread t = new Thread(r);
t.start();
}
}
}
Aggregations