diff --git a/app/src/main/java/com/github/gotify/service/GotifyMessengerService.kt b/app/src/main/java/com/github/gotify/service/GotifyMessengerService.kt index 5dfd822..3dacf8e 100644 --- a/app/src/main/java/com/github/gotify/service/GotifyMessengerService.kt +++ b/app/src/main/java/com/github/gotify/service/GotifyMessengerService.kt @@ -10,6 +10,7 @@ import com.github.gotify.api.ClientFactory import com.github.gotify.client.api.ApplicationApi import com.github.gotify.log.Log import java.io.IOException +import kotlin.concurrent.thread /** * THIS SERVICE IS USED BY OTHER APPS TO REGISTER @@ -33,25 +34,17 @@ class GotifyMessengerService : Service() { MSG_REGISTER_CLIENT -> { val uid = msg.sendingUid val msgData = msg.data - val thread = object : Thread() { - override fun run() { - registerApp(msgData, uid) - } - } - thread.start() - // We need to wait until the registration is done to send url & token : - thread.join() + thread(start = true) { + registerApp(msgData, uid) + }.join() sendInfo(msg) } MSG_UNREGISTER_CLIENT -> { - val thread = object : Thread() { - val uid = msg.sendingUid - val msgData = msg.data - override fun run() { - unregisterApp(msgData, uid) - } + val uid = msg.sendingUid + val msgData = msg.data + thread(start = true) { + unregisterApp(msgData, uid) } - thread.start() simpleAnswer(msg, MSG_UNREGISTER_CLIENT) } else -> super.handleMessage(msg) diff --git a/app/src/main/java/com/github/gotify/service/MessagingDatabase.kt b/app/src/main/java/com/github/gotify/service/MessagingDatabase.kt index 0e26b33..cb16385 100644 --- a/app/src/main/java/com/github/gotify/service/MessagingDatabase.kt +++ b/app/src/main/java/com/github/gotify/service/MessagingDatabase.kt @@ -61,36 +61,34 @@ class MessagingDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, n val db = readableDatabase val selection = "$FIELD_PACKAGE_NAME = ?" val selectionArgs = arrayOf(packageName) - val cursor = db.query( - TABLE_APPS, - null, - selection, - selectionArgs, - null, - null, - null - ) - val res = (cursor != null && cursor.count > 0) - cursor.close() - return res + return db.query( + TABLE_APPS, + null, + selection, + selectionArgs, + null, + null, + null + ).use { cursor -> + (cursor != null && cursor.count > 0) + } } fun strictIsRegistered(packageName: String, uid: Int): Boolean { val db = readableDatabase val selection = "$FIELD_PACKAGE_NAME = ? AND $FIELD_UID = ?" val selectionArgs = arrayOf(packageName,uid.toString()) - val cursor = db.query( - TABLE_APPS, - null, - selection, - selectionArgs, - null, - null, - null - ) - val res = (cursor != null && cursor.count > 0) - cursor.close() - return res + return db.query( + TABLE_APPS, + null, + selection, + selectionArgs, + null, + null, + null + ).use { cursor -> + (cursor != null && cursor.count > 0) + } } fun getServiceName(packageName: String): String{ @@ -98,21 +96,17 @@ class MessagingDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, n val projection = arrayOf(FIELD_SERVICE_NAME) val selection = "$FIELD_PACKAGE_NAME = ?" val selectionArgs = arrayOf(packageName) - val cursor = db.query( - TABLE_APPS, - projection, - selection, - selectionArgs, - null, - null, - null - ) - var res = "" - if(cursor.moveToFirst()){ - res = cursor.getString(cursor.getColumnIndex(FIELD_SERVICE_NAME)) + return db.query( + TABLE_APPS, + projection, + selection, + selectionArgs, + null, + null, + null + ).use { cursor -> + if (cursor.moveToFirst()) cursor.getString(cursor.getColumnIndex(FIELD_SERVICE_NAME)) else "" } - cursor.close() - return res } fun getAppFromId(appId: Int): String{ @@ -120,7 +114,7 @@ class MessagingDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, n val projection = arrayOf(FIELD_PACKAGE_NAME) val selection = "$FIELD_APP_ID = ?" val selectionArgs = arrayOf(appId.toString()) - val cursor = db.query( + return db.query( TABLE_APPS, projection, selection, @@ -128,13 +122,9 @@ class MessagingDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, n null, null, null - ) - var res = "" - if(cursor.moveToFirst()){ - res = cursor.getString(cursor.getColumnIndex(FIELD_PACKAGE_NAME)) + ).use { cursor -> + if (cursor.moveToFirst()) cursor.getString(cursor.getColumnIndex(FIELD_PACKAGE_NAME)) else "" } - cursor.close() - return res } fun getAppId(packageName: String): Int{ @@ -142,7 +132,7 @@ class MessagingDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, n val projection = arrayOf(FIELD_APP_ID) val selection = "$FIELD_PACKAGE_NAME = ?" val selectionArgs = arrayOf(packageName) - val cursor = db.query( + return db.query( TABLE_APPS, projection, selection, @@ -150,13 +140,9 @@ class MessagingDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, n null, null, null - ) - var res = -1 - if(cursor.moveToFirst()){ - res = cursor.getInt(cursor.getColumnIndex(FIELD_APP_ID)) + ).use { cursor -> + if (cursor.moveToFirst()) cursor.getInt(cursor.getColumnIndex(FIELD_APP_ID)) else -1 } - cursor.close() - return res } fun getToken(packageName: String): String{ @@ -164,7 +150,7 @@ class MessagingDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, n val projection = arrayOf(FIELD_TOKEN) val selection = "$FIELD_PACKAGE_NAME = ?" val selectionArgs = arrayOf(packageName) - val cursor = db.query( + val token = db.query( TABLE_APPS, projection, selection, @@ -172,14 +158,11 @@ class MessagingDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, n null, null, null - ) - var res = "" - if(cursor.moveToFirst()){ - res = cursor.getString(cursor.getColumnIndex(FIELD_TOKEN)) + ).use { cursor -> + if (cursor.moveToFirst()) cursor.getString(cursor.getColumnIndex(FIELD_TOKEN)) else "" } - cursor.close() - removeToken(packageName) - return res + removeToken(token) + return token } private fun removeToken(packageName: String){