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 { ...@@ -53,12 +53,12 @@ public class RocketChatCache {
setString(KEY_SELECTED_SERVER_HOSTNAME, newHostname); setString(KEY_SELECTED_SERVER_HOSTNAME, newHostname);
} }
public void addHostSiteName(@NonNull String siteName) { public void addHostSiteName(@NonNull String currentHostname, @NonNull String siteName) {
try { try {
String hostSiteNamesJson = getHostSiteNamesJson(); String hostSiteNamesJson = getHostSiteNamesJson();
JSONObject jsonObject = (hostSiteNamesJson == null) ? JSONObject jsonObject = (hostSiteNamesJson == null) ?
new JSONObject() : new JSONObject(hostSiteNamesJson); new JSONObject() : new JSONObject(hostSiteNamesJson);
jsonObject.put(getSelectedServerHostname(), siteName); jsonObject.put(currentHostname, siteName);
setString(KEY_SELECTED_SITE_NAME, jsonObject.toString()); setString(KEY_SELECTED_SITE_NAME, jsonObject.toString());
} catch (JSONException e) { } catch (JSONException e) {
RCLog.e(e); RCLog.e(e);
...@@ -88,7 +88,7 @@ public class RocketChatCache { ...@@ -88,7 +88,7 @@ public class RocketChatCache {
return getString(KEY_SELECTED_SITE_NAME, null); 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; String alias = null;
if (hostnameAlias != null) { if (hostnameAlias != null) {
alias = hostnameAlias.toLowerCase(); alias = hostnameAlias.toLowerCase();
...@@ -97,8 +97,7 @@ public class RocketChatCache { ...@@ -97,8 +97,7 @@ public class RocketChatCache {
String selectedHostnameAliasJson = getLoginHostnamesJson(); String selectedHostnameAliasJson = getLoginHostnamesJson();
JSONObject jsonObject = selectedHostnameAliasJson == null ? JSONObject jsonObject = selectedHostnameAliasJson == null ?
new JSONObject() : new JSONObject(selectedHostnameAliasJson); new JSONObject() : new JSONObject(selectedHostnameAliasJson);
String selectedServerHostname = getSelectedServerHostname(); jsonObject.put(alias, currentHostname);
jsonObject.put(alias, selectedServerHostname);
setString(KEY_SELECTED_SITE_URL, jsonObject.toString()); setString(KEY_SELECTED_SITE_URL, jsonObject.toString());
} catch (JSONException e) { } catch (JSONException e) {
RCLog.e(e); RCLog.e(e);
......
...@@ -415,23 +415,30 @@ public class MethodCallHelper { ...@@ -415,23 +415,30 @@ public class MethodCallHelper {
.onSuccessTask(task -> Task.forResult(null)); .onSuccessTask(task -> Task.forResult(null));
} }
public Task<Void> getPublicSettings() { public Task<Void> getPublicSettings(String currentHostname) {
return call("public-settings/get", TIMEOUT_MS) return call("public-settings/get", TIMEOUT_MS)
.onSuccessTask(CONVERT_TO_JSON_ARRAY) .onSuccessTask(CONVERT_TO_JSON_ARRAY)
.onSuccessTask(task -> { .onSuccessTask(task -> {
final JSONArray settings = task.getResult(); final JSONArray settings = task.getResult();
String siteUrl = null;
String siteName = null;
for (int i = 0; i < settings.length(); i++) { for (int i = 0; i < settings.length(); i++) {
JSONObject jsonObject = settings.getJSONObject(i); JSONObject jsonObject = settings.getJSONObject(i);
RealmPublicSetting.customizeJson(jsonObject); RealmPublicSetting.customizeJson(jsonObject);
if (isPublicSetting(jsonObject, PublicSettingsConstants.General.SITE_URL)) { if (isPublicSetting(jsonObject, PublicSettingsConstants.General.SITE_URL)) {
String siteUrl = jsonObject.getString(RealmPublicSetting.VALUE); siteUrl = jsonObject.getString(RealmPublicSetting.VALUE);
HttpUrl httpUrl = HttpUrl.parse(siteUrl);
if (httpUrl != null) {
new RocketChatCache(context).addHostnameSiteUrl(httpUrl.host());
}
} else if (isPublicSetting(jsonObject, PublicSettingsConstants.General.SITE_NAME)) { } else if (isPublicSetting(jsonObject, PublicSettingsConstants.General.SITE_NAME)) {
String siteName = jsonObject.getString(RealmPublicSetting.VALUE); siteName = jsonObject.getString(RealmPublicSetting.VALUE);
new RocketChatCache(context).addHostSiteName(siteName); }
}
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 ...@@ -21,11 +21,16 @@ import chat.rocket.android.RocketChatCache
import chat.rocket.android.activity.MainActivity import chat.rocket.android.activity.MainActivity
import org.json.JSONObject import org.json.JSONObject
import java.util.* import java.util.*
import kotlin.collections.HashMap
object PushManager { 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>>() 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() val randomizer = Random()
fun handle(context: Context, data: Bundle) { fun handle(context: Context, data: Bundle) {
...@@ -60,16 +65,48 @@ object PushManager { ...@@ -60,16 +65,48 @@ object PushManager {
val notificationManager: NotificationManager = val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as 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 val notification: Notification
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notification = createNotificationForOreoAndAbove(appContext, pushMessage, smallIcon, data) notification = createNotificationForOreoAndAbove(appContext, pushMessage, smallIcon, data)
notificationManager.notify(notificationId.toInt(), notification) notificationManager.notify(notificationId.toInt(), notification)
} else { } else {
notification = createCompatNotification(appContext, pushMessage, smallIcon, data) notification = createCompatNotification(appContext, pushMessage, smallIcon, data)
NotificationManagerCompat.from(appContext).notify(groupMap.get(pushMessage.host)!!, notGroupBuilder.build())
NotificationManagerCompat.from(appContext).notify(notificationId.toInt(), notification) 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) { fun clearMessageStack(notificationId: Int) {
messageStack.delete(notificationId) messageStack.delete(notificationId)
} }
...@@ -88,6 +125,7 @@ object PushManager { ...@@ -88,6 +125,7 @@ object PushManager {
.setContentTitle(title.fromHtml()) .setContentTitle(title.fromHtml())
.setContentText(message.fromHtml()) .setContentText(message.fromHtml())
.setNumber(count.toInt()) .setNumber(count.toInt())
.setGroup(host)
.setSmallIcon(smallIcon) .setSmallIcon(smallIcon)
.setDeleteIntent(deleteIntent) .setDeleteIntent(deleteIntent)
.setContentIntent(contentIntent) .setContentIntent(contentIntent)
......
...@@ -343,7 +343,7 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -343,7 +343,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
} }
private Task<Void> fetchPublicSettings() { private Task<Void> fetchPublicSettings() {
return new MethodCallHelper(appContext, realmHelper, ddpClientRef).getPublicSettings(); return new MethodCallHelper(appContext, realmHelper, ddpClientRef).getPublicSettings(hostname);
} }
private Task<Void> fetchPermissions() { 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