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

483 lines
19 KiB
Kotlin
Raw Normal View History

2021-10-28 16:04:14 +13:00
package io.heckel.ntfy.ui
2021-11-04 05:48:13 +13:00
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.app.AlertDialog
import android.content.Intent
2021-10-28 16:04:14 +13:00
import android.net.Uri
import android.os.Bundle
2021-11-02 02:57:05 +13:00
import android.util.Log
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.view.View
2021-11-08 07:29:19 +13:00
import android.widget.Toast
import androidx.activity.viewModels
2021-10-26 07:24:44 +13:00
import androidx.appcompat.app.AppCompatActivity
2021-11-04 05:48:13 +13:00
import androidx.core.content.ContextCompat
2021-11-08 07:29:19 +13:00
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
2021-11-15 08:20:30 +13:00
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import androidx.work.*
2021-10-31 13:16:12 +13:00
import io.heckel.ntfy.R
2021-10-30 14:13:58 +13:00
import io.heckel.ntfy.app.Application
2021-10-31 13:16:12 +13:00
import io.heckel.ntfy.data.Subscription
2021-11-28 10:18:09 +13:00
import io.heckel.ntfy.util.topicShortUrl
2021-11-08 07:29:19 +13:00
import io.heckel.ntfy.msg.ApiService
import io.heckel.ntfy.msg.NotificationService
import io.heckel.ntfy.work.PollWorker
2021-11-25 10:12:51 +13:00
import io.heckel.ntfy.firebase.FirebaseMessenger
2021-11-28 10:18:09 +13:00
import io.heckel.ntfy.util.fadeStatusBarColor
import io.heckel.ntfy.util.formatDateShort
2021-11-08 07:29:19 +13:00
import kotlinx.coroutines.Dispatchers
2021-11-23 09:45:43 +13:00
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
2021-11-08 07:29:19 +13:00
import kotlinx.coroutines.launch
2021-11-01 08:19:25 +13:00
import java.util.*
import java.util.concurrent.TimeUnit
2021-10-31 13:16:12 +13:00
import kotlin.random.Random
2021-11-23 09:45:43 +13:00
class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.SubscribeListener, NotificationFragment.NotificationSettingsListener {
2021-11-01 08:19:25 +13:00
private val viewModel by viewModels<SubscriptionsViewModel> {
2021-10-30 14:13:58 +13:00
SubscriptionsViewModelFactory((application as Application).repository)
}
2021-11-08 07:29:19 +13:00
private val repository by lazy { (application as Application).repository }
private val api = ApiService()
2021-11-25 10:12:51 +13:00
private val messenger = FirebaseMessenger()
2021-11-23 09:45:43 +13:00
// UI elements
private lateinit var menu: Menu
2021-11-04 05:48:13 +13:00
private lateinit var mainList: RecyclerView
2021-11-15 08:20:30 +13:00
private lateinit var mainListContainer: SwipeRefreshLayout
2021-11-04 05:48:13 +13:00
private lateinit var adapter: MainAdapter
private lateinit var fab: View
2021-11-23 09:45:43 +13:00
// Other stuff
2021-11-04 05:48:13 +13:00
private var actionMode: ActionMode? = null
private var workManager: WorkManager? = null // Context-dependent
private var notifier: NotificationService? = null // Context-dependent
private var subscriberManager: SubscriberManager? = null // Context-dependent
private var appBaseUrl: String? = null // Context-dependent
2021-10-30 14:13:58 +13:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2021-11-23 09:45:43 +13:00
setContentView(R.layout.activity_main)
Log.d(TAG, "Create $this")
2021-11-08 07:29:19 +13:00
// Dependencies that depend on Context
workManager = WorkManager.getInstance(this)
notifier = NotificationService(this)
subscriberManager = SubscriberManager(this)
appBaseUrl = getString(R.string.app_base_url)
2021-11-08 07:29:19 +13:00
2021-10-29 04:45:34 +13:00
// Action bar
title = getString(R.string.main_action_bar_title)
// Floating action button ("+")
2021-11-04 05:48:13 +13:00
fab = findViewById(R.id.fab)
fab.setOnClickListener {
2021-10-30 14:13:58 +13:00
onSubscribeButtonClick()
}
2021-11-15 10:48:50 +13:00
// Swipe to refresh
mainListContainer = findViewById(R.id.main_subscriptions_list_container)
2021-11-15 08:20:30 +13:00
mainListContainer.setOnRefreshListener { refreshAllSubscriptions() }
mainListContainer.setColorSchemeResources(R.color.primaryColor)
2021-11-01 08:19:25 +13:00
// Update main list based on viewModel (& its datasource/livedata)
2021-11-02 03:49:52 +13:00
val noEntries: View = findViewById(R.id.main_no_subscriptions)
2021-11-04 05:48:13 +13:00
val onSubscriptionClick = { s: Subscription -> onSubscriptionItemClick(s) }
val onSubscriptionLongClick = { s: Subscription -> onSubscriptionItemLongClick(s) }
mainList = findViewById(R.id.main_subscriptions_list)
adapter = MainAdapter(onSubscriptionClick, onSubscriptionLongClick)
2021-10-29 04:45:34 +13:00
mainList.adapter = adapter
2021-11-01 08:19:25 +13:00
viewModel.list().observe(this) {
it?.let { subscriptions ->
adapter.submitList(subscriptions as MutableList<Subscription>)
2021-10-29 04:45:34 +13:00
if (it.isEmpty()) {
2021-11-15 08:20:30 +13:00
mainListContainer.visibility = View.GONE
2021-11-02 03:49:52 +13:00
noEntries.visibility = View.VISIBLE
2021-10-29 04:45:34 +13:00
} else {
2021-11-15 08:20:30 +13:00
mainListContainer.visibility = View.VISIBLE
2021-11-02 03:49:52 +13:00
noEntries.visibility = View.GONE
2021-10-29 04:45:34 +13:00
}
}
2021-10-26 06:45:56 +13:00
}
// React to changes in fast delivery setting
viewModel.listIdsWithInstantStatus().observe(this) {
subscriberManager?.refreshService(it)
2021-11-14 13:26:37 +13:00
}
2021-11-30 08:06:08 +13:00
// Create notification channels right away, so we can configure them immediately after installing the app
notifier!!.createNotificationChannels()
2021-11-14 13:26:37 +13:00
// Background things
startPeriodicWorker()
}
private fun startPeriodicWorker() {
2021-11-23 09:45:43 +13:00
val pollWorkerVersion = repository.getPollWorkerVersion()
val workPolicy = if (pollWorkerVersion == PollWorker.VERSION) {
Log.d(TAG, "Poll worker version matches: choosing KEEP as existing work policy")
ExistingPeriodicWorkPolicy.KEEP
} else {
Log.d(TAG, "Poll worker version DOES NOT MATCH: choosing REPLACE as existing work policy")
2021-11-23 09:45:43 +13:00
repository.setPollWorkerVersion(PollWorker.VERSION)
ExistingPeriodicWorkPolicy.REPLACE
}
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val work = PeriodicWorkRequestBuilder<PollWorker>(15, TimeUnit.MINUTES)
.setConstraints(constraints)
.addTag(PollWorker.TAG)
.addTag(PollWorker.WORK_NAME_PERIODIC)
.build()
workManager!!.enqueueUniquePeriodicWork(PollWorker.WORK_NAME_PERIODIC, workPolicy, work)
2021-10-26 14:14:09 +13:00
}
2021-10-26 13:25:54 +13:00
2021-10-28 16:04:14 +13:00
override fun onCreateOptionsMenu(menu: Menu): Boolean {
2021-11-23 09:45:43 +13:00
menuInflater.inflate(R.menu.menu_main_action_bar, menu)
this.menu = menu
showHideNotificationMenuItems()
startNotificationMutedChecker() // This is done here, because then we know that we've initialized the menu
2021-10-28 16:04:14 +13:00
return true
}
2021-11-23 09:45:43 +13:00
private fun startNotificationMutedChecker() {
lifecycleScope.launch(Dispatchers.IO) {
delay(1000) // Just to be sure we've initialized all the things, we wait a bit ...
while (isActive) {
Log.d(DetailActivity.TAG, "Checking global and subscription-specific 'muted until' timestamp")
// Check global
val changed = repository.checkGlobalMutedUntil()
if (changed) {
Log.d(TAG, "Global muted until timestamp expired; updating prefs")
showHideNotificationMenuItems()
}
// Check subscriptions
var rerenderList = false
repository.getSubscriptions().forEach { subscription ->
val mutedUntilExpired = subscription.mutedUntil > 1L && System.currentTimeMillis()/1000 > subscription.mutedUntil
if (mutedUntilExpired) {
Log.d(TAG, "Subscription ${subscription.id}: Muted until timestamp expired, updating subscription")
val newSubscription = subscription.copy(mutedUntil = 0L)
repository.updateSubscription(newSubscription)
rerenderList = true
}
}
if (rerenderList) {
redrawList()
}
delay(60_000)
}
}
}
private fun showHideNotificationMenuItems() {
val mutedUntilSeconds = repository.getGlobalMutedUntil()
runOnUiThread {
val notificationsEnabledItem = menu.findItem(R.id.main_menu_notifications_enabled)
val notificationsDisabledUntilItem = menu.findItem(R.id.main_menu_notifications_disabled_until)
val notificationsDisabledForeverItem = menu.findItem(R.id.main_menu_notifications_disabled_forever)
notificationsEnabledItem?.isVisible = mutedUntilSeconds == 0L
notificationsDisabledForeverItem?.isVisible = mutedUntilSeconds == 1L
notificationsDisabledUntilItem?.isVisible = mutedUntilSeconds > 1L
if (mutedUntilSeconds > 1L) {
val formattedDate = formatDateShort(mutedUntilSeconds)
notificationsDisabledUntilItem?.title = getString(R.string.main_menu_notifications_disabled_until, formattedDate)
}
}
}
2021-10-28 16:04:14 +13:00
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
2021-11-23 09:45:43 +13:00
R.id.main_menu_notifications_enabled -> {
onNotificationSettingsClick(enable = false)
true
}
R.id.main_menu_notifications_disabled_forever -> {
onNotificationSettingsClick(enable = true)
true
}
R.id.main_menu_notifications_disabled_until -> {
onNotificationSettingsClick(enable = true)
true
}
2021-11-02 02:57:05 +13:00
R.id.main_menu_source -> {
2021-10-29 01:28:22 +13:00
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.main_menu_source_url))))
2021-10-28 16:04:14 +13:00
true
}
2021-11-02 02:57:05 +13:00
R.id.main_menu_website -> {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(appBaseUrl)))
2021-10-28 16:04:14 +13:00
true
}
else -> super.onOptionsItemSelected(item)
}
}
2021-11-23 09:45:43 +13:00
private fun onNotificationSettingsClick(enable: Boolean) {
if (!enable) {
Log.d(TAG, "Showing global notification settings dialog")
val notificationFragment = NotificationFragment()
notificationFragment.show(supportFragmentManager, NotificationFragment.TAG)
} else {
Log.d(TAG, "Re-enabling global notifications")
onNotificationMutedUntilChanged(0L)
}
}
override fun onNotificationMutedUntilChanged(mutedUntilTimestamp: Long) {
repository.setGlobalMutedUntil(mutedUntilTimestamp)
showHideNotificationMenuItems()
runOnUiThread {
when (mutedUntilTimestamp) {
0L -> Toast.makeText(this@MainActivity, getString(R.string.notification_dialog_enabled_toast_message), Toast.LENGTH_LONG).show()
1L -> Toast.makeText(this@MainActivity, getString(R.string.notification_dialog_muted_forever_toast_message), Toast.LENGTH_LONG).show()
else -> {
val formattedDate = formatDateShort(mutedUntilTimestamp)
Toast.makeText(this@MainActivity, getString(R.string.notification_dialog_muted_until_toast_message, formattedDate), Toast.LENGTH_LONG).show()
}
}
}
}
2021-10-30 14:13:58 +13:00
private fun onSubscribeButtonClick() {
val newFragment = AddFragment()
newFragment.show(supportFragmentManager, AddFragment.TAG)
}
override fun onSubscribe(topic: String, baseUrl: String, instant: Boolean) {
2021-11-02 02:57:05 +13:00
Log.d(TAG, "Adding subscription ${topicShortUrl(baseUrl, topic)}")
2021-11-08 07:29:19 +13:00
// Add subscription to database
2021-11-04 05:48:13 +13:00
val subscription = Subscription(
id = Random.nextLong(),
baseUrl = baseUrl,
topic = topic,
2021-11-14 13:26:37 +13:00
instant = instant,
2021-11-22 08:54:13 +13:00
mutedUntil = 0,
totalCount = 0,
newCount = 0,
2021-11-04 05:48:13 +13:00
lastActive = Date().time/1000
)
2021-11-01 08:19:25 +13:00
viewModel.add(subscription)
2021-11-08 07:29:19 +13:00
// Subscribe to Firebase topic if ntfy.sh (even if instant, just to be sure!)
if (baseUrl == appBaseUrl) {
2021-11-14 13:26:37 +13:00
Log.d(TAG, "Subscribing to Firebase")
2021-11-25 10:12:51 +13:00
messenger.subscribe(topic)
2021-11-14 13:26:37 +13:00
}
2021-11-06 13:25:02 +13:00
2021-11-08 07:29:19 +13:00
// Fetch cached messages
lifecycleScope.launch(Dispatchers.IO) {
try {
val notifications = api.poll(subscription.id, subscription.baseUrl, subscription.topic)
notifications.forEach { notification -> repository.addNotification(notification) }
} catch (e: Exception) {
Log.e(TAG, "Unable to fetch notifications: ${e.stackTrace}")
2021-11-08 07:29:19 +13:00
}
}
2021-11-03 14:43:31 +13:00
2021-11-08 07:29:19 +13:00
// Switch to detail view after adding it
onSubscriptionItemClick(subscription)
}
2021-10-26 13:25:54 +13:00
2021-11-01 08:19:25 +13:00
private fun onSubscriptionItemClick(subscription: Subscription) {
2021-11-04 05:48:13 +13:00
if (actionMode != null) {
handleActionModeClick(subscription)
} else {
startDetailView(subscription)
}
}
private fun onSubscriptionItemLongClick(subscription: Subscription) {
if (actionMode == null) {
beginActionMode(subscription)
}
}
private fun refreshAllSubscriptions() {
2021-11-11 15:16:00 +13:00
lifecycleScope.launch(Dispatchers.IO) {
try {
Log.d(TAG, "Polling for new notifications")
var newNotificationsCount = 0
repository.getSubscriptions().forEach { subscription ->
val notifications = api.poll(subscription.id, subscription.baseUrl, subscription.topic)
val newNotifications = repository.onlyNewNotifications(subscription.id, notifications)
newNotifications.forEach { notification ->
newNotificationsCount++
2021-11-23 09:45:43 +13:00
val notificationWithId = notification.copy(notificationId = Random.nextInt())
val shouldNotify = repository.addNotification(notificationWithId)
if (shouldNotify) {
notifier?.send(subscription, notificationWithId)
}
2021-11-11 15:16:00 +13:00
}
}
val toastMessage = if (newNotificationsCount == 0) {
getString(R.string.refresh_message_no_results)
} else {
getString(R.string.refresh_message_result, newNotificationsCount)
}
2021-11-15 08:20:30 +13:00
runOnUiThread {
Toast.makeText(this@MainActivity, toastMessage, Toast.LENGTH_LONG).show()
mainListContainer.isRefreshing = false
}
Log.d(TAG, "Finished polling for new notifications")
} catch (e: Exception) {
Log.e(TAG, "Polling failed: ${e.message}", e)
runOnUiThread {
Toast.makeText(this@MainActivity, getString(R.string.refresh_message_error, e.message), Toast.LENGTH_LONG).show()
2021-11-15 08:20:30 +13:00
mainListContainer.isRefreshing = false
2021-11-11 15:16:00 +13:00
}
}
}
}
2021-11-04 05:48:13 +13:00
private fun startDetailView(subscription: Subscription) {
2021-11-02 02:57:05 +13:00
Log.d(TAG, "Entering detail view for subscription $subscription")
2021-11-01 08:19:25 +13:00
val intent = Intent(this, DetailActivity::class.java)
intent.putExtra(EXTRA_SUBSCRIPTION_ID, subscription.id)
intent.putExtra(EXTRA_SUBSCRIPTION_BASE_URL, subscription.baseUrl)
intent.putExtra(EXTRA_SUBSCRIPTION_TOPIC, subscription.topic)
2021-11-14 13:26:37 +13:00
intent.putExtra(EXTRA_SUBSCRIPTION_INSTANT, subscription.instant)
2021-11-23 09:45:43 +13:00
intent.putExtra(EXTRA_SUBSCRIPTION_MUTED_UNTIL, subscription.mutedUntil)
startActivity(intent)
2021-10-27 05:23:41 +13:00
}
2021-10-27 06:46:49 +13:00
2021-11-04 05:48:13 +13:00
private fun handleActionModeClick(subscription: Subscription) {
adapter.toggleSelection(subscription.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) {
2021-11-23 09:45:43 +13:00
mode.menuInflater.inflate(R.menu.menu_main_action_mode, menu)
2021-11-04 05:48:13 +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-04 06:56:08 +13:00
R.id.main_action_mode_delete -> {
2021-11-04 05:48:13 +13:00
onMultiDeleteClick()
true
}
else -> false
}
}
private fun onMultiDeleteClick() {
Log.d(DetailActivity.TAG, "Showing multi-delete dialog for selected items")
val builder = AlertDialog.Builder(this)
val dialog = builder
2021-11-04 05:48:13 +13:00
.setMessage(R.string.main_action_mode_delete_dialog_message)
.setPositiveButton(R.string.main_action_mode_delete_dialog_permanently_delete) { _, _ ->
adapter.selected.map { viewModel.remove(it) }
finishActionMode()
}
.setNegativeButton(R.string.main_action_mode_delete_dialog_cancel) { _, _ ->
finishActionMode()
}
.create()
dialog.setOnShowListener {
dialog
.getButton(AlertDialog.BUTTON_POSITIVE)
.setTextColor(ContextCompat.getColor(this, R.color.primaryDangerButtonColor))
}
dialog.show()
2021-11-04 05:48:13 +13:00
}
override fun onDestroyActionMode(mode: ActionMode?) {
endActionModeAndRedraw()
}
private fun beginActionMode(subscription: Subscription) {
actionMode = startActionMode(this)
adapter.selected.add(subscription.id)
redrawList()
// Fade out FAB
fab.alpha = 1f
fab
.animate()
.alpha(0f)
.setDuration(ANIMATION_DURATION)
.setListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
fab.visibility = View.GONE
}
})
// Fade status bar color
val fromColor = ContextCompat.getColor(this, R.color.primaryColor)
val toColor = ContextCompat.getColor(this, R.color.primaryDarkColor)
2021-11-04 06:56:08 +13:00
fadeStatusBarColor(window, fromColor, toColor)
2021-11-04 05:48:13 +13:00
}
private fun finishActionMode() {
actionMode!!.finish()
endActionModeAndRedraw()
}
private fun endActionModeAndRedraw() {
actionMode = null
adapter.selected.clear()
redrawList()
// Fade in FAB
fab.alpha = 0f
fab.visibility = View.VISIBLE
fab
.animate()
.alpha(1f)
.setDuration(ANIMATION_DURATION)
.setListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
fab.visibility = View.VISIBLE // Required to replace the old listener
}
})
// Fade status bar color
val fromColor = ContextCompat.getColor(this, R.color.primaryDarkColor)
val toColor = ContextCompat.getColor(this, R.color.primaryColor)
2021-11-04 06:56:08 +13:00
fadeStatusBarColor(window, fromColor, toColor)
2021-11-04 05:48:13 +13:00
}
private fun redrawList() {
2021-11-23 09:45:43 +13:00
runOnUiThread {
mainList.adapter = adapter // Oh, what a hack ...
}
2021-11-04 05:48:13 +13:00
}
2021-10-30 14:13:58 +13:00
companion object {
const val TAG = "NtfyMainActivity"
2021-11-01 08:19:25 +13:00
const val EXTRA_SUBSCRIPTION_ID = "subscriptionId"
const val EXTRA_SUBSCRIPTION_BASE_URL = "subscriptionBaseUrl"
const val EXTRA_SUBSCRIPTION_TOPIC = "subscriptionTopic"
2021-11-14 13:26:37 +13:00
const val EXTRA_SUBSCRIPTION_INSTANT = "subscriptionInstant"
2021-11-23 09:45:43 +13:00
const val EXTRA_SUBSCRIPTION_MUTED_UNTIL = "subscriptionMutedUntil"
2021-11-04 05:48:13 +13:00
const val ANIMATION_DURATION = 80L
2021-10-27 06:46:49 +13:00
}
}