blob: 69bd7c1e537965ca781554a13a3c06db242e2346 (
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
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
|
package se.leap.bitmaskclient;
import android.content.res.AssetManager;
import com.pedrogomez.renderers.AdapteeCollection;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* Created by parmegv on 4/12/14.
*/
public class ProviderManager implements AdapteeCollection<Provider> {
private AssetManager assets_manager;
private File external_files_dir;
private Set<Provider> default_providers;
private Set<Provider> custom_providers;
private static ProviderManager instance;
final protected static String URLS = "urls";
public static ProviderManager getInstance(AssetManager assets_manager, File external_files_dir) {
if(instance == null)
instance = new ProviderManager(assets_manager);
instance.addCustomProviders(external_files_dir);
return instance;
}
public ProviderManager(AssetManager assets_manager) {
this.assets_manager = assets_manager;
addDefaultProviders(assets_manager);
}
private void addDefaultProviders(AssetManager assets_manager) {
try {
default_providers = providersFromAssets(URLS, assets_manager.list(URLS));
} catch (IOException e) {
e.printStackTrace();
}
}
private Set<Provider> providersFromAssets(String directory, String[] relative_file_paths) {
Set<Provider> providers = new HashSet<Provider>();
try {
for(String file : relative_file_paths) {
String main_url = extractMainUrlFromInputStream(assets_manager.open(directory + "/" + file));
providers.add(new Provider(new URL(main_url)));
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return providers;
}
private void addCustomProviders(File external_files_dir) {
this.external_files_dir = external_files_dir;
custom_providers = external_files_dir.isDirectory() ?
providersFromFiles(external_files_dir.list()) :
new HashSet<Provider>();
}
private Set<Provider> providersFromFiles(String[] files) {
Set<Provider> providers = new HashSet<Provider>();
try {
for(String file : files) {
String main_url = extractMainUrlFromInputStream(new FileInputStream(external_files_dir.getAbsolutePath() + "/" + file));
providers.add(new Provider(new URL(main_url)));
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return providers;
}
private String extractMainUrlFromInputStream(InputStream input_stream_file_contents) {
String main_url = "";
byte[] bytes = new byte[0];
try {
bytes = new byte[input_stream_file_contents.available()];
if(input_stream_file_contents.read(bytes) > 0) {
JSONObject file_contents = new JSONObject(new String(bytes));
main_url = file_contents.getString(Provider.MAIN_URL);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return main_url;
}
public Set<Provider> providers() {
Set<Provider> all_providers = new HashSet<Provider>();
all_providers.addAll(default_providers);
all_providers.addAll(custom_providers);
return all_providers;
}
@Override
public int size() {
return providers().size();
}
@Override
public Provider get(int index) {
Iterator<Provider> iterator = providers().iterator();
while(iterator.hasNext() && index > 0) {
iterator.next();
index--;
}
return iterator.next();
}
@Override
public void add(Provider element) {
if(!default_providers.contains(element))
custom_providers.add(element);
}
@Override
public void remove(Provider element) {
custom_providers.remove(element);
}
@Override
public void addAll(Collection<Provider> elements) {
custom_providers.addAll(elements);
}
@Override
public void removeAll(Collection<Provider> elements) {
custom_providers.removeAll(elements);
default_providers.removeAll(elements);
}
@Override
public void clear() {
default_providers.clear();
custom_providers.clear();
}
protected void saveCustomProvidersToFile() {
try {
for (Provider provider : custom_providers) {
File provider_file = new File(external_files_dir, provider.getName() + ".json");
if(!provider_file.exists()) {
FileWriter writer = new FileWriter(provider_file);
writer.write(provider.toJson().toString());
writer.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
|