1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.vauth.foxyvpn.quicksettings
import android.app.PendingIntent
import android.content.Intent
import android.graphics.drawable.Icon
import android.net.VpnService
import android.os.Build
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import com.vauth.foxyvpn.FoxyVpnApp
import com.vauth.foxyvpn.MainActivity
import com.vauth.foxyvpn.R
import com.vauth.foxyvpn.data.model.ConnectionState
import com.vauth.foxyvpn.data.SettingsStore
import com.vauth.foxyvpn.data.TokenStore
import com.vauth.foxyvpn.vpn.FoxyVpnService
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
class QuickConnectTileService : TileService() {
private val serviceScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
private var listenJob: Job? = null
override fun onStartListening() {
super.onStartListening()
listenJob?.cancel()
listenJob = serviceScope.launch {
FoxyVpnService.state.collect { state ->
updateTileState(state)
}
}
}
override fun onStopListening() {
listenJob?.cancel()
listenJob = null
super.onStopListening()
}
override fun onClick() {
super.onClick()
when (FoxyVpnService.state.value) {
ConnectionState.CONNECTED, ConnectionState.CONNECTING -> {
FoxyVpnService.stop(this)
}
ConnectionState.DISCONNECTED -> {
val app = applicationContext as? FoxyVpnApp
val tokenStore = app?.tokenStore ?: TokenStore(this)
if (!tokenStore.hasStoredSession()) {
launchMainActivityAndCollapse()
return
}
val settingsStore = app?.settingsStore ?: SettingsStore(this)
if (settingsStore.proxyOnlyMode) {
FoxyVpnService.start(this)
return
}
val prepareIntent = VpnService.prepare(this)
if (prepareIntent != null) {
launchMainActivityAndCollapse()
} else {
FoxyVpnService.start(this)
}
}
}
}
private fun updateTileState(state: ConnectionState) {
val tile = qsTile ?: return
tile.icon = Icon.createWithResource(this, android.R.drawable.ic_lock_lock)
tile.label = getString(R.string.quick_connect)
when (state) {
ConnectionState.CONNECTED -> {
tile.state = Tile.STATE_ACTIVE
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
tile.subtitle = getString(R.string.quick_connect_connected)
}
}
ConnectionState.CONNECTING -> {
tile.state = Tile.STATE_ACTIVE
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
tile.subtitle = getString(R.string.quick_connect_connecting)
}
}
ConnectionState.DISCONNECTED -> {
tile.state = Tile.STATE_INACTIVE
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
tile.subtitle = getString(R.string.quick_connect_disconnected)
}
}
}
tile.updateTile()
}
private fun launchMainActivityAndCollapse() {
val intent = Intent(this, MainActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
val pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)
startActivityAndCollapse(pendingIntent)
} else {
@Suppress("DEPRECATION")
startActivityAndCollapse(intent)
}
}
override fun onDestroy() {
listenJob?.cancel()
serviceScope.cancel()
super.onDestroy()
}
}