Commit c99cac5e authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Added server selection dialog to tasker activity

parent e3b4e9c0
...@@ -41,20 +41,25 @@ import kotlinx.coroutines.launch ...@@ -41,20 +41,25 @@ import kotlinx.coroutines.launch
*/ */
class ServerChoosalDialog( class ServerChoosalDialog(
context: AppCompatActivity, context: AppCompatActivity,
selectedServer:DnsServerInformation<*>?,
showTls:Boolean = selectedServer?.hasTlsServer() ?: true,
onEntrySelected: (config: DnsServerInformation<*>) -> Unit onEntrySelected: (config: DnsServerInformation<*>) -> Unit
) : ) :
BaseDialog(context, context.getPreferences().theme.dialogStyle) { BaseDialog(context, context.getPreferences().theme.dialogStyle) {
private var populationJob: Job? = null private var populationJob: Job? = null
private var currentSelectedServer: DnsServerInformation<*> private var currentSelectedServer: DnsServerInformation<*>?
private lateinit var defaultConfig: List<DnsServerInformation<*>> private lateinit var defaultConfig: List<DnsServerInformation<*>>
private lateinit var userConfig: List<UserServerConfiguration> private lateinit var userConfig: List<UserServerConfiguration>
constructor(context: AppCompatActivity,
onEntrySelected: (config: DnsServerInformation<*>) -> Unit):this(context, context.getPreferences().dnsServerConfig, onEntrySelected=onEntrySelected)
init { init {
val view = layoutInflater.inflate(R.layout.dialog_server_configuration, null, false) val view = layoutInflater.inflate(R.layout.dialog_server_configuration, null, false)
setTitle(R.string.dialog_serverconfiguration_title) setTitle(R.string.dialog_serverconfiguration_title)
setView(view) setView(view)
currentSelectedServer = context.getPreferences().dnsServerConfig currentSelectedServer = selectedServer
setButton( setButton(
DialogInterface.BUTTON_NEUTRAL, context.getString(android.R.string.cancel) DialogInterface.BUTTON_NEUTRAL, context.getString(android.R.string.cancel)
...@@ -62,10 +67,9 @@ class ServerChoosalDialog( ...@@ -62,10 +67,9 @@ class ServerChoosalDialog(
setButton( setButton(
DialogInterface.BUTTON_POSITIVE, context.getString(android.R.string.ok) DialogInterface.BUTTON_POSITIVE, context.getString(android.R.string.ok)
) { _, _ -> ) { _, _ ->
onEntrySelected.invoke(currentSelectedServer) currentSelectedServer?.apply(onEntrySelected)
} }
val isCurrentServerTls = currentSelectedServer.hasTlsServer() loadServerData(showTls)
loadServerData(isCurrentServerTls)
val spinnerAdapter = ArrayAdapter<String>( val spinnerAdapter = ArrayAdapter<String>(
context, android.R.layout.simple_spinner_item, context, android.R.layout.simple_spinner_item,
...@@ -77,7 +81,7 @@ class ServerChoosalDialog( ...@@ -77,7 +81,7 @@ class ServerChoosalDialog(
spinnerAdapter.setDropDownViewResource(R.layout.item_tasker_action_spinner_dropdown_item) spinnerAdapter.setDropDownViewResource(R.layout.item_tasker_action_spinner_dropdown_item)
val spinner = view.findViewById<Spinner>(R.id.spinner) val spinner = view.findViewById<Spinner>(R.id.spinner)
spinner.adapter = spinnerAdapter spinner.adapter = spinnerAdapter
if(isCurrentServerTls) spinner.setSelection(1) if(showTls) spinner.setSelection(1)
view.findViewById<RadioGroup>(R.id.knownServersGroup).setOnCheckedChangeListener { group, _ -> view.findViewById<RadioGroup>(R.id.knownServersGroup).setOnCheckedChangeListener { group, _ ->
val button = view.findViewById(group.checkedRadioButtonId) as RadioButton val button = view.findViewById(group.checkedRadioButtonId) as RadioButton
val payload = button.tag val payload = button.tag
...@@ -174,6 +178,7 @@ class ServerChoosalDialog( ...@@ -174,6 +178,7 @@ class ServerChoosalDialog(
} }
private fun markCurrentSelectedServer() { private fun markCurrentSelectedServer() {
val currentSelectedServer = this.currentSelectedServer ?: return
for (id in 0 until knownServersGroup.childCount) { for (id in 0 until knownServersGroup.childCount) {
val child = knownServersGroup.getChildAt(id) as RadioButton val child = knownServersGroup.getChildAt(id) as RadioButton
val payload = child.tag val payload = child.tag
...@@ -299,7 +304,7 @@ class ServerChoosalDialog( ...@@ -299,7 +304,7 @@ class ServerChoosalDialog(
currentSelectedServer = currentSelectedServer =
if (userConfiguration.isHttpsServer()) AbstractHttpsDNSHandle.KNOWN_DNS_SERVERS.minBy { it.key }!!.value else AbstractTLSDnsHandle.KNOWN_DNS_SERVERS.minBy { it.key }!!.value if (userConfiguration.isHttpsServer()) AbstractHttpsDNSHandle.KNOWN_DNS_SERVERS.minBy { it.key }!!.value else AbstractTLSDnsHandle.KNOWN_DNS_SERVERS.minBy { it.key }!!.value
markCurrentSelectedServer() markCurrentSelectedServer()
context.getPreferences().dnsServerConfig = currentSelectedServer context.getPreferences().dnsServerConfig = currentSelectedServer!!
} }
knownServersGroup.removeView(button) knownServersGroup.removeView(button)
}.show() }.show()
...@@ -348,7 +353,7 @@ class ServerChoosalDialog( ...@@ -348,7 +353,7 @@ class ServerChoosalDialog(
currentSelectedServer = currentSelectedServer =
if (isHttps) AbstractHttpsDNSHandle.KNOWN_DNS_SERVERS.minBy { it.key }!!.value else AbstractTLSDnsHandle.KNOWN_DNS_SERVERS.minBy { it.key }!!.value if (isHttps) AbstractHttpsDNSHandle.KNOWN_DNS_SERVERS.minBy { it.key }!!.value else AbstractTLSDnsHandle.KNOWN_DNS_SERVERS.minBy { it.key }!!.value
markCurrentSelectedServer() markCurrentSelectedServer()
context.getPreferences().dnsServerConfig = currentSelectedServer context.getPreferences().dnsServerConfig = currentSelectedServer!!
} }
knownServersGroup.removeView(button) knownServersGroup.removeView(button)
}.show() }.show()
......
...@@ -12,12 +12,10 @@ import android.widget.ArrayAdapter ...@@ -12,12 +12,10 @@ import android.widget.ArrayAdapter
import com.frostnerd.dnstunnelproxy.DnsServerInformation import com.frostnerd.dnstunnelproxy.DnsServerInformation
import com.frostnerd.encrypteddnstunnelproxy.HttpsDnsServerInformation import com.frostnerd.encrypteddnstunnelproxy.HttpsDnsServerInformation
import com.frostnerd.lifecyclemanagement.BaseActivity import com.frostnerd.lifecyclemanagement.BaseActivity
import com.frostnerd.smokescreen.R import com.frostnerd.smokescreen.*
import com.frostnerd.smokescreen.activity.BackgroundVpnConfigureActivity import com.frostnerd.smokescreen.activity.BackgroundVpnConfigureActivity
import com.frostnerd.smokescreen.dialog.NewServerDialog import com.frostnerd.smokescreen.dialog.NewServerDialog
import com.frostnerd.smokescreen.fromServerUrls import com.frostnerd.smokescreen.dialog.ServerChoosalDialog
import com.frostnerd.smokescreen.hasHttpsServer
import com.frostnerd.smokescreen.tlsServerFromHosts
import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout import com.google.android.material.textfield.TextInputLayout
import kotlinx.android.synthetic.main.activity_tasker_configure.* import kotlinx.android.synthetic.main.activity_tasker_configure.*
...@@ -157,6 +155,15 @@ class ConfigureActivity : BaseActivity() { ...@@ -157,6 +155,15 @@ class ConfigureActivity : BaseActivity() {
setHints() setHints()
} }
} }
selectServer.setOnClickListener {
ServerChoosalDialog(this@ConfigureActivity, null, serverType.selectedItemPosition == 1) {
val typePosition = if(it.hasTlsServer()) 1 else 0
if(serverType.selectedItemPosition != typePosition) serverType.setSelection(typePosition)
primaryServer.setText(it.servers.first().address.formatToString())
if(it.servers.size > 1) secondaryServer.setText(it.servers.last().address.formatToString())
else secondaryServer.setText("")
}.show()
}
addUrlTextWatcher(primaryServerWrap, primaryServer, false) addUrlTextWatcher(primaryServerWrap, primaryServer, false)
addUrlTextWatcher(secondaryServerWrap, secondaryServer, true) addUrlTextWatcher(secondaryServerWrap, secondaryServer, true)
setHints() setHints()
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingLeft="@dimen/dialog_horizontal_margin" android:orientation="vertical"
android:paddingRight="@dimen/dialog_horizontal_margin" android:paddingLeft="@dimen/dialog_horizontal_margin"
android:paddingTop="@dimen/dialog_vertical_margin" android:paddingTop="@dimen/dialog_vertical_margin"
android:paddingBottom="@dimen/dialog_vertical_margin" android:paddingRight="@dimen/dialog_horizontal_margin"
android:layout_height="match_parent"> android:paddingBottom="@dimen/dialog_vertical_margin">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:orientation="vertical" android:layout_height="wrap_content"
android:layout_height="wrap_content"> android:orientation="vertical">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:orientation="horizontal" android:layout_height="wrap_content"
android:layout_height="wrap_content"> android:orientation="horizontal">
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:text="@string/tasker_action" android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginEnd="12dp" android:layout_marginEnd="12dp"
android:layout_height="wrap_content"/> android:text="@string/tasker_action"
android:textStyle="bold" />
<Spinner <Spinner
android:layout_width="wrap_content"
android:id="@+id/actionType" android:id="@+id/actionType"
android:layout_height="wrap_content"/> android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="match_parent"
android:id="@+id/startConfigWrap" android:id="@+id/startConfigWrap"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="28dp" android:layout_marginTop="28dp"
android:layout_height="wrap_content"> android:orientation="vertical">
<CheckBox <CheckBox
android:id="@+id/startIfRunning"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:text="@string/tasker_start_if_running" android:layout_height="wrap_content"
android:textColor="?android:attr/textColor"
android:checked="true" android:checked="true"
android:id="@+id/startIfRunning" android:text="@string/tasker_start_if_running"
android:layout_height="wrap_content"/> android:textColor="?android:attr/textColor" />
<CheckBox <CheckBox
android:id="@+id/useCustomServer"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:text="@string/tasker_use_custom_server" android:layout_height="wrap_content"
android:textColor="?android:attr/textColor"
android:checked="false" android:checked="false"
android:id="@+id/useCustomServer" android:text="@string/tasker_use_custom_server"
android:layout_height="wrap_content"/> android:textColor="?android:attr/textColor" />
<Spinner <Spinner
android:layout_width="match_parent"
android:id="@+id/serverType" android:id="@+id/serverType"
android:visibility="gone" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp" android:layout_marginTop="12dp"
android:layout_height="wrap_content"/> android:visibility="gone" />
<LinearLayout <LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:id="@+id/serverConfigWrap" android:id="@+id/serverConfigWrap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp" android:layout_marginTop="18dp"
android:visibility="gone" android:orientation="vertical"
android:layout_height="wrap_content"> android:visibility="gone">
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:id="@+id/primaryServerWrap" android:id="@+id/primaryServerWrap"
app:errorEnabled="true" android:layout_width="match_parent"
android:textColorHint="?android:attr/textColorSecondary" android:layout_height="wrap_content"
android:hint="@string/dialog_newserver_primaryserver" android:hint="@string/dialog_newserver_primaryserver"
android:layout_height="wrap_content"> android:textColorHint="?android:attr/textColorSecondary"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:id="@+id/primaryServer" android:id="@+id/primaryServer"
android:maxLines="1" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext" android:imeOptions="actionNext"
android:inputType="textUri" android:inputType="textUri"
android:nextFocusDown="@id/secondaryServer" android:maxLines="1"
android:layout_height="wrap_content"/> android:nextFocusDown="@id/secondaryServer" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:id="@+id/secondaryServerWrap" android:id="@+id/secondaryServerWrap"
app:errorEnabled="true" android:layout_width="match_parent"
android:textColorHint="?android:attr/textColorSecondary" android:layout_height="wrap_content"
android:hint="@string/dialog_newserver_secondaryserver" android:hint="@string/dialog_newserver_secondaryserver"
android:layout_height="wrap_content"> android:textColorHint="?android:attr/textColorSecondary"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:id="@+id/secondaryServer" android:id="@+id/secondaryServer"
android:maxLines="1" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext" android:imeOptions="actionNext"
android:inputType="textUri" android:inputType="textUri"
android:nextFocusUp="@id/primaryServer" android:maxLines="1"
android:layout_height="wrap_content"/> android:nextFocusUp="@id/primaryServer" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<ImageButton
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/selectServer"
android:background="@drawable/main_roundbuttons"
android:contentDescription="@string/contentdescription_main_servericon"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_main_server"
android:tint="?android:attr/textColor" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
......
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