Skip to content

Commit

Permalink
Merge pull request #307 from cyb3rko/fix-build-warnings
Browse files Browse the repository at this point in the history
Fix build warnings
  • Loading branch information
jmattheis authored Jul 29, 2023
2 parents 53ff03e + 74e1be1 commit 310b73a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 52 deletions.
8 changes: 6 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<manifest
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Expand All @@ -16,8 +18,10 @@
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:enableOnBackInvokedCallback="true"
android:networkSecurityConfig="@xml/network_security_config"
android:theme="@style/AppTheme.SplashScreen">
android:theme="@style/AppTheme.SplashScreen"
tools:targetApi="tiramisu">
<activity
android:name=".init.InitializationActivity"
android:exported="true"
Expand Down
56 changes: 24 additions & 32 deletions app/src/main/kotlin/com/github/gotify/login/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.github.gotify.R
import com.github.gotify.SSLSettings
Expand Down Expand Up @@ -35,18 +36,34 @@ import java.security.cert.X509Certificate
import okhttp3.HttpUrl

internal class LoginActivity : AppCompatActivity() {
companion object {
// return value from startActivityForResult when choosing a certificate
private const val FILE_SELECT_CODE = 1
}

private lateinit var binding: ActivityLoginBinding
private lateinit var settings: Settings

private var disableSslValidation = false
private var caCertContents: String? = null
private lateinit var advancedDialog: AdvancedDialog

private val certificateDialogResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
try {
require(result.resultCode == RESULT_OK) { "result was ${result.resultCode}" }
requireNotNull(result.data) { "file path was null" }

val uri = result.data!!.data ?: throw IllegalArgumentException("file path was null")
val fileStream = contentResolver.openInputStream(uri)
?: throw IllegalArgumentException("file path was invalid")

val content = Utils.readFileFromStream(fileStream)
val name = getNameOfCertContent(content)

// temporarily set the contents (don't store to settings until they decide to login)
caCertContents = content
advancedDialog.showRemoveCACertificate(name)
} catch (e: Exception) {
Utils.showSnackBar(this, getString(R.string.select_ca_failed, e.message))
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
UncaughtExceptionHandler.registerCurrentThread()
Expand Down Expand Up @@ -152,40 +169,15 @@ internal class LoginActivity : AppCompatActivity() {
intent.addCategory(Intent.CATEGORY_OPENABLE)

try {
startActivityForResult(
Intent.createChooser(intent, getString(R.string.select_ca_file)),
FILE_SELECT_CODE
certificateDialogResultLauncher.launch(
Intent.createChooser(intent, getString(R.string.select_ca_file))
)
} catch (e: ActivityNotFoundException) {
// case for user not having a file browser installed
Utils.showSnackBar(this, getString(R.string.please_install_file_browser))
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
try {
if (requestCode == FILE_SELECT_CODE) {
require(resultCode == RESULT_OK) { "result was $resultCode" }
requireNotNull(data) { "file path was null" }

val uri = data.data ?: throw IllegalArgumentException("file path was null")

val fileStream = contentResolver.openInputStream(uri)
?: throw IllegalArgumentException("file path was invalid")

val content = Utils.readFileFromStream(fileStream)
val name = getNameOfCertContent(content)

// temporarily set the contents (don't store to settings until they decide to login)
caCertContents = content
advancedDialog.showRemoveCACertificate(name)
}
} catch (e: Exception) {
Utils.showSnackBar(this, getString(R.string.select_ca_failed, e.message))
}
}

private fun getNameOfCertContent(content: String): String {
val ca = CertUtils.parseCertificate(content)
return (ca as X509Certificate).subjectDN.name
Expand Down
20 changes: 15 additions & 5 deletions app/src/main/kotlin/com/github/gotify/messages/MessagesActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.view.MenuItem
import android.view.View
import android.widget.ImageButton
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -68,6 +69,7 @@ internal class MessagesActivity :
private var isLoadMore = false
private var updateAppOnDrawerClose: Long? = null
private lateinit var listMessageAdapter: ListMessageAdapter
private lateinit var onBackPressedCallback: OnBackPressedCallback

private val receiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Expand Down Expand Up @@ -108,6 +110,7 @@ internal class MessagesActivity :
listAnimation
)
}
addBackPressCallback()

messagesView.addItemDecoration(dividerItemDecoration)
messagesView.setHasFixedSize(true)
Expand All @@ -126,6 +129,9 @@ internal class MessagesActivity :
swipeRefreshLayout.setOnRefreshListener { onRefresh() }
binding.drawerLayout.addDrawerListener(
object : SimpleDrawerListener() {
override fun onDrawerOpened(drawerView: View) {
onBackPressedCallback.isEnabled = true
}
override fun onDrawerClosed(drawerView: View) {
updateAppOnDrawerClose?.let { selectApp ->
updateAppOnDrawerClose = null
Expand All @@ -135,6 +141,7 @@ internal class MessagesActivity :
}
invalidateOptionsMenu()
}
onBackPressedCallback.isEnabled = false
}
}
)
Expand Down Expand Up @@ -242,12 +249,15 @@ internal class MessagesActivity :
refreshAll.setOnClickListener { refreshAll() }
}

override fun onBackPressed() {
if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
binding.drawerLayout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
private fun addBackPressCallback() {
onBackPressedCallback = object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
binding.drawerLayout.closeDrawer(GravityCompat.START)
}
}
}
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal class MessagesModelFactory(
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass == MessagesModel::class.java) {
@Suppress("UNCHECKED_CAST")
return modelClass.cast(MessagesModel(modelParameterActivity)) as T
}
throw IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.gotify.service

import android.app.AlarmManager
import android.net.ConnectivityManager
import android.os.Build
import android.os.Handler
import android.os.Looper
Expand All @@ -24,7 +23,6 @@ internal class WebSocketConnection(
private val baseUrl: String,
settings: SSLSettings,
private val token: String?,
private val connectivityManager: ConnectivityManager,
private val alarmManager: AlarmManager
) {
companion object {
Expand Down Expand Up @@ -200,12 +198,6 @@ internal class WebSocketConnection(
}

errorCount++

val network = connectivityManager.activeNetworkInfo
if (network == null || !network.isConnected) {
Log.i("WebSocket($id): Network not connected")
}

val minutes = (errorCount * 2 - 1).coerceAtMost(20)

onNetworkFailure.execute(minutes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ internal class WebSocketService : Service() {
settings.url,
settings.sslSettings(),
settings.token,
cm,
alarmManager
)
.onOpen { onOpen() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ internal class SettingsActivity : AppCompatActivity(), OnSharedPreferenceChangeL
private fun showListPreferenceDialog(preference: ListPreference) {
val dialogFragment = MaterialListPreference()
dialogFragment.arguments = Bundle(1).apply { putString("key", preference.key) }
@Suppress("DEPRECATION") // https://issuetracker.google.com/issues/181793702#comment3
dialogFragment.setTargetFragment(this, 0)
dialogFragment.show(
parentFragmentManager,
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="nav_header_desc">Navigation header</string>
<string name="found_gotify_version">Found Gotify v%s</string>
<string name="version_failed_status_code">Request to \'%s\' failed with status code %d</string>
<string name="version_failed">Request to \'%s\' failed. %s.</string>
<string name="version_failed_status_code">Request to \'%1$s\' failed with status code %2$d</string>
<string name="version_failed">Request to \'%1$s\' failed. %2$s.</string>
<string name="wronguserpw">There is no user with this username and password</string>
<string name="create_client_title">Client Name</string>
<string name="create_client_message">Choose a name for your session</string>
Expand All @@ -18,7 +18,7 @@
<string name="oops">Oops</string>
<string name="not_available">Cannot connect to %s</string>
<string name="auth_failed">Server returned 401 unauthorized while trying to get current user. This can be caused by deleting the client for this session.</string>
<string name="other_error">Could not get current user. Server (%s) responded with code %d: body (first 200 chars): %s</string>
<string name="other_error">Could not get current user. Server (%1$s) responded with code %2$d: body (first 200 chars): %3$s</string>
<string name="user_action">User action required</string>
<string name="websocket_closed_logout">Please login, the authentication token isn\'t valid anymore.</string>
<string name="websocket_closed">Connection closed</string>
Expand Down Expand Up @@ -60,7 +60,7 @@
<string name="websocket_listening">Connected</string>
<string name="websocket_could_not_connect">Could not connect</string>
<string name="websocket_init">Initializing</string>
<string name="versions">gotify/android v%s; gotify/server v%s</string>
<string name="versions">gotify/android v%1$s; gotify/server v%2$s</string>
<string name="advanced_settings">Advanced Settings</string>
<string name="done">Done</string>
<string name="no_certificate_selected">No certificate selected</string>
Expand Down

0 comments on commit 310b73a

Please sign in to comment.