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
import 'dart:typed_data';
import 'package:flutter/material.dart';
import '../core/android_core.dart';
import '../l10n/app_localizations.dart';
/// Android, per-app proxy (v2rayN's words): bypass the selected apps, or
/// proxy only the selected apps. The routing region's list (Iran's banks,
/// messengers, and the like) is a suggestion: checked at first, and again
/// with "Suggested apps", never forced. The choice is kept by the app
/// (CoreService.kt) and applied when the page closes.
class BypassAppsPage extends StatefulWidget {
const BypassAppsPage({super.key, this.region});
/// The routing region's name while routing is on; its apps are marked.
final String? region;
@override
State<BypassAppsPage> createState() => _BypassAppsPageState();
}
class _BypassAppsPageState extends State<BypassAppsPage> {
List<InstalledApp>? _apps;
AppRouting? _routing;
/// The choice when the page opened, to know whether to apply it.
String _initial = '';
String _query = '';
final Map<String, Future<Uint8List?>> _icons = {};
@override
void initState() {
super.initState();
_load();
}
Future<void> _load() async {
final apps = await AndroidCore.installedApps();
final routing = await AndroidCore.appRouting();
if (!mounted) return;
setState(() {
_routing = routing;
_initial = routing.key;
_apps = apps;
_sort();
});
}
/// The checked apps first, then by name.
void _sort() {
final r = _routing!;
_apps!.sort((a, b) {
final ca = r.checked(a.package), cb = r.checked(b.package);
if (ca != cb) return ca ? -1 : 1;
return a.label.toLowerCase().compareTo(b.label.toLowerCase());
});
}
void _save() => AndroidCore.setAppRouting(_routing!, apply: false);
@override
Widget build(BuildContext context) {
final l = AppLocalizations.of(context);
final theme = Theme.of(context);
final routing = _routing;
final q = _query.trim().toLowerCase();
final apps = [
for (final a in _apps ?? const <InstalledApp>[])
if (q.isEmpty ||
a.label.toLowerCase().contains(q) ||
a.package.toLowerCase().contains(q))
a,
];
return PopScope(
onPopInvokedWithResult: (didPop, _) {
// Applied once: the VPN is set up again with the new choice.
if (didPop && routing != null && routing.key != _initial) {
AndroidCore.setAppRouting(routing, apply: true);
}
},
child: Scaffold(
appBar: AppBar(title: Text(l.bypassApps)),
body: routing == null || _apps == null
? const Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: apps.length + 1,
itemBuilder: (context, i) {
if (i == 0) {
return Padding(
padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SegmentedButton<bool>(
showSelectedIcon: false,
segments: [
ButtonSegment(
value: false,
label: Text(l.appModeExclude),
),
ButtonSegment(
value: true,
label: Text(l.appModeInclude),
),
],
selected: {routing.onlyChosen},
onSelectionChanged: (v) {
setState(() {
routing.onlyChosen = v.first;
_sort();
});
_save();
},
),
const SizedBox(height: 10),
Text(
routing.onlyChosen
? l.appModeIncludeHint
: l.appModeExcludeHint,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
if (!routing.onlyChosen &&
routing.suggested.isNotEmpty) ...[
const SizedBox(height: 8),
Align(
alignment: AlignmentDirectional.centerStart,
child: OutlinedButton.icon(
onPressed: () {
final added = routing.checkSuggested(
_apps!.map((a) => a.package),
);
setState(_sort);
_save();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(l.suggestedAdded(added)),
behavior: SnackBarBehavior.floating,
),
);
},
icon: const Icon(Icons.auto_awesome_rounded),
label: Text(l.suggestedApps),
),
),
],
const SizedBox(height: 12),
TextField(
decoration: InputDecoration(
isDense: true,
prefixIcon: const Icon(Icons.search_rounded),
hintText: l.search,
border: const OutlineInputBorder(),
),
onChanged: (v) => setState(() => _query = v),
),
],
),
);
}
final app = apps[i - 1];
final onList =
widget.region != null &&
routing.suggested.contains(app.package);
final icon = _icons.putIfAbsent(
app.package,
() => AndroidCore.appIcon(app.package),
);
return CheckboxListTile(
value: routing.checked(app.package),
onChanged: (v) {
setState(() => routing.toggle(app.package, v ?? false));
_save();
},
secondary: SizedBox.square(
dimension: 40,
child: FutureBuilder<Uint8List?>(
future: icon,
builder: (context, snap) => snap.data == null
? const Icon(Icons.android_rounded)
: Image.memory(snap.data!, gaplessPlayback: true),
),
),
title: Text(app.label),
subtitle: Text(
onList ? l.fromRegionList(widget.region!) : app.package,
style: theme.textTheme.bodySmall?.copyWith(
color: onList ? theme.colorScheme.primary : null,
),
overflow: TextOverflow.ellipsis,
),
);
},
),
),
);
}
}