-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathLoaderService.java
More file actions
289 lines (230 loc) · 9.39 KB
/
LoaderService.java
File metadata and controls
289 lines (230 loc) · 9.39 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
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
package at.tomtasche.reader.background;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Binder;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import java.io.File;
import java.io.OutputStream;
import at.tomtasche.reader.R;
import at.tomtasche.reader.nonfree.AnalyticsConstants;
import at.tomtasche.reader.nonfree.AnalyticsManager;
import at.tomtasche.reader.nonfree.ConfigManager;
import at.tomtasche.reader.nonfree.CrashManager;
import at.tomtasche.reader.ui.activity.DocumentFragment;
public class LoaderService extends Service implements FileLoader.FileLoaderListener {
private Handler mainHandler;
private HandlerThread backgroundThread;
private Handler backgroundHandler;
private CrashManager crashManager;
private ConfigManager configManager;
private AnalyticsManager analyticsManager;
private MetadataLoader metadataLoader;
private CoreLoader coreLoader;
private RawLoader rawLoader;
private OnlineLoader onlineLoader;
private LoaderListener currentListener;
@Override
public synchronized void onCreate() {
super.onCreate();
mainHandler = new Handler();
backgroundThread = new HandlerThread(DocumentFragment.class.getSimpleName());
backgroundThread.start();
backgroundHandler = new Handler(backgroundThread.getLooper());
Context context = this;
initializeProprietaryLibraries();
metadataLoader = new MetadataLoader(context);
metadataLoader.initialize(this, mainHandler, backgroundHandler, analyticsManager, crashManager);
coreLoader = new CoreLoader(context, configManager, true, true);
coreLoader.initialize(this, mainHandler, backgroundHandler, analyticsManager, crashManager);
rawLoader = new RawLoader(context);
rawLoader.initialize(this, mainHandler, backgroundHandler, analyticsManager, crashManager);
onlineLoader = new OnlineLoader(context, coreLoader);
onlineLoader.initialize(this, mainHandler, backgroundHandler, analyticsManager, crashManager);
}
// copied from MainActivity, consider how to deduplicate
private void initializeProprietaryLibraries() {
boolean useProprietaryLibraries = !getResources().getBoolean(R.bool.DISABLE_TRACKING);
if (useProprietaryLibraries) {
GoogleApiAvailability googleApi = GoogleApiAvailability.getInstance();
int googleAvailability = googleApi.isGooglePlayServicesAvailable(this);
if (googleAvailability != ConnectionResult.SUCCESS) {
useProprietaryLibraries = false;
}
}
crashManager = new CrashManager();
crashManager.setEnabled(useProprietaryLibraries);
crashManager.initialize();
analyticsManager = new AnalyticsManager();
analyticsManager.setEnabled(useProprietaryLibraries);
analyticsManager.initialize(this);
configManager = new ConfigManager();
configManager.setEnabled(useProprietaryLibraries);
configManager.initialize();
}
@Override
public IBinder onBind(Intent intent) {
return new LoaderBinder();
}
public synchronized void setListener(LoaderListener listener) {
this.currentListener = listener;
}
private void logMissingListener() {
crashManager.log(new RuntimeException("missing listener"));
}
public synchronized void loadWithType(FileLoader.LoaderType loaderType, FileLoader.Options options) {
FileLoader loader;
switch (loaderType) {
case CORE:
loader = coreLoader;
break;
case ONLINE:
loader = onlineLoader;
break;
case RAW:
loader = rawLoader;
break;
case METADATA:
loader = metadataLoader;
break;
default:
loader = null;
}
loader.loadAsync(options);
}
@Override
public void onSuccess(FileLoader.Result result) {
FileLoader.Options options = result.options;
if (result.loaderType == FileLoader.LoaderType.METADATA) {
if (!coreLoader.isSupported(options)) {
crashManager.log("we do not expect this file to be an ODF: " + options.originalUri.toString());
analyticsManager.report("load_odf_error_expected", AnalyticsConstants.PARAM_CONTENT_TYPE, options.fileType);
}
loadWithType(FileLoader.LoaderType.CORE, options);
} else {
analyticsManager.report("load_success", AnalyticsConstants.PARAM_CONTENT_TYPE, options.fileType, AnalyticsConstants.PARAM_CONTENT, result.loaderType.toString());
if (currentListener != null) {
currentListener.onLoadSuccess(result);
} else {
logMissingListener();
}
}
}
@Override
public void onError(FileLoader.Result result, Throwable error) {
FileLoader.Options options = result.options;
crashManager.log(error, options.originalUri);
if (error instanceof FileLoader.EncryptedDocumentException) {
analyticsManager.report("load_error_encrypted");
if (currentListener != null) {
currentListener.onEncrypted(result);
} else {
logMissingListener();
}
return;
}
if (result.loaderType == FileLoader.LoaderType.CORE) {
analyticsManager.report("load_odf_error", AnalyticsConstants.PARAM_CONTENT_TYPE, options.fileType);
if (rawLoader.isSupported(options)) {
loadWithType(FileLoader.LoaderType.RAW, options);
} else {
if (currentListener != null) {
currentListener.onUnsupported(result);
} else {
logMissingListener();
}
}
return;
} else if (result.loaderType != FileLoader.LoaderType.METADATA) {
if (currentListener != null) {
currentListener.onError(result, error);
} else {
logMissingListener();
}
return;
}
// MetadataLoader failed, so there's no point in trying to parse or upload the file
analyticsManager.report("load_error", AnalyticsConstants.PARAM_CONTENT_TYPE, options.fileType, AnalyticsConstants.PARAM_CONTENT, result.loaderType.toString());
if (currentListener != null) {
currentListener.onError(result, error);
} else {
logMissingListener();
}
}
public boolean isOnlineSupported(FileLoader.Options options) {
return onlineLoader.isSupported(options);
}
public void saveAsync(FileLoader.Result lastResult, Uri outFile, String htmlDiff) {
backgroundHandler.post(() -> saveSync(lastResult, outFile, htmlDiff));
}
private void saveSync(FileLoader.Result lastResult, Uri outFile, String htmlDiff) {
try {
File fileToSave;
if (htmlDiff != null) {
fileToSave = coreLoader.retranslate(lastResult.options, htmlDiff);
if (fileToSave == null) {
throw new RuntimeException("retranslate failed");
}
} else {
// "full save" from the main UI
fileToSave = AndroidFileCache.getCacheFile(this, lastResult.options.cacheUri);
}
OutputStream outputStream = getContentResolver().openOutputStream(outFile);
StreamUtil.copy(fileToSave, outputStream);
outputStream.close();
if (htmlDiff != null) {
fileToSave.delete();
}
mainHandler.post(() -> {
if (currentListener != null) {
currentListener.onSaveSuccess(outFile);
} else {
logMissingListener();
}
});
} catch (Throwable e) {
analyticsManager.report("save_error", AnalyticsConstants.PARAM_CONTENT_TYPE, lastResult.options.fileType);
crashManager.log(e, lastResult.options.originalUri);
if (currentListener != null) {
currentListener.onSaveError();
} else {
logMissingListener();
}
}
}
@Override
public void onDestroy() {
if (metadataLoader != null) {
metadataLoader.close();
}
if (coreLoader != null) {
coreLoader.close();
}
if (rawLoader != null) {
rawLoader.close();
}
if (onlineLoader != null) {
onlineLoader.close();
}
backgroundThread.quit();
super.onDestroy();
}
public class LoaderBinder extends Binder {
public LoaderService getService() {
return LoaderService.this;
}
}
public interface LoaderListener {
void onLoadSuccess(FileLoader.Result result);
void onSaveSuccess(Uri outFile);
void onError(FileLoader.Result result, Throwable error);
void onEncrypted(FileLoader.Result result);
void onUnsupported(FileLoader.Result result);
void onSaveError();
}
}