Commit 857c8cf2 authored by Tiago Cunha's avatar Tiago Cunha Committed by GitHub

Merge branch 'develop' into fix/sync-failure-error

parents da5320f3 d6380c71
...@@ -5,13 +5,15 @@ import android.content.SharedPreferences; ...@@ -5,13 +5,15 @@ import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import java.util.List;
import chat.rocket.android.LaunchUtil;
import chat.rocket.android.RocketChatCache; import chat.rocket.android.RocketChatCache;
import chat.rocket.android.model.ddp.RoomSubscription; import chat.rocket.android.model.ddp.RoomSubscription;
import chat.rocket.android.push.PushConstants; import chat.rocket.android.push.PushConstants;
import chat.rocket.android.push.PushNotificationHandler; import chat.rocket.android.push.PushNotificationHandler;
import chat.rocket.android.realm_helper.RealmListObserver;
import chat.rocket.android.realm_helper.RealmStore; import chat.rocket.android.realm_helper.RealmStore;
import chat.rocket.android.service.ConnectivityManager; import chat.rocket.android.service.ConnectivityManager;
import chat.rocket.android.service.ServerInfo;
import icepick.State; import icepick.State;
abstract class AbstractAuthedActivity extends AbstractFragmentActivity { abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
...@@ -66,25 +68,22 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity { ...@@ -66,25 +68,22 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
private void updateHostnameIfNeeded(SharedPreferences prefs) { private void updateHostnameIfNeeded(SharedPreferences prefs) {
String newHostname = prefs.getString(RocketChatCache.KEY_SELECTED_SERVER_HOSTNAME, null); String newHostname = prefs.getString(RocketChatCache.KEY_SELECTED_SERVER_HOSTNAME, null);
if (hostname == null) { if (hostname == null) {
if (newHostname != null && assertServerRealmStoreExists(newHostname, prefs)) { if (newHostname != null && assertServerRealmStoreExists(newHostname)) {
updateHostname(newHostname); updateHostname(newHostname);
} else {
recoverFromHostnameError(prefs);
} }
} else { } else {
if (!hostname.equals(newHostname) && assertServerRealmStoreExists(newHostname, prefs)) { if (!hostname.equals(newHostname) && assertServerRealmStoreExists(newHostname)) {
updateHostname(newHostname); updateHostname(newHostname);
} else {
recoverFromHostnameError(prefs);
} }
} }
} }
private boolean assertServerRealmStoreExists(String hostname, SharedPreferences prefs) { private boolean assertServerRealmStoreExists(String hostname) {
if (RealmStore.get(hostname) == null) { return RealmStore.get(hostname) != null;
prefs.edit()
.remove(RocketChatCache.KEY_SELECTED_SERVER_HOSTNAME)
.remove(RocketChatCache.KEY_SELECTED_ROOM_ID)
.apply();
return false;
}
return true;
} }
private void updateHostname(String hostname) { private void updateHostname(String hostname) {
...@@ -92,6 +91,22 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity { ...@@ -92,6 +91,22 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
onHostnameUpdated(); onHostnameUpdated();
} }
private void recoverFromHostnameError(SharedPreferences prefs) {
final List<ServerInfo> serverInfoList =
ConnectivityManager.getInstance(getApplicationContext()).getServerList();
if (serverInfoList == null || serverInfoList.size() == 0) {
LaunchUtil.showAddServerActivity(this);
return;
}
// just connect to the first available
final ServerInfo serverInfo = serverInfoList.get(0);
prefs.edit()
.putString(RocketChatCache.KEY_SELECTED_SERVER_HOSTNAME, serverInfo.hostname)
.remove(RocketChatCache.KEY_SELECTED_ROOM_ID)
.apply();
}
private void updateRoomIdIfNeeded(SharedPreferences prefs) { private void updateRoomIdIfNeeded(SharedPreferences prefs) {
String newRoomId = prefs.getString(RocketChatCache.KEY_SELECTED_ROOM_ID, null); String newRoomId = prefs.getString(RocketChatCache.KEY_SELECTED_ROOM_ID, null);
if (roomId == null) { if (roomId == null) {
...@@ -106,7 +121,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity { ...@@ -106,7 +121,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
} }
private boolean assertRoomSubscriptionExists(String roomId, SharedPreferences prefs) { private boolean assertRoomSubscriptionExists(String roomId, SharedPreferences prefs) {
if (!assertServerRealmStoreExists(hostname, prefs)) { if (!assertServerRealmStoreExists(hostname)) {
return false; return false;
} }
......
...@@ -29,8 +29,12 @@ import java.io.InputStream; ...@@ -29,8 +29,12 @@ import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Random; import java.util.Random;
import chat.rocket.android.activity.MainActivity; import chat.rocket.android.activity.MainActivity;
import chat.rocket.android.helper.ServerPolicyHelper;
import chat.rocket.android.service.ConnectivityManager;
import chat.rocket.android.service.ServerInfo;
public class PushNotificationHandler implements PushConstants { public class PushNotificationHandler implements PushConstants {
...@@ -98,7 +102,7 @@ public class PushNotificationHandler implements PushConstants { ...@@ -98,7 +102,7 @@ public class PushNotificationHandler implements PushConstants {
String hostname = getHostname(extras); String hostname = getHostname(extras);
String roomId = getRoomId(extras); String roomId = getRoomId(extras);
if (hostname == null || roomId == null) { if (hostname == null || roomId == null || !isValidHostname(context, hostname)) {
return; return;
} }
...@@ -633,7 +637,7 @@ public class PushNotificationHandler implements PushConstants { ...@@ -633,7 +637,7 @@ public class PushNotificationHandler implements PushConstants {
return null; return null;
} }
return jsonObject.getString("host"); return ServerPolicyHelper.enforceHostname(jsonObject.getString("host"));
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
...@@ -651,4 +655,17 @@ public class PushNotificationHandler implements PushConstants { ...@@ -651,4 +655,17 @@ public class PushNotificationHandler implements PushConstants {
return null; return null;
} }
} }
private boolean isValidHostname(Context context, String hostname) {
final List<ServerInfo> serverInfoList =
ConnectivityManager.getInstance(context.getApplicationContext()).getServerList();
for (ServerInfo serverInfo : serverInfoList) {
if (serverInfo.hostname.equals(hostname)) {
return true;
}
}
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