forked from tailscale/tailscale-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitTunnelAppPickerView.kt
More file actions
237 lines (230 loc) · 8.88 KB
/
SplitTunnelAppPickerView.kt
File metadata and controls
237 lines (230 loc) · 8.88 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package com.tailscale.ipn.ui.view
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SegmentedButton
import androidx.compose.material3.SegmentedButtonDefaults
import androidx.compose.material3.SingleChoiceSegmentedButtonRow
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.core.graphics.drawable.toBitmap
import androidx.lifecycle.viewmodel.compose.viewModel
import com.tailscale.ipn.App
import com.tailscale.ipn.R
import com.tailscale.ipn.ui.util.Lists
import com.tailscale.ipn.ui.util.set
import com.tailscale.ipn.ui.viewModel.SplitTunnelAppPickerViewModel
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SplitTunnelAppPickerView(
backToSettings: BackNavigation,
model: SplitTunnelAppPickerViewModel = viewModel(),
) {
val installedApps by model.installedApps.collectAsState()
val selectedPackageNames by model.selectedPackageNames.collectAsState()
val allowSelected by model.allowSelected.collectAsState()
val builtInDisallowedPackageNames: List<String> = App.get().builtInDisallowedPackageNames
val mdmIncludedPackages by model.mdmIncludedPackages.collectAsState()
val mdmExcludedPackages by model.mdmExcludedPackages.collectAsState()
val showSwitchDialog by model.showSwitchDialog.collectAsState()
if (showSwitchDialog) {
SwitchAlertDialog(
allowSelected = allowSelected,
onConfirm = {
model.showSwitchDialog.set(false)
model.performSelectionSwitch()
},
onDismiss = { model.showSwitchDialog.set(false) },
)
}
Scaffold(
topBar = {
Header(titleRes = R.string.split_tunneling, onBack = backToSettings)
},
) { innerPadding ->
LazyColumn(modifier = Modifier.padding(innerPadding)) {
if (mdmExcludedPackages.value?.isNotEmpty() == true) {
item("mdmExcludedNotice") {
ListItem(
headlineContent = {
Text(stringResource(R.string.certain_apps_are_not_routed_via_tailscale))
})
}
} else if (mdmIncludedPackages.value?.isNotEmpty() == true) {
item("mdmIncludedNotice") {
ListItem(
headlineContent = {
Text(stringResource(R.string.only_specific_apps_are_routed_via_tailscale))
})
}
} else {
item("header") {
ListItem(
headlineContent = {
Text(
stringResource(
if (allowSelected) R.string.selected_apps_will_access_tailscale
else
R.string
.selected_apps_will_access_the_internet_directly_without_using_tailscale))
})
}
item("modeSelector") {
SingleChoiceSegmentedButtonRow(
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp),
) {
SegmentedButton(
selected = !allowSelected,
onClick = {
if (allowSelected) {
model.showSwitchDialog.set(true)
}
},
shape = SegmentedButtonDefaults.itemShape(index = 0, count = 2),
) {
Text(stringResource(R.string.split_tunnel_mode_exclude))
}
SegmentedButton(
selected = allowSelected,
onClick = {
if (!allowSelected) {
model.showSwitchDialog.set(true)
}
},
shape = SegmentedButtonDefaults.itemShape(index = 1, count = 2),
) {
Text(stringResource(R.string.split_tunnel_mode_include))
}
}
}
item("resolversHeader") {
Lists.SectionDivider(
stringResource(
if (allowSelected) R.string.count_included_apps else R.string.count_excluded_apps,
selectedPackageNames.count(),
))
}
item("bulkActions") {
Row(modifier = Modifier.padding(horizontal = 8.dp)) {
TextButton(
onClick = { model.selectAll() },
enabled = installedApps.isNotEmpty(),
) {
Text(stringResource(R.string.select_all_apps))
}
TextButton(
onClick = { model.deselectAll() },
enabled = installedApps.isNotEmpty(),
) {
Text(stringResource(R.string.deselect_all_apps))
}
}
}
if (installedApps.isEmpty()) {
item("spinner") {
Box(
modifier = Modifier.fillMaxSize().padding(innerPadding),
contentAlignment = Alignment.Center,
) {
CircularProgressIndicator(
modifier = Modifier.width(64.dp),
color = MaterialTheme.colorScheme.secondary,
trackColor = MaterialTheme.colorScheme.surfaceVariant,
)
}
}
} else {
items(installedApps, key = { it.packageName }) { app ->
ListItem(
headlineContent = { Text(app.name, fontWeight = FontWeight.SemiBold) },
leadingContent = {
Image(
bitmap =
model.installedAppsManager.packageManager
.getApplicationIcon(app.packageName)
.toBitmap()
.asImageBitmap(),
contentDescription = null,
modifier = Modifier.width(40.dp).height(40.dp),
)
},
supportingContent = {
Text(
app.packageName,
color = MaterialTheme.colorScheme.secondary,
fontSize = MaterialTheme.typography.bodySmall.fontSize,
letterSpacing = MaterialTheme.typography.bodySmall.letterSpacing,
)
},
trailingContent = {
Checkbox(
checked = selectedPackageNames.contains(app.packageName),
enabled = !builtInDisallowedPackageNames.contains(app.packageName),
onCheckedChange = { checked ->
if (checked) {
model.select(packageName = app.packageName)
} else {
model.deselect(packageName = app.packageName)
}
},
)
},
)
Lists.ItemDivider()
}
}
}
}
}
}
@Composable
fun SwitchAlertDialog(allowSelected: Boolean, onConfirm: () -> Unit, onDismiss: () -> Unit) {
val switchString =
stringResource(
if (allowSelected) R.string.switch_to_select_to_exclude
else R.string.switch_to_select_to_include)
val switchDescription =
stringResource(
if (allowSelected)
R.string.selected_apps_will_access_the_internet_directly_without_using_tailscale
else R.string.selected_apps_will_access_tailscale)
AlertDialog(
title = { Text(text = "$switchString?") },
text = {
Text(
text =
stringResource(R.string.your_current_selection_will_be_cleared) +
"\n$switchDescription")
},
onDismissRequest = onDismiss,
confirmButton = { TextButton(onClick = onConfirm) { Text(text = switchString) } },
dismissButton = {
TextButton(onClick = onDismiss) { Text(text = stringResource(R.string.cancel)) }
},
)
}