Commit 888c28ec authored by Yusuke Iwaki's avatar Yusuke Iwaki

implement connecting with session.

parent 5f138f58
......@@ -53,7 +53,7 @@ dependencies {
compile 'com.facebook.stetho:stetho-okhttp3:1.4.1'
compile 'com.uphyca:stetho_realm:2.0.0'
compile 'chat.rocket:android-ddp:0.0.5'
compile 'chat.rocket:android-ddp:0.0.6'
compile 'com.jakewharton.timber:timber:4.3.1'
compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'
......
......@@ -9,12 +9,11 @@ import chat.rocket.android.LaunchUtil;
import chat.rocket.android.R;
import chat.rocket.android.fragment.server_config.ConnectingToHostFragment;
import chat.rocket.android.fragment.server_config.InputHostnameFragment;
import chat.rocket.android.fragment.server_config.LoginFragment;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.MeteorLoginServiceConfiguration;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.service.RocketChatService;
import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmQuery;
import java.util.List;
import jp.co.crowdworks.realm_java_helpers.RealmObjectObserver;
......@@ -49,7 +48,7 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
}
for (ServerConfig config : configList) {
if (config.getAuthProviders().isEmpty()) {
if (TextUtils.isEmpty(config.getSession())) {
return launchFor(context, config);
}
}
......@@ -135,9 +134,8 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
return;
}
RealmList<MeteorLoginServiceConfiguration> providers = config.getAuthProviders();
if (!providers.isEmpty()) {
if (!TextUtils.isEmpty(config.getSession())) {
showFragment(new LoginFragment());
return;
}
......
......@@ -56,7 +56,8 @@ public class InputHostnameFragment extends AbstractServerConfigFragment {
realm -> realm.createOrUpdateObjectFromJson(ServerConfig.class,
new JSONObject().put("id", serverConfigId)
.put("hostname", hostname)
.put("connectionError", JSONObject.NULL))).continueWith(new LogcatIfError());
.put("connectionError", JSONObject.NULL)
.put("session", JSONObject.NULL))).continueWith(new LogcatIfError());
}
@Override public void onResume() {
......
package chat.rocket.android.fragment.server_config;
import chat.rocket.android.R;
/**
* Login screen.
*/
public class LoginFragment extends AbstractServerConfigFragment {
@Override protected int getLayout() {
return R.layout.fragment_login;
}
@Override protected void onSetupView() {
}
}
......@@ -10,6 +10,7 @@ import io.realm.annotations.PrimaryKey;
public class MeteorLoginServiceConfiguration
extends RealmObject {
@PrimaryKey private String id;
private ServerConfig serverConfig;
private String service;
private String consumerKey; //for Twitter
private String appId; //for Facebook
......
......@@ -3,7 +3,6 @@ package chat.rocket.android.model;
import chat.rocket.android.helper.LogcatIfError;
import hugo.weaving.DebugLog;
import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.RealmQuery;
import io.realm.annotations.PrimaryKey;
......@@ -19,9 +18,9 @@ public class ServerConfig extends RealmObject {
@PrimaryKey private String id;
private String hostname;
private String connectionError;
private String session;
private String token;
private boolean tokenVerified;
private RealmList<MeteorLoginServiceConfiguration> authProviders;
private String selectedProviderName;
public static RealmQuery<ServerConfig> queryLoginRequiredConnections(Realm realm) {
......@@ -41,8 +40,10 @@ public class ServerConfig extends RealmObject {
@DebugLog public static void logError(String id, Exception exception) {
RealmHelperBolts.executeTransaction(
realm -> realm.createOrUpdateObjectFromJson(ServerConfig.class,
new JSONObject().put("id", id).put("connectionError", exception.getMessage())))
realm -> realm.createOrUpdateObjectFromJson(ServerConfig.class, new JSONObject()
.put("id", id)
.put("connectionError", exception.getMessage())
.put("session", JSONObject.NULL)))
.continueWith(new LogcatIfError());
}
......@@ -70,6 +71,14 @@ public class ServerConfig extends RealmObject {
this.connectionError = connectionError;
}
public String getSession() {
return session;
}
public void setSession(String session) {
this.session = session;
}
public String getToken() {
return token;
}
......@@ -86,14 +95,6 @@ public class ServerConfig extends RealmObject {
this.tokenVerified = tokenVerified;
}
public RealmList<MeteorLoginServiceConfiguration> getAuthProviders() {
return authProviders;
}
public void setAuthProviders(RealmList<MeteorLoginServiceConfiguration> authProviders) {
this.authProviders = authProviders;
}
public String getSelectedProviderName() {
return selectedProviderName;
}
......
......@@ -3,18 +3,22 @@ package chat.rocket.android.service;
import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import bolts.Continuation;
import bolts.Task;
import bolts.TaskCompletionSource;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.service.ddp_subscriber.LoginServiceConfigurationSubscriber;
import chat.rocket.android.ws.RocketChatWebSocketAPI;
import chat.rocket.android_ddp.DDPClient;
import chat.rocket.android_ddp.DDPClientCallback;
import hugo.weaving.DebugLog;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Iterator;
import jp.co.crowdworks.realm_java_helpers.RealmHelper;
import jp.co.crowdworks.realm_java_helpers_bolts.RealmHelperBolts;
import org.json.JSONObject;
import timber.log.Timber;
/**
......@@ -111,10 +115,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
}
}
private void prepareWebSocket() {
ServerConfig config = RealmHelper.executeTransactionForRead(
realm -> realm.where(ServerConfig.class).equalTo("id", serverConfigId).findFirst());
private void prepareWebSocket(ServerConfig config) {
if (webSocketAPI == null || !webSocketAPI.isConnected()) {
webSocketAPI = RocketChatWebSocketAPI.create(config.getHostname());
}
......@@ -124,26 +125,34 @@ public class RocketChatWebSocketThread extends HandlerThread {
if (socketExists) {
return Task.forResult(null);
}
socketExists = true;
prepareWebSocket();
return webSocketAPI.connect().onSuccess(task -> {
registerListenersActually();
DDPClient client = task.getResult().client;
// handling WebSocket#onClose() callback.
client.getOnCloseCallback().onSuccess(_task -> {
quit();
return null;
});
final ServerConfig config = RealmHelper.executeTransactionForRead(
realm -> realm.where(ServerConfig.class).equalTo("id", serverConfigId).findFirst());
// just for debugging.
client.getSubscriptionCallback().subscribe(event -> {
Timber.d("Callback [DEBUG] < " + event);
});
prepareWebSocket(config);
return webSocketAPI.connect(config.getSession()).onSuccessTask(task -> {
final String session = task.getResult().session;
RealmHelperBolts.executeTransaction(realm ->
realm.createOrUpdateObjectFromJson(ServerConfig.class, new JSONObject()
.put("id", serverConfigId)
.put("session", session))
).continueWith(new LogcatIfError());
return task;
}).onSuccess(new Continuation<DDPClientCallback.Connect, Object>() {
// TODO type detection doesn't work due to retrolambda's bug...
@Override public Object then(Task<DDPClientCallback.Connect> task)
throws Exception {
registerListenersActually();
// handling WebSocket#onClose() callback.
task.getResult().client.getOnCloseCallback().onSuccess(_task -> {
quit();
return null;
});
return null;
return null;
}
}).continueWith(task -> {
if (task.isFaulted()) {
ServerConfig.logError(serverConfigId, task.getError());
......
package chat.rocket.android.ws;
import android.support.annotation.Nullable;
import bolts.Task;
import chat.rocket.android.helper.OkHttpHelper;
import chat.rocket.android_ddp.DDPClient;
......@@ -31,8 +32,8 @@ public class RocketChatWebSocketAPI {
/**
* Connect to WebSocket server with DDP client.
*/
public Task<DDPClientCallback.Connect> connect() {
return ddpClient.connect("wss://" + hostname + "/websocket");
public Task<DDPClientCallback.Connect> connect(@Nullable String session) {
return ddpClient.connect("wss://" + hostname + "/websocket", session);
}
/**
......
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorPrimaryDark"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/white"
android:minWidth="288dp"
android:orientation="horizontal"
android:padding="@dimen/margin_24"
>
<LinearLayout
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_username"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="@+id/editor_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="username or email"
android:imeOptions="actionNext"
android:inputType="textWebEmailAddress"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<Space
android:layout_width="wrap_content"
android:layout_height="@dimen/margin_8"
/>
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_passwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="@+id/editor_passwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password"
android:imeOptions="actionNext"
android:inputType="textWebPassword"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Space
android:layout_width="@dimen/margin_8"
android:layout_height="wrap_content"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/btn_connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
app:elevation="2dp"
app:fabSize="mini"
app:srcCompat="@drawable/ic_arrow_forward_white_24dp"
/>
</LinearLayout>
</FrameLayout>
\ No newline at end of file
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