@@ -110,7 +110,7 @@ import okhttp3.internal.closeQuietly
110110import java.net.InetSocketAddress
111111import java.net.Socket
112112import java.net.UnknownHostException
113- import java.util.Collections
113+ import java.util.concurrent.ConcurrentHashMap
114114import java.util.concurrent.ConcurrentLinkedQueue
115115import java.util.concurrent.atomic.AtomicInteger
116116import java.util.zip.ZipInputStream
@@ -608,22 +608,30 @@ class ConfigurationFragment @JvmOverloads constructor(
608608 lateinit var cancel: () -> Unit
609609 lateinit var minimize: () -> Unit
610610
611- var dialogHidden = false
611+ val dialogStatus = AtomicInteger ( 0 ) // 1: hidden 2: cancelled
612612 var notification: ConnectionTestNotification ? = null
613613
614- val results = Collections .synchronizedSet(mutableSetOf< ProxyEntity >() )
614+ val results: MutableSet < ProxyEntity > = ConcurrentHashMap .newKeySet( )
615615 var proxyN = 0
616616 val finishedN = AtomicInteger (0 )
617617
618618 fun update (profile : ProxyEntity ) {
619- results.add(profile)
619+ if (dialogStatus.get() != 2 ) {
620+ results.add(profile)
621+ }
620622 runOnMainDispatcher {
621623 val context = context ? : return @runOnMainDispatcher
624+ val progress = finishedN.addAndGet(1 )
625+ val status = dialogStatus.get()
626+ notification?.updateNotification(
627+ progress,
628+ proxyN,
629+ progress >= proxyN || status == 2
630+ )
631+ if (status >= 1 ) return @runOnMainDispatcher
622632 if (! isAdded) return @runOnMainDispatcher
623633
624- val progress = finishedN.addAndGet(1 )
625- notification?.updateNotification(progress, proxyN, progress >= proxyN)
626- if (dialogHidden) return @runOnMainDispatcher
634+ // refresh dialog
627635
628636 var profileStatusText: String? = null
629637 var profileStatusColor = 0
@@ -797,8 +805,11 @@ class ConfigurationFragment @JvmOverloads constructor(
797805 }
798806 }
799807 test.cancel = {
808+ test.dialogStatus.set(2 )
800809 dialog.dismiss()
801810 runOnDefaultDispatcher {
811+ mainJob.cancel()
812+ testJobs.forEach { it.cancel() }
802813 test.results.forEach {
803814 try {
804815 ProfileManager .updateProfile(it)
@@ -807,13 +818,11 @@ class ConfigurationFragment @JvmOverloads constructor(
807818 }
808819 }
809820 GroupManager .postReload(DataStore .currentGroupId())
810- mainJob.cancel()
811- testJobs.forEach { it.cancel() }
812821 DataStore .runningTest = false
813822 }
814823 }
815824 test.minimize = {
816- test.dialogHidden = true
825+ test.dialogStatus.set( 1 )
817826 test.notification = ConnectionTestNotification (
818827 dialog.context,
819828 " [${group.displayName()} ] ${getString(R .string.connection_test)} "
@@ -865,8 +874,11 @@ class ConfigurationFragment @JvmOverloads constructor(
865874 }
866875 }
867876 test.cancel = {
877+ test.dialogStatus.set(2 )
868878 dialog.dismiss()
869879 runOnDefaultDispatcher {
880+ mainJob.cancel()
881+ testJobs.forEach { it.cancel() }
870882 test.results.forEach {
871883 try {
872884 ProfileManager .updateProfile(it)
@@ -875,13 +887,11 @@ class ConfigurationFragment @JvmOverloads constructor(
875887 }
876888 }
877889 GroupManager .postReload(DataStore .currentGroupId())
878- mainJob.cancel()
879- testJobs.forEach { it.cancel() }
880890 DataStore .runningTest = false
881891 }
882892 }
883893 test.minimize = {
884- test.dialogHidden = true
894+ test.dialogStatus.set( 1 )
885895 test.notification = ConnectionTestNotification (
886896 dialog.context,
887897 " [${group.displayName()} ] ${getString(R .string.connection_test)} "
0 commit comments