Commit 19b09b61 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Only dismiss dialog if it is showing

parent c7335098
package com.frostnerd.smokescreen
import android.app.Activity
import android.app.AlarmManager
import android.app.KeyguardManager
import android.app.PendingIntent
import android.app.*
import android.content.*
import android.hardware.fingerprint.FingerprintManager
import android.net.ConnectivityManager
......@@ -142,7 +139,7 @@ fun Context.registerLocalReceiver(
fun AppCompatActivity.registerLocalReceiver(
filteredActions: List<String>,
unregisterOnDestroy:Boolean,
unregisterOnDestroy: Boolean,
receiver: (intent: Intent?) -> Unit
): BroadcastReceiver {
val filter = IntentFilter()
......@@ -157,7 +154,7 @@ fun AppCompatActivity.registerLocalReceiver(
}
val mgr = LocalBroadcastManager.getInstance(this)
mgr.registerReceiver(actualReceiver, filter)
if(unregisterOnDestroy) lifecycle.addObserver(object:LifecycleObserver {
if(unregisterOnDestroy) lifecycle.addObserver(object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
@Suppress("unused")
fun onDestroy() {
......@@ -189,13 +186,22 @@ fun Context.isAppBatteryOptimized(): Boolean {
return !pwrm.isIgnoringBatteryOptimizations(packageName)
}
fun <T:Activity>Activity.restart(activityClass:Class<T>? = null, exitProcess:Boolean = false) {
fun <T : Activity>Activity.restart(activityClass: Class<T>? = null, exitProcess: Boolean = false) {
val intent = (if(activityClass != null) Intent(this, activityClass) else intent)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_ANIMATION or Intent.FLAG_ACTIVITY_CLEAR_TASK)
if(exitProcess) {
finish()
val pendingIntent = PendingIntent.getActivity(this, RequestCodes.RESTART_WHOLE_APP, intent, PendingIntent.FLAG_CANCEL_CURRENT)
(getSystemService(Context.ALARM_SERVICE) as AlarmManager).setExact(AlarmManager.RTC, System.currentTimeMillis() + 800, pendingIntent)
val pendingIntent = PendingIntent.getActivity(
this,
RequestCodes.RESTART_WHOLE_APP,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
)
(getSystemService(Context.ALARM_SERVICE) as AlarmManager).setExact(
AlarmManager.RTC,
System.currentTimeMillis() + 800,
pendingIntent
)
kotlin.system.exitProcess(0)
} else {
finish()
......@@ -223,7 +229,7 @@ fun Context.hasDeviceIpv4Address(): Boolean {
if(network == null) continue
val info = try {
mgr.getNetworkInfo(network)
} catch (ex:NullPointerException) {
} catch (ex: NullPointerException) {
// Android seems to love to throw NullPointerException with getNetworkInfo() - completely out of our control.
log("Exception when trying to determine IPv4 capability: $ex")
null
......@@ -252,7 +258,7 @@ fun Context.hasDeviceIpv6Address(): Boolean {
if(network == null) continue
val info = try {
mgr.getNetworkInfo(network)
} catch (ex:NullPointerException) {
} catch (ex: NullPointerException) {
// Android seems to love to throw NullPointerException with getNetworkInfo() - completely out of our control.
log("Exception when trying to determine IPv6 capability: $ex")
null
......@@ -274,13 +280,13 @@ fun Context.hasDeviceIpv6Address(): Boolean {
return !hasNetwork
}
fun Context.getLanIP(ipv4:Boolean): InetAddress? {
fun Context.getLanIP(ipv4: Boolean): InetAddress? {
val mgr = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
for (network in mgr.allNetworks) {
if(network == null) continue
val info = try {
mgr.getNetworkInfo(network)
} catch (ex:NullPointerException) {
} catch (ex: NullPointerException) {
// Android seems to love to throw NullPointerException with getNetworkInfo() - completely out of our control.
log("Exception when trying to determine IPv6 capability: $ex")
null
......@@ -301,7 +307,7 @@ fun Context.isDeviceRooted():Boolean {
return RootChecker(this, BuildInfoProvider(), NoOpLogger.getInstance()).isDeviceRooted
}
fun Context.clearPreviousIptablesRedirect(forceClear:Boolean = false) {
fun Context.clearPreviousIptablesRedirect(forceClear: Boolean = false) {
if(forceClear || !isServiceRunning(DnsVpnService::class.java) || getPreferences().vpnServiceState == VpnServiceState.STOPPED) {
val ipv4 = getPreferences().lastIptablesRedirectAddress?.split(":")?.let {
it[0] to it[1].toInt()
......@@ -321,7 +327,7 @@ fun Context.clearPreviousIptablesRedirect(forceClear:Boolean = false) {
}
}
operator fun Level.compareTo(otherLevel:Level):Int {
operator fun Level.compareTo(otherLevel: Level):Int {
return this.intValue() - otherLevel.intValue()
}
......@@ -330,20 +336,28 @@ val DnsServerInformation<*>.type
fun DnsServerInformation<*>.toJson():String {
return when(type) {
ServerType.DOH -> HttpsDnsServerInformationTypeAdapter().toJson(this as HttpsDnsServerInformation)
ServerType.DOH -> HttpsDnsServerInformationTypeAdapter().toJson(this as HttpsDnsServerInformation)
ServerType.DOT, ServerType.DOQ -> DnsServerInformationTypeAdapter().toJson(this)
}
}
fun HttpsDnsServerInformation.Companion.fromServerUrls(primaryUrl:String, secondaryUrl:String?): HttpsDnsServerInformation {
fun HttpsDnsServerInformation.Companion.fromServerUrls(primaryUrl: String, secondaryUrl: String?): HttpsDnsServerInformation {
val serverInfo = mutableListOf<HttpsDnsServerConfiguration>()
val requestType = mapOf(RequestType.WIREFORMAT_POST to ResponseType.WIREFORMAT)
serverInfo.add(
HttpsDnsServerConfiguration(address = createHttpsUpstreamAddress(primaryUrl), experimental = false, requestTypes = requestType)
HttpsDnsServerConfiguration(
address = createHttpsUpstreamAddress(primaryUrl),
experimental = false,
requestTypes = requestType
)
)
if(secondaryUrl != null)
serverInfo.add(
HttpsDnsServerConfiguration(address = createHttpsUpstreamAddress(secondaryUrl), experimental = false, requestTypes = requestType)
HttpsDnsServerConfiguration(
address = createHttpsUpstreamAddress(secondaryUrl),
experimental = false,
requestTypes = requestType
)
)
return HttpsDnsServerInformation(
"shortcutServer",
......@@ -358,14 +372,28 @@ fun HttpsDnsServerInformation.Companion.fromServerUrls(primaryUrl:String, second
)
}
fun tlsServerFromHosts(primaryHost:String, secondaryHost:String?): DnsServerInformation<TLSUpstreamAddress> {
fun tlsServerFromHosts(primaryHost: String, secondaryHost: String?): DnsServerInformation<TLSUpstreamAddress> {
val serverInfo = mutableListOf<DnsServerConfiguration<TLSUpstreamAddress>>()
serverInfo.add(
DnsServerConfiguration(address = createTlsUpstreamAddress(primaryHost), experimental = false, preferredProtocol = TLS, supportedProtocols = listOf(TLS))
DnsServerConfiguration(
address = createTlsUpstreamAddress(primaryHost),
experimental = false,
preferredProtocol = TLS,
supportedProtocols = listOf(
TLS
)
)
)
if(secondaryHost != null)
serverInfo.add(
DnsServerConfiguration(address = createTlsUpstreamAddress(secondaryHost), experimental = false, preferredProtocol = TLS, supportedProtocols = listOf(TLS))
DnsServerConfiguration(
address = createTlsUpstreamAddress(secondaryHost),
experimental = false,
preferredProtocol = TLS,
supportedProtocols = listOf(
TLS
)
)
)
return DnsServerInformation(
"shortcutServer",
......@@ -412,7 +440,7 @@ private fun createTlsUpstreamAddress(host: String): TLSUpstreamAddress {
else TLSUpstreamAddress(parsedHost)
}
fun String.equalsAny(vararg options:String, ignoreCase:Boolean = false):Boolean {
fun String.equalsAny(vararg options: String, ignoreCase: Boolean = false):Boolean {
return options.any {
it.equals(this, ignoreCase)
}
......@@ -430,7 +458,7 @@ val Context.isPrivateDnsActive: Boolean
}
}
fun Context.tryViewUri(withLink:String, alternativeLink:String? = null) {
fun Context.tryViewUri(withLink: String, alternativeLink: String? = null) {
try {
startActivity(
Intent(
......@@ -464,7 +492,7 @@ fun Context.askOpenFAQ(topic: FAQTopic?) {
)
}
fun Context.openFAQ(topic:FAQTopic?) {
fun Context.openFAQ(topic: FAQTopic?) {
tryViewUri("https://nebulo.app/faq#${topic?.id ?: ""}")
}
......@@ -479,4 +507,12 @@ fun enableResourceCloseWatcher() {
} catch (e: ReflectiveOperationException) {
throw RuntimeException(e)
}
}
fun Dialog.dismissIfShowing() {
if(isShowing) {
try {
dismiss()
} catch (ignored:Throwable){}
}
}
\ No newline at end of file
......@@ -16,9 +16,9 @@ import com.frostnerd.encrypteddnstunnelproxy.tls.AbstractTLSDnsHandle
import com.frostnerd.encrypteddnstunnelproxy.tls.TLS
import com.frostnerd.encrypteddnstunnelproxy.tls.TLSUpstreamAddress
import com.frostnerd.lifecyclemanagement.BaseDialog
import com.frostnerd.smokescreen.*
import com.frostnerd.smokescreen.BuildConfig
import com.frostnerd.smokescreen.R
import com.frostnerd.smokescreen.getPreferences
import com.frostnerd.smokescreen.log
import com.frostnerd.smokescreen.util.ServerType
import com.frostnerd.smokescreen.util.preferences.UserServerConfiguration
import com.google.android.material.textfield.TextInputEditText
......@@ -34,7 +34,6 @@ import org.minidns.dnsmessage.DnsMessage
import org.minidns.dnsmessage.Question
import org.minidns.record.Record
import java.util.concurrent.TimeUnit
import com.frostnerd.smokescreen.BuildConfig
/*
* Copyright (C) 2019 Daniel Wolf (Ch4t4r)
......@@ -348,7 +347,7 @@ class NewServerDialog(
))
}
GlobalScope.launch(Dispatchers.Main) {
dialog.dismiss()
dialog.dismissIfShowing()
onServerAdded.invoke(
HttpsDnsServerInformation(
name,
......
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