|
1 | 1 | /* |
2 | 2 | * ConverterWorker.java |
3 | 3 | * |
4 | | - * Copyright (C) 2021 Vilius Sutkus'89 |
| 4 | + * Copyright (C) 2021, 2022 Vilius Sutkus '89 <ViliusSutkus89@gmail.com> |
5 | 5 | * |
6 | 6 | * This program is free software: you can redistribute it and/or modify |
7 | 7 | * it under the terms of the GNU General Public License as published by |
|
24 | 24 | import android.net.Uri; |
25 | 25 |
|
26 | 26 | import androidx.annotation.NonNull; |
| 27 | +import androidx.concurrent.futures.CallbackToFutureAdapter; |
27 | 28 | import androidx.work.Data; |
28 | 29 | import androidx.work.WorkInfo; |
29 | | -import androidx.work.Worker; |
30 | 30 | import androidx.work.WorkerParameters; |
| 31 | +import androidx.work.multiprocess.RemoteListenableWorker; |
31 | 32 |
|
| 33 | +import com.google.common.util.concurrent.ListenableFuture; |
32 | 34 | import com.viliussutkus89.android.pdf2htmlex.pdf2htmlEX; |
33 | 35 |
|
34 | 36 | import java.io.File; |
|
37 | 39 | import java.io.InputStream; |
38 | 40 | import java.io.OutputStream; |
39 | 41 |
|
40 | | -public class ConverterWorker extends Worker { |
| 42 | +public class ConverterWorker extends RemoteListenableWorker { |
41 | 43 | public static final String INPUT__URI = "input__uri"; |
42 | 44 | public static final String INPUT__DESTINATION_DIR = "input__destination_dir"; |
43 | 45 | public static final String OUTPUT__URI = "output__uri"; |
@@ -75,49 +77,51 @@ public static Progress getProgress(@NonNull WorkInfo workInfo) { |
75 | 77 |
|
76 | 78 | @NonNull |
77 | 79 | @Override |
78 | | - public Result doWork() { |
79 | | - final Uri inputUri = Uri.parse(getInputData().getString(INPUT__URI)); |
80 | | - if (null == inputUri) { |
81 | | - return Result.failure(); |
82 | | - } |
| 80 | + public ListenableFuture<Result> startRemoteWork() { |
| 81 | + return CallbackToFutureAdapter.getFuture(completer -> { |
| 82 | + final Uri inputUri = Uri.parse(getInputData().getString(INPUT__URI)); |
| 83 | + if (null == inputUri) { |
| 84 | + return completer.set(Result.failure()); |
| 85 | + } |
83 | 86 |
|
84 | | - final pdf2htmlEX converter = new pdf2htmlEX(getApplicationContext()); |
85 | | - if (isStopped()) { |
86 | | - return Result.failure(); |
87 | | - } |
| 87 | + final pdf2htmlEX converter = new pdf2htmlEX(getApplicationContext()); |
| 88 | + if (isStopped()) { |
| 89 | + return completer.set(Result.failure()); |
| 90 | + } |
88 | 91 |
|
89 | | - setProgress(Progress.COPYING_INPUT); |
90 | | - final File inputFileInCache = copyUriToCache(getApplicationContext(), inputUri); |
91 | | - if (null == inputFileInCache) { |
92 | | - return Result.failure(); |
93 | | - } |
| 92 | + setProgress(Progress.COPYING_INPUT); |
| 93 | + final File inputFileInCache = copyUriToCache(getApplicationContext(), inputUri); |
| 94 | + if (null == inputFileInCache) { |
| 95 | + return completer.set(Result.failure()); |
| 96 | + } |
94 | 97 |
|
95 | | - try { |
96 | | - setProgress(Progress.CONVERTING); |
| 98 | + try { |
| 99 | + setProgress(Progress.CONVERTING); |
97 | 100 |
|
98 | | - if (isStopped()) { |
99 | | - return Result.failure(); |
100 | | - } |
101 | | - File outputFile = converter.convert(inputFileInCache); |
102 | | - |
103 | | - String destinationDirStr = getInputData().getString(INPUT__DESTINATION_DIR); |
104 | | - if (null != destinationDirStr) { |
105 | | - final File destinationDir = new File(destinationDirStr); |
106 | | - destinationDir.mkdir(); |
107 | | - final File outputFileInDestinationDir = generateNewFileInCache(destinationDir, outputFile.getName()); |
108 | | - if (null == outputFileInDestinationDir || !outputFile.renameTo(outputFileInDestinationDir)) { |
109 | | - outputFile.delete(); |
110 | | - return Result.failure(); |
| 101 | + if (isStopped()) { |
| 102 | + return completer.set(Result.failure()); |
| 103 | + } |
| 104 | + File outputFile = converter.convert(inputFileInCache); |
| 105 | + |
| 106 | + String destinationDirStr = getInputData().getString(INPUT__DESTINATION_DIR); |
| 107 | + if (null != destinationDirStr) { |
| 108 | + final File destinationDir = new File(destinationDirStr); |
| 109 | + destinationDir.mkdir(); |
| 110 | + final File outputFileInDestinationDir = generateNewFileInCache(destinationDir, outputFile.getName()); |
| 111 | + if (null == outputFileInDestinationDir || !outputFile.renameTo(outputFileInDestinationDir)) { |
| 112 | + outputFile.delete(); |
| 113 | + return completer.set(Result.failure()); |
| 114 | + } |
| 115 | + outputFile = outputFileInDestinationDir; |
111 | 116 | } |
112 | | - outputFile = outputFileInDestinationDir; |
113 | | - } |
114 | 117 |
|
115 | | - return Result.success(new Data.Builder().putString(OUTPUT__URI, outputFile.getAbsolutePath()).build()); |
116 | | - } catch (pdf2htmlEX.ConversionFailedException | IOException e) { |
117 | | - return Result.failure(); |
118 | | - } finally { |
119 | | - inputFileInCache.delete(); |
120 | | - } |
| 118 | + return completer.set(Result.success(new Data.Builder().putString(OUTPUT__URI, outputFile.getAbsolutePath()).build())); |
| 119 | + } catch (pdf2htmlEX.ConversionFailedException | IOException e) { |
| 120 | + return completer.set(Result.failure()); |
| 121 | + } finally { |
| 122 | + inputFileInCache.delete(); |
| 123 | + } |
| 124 | + }); |
121 | 125 | } |
122 | 126 |
|
123 | 127 | private static File generateNewFileInCache(File cacheDir, String filename) { |
|
0 commit comments