Commit 76e7a2db authored by Leonardo Aramaki's avatar Leonardo Aramaki

Fix caching of Site_Url and Site_Name by no relying on current selected server...

Fix caching of Site_Url and Site_Name by no relying on current selected server anymore; add bundled notifications
parent f646a02e
......@@ -53,12 +53,12 @@ public class RocketChatCache {
setString(KEY_SELECTED_SERVER_HOSTNAME, newHostname);
}
public void addHostSiteName(@NonNull String siteName) {
public void addHostSiteName(@NonNull String currentHostname, @NonNull String siteName) {
try {
String hostSiteNamesJson = getHostSiteNamesJson();
JSONObject jsonObject = (hostSiteNamesJson == null) ?
new JSONObject() : new JSONObject(hostSiteNamesJson);
jsonObject.put(getSelectedServerHostname(), siteName);
jsonObject.put(currentHostname, siteName);
setString(KEY_SELECTED_SITE_NAME, jsonObject.toString());
} catch (JSONException e) {
RCLog.e(e);
......@@ -88,7 +88,7 @@ public class RocketChatCache {
return getString(KEY_SELECTED_SITE_NAME, null);
}
public void addHostnameSiteUrl(@Nullable String hostnameAlias) {
public void addHostnameSiteUrl(@Nullable String hostnameAlias, @NonNull String currentHostname) {
String alias = null;
if (hostnameAlias != null) {
alias = hostnameAlias.toLowerCase();
......@@ -97,8 +97,7 @@ public class RocketChatCache {
String selectedHostnameAliasJson = getLoginHostnamesJson();
JSONObject jsonObject = selectedHostnameAliasJson == null ?
new JSONObject() : new JSONObject(selectedHostnameAliasJson);
String selectedServerHostname = getSelectedServerHostname();
jsonObject.put(alias, selectedServerHostname);
jsonObject.put(alias, currentHostname);
setString(KEY_SELECTED_SITE_URL, jsonObject.toString());
} catch (JSONException e) {
RCLog.e(e);
......
......@@ -415,23 +415,30 @@ public class MethodCallHelper {
.onSuccessTask(task -> Task.forResult(null));
}
public Task<Void> getPublicSettings() {
public Task<Void> getPublicSettings(String currentHostname) {
return call("public-settings/get", TIMEOUT_MS)
.onSuccessTask(CONVERT_TO_JSON_ARRAY)
.onSuccessTask(task -> {
final JSONArray settings = task.getResult();
String siteUrl = null;
String siteName = null;
for (int i = 0; i < settings.length(); i++) {
JSONObject jsonObject = settings.getJSONObject(i);
RealmPublicSetting.customizeJson(jsonObject);
if (isPublicSetting(jsonObject, PublicSettingsConstants.General.SITE_URL)) {
String siteUrl = jsonObject.getString(RealmPublicSetting.VALUE);
HttpUrl httpUrl = HttpUrl.parse(siteUrl);
if (httpUrl != null) {
new RocketChatCache(context).addHostnameSiteUrl(httpUrl.host());
}
siteUrl = jsonObject.getString(RealmPublicSetting.VALUE);
} else if (isPublicSetting(jsonObject, PublicSettingsConstants.General.SITE_NAME)) {
String siteName = jsonObject.getString(RealmPublicSetting.VALUE);
new RocketChatCache(context).addHostSiteName(siteName);
siteName = jsonObject.getString(RealmPublicSetting.VALUE);
}
}
if (siteName != null && siteUrl != null) {
HttpUrl httpSiteUrl = HttpUrl.parse(siteUrl);
if (httpSiteUrl != null) {
String host = httpSiteUrl.host();
RocketChatCache rocketChatCache = new RocketChatCache(context);
rocketChatCache.addHostnameSiteUrl(host, currentHostname);
rocketChatCache.addHostSiteName(currentHostname, siteName);
}
}
......
......@@ -21,11 +21,16 @@ import chat.rocket.android.RocketChatCache
import chat.rocket.android.activity.MainActivity
import org.json.JSONObject
import java.util.*
import kotlin.collections.HashMap
object PushManager {
// A map associating a notification id to a list of corresponding messages.
// Map associating a notification id to a list of corresponding messages ie. an id corresponds
// to a user and the corresponding list is all the messages sent by him.
val messageStack = SparseArray<ArrayList<String>>()
// Notifications received from the same server are grouped in a single bundled notification.
// This map associates a host to a group id.
val groupMap = HashMap<String, Int>()
val randomizer = Random()
fun handle(context: Context, data: Bundle) {
......@@ -60,16 +65,48 @@ object PushManager {
val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Create notification group.
addGroupToBundle(pushMessage.host)
val id = pushMessage.notificationId.toInt()
val contentIntent = getContentIntent(context, id, data, pushMessage)
val deleteIntent = getDismissIntent(context, id)
val notGroupBuilder = NotificationCompat.Builder(context)
.setAutoCancel(true)
.setShowWhen(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(pushMessage.createdAt)
.setContentTitle(pushMessage.title.fromHtml())
.setContentText(pushMessage.message.fromHtml())
.setGroup(pushMessage.host)
.setGroupSummary(true)
.setSmallIcon(smallIcon)
.setStyle(NotificationCompat.BigTextStyle().bigText(pushMessage.message.fromHtml()))
.setContentIntent(contentIntent)
.setDeleteIntent(deleteIntent)
val subText = RocketChatCache(context).getHostSiteName(pushMessage.host)
if (subText.isNotEmpty()) {
notGroupBuilder.setSubText(subText)
}
val notification: Notification
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notification = createNotificationForOreoAndAbove(appContext, pushMessage, smallIcon, data)
notificationManager.notify(notificationId.toInt(), notification)
} else {
notification = createCompatNotification(appContext, pushMessage, smallIcon, data)
NotificationManagerCompat.from(appContext).notify(groupMap.get(pushMessage.host)!!, notGroupBuilder.build())
NotificationManagerCompat.from(appContext).notify(notificationId.toInt(), notification)
}
}
private fun addGroupToBundle(host: String) {
val size = groupMap.size
if (groupMap.get(host) == null) {
groupMap.put(host, size + 1)
}
}
fun clearMessageStack(notificationId: Int) {
messageStack.delete(notificationId)
}
......@@ -88,6 +125,7 @@ object PushManager {
.setContentTitle(title.fromHtml())
.setContentText(message.fromHtml())
.setNumber(count.toInt())
.setGroup(host)
.setSmallIcon(smallIcon)
.setDeleteIntent(deleteIntent)
.setContentIntent(contentIntent)
......
......@@ -343,7 +343,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
}
private Task<Void> fetchPublicSettings() {
return new MethodCallHelper(appContext, realmHelper, ddpClientRef).getPublicSettings();
return new MethodCallHelper(appContext, realmHelper, ddpClientRef).getPublicSettings(hostname);
}
private Task<Void> fetchPermissions() {
......
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