Implement changes required by new Kotlin codebase
This commit is contained in:
@@ -49,7 +49,7 @@ internal object ClientFactory {
|
||||
}
|
||||
|
||||
fun userApiWithToken(settings: Settings): UserApi? {
|
||||
return clientToken(settings.url(), settings.sslSettings(), settings.token())
|
||||
return clientToken(settings.url, settings.sslSettings(), settings.token)
|
||||
.createService(UserApi::class.java)
|
||||
}
|
||||
|
||||
|
||||
@@ -59,16 +59,18 @@ class InitializationActivity : AppCompatActivity() {
|
||||
|
||||
private fun tryAuthenticate() {
|
||||
ClientFactory.userApiWithToken(settings)
|
||||
.currentUser()
|
||||
.enqueue(Callback.callInUI(this, { authenticated(it) }) { apiException ->
|
||||
failed(apiException)
|
||||
})
|
||||
?.currentUser()
|
||||
?.enqueue(
|
||||
Callback.callInUI(this, { if (it != null) authenticated(it) }) { apiException ->
|
||||
failed(apiException)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private fun failed(exception: ApiException) {
|
||||
when (exception.code()) {
|
||||
when (exception.code) {
|
||||
0 -> {
|
||||
dialog(getString(R.string.not_available, settings.url()))
|
||||
dialog(getString(R.string.not_available, settings.url))
|
||||
return
|
||||
}
|
||||
401 -> {
|
||||
@@ -77,9 +79,9 @@ class InitializationActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
var response = exception.body()
|
||||
var response = exception.body
|
||||
response = response.substring(0, 200.coerceAtMost(response.length))
|
||||
dialog(getString(R.string.other_error, settings.url(), exception.code(), response))
|
||||
dialog(getString(R.string.other_error, settings.url, exception.code, response))
|
||||
}
|
||||
|
||||
private fun dialog(message: String) {
|
||||
@@ -94,7 +96,7 @@ class InitializationActivity : AppCompatActivity() {
|
||||
private fun authenticated(user: User) {
|
||||
Log.i("Authenticated as ${user.name}")
|
||||
|
||||
settings.user(user.name, user.isAdmin)
|
||||
settings.setUser(user.name, user.isAdmin)
|
||||
requestVersion {
|
||||
startActivity(Intent(this, MessagesActivity::class.java))
|
||||
finish()
|
||||
@@ -108,9 +110,11 @@ class InitializationActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun requestVersion(runnable: Runnable) {
|
||||
requestVersion({ version: VersionInfo ->
|
||||
Log.i("Server version: ${version.version}@${version.buildDate}")
|
||||
settings.serverVersion(version.version)
|
||||
requestVersion({ version: VersionInfo? ->
|
||||
if (version != null) {
|
||||
Log.i("Server version: ${version.version}@${version.buildDate}")
|
||||
settings.serverVersion = version.version
|
||||
}
|
||||
runnable.run()
|
||||
}) { runnable.run() }
|
||||
}
|
||||
@@ -119,8 +123,8 @@ class InitializationActivity : AppCompatActivity() {
|
||||
callback: SuccessCallback<VersionInfo>,
|
||||
errorCallback: Callback.ErrorCallback
|
||||
) {
|
||||
ClientFactory.versionApi(settings.url(), settings.sslSettings())
|
||||
.version
|
||||
.enqueue(Callback.callInUI(this, callback, errorCallback))
|
||||
ClientFactory.versionApi(settings.url, settings.sslSettings())
|
||||
?.version
|
||||
?.enqueue(Callback.callInUI(this, callback, errorCallback))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,8 +104,8 @@ class LoginActivity : AppCompatActivity() {
|
||||
|
||||
try {
|
||||
ClientFactory.versionApi(fixedUrl, tempSslSettings())
|
||||
.version
|
||||
.enqueue(Callback.callInUI(this, onValidUrl(fixedUrl), onInvalidUrl(fixedUrl)))
|
||||
?.version
|
||||
?.enqueue(Callback.callInUI(this, onValidUrl(fixedUrl), onInvalidUrl(fixedUrl)))
|
||||
} catch (e: Exception) {
|
||||
binding.checkurlProgress.visibility = View.GONE
|
||||
binding.checkurl.visibility = View.VISIBLE
|
||||
@@ -196,10 +196,10 @@ class LoginActivity : AppCompatActivity() {
|
||||
|
||||
private fun onValidUrl(url: String): SuccessCallback<VersionInfo> {
|
||||
return SuccessCallback { version ->
|
||||
settings.url(url)
|
||||
settings.url = url
|
||||
binding.checkurlProgress.visibility = View.GONE
|
||||
binding.checkurl.visibility = View.VISIBLE
|
||||
binding.checkurl.text = getString(R.string.found_gotify_version, version.version)
|
||||
binding.checkurl.text = getString(R.string.found_gotify_version, version?.version)
|
||||
binding.username.visibility = View.VISIBLE
|
||||
binding.username.requestFocus()
|
||||
binding.password.visibility = View.VISIBLE
|
||||
@@ -222,7 +222,7 @@ class LoginActivity : AppCompatActivity() {
|
||||
binding.login.visibility = View.GONE
|
||||
binding.loginProgress.visibility = View.VISIBLE
|
||||
|
||||
val client = ClientFactory.basicAuth(settings.url(), tempSslSettings(), username, password)
|
||||
val client = ClientFactory.basicAuth(settings.url, tempSslSettings(), username, password)
|
||||
client.createService(UserApi::class.java)
|
||||
.currentUser()
|
||||
.enqueue(Callback.callInUI(this, { newClientDialog(client) }) {
|
||||
@@ -256,16 +256,16 @@ class LoginActivity : AppCompatActivity() {
|
||||
val newClient = Client().name(nameProvider.text.toString())
|
||||
client.createService(ClientApi::class.java)
|
||||
.createClient(newClient)
|
||||
.enqueue(Callback.callInUI(this, { onCreatedClient(it) }) {
|
||||
.enqueue(Callback.callInUI(this, { if (it != null) onCreatedClient(it) }) {
|
||||
onFailedToCreateClient()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private fun onCreatedClient(client: Client) {
|
||||
settings.token(client.token)
|
||||
settings.validateSSL(!disableSslValidation)
|
||||
settings.cert(caCertContents)
|
||||
settings.token = client.token
|
||||
settings.validateSSL = !disableSslValidation
|
||||
settings.cert = caCertContents.toString()
|
||||
|
||||
Utils.showSnackBar(this, getString(R.string.created_client))
|
||||
startActivity(Intent(this, InitializationActivity::class.java))
|
||||
@@ -284,10 +284,10 @@ class LoginActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun versionError(url: String, exception: ApiException): String {
|
||||
return getString(R.string.version_failed_status_code, "$url/version", exception.code())
|
||||
return getString(R.string.version_failed_status_code, "$url/version", exception.code)
|
||||
}
|
||||
|
||||
private fun tempSslSettings(): SSLSettings {
|
||||
return SSLSettings(!disableSslValidation, caCertContents)
|
||||
return SSLSettings(!disableSslValidation, caCertContents.toString())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ class ListMessageAdapter(
|
||||
holder.message!!.text = message.message.message
|
||||
}
|
||||
holder.title!!.text = message.message.title
|
||||
picasso.load(Utils.resolveAbsoluteUrl("${settings.url()}/", message.image))
|
||||
picasso.load(Utils.resolveAbsoluteUrl("${settings.url}/", message.image))
|
||||
.error(R.drawable.ic_alarm)
|
||||
.placeholder(R.drawable.ic_placeholder)
|
||||
.into(holder.image)
|
||||
@@ -144,7 +144,7 @@ class ListMessageAdapter(
|
||||
if (dateTime != null) {
|
||||
text = if (relativeTimeFormat) {
|
||||
// Relative time format
|
||||
Utils.dateToRelative(dateTime)
|
||||
Utils.dateToRelative(dateTime!!)
|
||||
} else {
|
||||
// Absolute time format
|
||||
val time = dateTime!!.toInstant().toEpochMilli()
|
||||
|
||||
@@ -196,7 +196,7 @@ class MessagesActivity : AppCompatActivity(), NavigationView.OnNavigationItemSel
|
||||
.get()
|
||||
.load(
|
||||
Utils.resolveAbsoluteUrl(
|
||||
viewModel.settings.url() + "/", app.image
|
||||
viewModel.settings.url + "/", app.image
|
||||
)
|
||||
)
|
||||
.error(R.drawable.ic_alarm)
|
||||
@@ -223,12 +223,12 @@ class MessagesActivity : AppCompatActivity(), NavigationView.OnNavigationItemSel
|
||||
val headerView: View = binding.navView.getHeaderView(0)
|
||||
val settings = viewModel.settings
|
||||
val user = headerView.findViewById<TextView>(R.id.header_user)
|
||||
user.text = settings.user().name
|
||||
user.text = settings.user?.name
|
||||
val connection = headerView.findViewById<TextView>(R.id.header_connection)
|
||||
connection.text = getString(R.string.connection, settings.user().name, settings.url())
|
||||
connection.text = getString(R.string.connection, settings.user?.name, settings.url)
|
||||
val version = headerView.findViewById<TextView>(R.id.header_version)
|
||||
version.text =
|
||||
getString(R.string.versions, BuildConfig.VERSION_NAME, settings.serverVersion())
|
||||
getString(R.string.versions, BuildConfig.VERSION_NAME, settings.serverVersion)
|
||||
val refreshAll = headerView.findViewById<ImageButton>(R.id.refresh_all)
|
||||
refreshAll.setOnClickListener { view: View? ->
|
||||
onRefreshAll(
|
||||
@@ -480,7 +480,7 @@ class MessagesActivity : AppCompatActivity(), NavigationView.OnNavigationItemSel
|
||||
|
||||
private inner class UpdateMissedMessages : AsyncTask<Long?, Void?, Boolean>() {
|
||||
override fun doInBackground(vararg ids: Long?): Boolean {
|
||||
val id = Utils.first<Long>(ids)
|
||||
val id = ids.first()!!
|
||||
if (id == -1L) {
|
||||
return false
|
||||
}
|
||||
@@ -488,8 +488,7 @@ class MessagesActivity : AppCompatActivity(), NavigationView.OnNavigationItemSel
|
||||
viewModel.client.createService(
|
||||
MessageApi::class.java
|
||||
)
|
||||
)
|
||||
.missingMessages(id)
|
||||
).missingMessages(id).filterNotNull()
|
||||
viewModel.messages.addMessages(newMessages)
|
||||
return newMessages.isNotEmpty()
|
||||
}
|
||||
@@ -528,7 +527,7 @@ class MessagesActivity : AppCompatActivity(), NavigationView.OnNavigationItemSel
|
||||
private fun deleteApp(appId: Long) {
|
||||
val settings = viewModel.settings
|
||||
val client =
|
||||
ClientFactory.clientToken(settings.url(), settings.sslSettings(), settings.token())
|
||||
ClientFactory.clientToken(settings.url, settings.sslSettings(), settings.token)
|
||||
client.createService(ApplicationApi::class.java)
|
||||
.deleteApp(appId)
|
||||
.enqueue(
|
||||
@@ -561,7 +560,7 @@ class MessagesActivity : AppCompatActivity(), NavigationView.OnNavigationItemSel
|
||||
}
|
||||
|
||||
override fun doInBackground(vararg appIds: Long?): Long {
|
||||
val appId = Utils.first<Long>(appIds)
|
||||
val appId = appIds.first()!!
|
||||
viewModel.messages.loadMoreIfNotPresent(appId)
|
||||
return appId
|
||||
}
|
||||
@@ -610,20 +609,19 @@ class MessagesActivity : AppCompatActivity(), NavigationView.OnNavigationItemSel
|
||||
}
|
||||
}
|
||||
|
||||
private inner class DeleteClientAndNavigateToLogin :
|
||||
AsyncTask<Void?, Void?, Void?>() {
|
||||
private inner class DeleteClientAndNavigateToLogin : AsyncTask<Void?, Void?, Void?>() {
|
||||
override fun doInBackground(vararg ignore: Void?): Void? {
|
||||
val settings = viewModel.settings
|
||||
val api = ClientFactory.clientToken(
|
||||
settings.url(), settings.sslSettings(), settings.token()
|
||||
settings.url, settings.sslSettings(), settings.token
|
||||
)
|
||||
.createService(ClientApi::class.java)
|
||||
stopService(Intent(this@MessagesActivity, WebSocketService::class.java))
|
||||
try {
|
||||
val clients = Api.execute(api.clients)
|
||||
val clients = Api.execute(api.clients) ?: emptyList()
|
||||
var currentClient: Client? = null
|
||||
for (client in clients) {
|
||||
if (client.token == settings.token()) {
|
||||
if (client.token == settings.token) {
|
||||
currentClient = client
|
||||
break
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ class MessagesModel(parentView: Activity) : ViewModel() {
|
||||
init {
|
||||
settings = Settings(parentView)
|
||||
picassoHandler = PicassoHandler(parentView, settings)
|
||||
client = ClientFactory.clientToken(settings.url(), settings.sslSettings(), settings.token())
|
||||
client = ClientFactory.clientToken(settings.url, settings.sslSettings(), settings.token)
|
||||
appsHolder = ApplicationHolder(parentView, client)
|
||||
messages = MessageFacade(client.createService(MessageApi::class.java), appsHolder)
|
||||
}
|
||||
|
||||
@@ -23,10 +23,8 @@ class ApplicationHolder(private val activity: Activity, private val client: ApiC
|
||||
.enqueue(
|
||||
Callback.callInUI(
|
||||
activity,
|
||||
{ apps: List<Application> ->
|
||||
onReceiveApps(
|
||||
apps
|
||||
)
|
||||
{ apps: List<Application>? ->
|
||||
if (apps != null) onReceiveApps(apps)
|
||||
}
|
||||
) { e: ApiException -> onFailedApps(e) })
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ class PicassoHandler(private val context: Context, private val settings: Setting
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun getImageFromUrl(url: String): Bitmap {
|
||||
fun getImageFromUrl(url: String?): Bitmap {
|
||||
return picasso.load(url).get()
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ class PicassoHandler(private val context: Context, private val settings: Setting
|
||||
}
|
||||
try {
|
||||
return getImageFromUrl(
|
||||
Utils.resolveAbsoluteUrl("${settings.url()}/", appIdToAppImage[appId])
|
||||
Utils.resolveAbsoluteUrl("${settings.url}/", appIdToAppImage[appId])
|
||||
)
|
||||
} catch (e: IOException) {
|
||||
Log.e("Could not load image for notification", e)
|
||||
@@ -65,12 +65,12 @@ class PicassoHandler(private val context: Context, private val settings: Setting
|
||||
}
|
||||
|
||||
fun updateAppIds() {
|
||||
ClientFactory.clientToken(settings.url(), settings.sslSettings(), settings.token())
|
||||
ClientFactory.clientToken(settings.url, settings.sslSettings(), settings.token)
|
||||
.createService(ApplicationApi::class.java)
|
||||
.apps
|
||||
.enqueue(Callback.call({ apps ->
|
||||
appIdToAppImage.clear()
|
||||
appIdToAppImage.putAll(MessageImageCombiner.appIdToImage(apps))
|
||||
appIdToAppImage.putAll(MessageImageCombiner.appIdToImage(apps ?: emptyList()))
|
||||
}) { appIdToAppImage.clear() })
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ import okhttp3.*
|
||||
|
||||
internal class WebSocketConnection(
|
||||
private val baseUrl: String,
|
||||
settings: SSLSettings?,
|
||||
settings: SSLSettings,
|
||||
private val token: String,
|
||||
private val connectivityManager: ConnectivityManager,
|
||||
private val alarmManager: AlarmManager
|
||||
|
||||
@@ -45,9 +45,9 @@ class WebSocketService : Service() {
|
||||
super.onCreate()
|
||||
settings = Settings(this)
|
||||
val client = ClientFactory.clientToken(
|
||||
settings.url(),
|
||||
settings.url,
|
||||
settings.sslSettings(),
|
||||
settings.token()
|
||||
settings.token
|
||||
)
|
||||
missingMessageUtil = MissedMessageUtil(client.createService(MessageApi::class.java))
|
||||
Log.i("Create ${javaClass.simpleName}")
|
||||
@@ -63,7 +63,7 @@ class WebSocketService : Service() {
|
||||
Log.w("Destroy ${javaClass.simpleName}")
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
Log.init(this)
|
||||
if (connection != null) {
|
||||
connection!!.close()
|
||||
@@ -80,16 +80,16 @@ class WebSocketService : Service() {
|
||||
showForegroundNotification(getString(R.string.websocket_init))
|
||||
|
||||
if (lastReceivedMessage.get() == NOT_LOADED) {
|
||||
missingMessageUtil.lastReceivedMessage { lastReceivedMessage.set(it) }
|
||||
missingMessageUtil.lastReceivedMessage { lastReceivedMessage.set(it ?: 0L) }
|
||||
}
|
||||
|
||||
val cm = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
val alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
|
||||
|
||||
connection = WebSocketConnection(
|
||||
settings.url(),
|
||||
settings.url,
|
||||
settings.sslSettings(),
|
||||
settings.token(),
|
||||
settings.token,
|
||||
cm,
|
||||
alarmManager
|
||||
)
|
||||
@@ -105,7 +105,7 @@ class WebSocketService : Service() {
|
||||
onNetworkFailure(minutes)
|
||||
}
|
||||
})
|
||||
.onMessage { onMessage(it) }
|
||||
.onMessage { if (it != null) onMessage(it) }
|
||||
.onReconnected { notifyMissedNotifications() }
|
||||
.start()
|
||||
|
||||
@@ -120,9 +120,9 @@ class WebSocketService : Service() {
|
||||
getString(R.string.websocket_closed), getString(R.string.websocket_reconnect)
|
||||
)
|
||||
ClientFactory.userApiWithToken(settings)
|
||||
.currentUser()
|
||||
.enqueue(Callback.call({ doReconnect() }) { exception ->
|
||||
if (exception.code() == 401) {
|
||||
?.currentUser()
|
||||
?.enqueue(Callback.call({ doReconnect() }) { exception ->
|
||||
if (exception.code == 401) {
|
||||
showForegroundNotification(
|
||||
getString(R.string.user_action),
|
||||
getString(R.string.websocket_closed_logout)
|
||||
@@ -164,7 +164,7 @@ class WebSocketService : Service() {
|
||||
return
|
||||
}
|
||||
|
||||
val messages = missingMessageUtil.missingMessages(messageId)
|
||||
val messages = missingMessageUtil.missingMessages(messageId).filterNotNull()
|
||||
|
||||
if (messages.size > 5) {
|
||||
onGroupedMessages(messages)
|
||||
|
||||
@@ -58,9 +58,9 @@ class ShareActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
val client = ClientFactory.clientToken(
|
||||
settings.url(),
|
||||
settings.url,
|
||||
settings.sslSettings(),
|
||||
settings.token()
|
||||
settings.token
|
||||
)
|
||||
appsHolder = ApplicationHolder(this, client)
|
||||
appsHolder.onUpdate {
|
||||
@@ -116,7 +116,7 @@ class ShareActivity : AppCompatActivity() {
|
||||
|
||||
PushMessage({
|
||||
val pushClient = ClientFactory.clientToken(
|
||||
settings.url(),
|
||||
settings.url,
|
||||
settings.sslSettings(),
|
||||
appsHolder.get()[appIndex].token
|
||||
)
|
||||
@@ -150,7 +150,7 @@ class ShareActivity : AppCompatActivity() {
|
||||
) : AsyncTask<Message?, String?, String>() {
|
||||
@Deprecated("Deprecated in Java")
|
||||
override fun doInBackground(vararg messages: Message?): String {
|
||||
return backgroundAction(Utils.first(messages))
|
||||
return backgroundAction(messages.first())
|
||||
}
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
|
||||
Reference in New Issue
Block a user