Commit 659952bf authored by Tiago Cunha's avatar Tiago Cunha Committed by GitHub

Merge pull request #212 from RocketChat/fix/firebase-crashes-20170220

Fix/firebase crashes 20170220
parents 3b05bafe 32982d9f
......@@ -54,38 +54,47 @@ public class DDPClientImpl {
flowable = websocket.connect(url).autoConnect();
CompositeDisposable subscriptions = new CompositeDisposable();
subscriptions.add(flowable.filter(callback -> callback instanceof RxWebSocketCallback.Open)
.subscribe(callback -> {
sendMessage("connect",
json -> (TextUtils.isEmpty(session) ? json : json.put("session", session)).put(
"version", "pre2").put("support", new JSONArray().put("pre2").put("pre1")),
task);
}, err -> {
}));
subscriptions.add(
flowable.filter(callback -> callback instanceof RxWebSocketCallback.Open)
.subscribe(
callback -> {
sendMessage("connect",
json -> (TextUtils.isEmpty(session) ? json : json.put("session", session))
.put(
"version", "pre2")
.put("support", new JSONArray().put("pre2").put("pre1")),
task);
},
err -> {
}
)
);
subscriptions.add(
flowable.filter(callback -> callback instanceof RxWebSocketCallback.Message)
flowable.filter(
callback -> callback instanceof RxWebSocketCallback.Message)
.map(callback -> ((RxWebSocketCallback.Message) callback).responseBodyString)
.map(DDPClientImpl::toJson)
.timeout(7, TimeUnit.SECONDS)
.subscribe(response -> {
String msg = extractMsg(response);
if ("connected".equals(msg) && !response.isNull("session")) {
task.trySetResult(
new DDPClientCallback.Connect(client, response.optString("session")));
subscriptions.dispose();
} else if ("error".equals(msg) && "Already connected".equals(
response.optString("reason"))) {
task.trySetResult(new DDPClientCallback.Connect(client, null));
subscriptions.dispose();
} else if ("failed".equals(msg)) {
task.trySetError(
new DDPClientCallback.Connect.Failed(client, response.optString("version")));
subscriptions.dispose();
}
}, err -> {
task.trySetError(new DDPClientCallback.Connect.Timeout(client));
}));
String msg = extractMsg(response);
if ("connected".equals(msg) && !response.isNull("session")) {
task.trySetResult(
new DDPClientCallback.Connect(client, response.optString("session")));
subscriptions.dispose();
} else if ("error".equals(msg) && "Already connected".equals(
response.optString("reason"))) {
task.trySetResult(new DDPClientCallback.Connect(client, null));
subscriptions.dispose();
} else if ("failed".equals(msg)) {
task.trySetError(
new DDPClientCallback.Connect.Failed(client, response.optString("version")));
subscriptions.dispose();
}
},
err -> task.trySetError(new DDPClientCallback.Connect.Timeout(client))
)
);
addErrorCallback(subscriptions, task);
......@@ -110,23 +119,25 @@ public class DDPClientImpl {
.map(callback -> ((RxWebSocketCallback.Message) callback).responseBodyString)
.map(DDPClientImpl::toJson)
.timeout(4, TimeUnit.SECONDS)
.subscribe(response -> {
String msg = extractMsg(response);
if ("pong".equals(msg)) {
if (response.isNull("id")) {
task.setResult(new DDPClientCallback.Ping(client, null));
subscriptions.dispose();
} else {
String _id = response.optString("id");
if (id.equals(_id)) {
task.setResult(new DDPClientCallback.Ping(client, id));
subscriptions.dispose();
.subscribe(
response -> {
String msg = extractMsg(response);
if ("pong".equals(msg)) {
if (response.isNull("id")) {
task.setResult(new DDPClientCallback.Ping(client, null));
subscriptions.dispose();
} else {
String _id = response.optString("id");
if (id.equals(_id)) {
task.setResult(new DDPClientCallback.Ping(client, id));
subscriptions.dispose();
}
}
}
}
}
}, err -> {
task.setError(new DDPClientCallback.Ping.Timeout(client));
}));
},
err -> task.setError(new DDPClientCallback.Ping.Timeout(client))
)
);
addErrorCallback(subscriptions, task);
} else {
......@@ -146,29 +157,33 @@ public class DDPClientImpl {
flowable.filter(callback -> callback instanceof RxWebSocketCallback.Message)
.map(callback -> ((RxWebSocketCallback.Message) callback).responseBodyString)
.map(DDPClientImpl::toJson)
.subscribe(response -> {
String msg = extractMsg(response);
if ("ready".equals(msg) && !response.isNull("subs")) {
JSONArray ids = response.optJSONArray("subs");
for (int i = 0; i < ids.length(); i++) {
String _id = ids.optString(i);
if (id.equals(_id)) {
task.setResult(new DDPSubscription.Ready(client, id));
subscriptions.dispose();
break;
.subscribe(
response -> {
String msg = extractMsg(response);
if ("ready".equals(msg) && !response.isNull("subs")) {
JSONArray ids = response.optJSONArray("subs");
for (int i = 0; i < ids.length(); i++) {
String _id = ids.optString(i);
if (id.equals(_id)) {
task.setResult(new DDPSubscription.Ready(client, id));
subscriptions.dispose();
break;
}
}
} else if ("nosub".equals(msg) && !response.isNull("id") && !response.isNull(
"error")) {
String _id = response.optString("id");
if (id.equals(_id)) {
task.setError(new DDPSubscription.NoSub.Error(client, id,
response.optJSONObject("error")));
subscriptions.dispose();
}
}
},
err -> {
}
} else if ("nosub".equals(msg) && !response.isNull("id") && !response.isNull(
"error")) {
String _id = response.optString("id");
if (id.equals(_id)) {
task.setError(new DDPSubscription.NoSub.Error(client, id,
response.optJSONObject("error")));
subscriptions.dispose();
}
}
}, err -> {
}));
)
);
addErrorCallback(subscriptions, task);
} else {
......@@ -188,17 +203,21 @@ public class DDPClientImpl {
flowable.filter(callback -> callback instanceof RxWebSocketCallback.Message)
.map(callback -> ((RxWebSocketCallback.Message) callback).responseBodyString)
.map(DDPClientImpl::toJson)
.subscribe(response -> {
String msg = extractMsg(response);
if ("nosub".equals(msg) && response.isNull("error") && !response.isNull("id")) {
String _id = response.optString("id");
if (id.equals(_id)) {
task.setResult(new DDPSubscription.NoSub(client, id));
subscriptions.dispose();
.subscribe(
response -> {
String msg = extractMsg(response);
if ("nosub".equals(msg) && response.isNull("error") && !response.isNull("id")) {
String _id = response.optString("id");
if (id.equals(_id)) {
task.setResult(new DDPSubscription.NoSub(client, id));
subscriptions.dispose();
}
}
},
err -> {
}
}
}, err -> {
}));
)
);
addErrorCallback(subscriptions, task);
} else {
......@@ -220,26 +239,30 @@ public class DDPClientImpl {
.map(callback -> ((RxWebSocketCallback.Message) callback).responseBodyString)
.map(DDPClientImpl::toJson)
.timeout(timeoutMs, TimeUnit.MILLISECONDS)
.subscribe(response -> {
String msg = extractMsg(response);
if ("result".equals(msg)) {
String _id = response.optString("id");
if (id.equals(_id)) {
if (!response.isNull("error")) {
task.setError(new DDPClientCallback.RPC.Error(client, id,
response.optJSONObject("error")));
} else {
String result = response.optString("result");
task.setResult(new DDPClientCallback.RPC(client, id, result));
.subscribe(
response -> {
String msg = extractMsg(response);
if ("result".equals(msg)) {
String _id = response.optString("id");
if (id.equals(_id)) {
if (!response.isNull("error")) {
task.setError(new DDPClientCallback.RPC.Error(client, id,
response.optJSONObject("error")));
} else {
String result = response.optString("result");
task.setResult(new DDPClientCallback.RPC(client, id, result));
}
subscriptions.dispose();
}
}
},
err -> {
if (err instanceof TimeoutException) {
task.setError(new DDPClientCallback.RPC.Timeout(client));
}
subscriptions.dispose();
}
}
}, err -> {
if (err instanceof TimeoutException) {
task.setError(new DDPClientCallback.RPC.Timeout(client));
}
}));
)
);
addErrorCallback(subscriptions, task);
} else {
......@@ -258,17 +281,21 @@ public class DDPClientImpl {
flowable.filter(callback -> callback instanceof RxWebSocketCallback.Message)
.map(callback -> ((RxWebSocketCallback.Message) callback).responseBodyString)
.map(DDPClientImpl::toJson)
.subscribe(response -> {
String msg = extractMsg(response);
if ("ping".equals(msg)) {
if (response.isNull("id")) {
sendMessage("pong", null);
} else {
sendMessage("pong", json -> json.put("id", response.getString("id")));
.subscribe(
response -> {
String msg = extractMsg(response);
if ("ping".equals(msg)) {
if (response.isNull("id")) {
sendMessage("pong", null);
} else {
sendMessage("pong", json -> json.put("id", response.getString("id")));
}
}
},
err -> {
}
}
}, err -> {
}));
)
);
}
public Flowable<DDPSubscription.Event> getDDPSubscription() {
......@@ -325,13 +352,16 @@ public class DDPClientImpl {
flowable.filter(callback -> callback instanceof RxWebSocketCallback.Close)
.cast(RxWebSocketCallback.Close.class)
.subscribe(task::setResult, err -> {
if (err instanceof Exception) {
task.setError((Exception) err);
} else {
task.setError(new Exception(err));
}
});
.subscribe(
task::setResult,
err -> {
if (err instanceof Exception) {
task.setError((Exception) err);
} else {
task.setError(new Exception(err));
}
}
);
return task.getTask().onSuccessTask(_task -> {
unsubscribeBaseListeners();
......@@ -358,11 +388,16 @@ public class DDPClientImpl {
}
private void addErrorCallback(CompositeDisposable subscriptions, TaskCompletionSource<?> task) {
subscriptions.add(flowable.subscribe(base -> {
}, err -> {
task.trySetError(new Exception(err));
subscriptions.dispose();
}));
subscriptions.add(
flowable.subscribe(
base -> {
},
err -> {
task.trySetError(new Exception(err));
subscriptions.dispose();
}
)
);
}
public void close(int code, String reason) {
......
......@@ -33,8 +33,8 @@ android {
applicationId "chat.rocket.android"
minSdkVersion 16
targetSdkVersion 25
versionCode 10
versionName "1.0.1"
versionCode 11
versionName "1.0.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
......
......@@ -75,7 +75,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
}
} else {
if (hostname.equals(newHostname)) {
// we are good
updateHostname(newHostname);
return;
}
......
......@@ -99,23 +99,27 @@ public abstract class AbstractDDPDocEventSubscriber implements Registrable {
.filter(event -> event instanceof DDPSubscription.DocEvent)
.cast(DDPSubscription.DocEvent.class)
.filter(event -> isTarget(event.collection))
.subscribe(docEvent -> {
try {
if (docEvent instanceof DDPSubscription.Added.Before) {
onDocumentAdded((DDPSubscription.Added) docEvent); //ignore Before
} else if (docEvent instanceof DDPSubscription.Added) {
onDocumentAdded((DDPSubscription.Added) docEvent);
} else if (docEvent instanceof DDPSubscription.Removed) {
onDocumentRemoved((DDPSubscription.Removed) docEvent);
} else if (docEvent instanceof DDPSubscription.Changed) {
onDocumentChanged((DDPSubscription.Changed) docEvent);
} else if (docEvent instanceof DDPSubscription.MovedBefore) {
//ignore movedBefore
.subscribe(
docEvent -> {
try {
if (docEvent instanceof DDPSubscription.Added.Before) {
onDocumentAdded((DDPSubscription.Added) docEvent); //ignore Before
} else if (docEvent instanceof DDPSubscription.Added) {
onDocumentAdded((DDPSubscription.Added) docEvent);
} else if (docEvent instanceof DDPSubscription.Removed) {
onDocumentRemoved((DDPSubscription.Removed) docEvent);
} else if (docEvent instanceof DDPSubscription.Changed) {
onDocumentChanged((DDPSubscription.Changed) docEvent);
} else if (docEvent instanceof DDPSubscription.MovedBefore) {
//ignore movedBefore
}
} catch (Exception exception) {
RCLog.w(exception, "failed to handle subscription callback");
}
},
throwable -> {
}
} catch (Exception exception) {
RCLog.w(exception, "failed to handle subscription callback");
}
});
);
}
protected void onDocumentAdded(DDPSubscription.Added docEvent) {
......
......@@ -109,9 +109,17 @@ public class MethodCallObserver extends AbstractModelObserver<MethodCall> {
if (task.isFaulted()) {
return realmHelper.executeTransaction(realm -> {
Exception exception = task.getError();
final String errMessage = (exception instanceof DDPClientCallback.RPC.Error)
? ((DDPClientCallback.RPC.Error) exception).error.toString()
: exception.getMessage();
final String errMessage;
if (exception instanceof DDPClientCallback.RPC.Error) {
errMessage = ((DDPClientCallback.RPC.Error) exception).error.toString();
} else if (exception instanceof DDPClientCallback.RPC.Timeout) {
// temp "fix"- we need to rewrite the connection layer a bit
errMessage = "{\"message\": \"Connection Timeout\"}";
} else {
errMessage = exception.getMessage();
}
realm.createOrUpdateObjectFromJson(MethodCall.class, new JSONObject()
.put(MethodCall.ID, methodCallId)
.put(MethodCall.SYNC_STATE, SyncState.FAILED)
......
......@@ -11,7 +11,7 @@ public class RealmPreferences extends RealmObject {
@PrimaryKey private String id;
private String newRoomNotification;
private boolean newMessageNotification;
private String newMessageNotification;
private boolean useEmojis;
private boolean convertAsciiEmoji;
private boolean saveMobileBandwidth;
......@@ -52,7 +52,7 @@ public class RealmPreferences extends RealmObject {
return newRoomNotification;
}
public boolean isNewMessageNotification() {
public String getNewMessageNotification() {
return newMessageNotification;
}
......@@ -123,7 +123,8 @@ public class RealmPreferences extends RealmObject {
: that.newRoomNotification == null) {
return false;
}
if (newMessageNotification != that.newMessageNotification) {
if (newMessageNotification != null ? newMessageNotification.equals(that.newMessageNotification)
: that.newMessageNotification == null) {
return false;
}
if (useEmojis != that.useEmojis) {
......@@ -174,7 +175,7 @@ public class RealmPreferences extends RealmObject {
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (newRoomNotification != null ? newRoomNotification.hashCode() : 0);
result = 31 * result + (newMessageNotification ? 1 : 0);
result = 31 * result + (newMessageNotification != null ? newMessageNotification.hashCode() : 0);
result = 31 * result + (useEmojis ? 1 : 0);
result = 31 * result + (convertAsciiEmoji ? 1 : 0);
result = 31 * result + (saveMobileBandwidth ? 1 : 0);
......
......@@ -87,7 +87,7 @@ public class RealmUser extends RealmObject {
public User asUser() {
// convert email list
final int total = emails.size();
final int total = emails != null ? emails.size() : 0;
final List<Email> coreEmails = new ArrayList<>(total);
for (int i = 0; i < total; i++) {
......
......@@ -52,6 +52,9 @@ public class ImageKeyboardEditText extends EditText {
@Override
public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
final InputConnection inputConnection = super.onCreateInputConnection(editorInfo);
if (inputConnection == null) {
return null;
}
EditorInfoCompat.setContentMimeTypes(editorInfo, mimeTypes);
......
......@@ -12,7 +12,8 @@ public abstract class Preferences {
@Nullable
public abstract String getNewRoomNotification();
public abstract boolean isNewMessageNotification();
@Nullable
public abstract String getNewMessageNotification();
public abstract boolean isUseEmojis();
......@@ -51,7 +52,7 @@ public abstract class Preferences {
public abstract Builder setNewRoomNotification(String newRoomNotification);
public abstract Builder setNewMessageNotification(boolean newMessageNotification);
public abstract Builder setNewMessageNotification(String newMessageNotification);
public abstract Builder setUseEmojis(boolean useEmojis);
......
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