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

3
4
import android.content.Context
import com.frostnerd.smokescreen.database.converters.StringListConverter
5
6
7
import com.frostnerd.smokescreen.database.dao.DnsQueryDao
import com.frostnerd.smokescreen.database.entities.DnsQuery
import kotlinx.coroutines.*
8
9
10
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
11

Daniel Wolf's avatar
Daniel Wolf committed
12
13
/*
 * Copyright (C) 2019 Daniel Wolf (Ch4t4r)
14
 *
Daniel Wolf's avatar
Daniel Wolf committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can contact the developer at daniel.wolf@frostnerd.com.
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 */

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()
    }
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
81
82
83
84
85
86
87

    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)
        }
    }
88
}