Commit 5b476274 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Add support for new PushManager class from AbstractAuthedActivities

parent 1c73d226
...@@ -3,23 +3,25 @@ package chat.rocket.android.activity; ...@@ -3,23 +3,25 @@ package chat.rocket.android.activity;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import com.hadisatrio.optional.Optional; import com.hadisatrio.optional.Optional;
import chat.rocket.android.push.PushManager;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import java.util.List; import java.util.List;
import chat.rocket.android.LaunchUtil; import chat.rocket.android.LaunchUtil;
import chat.rocket.android.RocketChatCache; import chat.rocket.android.RocketChatCache;
import chat.rocket.android.helper.Logger; import chat.rocket.android.helper.Logger;
import chat.rocket.android.push.PushConstants; import chat.rocket.android.push.PushConstants;
import chat.rocket.android.push.PushNotificationHandler; import chat.rocket.android.push.PushManager;
import chat.rocket.android.service.ConnectivityManager; import chat.rocket.android.service.ConnectivityManager;
import chat.rocket.core.models.ServerInfo; import chat.rocket.core.models.ServerInfo;
import chat.rocket.persistence.realm.RealmStore; import chat.rocket.persistence.realm.RealmStore;
import chat.rocket.persistence.realm.models.ddp.RealmRoom; import chat.rocket.persistence.realm.models.ddp.RealmRoom;
import icepick.State; import icepick.State;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.HttpUrl;
abstract class AbstractAuthedActivity extends AbstractFragmentActivity { abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
@State protected String hostname; @State protected String hostname;
...@@ -56,11 +58,15 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity { ...@@ -56,11 +58,15 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
} }
if (intent.hasExtra(PushConstants.HOSTNAME)) { if (intent.hasExtra(PushConstants.HOSTNAME)) {
rocketChatCache.setSelectedServerHostname(intent.getStringExtra(PushConstants.HOSTNAME)); String hostname = intent.getStringExtra(PushConstants.HOSTNAME);
HttpUrl url = HttpUrl.parse(hostname);
if (url != null) {
rocketChatCache.setSelectedServerHostname(url.host());
if (intent.hasExtra(PushConstants.ROOM_ID)) { if (intent.hasExtra(PushConstants.ROOM_ID)) {
rocketChatCache.setSelectedRoomId(intent.getStringExtra(PushConstants.ROOM_ID)); rocketChatCache.setSelectedRoomId(intent.getStringExtra(PushConstants.ROOM_ID));
} }
}
} else { } else {
updateHostnameIfNeeded(rocketChatCache.getSelectedServerHostname()); updateHostnameIfNeeded(rocketChatCache.getSelectedServerHostname());
} }
...@@ -68,9 +74,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity { ...@@ -68,9 +74,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
if (intent.hasExtra(PushConstants.NOT_ID)) { if (intent.hasExtra(PushConstants.NOT_ID)) {
isNotification = true; isNotification = true;
int notificationId = intent.getIntExtra(PushConstants.NOT_ID, 0); int notificationId = intent.getIntExtra(PushConstants.NOT_ID, 0);
PushNotificationHandler PushManager.INSTANCE.clearMessageStack(notificationId);
.cleanUpNotificationStack(notificationId);
PushManager.INSTANCE.clearStack(notificationId);
} }
} }
......
...@@ -14,12 +14,18 @@ import android.support.v4.app.NotificationCompat ...@@ -14,12 +14,18 @@ import android.support.v4.app.NotificationCompat
import android.support.v4.app.NotificationManagerCompat import android.support.v4.app.NotificationManagerCompat
import android.text.Html import android.text.Html
import android.text.Spanned import android.text.Spanned
import android.util.Log
import android.util.SparseArray import android.util.SparseArray
import chat.rocket.android.BuildConfig
import chat.rocket.android.activity.MainActivity
import org.json.JSONObject import org.json.JSONObject
import java.util.*
object PushManager { object PushManager {
// A map associating a notification id to a list of corresponding messages.
val messageStack = SparseArray<ArrayList<String>>() val messageStack = SparseArray<ArrayList<String>>()
val randomizer = Random()
fun handle(context: Context, data: Bundle) { fun handle(context: Context, data: Bundle) {
val appContext = context.applicationContext val appContext = context.applicationContext
...@@ -39,6 +45,11 @@ object PushManager { ...@@ -39,6 +45,11 @@ object PushManager {
summaryText, summaryText,
style) style)
// We should use Timber here
if (BuildConfig.DEBUG) {
Log.d(PushMessage::class.java.simpleName, pushMessage.toString())
}
val res = appContext.resources val res = appContext.resources
val smallIcon = res.getIdentifier("rocket_chat_notification", "drawable", appContext.packageName) val smallIcon = res.getIdentifier("rocket_chat_notification", "drawable", appContext.packageName)
...@@ -50,20 +61,24 @@ object PushManager { ...@@ -50,20 +61,24 @@ object PushManager {
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) notification = createNotificationForOreoAndAbove(appContext, pushMessage, smallIcon, data)
notificationManager.notify(notificationId.toInt(), notification) notificationManager.notify(notificationId.toInt(), notification)
} else { } else {
notification = createCompatNotification(appContext, pushMessage, smallIcon) notification = createCompatNotification(appContext, pushMessage, smallIcon, data)
NotificationManagerCompat.from(appContext).notify(notificationId.toInt(), notification) NotificationManagerCompat.from(appContext).notify(notificationId.toInt(), notification)
} }
} }
fun clearStack(notificationId: Int) { fun clearMessageStack(notificationId: Int) {
messageStack.delete(notificationId) messageStack.delete(notificationId)
} }
private fun createCompatNotification(context: Context, pushMessage: PushMessage, smallIcon: Int): Notification { private fun createCompatNotification(context: Context, pushMessage: PushMessage, smallIcon: Int, data: Bundle): Notification {
with(pushMessage) { with(pushMessage) {
val id = notificationId.toInt()
val contentIntent = getContentIntent(context, id, data, pushMessage)
val deleteIntent = getDismissIntent(context, id)
val notificationBuilder = NotificationCompat.Builder(context) val notificationBuilder = NotificationCompat.Builder(context)
.setAutoCancel(true) .setAutoCancel(true)
.setShowWhen(true) .setShowWhen(true)
...@@ -73,25 +88,29 @@ object PushManager { ...@@ -73,25 +88,29 @@ object PushManager {
.setContentText(message.fromHtml()) .setContentText(message.fromHtml())
.setNumber(count.toInt()) .setNumber(count.toInt())
.setSmallIcon(smallIcon) .setSmallIcon(smallIcon)
.setDeleteIntent(getDismissIntent(context, notificationId.toInt())) .setDeleteIntent(deleteIntent)
.setContentIntent(contentIntent)
if ("inbox" == style) { if ("inbox" == style) {
val messages = chat.rocket.android.push.PushManager.messageStack.get(notificationId.toInt()) val messages = messageStack.get(notificationId.toInt())
val messageCount = messages.size val messageCount = messages.size
if (messageCount > 1) { if (messageCount > 1) {
val summary = summaryText.replace("%n%", messageCount.toString()) val summary = summaryText.replace("%n%", messageCount.toString())
.fromHtml() .fromHtml()
val inbox = android.support.v4.app.NotificationCompat.InboxStyle() val inbox = NotificationCompat.InboxStyle()
.setBigContentTitle(title.fromHtml()) .setBigContentTitle(title.fromHtml())
.setSummaryText(summary) .setSummaryText(summary)
messages.forEach { msg -> messages.forEach { msg ->
inbox.addLine(msg.fromHtml()) inbox.addLine(msg.fromHtml())
} }
notificationBuilder.setStyle(inbox) notificationBuilder.setStyle(inbox)
} else { } else {
val bigText = android.support.v4.app.NotificationCompat.BigTextStyle() val bigText = NotificationCompat.BigTextStyle()
.bigText(message.fromHtml()) .bigText(message.fromHtml())
.setBigContentTitle(title.fromHtml()) .setBigContentTitle(title.fromHtml())
notificationBuilder.setStyle(bigText) notificationBuilder.setStyle(bigText)
} }
} else { } else {
...@@ -103,11 +122,15 @@ object PushManager { ...@@ -103,11 +122,15 @@ object PushManager {
} }
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationForOreoAndAbove(context: Context, pushMessage: PushMessage, smallIcon: Int): Notification { private fun createNotificationForOreoAndAbove(context: Context, pushMessage: PushMessage, smallIcon: Int, data: Bundle): Notification {
val notificationManager: NotificationManager = val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
with(pushMessage) { with(pushMessage) {
val id = notificationId.toInt()
val contentIntent = getContentIntent(context, id, data, pushMessage)
val deleteIntent = getDismissIntent(context, id)
val channel = NotificationChannel(notificationId, sender.username, NotificationManager.IMPORTANCE_HIGH) val channel = NotificationChannel(notificationId, sender.username, NotificationManager.IMPORTANCE_HIGH)
val notification = Notification.Builder(context, pushMessage.rid) val notification = Notification.Builder(context, pushMessage.rid)
.setAutoCancel(true) .setAutoCancel(true)
...@@ -117,6 +140,8 @@ object PushManager { ...@@ -117,6 +140,8 @@ object PushManager {
.setContentText(message.fromHtml()) .setContentText(message.fromHtml())
.setNumber(count.toInt()) .setNumber(count.toInt())
.setSmallIcon(smallIcon) .setSmallIcon(smallIcon)
.setDeleteIntent(deleteIntent)
.setContentIntent(contentIntent)
.build() .build()
channel.enableLights(true) channel.enableLights(true)
...@@ -144,6 +169,17 @@ object PushManager { ...@@ -144,6 +169,17 @@ object PushManager {
return PendingIntent.getBroadcast(context, notificationId, deleteIntent, 0) return PendingIntent.getBroadcast(context, notificationId, deleteIntent, 0)
} }
private fun getContentIntent(context: Context, notificationId: Int, extras: Bundle, pushMessage: PushMessage): PendingIntent {
val notificationIntent = Intent(context, MainActivity::class.java)
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
notificationIntent.putExtra(PushConstants.PUSH_BUNDLE, extras)
notificationIntent.putExtra(PushConstants.NOT_ID, notificationId)
notificationIntent.putExtra(PushConstants.HOSTNAME, pushMessage.host)
notificationIntent.putExtra(PushConstants.ROOM_ID, pushMessage.rid)
return PendingIntent.getActivity(context, randomizer.nextInt(), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
}
data class PushMessage(val title: String, data class PushMessage(val title: String,
val message: String, val message: String,
val image: String?, val image: String?,
...@@ -187,7 +223,7 @@ object PushManager { ...@@ -187,7 +223,7 @@ object PushManager {
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
val notificationId = intent?.extras?.getInt("notId") val notificationId = intent?.extras?.getInt("notId")
if (notificationId != null) { if (notificationId != null) {
PushManager.clearStack(notificationId) PushManager.clearMessageStack(notificationId)
} }
} }
} }
......
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