Commit f646a02e authored by Leonardo Aramaki's avatar Leonardo Aramaki

Caching Site_Name public setting and setting it as subtext on

notification.
parent 8e9506c0
......@@ -22,6 +22,7 @@ import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;
import okhttp3.HttpUrl;
/**
* sharedpreference-based cache.
......@@ -29,6 +30,7 @@ import io.reactivex.annotations.Nullable;
public class RocketChatCache {
private static final String KEY_SELECTED_SERVER_HOSTNAME = "KEY_SELECTED_SERVER_HOSTNAME";
private static final String KEY_SELECTED_SITE_URL = "KEY_SELECTED_SITE_URL";
private static final String KEY_SELECTED_SITE_NAME = "KEY_SELECTED_SITE_NAME";
private static final String KEY_SELECTED_ROOM_ID = "KEY_SELECTED_ROOM_ID";
private static final String KEY_PUSH_ID = "KEY_PUSH_ID";
private static final String KEY_HOSTNAME_LIST = "KEY_HOSTNAME_LIST";
......@@ -51,7 +53,42 @@ public class RocketChatCache {
setString(KEY_SELECTED_SERVER_HOSTNAME, newHostname);
}
public void setSelectedServerHostnameAlias(@Nullable String hostnameAlias) {
public void addHostSiteName(@NonNull String siteName) {
try {
String hostSiteNamesJson = getHostSiteNamesJson();
JSONObject jsonObject = (hostSiteNamesJson == null) ?
new JSONObject() : new JSONObject(hostSiteNamesJson);
jsonObject.put(getSelectedServerHostname(), siteName);
setString(KEY_SELECTED_SITE_NAME, jsonObject.toString());
} catch (JSONException e) {
RCLog.e(e);
}
}
public @NonNull String getHostSiteName(@NonNull String host) {
if (host.startsWith("http")) {
HttpUrl url = HttpUrl.parse(host);
if (url != null) {
host = url.host();
}
}
try {
String hostSiteNamesJson = getHostSiteNamesJson();
JSONObject jsonObject = (hostSiteNamesJson == null) ?
new JSONObject() : new JSONObject(hostSiteNamesJson);
host = getSiteUrlFor(host);
return jsonObject.optString(host);
} catch (JSONException e) {
RCLog.e(e);
}
return "";
}
private @Nullable String getHostSiteNamesJson() {
return getString(KEY_SELECTED_SITE_NAME, null);
}
public void addHostnameSiteUrl(@Nullable String hostnameAlias) {
String alias = null;
if (hostnameAlias != null) {
alias = hostnameAlias.toLowerCase();
......@@ -68,7 +105,7 @@ public class RocketChatCache {
}
}
public @NonNull String getLoginHostnameFrom(String hostname) {
public @NonNull String getSiteUrlFor(String hostname) {
try {
String selectedServerHostname = getSelectedServerHostname();
return new JSONObject(getLoginHostnamesJson())
......
......@@ -62,7 +62,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
HttpUrl url = HttpUrl.parse(hostname);
if (url != null) {
String hostnameFromPush = url.host();
String loginHostname = rocketChatCache.getLoginHostnameFrom(hostnameFromPush);
String loginHostname = rocketChatCache.getSiteUrlFor(hostnameFromPush);
rocketChatCache.setSelectedServerHostname(loginHostname);
if (intent.hasExtra(PushConstants.ROOM_ID)) {
......
......@@ -423,12 +423,15 @@ public class MethodCallHelper {
for (int i = 0; i < settings.length(); i++) {
JSONObject jsonObject = settings.getJSONObject(i);
RealmPublicSetting.customizeJson(jsonObject);
if (jsonObject.getString(RealmPublicSetting.ID).equalsIgnoreCase(PublicSettingsConstants.General.SITE_URL)) {
if (isPublicSetting(jsonObject, PublicSettingsConstants.General.SITE_URL)) {
String siteUrl = jsonObject.getString(RealmPublicSetting.VALUE);
HttpUrl httpUrl = HttpUrl.parse(siteUrl);
if (httpUrl != null) {
new RocketChatCache(context).setSelectedServerHostnameAlias(httpUrl.host());
new RocketChatCache(context).addHostnameSiteUrl(httpUrl.host());
}
} else if (isPublicSetting(jsonObject, PublicSettingsConstants.General.SITE_NAME)) {
String siteName = jsonObject.getString(RealmPublicSetting.VALUE);
new RocketChatCache(context).addHostSiteName(siteName);
}
}
......@@ -440,6 +443,10 @@ public class MethodCallHelper {
});
}
private boolean isPublicSetting(JSONObject jsonObject, String id) {
return jsonObject.optString(RealmPublicSetting.ID).equalsIgnoreCase(id);
}
public Task<Void> getPermissions() {
return call("permissions/get", TIMEOUT_MS)
.onSuccessTask(CONVERT_TO_JSON_ARRAY)
......
......@@ -17,6 +17,7 @@ import android.text.Spanned
import android.util.Log
import android.util.SparseArray
import chat.rocket.android.BuildConfig
import chat.rocket.android.RocketChatCache
import chat.rocket.android.activity.MainActivity
import org.json.JSONObject
import java.util.*
......@@ -91,6 +92,11 @@ object PushManager {
.setDeleteIntent(deleteIntent)
.setContentIntent(contentIntent)
val subText = RocketChatCache(context).getHostSiteName(pushMessage.host)
if (subText.isNotEmpty()) {
notificationBuilder.setSubText(subText)
}
if ("inbox" == style) {
val messages = messageStack.get(notificationId.toInt())
val messageCount = messages.size
......
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