Commit 2ae8425d authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Added functionality to the DnsRuleDialog

parent 48bf1b69
......@@ -157,9 +157,10 @@ class DnsRuleActivity : BaseActivity() {
else -> it
}
}
userRuleCount += 1
getDatabase().dnsRuleRepository().insertAsync(newRule)
userDnsRules.add(insertPos, newRule)
if(showUserRules) {
userRuleCount += 1
sourceAdapter.notifyItemInserted(sourceAdapterList.size + 1 + insertPos)
}
}).show()
......
......@@ -27,6 +27,9 @@ import org.minidns.record.Record
@TypeConverters(DnsTypeConverter::class)
interface DnsRuleDao {
@Insert
fun insert(dnsRule: DnsRule)
@Query("DELETE FROM DnsRule WHERE importedFrom IS NULL")
fun deleteAllUserRules()
......
......@@ -28,6 +28,12 @@ import kotlinx.coroutines.launch
*/
class DnsRuleRepository(val dnsRuleDao: DnsRuleDao) {
fun insertAsync(dnsRule: DnsRule, coroutineScope: CoroutineScope = GlobalScope) {
coroutineScope.launch {
dnsRuleDao.insert(dnsRule)
}
}
fun deleteAllUserRulesAsync(coroutineScope: CoroutineScope = GlobalScope) {
coroutineScope.launch {
dnsRuleDao.deleteAllUserRules()
......
package com.frostnerd.smokescreen.dialog
import android.content.Context
import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog
import com.frostnerd.smokescreen.R
import com.frostnerd.smokescreen.database.entities.DnsRule
import com.frostnerd.smokescreen.getPreferences
import kotlinx.android.synthetic.main.dialog_create_dnsrule.view.*
import kotlinx.android.synthetic.main.dialog_create_dnsrule.view.ipv4Address
import org.minidns.record.Record
import java.lang.Exception
import java.net.Inet4Address
import java.net.Inet6Address
/*
* Copyright (C) 2019 Daniel Wolf (Ch4t4r)
......@@ -23,4 +31,68 @@ import com.frostnerd.smokescreen.getPreferences
*
* You can contact the developer at daniel.wolf@frostnerd.com.
*/
class DnsRuleDialog(context:Context, dnsRule: DnsRule? = null, onRuleCreated:(DnsRule) -> Unit): AlertDialog(context, context.getPreferences().theme.dialogStyle)
\ No newline at end of file
class DnsRuleDialog(context:Context, dnsRule: DnsRule? = null, onRuleCreated:(DnsRule) -> Unit): AlertDialog(context, context.getPreferences().theme.dialogStyle) {
init {
val view = layoutInflater.inflate(R.layout.dialog_create_dnsrule, null, false)
setView(view)
setTitle(R.string.dialog_newdnsrule_title)
setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.cancel)) { dialog, _ ->
dialog.dismiss()
}
setButton(DialogInterface.BUTTON_POSITIVE, context.getString(android.R.string.ok)) { _, _ ->
}
setOnShowListener {
getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
var valid = true
view.host.error = null
view.ipv4Til.error = null
view.ipv6Til.error = null
if(view.host.text.isNullOrBlank()) {
view.host.error = context.getString(R.string.dialog_newdnsrule_host_invalid)
valid = false
} else {
val ipv4Valid = isIpv4Address(view.ipv4Address.text.toString())
val ipv6Valid = isIpv6Address(view.ipv6Address.text.toString())
val bothEmpty = view.ipv4Address.text.isNullOrEmpty() && view.ipv6Address.text.isNullOrEmpty()
if(!ipv4Valid || bothEmpty) {
valid = false
view.ipv4Address.error = context.getString(R.string.dialog_newdnsrule_ipv4_invalid)
}
if(!ipv6Valid || bothEmpty) {
valid = false
view.ipv6Address.error = context.getString(R.string.dialog_newdnsrule_ipv6_invalid)
}
}
if (valid) {
dismiss()
onRuleCreated(
dnsRule?.copy(host = view.host.text.toString(), target = view.ipv4Address.text.toString())
?: DnsRule(Record.TYPE.A, view.host.text.toString(), view.ipv4Address.text.toString())
)
}
}
}
if(dnsRule != null) {
view.host.setText(dnsRule.host)
}
}
private fun isIpv4Address(text:String?): Boolean {
return text.isNullOrBlank() || try {
Inet4Address.getByName(text)
true
} catch (ex:Exception) {
false
}
}
private fun isIpv6Address(text:String?): Boolean {
return text.isNullOrBlank() || try {
Inet6Address.getByName(text)
true
} catch (ex:Exception) {
false
}
}
}
\ No newline at end of file
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>
......@@ -39,7 +39,7 @@
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:id="@+id/addSource"
android:src="@drawable/ic_plus"
android:src="@drawable/ic_add"
android:elevation="0dp"
android:layout_height="wrap_content"/>
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:paddingLeft="@dimen/dialog_horizontal_margin"
android:paddingRight="@dimen/dialog_horizontal_margin"
android:paddingTop="@dimen/dialog_vertical_margin"
android:paddingBottom="@dimen/dialog_vertical_margin"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:id="@+id/hostTil"
app:errorEnabled="true"
android:textColorHint="?android:attr/textColorSecondary"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:hint="@string/dialog_newdnsrule_hint_host"
android:id="@+id/host"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:id="@+id/ipv4Til"
app:errorEnabled="true"
android:textColorHint="?android:attr/textColorSecondary"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:hint="@string/dialog_newdnsrule_hint_ipv4address"
android:id="@+id/ipv4Address"
android:text="0.0.0.0"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:id="@+id/ipv6Til"
app:errorEnabled="true"
android:textColorHint="?android:attr/textColorSecondary"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:hint="@string/dialog_newdnsrule_hint_ipv6address"
android:id="@+id/ipv6Address"
android:text="::1"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
\ No newline at end of file
......@@ -81,9 +81,8 @@
<ImageButton
android:layout_width="48dp"
style="@style/Widget.AppCompat.Button.Borderless"
android:src="@drawable/ic_plus"
android:src="@drawable/ic_add"
android:id="@+id/add"
android:scaleType="fitXY"
android:tint="?android:attr/textColor"
android:layout_height="48dp"/>
</LinearLayout>
......
......@@ -122,4 +122,12 @@
<string name="dialog_newhostsource_url">URL</string>
<string name="dialog_newhostsource_error_name_empty">Please provide a name.</string>
<string name="dialog_newhostsource_url_invalid">Please provide a valid url.</string>
<string name="dialog_newdnsrule_title">New Dns rule</string>
<string name="dialog_newdnsrule_hint_host">Host</string>
<string name="dialog_newdnsrule_hint_ipv4address">IPv4 address</string>
<string name="dialog_newdnsrule_hint_ipv6address">Ipv6 address</string>
<string name="dialog_newdnsrule_host_invalid">Please provide a valid host</string>
<string name="dialog_newdnsrule_ipv4_invalid">Please provide a valid IPv4 address</string>
<string name="dialog_newdnsrule_ipv6_invalid">Please provide a valid IPv6 address</string>
</resources>
\ 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