mirror of
https://codeberg.org/NextPush/nextpush-android.git
synced 2025-02-08 08:09:08 +01:00
Add getDb and clean db when remove device
This commit is contained in:
parent
4cc86f6d21
commit
391382dd51
@ -30,7 +30,7 @@ import org.unifiedpush.distributor.nextpush.account.connect
|
||||
import org.unifiedpush.distributor.nextpush.account.ssoAccount
|
||||
import org.unifiedpush.distributor.nextpush.api.ApiUtils
|
||||
import org.unifiedpush.distributor.nextpush.distributor.sendUnregistered
|
||||
import org.unifiedpush.distributor.nextpush.distributor.MessagingDatabase
|
||||
import org.unifiedpush.distributor.nextpush.distributor.getDb
|
||||
import java.lang.String.format
|
||||
|
||||
private const val TAG = "NextPush-MainActivity"
|
||||
@ -55,7 +55,6 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
try {
|
||||
AccountImporter.onActivityResult(
|
||||
requestCode,
|
||||
@ -91,6 +90,7 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
})
|
||||
} catch (e: AccountImportCancelledException) {}
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
}
|
||||
|
||||
private fun showMain() {
|
||||
@ -120,7 +120,8 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
menuInflater.inflate(R.menu.menu_main, menu)
|
||||
menu.findItem(R.id.action_logout).isVisible = showLogout
|
||||
menu.findItem(R.id.action_restart).isEnabled = showLogout
|
||||
menu.findItem(R.id.action_logout).isEnabled = showLogout
|
||||
return true
|
||||
}
|
||||
|
||||
@ -179,13 +180,12 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
private fun setListView(){
|
||||
listView = findViewById<ListView>(R.id.applications_list)
|
||||
val db = MessagingDatabase(this)
|
||||
val db = getDb(this)
|
||||
val tokenList = db.listTokens().toMutableList()
|
||||
val appList = emptyArray<String>().toMutableList()
|
||||
tokenList.forEach {
|
||||
appList.add(db.getPackageName(it))
|
||||
}
|
||||
db.close()
|
||||
listView.adapter = ArrayAdapter(
|
||||
this,
|
||||
android.R.layout.simple_list_item_1,
|
||||
@ -200,10 +200,9 @@ class MainActivity : AppCompatActivity() {
|
||||
alert.setPositiveButton("YES") { dialog, _ ->
|
||||
val connectorToken = tokenList[position]
|
||||
sendUnregistered(this, connectorToken)
|
||||
val db = MessagingDatabase(this)
|
||||
val db = getDb(this)
|
||||
val appToken = db.getAppToken(connectorToken)
|
||||
db.unregisterApp(connectorToken)
|
||||
db.close()
|
||||
ApiUtils().deleteApp(this, appToken) {
|
||||
Log.d(TAG,"Unregistration is finished")
|
||||
}
|
||||
|
@ -14,7 +14,8 @@ import okhttp3.sse.EventSource
|
||||
import okhttp3.sse.EventSources
|
||||
import org.unifiedpush.distributor.nextpush.account.*
|
||||
import org.unifiedpush.distributor.nextpush.api.ProviderApi.Companion.mApiEndpoint
|
||||
import org.unifiedpush.distributor.nextpush.distributor.MessagingDatabase
|
||||
import org.unifiedpush.distributor.nextpush.distributor.getDb
|
||||
import org.unifiedpush.distributor.nextpush.distributor.sendUnregistered
|
||||
import org.unifiedpush.distributor.nextpush.services.SSEListener
|
||||
import retrofit2.NextcloudRetrofitApiBuilder
|
||||
import java.util.concurrent.TimeUnit
|
||||
@ -112,6 +113,11 @@ class ApiUtils {
|
||||
}
|
||||
|
||||
fun deleteDevice(context: Context) {
|
||||
val db = getDb(context)
|
||||
db.listTokens().forEach {
|
||||
sendUnregistered(context, it)
|
||||
db.unregisterApp(it)
|
||||
}
|
||||
cApi(context) { cDeleteDevice(context) }
|
||||
}
|
||||
|
||||
@ -160,10 +166,9 @@ class ApiUtils {
|
||||
appName: String,
|
||||
connectorToken: String,
|
||||
callback: ()->Unit) {
|
||||
val db = MessagingDatabase(context)
|
||||
val db = getDb(context)
|
||||
if (db.isRegistered(appName, connectorToken)) {
|
||||
Log.i("RegisterService","$appName already registered")
|
||||
db.close()
|
||||
callback()
|
||||
return
|
||||
}
|
||||
@ -193,7 +198,6 @@ class ApiUtils {
|
||||
}
|
||||
|
||||
override fun onComplete() {
|
||||
db.close()
|
||||
callback()
|
||||
}
|
||||
})
|
||||
|
@ -11,10 +11,19 @@ import org.unifiedpush.distributor.nextpush.account.getUrl
|
||||
|
||||
private const val TAG = "DistributorUtils"
|
||||
|
||||
private var db : MessagingDatabase? = null
|
||||
|
||||
fun getDb(context: Context): MessagingDatabase {
|
||||
if (db == null) {
|
||||
db = MessagingDatabase(context)
|
||||
}
|
||||
return db!!
|
||||
}
|
||||
|
||||
fun sendMessage(context: Context, appToken: String, message: String) {
|
||||
val db = MessagingDatabase(context)
|
||||
val db = getDb(context)
|
||||
val connectorToken = db.getConnectorToken(appToken)
|
||||
val application = getApp(context, connectorToken, db)
|
||||
val application = getApp(context, connectorToken)
|
||||
if (application.isNullOrBlank()) {
|
||||
return
|
||||
}
|
||||
@ -51,12 +60,9 @@ fun sendUnregistered(context: Context, connectorToken: String) {
|
||||
context.sendBroadcast(broadcastIntent)
|
||||
}
|
||||
|
||||
fun getApp(context: Context,
|
||||
connectorToken: String,
|
||||
db: MessagingDatabase = MessagingDatabase(context)
|
||||
): String?{
|
||||
fun getApp(context: Context, connectorToken: String): String?{
|
||||
val db = getDb(context)
|
||||
val app = db.getPackageName(connectorToken)
|
||||
db.close()
|
||||
return if (app.isBlank()) {
|
||||
Log.w(TAG, "No app found for $connectorToken")
|
||||
null
|
||||
@ -66,8 +72,7 @@ fun getApp(context: Context,
|
||||
}
|
||||
|
||||
fun getEndpoint(context: Context, connectorToken: String): String {
|
||||
val db = MessagingDatabase(context)
|
||||
val db = getDb(context)
|
||||
val appToken = db.getAppToken(connectorToken)
|
||||
db.close()
|
||||
return "${getUrl(context)}/push/$appToken"
|
||||
}
|
@ -37,10 +37,9 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
|
||||
return
|
||||
}
|
||||
sendUnregistered(context!!.applicationContext, connectorToken)
|
||||
val db = MessagingDatabase(context.applicationContext)
|
||||
val db = getDb(context.applicationContext)
|
||||
val appToken = db.getAppToken(connectorToken)
|
||||
db.unregisterApp(connectorToken)
|
||||
db.close()
|
||||
ApiUtils().deleteApp(context.applicationContext, appToken) {
|
||||
Log.d(TAG,"Unregistration is finished")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user