DnsQueryRepository.kt 3.09 KB
Newer Older
1
2
package com.frostnerd.smokescreen.database.repository

3
import android.content.Context
4
import androidx.room.Insert
5
import com.frostnerd.smokescreen.database.converters.StringListConverter
6
7
8
import com.frostnerd.smokescreen.database.dao.DnsQueryDao
import com.frostnerd.smokescreen.database.entities.DnsQuery
import kotlinx.coroutines.*
9
10
11
12
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import java.lang.StringBuilder
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

/**
 * Copyright Daniel Wolf 2018
 * All rights reserved.
 * Code may NOT be used without proper permission, neither in binary nor in source form.
 * All redistributions of this software in source code must retain this copyright header
 * All redistributions of this software in binary form must visibly inform users about usage of this software
 *
 * development@frostnerd.com
 */

class DnsQueryRepository(val dnsQueryDao: DnsQueryDao) {

    fun updateAsync(dnsQuery: DnsQuery): Job {
        return GlobalScope.launch {
            dnsQueryDao.update(dnsQuery)
        }
    }

    fun insertAllAsync(dnsQueries:List<DnsQuery>): Job {
        return GlobalScope.launch {
            dnsQueryDao.insertAll(dnsQueries)
        }
    }

    fun insertAsync(dnsQuery:DnsQuery): Job {
        return GlobalScope.launch {
            dnsQueryDao.insert(dnsQuery)
        }
    }

    suspend fun getAllAsync(coroutineScope: CoroutineScope): List<DnsQuery> {
        return coroutineScope.async(start = CoroutineStart.DEFAULT) {
            dnsQueryDao.getAll()
        }.await()
    }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

    fun exportQueriesAsCsvAsync(context: Context, fileReadyCallback:(createdFile: File) ->Unit):Job {
        val exportDir = File(context.filesDir, "queryexport/")
        exportDir.mkdirs()
        val exportFile = File(exportDir, "queries.csv")
        exportFile.delete()
        return GlobalScope.launch {
            val all = getAllAsync(this)
            val outStream = BufferedWriter(FileWriter(exportFile))
            outStream.write("Name,Short Name,Type Name,Type ID,Asked Server,Answered from Cache,Question time,Response Time,Responses(JSON-Array of Base64)")
            outStream.newLine()
            val builder = StringBuilder()
            val responseConverter = StringListConverter()
            for (query in all) {
                builder.append(query.name).append(",")
                builder.append(query.shortName).append(",")
                builder.append(query.type.name).append(",")
                builder.append(query.type.value).append(",")
                builder.append(query.askedServer).append(",")
                builder.append(query.fromCache).append(",")
                builder.append(query.questionTime).append(",")
                builder.append(query.responseTime).append(",")
                builder.append("\"").append(responseConverter.someObjectListToString(query.responses).replace(",", ";").replace("\"", "'")).append("\"")
                outStream.write(builder.toString())
                outStream.newLine()
                outStream.flush()
                builder.clear()
            }
            outStream.close()
            fileReadyCallback(exportFile)
        }
    }
81
}