ntfy-android/app/src/main/java/io/heckel/ntfy/ui/DetailActivity.kt

771 lines
33 KiB
Kotlin
Raw Normal View History

2021-11-01 08:19:25 +13:00
package io.heckel.ntfy.ui
import android.app.AlertDialog
2021-11-08 15:02:27 +13:00
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
2022-01-31 08:05:36 +13:00
import android.content.Intent
2022-04-12 14:13:03 +12:00
import android.content.Intent.ACTION_VIEW
import android.net.Uri
2021-11-01 08:19:25 +13:00
import android.os.Bundle
2021-11-02 05:12:36 +13:00
import android.text.Html
2021-11-04 06:56:08 +13:00
import android.view.ActionMode
2021-11-01 08:19:25 +13:00
import android.view.Menu
import android.view.MenuItem
import android.view.View
2021-11-02 05:12:36 +13:00
import android.widget.TextView
2021-11-02 02:57:05 +13:00
import android.widget.Toast
2021-11-01 08:19:25 +13:00
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
2021-11-04 06:56:08 +13:00
import androidx.core.content.ContextCompat
2021-11-08 07:13:32 +13:00
import androidx.lifecycle.lifecycleScope
2022-02-06 15:02:05 +13:00
import androidx.recyclerview.widget.ItemTouchHelper
2021-11-01 08:19:25 +13:00
import androidx.recyclerview.widget.RecyclerView
2021-11-15 10:48:50 +13:00
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
2022-02-06 15:02:05 +13:00
import com.google.android.material.snackbar.Snackbar
2021-11-25 10:12:51 +13:00
import io.heckel.ntfy.BuildConfig
2021-11-01 08:19:25 +13:00
import io.heckel.ntfy.R
import io.heckel.ntfy.app.Application
2022-01-19 08:28:48 +13:00
import io.heckel.ntfy.db.Notification
2022-01-20 15:05:41 +13:00
import io.heckel.ntfy.db.Repository
2022-04-12 14:13:03 +12:00
import io.heckel.ntfy.db.Subscription
import io.heckel.ntfy.firebase.FirebaseMessenger
import io.heckel.ntfy.util.Log
2021-11-08 07:13:32 +13:00
import io.heckel.ntfy.msg.ApiService
import io.heckel.ntfy.msg.NotificationService
import io.heckel.ntfy.service.SubscriberServiceManager
import io.heckel.ntfy.util.*
2021-11-23 09:45:43 +13:00
import kotlinx.coroutines.*
2021-11-02 02:57:05 +13:00
import java.util.*
import kotlin.random.Random
2021-11-14 13:26:37 +13:00
2022-02-06 15:02:05 +13:00
2021-11-22 08:54:13 +13:00
class DetailActivity : AppCompatActivity(), ActionMode.Callback, NotificationFragment.NotificationSettingsListener {
2021-11-01 08:19:25 +13:00
private val viewModel by viewModels<DetailViewModel> {
DetailViewModelFactory((application as Application).repository)
}
2021-11-08 07:13:32 +13:00
private val repository by lazy { (application as Application).repository }
private val api = ApiService()
private val messenger = FirebaseMessenger()
private var notifier: NotificationService? = null // Context-dependent
private var appBaseUrl: String? = null // Context-dependent
2021-11-04 06:56:08 +13:00
// Which subscription are we looking at
2021-11-01 08:19:25 +13:00
private var subscriptionId: Long = 0L // Set in onCreate()
2021-11-02 02:57:05 +13:00
private var subscriptionBaseUrl: String = "" // Set in onCreate()
2021-11-01 08:19:25 +13:00
private var subscriptionTopic: String = "" // Set in onCreate()
private var subscriptionDisplayName: String = "" // Set in onCreate() & updated by options menu!
private var subscriptionInstant: Boolean = false // Set in onCreate() & updated by options menu!
2021-11-23 09:45:43 +13:00
private var subscriptionMutedUntil: Long = 0L // Set in onCreate() & updated by options menu!
2021-11-01 08:19:25 +13:00
// UI elements
2021-11-04 06:56:08 +13:00
private lateinit var adapter: DetailAdapter
private lateinit var mainList: RecyclerView
2021-11-15 10:48:50 +13:00
private lateinit var mainListContainer: SwipeRefreshLayout
private lateinit var menu: Menu
// Action mode stuff
2021-11-04 06:56:08 +13:00
private var actionMode: ActionMode? = null
2021-11-01 08:19:25 +13:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2021-11-23 09:45:43 +13:00
setContentView(R.layout.activity_detail)
2021-11-01 08:19:25 +13:00
2022-04-12 14:13:03 +12:00
Log.d(TAG, "Create $this")
// Dependencies that depend on Context
notifier = NotificationService(this)
appBaseUrl = getString(R.string.app_base_url)
// Show 'Back' button
supportActionBar?.setDisplayHomeAsUpEnabled(true)
2021-11-08 07:13:32 +13:00
2022-04-12 14:13:03 +12:00
// Handle direct deep links to topic "ntfy://..."
val url = intent?.data
if (intent?.action == ACTION_VIEW && url != null) {
maybeSubscribeAndLoadView(url)
} else {
loadView()
}
}
private fun maybeSubscribeAndLoadView(url: Uri) {
if (url.pathSegments.size != 1) {
Log.w(TAG, "Invalid link $url. Aborting.")
finish()
return
}
val secure = url.getBooleanQueryParameter("secure", true)
val baseUrl = if (secure) "https://${url.host}" else "http://${url.host}"
val topic = url.pathSegments.first()
2022-06-25 04:32:48 +12:00
title = topicShortUrl(baseUrl, topic)
2022-04-12 14:13:03 +12:00
2022-04-26 02:18:26 +12:00
// Subscribe to topic if it doesn't already exist
2022-04-12 14:13:03 +12:00
lifecycleScope.launch(Dispatchers.IO) {
var subscription = repository.getSubscription(baseUrl, topic)
if (subscription == null) {
val instant = baseUrl != appBaseUrl
subscription = Subscription(
2022-06-20 06:45:01 +12:00
id = randomSubscriptionId(),
2022-04-12 14:13:03 +12:00
baseUrl = baseUrl,
topic = topic,
instant = instant,
mutedUntil = 0,
2022-05-06 08:56:06 +12:00
minPriority = Repository.MIN_PRIORITY_USE_GLOBAL,
autoDelete = Repository.AUTO_DELETE_USE_GLOBAL,
2022-06-19 13:01:05 +12:00
lastNotificationId = null,
2022-05-07 13:03:15 +12:00
icon = null,
2022-04-12 14:13:03 +12:00
upAppId = null,
upConnectorToken = null,
displayName = null,
2022-08-19 13:11:29 +12:00
encryptionKey = null,
2022-04-12 14:13:03 +12:00
totalCount = 0,
newCount = 0,
lastActive = Date().time/1000
)
repository.addSubscription(subscription)
// Subscribe to Firebase topic if ntfy.sh (even if instant, just to be sure!)
if (baseUrl == appBaseUrl) {
Log.d(TAG, "Subscribing to Firebase topic $topic")
messenger.subscribe(topic)
}
// Fetch cached messages
try {
val user = repository.getUser(subscription.baseUrl) // May be null
2022-08-19 13:11:29 +12:00
val notifications = api
.poll(subscription.id, subscription.baseUrl, subscription.topic, user)
.map { n -> Encryption.maybeDecrypt(subscription, n) }
2022-04-12 14:13:03 +12:00
notifications.forEach { notification -> repository.addNotification(notification) }
} catch (e: Exception) {
Log.e(TAG, "Unable to fetch notifications: ${e.message}", e)
}
runOnUiThread {
val message = getString(R.string.detail_deep_link_subscribed_toast_message, topicShortUrl(baseUrl, topic))
Toast.makeText(this@DetailActivity, message, Toast.LENGTH_LONG).show()
}
}
2022-04-26 02:18:26 +12:00
// Add extras needed in loadView(); normally these are added in MainActivity
2022-04-12 14:13:03 +12:00
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_ID, subscription.id)
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_BASE_URL, subscription.baseUrl)
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_TOPIC, subscription.topic)
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_DISPLAY_NAME, displayName(subscription))
2022-04-12 14:13:03 +12:00
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_INSTANT, subscription.instant)
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_MUTED_UNTIL, subscription.mutedUntil)
runOnUiThread {
loadView()
}
}
}
private fun loadView() {
2021-11-01 08:19:25 +13:00
// Get extras required for the return to the main activity
subscriptionId = intent.getLongExtra(MainActivity.EXTRA_SUBSCRIPTION_ID, 0)
2021-11-02 02:57:05 +13:00
subscriptionBaseUrl = intent.getStringExtra(MainActivity.EXTRA_SUBSCRIPTION_BASE_URL) ?: return
2021-11-01 08:19:25 +13:00
subscriptionTopic = intent.getStringExtra(MainActivity.EXTRA_SUBSCRIPTION_TOPIC) ?: return
subscriptionDisplayName = intent.getStringExtra(MainActivity.EXTRA_SUBSCRIPTION_DISPLAY_NAME) ?: return
2021-11-14 13:26:37 +13:00
subscriptionInstant = intent.getBooleanExtra(MainActivity.EXTRA_SUBSCRIPTION_INSTANT, false)
2021-11-23 09:45:43 +13:00
subscriptionMutedUntil = intent.getLongExtra(MainActivity.EXTRA_SUBSCRIPTION_MUTED_UNTIL, 0L)
2021-11-01 08:19:25 +13:00
// Set title
val subscriptionBaseUrl = intent.getStringExtra(MainActivity.EXTRA_SUBSCRIPTION_BASE_URL) ?: return
2021-11-02 05:12:36 +13:00
val topicUrl = topicShortUrl(subscriptionBaseUrl, subscriptionTopic)
title = subscriptionDisplayName
2021-11-02 05:12:36 +13:00
// Set "how to instructions"
val howToExample: TextView = findViewById(R.id.detail_how_to_example)
howToExample.linksClickable = true
val howToText = getString(R.string.detail_how_to_example, topicUrl)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
howToExample.text = Html.fromHtml(howToText, Html.FROM_HTML_MODE_LEGACY)
} else {
howToExample.text = Html.fromHtml(howToText)
}
2021-11-01 08:19:25 +13:00
2021-11-15 10:48:50 +13:00
// Swipe to refresh
mainListContainer = findViewById(R.id.detail_notification_list_container)
mainListContainer.setOnRefreshListener { refresh() }
mainListContainer.setColorSchemeResources(Colors.refreshProgressIndicator)
2021-11-15 10:48:50 +13:00
2021-11-01 08:19:25 +13:00
// Update main list based on viewModel (& its datasource/livedata)
2021-11-02 04:12:09 +13:00
val noEntriesText: View = findViewById(R.id.detail_no_notifications)
2021-11-04 06:56:08 +13:00
val onNotificationClick = { n: Notification -> onNotificationClick(n) }
val onNotificationLongClick = { n: Notification -> onNotificationLongClick(n) }
2022-04-30 03:03:02 +12:00
adapter = DetailAdapter(this, lifecycleScope, repository, onNotificationClick, onNotificationLongClick)
2021-11-04 06:56:08 +13:00
mainList = findViewById(R.id.detail_notification_list)
2021-11-01 08:19:25 +13:00
mainList.adapter = adapter
viewModel.list(subscriptionId).observe(this) {
it?.let {
// Show list view
2021-11-01 08:19:25 +13:00
adapter.submitList(it as MutableList<Notification>)
if (it.isEmpty()) {
2021-11-15 10:48:50 +13:00
mainListContainer.visibility = View.GONE
2021-11-01 08:19:25 +13:00
noEntriesText.visibility = View.VISIBLE
} else {
2021-11-15 10:48:50 +13:00
mainListContainer.visibility = View.VISIBLE
2021-11-01 08:19:25 +13:00
noEntriesText.visibility = View.GONE
}
// Cancel notifications that still have popups
maybeCancelNotificationPopups(it)
2021-11-01 08:19:25 +13:00
}
}
2022-02-06 15:02:05 +13:00
// Swipe to remove
val itemTouchCallback = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
return false
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, swipeDir: Int) {
val notification = adapter.get(viewHolder.absoluteAdapterPosition)
lifecycleScope.launch(Dispatchers.IO) {
repository.markAsDeleted(notification.id)
}
val snackbar = Snackbar.make(mainList, R.string.detail_item_snack_deleted, Snackbar.LENGTH_SHORT)
snackbar.setAction(R.string.detail_item_snack_undo) {
lifecycleScope.launch(Dispatchers.IO) {
repository.undeleteNotification(notification.id)
}
}
snackbar.show()
}
}
val itemTouchHelper = ItemTouchHelper(itemTouchCallback)
itemTouchHelper.attachToRecyclerView(mainList)
// Scroll up when new notification is added
adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
if (positionStart == 0) {
Log.d(TAG, "$itemCount item(s) inserted at $positionStart, scrolling to the top")
mainList.scrollToPosition(positionStart)
}
}
})
// React to changes in fast delivery setting
repository.getSubscriptionIdsWithInstantStatusLiveData().observe(this) {
2021-12-31 14:00:08 +13:00
SubscriberServiceManager.refresh(this)
}
// Mark this subscription as "open" so we don't receive notifications for it
repository.detailViewSubscriptionId.set(subscriptionId)
}
override fun onResume() {
super.onResume()
2022-05-06 13:06:21 +12:00
// Mark as "open" so we don't send notifications while this is open
repository.detailViewSubscriptionId.set(subscriptionId)
// Update buttons (this is for when we return from the preferences screen)
lifecycleScope.launch(Dispatchers.IO) {
val subscription = repository.getSubscription(subscriptionId) ?: return@launch
subscriptionInstant = subscription.instant
subscriptionMutedUntil = subscription.mutedUntil
subscriptionDisplayName = displayName(subscription)
2022-05-06 13:06:21 +12:00
showHideInstantMenuItems(subscriptionInstant)
showHideMutedUntilMenuItems(subscriptionMutedUntil)
updateTitle(subscriptionDisplayName)
2022-05-06 13:06:21 +12:00
}
}
override fun onPause() {
super.onPause()
2021-11-23 09:45:43 +13:00
Log.d(TAG, "onPause hook: Removing 'notificationId' from all notifications for $subscriptionId")
GlobalScope.launch(Dispatchers.IO) {
// Note: This is here and not in onDestroy/onStop, because we want to clear notifications as early
// as possible, so that we don't see the "new" bubble in the main list anymore.
repository.clearAllNotificationIds(subscriptionId)
}
Log.d(TAG, "onPause hook: Marking subscription $subscriptionId as 'not open'")
repository.detailViewSubscriptionId.set(0) // Mark as closed
}
private fun maybeCancelNotificationPopups(notifications: List<Notification>) {
val notificationsWithPopups = notifications.filter { notification -> notification.notificationId != 0 }
if (notificationsWithPopups.isNotEmpty()) {
lifecycleScope.launch(Dispatchers.IO) {
notificationsWithPopups.forEach { notification ->
notifier?.cancel(notification)
2021-11-23 09:45:43 +13:00
// Do NOT remove the notificationId here, we need that for the UI indicators; we'll remove it in onPause()
}
}
}
2021-11-01 08:19:25 +13:00
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
2021-11-23 09:45:43 +13:00
menuInflater.inflate(R.menu.menu_detail_action_bar, menu)
this.menu = menu
2021-11-23 09:45:43 +13:00
// Show and hide buttons
showHideInstantMenuItems(subscriptionInstant)
2022-05-06 13:06:21 +12:00
showHideMutedUntilMenuItems(subscriptionMutedUntil)
2021-11-23 09:45:43 +13:00
// Regularly check if "notification muted" time has passed
// NOTE: This is done here, because then we know that we've initialized the menu items.
startNotificationMutedChecker()
2021-11-01 08:19:25 +13:00
return true
}
2021-11-23 09:45:43 +13:00
private fun startNotificationMutedChecker() {
2022-05-06 13:06:21 +12:00
// FIXME This is awful and has to go.
2021-11-23 09:45:43 +13:00
lifecycleScope.launch(Dispatchers.IO) {
delay(1000) // Just to be sure we've initialized all the things, we wait a bit ...
while (isActive) {
Log.d(TAG, "Checking 'muted until' timestamp for subscription $subscriptionId")
val subscription = repository.getSubscription(subscriptionId) ?: return@launch
val mutedUntilExpired = subscription.mutedUntil > 1L && System.currentTimeMillis()/1000 > subscription.mutedUntil
if (mutedUntilExpired) {
val newSubscription = subscription.copy(mutedUntil = 0L)
repository.updateSubscription(newSubscription)
2022-05-06 13:06:21 +12:00
showHideMutedUntilMenuItems(0L)
2021-11-23 09:45:43 +13:00
}
delay(60_000)
}
}
}
2021-11-01 08:19:25 +13:00
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
2021-11-02 02:57:05 +13:00
R.id.detail_menu_test -> {
onTestClick()
true
}
2021-11-23 09:45:43 +13:00
R.id.detail_menu_notifications_enabled -> {
2022-01-31 08:05:36 +13:00
onMutedUntilClick(enable = false)
2021-11-23 09:45:43 +13:00
true
}
R.id.detail_menu_notifications_disabled_until -> {
2022-01-31 08:05:36 +13:00
onMutedUntilClick(enable = true)
2021-11-23 09:45:43 +13:00
true
}
R.id.detail_menu_notifications_disabled_forever -> {
2022-01-31 08:05:36 +13:00
onMutedUntilClick(enable = true)
2021-11-22 08:54:13 +13:00
true
}
R.id.detail_menu_enable_instant -> {
onInstantEnableClick(enable = true)
true
}
R.id.detail_menu_disable_instant -> {
onInstantEnableClick(enable = false)
true
}
R.id.detail_menu_copy_url -> {
onCopyUrlClick()
true
}
R.id.detail_menu_clear -> {
onClearClick()
true
}
2022-05-06 08:56:06 +12:00
R.id.detail_menu_settings -> {
2022-01-31 08:05:36 +13:00
onSettingsClick()
true
2022-05-06 08:56:06 +12:00
}
2021-11-04 05:48:13 +13:00
R.id.detail_menu_unsubscribe -> {
2021-11-01 08:19:25 +13:00
onDeleteClick()
true
}
else -> super.onOptionsItemSelected(item)
}
}
2021-11-08 07:29:19 +13:00
private fun onTestClick() {
Log.d(TAG, "Sending test notification to ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")
lifecycleScope.launch(Dispatchers.IO) {
try {
val user = repository.getUser(subscriptionBaseUrl) // May be null
2021-11-29 13:28:58 +13:00
val possibleTags = listOf(
"warning", "skull", "success", "triangular_flag_on_post", "de", "dog", "rotating_light", "cat", "bike", // Emojis
"backup", "rsync", "de-server1", "this-is-a-tag"
)
2021-11-28 10:18:09 +13:00
val priority = Random.nextInt(1, 6)
2021-11-29 13:28:58 +13:00
val tags = possibleTags.shuffled().take(Random.nextInt(0, 4))
2021-11-28 10:18:09 +13:00
val title = if (Random.nextBoolean()) getString(R.string.detail_test_title) else ""
val message = getString(R.string.detail_test_message, priority)
api.publish(subscriptionBaseUrl, subscriptionTopic, user, message, title, priority, tags, delay = "")
} catch (e: Exception) {
2021-11-14 13:26:37 +13:00
runOnUiThread {
2022-02-05 13:52:34 +13:00
val message = if (e is ApiService.UnauthorizedException) {
if (e.user != null) {
getString(R.string.detail_test_message_error_unauthorized_user, e.user.username)
} else {
getString(R.string.detail_test_message_error_unauthorized_anon)
}
} else {
getString(R.string.detail_test_message_error, e.message)
}
2021-11-14 13:26:37 +13:00
Toast
2022-02-05 13:52:34 +13:00
.makeText(this@DetailActivity, message, Toast.LENGTH_LONG)
2021-11-14 13:26:37 +13:00
.show()
}
}
2021-11-08 07:29:19 +13:00
}
}
2022-01-31 08:05:36 +13:00
private fun onMutedUntilClick(enable: Boolean) {
2021-11-23 09:45:43 +13:00
if (!enable) {
Log.d(TAG, "Showing notification settings dialog for ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")
val notificationFragment = NotificationFragment()
notificationFragment.show(supportFragmentManager, NotificationFragment.TAG)
} else {
Log.d(TAG, "Re-enabling notifications ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")
2022-01-20 15:05:41 +13:00
onNotificationMutedUntilChanged(Repository.MUTED_UNTIL_SHOW_ALL)
2021-11-23 09:45:43 +13:00
}
2021-11-22 08:54:13 +13:00
}
2021-11-23 09:45:43 +13:00
override fun onNotificationMutedUntilChanged(mutedUntilTimestamp: Long) {
2021-11-22 08:54:13 +13:00
lifecycleScope.launch(Dispatchers.IO) {
Log.d(TAG, "Setting subscription 'muted until' to $mutedUntilTimestamp")
2021-11-22 08:54:13 +13:00
val subscription = repository.getSubscription(subscriptionId)
2021-11-23 09:45:43 +13:00
val newSubscription = subscription?.copy(mutedUntil = mutedUntilTimestamp)
2021-11-22 08:54:13 +13:00
newSubscription?.let { repository.updateSubscription(newSubscription) }
2021-11-23 09:45:43 +13:00
subscriptionMutedUntil = mutedUntilTimestamp
2022-05-06 13:06:21 +12:00
showHideMutedUntilMenuItems(mutedUntilTimestamp)
2021-11-22 08:54:13 +13:00
runOnUiThread {
2021-11-23 09:45:43 +13:00
when (mutedUntilTimestamp) {
0L -> Toast.makeText(this@DetailActivity, getString(R.string.notification_dialog_enabled_toast_message), Toast.LENGTH_LONG).show()
1L -> Toast.makeText(this@DetailActivity, getString(R.string.notification_dialog_muted_forever_toast_message), Toast.LENGTH_LONG).show()
2021-11-22 08:54:13 +13:00
else -> {
2021-11-23 09:45:43 +13:00
val formattedDate = formatDateShort(mutedUntilTimestamp)
Toast.makeText(this@DetailActivity, getString(R.string.notification_dialog_muted_until_toast_message, formattedDate), Toast.LENGTH_LONG).show()
2021-11-22 08:54:13 +13:00
}
}
}
}
}
private fun onCopyUrlClick() {
val url = topicUrl(subscriptionBaseUrl, subscriptionTopic)
Log.d(TAG, "Copying topic URL $url to clipboard ")
runOnUiThread {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("topic address", url)
clipboard.setPrimaryClip(clip)
Toast
.makeText(this, getString(R.string.detail_copied_to_clipboard_message), Toast.LENGTH_LONG)
.show()
}
}
2021-11-15 10:48:50 +13:00
private fun refresh() {
2021-11-08 07:29:19 +13:00
Log.d(TAG, "Fetching cached notifications for ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")
lifecycleScope.launch(Dispatchers.IO) {
try {
2022-06-19 13:01:05 +12:00
val subscription = repository.getSubscription(subscriptionId) ?: return@launch
val user = repository.getUser(subscription.baseUrl) // May be null
2022-08-19 13:11:29 +12:00
val notifications = api
.poll(subscription.id, subscription.baseUrl, subscription.topic, user, subscription.lastNotificationId)
.map { n -> Encryption.maybeDecrypt(subscription, n) }
val newNotifications = repository.onlyNewNotifications(subscriptionId, notifications)
2021-11-08 07:13:32 +13:00
val toastMessage = if (newNotifications.isEmpty()) {
getString(R.string.refresh_message_no_results)
2021-11-08 07:13:32 +13:00
} else {
getString(R.string.refresh_message_result, newNotifications.size)
2021-11-08 07:13:32 +13:00
}
newNotifications.forEach { notification -> repository.addNotification(notification) }
2021-11-15 10:48:50 +13:00
runOnUiThread {
Toast.makeText(this@DetailActivity, toastMessage, Toast.LENGTH_LONG).show()
mainListContainer.isRefreshing = false
}
} catch (e: Exception) {
2021-11-19 08:59:54 +13:00
Log.e(TAG, "Error fetching notifications for ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}: ${e.stackTrace}", e)
2021-11-14 13:26:37 +13:00
runOnUiThread {
Toast
.makeText(this@DetailActivity, getString(R.string.refresh_message_error_one, e.message), Toast.LENGTH_LONG)
2021-11-14 13:26:37 +13:00
.show()
2021-11-15 10:48:50 +13:00
mainListContainer.isRefreshing = false
2021-11-14 13:26:37 +13:00
}
2021-11-02 02:57:05 +13:00
}
2021-11-08 07:13:32 +13:00
}
}
private fun onInstantEnableClick(enable: Boolean) {
Log.d(TAG, "Toggling instant delivery setting for ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")
lifecycleScope.launch(Dispatchers.IO) {
val subscription = repository.getSubscription(subscriptionId)
val newSubscription = subscription?.copy(instant = enable)
newSubscription?.let { repository.updateSubscription(newSubscription) }
showHideInstantMenuItems(enable)
runOnUiThread {
if (enable) {
Toast.makeText(this@DetailActivity, getString(R.string.detail_instant_delivery_enabled), Toast.LENGTH_SHORT)
.show()
} else {
Toast.makeText(this@DetailActivity, getString(R.string.detail_instant_delivery_disabled), Toast.LENGTH_SHORT)
.show()
}
}
}
}
private fun showHideInstantMenuItems(enable: Boolean) {
2022-05-06 13:06:21 +12:00
if (!this::menu.isInitialized) {
return
}
subscriptionInstant = enable
runOnUiThread {
val appBaseUrl = getString(R.string.app_base_url)
val enableInstantItem = menu.findItem(R.id.detail_menu_enable_instant)
val disableInstantItem = menu.findItem(R.id.detail_menu_disable_instant)
2021-11-25 10:12:51 +13:00
val allowToggleInstant = BuildConfig.FIREBASE_AVAILABLE && subscriptionBaseUrl == appBaseUrl
if (allowToggleInstant) {
enableInstantItem?.isVisible = !subscriptionInstant
disableInstantItem?.isVisible = subscriptionInstant
} else {
enableInstantItem?.isVisible = false
disableInstantItem?.isVisible = false
}
}
}
2022-05-06 13:06:21 +12:00
private fun showHideMutedUntilMenuItems(mutedUntilTimestamp: Long) {
if (!this::menu.isInitialized) {
return
}
2021-11-23 09:45:43 +13:00
subscriptionMutedUntil = mutedUntilTimestamp
runOnUiThread {
val notificationsEnabledItem = menu.findItem(R.id.detail_menu_notifications_enabled)
val notificationsDisabledUntilItem = menu.findItem(R.id.detail_menu_notifications_disabled_until)
val notificationsDisabledForeverItem = menu.findItem(R.id.detail_menu_notifications_disabled_forever)
notificationsEnabledItem?.isVisible = subscriptionMutedUntil == 0L
notificationsDisabledForeverItem?.isVisible = subscriptionMutedUntil == 1L
notificationsDisabledUntilItem?.isVisible = subscriptionMutedUntil > 1L
if (subscriptionMutedUntil > 1L) {
val formattedDate = formatDateShort(subscriptionMutedUntil)
notificationsDisabledUntilItem?.title = getString(R.string.detail_menu_notifications_disabled_until, formattedDate)
}
}
}
private fun updateTitle(subscriptionDisplayName: String) {
runOnUiThread {
title = subscriptionDisplayName
}
}
private fun onClearClick() {
Log.d(TAG, "Clearing all notifications for ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")
val builder = AlertDialog.Builder(this)
val dialog = builder
.setMessage(R.string.detail_clear_dialog_message)
.setPositiveButton(R.string.detail_clear_dialog_permanently_delete) { _, _ ->
lifecycleScope.launch(Dispatchers.IO) {
repository.markAllAsDeleted(subscriptionId)
}
}
.setNegativeButton(R.string.detail_clear_dialog_cancel) { _, _ -> /* Do nothing */ }
.create()
dialog.setOnShowListener {
dialog
.getButton(AlertDialog.BUTTON_POSITIVE)
.setTextAppearance(R.style.DangerText)
}
dialog.show()
}
2022-01-31 08:05:36 +13:00
private fun onSettingsClick() {
Log.d(TAG, "Opening subscription settings for ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")
val intent = Intent(this, DetailSettingsActivity::class.java)
intent.putExtra(EXTRA_SUBSCRIPTION_ID, subscriptionId)
2022-05-06 08:56:06 +12:00
intent.putExtra(EXTRA_SUBSCRIPTION_BASE_URL, subscriptionBaseUrl)
intent.putExtra(EXTRA_SUBSCRIPTION_TOPIC, subscriptionTopic)
intent.putExtra(EXTRA_SUBSCRIPTION_DISPLAY_NAME, subscriptionDisplayName)
startActivity(intent)
2022-01-31 08:05:36 +13:00
}
2021-11-01 08:19:25 +13:00
private fun onDeleteClick() {
2021-11-02 02:57:05 +13:00
Log.d(TAG, "Deleting subscription ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")
2021-11-01 08:19:25 +13:00
val builder = AlertDialog.Builder(this)
val dialog = builder
2021-11-01 08:19:25 +13:00
.setMessage(R.string.detail_delete_dialog_message)
.setPositiveButton(R.string.detail_delete_dialog_permanently_delete) { _, _ ->
2022-04-12 14:13:03 +12:00
Log.d(TAG, "Deleting subscription with subscription ID $subscriptionId (topic: $subscriptionTopic)")
GlobalScope.launch(Dispatchers.IO) {
repository.removeAllNotifications(subscriptionId)
repository.removeSubscription(subscriptionId)
if (subscriptionBaseUrl == appBaseUrl) {
messenger.unsubscribe(subscriptionTopic)
}
}
2021-11-01 08:19:25 +13:00
finish()
}
.setNegativeButton(R.string.detail_delete_dialog_cancel) { _, _ -> /* Do nothing */ }
.create()
dialog.setOnShowListener {
dialog
.getButton(AlertDialog.BUTTON_POSITIVE)
.setTextAppearance(R.style.DangerText)
}
dialog.show()
2021-11-01 08:19:25 +13:00
}
private fun onNotificationClick(notification: Notification) {
2021-11-04 06:56:08 +13:00
if (actionMode != null) {
handleActionModeClick(notification)
} else if (notification.click != "") {
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(notification.click)))
} catch (e: Exception) {
Log.w(TAG, "Cannot open click URL", e)
runOnUiThread {
Toast
2022-05-04 11:59:33 +12:00
.makeText(this@DetailActivity, getString(R.string.detail_item_cannot_open_url, e.message), Toast.LENGTH_LONG)
.show()
}
}
2021-11-08 15:02:27 +13:00
} else {
copyToClipboard(notification)
2021-11-04 06:56:08 +13:00
}
}
2021-11-08 15:02:27 +13:00
private fun copyToClipboard(notification: Notification) {
runOnUiThread {
copyToClipboard(this, notification)
}
2021-11-08 15:02:27 +13:00
}
2021-11-04 06:56:08 +13:00
private fun onNotificationLongClick(notification: Notification) {
if (actionMode == null) {
beginActionMode(notification)
}
}
private fun handleActionModeClick(notification: Notification) {
adapter.toggleSelection(notification.id)
if (adapter.selected.size == 0) {
finishActionMode()
} else {
actionMode!!.title = adapter.selected.size.toString()
}
}
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
this.actionMode = mode
if (mode != null) {
2021-11-23 09:45:43 +13:00
mode.menuInflater.inflate(R.menu.menu_detail_action_mode, menu)
2021-11-04 06:56:08 +13:00
mode.title = "1" // One item selected
}
return true
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return false
}
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
return when (item?.itemId) {
2021-11-15 14:22:02 +13:00
R.id.detail_action_mode_copy -> {
onMultiCopyClick()
true
}
2021-11-04 06:56:08 +13:00
R.id.detail_action_mode_delete -> {
onMultiDeleteClick()
true
}
else -> false
}
}
2021-11-15 14:22:02 +13:00
private fun onMultiCopyClick() {
Log.d(TAG, "Copying multiple notifications to clipboard")
lifecycleScope.launch(Dispatchers.IO) {
val content = adapter.selected.joinToString("\n\n") { notificationId ->
val notification = repository.getNotification(notificationId)
notification?.let {
decodeMessage(it) + "\n" + Date(it.timestamp * 1000).toString()
2021-11-15 14:22:02 +13:00
}.orEmpty()
}
runOnUiThread {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("notifications", content)
clipboard.setPrimaryClip(clip)
2021-11-15 14:22:02 +13:00
Toast
.makeText(this@DetailActivity, getString(R.string.detail_copied_to_clipboard_message), Toast.LENGTH_LONG)
.show()
finishActionMode()
2021-11-15 14:22:02 +13:00
}
}
}
2021-11-04 06:56:08 +13:00
private fun onMultiDeleteClick() {
Log.d(TAG, "Showing multi-delete dialog for selected items")
val builder = AlertDialog.Builder(this)
val dialog = builder
2021-11-04 06:56:08 +13:00
.setMessage(R.string.detail_action_mode_delete_dialog_message)
.setPositiveButton(R.string.detail_action_mode_delete_dialog_permanently_delete) { _, _ ->
adapter.selected.map { notificationId -> viewModel.markAsDeleted(notificationId) }
2021-11-04 06:56:08 +13:00
finishActionMode()
}
.setNegativeButton(R.string.detail_action_mode_delete_dialog_cancel) { _, _ ->
finishActionMode()
}
.create()
dialog.setOnShowListener {
dialog
.getButton(AlertDialog.BUTTON_POSITIVE)
.setTextAppearance(R.style.DangerText)
}
dialog.show()
2021-11-04 06:56:08 +13:00
}
override fun onDestroyActionMode(mode: ActionMode?) {
endActionModeAndRedraw()
}
private fun beginActionMode(notification: Notification) {
actionMode = startActionMode(this)
2022-06-06 10:22:26 +12:00
adapter.toggleSelection(notification.id)
2021-11-04 06:56:08 +13:00
// Fade status bar color
val fromColor = ContextCompat.getColor(this, Colors.statusBarNormal(this))
val toColor = ContextCompat.getColor(this, Colors.statusBarActionMode(this))
2021-11-04 06:56:08 +13:00
fadeStatusBarColor(window, fromColor, toColor)
}
private fun finishActionMode() {
actionMode!!.finish()
endActionModeAndRedraw()
}
private fun endActionModeAndRedraw() {
actionMode = null
adapter.selected.clear()
adapter.notifyItemRangeChanged(0, adapter.currentList.size)
2021-11-04 06:56:08 +13:00
// Fade status bar color
val fromColor = ContextCompat.getColor(this, Colors.statusBarActionMode(this))
val toColor = ContextCompat.getColor(this, Colors.statusBarNormal(this))
2021-11-04 06:56:08 +13:00
fadeStatusBarColor(window, fromColor, toColor)
}
2021-11-02 02:57:05 +13:00
companion object {
const val TAG = "NtfyDetailActivity"
const val EXTRA_SUBSCRIPTION_ID = "subscriptionId"
2022-05-06 08:56:06 +12:00
const val EXTRA_SUBSCRIPTION_BASE_URL = "baseUrl"
const val EXTRA_SUBSCRIPTION_TOPIC = "topic"
const val EXTRA_SUBSCRIPTION_DISPLAY_NAME = "displayName"
2021-11-01 08:19:25 +13:00
}
}