2021-10-31 20:19:25 +01:00
|
|
|
package io.heckel.ntfy.ui
|
|
|
|
|
|
|
|
import android.app.AlertDialog
|
2021-11-08 03:02:27 +01:00
|
|
|
import android.content.ClipData
|
|
|
|
import android.content.ClipboardManager
|
|
|
|
import android.content.Context
|
2021-10-31 20:19:25 +01:00
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
2021-11-01 17:12:36 +01:00
|
|
|
import android.text.Html
|
2021-11-01 14:57:05 +01:00
|
|
|
import android.util.Log
|
2021-11-03 18:56:08 +01:00
|
|
|
import android.view.ActionMode
|
2021-10-31 20:19:25 +01:00
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.View
|
2021-11-01 17:12:36 +01:00
|
|
|
import android.widget.TextView
|
2021-11-01 14:57:05 +01:00
|
|
|
import android.widget.Toast
|
2021-10-31 20:19:25 +01:00
|
|
|
import androidx.activity.viewModels
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2021-11-03 18:56:08 +01:00
|
|
|
import androidx.core.content.ContextCompat
|
2021-11-07 19:13:32 +01:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2021-10-31 20:19:25 +01:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2021-11-14 22:48:50 +01:00
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
2021-10-31 20:19:25 +01:00
|
|
|
import io.heckel.ntfy.R
|
|
|
|
import io.heckel.ntfy.app.Application
|
|
|
|
import io.heckel.ntfy.data.Notification
|
|
|
|
import io.heckel.ntfy.data.topicShortUrl
|
2021-11-12 04:14:28 +01:00
|
|
|
import io.heckel.ntfy.data.topicUrl
|
2021-11-07 19:13:32 +01:00
|
|
|
import io.heckel.ntfy.msg.ApiService
|
2021-11-15 22:24:31 +01:00
|
|
|
import io.heckel.ntfy.msg.NotificationService
|
2021-11-07 19:13:32 +01:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
2021-11-15 22:24:31 +01:00
|
|
|
import kotlinx.coroutines.delay
|
2021-11-07 19:13:32 +01:00
|
|
|
import kotlinx.coroutines.launch
|
2021-11-01 14:57:05 +01:00
|
|
|
import java.util.*
|
2021-11-15 22:24:31 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicLong
|
2021-11-14 01:26:37 +01:00
|
|
|
|
2021-11-03 18:56:08 +01:00
|
|
|
class DetailActivity : AppCompatActivity(), ActionMode.Callback {
|
2021-10-31 20:19:25 +01:00
|
|
|
private val viewModel by viewModels<DetailViewModel> {
|
|
|
|
DetailViewModelFactory((application as Application).repository)
|
|
|
|
}
|
2021-11-07 19:13:32 +01:00
|
|
|
private val repository by lazy { (application as Application).repository }
|
2021-11-12 01:41:29 +01:00
|
|
|
private val api = ApiService()
|
2021-11-14 19:54:48 +01:00
|
|
|
private var subscriberManager: SubscriberManager? = null // Context-dependent
|
2021-11-15 22:24:31 +01:00
|
|
|
private var notifier: NotificationService? = null // Context-dependent
|
2021-11-03 18:56:08 +01:00
|
|
|
|
|
|
|
// Which subscription are we looking at
|
2021-10-31 20:19:25 +01:00
|
|
|
private var subscriptionId: Long = 0L // Set in onCreate()
|
2021-11-01 14:57:05 +01:00
|
|
|
private var subscriptionBaseUrl: String = "" // Set in onCreate()
|
2021-10-31 20:19:25 +01:00
|
|
|
private var subscriptionTopic: String = "" // Set in onCreate()
|
2021-11-14 19:54:48 +01:00
|
|
|
private var subscriptionInstant: Boolean = false // Set in onCreate() & updated by options menu!
|
2021-10-31 20:19:25 +01:00
|
|
|
|
2021-11-14 19:54:48 +01:00
|
|
|
// UI elements
|
2021-11-03 18:56:08 +01:00
|
|
|
private lateinit var adapter: DetailAdapter
|
2021-11-14 19:54:48 +01:00
|
|
|
private lateinit var mainList: RecyclerView
|
2021-11-14 22:48:50 +01:00
|
|
|
private lateinit var mainListContainer: SwipeRefreshLayout
|
2021-11-14 19:54:48 +01:00
|
|
|
private lateinit var menu: Menu
|
|
|
|
|
|
|
|
// Action mode stuff
|
2021-11-03 18:56:08 +01:00
|
|
|
private var actionMode: ActionMode? = null
|
|
|
|
|
2021-10-31 20:19:25 +01:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.detail_activity)
|
|
|
|
|
2021-11-12 01:41:29 +01:00
|
|
|
Log.d(MainActivity.TAG, "Create $this")
|
|
|
|
|
2021-11-14 19:54:48 +01:00
|
|
|
// Dependencies that depend on Context
|
|
|
|
subscriberManager = SubscriberManager(this)
|
2021-11-15 22:24:31 +01:00
|
|
|
notifier = NotificationService(this)
|
2021-11-14 19:54:48 +01:00
|
|
|
|
2021-11-12 01:41:29 +01:00
|
|
|
// Show 'Back' button
|
|
|
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
2021-11-07 19:13:32 +01:00
|
|
|
|
2021-10-31 20:19:25 +01:00
|
|
|
// Get extras required for the return to the main activity
|
|
|
|
subscriptionId = intent.getLongExtra(MainActivity.EXTRA_SUBSCRIPTION_ID, 0)
|
2021-11-01 14:57:05 +01:00
|
|
|
subscriptionBaseUrl = intent.getStringExtra(MainActivity.EXTRA_SUBSCRIPTION_BASE_URL) ?: return
|
2021-10-31 20:19:25 +01:00
|
|
|
subscriptionTopic = intent.getStringExtra(MainActivity.EXTRA_SUBSCRIPTION_TOPIC) ?: return
|
2021-11-14 01:26:37 +01:00
|
|
|
subscriptionInstant = intent.getBooleanExtra(MainActivity.EXTRA_SUBSCRIPTION_INSTANT, false)
|
2021-10-31 20:19:25 +01:00
|
|
|
|
|
|
|
// Set title
|
|
|
|
val subscriptionBaseUrl = intent.getStringExtra(MainActivity.EXTRA_SUBSCRIPTION_BASE_URL) ?: return
|
2021-11-01 17:12:36 +01:00
|
|
|
val topicUrl = topicShortUrl(subscriptionBaseUrl, subscriptionTopic)
|
|
|
|
title = topicUrl
|
|
|
|
|
|
|
|
// 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-10-31 20:19:25 +01:00
|
|
|
|
2021-11-14 22:48:50 +01:00
|
|
|
// Swipe to refresh
|
|
|
|
mainListContainer = findViewById(R.id.detail_notification_list_container)
|
|
|
|
mainListContainer.setOnRefreshListener { refresh() }
|
|
|
|
mainListContainer.setColorSchemeResources(R.color.primaryColor)
|
|
|
|
|
2021-10-31 20:19:25 +01:00
|
|
|
// Update main list based on viewModel (& its datasource/livedata)
|
2021-11-01 16:12:09 +01:00
|
|
|
val noEntriesText: View = findViewById(R.id.detail_no_notifications)
|
2021-11-03 18:56:08 +01:00
|
|
|
val onNotificationClick = { n: Notification -> onNotificationClick(n) }
|
|
|
|
val onNotificationLongClick = { n: Notification -> onNotificationLongClick(n) }
|
|
|
|
|
|
|
|
adapter = DetailAdapter(onNotificationClick, onNotificationLongClick)
|
|
|
|
mainList = findViewById(R.id.detail_notification_list)
|
2021-10-31 20:19:25 +01:00
|
|
|
mainList.adapter = adapter
|
|
|
|
|
|
|
|
viewModel.list(subscriptionId).observe(this) {
|
|
|
|
it?.let {
|
2021-11-15 22:24:31 +01:00
|
|
|
// Show list view
|
2021-10-31 20:19:25 +01:00
|
|
|
adapter.submitList(it as MutableList<Notification>)
|
|
|
|
if (it.isEmpty()) {
|
2021-11-14 22:48:50 +01:00
|
|
|
mainListContainer.visibility = View.GONE
|
2021-10-31 20:19:25 +01:00
|
|
|
noEntriesText.visibility = View.VISIBLE
|
|
|
|
} else {
|
2021-11-14 22:48:50 +01:00
|
|
|
mainListContainer.visibility = View.VISIBLE
|
2021-10-31 20:19:25 +01:00
|
|
|
noEntriesText.visibility = View.GONE
|
|
|
|
}
|
2021-11-15 22:24:31 +01:00
|
|
|
|
|
|
|
// Cancel notifications that still have popups
|
|
|
|
maybeCancelNotificationPopups(it)
|
2021-10-31 20:19:25 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-14 19:54:48 +01:00
|
|
|
|
2021-11-15 22:24:31 +01:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-11-14 19:54:48 +01:00
|
|
|
// React to changes in fast delivery setting
|
|
|
|
repository.getSubscriptionIdsWithInstantStatusLiveData().observe(this) {
|
|
|
|
subscriberManager?.refreshService(it)
|
|
|
|
}
|
2021-11-15 22:24:31 +01:00
|
|
|
|
|
|
|
// Mark this subscription as "open" so we don't receive notifications for it
|
|
|
|
Log.d(TAG, "onCreate hook: Marking subscription $subscriptionId as 'open'")
|
|
|
|
repository.detailViewSubscriptionId.set(subscriptionId)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
|
|
|
|
Log.d(TAG, "onResume hook: Marking subscription $subscriptionId as 'open'")
|
|
|
|
repository.detailViewSubscriptionId.set(subscriptionId) // Mark as "open" so we don't send notifications while this is open
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
|
|
|
Log.d(TAG, "onResume hook: Marking subscription $subscriptionId as 'not open'")
|
|
|
|
repository.detailViewSubscriptionId.set(0) // Mark as closed
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
repository.detailViewSubscriptionId.set(0) // Mark as closed
|
|
|
|
Log.d(TAG, "onDestroy hook: Marking subscription $subscriptionId as 'not open'")
|
|
|
|
super.onDestroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
repository.updateNotification(notification.copy(notificationId = 0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-31 20:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
|
|
|
menuInflater.inflate(R.menu.detail_action_bar_menu, menu)
|
2021-11-14 19:54:48 +01:00
|
|
|
this.menu = menu
|
|
|
|
showHideInstantMenuItems(subscriptionInstant)
|
2021-10-31 20:19:25 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
return when (item.itemId) {
|
2021-11-01 14:57:05 +01:00
|
|
|
R.id.detail_menu_test -> {
|
|
|
|
onTestClick()
|
|
|
|
true
|
|
|
|
}
|
2021-11-14 19:54:48 +01:00
|
|
|
R.id.detail_menu_enable_instant -> {
|
|
|
|
onInstantEnableClick(enable = true)
|
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.detail_menu_disable_instant -> {
|
|
|
|
onInstantEnableClick(enable = false)
|
|
|
|
true
|
|
|
|
}
|
2021-11-15 13:57:35 +01:00
|
|
|
R.id.detail_menu_instant_info -> {
|
|
|
|
onInstantInfoClick()
|
|
|
|
true
|
|
|
|
}
|
2021-11-12 04:14:28 +01:00
|
|
|
R.id.detail_menu_copy_url -> {
|
|
|
|
onCopyUrlClick()
|
|
|
|
true
|
|
|
|
}
|
2021-11-03 17:48:13 +01:00
|
|
|
R.id.detail_menu_unsubscribe -> {
|
2021-10-31 20:19:25 +01:00
|
|
|
onDeleteClick()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
else -> super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 19:29:19 +01:00
|
|
|
private fun onTestClick() {
|
|
|
|
Log.d(TAG, "Sending test notification to ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")
|
|
|
|
|
2021-11-12 01:41:29 +01:00
|
|
|
lifecycleScope.launch(Dispatchers.IO) {
|
|
|
|
try {
|
|
|
|
val message = getString(R.string.detail_test_message, Date().toString())
|
|
|
|
api.publish(subscriptionBaseUrl, subscriptionTopic, message)
|
|
|
|
} catch (e: Exception) {
|
2021-11-14 01:26:37 +01:00
|
|
|
runOnUiThread {
|
|
|
|
Toast
|
|
|
|
.makeText(this@DetailActivity, getString(R.string.detail_test_message_error, e.message), Toast.LENGTH_LONG)
|
|
|
|
.show()
|
|
|
|
}
|
2021-11-12 01:41:29 +01:00
|
|
|
}
|
2021-11-07 19:29:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 04:14:28 +01:00
|
|
|
private fun onCopyUrlClick() {
|
|
|
|
val url = topicUrl(subscriptionBaseUrl, subscriptionTopic)
|
|
|
|
Log.d(TAG, "Copying topic URL $url to clipboard ")
|
|
|
|
|
2021-11-17 21:30:57 +01:00
|
|
|
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-12 04:14:28 +01:00
|
|
|
}
|
|
|
|
|
2021-11-14 22:48:50 +01:00
|
|
|
private fun refresh() {
|
2021-11-07 19:29:19 +01:00
|
|
|
Log.d(TAG, "Fetching cached notifications for ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")
|
|
|
|
|
2021-11-12 01:41:29 +01:00
|
|
|
lifecycleScope.launch(Dispatchers.IO) {
|
|
|
|
try {
|
|
|
|
val notifications = api.poll(subscriptionId, subscriptionBaseUrl, subscriptionTopic)
|
|
|
|
val newNotifications = repository.onlyNewNotifications(subscriptionId, notifications)
|
2021-11-07 19:13:32 +01:00
|
|
|
val toastMessage = if (newNotifications.isEmpty()) {
|
2021-11-12 04:14:28 +01:00
|
|
|
getString(R.string.refresh_message_no_results)
|
2021-11-07 19:13:32 +01:00
|
|
|
} else {
|
2021-11-12 04:14:28 +01:00
|
|
|
getString(R.string.refresh_message_result, newNotifications.size)
|
2021-11-07 19:13:32 +01:00
|
|
|
}
|
2021-11-12 04:14:28 +01:00
|
|
|
newNotifications.forEach { notification -> repository.addNotification(notification) }
|
2021-11-14 22:48:50 +01:00
|
|
|
runOnUiThread {
|
|
|
|
Toast.makeText(this@DetailActivity, toastMessage, Toast.LENGTH_LONG).show()
|
|
|
|
mainListContainer.isRefreshing = false
|
|
|
|
}
|
2021-11-12 01:41:29 +01:00
|
|
|
} catch (e: Exception) {
|
2021-11-18 20:59:54 +01:00
|
|
|
Log.e(TAG, "Error fetching notifications for ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}: ${e.stackTrace}", e)
|
2021-11-14 01:26:37 +01:00
|
|
|
runOnUiThread {
|
|
|
|
Toast
|
|
|
|
.makeText(this@DetailActivity, getString(R.string.refresh_message_error, e.message), Toast.LENGTH_LONG)
|
|
|
|
.show()
|
2021-11-14 22:48:50 +01:00
|
|
|
mainListContainer.isRefreshing = false
|
2021-11-14 01:26:37 +01:00
|
|
|
}
|
2021-11-01 14:57:05 +01:00
|
|
|
}
|
2021-11-07 19:13:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-14 19:54:48 +01: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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 13:57:35 +01:00
|
|
|
private fun onInstantInfoClick() {
|
|
|
|
Log.d(TAG, "Showing instant info toast")
|
|
|
|
Toast.makeText(this@DetailActivity, getString(R.string.detail_instant_info), Toast.LENGTH_LONG)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
2021-11-14 19:54:48 +01:00
|
|
|
private fun showHideInstantMenuItems(enable: Boolean) {
|
|
|
|
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-15 13:57:35 +01:00
|
|
|
val instantInfoItem = menu.findItem(R.id.detail_menu_instant_info)
|
2021-11-14 19:54:48 +01:00
|
|
|
if (subscriptionBaseUrl == appBaseUrl) {
|
|
|
|
enableInstantItem?.isVisible = !subscriptionInstant
|
|
|
|
disableInstantItem?.isVisible = subscriptionInstant
|
2021-11-15 13:57:35 +01:00
|
|
|
instantInfoItem?.isVisible = false
|
2021-11-14 19:54:48 +01:00
|
|
|
} else {
|
|
|
|
enableInstantItem?.isVisible = false
|
|
|
|
disableInstantItem?.isVisible = false
|
2021-11-15 13:57:35 +01:00
|
|
|
instantInfoItem?.isVisible = true
|
2021-11-14 19:54:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 20:19:25 +01:00
|
|
|
private fun onDeleteClick() {
|
2021-11-01 14:57:05 +01:00
|
|
|
Log.d(TAG, "Deleting subscription ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")
|
|
|
|
|
2021-10-31 20:19:25 +01:00
|
|
|
val builder = AlertDialog.Builder(this)
|
|
|
|
builder
|
|
|
|
.setMessage(R.string.detail_delete_dialog_message)
|
|
|
|
.setPositiveButton(R.string.detail_delete_dialog_permanently_delete) { _, _ ->
|
|
|
|
// Return to main activity
|
|
|
|
val result = Intent()
|
|
|
|
.putExtra(MainActivity.EXTRA_SUBSCRIPTION_ID, subscriptionId)
|
2021-11-14 01:26:37 +01:00
|
|
|
.putExtra(MainActivity.EXTRA_SUBSCRIPTION_BASE_URL, subscriptionBaseUrl)
|
2021-10-31 20:19:25 +01:00
|
|
|
.putExtra(MainActivity.EXTRA_SUBSCRIPTION_TOPIC, subscriptionTopic)
|
2021-11-14 01:26:37 +01:00
|
|
|
.putExtra(MainActivity.EXTRA_SUBSCRIPTION_INSTANT, subscriptionInstant)
|
2021-10-31 20:19:25 +01:00
|
|
|
setResult(RESULT_OK, result)
|
|
|
|
finish()
|
|
|
|
|
2021-11-12 01:41:29 +01:00
|
|
|
// The deletion will be done in MainActivity.onResult
|
2021-10-31 20:19:25 +01:00
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.detail_delete_dialog_cancel) { _, _ -> /* Do nothing */ }
|
|
|
|
.create()
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onNotificationClick(notification: Notification) {
|
2021-11-03 18:56:08 +01:00
|
|
|
if (actionMode != null) {
|
|
|
|
handleActionModeClick(notification)
|
2021-11-08 03:02:27 +01:00
|
|
|
} else {
|
|
|
|
copyToClipboard(notification)
|
2021-11-03 18:56:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 03:02:27 +01:00
|
|
|
private fun copyToClipboard(notification: Notification) {
|
2021-11-17 21:30:57 +01:00
|
|
|
runOnUiThread {
|
|
|
|
val message = notification.message + "\n\n" + Date(notification.timestamp * 1000).toString()
|
|
|
|
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
|
|
|
val clip = ClipData.newPlainText("notification message", message)
|
|
|
|
clipboard.setPrimaryClip(clip)
|
|
|
|
Toast
|
|
|
|
.makeText(this, getString(R.string.detail_copied_to_clipboard_message), Toast.LENGTH_LONG)
|
|
|
|
.show()
|
|
|
|
}
|
2021-11-08 03:02:27 +01:00
|
|
|
}
|
|
|
|
|
2021-11-03 18:56:08 +01: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()
|
|
|
|
redrawList()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
|
|
|
|
this.actionMode = mode
|
|
|
|
if (mode != null) {
|
|
|
|
mode.menuInflater.inflate(R.menu.detail_action_mode_menu, menu)
|
|
|
|
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 02:22:02 +01:00
|
|
|
R.id.detail_action_mode_copy -> {
|
|
|
|
onMultiCopyClick()
|
|
|
|
true
|
|
|
|
}
|
2021-11-03 18:56:08 +01:00
|
|
|
R.id.detail_action_mode_delete -> {
|
|
|
|
onMultiDeleteClick()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
else -> false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 02:22:02 +01: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 {
|
|
|
|
it.message + "\n" + Date(it.timestamp * 1000).toString()
|
|
|
|
}.orEmpty()
|
|
|
|
}
|
|
|
|
runOnUiThread {
|
2021-11-17 21:30:57 +01:00
|
|
|
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
|
|
|
val clip = ClipData.newPlainText("notifications", content)
|
|
|
|
clipboard.setPrimaryClip(clip)
|
2021-11-15 02:22:02 +01:00
|
|
|
Toast
|
|
|
|
.makeText(this@DetailActivity, getString(R.string.detail_copied_to_clipboard_message), Toast.LENGTH_LONG)
|
|
|
|
.show()
|
2021-11-15 13:57:35 +01:00
|
|
|
finishActionMode()
|
2021-11-15 02:22:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:56:08 +01:00
|
|
|
private fun onMultiDeleteClick() {
|
|
|
|
Log.d(TAG, "Showing multi-delete dialog for selected items")
|
|
|
|
|
|
|
|
val builder = AlertDialog.Builder(this)
|
|
|
|
builder
|
|
|
|
.setMessage(R.string.detail_action_mode_delete_dialog_message)
|
|
|
|
.setPositiveButton(R.string.detail_action_mode_delete_dialog_permanently_delete) { _, _ ->
|
2021-11-12 04:14:28 +01:00
|
|
|
adapter.selected.map { notificationId -> viewModel.remove(notificationId) }
|
2021-11-03 18:56:08 +01:00
|
|
|
finishActionMode()
|
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.detail_action_mode_delete_dialog_cancel) { _, _ ->
|
|
|
|
finishActionMode()
|
|
|
|
}
|
|
|
|
.create()
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroyActionMode(mode: ActionMode?) {
|
|
|
|
endActionModeAndRedraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun beginActionMode(notification: Notification) {
|
|
|
|
actionMode = startActionMode(this)
|
|
|
|
adapter.selected.add(notification.id)
|
|
|
|
redrawList()
|
|
|
|
|
|
|
|
// Fade status bar color
|
|
|
|
val fromColor = ContextCompat.getColor(this, R.color.primaryColor)
|
|
|
|
val toColor = ContextCompat.getColor(this, R.color.primaryDarkColor)
|
|
|
|
fadeStatusBarColor(window, fromColor, toColor)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun finishActionMode() {
|
|
|
|
actionMode!!.finish()
|
|
|
|
endActionModeAndRedraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun endActionModeAndRedraw() {
|
|
|
|
actionMode = null
|
|
|
|
adapter.selected.clear()
|
|
|
|
redrawList()
|
|
|
|
|
|
|
|
// Fade status bar color
|
|
|
|
val fromColor = ContextCompat.getColor(this, R.color.primaryDarkColor)
|
|
|
|
val toColor = ContextCompat.getColor(this, R.color.primaryColor)
|
|
|
|
fadeStatusBarColor(window, fromColor, toColor)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun redrawList() {
|
|
|
|
mainList.adapter = adapter // Oh, what a hack ...
|
2021-11-01 14:57:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val TAG = "NtfyDetailActivity"
|
2021-11-15 22:24:31 +01:00
|
|
|
const val CANCEL_NOTIFICATION_DELAY_MILLIS = 20_000L
|
2021-10-31 20:19:25 +01:00
|
|
|
}
|
|
|
|
}
|