blob: faecde72c9f76cc9769a0a5e86ea82d7b0b5d410 (
plain)
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
|
package com.wireguard.android;
import android.app.Service;
import android.content.Intent;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import com.wireguard.config.Profile;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
/**
* Service that handles profile state coordination and all background processing for the app.
*/
public class ProfileService extends Service {
private static final String TAG = "ProfileService";
private final IBinder binder = new ProfileServiceBinder();
private final ObservableList<Profile> profiles = new ObservableArrayList<>();
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onCreate() {
new ProfileLoader().execute(getFilesDir().listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".conf");
}
}));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
private class ProfileLoader extends AsyncTask<File, Void, List<Profile>> {
@Override
protected List<Profile> doInBackground(File... files) {
final List<Profile> loadedProfiles = new LinkedList<>();
for (File file : files) {
final String fileName = file.getName();
final String profileName = fileName.substring(0, fileName.length() - 5);
final Profile profile = new Profile(profileName);
try {
profile.parseFrom(openFileInput(fileName));
loadedProfiles.add(profile);
} catch (IOException e) {
Log.w(TAG, "Failed to load profile from " + fileName, e);
}
if (isCancelled())
break;
}
return loadedProfiles;
}
@Override
protected void onPostExecute(List<Profile> loadedProfiles) {
profiles.addAll(loadedProfiles);
}
}
private class ProfileServiceBinder extends Binder implements ProfileServiceInterface {
public ObservableList<Profile> getProfiles() {
return profiles;
}
}
}
|