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; ...@@ -12,6 +12,7 @@ import bolts.Task;
import chat.rocket.android.helper.CheckSum; import chat.rocket.android.helper.CheckSum;
import chat.rocket.android.helper.TextUtils; import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.service.ConnectivityManager; 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.RealmPermission;
import chat.rocket.persistence.realm.models.ddp.RealmPublicSetting; import chat.rocket.persistence.realm.models.ddp.RealmPublicSetting;
import chat.rocket.core.SyncState; import chat.rocket.core.SyncState;
...@@ -261,6 +262,13 @@ public class MethodCallHelper { ...@@ -261,6 +262,13 @@ public class MethodCallHelper {
return call("logout", TIMEOUT_MS).onSuccessTask(task -> return call("logout", TIMEOUT_MS).onSuccessTask(task ->
realmHelper.executeTransaction(realm -> { realmHelper.executeTransaction(realm -> {
realm.delete(RealmSession.class); 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; return null;
})); }));
} }
......
...@@ -267,6 +267,9 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -267,6 +267,9 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> { rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> {
presenter.onLogout(); presenter.onLogout();
closeUserActionContainer(); closeUserActionContainer();
// destroy Activity on logout to be able to recreate most of the environment
this.getActivity().finish();
}); });
} }
......
...@@ -238,10 +238,12 @@ import rx.subjects.PublishSubject; ...@@ -238,10 +238,12 @@ import rx.subjects.PublishSubject;
serverConnectivityList.put(hostname, ServerConnectivity.STATE_DISCONNECTING); serverConnectivityList.put(hostname, ServerConnectivity.STATE_DISCONNECTING);
if (serviceInterface != null) { 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 { } else {
return Single.error(new IllegalStateException("not prepared")); return Single.error(new IllegalStateException("not prepared"));
} }
}); });
} }
} }
\ No newline at end of file
...@@ -10,6 +10,9 @@ import android.support.annotation.Nullable; ...@@ -10,6 +10,9 @@ import android.support.annotation.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import chat.rocket.android.activity.MainActivity;
import chat.rocket.persistence.realm.RealmStore;
import hugo.weaving.DebugLog; import hugo.weaving.DebugLog;
import rx.Observable; import rx.Observable;
import rx.Single; import rx.Single;
...@@ -49,7 +52,6 @@ public class RocketChatService extends Service implements ConnectivityServiceInt ...@@ -49,7 +52,6 @@ public class RocketChatService extends Service implements ConnectivityServiceInt
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
connectivityManager = ConnectivityManager.getInstanceForInternal(getApplicationContext()); connectivityManager = ConnectivityManager.getInstanceForInternal(getApplicationContext());
connectivityManager.resetConnectivityStateList(); connectivityManager.resetConnectivityStateList();
webSocketThreads = new HashMap<>(); webSocketThreads = new HashMap<>();
...@@ -80,7 +82,20 @@ public class RocketChatService extends Service implements ConnectivityServiceInt ...@@ -80,7 +82,20 @@ public class RocketChatService extends Service implements ConnectivityServiceInt
RocketChatWebSocketThread thread = webSocketThreads.get(hostname); RocketChatWebSocketThread thread = webSocketThreads.get(hostname);
if (thread != null) { 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 { } else {
return Observable.timer(1, TimeUnit.SECONDS).toSingle() return Observable.timer(1, TimeUnit.SECONDS).toSingle()
.flatMap(_val -> disconnectFromServer(hostname)); .flatMap(_val -> disconnectFromServer(hostname));
......
...@@ -70,4 +70,16 @@ public class Migration implements RealmMigration { ...@@ -70,4 +70,16 @@ public class Migration implements RealmMigration {
messageSchema.addField(RealmMessage.EDITED_AT, long.class); 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 { ...@@ -16,6 +16,8 @@ public class RealmStore {
.modules(new RocketChatLibraryModule()) .modules(new RocketChatLibraryModule())
.migration(new Migration()) .migration(new Migration())
.schemaVersion(5) .schemaVersion(5)
// Just in case
.deleteRealmIfMigrationNeeded()
.build(); .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