Commit cf18e7f8 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Implement go back action from LoginActivity to fix the user getting locked at this screen

parent b1514363
......@@ -2,10 +2,13 @@ package chat.rocket.android;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.hadisatrio.optional.Optional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -20,8 +23,6 @@ import chat.rocket.android.log.RCLog;
import chat.rocket.core.utils.Pair;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;
import okhttp3.HttpUrl;
/**
......@@ -38,13 +39,12 @@ public class RocketChatCache {
private static final String KEY_SESSION_TOKEN = "KEY_SESSION_TOKEN";
private Context context;
private String session;
public RocketChatCache(Context context) {
this.context = context.getApplicationContext();
}
public void addOpenedRoom(@NonNull String roomId, long lastSeen) {
public void addOpenedRoom(@NotNull String roomId, long lastSeen) {
JSONObject openedRooms = getOpenedRooms();
try {
JSONObject room = new JSONObject().put("rid", roomId).put("ls", lastSeen);
......@@ -55,15 +55,15 @@ public class RocketChatCache {
setString(KEY_OPENED_ROOMS, openedRooms.toString());
}
public void removeOpenedRoom(@NonNull String roomId) {
public void removeOpenedRoom(@NotNull String roomId) {
JSONObject openedRooms = getOpenedRooms();
if (openedRooms.has(roomId)) {
openedRooms.remove(roomId);
}
}
public @NonNull
JSONObject getOpenedRooms() {
@NotNull
public JSONObject getOpenedRooms() {
String openedRooms = getString(KEY_OPENED_ROOMS, "");
if (openedRooms.isEmpty()) {
return new JSONObject();
......@@ -88,9 +88,9 @@ public class RocketChatCache {
setString(KEY_SELECTED_SERVER_HOSTNAME, newHostname);
}
public void addHostSiteName(@NonNull String currentHostname, @NonNull String siteName) {
public void addSiteName(@NotNull String currentHostname, @NotNull String siteName) {
try {
String hostSiteNamesJson = getHostSiteNamesJson();
String hostSiteNamesJson = getSiteName();
JSONObject jsonObject = (hostSiteNamesJson == null) ?
new JSONObject() : new JSONObject(hostSiteNamesJson);
jsonObject.put(currentHostname, siteName);
......@@ -100,8 +100,22 @@ public class RocketChatCache {
}
}
public @NonNull
String getHostSiteName(@NonNull String host) {
public void removeSiteName(@NotNull String hostname) {
try {
String siteNameJson = getSiteName();
JSONObject jsonObject = (siteNameJson == null) ?
new JSONObject() : new JSONObject(siteNameJson);
if (jsonObject.has(hostname)) {
jsonObject.remove(hostname);
}
setString(KEY_SELECTED_SITE_NAME, jsonObject.toString());
} catch (JSONException e) {
RCLog.e(e);
}
}
@NotNull
public String getHostSiteName(@NotNull String host) {
if (host.startsWith("http")) {
HttpUrl url = HttpUrl.parse(host);
if (url != null) {
......@@ -109,7 +123,7 @@ public class RocketChatCache {
}
}
try {
String hostSiteNamesJson = getHostSiteNamesJson();
String hostSiteNamesJson = getSiteName();
JSONObject jsonObject = (hostSiteNamesJson == null) ?
new JSONObject() : new JSONObject(hostSiteNamesJson);
host = getSiteUrlFor(host);
......@@ -120,18 +134,18 @@ public class RocketChatCache {
return "";
}
private @Nullable
String getHostSiteNamesJson() {
@Nullable
private String getSiteName() {
return getString(KEY_SELECTED_SITE_NAME, null);
}
public void addHostnameSiteUrl(@Nullable String hostnameAlias, @NonNull String currentHostname) {
public void addSiteUrl(@Nullable String hostnameAlias, @NotNull String currentHostname) {
String alias = null;
if (hostnameAlias != null) {
alias = hostnameAlias.toLowerCase();
}
try {
String selectedHostnameAliasJson = getLoginHostnamesJson();
String selectedHostnameAliasJson = getSiteUrlForAllServers();
JSONObject jsonObject = selectedHostnameAliasJson == null ?
new JSONObject() : new JSONObject(selectedHostnameAliasJson);
jsonObject.put(alias, currentHostname);
......@@ -141,14 +155,33 @@ public class RocketChatCache {
}
}
public @Nullable
String getSiteUrlFor(String hostname) {
private void removeSiteUrl(@NotNull String hostname) {
try {
String siteUrlForAllServersJson = getSiteUrlForAllServers();
JSONObject jsonObject = siteUrlForAllServersJson == null ?
new JSONObject() : new JSONObject(siteUrlForAllServersJson);
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()) {
String alias = keys.next();
if (hostname.equals(jsonObject.getString(alias))) {
jsonObject.remove(alias);
break;
}
}
setString(KEY_SELECTED_SITE_URL, jsonObject.toString());
} catch (JSONException e) {
RCLog.e(e);
}
}
@Nullable
public String getSiteUrlFor(String hostname) {
try {
String selectedServerHostname = getSelectedServerHostname();
if (getLoginHostnamesJson() == null || getLoginHostnamesJson().isEmpty()) {
if (getSiteUrlForAllServers() == null || getSiteUrlForAllServers().isEmpty()) {
return null;
}
return new JSONObject(getLoginHostnamesJson())
return new JSONObject(getSiteUrlForAllServers())
.optString(hostname, selectedServerHostname);
} catch (JSONException e) {
RCLog.e(e);
......@@ -156,12 +189,12 @@ public class RocketChatCache {
return null;
}
private @Nullable
String getLoginHostnamesJson() {
@Nullable
private String getSiteUrlForAllServers() {
return getString(KEY_SELECTED_SITE_URL, null);
}
public void addHostname(@NonNull String hostname, @Nullable String hostnameAvatarUri, String siteName) {
public void addHostname(@NotNull String hostname, @Nullable String hostnameAvatarUri, String siteName) {
String hostnameList = getString(KEY_HOSTNAME_LIST, null);
try {
JSONObject json;
......@@ -257,6 +290,7 @@ public class RocketChatCache {
}
}
@NonNull
private JSONObject getSelectedRoomIdJsonObject() throws JSONException {
String json = getString(KEY_SELECTED_ROOM_ID, null);
if (json == null) {
......@@ -367,4 +401,17 @@ public class RocketChatCache {
return null;
}
/**
* Wipe all given hostname entries and references from cache.
*/
public void clearSelectedHostnameReferences() {
String hostname = getSelectedServerHostname();
if (hostname != null) {
removeSiteName(hostname);
removeHostname(hostname);
removeSiteUrl(hostname);
setSelectedServerHostname(null);
}
}
}
......@@ -512,8 +512,8 @@ public class MethodCallHelper {
if (httpSiteUrl != null) {
String host = httpSiteUrl.host();
RocketChatCache rocketChatCache = new RocketChatCache(context);
rocketChatCache.addHostnameSiteUrl(host, currentHostname);
rocketChatCache.addHostSiteName(currentHostname, siteName);
rocketChatCache.addSiteUrl(host, currentHostname);
rocketChatCache.addSiteName(currentHostname, siteName);
}
}
......
......@@ -11,7 +11,7 @@ import chat.rocket.android.fragment.AbstractFragment;
import chat.rocket.android.widget.RoomToolbar;
import chat.rocket.core.models.User;
abstract class AbstractChatRoomFragment extends AbstractFragment {
public abstract class AbstractChatRoomFragment extends AbstractFragment {
private RoomToolbar roomToolbar;
@Nullable
......
......@@ -131,7 +131,7 @@ public class RoomFragment extends AbstractChatRoomFragment implements
private MethodCallHelper methodCallHelper;
private AbsoluteUrlHelper absoluteUrlHelper;
private Message edittingMessage = null;
private Message editingMessage = null;
private RoomToolbar toolbar;
......@@ -344,7 +344,7 @@ public class RoomFragment extends AbstractChatRoomFragment implements
optionalPane.ifPresent(pane -> pane.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
@Override
public void onPanelSlide(View view, float v) {
public void onPanelSlide(@NonNull View view, float v) {
messageFormManager.enableComposingText(false);
sidebarFragment.clearSearchViewFocus();
//Ref: ActionBarDrawerToggle#setProgress
......@@ -352,12 +352,12 @@ public class RoomFragment extends AbstractChatRoomFragment implements
}
@Override
public void onPanelOpened(View view) {
public void onPanelOpened(@NonNull View view) {
toolbar.setNavigationIconVerticalMirror(true);
}
@Override
public void onPanelClosed(View view) {
public void onPanelClosed(@NonNull View view) {
messageFormManager.enableComposingText(true);
toolbar.setNavigationIconVerticalMirror(false);
subPane.closePane();
......@@ -487,8 +487,8 @@ public class RoomFragment extends AbstractChatRoomFragment implements
@Override
public boolean onBackPressed() {
if (edittingMessage != null) {
edittingMessage = null;
if (editingMessage != null) {
editingMessage = null;
messageFormManager.clearComposingText();
}
return false;
......@@ -547,15 +547,15 @@ public class RoomFragment extends AbstractChatRoomFragment implements
}
private void sendMessage(String messageText) {
if (edittingMessage == null) {
if (editingMessage == null) {
presenter.sendMessage(messageText);
} else {
presenter.updateMessage(edittingMessage, messageText);
presenter.updateMessage(editingMessage, messageText);
}
}
@Override
public void setupWith(RocketChatAbsoluteUrl rocketChatAbsoluteUrl) {
public void setupWith(@NonNull RocketChatAbsoluteUrl rocketChatAbsoluteUrl) {
if (rocketChatAbsoluteUrl != null) {
token = rocketChatAbsoluteUrl.getToken();
userId = rocketChatAbsoluteUrl.getUserId();
......@@ -564,7 +564,7 @@ public class RoomFragment extends AbstractChatRoomFragment implements
}
@Override
public void render(Room room) {
public void render(@NonNull Room room) {
roomType = room.getType();
setToolbarTitle(room.getName());
......@@ -589,7 +589,7 @@ public class RoomFragment extends AbstractChatRoomFragment implements
}
@Override
public void showUserStatus(User user) {
public void showUserStatus(@NonNull User user) {
showToolbarUserStatuslIcon(user.getStatus());
}
......@@ -610,7 +610,7 @@ public class RoomFragment extends AbstractChatRoomFragment implements
public void onMessageSendSuccessfully() {
scrollToLatestMessage();
messageFormManager.onMessageSend();
edittingMessage = null;
editingMessage = null;
}
@Override
......@@ -629,15 +629,16 @@ public class RoomFragment extends AbstractChatRoomFragment implements
}
@Override
public void showMessages(List<Message> messages) {
public void showMessages(@NonNull List<? extends Message> messages) {
if (messageListAdapter == null) {
return;
}
messageListAdapter.updateData(messages);
messageListAdapter.updateData((List<Message>) messages);
}
@Override
public void showMessageSendFailure(Message message) {
public void showMessageSendFailure(@NonNull Message message) {
new AlertDialog.Builder(getContext())
.setPositiveButton(R.string.resend,
(dialog, which) -> presenter.resendMessage(message))
......@@ -648,7 +649,7 @@ public class RoomFragment extends AbstractChatRoomFragment implements
}
@Override
public void showMessageDeleteFailure(Message message) {
public void showMessageDeleteFailure(@NonNull Message message) {
new AlertDialog.Builder(getContext())
.setTitle(getContext().getString(R.string.failed_to_delete))
.setMessage(getContext().getString(R.string.failed_to_delete_message))
......@@ -667,12 +668,12 @@ public class RoomFragment extends AbstractChatRoomFragment implements
}
@Override
public void onReply(AbsoluteUrl absoluteUrl, String markdown, Message message) {
public void onReply(@NonNull AbsoluteUrl absoluteUrl, @NonNull String markdown, @NonNull Message message) {
messageFormManager.setReply(absoluteUrl, markdown, message);
}
@Override
public void onCopy(String message) {
public void onCopy(@NonNull String message) {
RocketChatApplication context = RocketChatApplication.getInstance();
ClipboardManager clipboardManager =
(ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
......@@ -680,7 +681,7 @@ public class RoomFragment extends AbstractChatRoomFragment implements
}
@Override
public void showMessageActions(Message message) {
public void showMessageActions(@NonNull Message message) {
Activity context = getActivity();
if (context != null && context instanceof MainActivity) {
MessagePopup.take(message)
......@@ -694,7 +695,7 @@ public class RoomFragment extends AbstractChatRoomFragment implements
}
private void onEditMessage(Message message) {
edittingMessage = message;
editingMessage = message;
messageFormManager.setEditMessage(message.getMessage());
}
......
......@@ -8,7 +8,7 @@ import chat.rocket.android.R;
import chat.rocket.android.fragment.AbstractFragment;
import chat.rocket.android.helper.TextUtils;
abstract class AbstractServerConfigFragment extends AbstractFragment {
public abstract class AbstractServerConfigFragment extends AbstractFragment {
public static final String KEY_HOSTNAME = "hostname";
protected String hostname;
......
......@@ -99,14 +99,14 @@ class LoginFragment : AbstractServerConfigFragment(), LoginContract.View {
showFragmentWithBackStack(fragment)
}
}
viewMap[info.serviceName]?.setVisibility(View.VISIBLE)
viewMap[info.serviceName]?.visibility = View.VISIBLE
}
}
}
for (info in OAuthProviderInfo.LIST) {
if (supportedMap[info.serviceName] == false) {
viewMap[info.serviceName]?.setVisibility(View.GONE)
viewMap[info.serviceName]?.visibility = View.GONE
}
}
}
......
......@@ -3,10 +3,14 @@ package chat.rocket.android.fragment.server_config
import bolts.Continuation
import bolts.Task
import chat.rocket.android.BackgroundLooper
import chat.rocket.android.LaunchUtil
import chat.rocket.android.RocketChatApplication
import chat.rocket.android.RocketChatCache
import chat.rocket.android.api.MethodCallHelper
import chat.rocket.android.api.TwoStepAuthException
import chat.rocket.android.helper.Logger
import chat.rocket.android.helper.TextUtils
import chat.rocket.android.service.ConnectivityManager
import chat.rocket.android.shared.BasePresenter
import chat.rocket.core.PublicSettingsConstants
import chat.rocket.core.models.PublicSetting
......@@ -26,6 +30,20 @@ class LoginPresenter(private val loginServiceConfigurationRepository: LoginServi
getLoginServices()
}
override fun release() {
val context = RocketChatApplication.getInstance()
val rocketChatCache = RocketChatCache(context)
val hostname = rocketChatCache.selectedServerHostname
hostname?.let {
ConnectivityManager.getInstance(context).removeServer(hostname)
rocketChatCache.clearSelectedHostnameReferences()
}
super.release()
LaunchUtil.showMainActivity(context)
}
override fun login(username: String, password: String) {
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
return
......@@ -50,7 +68,9 @@ class LoginPresenter(private val loginServiceConfigurationRepository: LoginServi
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onNext = { loginServiceConfigurations -> view.showLoginServices(loginServiceConfigurations) },
onNext = { loginServiceConfigurations ->
view.showLoginServices(loginServiceConfigurations);
},
onError = { Logger.report(it) }
)
)
......
package chat.rocket.android.helper
import chat.rocket.android.BuildConfig
import com.crashlytics.android.Crashlytics
import com.google.firebase.crash.FirebaseCrash
import chat.rocket.android.BuildConfig
object Logger {
fun report(throwable: Throwable) {
......
......@@ -22,7 +22,6 @@ import chat.rocket.android.BuildConfig
import chat.rocket.android.R
import chat.rocket.android.RocketChatCache
import chat.rocket.android.activity.MainActivity
import chat.rocket.android.extensions.printStackTraceOnDebug
import chat.rocket.android.helper.Logger
import chat.rocket.core.interactors.MessageInteractor
import chat.rocket.core.models.Room
......
......@@ -107,7 +107,7 @@ import io.reactivex.subjects.BehaviorSubject;
public void removeServer(String hostname) {
RealmBasedServerInfo.remove(hostname);
if (serverConnectivityList.containsKey(hostname)) {
disconnectFromServerIfNeeded(hostname)
disconnectFromServerIfNeeded(hostname, DDPClient.REASON_CLOSED_BY_USER)
.subscribe(_val -> {
}, RCLog::e);
}
......@@ -207,7 +207,7 @@ import io.reactivex.subjects.BehaviorSubject;
});
}
private Single<Boolean> disconnectFromServerIfNeeded(String hostname) {
private Single<Boolean> disconnectFromServerIfNeeded(String hostname, int reason) {
return Single.defer(() -> {
final int connectivity = serverConnectivityList.get(hostname);
if (connectivity == ServerConnectivity.STATE_DISCONNECTED) {
......@@ -216,8 +216,8 @@ import io.reactivex.subjects.BehaviorSubject;
if (connectivity == ServerConnectivity.STATE_CONNECTING) {
return waitForConnected(hostname)
.doOnError(err -> notifyConnectionLost(hostname, DDPClient.REASON_NETWORK_ERROR))
.flatMap(_val -> disconnectFromServerIfNeeded(hostname));
// .doOnError(err -> notifyConnectionLost(hostname, DDPClient.REASON_CLOSED_BY_USER))
.flatMap(_val -> disconnectFromServerIfNeeded(hostname, DDPClient.REASON_CLOSED_BY_USER));
}
if (connectivity == ServerConnectivity.STATE_DISCONNECTING) {
......@@ -286,7 +286,7 @@ import io.reactivex.subjects.BehaviorSubject;
if (serviceInterface != null) {
return serviceInterface.disconnectFromServer(hostname)
//after disconnection from server, remove HOSTNAME key from HashMap
//after disconnection from server, remove HOSTNAME key from HashMap
.doAfterTerminate(() -> {
serverConnectivityList.remove(hostname);
serverConnectivityList.put(hostname, ServerConnectivity.STATE_DISCONNECTED);
......
This diff is collapsed.
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