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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
/*
* Copyright (c) 2012-2016 Arne Schwabe
* Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
*/
package de.blinkt.openvpn.fragments;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import androidx.annotation.RequiresApi;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;
import de.blinkt.openvpn.LaunchVPN;
import de.blinkt.openvpn.R;
import de.blinkt.openvpn.VpnProfile;
import de.blinkt.openvpn.activities.ConfigConverter;
import de.blinkt.openvpn.activities.DisconnectVPN;
import de.blinkt.openvpn.activities.FileSelect;
import de.blinkt.openvpn.activities.VPNPreferences;
import de.blinkt.openvpn.core.ConnectionStatus;
import de.blinkt.openvpn.core.Preferences;
import de.blinkt.openvpn.core.ProfileManager;
import de.blinkt.openvpn.core.VpnStatus;
import static de.blinkt.openvpn.core.OpenVPNService.DISCONNECT_VPN;
public class VPNProfileList extends ListFragment implements OnClickListener, VpnStatus.StateListener {
public final static int RESULT_VPN_DELETED = Activity.RESULT_FIRST_USER;
public final static int RESULT_VPN_DUPLICATE = Activity.RESULT_FIRST_USER + 1;
private static final int MENU_ADD_PROFILE = Menu.FIRST;
private static final int START_VPN_CONFIG = 92;
private static final int SELECT_PROFILE = 43;
private static final int IMPORT_PROFILE = 231;
private static final int FILE_PICKER_RESULT_KITKAT = 392;
private static final int MENU_IMPORT_PROFILE = Menu.FIRST + 1;
private static final int MENU_CHANGE_SORTING = Menu.FIRST + 2;
private static final String PREF_SORT_BY_LRU = "sortProfilesByLRU";
private String mLastStatusMessage;
@Override
public void updateState(String state, String logmessage, final int localizedResId, ConnectionStatus level) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mLastStatusMessage = VpnStatus.getLastCleanLogMessage(getActivity());
mArrayadapter.notifyDataSetChanged();
}
});
}
@Override
public void setConnectedVPN(String uuid) {
}
private class VPNArrayAdapter extends ArrayAdapter<VpnProfile> {
public VPNArrayAdapter(Context context, int resource,
int textViewResourceId) {
super(context, resource, textViewResourceId);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
final VpnProfile profile = (VpnProfile) getListAdapter().getItem(position);
View titleview = v.findViewById(R.id.vpn_list_item_left);
titleview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startOrStopVPN(profile);
}
});
View settingsview = v.findViewById(R.id.quickedit_settings);
settingsview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
editVPN(profile);
}
});
TextView subtitle = (TextView) v.findViewById(R.id.vpn_item_subtitle);
if (profile.getUUIDString().equals(VpnStatus.getLastConnectedVPNProfile())) {
subtitle.setText(mLastStatusMessage);
subtitle.setVisibility(View.VISIBLE);
} else {
subtitle.setText("");
subtitle.setVisibility(View.GONE);
}
return v;
}
}
private void startOrStopVPN(VpnProfile profile) {
if (VpnStatus.isVPNActive() && profile.getUUIDString().equals(VpnStatus.getLastConnectedVPNProfile())) {
Intent disconnectVPN = new Intent(getActivity(), DisconnectVPN.class);
startActivity(disconnectVPN);
} else {
startVPN(profile);
}
}
private ArrayAdapter<VpnProfile> mArrayadapter;
protected VpnProfile mEditProfile = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
// Shortcut version is increased to refresh all shortcuts
final static int SHORTCUT_VERSION = 1;
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
void updateDynamicShortcuts() {
PersistableBundle versionExtras = new PersistableBundle();
versionExtras.putInt("version", SHORTCUT_VERSION);
ShortcutManager shortcutManager = getContext().getSystemService(ShortcutManager.class);
if (shortcutManager.isRateLimitingActive())
return;
List<ShortcutInfo> shortcuts = shortcutManager.getDynamicShortcuts();
int maxvpn = shortcutManager.getMaxShortcutCountPerActivity() - 1;
ShortcutInfo disconnectShortcut = new ShortcutInfo.Builder(getContext(), "disconnectVPN")
.setShortLabel("Disconnect")
.setLongLabel("Disconnect VPN")
.setIntent(new Intent(getContext(), DisconnectVPN.class).setAction(DISCONNECT_VPN))
.setIcon(Icon.createWithResource(getContext(), R.drawable.ic_shortcut_cancel))
.setExtras(versionExtras)
.build();
LinkedList<ShortcutInfo> newShortcuts = new LinkedList<>();
LinkedList<ShortcutInfo> updateShortcuts = new LinkedList<>();
LinkedList<String> removeShortcuts = new LinkedList<>();
LinkedList<String> disableShortcuts = new LinkedList<>();
boolean addDisconnect = true;
TreeSet<VpnProfile> sortedProfilesLRU = new TreeSet<VpnProfile>(new VpnProfileLRUComparator());
ProfileManager profileManager = ProfileManager.getInstance(getContext());
sortedProfilesLRU.addAll(profileManager.getProfiles());
LinkedList<VpnProfile> LRUProfiles = new LinkedList<>();
maxvpn = Math.min(maxvpn, sortedProfilesLRU.size());
for (int i = 0; i < maxvpn; i++) {
LRUProfiles.add(sortedProfilesLRU.pollFirst());
}
for (ShortcutInfo shortcut : shortcuts) {
if (shortcut.getId().equals("disconnectVPN")) {
addDisconnect = false;
if (shortcut.getExtras() == null
|| shortcut.getExtras().getInt("version") != SHORTCUT_VERSION)
updateShortcuts.add(disconnectShortcut);
} else {
VpnProfile p = ProfileManager.get(getContext(), shortcut.getId());
if (p == null || p.profileDeleted) {
if (shortcut.isEnabled()) {
disableShortcuts.add(shortcut.getId());
removeShortcuts.add(shortcut.getId());
}
if (!shortcut.isPinned())
removeShortcuts.add(shortcut.getId());
} else {
if (LRUProfiles.contains(p))
LRUProfiles.remove(p);
else
removeShortcuts.add(p.getUUIDString());
if (!p.getName().equals(shortcut.getShortLabel())
|| shortcut.getExtras() == null
|| shortcut.getExtras().getInt("version") != SHORTCUT_VERSION)
updateShortcuts.add(createShortcut(p));
}
}
}
if (addDisconnect)
newShortcuts.add(disconnectShortcut);
for (VpnProfile p : LRUProfiles)
newShortcuts.add(createShortcut(p));
if (updateShortcuts.size() > 0)
shortcutManager.updateShortcuts(updateShortcuts);
if (removeShortcuts.size() > 0)
shortcutManager.removeDynamicShortcuts(removeShortcuts);
if (newShortcuts.size() > 0)
shortcutManager.addDynamicShortcuts(newShortcuts);
if (disableShortcuts.size() > 0)
shortcutManager.disableShortcuts(disableShortcuts, "VpnProfile does not exist anymore.");
}
@RequiresApi(Build.VERSION_CODES.N_MR1)
ShortcutInfo createShortcut(VpnProfile profile) {
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClass(getActivity(), LaunchVPN.class);
shortcutIntent.putExtra(LaunchVPN.EXTRA_KEY, profile.getUUID().toString());
shortcutIntent.setAction(Intent.ACTION_MAIN);
shortcutIntent.putExtra("EXTRA_HIDELOG", true);
PersistableBundle versionExtras = new PersistableBundle();
versionExtras.putInt("version", SHORTCUT_VERSION);
return new ShortcutInfo.Builder(getContext(), profile.getUUIDString())
.setShortLabel(profile.getName())
.setLongLabel(getString(R.string.qs_connect, profile.getName()))
.setIcon(Icon.createWithResource(getContext(), R.drawable.ic_shortcut_vpn_key))
.setIntent(shortcutIntent)
.setExtras(versionExtras)
.build();
}
class MiniImageGetter implements ImageGetter {
@Override
public Drawable getDrawable(String source) {
Drawable d = null;
if ("ic_menu_add".equals(source))
d = getActivity().getResources().getDrawable(R.drawable.ic_menu_add_grey);
else if ("ic_menu_archive".equals(source))
d = getActivity().getResources().getDrawable(R.drawable.ic_menu_import_grey);
if (d != null) {
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
} else {
return null;
}
}
}
@Override
public void onResume() {
super.onResume();
setListAdapter();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
updateDynamicShortcuts();
}
VpnStatus.addStateListener(this);
}
@Override
public void onPause() {
super.onPause();
VpnStatus.removeStateListener(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.vpn_profile_list, container, false);
TextView newvpntext = (TextView) v.findViewById(R.id.add_new_vpn_hint);
TextView importvpntext = (TextView) v.findViewById(R.id.import_vpn_hint);
newvpntext.setText(Html.fromHtml(getString(R.string.add_new_vpn_hint), new MiniImageGetter(), null));
importvpntext.setText(Html.fromHtml(getString(R.string.vpn_import_hint), new MiniImageGetter(), null));
ImageButton fab_add = (ImageButton) v.findViewById(R.id.fab_add);
ImageButton fab_import = (ImageButton) v.findViewById(R.id.fab_import);
if (fab_add != null)
fab_add.setOnClickListener(this);
if (fab_import != null)
fab_import.setOnClickListener(this);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter();
}
static class VpnProfileNameComparator implements Comparator<VpnProfile> {
@Override
public int compare(VpnProfile lhs, VpnProfile rhs) {
if (lhs == rhs)
// Catches also both null
return 0;
if (lhs == null)
return -1;
if (rhs == null)
return 1;
if (lhs.mName == null)
return -1;
if (rhs.mName == null)
return 1;
return lhs.mName.compareTo(rhs.mName);
}
}
static class VpnProfileLRUComparator implements Comparator<VpnProfile> {
VpnProfileNameComparator nameComparator = new VpnProfileNameComparator();
@Override
public int compare(VpnProfile lhs, VpnProfile rhs) {
if (lhs == rhs)
// Catches also both null
return 0;
if (lhs == null)
return -1;
if (rhs == null)
return 1;
// Copied from Long.compare
if (lhs.mLastUsed > rhs.mLastUsed)
return -1;
if (lhs.mLastUsed < rhs.mLastUsed)
return 1;
else
return nameComparator.compare(lhs, rhs);
}
}
private void setListAdapter() {
if (mArrayadapter == null) {
mArrayadapter = new VPNArrayAdapter(getActivity(), R.layout.vpn_list_item, R.id.vpn_item_title);
}
populateVpnList();
}
private void populateVpnList() {
boolean sortByLRU = Preferences.getDefaultSharedPreferences(getActivity()).getBoolean(PREF_SORT_BY_LRU, false);
Collection<VpnProfile> allvpn = getPM().getProfiles();
TreeSet<VpnProfile> sortedset;
if (sortByLRU)
sortedset = new TreeSet<>(new VpnProfileLRUComparator());
else
sortedset = new TreeSet<>(new VpnProfileNameComparator());
sortedset.addAll(allvpn);
mArrayadapter.clear();
mArrayadapter.addAll(sortedset);
setListAdapter(mArrayadapter);
mArrayadapter.notifyDataSetChanged();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add(0, MENU_ADD_PROFILE, 0, R.string.menu_add_profile)
.setIcon(R.drawable.ic_menu_add)
.setAlphabeticShortcut('a')
.setTitleCondensed(getActivity().getString(R.string.add))
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0, MENU_IMPORT_PROFILE, 0, R.string.menu_import)
.setIcon(R.drawable.ic_menu_import)
.setAlphabeticShortcut('i')
.setTitleCondensed(getActivity().getString(R.string.menu_import_short))
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0, MENU_CHANGE_SORTING, 0, R.string.change_sorting)
.setIcon(R.drawable.ic_sort)
.setAlphabeticShortcut('s')
.setTitleCondensed(getString(R.string.sort))
.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final int itemId = item.getItemId();
if (itemId == MENU_ADD_PROFILE) {
onAddOrDuplicateProfile(null);
return true;
} else if (itemId == MENU_IMPORT_PROFILE) {
return startImportConfigFilePicker();
} else if (itemId == MENU_CHANGE_SORTING) {
return changeSorting();
} else {
return super.onOptionsItemSelected(item);
}
}
private boolean changeSorting() {
SharedPreferences prefs = Preferences.getDefaultSharedPreferences(getActivity());
boolean oldValue = prefs.getBoolean(PREF_SORT_BY_LRU, false);
SharedPreferences.Editor prefsedit = prefs.edit();
if (oldValue) {
Toast.makeText(getActivity(), R.string.sorted_az, Toast.LENGTH_SHORT).show();
prefsedit.putBoolean(PREF_SORT_BY_LRU, false);
} else {
prefsedit.putBoolean(PREF_SORT_BY_LRU, true);
Toast.makeText(getActivity(), R.string.sorted_lru, Toast.LENGTH_SHORT).show();
}
prefsedit.apply();
populateVpnList();
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fab_import:
startImportConfigFilePicker();
break;
case R.id.fab_add:
onAddOrDuplicateProfile(null);
break;
}
}
private boolean startImportConfigFilePicker() {
boolean startOldFileDialog = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && !Utils.alwaysUseOldFileChooser(getActivity() ))
startOldFileDialog = !startFilePicker();
if (startOldFileDialog)
startImportConfig();
return true;
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private boolean startFilePicker() {
Intent i = Utils.getFilePickerIntent(getActivity(), Utils.FileType.OVPN_CONFIG);
if (i != null) {
startActivityForResult(i, FILE_PICKER_RESULT_KITKAT);
return true;
} else
return false;
}
private void startImportConfig() {
Intent intent = new Intent(getActivity(), FileSelect.class);
intent.putExtra(FileSelect.NO_INLINE_SELECTION, true);
intent.putExtra(FileSelect.WINDOW_TITLE, R.string.import_configuration_file);
startActivityForResult(intent, SELECT_PROFILE);
}
private void onAddOrDuplicateProfile(final VpnProfile mCopyProfile) {
Context context = getActivity();
if (context != null) {
final EditText entry = new EditText(context);
entry.setSingleLine();
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
if (mCopyProfile == null)
dialog.setTitle(R.string.menu_add_profile);
else {
dialog.setTitle(context.getString(R.string.duplicate_profile_title, mCopyProfile.mName));
entry.setText(getString(R.string.copy_of_profile, mCopyProfile.mName));
}
dialog.setMessage(R.string.add_profile_name_prompt);
dialog.setView(entry);
dialog.setNeutralButton(R.string.menu_import_short,
(dialog1, which) -> startImportConfigFilePicker());
dialog.setPositiveButton(android.R.string.ok,
(dialog12, which) -> {
String name = entry.getText().toString();
if (getPM().getProfileByName(name) == null) {
VpnProfile profile;
if (mCopyProfile != null) {
profile = mCopyProfile.copy(name);
// Remove restrictions on copy profile
profile.mProfileCreator = null;
profile.mUserEditable = true;
} else
profile = new VpnProfile(name);
addProfile(profile);
editVPN(profile);
} else {
Toast.makeText(getActivity(), R.string.duplicate_profile_name, Toast.LENGTH_LONG).show();
}
});
dialog.setNegativeButton(android.R.string.cancel, null);
dialog.create().show();
}
}
private void addProfile(VpnProfile profile) {
getPM().addProfile(profile);
getPM().saveProfileList(getActivity());
getPM().saveProfile(getActivity(), profile);
mArrayadapter.add(profile);
}
private ProfileManager getPM() {
return ProfileManager.getInstance(getActivity());
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_VPN_DELETED) {
if (mArrayadapter != null && mEditProfile != null)
mArrayadapter.remove(mEditProfile);
} else if (resultCode == RESULT_VPN_DUPLICATE && data != null) {
String profileUUID = data.getStringExtra(VpnProfile.EXTRA_PROFILEUUID);
VpnProfile profile = ProfileManager.get(getActivity(), profileUUID);
if (profile != null)
onAddOrDuplicateProfile(profile);
}
if (resultCode != Activity.RESULT_OK)
return;
if (requestCode == START_VPN_CONFIG) {
String configuredVPN = data.getStringExtra(VpnProfile.EXTRA_PROFILEUUID);
VpnProfile profile = ProfileManager.get(getActivity(), configuredVPN);
getPM().saveProfile(getActivity(), profile);
// Name could be modified, reset List adapter
setListAdapter();
} else if (requestCode == SELECT_PROFILE) {
String fileData = data.getStringExtra(FileSelect.RESULT_DATA);
Uri uri = new Uri.Builder().path(fileData).scheme("file").build();
startConfigImport(uri);
} else if (requestCode == IMPORT_PROFILE) {
String profileUUID = data.getStringExtra(VpnProfile.EXTRA_PROFILEUUID);
mArrayadapter.add(ProfileManager.get(getActivity(), profileUUID));
} else if (requestCode == FILE_PICKER_RESULT_KITKAT) {
if (data != null) {
Uri uri = data.getData();
startConfigImport(uri);
}
}
}
private void startConfigImport(Uri uri) {
Intent startImport = new Intent(getActivity(), ConfigConverter.class);
startImport.setAction(ConfigConverter.IMPORT_PROFILE);
startImport.setData(uri);
startActivityForResult(startImport, IMPORT_PROFILE);
}
private void editVPN(VpnProfile profile) {
mEditProfile = profile;
Intent vprefintent = new Intent(getActivity(), VPNPreferences.class)
.putExtra(getActivity().getPackageName() + ".profileUUID", profile.getUUID().toString());
startActivityForResult(vprefintent, START_VPN_CONFIG);
}
private void startVPN(VpnProfile profile) {
getPM().saveProfile(getActivity(), profile);
Intent intent = new Intent(getActivity(), LaunchVPN.class);
intent.putExtra(LaunchVPN.EXTRA_KEY, profile.getUUID().toString());
intent.setAction(Intent.ACTION_MAIN);
startActivity(intent);
}
}
|