@@ -7,7 +7,6 @@ import android.app.Application
7
7
import android.app.Notification
8
8
import android.app.NotificationChannel
9
9
import android.app.PendingIntent
10
- import android.content.BroadcastReceiver
11
10
import android.content.Context
12
11
import android.content.Intent
13
12
import android.content.IntentFilter
@@ -37,11 +36,6 @@ import com.tailscale.ipn.ui.viewModel.VpnViewModel
37
36
import com.tailscale.ipn.ui.viewModel.VpnViewModelFactory
38
37
import com.tailscale.ipn.util.FeatureFlags
39
38
import com.tailscale.ipn.util.TSLog
40
- import java.io.File
41
- import java.io.IOException
42
- import java.net.NetworkInterface
43
- import java.security.GeneralSecurityException
44
- import java.util.Locale
45
39
import kotlinx.coroutines.CoroutineScope
46
40
import kotlinx.coroutines.Dispatchers
47
41
import kotlinx.coroutines.SupervisorJob
@@ -53,6 +47,11 @@ import kotlinx.coroutines.launch
53
47
import kotlinx.serialization.encodeToString
54
48
import kotlinx.serialization.json.Json
55
49
import libtailscale.Libtailscale
50
+ import java.io.File
51
+ import java.io.IOException
52
+ import java.net.NetworkInterface
53
+ import java.security.GeneralSecurityException
54
+ import java.util.Locale
56
55
57
56
class App : UninitializedApp (), libtailscale.AppContext, ViewModelStoreOwner {
58
57
val applicationScope = CoroutineScope (SupervisorJob () + Dispatchers .Default )
@@ -470,25 +469,15 @@ open class UninitializedApp : Application() {
470
469
}
471
470
472
471
fun restartVPN () {
473
- // Register a receiver to listen for the completion of stopVPN
474
- val stopReceiver =
475
- object : BroadcastReceiver () {
476
- override fun onReceive (context : Context ? , intent : Intent ? ) {
477
- // Ensure stop intent is complete
478
- if (intent?.action == IPNService .ACTION_STOP_VPN ) {
479
- // Unregister receiver after receiving the broadcast
480
- context?.unregisterReceiver(this )
481
- // Now start the VPN
482
- startVPN()
483
- }
484
- }
485
- }
486
-
487
- // Register the receiver before stopping VPN
488
- val intentFilter = IntentFilter (IPNService .ACTION_STOP_VPN )
489
- this .registerReceiver(stopReceiver, intentFilter, Context .RECEIVER_NOT_EXPORTED )
490
-
491
- stopVPN()
472
+ val intent =
473
+ Intent (this , IPNService ::class .java).apply { action = IPNService .ACTION_RESTART_VPN }
474
+ try {
475
+ startService(intent)
476
+ } catch (illegalStateException: IllegalStateException ) {
477
+ TSLog .e(TAG , " restartVPN hit IllegalStateException in startService(): $illegalStateException " )
478
+ } catch (e: Exception ) {
479
+ TSLog .e(TAG , " restartVPN hit exception in startService(): $e " )
480
+ }
492
481
}
493
482
494
483
fun createNotificationChannel (id : String , name : String , description : String , importance : Int ) {
@@ -569,33 +558,13 @@ open class UninitializedApp : Application() {
569
558
return builder.build()
570
559
}
571
560
572
- fun addUserDisallowedPackageName (packageName : String ) {
573
- if (packageName.isEmpty()) {
574
- TSLog .e(TAG , " addUserDisallowedPackageName called with empty packageName" )
575
- return
576
- }
577
-
578
- getUnencryptedPrefs()
579
- .edit()
580
- .putStringSet(
581
- DISALLOWED_APPS_KEY , disallowedPackageNames().toMutableSet().union(setOf (packageName)))
582
- .apply ()
583
-
584
- this .restartVPN()
585
- }
586
-
587
- fun removeUserDisallowedPackageName (packageName : String ) {
588
- if (packageName.isEmpty()) {
589
- TSLog .e(TAG , " removeUserDisallowedPackageName called with empty packageName" )
561
+ fun updateUserDisallowedPackageNames (packageNames : List <String >) {
562
+ if (packageNames.any { it.isEmpty() }) {
563
+ TSLog .e(TAG , " updateUserDisallowedPackageNames called with empty packageName(s)" )
590
564
return
591
565
}
592
566
593
- getUnencryptedPrefs()
594
- .edit()
595
- .putStringSet(
596
- DISALLOWED_APPS_KEY ,
597
- disallowedPackageNames().toMutableSet().subtract(setOf (packageName)))
598
- .apply ()
567
+ getUnencryptedPrefs().edit().putStringSet(DISALLOWED_APPS_KEY , packageNames.toSet()).apply ()
599
568
600
569
this .restartVPN()
601
570
}
0 commit comments