Commit eb3d41bb authored by Leonardo Aramaki's avatar Leonardo Aramaki

Keep alive the server when tapping a notification

parent a088ec8b
...@@ -74,8 +74,10 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -74,8 +74,10 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
onHostnameUpdated(); onHostnameUpdated();
} }
} else { } else {
presenter.bindViewOnly(this); ConnectivityManager.getInstance(getApplicationContext()).keepAliveServer();
presenter.bindView(this);
presenter.loadSignedInServers(hostname); presenter.loadSignedInServers(hostname);
roomId = new RocketChatCache(getApplicationContext()).getSelectedRoomId();
} }
} }
...@@ -302,6 +304,15 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -302,6 +304,15 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
} }
} }
@Override
public void refreshRoom() {
Fragment fragment = getSupportFragmentManager().findFragmentById(getLayoutContainerForFragment());
if (fragment != null && fragment instanceof RoomFragment) {
RoomFragment roomFragment = (RoomFragment) fragment;
roomFragment.loadMessages();
}
}
private void changeServerIfNeeded(String serverHostname) { private void changeServerIfNeeded(String serverHostname) {
if (!hostname.equalsIgnoreCase(serverHostname)) { if (!hostname.equalsIgnoreCase(serverHostname)) {
RocketChatCache rocketChatCache = new RocketChatCache(getApplicationContext()); RocketChatCache rocketChatCache = new RocketChatCache(getApplicationContext());
...@@ -328,7 +339,6 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -328,7 +339,6 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
public static final int STATUS_DISMISS = 0; public static final int STATUS_DISMISS = 0;
public static final int STATUS_CONNECTION_ERROR = 1; public static final int STATUS_CONNECTION_ERROR = 1;
public static final int STATUS_TOKEN_LOGIN = 2; public static final int STATUS_TOKEN_LOGIN = 2;
public static final int STATUS_LOGGING_OUT = 3;
private int status; private int status;
private Snackbar snackbar; private Snackbar snackbar;
......
...@@ -26,6 +26,8 @@ public interface MainContract { ...@@ -26,6 +26,8 @@ public interface MainContract {
void showConnectionOk(); void showConnectionOk();
void showSignedInServers(List<Pair<String, Pair<String, String>>> serverList); void showSignedInServers(List<Pair<String, Pair<String, String>>> serverList);
void refreshRoom();
} }
interface Presenter extends BaseContract.Presenter<View> { interface Presenter extends BaseContract.Presenter<View> {
......
...@@ -225,6 +225,7 @@ public class MainPresenter extends BasePresenter<MainContract.View> ...@@ -225,6 +225,7 @@ public class MainPresenter extends BasePresenter<MainContract.View>
connectivity -> { connectivity -> {
if (connectivity.state == ServerConnectivity.STATE_CONNECTED) { if (connectivity.state == ServerConnectivity.STATE_CONNECTED) {
view.showConnectionOk(); view.showConnectionOk();
view.refreshRoom();
return; return;
} }
view.showConnecting(); view.showConnecting();
......
...@@ -701,4 +701,8 @@ public class RoomFragment extends AbstractChatRoomFragment implements ...@@ -701,4 +701,8 @@ public class RoomFragment extends AbstractChatRoomFragment implements
startActivity(intent); startActivity(intent);
} }
} }
public void loadMessages() {
presenter.loadMessages();
}
} }
\ No newline at end of file
...@@ -348,8 +348,8 @@ object PushManager { ...@@ -348,8 +348,8 @@ object PushManager {
val userMessages = pushMessageList.filter { val userMessages = pushMessageList.filter {
it.notificationId == lastPushMessage.notificationId } it.notificationId == lastPushMessage.notificationId }
builder.setContentTitle(getTitle(userMessages.size, title)) builder.setContentTitle(getTitle(messageCount, title))
inbox.setBigContentTitle(getTitle(userMessages.size, title)) inbox.setBigContentTitle(getTitle(messageCount, title))
for (push in userMessages) { for (push in userMessages) {
inbox.addLine(push.message) inbox.addLine(push.message)
...@@ -601,11 +601,9 @@ object PushManager { ...@@ -601,11 +601,9 @@ object PushManager {
roomUserTuple.flatMap { tuple -> messageInteractor.send(tuple.first, tuple.second, message as String) } roomUserTuple.flatMap { tuple -> messageInteractor.send(tuple.first, tuple.second, message as String) }
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe({ success ->
{ success ->
// Empty // Empty
}, }, { throwable ->
{ throwable ->
throwable.printStackTrace() throwable.printStackTrace()
Logger.report(throwable) Logger.report(throwable)
}) })
......
package chat.rocket.android.service; package chat.rocket.android.service;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import java.util.List; import java.util.List;
...@@ -22,4 +23,6 @@ public interface ConnectivityManagerApi { ...@@ -22,4 +23,6 @@ public interface ConnectivityManagerApi {
List<ServerInfo> getServerList(); List<ServerInfo> getServerList();
Observable<ServerConnectivity> getServerConnectivityAsObservable(); Observable<ServerConnectivity> getServerConnectivityAsObservable();
int getConnectivityState(@NonNull String hostname);
} }
...@@ -4,6 +4,7 @@ import android.content.ComponentName; ...@@ -4,6 +4,7 @@ import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.os.IBinder; import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -148,6 +149,11 @@ import rx.subjects.PublishSubject; ...@@ -148,6 +149,11 @@ import rx.subjects.PublishSubject;
return Observable.concat(Observable.from(getCurrentConnectivityList()), connectivitySubject); return Observable.concat(Observable.from(getCurrentConnectivityList()), connectivitySubject);
} }
@Override
public int getConnectivityState(@NonNull String hostname) {
return serverConnectivityList.get(hostname);
}
@DebugLog @DebugLog
private Single<Boolean> connectToServerIfNeeded(String hostname, boolean forceConnect) { private Single<Boolean> connectToServerIfNeeded(String hostname, boolean forceConnect) {
return Single.defer(() -> { return Single.defer(() -> {
......
...@@ -105,11 +105,14 @@ public class RocketChatService extends Service implements ConnectivityServiceInt ...@@ -105,11 +105,14 @@ public class RocketChatService extends Service implements ConnectivityServiceInt
private Single<RocketChatWebSocketThread> getOrCreateWebSocketThread(String hostname) { private Single<RocketChatWebSocketThread> getOrCreateWebSocketThread(String hostname) {
return Single.defer(() -> { return Single.defer(() -> {
webSocketThreadLock.acquire(); webSocketThreadLock.acquire();
if (webSocketThreads.containsKey(hostname)) { int connectivityState = ConnectivityManager.getInstance(getApplicationContext()).getConnectivityState(hostname);
boolean isConnected = connectivityState == ServerConnectivity.STATE_CONNECTED;
if (webSocketThreads.containsKey(hostname) && isConnected) {
RocketChatWebSocketThread thread = webSocketThreads.get(hostname); RocketChatWebSocketThread thread = webSocketThreads.get(hostname);
webSocketThreadLock.release(); webSocketThreadLock.release();
return Single.just(thread); return Single.just(thread);
} }
connectivityManager.notifyConnecting(hostname);
return RocketChatWebSocketThread.getStarted(getApplicationContext(), hostname) return RocketChatWebSocketThread.getStarted(getApplicationContext(), hostname)
.doOnSuccess(thread -> { .doOnSuccess(thread -> {
webSocketThreads.put(hostname, thread); webSocketThreads.put(hostname, thread);
......
...@@ -209,6 +209,8 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -209,6 +209,8 @@ public class RocketChatWebSocketThread extends HandlerThread {
if (task.isFaulted()) { if (task.isFaulted()) {
Exception error = task.getError(); Exception error = task.getError();
RCLog.e(error); RCLog.e(error);
connectivityManager.notifyConnectionLost(
hostname, ConnectivityManagerInternal.REASON_NETWORK_ERROR);
emitter.onError(error); emitter.onError(error);
} else { } else {
keepAliveTimer.update(); keepAliveTimer.update();
......
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