ntfy-android/app/src/main/java/io/heckel/ntfy/list/TopicsListActivity.kt

195 lines
7.3 KiB
Kotlin
Raw Normal View History

/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.heckel.ntfy.list
import android.app.Activity
2021-10-26 13:25:54 +13:00
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
2021-10-26 13:25:54 +13:00
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
2021-10-26 07:24:44 +13:00
import androidx.appcompat.app.AppCompatActivity
2021-10-26 13:25:54 +13:00
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
2021-10-26 09:16:23 +13:00
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
2021-10-27 05:23:41 +13:00
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import com.google.gson.JsonSyntaxException
2021-10-26 09:16:23 +13:00
import io.heckel.ntfy.R
2021-10-26 07:24:44 +13:00
import io.heckel.ntfy.add.AddTopicActivity
import io.heckel.ntfy.add.TOPIC_URL
2021-10-26 14:14:09 +13:00
import io.heckel.ntfy.data.Event
2021-10-26 09:16:23 +13:00
import io.heckel.ntfy.data.NtfyApi
import io.heckel.ntfy.data.Topic
2021-10-26 07:24:44 +13:00
import io.heckel.ntfy.detail.TopicDetailActivity
2021-10-27 05:23:41 +13:00
import kotlinx.coroutines.*
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
2021-10-26 13:25:54 +13:00
import kotlin.random.Random
2021-10-26 06:45:56 +13:00
const val TOPIC_ID = "topic id"
class TopicsListActivity : AppCompatActivity() {
2021-10-26 09:16:23 +13:00
private val api = NtfyApi(this)
2021-10-27 05:23:41 +13:00
private val jobs = mutableMapOf<Long, Job>()
private val newTopicActivityRequestCode = 1
private val topicsListViewModel by viewModels<TopicsListViewModel> {
2021-10-26 06:45:56 +13:00
TopicsListViewModelFactory(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
2021-10-26 06:45:56 +13:00
val adapter = TopicsAdapter { topic -> adapterOnClick(topic) }
val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
2021-10-26 06:45:56 +13:00
recyclerView.adapter = adapter
2021-10-26 14:14:09 +13:00
topicsListViewModel.topics.observe(this) {
it?.let {
2021-10-26 06:45:56 +13:00
adapter.submitList(it as MutableList<Topic>)
}
2021-10-26 06:45:56 +13:00
}
val fab: View = findViewById(R.id.fab)
fab.setOnClickListener {
fabOnClick()
}
2021-10-26 09:16:23 +13:00
2021-10-26 13:25:54 +13:00
createNotificationChannel()
2021-10-26 14:14:09 +13:00
}
2021-10-26 13:25:54 +13:00
2021-10-26 06:45:56 +13:00
/* Opens TopicDetailActivity when RecyclerView item is clicked. */
private fun adapterOnClick(topic: Topic) {
val intent = Intent(this, TopicDetailActivity()::class.java)
2021-10-26 06:45:56 +13:00
intent.putExtra(TOPIC_ID, topic.id)
startActivity(intent)
}
2021-10-26 06:45:56 +13:00
/* Adds topic to topicList when FAB is clicked. */
private fun fabOnClick() {
val intent = Intent(this, AddTopicActivity::class.java)
startActivityForResult(intent, newTopicActivityRequestCode)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, intentData: Intent?) {
super.onActivityResult(requestCode, resultCode, intentData)
2021-10-26 06:45:56 +13:00
/* Inserts topic into viewModel. */
if (requestCode == newTopicActivityRequestCode && resultCode == Activity.RESULT_OK) {
intentData?.let { data ->
2021-10-27 05:23:41 +13:00
val topicId = Random.nextLong()
2021-10-26 14:14:09 +13:00
val topicUrl = data.getStringExtra(TOPIC_URL) ?: return
2021-10-27 05:23:41 +13:00
val topic = Topic(topicId, topicUrl)
jobs[topicId] = startListening(topicUrl)
topicsListViewModel.add(topic)
}
}
}
2021-10-26 13:25:54 +13:00
private fun createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = getString(R.string.notification_channel_id)
val name = getString(R.string.notification_channel_name)
val descriptionText = getString(R.string.notification_channel_name)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(channelId, name, importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
2021-10-27 05:23:41 +13:00
private val gson = GsonBuilder().create()
fun startListening(url: String): Job {
return this.lifecycleScope.launch(Dispatchers.IO) {
while (isActive) {
println("connecting ...")
val conn = (URL(url).openConnection() as HttpURLConnection).also {
it.doInput = true
}
val input = conn.inputStream.bufferedReader()
try {
conn.connect()
var event = Event()
while (isActive) {
val line = input.readLine()
println("PHIL: " + line)
when {
line == null -> {
println("line is null")
break
}
line.startsWith("event:") -> {
event = event.copy(name = line.substring(6).trim())
}
line.startsWith("data:") -> {
val data = line.substring(5).trim()
try {
event = event.copy(data = gson.fromJson(data, JsonObject::class.java))
} catch (e: JsonSyntaxException) {
// Nothing
}
}
line.isEmpty() -> {
handleEvent(event)
event = Event()
}
}
}
} catch (e: IOException) {
println("PHIL: " + e.message)
} finally {
conn.disconnect()
input.close()
}
println("connection died")
delay(5000)
}
}
}
private fun handleEvent(event: Event) {
if (event.data.isJsonNull || !event.data.has("message")) {
return
}
println("PHIL EVENT: " + event.data)
val channelId = getString(R.string.notification_channel_id)
val notification = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ntfy)
.setContentTitle("ntfy")
.setContentText(event.data.get("message").asString)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
with(NotificationManagerCompat.from(this)) {
notify(Random.nextInt(), notification)
}
}
}