Commit 896dba20 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Queries can now be exported as CSV

Implements #25
parent 006e47fb
......@@ -58,7 +58,7 @@
android:uiOptions="splitActionBarWhenNarrow"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING" />
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING"/>
</intent-filter>
</activity>
......@@ -111,7 +111,7 @@
android:exported="true"
android:process=":background">
<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.FIRE_SETTING" />
<action android:name="com.twofortyfouram.locale.intent.action.FIRE_SETTING"/>
</intent-filter>
</receiver>
......
package com.frostnerd.smokescreen.database.repository
import android.content.Context
import androidx.room.Insert
import com.frostnerd.smokescreen.database.converters.StringListConverter
import com.frostnerd.smokescreen.database.dao.DnsQueryDao
import com.frostnerd.smokescreen.database.entities.DnsQuery
import kotlinx.coroutines.*
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import java.lang.StringBuilder
/**
* Copyright Daniel Wolf 2018
......@@ -40,4 +46,36 @@ class DnsQueryRepository(val dnsQueryDao: DnsQueryDao) {
dnsQueryDao.getAll()
}.await()
}
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)
}
}
}
\ No newline at end of file
......@@ -17,6 +17,7 @@ import androidx.preference.EditTextPreference
import androidx.preference.PreferenceFragmentCompat
import com.frostnerd.general.isInt
import com.frostnerd.smokescreen.*
import com.frostnerd.smokescreen.database.getDatabase
import com.frostnerd.smokescreen.dialog.AppChoosalDialog
import com.frostnerd.smokescreen.util.preferences.Theme
......@@ -112,7 +113,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
hideIconPreference.isEnabled = false
hideIconPreference.isVisible = false
}
if(!requireContext().getPreferences().isUsingKeweon()){
if (!requireContext().getPreferences().isUsingKeweon()) {
val terminateKeweonPreference = findPreference("null_terminate_keweon")
terminateKeweonPreference.isVisible = false
terminateKeweonPreference.isEnabled = false
......@@ -121,6 +122,38 @@ class SettingsFragment : PreferenceFragmentCompat() {
processCacheCategory()
processLoggingCategory()
processNetworkCategory()
processQueryCategory()
}
private fun processQueryCategory() {
val queryLogging = findPreference("log_dns_queries")
val exportQueries = findPreference("export_dns_queries")
exportQueries.setOnPreferenceClickListener {
requireContext().getDatabase().dnsQueryRepository().exportQueriesAsCsvAsync(requireContext()) {file ->
println("File created")
val uri = FileProvider.getUriForFile(requireContext(), "com.frostnerd.smokescreen.LogZipProvider", file)
val exportIntent = Intent(Intent.ACTION_SEND)
exportIntent.putExtra(Intent.EXTRA_TEXT, "")
exportIntent.type = "text/csv"
exportIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name) + " -- Logged Queries")
for (receivingApps in requireContext().packageManager.queryIntentActivities(
exportIntent,
PackageManager.MATCH_DEFAULT_ONLY
)) {
requireContext().grantUriPermission(
receivingApps.activityInfo.packageName,
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
)
}
exportIntent.putExtra(Intent.EXTRA_STREAM, uri)
exportIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
startActivity(Intent.createChooser(exportIntent, getString(R.string.title_export_queries)))
}
println("Clicked")
true
}
}
@SuppressLint("NewApi")
......
......@@ -77,4 +77,7 @@
<string name="title_log_queries">Query logging</string>
<string name="summary_log_queries">Log all queries and their responses</string>
<string name="title_export_queries">Export queries</string>
<string name="summary_export_queries">Export queries as CSV</string>
</resources>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path path="logs/" name="logdir" />
<files-path path="queryexport/" name="queryexport" />
</paths>
\ No newline at end of file
......@@ -138,5 +138,11 @@
android:title="@string/title_log_queries"
android:summary="@string/summary_log_queries"
android:defaultValue="false"/>
<Preference
android:key="export_dns_queries"
android:title="@string/title_export_queries"
android:dependency="log_dns_queries"
android:summary="@string/summary_export_queries"/>
</PreferenceCategory>
</PreferenceScreen>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment