Commit 06326eb3 authored by Tiago Cunha's avatar Tiago Cunha

PR related fixes

parent 529825c9
...@@ -5,21 +5,14 @@ import android.os.Bundle; ...@@ -5,21 +5,14 @@ import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import bolts.Task;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.fragment.server_config.LoginFragment; import chat.rocket.android.fragment.server_config.LoginFragment;
import chat.rocket.android.fragment.server_config.RetryConnectFragment; import chat.rocket.android.fragment.server_config.RetryConnectFragment;
import chat.rocket.android.fragment.server_config.RetryLoginFragment; import chat.rocket.android.fragment.server_config.RetryLoginFragment;
import chat.rocket.android.fragment.server_config.WaitingFragment; import chat.rocket.android.fragment.server_config.WaitingFragment;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.TextUtils; import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.ServerConfig; import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.ddp.PublicSetting;
import chat.rocket.android.model.ddp.PublicSettingsConstants;
import chat.rocket.android.model.internal.Session; import chat.rocket.android.model.internal.Session;
import chat.rocket.android.push.gcm.GcmRegistrationIntentService;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmObjectObserver; import chat.rocket.android.realm_helper.RealmObjectObserver;
import chat.rocket.android.realm_helper.RealmStore; import chat.rocket.android.realm_helper.RealmStore;
import chat.rocket.android.service.RocketChatService; import chat.rocket.android.service.RocketChatService;
...@@ -57,12 +50,13 @@ public class ServerConfigActivity extends AbstractFragmentActivity { ...@@ -57,12 +50,13 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
serverConfigErrorObserver = RealmStore.getDefault() serverConfigErrorObserver = RealmStore.getDefault()
.createObjectObserver(realm -> .createObjectObserver(realm ->
realm.where(ServerConfig.class) realm.where(ServerConfig.class)
.equalTo(ServerConfig.ID, serverConfigId)) .equalTo(ServerConfig.ID, serverConfigId)
.equalTo(ServerConfig.STATE, ServerConfig.STATE_CONNECTION_ERROR))
.setOnUpdateListener(this::onRenderServerConfigError); .setOnUpdateListener(this::onRenderServerConfigError);
sessionObserver = RealmStore.get(serverConfigId) sessionObserver = RealmStore.get(serverConfigId)
.createObjectObserver(Session::queryDefaultSession) .createObjectObserver(Session::queryDefaultSession)
.setOnUpdateListener(this::continueWithServerConfigSession); .setOnUpdateListener(this::onRenderServerConfigSession);
setContentView(R.layout.simple_screen); setContentView(R.layout.simple_screen);
showFragment(new WaitingFragment()); showFragment(new WaitingFragment());
...@@ -83,7 +77,7 @@ public class ServerConfigActivity extends AbstractFragmentActivity { ...@@ -83,7 +77,7 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
} }
private void onRenderServerConfigError(ServerConfig config) { private void onRenderServerConfigError(ServerConfig config) {
if (config.getState() == ServerConfig.STATE_CONNECTION_ERROR) { if (config != null) {
sessionObserver.unsub(); sessionObserver.unsub();
showFragment(new RetryConnectFragment()); showFragment(new RetryConnectFragment());
} else { } else {
...@@ -91,19 +85,6 @@ public class ServerConfigActivity extends AbstractFragmentActivity { ...@@ -91,19 +85,6 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
} }
} }
private void continueWithServerConfigSession(final Session session) {
fetchPublicSettings()
.continueWith(task -> {
registerForPush();
return task;
})
.continueWith(task -> {
onRenderServerConfigSession(session);
return task;
})
.continueWith(new LogcatIfError());
}
private void onRenderServerConfigSession(Session session) { private void onRenderServerConfigSession(Session session) {
if (session == null) { if (session == null) {
showFragment(new LoginFragment()); showFragment(new LoginFragment());
...@@ -151,44 +132,6 @@ public class ServerConfigActivity extends AbstractFragmentActivity { ...@@ -151,44 +132,6 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
fragment.setArguments(args); fragment.setArguments(args);
} }
private Task<Void> fetchPublicSettings() {
return new MethodCallHelper(this, serverConfigId).getPublicSettings();
}
private void registerForPush() {
RealmHelper realmHelper = RealmStore.getDefault();
final ServerConfig serverConfig = realmHelper.executeTransactionForRead(
realm -> realm.where(ServerConfig.class).equalTo(ServerConfig.ID, serverConfigId)
.findFirst());
serverConfig.setSyncPushToken(isPushEnabled());
realmHelper
.executeTransaction(realm -> realm.copyToRealmOrUpdate(serverConfig))
.continueWith(task -> {
if (serverConfig.shouldSyncPushToken()) {
Intent intent = new Intent(this, GcmRegistrationIntentService.class);
startService(intent);
}
return task;
})
.continueWith(new LogcatIfError());
}
private boolean isPushEnabled() {
RealmHelper realmHelper = RealmStore.getOrCreate(serverConfigId);
boolean isPushEnable = PublicSetting
.getBoolean(realmHelper, PublicSettingsConstants.Push.ENABLE, false);
String senderId = PublicSetting
.getString(realmHelper, PublicSettingsConstants.Push.GCM_PROJECT_NUMBER, "").trim();
return isPushEnable && !"".equals(senderId);
}
@Override @Override
protected void onBackPressedNotHandled() { protected void onBackPressedNotHandled() {
moveTaskToBack(true); moveTaskToBack(true);
......
...@@ -73,6 +73,7 @@ public class ServerPolicyHelper { ...@@ -73,6 +73,7 @@ public class ServerPolicyHelper {
private static String removeTrailingSlash(String hostname) { private static String removeTrailingSlash(String hostname) {
if (hostname.charAt(hostname.length() - 1) != '/') { if (hostname.charAt(hostname.length() - 1) != '/') {
// no need for a regex - just return it
return hostname; return hostname;
} }
......
...@@ -12,6 +12,7 @@ import bolts.Continuation; ...@@ -12,6 +12,7 @@ import bolts.Continuation;
import bolts.Task; import bolts.Task;
import bolts.TaskCompletionSource; import bolts.TaskCompletionSource;
import chat.rocket.android.api.DDPClientWrapper; import chat.rocket.android.api.DDPClientWrapper;
import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.helper.LogcatIfError; import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.TextUtils; import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.log.RCLog; import chat.rocket.android.log.RCLog;
...@@ -29,6 +30,7 @@ import chat.rocket.android.service.observer.GetUsersOfRoomsProcedureObserver; ...@@ -29,6 +30,7 @@ import chat.rocket.android.service.observer.GetUsersOfRoomsProcedureObserver;
import chat.rocket.android.service.observer.LoadMessageProcedureObserver; import chat.rocket.android.service.observer.LoadMessageProcedureObserver;
import chat.rocket.android.service.observer.MethodCallObserver; import chat.rocket.android.service.observer.MethodCallObserver;
import chat.rocket.android.service.observer.NewMessageObserver; import chat.rocket.android.service.observer.NewMessageObserver;
import chat.rocket.android.service.observer.PushSettingsObserver;
import chat.rocket.android.service.observer.ReactiveNotificationManager; import chat.rocket.android.service.observer.ReactiveNotificationManager;
import chat.rocket.android.service.observer.SessionObserver; import chat.rocket.android.service.observer.SessionObserver;
import chat.rocket.android.service.observer.TokenLoginObserver; import chat.rocket.android.service.observer.TokenLoginObserver;
...@@ -52,7 +54,8 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -52,7 +54,8 @@ public class RocketChatWebSocketThread extends HandlerThread {
CurrentUserObserver.class, CurrentUserObserver.class,
ReactiveNotificationManager.class, ReactiveNotificationManager.class,
FileUploadingToS3Observer.class, FileUploadingToS3Observer.class,
FileUploadingWithUfsObserver.class FileUploadingWithUfsObserver.class,
PushSettingsObserver.class
}; };
private final Context appContext; private final Context appContext;
private final String serverConfigId; private final String serverConfigId;
...@@ -165,62 +168,68 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -165,62 +168,68 @@ public class RocketChatWebSocketThread extends HandlerThread {
realm.where(ServerConfig.class).equalTo(ServerConfig.ID, serverConfigId).findFirst()); realm.where(ServerConfig.class).equalTo(ServerConfig.ID, serverConfigId).findFirst());
prepareWebSocket(config.getHostname()); prepareWebSocket(config.getHostname());
return ddpClient.connect(config.getSession()).onSuccessTask(task -> { return ddpClient.connect(config.getSession())
final String session = task.getResult().session; .onSuccessTask(task -> {
defaultRealm.executeTransaction(realm -> final String session = task.getResult().session;
realm.createOrUpdateObjectFromJson(ServerConfig.class, new JSONObject() defaultRealm.executeTransaction(realm ->
.put("serverConfigId", serverConfigId) realm.createOrUpdateObjectFromJson(ServerConfig.class, new JSONObject()
.put("session", session)) .put("serverConfigId", serverConfigId)
).onSuccess(_task -> serverConfigRealm.executeTransaction(realm -> { .put("session", session))
Session sessionObj = Session.queryDefaultSession(realm).findFirst(); ).onSuccess(_task -> serverConfigRealm.executeTransaction(realm -> {
Session sessionObj = Session.queryDefaultSession(realm).findFirst();
if (sessionObj == null) { if (sessionObj == null) {
realm.createOrUpdateObjectFromJson(Session.class, realm.createOrUpdateObjectFromJson(Session.class,
new JSONObject().put("sessionId", Session.DEFAULT_ID)); new JSONObject().put("sessionId", Session.DEFAULT_ID));
} }
return null; return null;
})).continueWith(new LogcatIfError()); })).continueWith(new LogcatIfError());
return task; return task;
}).onSuccess(new Continuation<DDPClientCallback.Connect, Void>() { })
// TODO type detection doesn't work due to retrolambda's bug... .onSuccess(new Continuation<DDPClientCallback.Connect, Void>() {
@Override // TODO type detection doesn't work due to retrolambda's bug...
public Void then(Task<DDPClientCallback.Connect> task) @Override
throws Exception { public Void then(Task<DDPClientCallback.Connect> task)
registerListeners(); throws Exception {
fetchPublicSettings();
registerListeners();
// handling WebSocket#onClose() callback. // handling WebSocket#onClose() callback.
task.getResult().client.getOnCloseCallback().onSuccess(_task -> { task.getResult().client.getOnCloseCallback().onSuccess(_task -> {
quit(); quit();
return null; return null;
}).continueWithTask(_task -> { }).continueWithTask(_task -> {
if (_task.isFaulted()) { if (_task.isFaulted()) {
ServerConfig.logConnectionError(serverConfigId, _task.getError()); ServerConfig.logConnectionError(serverConfigId, _task.getError());
}
return _task;
});
return null;
}
})
.continueWithTask(task -> {
if (task.isFaulted()) {
Exception error = task.getError();
if (error instanceof DDPClientCallback.Connect.Timeout) {
ServerConfig.logConnectionError(serverConfigId, new Exception("Connection Timeout"));
} else {
ServerConfig.logConnectionError(serverConfigId, task.getError());
}
} }
return _task; return task;
}); });
}
return null; private Task<Void> fetchPublicSettings() {
} return new MethodCallHelper(serverConfigRealm, ddpClient).getPublicSettings();
}).continueWithTask(task -> {
if (task.isFaulted()) {
Exception error = task.getError();
if (error instanceof DDPClientCallback.Connect.Timeout) {
ServerConfig.logConnectionError(serverConfigId, new Exception("Connection Timeout"));
} else {
ServerConfig.logConnectionError(serverConfigId, task.getError());
}
}
return task;
});
} }
//@DebugLog //@DebugLog
private void registerListeners() { private void registerListeners() {
if (!Thread.currentThread().getName().equals("RC_thread_" + serverConfigId)) { if (!Thread.currentThread().getName().equals("RC_thread_" + serverConfigId)) {
// execute in Looper. // execute in Looper.
new Handler(getLooper()).post(() -> { new Handler(getLooper()).post(this::registerListeners);
registerListeners();
});
return; return;
} }
......
package chat.rocket.android.service.observer;
import android.content.Context;
import android.content.Intent;
import io.realm.Realm;
import io.realm.RealmResults;
import java.util.List;
import chat.rocket.android.api.DDPClientWrapper;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.ddp.PublicSetting;
import chat.rocket.android.model.ddp.PublicSettingsConstants;
import chat.rocket.android.push.gcm.GcmRegistrationIntentService;
import chat.rocket.android.push.interactors.DefaultPushInteractor;
import chat.rocket.android.push.interactors.PushInteractor;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmStore;
public class PushSettingsObserver extends AbstractModelObserver<PublicSetting> {
public PushSettingsObserver(Context context, String hostname,
RealmHelper realmHelper, DDPClientWrapper ddpClient) {
super(context, hostname, realmHelper, ddpClient);
}
@Override
public void onUpdateResults(List<PublicSetting> results) {
RealmHelper realmHelper = RealmStore.getDefault();
PushInteractor interactor = new DefaultPushInteractor();
String serverConfigId = interactor.getServerConfigId(hostname);
final ServerConfig serverConfig = realmHelper.executeTransactionForRead(
realm -> realm.where(ServerConfig.class).equalTo(ServerConfig.ID, serverConfigId)
.findFirst());
serverConfig.setSyncPushToken(isPushEnabled(results));
realmHelper
.executeTransaction(realm -> realm.copyToRealmOrUpdate(serverConfig))
.continueWith(task -> {
if (serverConfig.shouldSyncPushToken()) {
Intent intent = new Intent(
context.getApplicationContext(), GcmRegistrationIntentService.class);
context.getApplicationContext().startService(intent);
}
return task;
})
.continueWith(new LogcatIfError());
}
@Override
public RealmResults<PublicSetting> queryItems(Realm realm) {
return realm.where(PublicSetting.class)
.equalTo(PublicSetting.ID, PublicSettingsConstants.Push.ENABLE)
.or()
.equalTo(PublicSetting.ID, PublicSettingsConstants.Push.GCM_PROJECT_NUMBER)
.findAll();
}
private boolean isPushEnabled(List<PublicSetting> results) {
return isPushEnable(results) && isGcmValid(results);
}
private boolean isPushEnable(List<PublicSetting> results) {
for (PublicSetting setting : results) {
if (PublicSettingsConstants.Push.ENABLE.equals(setting.getId())) {
return "true".equals(setting.getValue());
}
}
return false;
}
private boolean isGcmValid(List<PublicSetting> results) {
for (PublicSetting setting : results) {
if (PublicSettingsConstants.Push.GCM_PROJECT_NUMBER.equals(setting.getId())) {
return setting.getValue() != null && !"".equals(setting.getValue());
}
}
return false;
}
}
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