Commit 841593bc authored by Matheus Jardim Bernardes's avatar Matheus Jardim Bernardes Committed by GitHub

Merge pull request #307 from VoiSmart/fix_247_change-hostname

Fix 247 change hostname
parents 0afa0820 606d8ea0
......@@ -12,6 +12,7 @@ import bolts.Task;
import chat.rocket.android.helper.CheckSum;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.service.ConnectivityManager;
import chat.rocket.core.models.ServerInfo;
import chat.rocket.persistence.realm.models.ddp.RealmPermission;
import chat.rocket.persistence.realm.models.ddp.RealmPublicSetting;
import chat.rocket.core.SyncState;
......@@ -261,6 +262,13 @@ public class MethodCallHelper {
return call("logout", TIMEOUT_MS).onSuccessTask(task ->
realmHelper.executeTransaction(realm -> {
realm.delete(RealmSession.class);
//check whether the server list is empty
if (!ConnectivityManager.getInstance(context).getServerList().isEmpty()){
//for each server in serverList -> remove the server
for (ServerInfo server: ConnectivityManager.getInstance(context).getServerList()) {
ConnectivityManager.getInstance(context.getApplicationContext()).removeServer(server.getHostname());
}
}
return null;
}));
}
......
......@@ -267,6 +267,9 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> {
presenter.onLogout();
closeUserActionContainer();
// destroy Activity on logout to be able to recreate most of the environment
this.getActivity().finish();
});
}
......
......@@ -238,7 +238,9 @@ import rx.subjects.PublishSubject;
serverConnectivityList.put(hostname, ServerConnectivity.STATE_DISCONNECTING);
if (serviceInterface != null) {
return serviceInterface.disconnectFromServer(hostname);
return serviceInterface.disconnectFromServer(hostname)
// //after disconnection from server, remove HOSTNAME key from HashMap
.doAfterTerminate(() -> serverConnectivityList.remove(hostname));
} else {
return Single.error(new IllegalStateException("not prepared"));
}
......
......@@ -10,6 +10,9 @@ import android.support.annotation.Nullable;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import chat.rocket.android.activity.MainActivity;
import chat.rocket.persistence.realm.RealmStore;
import hugo.weaving.DebugLog;
import rx.Observable;
import rx.Single;
......@@ -49,7 +52,6 @@ public class RocketChatService extends Service implements ConnectivityServiceInt
@Override
public void onCreate() {
super.onCreate();
connectivityManager = ConnectivityManager.getInstanceForInternal(getApplicationContext());
connectivityManager.resetConnectivityStateList();
webSocketThreads = new HashMap<>();
......@@ -80,7 +82,20 @@ public class RocketChatService extends Service implements ConnectivityServiceInt
RocketChatWebSocketThread thread = webSocketThreads.get(hostname);
if (thread != null) {
return thread.terminate();
return thread.terminate()
// after disconnection from server
.doAfterTerminate(() -> {
// remove RCWebSocket key from HashMap
webSocketThreads.remove(hostname);
// remove RealmConfiguration key from HashMap
RealmStore.sStore.remove(hostname);
// clear "cache" SharedPreference
this.getSharedPreferences("cache", 0).edit().clear().apply();
// start a fresh new MainActivity
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
});
} else {
return Observable.timer(1, TimeUnit.SECONDS).toSingle()
.flatMap(_val -> disconnectFromServer(hostname));
......
......@@ -70,4 +70,16 @@ public class Migration implements RealmMigration {
messageSchema.addField(RealmMessage.EDITED_AT, long.class);
}
}
// hack around to avoid "new different configuration cannot access the same file" error
@Override
public int hashCode() {
return 37;
}
@Override
public boolean equals(Object o) {
return (o instanceof Migration);
}
// end hack
}
......@@ -16,6 +16,8 @@ public class RealmStore {
.modules(new RocketChatLibraryModule())
.migration(new Migration())
.schemaVersion(5)
// Just in case
.deleteRealmIfMigrationNeeded()
.build();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment