Deprecated users will be deleted, too now.
This commit is contained in:
@@ -34,8 +34,8 @@ android {
|
||||
applicationId "de.beyondsoft.ownchat"
|
||||
minSdkVersion 19
|
||||
targetSdkVersion 26
|
||||
versionCode 10
|
||||
versionName "1.10"
|
||||
versionCode 11
|
||||
versionName "1.11"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
multiDexEnabled true
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import io.realm.Realm;
|
||||
public class BewoplanerApplication extends Application {
|
||||
|
||||
private ApplicationComponent mApplicationComponent;
|
||||
private static Context mContext;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
@@ -33,6 +34,8 @@ public class BewoplanerApplication extends Application {
|
||||
Fabric.with(this, new Crashlytics());
|
||||
}
|
||||
|
||||
mContext = getApplicationContext();
|
||||
|
||||
buildGraphAndInject();
|
||||
MessagingInstanceIdService.setApplicationContext(this);
|
||||
|
||||
@@ -69,4 +72,7 @@ public class BewoplanerApplication extends Application {
|
||||
buildGraphAndInject();
|
||||
}
|
||||
|
||||
public static Context getAppContext() {
|
||||
return mContext;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package de.beyondsoft.ownchat.data.repository;
|
||||
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
|
||||
import de.beyondsoft.ownchat.BewoplanerApplication;
|
||||
@@ -19,12 +18,10 @@ import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import de.beyondsoft.ownchat.utils.Constants;
|
||||
import io.realm.Realm;
|
||||
import io.realm.RealmList;
|
||||
import io.realm.RealmQuery;
|
||||
import io.realm.RealmResults;
|
||||
import io.realm.Sort;
|
||||
import rx.Completable;
|
||||
@@ -102,11 +99,22 @@ public class GroupRealmRepository implements Repository<Group> {
|
||||
|
||||
GroupByIdSpecification realmSpecification = (GroupByIdSpecification) specification;
|
||||
|
||||
int itemsSize = ((ArrayList<Group>) items).size();
|
||||
ArrayList<Group> newGroups = (ArrayList<Group>) items;
|
||||
ArrayList<Long> newUserIds = new ArrayList<>();
|
||||
ArrayList<Long> oldUserIds = new ArrayList<>();
|
||||
ArrayList<Long> userIdsToDelete = new ArrayList<>();
|
||||
|
||||
Long myself = ((GroupByIdSpecification) specification).getUserId();
|
||||
|
||||
if(newGroups.size() > 0) {
|
||||
newUserIds = getUserIdsFromGroups(newGroups, myself);
|
||||
}
|
||||
|
||||
int itemsSize = newGroups.size();
|
||||
|
||||
Long[] ids = new Long[itemsSize];
|
||||
for(int i = 0; i < ids.length; i++) {
|
||||
ids[i] = ((ArrayList<Group>) items).get(i).id;
|
||||
ids[i] = newGroups.get(i).id;
|
||||
}
|
||||
|
||||
List<String> filePaths = new ArrayList<>();
|
||||
@@ -119,6 +127,19 @@ public class GroupRealmRepository implements Repository<Group> {
|
||||
|
||||
RealmResults<GroupRealm> allWhoAreNotComingFromTheServer = realmWrapper.realm.where(GroupRealm.class).not().in(GroupRealm.ID, ids).findAll();
|
||||
RealmResults<MessageRealm> messagesToDelete = realmWrapper.realm.where(MessageRealm.class).not().in(MessageRealm.GROUP_ID, ids).findAll();
|
||||
RealmResults<GroupRealm> allOldGroups = realmWrapper.realm.where(GroupRealm.class).findAll();
|
||||
|
||||
for(GroupRealm groupRealm : allOldGroups) {
|
||||
for(UserRealm userRealm : groupRealm.getUserIdList()) {
|
||||
oldUserIds.add(userRealm.getId());
|
||||
}
|
||||
}
|
||||
|
||||
for(Long id : oldUserIds) {
|
||||
if(!newUserIds.contains(id) && !Objects.equals(id, myself) && !userIdsToDelete.contains(id)) {
|
||||
userIdsToDelete.add(id);
|
||||
}
|
||||
}
|
||||
|
||||
for(MessageRealm mr : messagesToDelete) {
|
||||
if(mr.isFileDownloaded()) {
|
||||
@@ -129,33 +150,37 @@ public class GroupRealmRepository implements Repository<Group> {
|
||||
hasGroupsToDelete = allWhoAreNotComingFromTheServer.size() > 0;
|
||||
hasMessagesToDelete = messagesToDelete.size() > 0;
|
||||
|
||||
Log.d("REALM_TRANSACTION", String.format(Locale.getDefault(), "Number of groups to delete: %d and the number of their messages: %d", allWhoAreNotComingFromTheServer.size(), messagesToDelete.size()));
|
||||
|
||||
realmWrapper.realm.commitTransaction();
|
||||
realmWrapper.realm.close();
|
||||
} catch(Exception e) {
|
||||
Log.e("REALM_TRANSACTION", e.getMessage());
|
||||
RealmTransactionLog(e.getMessage(), 2);
|
||||
|
||||
realmWrapper.realm.cancelTransaction();
|
||||
realmWrapper.realm.close();
|
||||
}
|
||||
|
||||
if(hasGroupsToDelete) {
|
||||
removeGroupsById(ids);
|
||||
removeGroupsByIds(ids);
|
||||
}
|
||||
|
||||
if(userIdsToDelete.size() > 0) {
|
||||
removeUsersByIds(userIdsToDelete.toArray(new Long[userIdsToDelete.size()]));
|
||||
}
|
||||
|
||||
if(hasMessagesToDelete) {
|
||||
removeMessagesByGroupIds(ids);
|
||||
|
||||
// for (String path : filePaths) {
|
||||
// ///storage/emulated/0/Android/data/de.beyondsoft.ownchat/files/26_1512056288.pdf
|
||||
// String[] splitFileName = path.split("/");
|
||||
// String fileName = splitFileName[splitFileName.length - 1];
|
||||
//
|
||||
// boolean fileExists = (new File(getExternalFilesDir(), "ownChat" + File.separator + fileName)).exists();
|
||||
//
|
||||
// Log.d("REALM_TRANSACTION", String.format(Locale.getDefault(), "%s; exists on phone: %s", splitFileName[splitFileName.length - 1], fileExists));
|
||||
// }
|
||||
for (String path : filePaths) {
|
||||
String[] splitFileName = path.split("/");
|
||||
String fileName = splitFileName[splitFileName.length - 1];
|
||||
File fileToDelete = new File(BewoplanerApplication.getAppContext().getExternalFilesDir(null), File.separator + fileName);
|
||||
|
||||
boolean fileExists = fileToDelete.exists();
|
||||
|
||||
if(fileExists) {
|
||||
fileToDelete.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Observable.from(items)
|
||||
@@ -183,7 +208,7 @@ public class GroupRealmRepository implements Repository<Group> {
|
||||
realmWrapper.realm.copyToRealmOrUpdate(newGroup);
|
||||
})
|
||||
.doOnError(throwable -> {
|
||||
Log.e("REALM_TRANSACTION", "Error while updating groups: " + throwable.getMessage());
|
||||
RealmTransactionLog(String.format(Locale.getDefault(), "Error while updating groups: %s", throwable.getMessage()), 2);
|
||||
realmWrapper.realm.cancelTransaction();
|
||||
realmWrapper.realm.close();
|
||||
})
|
||||
@@ -194,7 +219,43 @@ public class GroupRealmRepository implements Repository<Group> {
|
||||
.toCompletable();
|
||||
}
|
||||
|
||||
private void removeGroupsById(Long[] groupIds) {
|
||||
private ArrayList<Long> getUserIdsFromGroups(ArrayList<Group> groups, long myself) {
|
||||
ArrayList<Long> userIds = new ArrayList<>();
|
||||
|
||||
for(Group group : groups) {
|
||||
for(User user : group.users) {
|
||||
if(user.id == myself) {
|
||||
continue;
|
||||
}
|
||||
|
||||
userIds.add(user.id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return userIds;
|
||||
}
|
||||
|
||||
private void RealmTransactionLog(String message, int mode) {
|
||||
final String tag = "REALM_TRANSACTION";
|
||||
|
||||
switch(mode) {
|
||||
case 0:
|
||||
Log.d(tag, message);
|
||||
break;
|
||||
case 1:
|
||||
Log.i(tag, message);
|
||||
break;
|
||||
case 2:
|
||||
Log.e(tag, message);
|
||||
break;
|
||||
default:
|
||||
Log.wtf(tag, message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void removeGroupsByIds(Long[] groupIds) {
|
||||
final RealmWrapper realmWrapper = new RealmWrapper();
|
||||
|
||||
try {
|
||||
@@ -232,6 +293,25 @@ public class GroupRealmRepository implements Repository<Group> {
|
||||
}
|
||||
}
|
||||
|
||||
private void removeUsersByIds(Long[] userIds) {
|
||||
final RealmWrapper realmWrapper = new RealmWrapper();
|
||||
|
||||
try {
|
||||
realmWrapper.realm = mRealmProvider.provide();
|
||||
realmWrapper.realm.beginTransaction();
|
||||
|
||||
realmWrapper.realm.where(UserRealm.class).in(UserRealm.ID, userIds).findAll().deleteAllFromRealm();
|
||||
|
||||
realmWrapper.realm.commitTransaction();
|
||||
realmWrapper.realm.close();
|
||||
} catch(Exception e) {
|
||||
Log.e("REALM_TRANSACTION", "Error while removing users: " + e.getMessage());
|
||||
|
||||
realmWrapper.realm.cancelTransaction();
|
||||
realmWrapper.realm.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Completable remove(Specification specification) {
|
||||
return null;
|
||||
|
||||
@@ -611,36 +611,37 @@ public class LoginPresenter extends MVPAbstractPresenter<LoginView> implements L
|
||||
}
|
||||
|
||||
private void deleteDataOnCustomerIdChange() {
|
||||
File dir = mContext.getExternalFilesDir(null);
|
||||
File dir = mContext.getExternalFilesDir(null);
|
||||
mGlide.clearMemory();
|
||||
|
||||
if (dir != null) {
|
||||
if (dir.isDirectory()) {
|
||||
String[] children = dir.list();
|
||||
if (dir != null) {
|
||||
if (dir.isDirectory()) {
|
||||
String[] children = dir.list();
|
||||
|
||||
for (String aChildren : children) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
new File(dir, aChildren).delete();
|
||||
}
|
||||
} else {
|
||||
Log.e("deleteData", dir.getAbsolutePath() + " is not a directory!");
|
||||
for (String aChildren : children) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
new File(dir, aChildren).delete();
|
||||
}
|
||||
} else {
|
||||
dir = new File(Environment.getExternalStorageDirectory(), "BeWoPlaner");
|
||||
|
||||
if (dir.isDirectory()) {
|
||||
String[] children = dir.list();
|
||||
|
||||
for (String aChildren : children) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
new File(dir, aChildren).delete();
|
||||
}
|
||||
} else {
|
||||
Log.e("deleteData", dir.getAbsolutePath() + " is not a directory!");
|
||||
}
|
||||
Log.e("deleteData", dir.getAbsolutePath() + " is not a directory!");
|
||||
}
|
||||
} else {
|
||||
dir = new File(Environment.getExternalStorageDirectory(), "BeWoPlaner");
|
||||
|
||||
Realm realm = mRealmProvider.provide();
|
||||
realm.executeTransaction(realm1 -> realm1.deleteAll());
|
||||
if (dir.isDirectory()) {
|
||||
String[] children = dir.list();
|
||||
|
||||
for (String aChildren : children) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
new File(dir, aChildren).delete();
|
||||
}
|
||||
} else {
|
||||
Log.e("deleteData", dir.getAbsolutePath() + " is not a directory!");
|
||||
}
|
||||
}
|
||||
|
||||
Realm realm = mRealmProvider.provide();
|
||||
realm.executeTransaction(realm1 -> realm1.deleteAll());
|
||||
}
|
||||
|
||||
private boolean checkPassword(String password) {
|
||||
|
||||
Reference in New Issue
Block a user