chore: apply IDE hints and KTX functions

This commit is contained in:
Niko Diamadis
2025-06-30 17:29:46 +02:00
parent cd391cceab
commit 489ac9ecc1
11 changed files with 31 additions and 28 deletions

View File

@@ -3,12 +3,12 @@ package com.github.gotify
import android.content.Context import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.net.Uri import android.net.Uri
import android.util.Base64 import android.util.Base64
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.appcompat.content.res.AppCompatResources import androidx.appcompat.content.res.AppCompatResources
import androidx.core.graphics.drawable.toBitmap import androidx.core.graphics.drawable.toBitmap
import androidx.core.graphics.drawable.toDrawable
import coil.ImageLoader import coil.ImageLoader
import coil.annotation.ExperimentalCoilApi import coil.annotation.ExperimentalCoilApi
import coil.decode.DataSource import coil.decode.DataSource
@@ -148,7 +148,7 @@ class DataDecoderFactory : Fetcher.Factory<Uri> {
return Fetcher { return Fetcher {
DrawableResult( DrawableResult(
drawable = BitmapDrawable(options.context.resources, bitmap), drawable = bitmap.toDrawable(options.context.resources),
isSampled = false, isSampled = false,
dataSource = DataSource.MEMORY dataSource = DataSource.MEMORY
) )

View File

@@ -33,7 +33,7 @@ internal class MissedMessageUtil(private val api: MessageApi) {
val filtered = filter(messages, till) val filtered = filter(messages, till)
result.addAll(filtered) result.addAll(filtered)
if (messages.size != filtered.size || if (messages.size != filtered.size ||
messages.size == 0 || messages.isEmpty() ||
pagedMessages.paging.next == null pagedMessages.paging.next == null
) { ) {
break break

View File

@@ -2,6 +2,7 @@ package com.github.gotify
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import androidx.core.content.edit
import com.github.gotify.client.model.User import com.github.gotify.client.model.User
internal class Settings(context: Context) { internal class Settings(context: Context) {
@@ -9,10 +10,10 @@ internal class Settings(context: Context) {
val filesDir: String val filesDir: String
var url: String var url: String
get() = sharedPreferences.getString("url", "")!! get() = sharedPreferences.getString("url", "")!!
set(value) = sharedPreferences.edit().putString("url", value).apply() set(value) = sharedPreferences.edit { putString("url", value) }
var token: String? var token: String?
get() = sharedPreferences.getString("token", null) get() = sharedPreferences.getString("token", null)
set(value) = sharedPreferences.edit().putString("token", value).apply() set(value) = sharedPreferences.edit { putString("token", value) }
var user: User? = null var user: User? = null
get() { get() {
val username = sharedPreferences.getString("username", null) val username = sharedPreferences.getString("username", null)
@@ -26,22 +27,24 @@ internal class Settings(context: Context) {
private set private set
var serverVersion: String var serverVersion: String
get() = sharedPreferences.getString("version", "UNKNOWN")!! get() = sharedPreferences.getString("version", "UNKNOWN")!!
set(value) = sharedPreferences.edit().putString("version", value).apply() set(value) = sharedPreferences.edit { putString("version", value) }
var legacyCert: String? var legacyCert: String?
get() = sharedPreferences.getString("cert", null) get() = sharedPreferences.getString("cert", null)
set(value) = sharedPreferences.edit().putString("cert", value).commit().toUnit() set(value) = sharedPreferences.edit(commit = true) { putString("cert", value) }.toUnit()
var caCertPath: String? var caCertPath: String?
get() = sharedPreferences.getString("caCertPath", null) get() = sharedPreferences.getString("caCertPath", null)
set(value) = sharedPreferences.edit().putString("caCertPath", value).commit().toUnit() set(value) = sharedPreferences
.edit(commit = true) { putString("caCertPath", value) }
.toUnit()
var validateSSL: Boolean var validateSSL: Boolean
get() = sharedPreferences.getBoolean("validateSSL", true) get() = sharedPreferences.getBoolean("validateSSL", true)
set(value) = sharedPreferences.edit().putBoolean("validateSSL", value).apply() set(value) = sharedPreferences.edit { putBoolean("validateSSL", value) }
var clientCertPath: String? var clientCertPath: String?
get() = sharedPreferences.getString("clientCertPath", null) get() = sharedPreferences.getString("clientCertPath", null)
set(value) = sharedPreferences.edit().putString("clientCertPath", value).apply() set(value) = sharedPreferences.edit { putString("clientCertPath", value) }
var clientCertPassword: String? var clientCertPassword: String?
get() = sharedPreferences.getString("clientCertPass", null) get() = sharedPreferences.getString("clientCertPass", null)
set(value) = sharedPreferences.edit().putString("clientCertPass", value).apply() set(value) = sharedPreferences.edit { putString("clientCertPass", value) }
init { init {
sharedPreferences = context.getSharedPreferences("gotify", Context.MODE_PRIVATE) sharedPreferences = context.getSharedPreferences("gotify", Context.MODE_PRIVATE)
@@ -61,7 +64,7 @@ internal class Settings(context: Context) {
} }
fun setUser(name: String?, admin: Boolean) { fun setUser(name: String?, admin: Boolean) {
sharedPreferences.edit().putString("username", name).putBoolean("admin", admin).apply() sharedPreferences.edit { putString("username", name).putBoolean("admin", admin) }
} }
fun sslSettings(): SSLSettings { fun sslSettings(): SSLSettings {

View File

@@ -1,8 +1,6 @@
package com.github.gotify.api package com.github.gotify.api
import android.app.Activity import android.app.Activity
import com.github.gotify.api.Callback.ErrorCallback
import com.github.gotify.api.Callback.SuccessCallback
import org.tinylog.kotlin.Logger import org.tinylog.kotlin.Logger
import retrofit2.Call import retrofit2.Call
import retrofit2.Response import retrofit2.Response

View File

@@ -3,13 +3,13 @@ package com.github.gotify.init
import android.Manifest import android.Manifest
import android.app.AlarmManager import android.app.AlarmManager
import android.content.Intent import android.content.Intent
import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.github.gotify.R import com.github.gotify.R
import com.github.gotify.Settings import com.github.gotify.Settings
@@ -126,7 +126,7 @@ internal class InitializationActivity : AppCompatActivity() {
.setPositiveButton(getString(R.string.permissions_dialog_grant)) { _, _ -> .setPositiveButton(getString(R.string.permissions_dialog_grant)) { _, _ ->
Intent( Intent(
android.provider.Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM, android.provider.Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM,
Uri.parse("package:$packageName") "package:$packageName".toUri()
).apply { ).apply {
activityResultLauncher.launch(this) activityResultLauncher.launch(this)
} }

View File

@@ -2,7 +2,6 @@ package com.github.gotify.log
import android.content.ClipData import android.content.ClipData
import android.content.ClipboardManager import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
@@ -73,7 +72,7 @@ internal class LogsActivity : AppCompatActivity() {
R.id.action_copy_logs -> { R.id.action_copy_logs -> {
val content = binding.logContent val content = binding.logContent
val clipboardManager = val clipboardManager =
getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText("GotifyLog", content.text.toString()) val clipData = ClipData.newPlainText("GotifyLog", content.text.toString())
clipboardManager.setPrimaryClip(clipData) clipboardManager.setPrimaryClip(clipData)
Utils.showSnackBar(this, getString(R.string.logs_copied)) Utils.showSnackBar(this, getString(R.string.logs_copied))

View File

@@ -215,7 +215,7 @@ internal class LoginActivity : AppCompatActivity() {
try { try {
resultLauncher.launch(Intent.createChooser(intent, getString(descriptionId))) resultLauncher.launch(Intent.createChooser(intent, getString(descriptionId)))
} catch (e: ActivityNotFoundException) { } catch (_: ActivityNotFoundException) {
// case for user not having a file browser installed // case for user not having a file browser installed
Utils.showSnackBar(this, getString(R.string.please_install_file_browser)) Utils.showSnackBar(this, getString(R.string.please_install_file_browser))
} }

View File

@@ -1,9 +1,9 @@
package com.github.gotify.messages package com.github.gotify.messages
import android.content.Intent import android.content.Intent
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.net.toUri
import com.github.gotify.databinding.ActivityDialogIntentUrlBinding import com.github.gotify.databinding.ActivityDialogIntentUrlBinding
internal class IntentUrlDialogActivity : AppCompatActivity() { internal class IntentUrlDialogActivity : AppCompatActivity() {
@@ -18,7 +18,7 @@ internal class IntentUrlDialogActivity : AppCompatActivity() {
binding.openButton.setOnClickListener { binding.openButton.setOnClickListener {
finish() finish()
Intent(Intent.ACTION_VIEW).apply { Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(intentUrl) data = intentUrl?.toUri()
flags = Intent.FLAG_ACTIVITY_NEW_TASK flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(this) startActivity(this)
} }

View File

@@ -1,5 +1,6 @@
package com.github.gotify.messages package com.github.gotify.messages
import android.annotation.SuppressLint
import android.app.NotificationManager import android.app.NotificationManager
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
@@ -8,7 +9,6 @@ import android.content.IntentFilter
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
@@ -21,6 +21,8 @@ import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.graphics.drawable.toDrawable
import androidx.core.net.toUri
import androidx.core.view.GravityCompat import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout.SimpleDrawerListener import androidx.drawerlayout.widget.DrawerLayout.SimpleDrawerListener
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
@@ -188,7 +190,7 @@ internal class MessagesActivity :
} }
private fun openDocumentation() { private fun openDocumentation() {
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://gotify.net/docs/pushmsg")) val browserIntent = Intent(Intent.ACTION_VIEW, "https://gotify.net/docs/pushmsg".toUri())
startActivity(browserIntent) startActivity(browserIntent)
} }
@@ -317,6 +319,7 @@ internal class MessagesActivity :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(receiver, filter, RECEIVER_EXPORTED) registerReceiver(receiver, filter, RECEIVER_EXPORTED)
} else { } else {
@SuppressLint("UnspecifiedRegisterReceiverFlag")
registerReceiver(receiver, filter) registerReceiver(receiver, filter)
} }
launchCoroutine { launchCoroutine {
@@ -409,7 +412,7 @@ internal class MessagesActivity :
icon = DrawableCompat.wrap(drawable.mutate()) icon = DrawableCompat.wrap(drawable.mutate())
DrawableCompat.setTint(icon!!, iconColorId) DrawableCompat.setTint(icon!!, iconColorId)
} }
background = ColorDrawable(backgroundColorId) background = backgroundColorId.toDrawable()
} }
override fun onMove( override fun onMove(

View File

@@ -10,12 +10,12 @@ import android.content.pm.ServiceInfo
import android.graphics.Color import android.graphics.Color
import android.net.ConnectivityManager import android.net.ConnectivityManager
import android.net.Network import android.net.Network
import android.net.Uri
import android.os.Build import android.os.Build
import android.os.IBinder import android.os.IBinder
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import com.github.gotify.BuildConfig import com.github.gotify.BuildConfig
import com.github.gotify.CoilInstance import com.github.gotify.CoilInstance
import com.github.gotify.MarkwonFactory import com.github.gotify.MarkwonFactory
@@ -331,7 +331,7 @@ internal class WebSocketService : Service() {
if (url != null) { if (url != null) {
intent = Intent(Intent.ACTION_VIEW) intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(url) intent.data = url.toUri()
} else { } else {
intent = Intent(this, MessagesActivity::class.java) intent = Intent(this, MessagesActivity::class.java)
} }

View File

@@ -5,13 +5,13 @@ import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.SharedPreferences.OnSharedPreferenceChangeListener import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.provider.Settings import android.provider.Settings
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.net.toUri
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.ListPreferenceDialogFragmentCompat import androidx.preference.ListPreferenceDialogFragmentCompat
import androidx.preference.Preference import androidx.preference.Preference
@@ -128,7 +128,7 @@ internal class SettingsActivity :
private fun openSystemAlertWindowPermissionPage(): Boolean { private fun openSystemAlertWindowPermissionPage(): Boolean {
Intent( Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:${requireContext().packageName}") "package:${requireContext().packageName}".toUri()
).apply { ).apply {
startActivity(this) startActivity(this)
} }