-
-
Notifications
You must be signed in to change notification settings - Fork 509
Expand file tree
/
Copy pathFileUtils.java
More file actions
720 lines (588 loc) · 21.8 KB
/
FileUtils.java
File metadata and controls
720 lines (588 loc) · 21.8 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
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*******************************************************************************
* This file is part of RedReader.
*
* RedReader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RedReader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RedReader. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package org.quantumbadger.redreader.common;
import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.ClipData;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.StatFs;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import org.quantumbadger.redreader.R;
import org.quantumbadger.redreader.account.RedditAccountManager;
import org.quantumbadger.redreader.activities.BaseActivity;
import org.quantumbadger.redreader.activities.BugReportActivity;
import org.quantumbadger.redreader.cache.CacheCompressionType;
import org.quantumbadger.redreader.cache.CacheContentProvider;
import org.quantumbadger.redreader.cache.CacheManager;
import org.quantumbadger.redreader.cache.CacheRequest;
import org.quantumbadger.redreader.cache.CacheRequestCallbacks;
import org.quantumbadger.redreader.cache.downloadstrategy.DownloadStrategyIfNotCached;
import org.quantumbadger.redreader.common.time.TimestampUTC;
import org.quantumbadger.redreader.fragments.ShareOrderDialog;
import org.quantumbadger.redreader.image.GetImageInfoListener;
import org.quantumbadger.redreader.image.ImageInfo;
import org.quantumbadger.redreader.image.LegacySaveImageCallback;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.UUID;
public class FileUtils {
private static final String TAG = "FileUtils";
private static final HashMap<String, String> MIMETYPE_TO_EXTENSION = new HashMap<>();
static {
MIMETYPE_TO_EXTENSION.put("audio/3gpp2", "3g2");
MIMETYPE_TO_EXTENSION.put("video/3gpp2", "3g2");
MIMETYPE_TO_EXTENSION.put("audio/3gpp", "3gp");
MIMETYPE_TO_EXTENSION.put("video/3gpp", "3gp");
MIMETYPE_TO_EXTENSION.put("application/x-7z-compressed", "7z");
MIMETYPE_TO_EXTENSION.put("audio/aac", "aac");
MIMETYPE_TO_EXTENSION.put("application/x-abiword", "abw");
MIMETYPE_TO_EXTENSION.put("application/x-freearc", "arc");
MIMETYPE_TO_EXTENSION.put("video/x-msvideo", "avi");
MIMETYPE_TO_EXTENSION.put("application/vnd.amazon.ebook", "azw");
MIMETYPE_TO_EXTENSION.put("application/octet-stream", "bin");
MIMETYPE_TO_EXTENSION.put("image/bmp", "bmp");
MIMETYPE_TO_EXTENSION.put("application/x-bzip2", "bz2");
MIMETYPE_TO_EXTENSION.put("application/x-bzip", "bz");
MIMETYPE_TO_EXTENSION.put("application/x-csh", "csh");
MIMETYPE_TO_EXTENSION.put("text/css", "css");
MIMETYPE_TO_EXTENSION.put("text/csv", "csv");
MIMETYPE_TO_EXTENSION.put("application/msword", "doc");
MIMETYPE_TO_EXTENSION.put(
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"docx");
MIMETYPE_TO_EXTENSION.put("application/vnd.ms-fontobject", "eot");
MIMETYPE_TO_EXTENSION.put("application/epub+zip", "epub");
MIMETYPE_TO_EXTENSION.put("image/gif", "gif");
MIMETYPE_TO_EXTENSION.put("application/gzip", "gz");
MIMETYPE_TO_EXTENSION.put("video/h263", "h263");
MIMETYPE_TO_EXTENSION.put("video/h264", "h264");
MIMETYPE_TO_EXTENSION.put("video/h265", "h265");
MIMETYPE_TO_EXTENSION.put("image/heic ", "heic");
MIMETYPE_TO_EXTENSION.put("image/heic-sequence ", "heic");
MIMETYPE_TO_EXTENSION.put("image/heif ", "heif");
MIMETYPE_TO_EXTENSION.put("image/heif-sequence", "heif");
MIMETYPE_TO_EXTENSION.put("text/html", "html");
MIMETYPE_TO_EXTENSION.put("image/vnd.microsoft.icon", "ico");
MIMETYPE_TO_EXTENSION.put("text/calendar", "ics");
MIMETYPE_TO_EXTENSION.put("application/java-archive", "jar");
MIMETYPE_TO_EXTENSION.put("image/jpeg", "jpg");
MIMETYPE_TO_EXTENSION.put("application/json", "json");
MIMETYPE_TO_EXTENSION.put("application/ld+json", "jsonld");
MIMETYPE_TO_EXTENSION.put("text/javascript", "js");
MIMETYPE_TO_EXTENSION.put("audio/midi audio/x-midi", "mid");
MIMETYPE_TO_EXTENSION.put("audio/mpeg", "mp3");
MIMETYPE_TO_EXTENSION.put("video/mp4", "mp4");
MIMETYPE_TO_EXTENSION.put("application/dash+xml", "mpd");
MIMETYPE_TO_EXTENSION.put("video/mpeg", "mpeg");
MIMETYPE_TO_EXTENSION.put("application/vnd.apple.installer+xml", "mpkg");
MIMETYPE_TO_EXTENSION.put("video/mpv", "mpv");
MIMETYPE_TO_EXTENSION.put("application/vnd.oasis.opendocument.presentation", "odp");
MIMETYPE_TO_EXTENSION.put("application/vnd.oasis.opendocument.spreadsheet", "ods");
MIMETYPE_TO_EXTENSION.put("application/vnd.oasis.opendocument.text", "odt");
MIMETYPE_TO_EXTENSION.put("audio/ogg", "oga");
MIMETYPE_TO_EXTENSION.put("video/ogg", "ogv");
MIMETYPE_TO_EXTENSION.put("application/ogg", "ogx");
MIMETYPE_TO_EXTENSION.put("audio/opus", "opus");
MIMETYPE_TO_EXTENSION.put("font/otf", "otf");
MIMETYPE_TO_EXTENSION.put("application/pdf", "pdf");
MIMETYPE_TO_EXTENSION.put("application/x-httpd-php", "php");
MIMETYPE_TO_EXTENSION.put("image/png", "png");
MIMETYPE_TO_EXTENSION.put("application/vnd.ms-powerpoint", "ppt");
MIMETYPE_TO_EXTENSION.put(
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"pptx");
MIMETYPE_TO_EXTENSION.put("application/vnd.rar", "rar");
MIMETYPE_TO_EXTENSION.put("application/rtf", "rtf");
MIMETYPE_TO_EXTENSION.put("application/x-sh", "sh");
MIMETYPE_TO_EXTENSION.put("image/svg+xml", "svg");
MIMETYPE_TO_EXTENSION.put("application/x-shockwave-flash", "swf");
MIMETYPE_TO_EXTENSION.put("application/x-tar", "tar");
MIMETYPE_TO_EXTENSION.put("image/tiff", "tiff");
MIMETYPE_TO_EXTENSION.put("video/mp2t", "ts");
MIMETYPE_TO_EXTENSION.put("font/ttf", "ttf");
MIMETYPE_TO_EXTENSION.put("text/plain", "txt");
MIMETYPE_TO_EXTENSION.put("application/vnd.visio", "vsd");
MIMETYPE_TO_EXTENSION.put("audio/wav", "wav");
MIMETYPE_TO_EXTENSION.put("audio/webm", "weba");
MIMETYPE_TO_EXTENSION.put("video/webm", "webm");
MIMETYPE_TO_EXTENSION.put("image/webp", "webp");
MIMETYPE_TO_EXTENSION.put("font/woff2", "woff2");
MIMETYPE_TO_EXTENSION.put("font/woff", "woff");
MIMETYPE_TO_EXTENSION.put("application/xhtml+xml", "xhtml");
MIMETYPE_TO_EXTENSION.put("application/vnd.ms-excel", "xls");
MIMETYPE_TO_EXTENSION.put(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"xlsx");
MIMETYPE_TO_EXTENSION.put("application/xml", "xml");
MIMETYPE_TO_EXTENSION.put("text/xml", "xml");
MIMETYPE_TO_EXTENSION.put("application/vnd.mozilla.xul+xml", "xul");
MIMETYPE_TO_EXTENSION.put("application/zip", "zip");
}
@NonNull
public static Optional<String> getExtensionForMimetype(@NonNull final String mimetype) {
final String splitType;
if (mimetype.contains(";")) {
splitType = mimetype.split(";")[0];
} else {
splitType = mimetype;
}
return Optional.ofNullable(MIMETYPE_TO_EXTENSION.get(
StringUtils.asciiLowercase(splitType)));
}
public static void moveFile(final File src, final File dst) throws IOException {
if (!src.renameTo(dst)) {
copyFile(src, dst);
if (!src.delete()) {
src.deleteOnExit();
}
}
}
public static void copyFile(final File src, final File dst) throws IOException {
try (FileInputStream fis = new FileInputStream(src)) {
copyFile(fis, dst);
}
}
public static void copyFile(final InputStream fis, final File dst) throws IOException {
try (FileOutputStream fos = new FileOutputStream(dst)) {
General.copyStream(fis, fos);
fos.flush();
}
}
public static boolean isCacheDiskFull(final Context context) {
final long space = getFreeSpaceAvailable(PrefsUtility.pref_cache_location(context));
return space < 128 * 1024 * 1024;
}
/// Get the number of free bytes that are available on the external storage.
public static long getFreeSpaceAvailable(final String path) {
final StatFs stat = new StatFs(path);
final long availableBlocks = stat.getAvailableBlocksLong();
final long blockSize = stat.getBlockSizeLong();
return availableBlocks * blockSize;
}
public static void shareImageAtUri(
@NonNull final BaseActivity activity,
@Nullable final UriString uri) {
if (uri == null) {
return;
}
downloadImageToSave(activity, uri, (info, cacheFile, mimetype) -> {
final Uri externalUri = CacheContentProvider.getUriForFile(
cacheFile.getId(),
mimetype,
getExtensionFromPath(info.original.url.value).orElse("jpg"));
Log.i(TAG, "Sharing image with external uri: " + externalUri);
final Intent shareIntent = new Intent()
.setAction(Intent.ACTION_SEND)
.putExtra(Intent.EXTRA_STREAM, externalUri)
.setType(mimetype)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Workaround for third party share apps
shareIntent.setClipData(ClipData.newRawUri(null, externalUri));
if (PrefsUtility.pref_behaviour_sharing_dialog()) {
ShareOrderDialog.newInstance(shareIntent)
.show(activity.getSupportFragmentManager(), null);
} else {
activity.startActivity(Intent.createChooser(
shareIntent,
activity.getString(R.string.action_share)));
}
});
}
private interface FileDataSource {
void writeTo(@NonNull OutputStream outputStream) throws IOException;
}
private static class CacheFileDataSource implements FileDataSource {
@NonNull
private final CacheManager.ReadableCacheFile mCacheFile;
private CacheFileDataSource(
@NonNull final CacheManager.ReadableCacheFile cacheFile) {
mCacheFile = cacheFile;
}
@Override
public void writeTo(@NonNull final OutputStream outputStream) throws IOException {
try (InputStream inputStream = mCacheFile.getInputStream()) {
General.copyStream(inputStream, outputStream);
outputStream.flush();
}
}
}
@RequiresApi(Build.VERSION_CODES.Q)
private static void mediaStoreDownloadsInsertFile(
@NonNull final BaseActivity activity,
@NonNull final String name,
@Nullable final String mimetype,
final long fileSize,
@NonNull final FileDataSource source,
@NonNull final Runnable onSuccess) {
final Uri downloads = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL);
Log.i(TAG, "Got downloads URI: " + downloads.toString());
final ContentValues fileMetadata = new ContentValues();
fileMetadata.put(MediaStore.Downloads.DISPLAY_NAME, name);
fileMetadata.put(MediaStore.Downloads.SIZE, fileSize);
if (mimetype != null) {
fileMetadata.put(MediaStore.Downloads.MIME_TYPE, mimetype);
}
fileMetadata.put(MediaStore.Downloads.IS_PENDING, true);
final ContentResolver resolver = activity.getContentResolver();
final Uri fileUri = resolver.insert(downloads, fileMetadata);
Log.i(TAG, "Got file URI: " + fileUri.toString());
new Thread(() -> {
try (OutputStream os = resolver.openOutputStream(fileUri)) {
source.writeTo(os);
os.flush();
} catch (final IOException e) {
showUnexpectedStorageErrorDialog(
activity,
e,
new UriString(fileUri.toString()));
resolver.delete(fileUri, null, null);
return;
}
fileMetadata.put(MediaStore.Downloads.IS_PENDING, false);
resolver.update(fileUri, fileMetadata, null, null);
onSuccess.run();
}).start();
}
private static void createSAFDocumentWithIntent(
@NonNull final BaseActivity activity,
@NonNull final String filename,
@Nullable final String mimetype,
@NonNull final FileDataSource source,
@NonNull final Runnable onSuccess) {
final Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT)
.setType(mimetype)
.putExtra(Intent.EXTRA_TITLE, filename)
.addCategory(Intent.CATEGORY_OPENABLE);
try {
activity.startActivityForResultWithCallback(
intent,
(resultCode, data) -> {
if (data == null || data.getData() == null) {
return;
}
new Thread(() -> {
try (OutputStream outputStream = activity.getContentResolver()
.openOutputStream(data.getData())) {
source.writeTo(outputStream);
onSuccess.run();
} catch (final IOException e) {
showUnexpectedStorageErrorDialog(
activity,
e,
new UriString(data.getData().toString()));
}
}).start();
});
} catch (final ActivityNotFoundException e) {
DialogUtils.showDialog(
activity,
R.string.error_no_file_manager_title,
R.string.error_no_file_manager_message);
}
}
public static void saveImageAtUri(
@NonNull final BaseActivity activity,
@Nullable final UriString uri) {
if (uri == null) {
return;
}
final PrefsUtility.SaveLocation saveLocation
= PrefsUtility.pref_behaviour_save_location();
switch (saveLocation) {
case PROMPT_EVERY_TIME: {
downloadImageToSave(activity, uri, (info, cacheFile, mimetype) -> {
final String filename = General.filenameFromString(info.original.url.value);
createSAFDocumentWithIntent(
activity,
filename,
mimetype,
new CacheFileDataSource(cacheFile),
() -> General.quickToast(
activity,
R.string.action_save_image_success_no_path));
});
break;
}
case SYSTEM_DEFAULT: {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Log.i(TAG, "Android version Q or higher, saving with MediaStore");
downloadImageToSave(activity, uri, (info, cacheFile, mimetype) -> {
final String filename = General.filenameFromString(info.original.url.value);
mediaStoreDownloadsInsertFile(
activity,
filename,
mimetype,
cacheFile.getFile().map(File::length).orElse(0L),
new CacheFileDataSource(cacheFile),
() -> General.quickToast(
activity,
R.string.action_save_image_success_no_path));
});
} else {
Log.i(TAG, "Android version below Q, saving with legacy method");
activity.requestPermissionWithCallback(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
new LegacySaveImageCallback(activity, uri));
}
break;
}
default: {
BugReportActivity.handleGlobalError(activity, new RuntimeException(
"Missing handler for preference value " + saveLocation));
}
}
}
private static void showUnexpectedStorageErrorDialog(
@NonNull final BaseActivity activity,
@NonNull final Throwable throwable,
@NonNull final UriString uri) {
General.showResultDialog(activity, new RRError(
activity.getString(R.string.error_unexpected_storage_title),
activity.getString(R.string.error_unexpected_storage_message),
true,
throwable,
null,
uri,
null));
}
public interface DownloadImageToSaveSuccessCallback {
void onSuccess(
@NonNull ImageInfo info,
CacheManager.ReadableCacheFile cacheFile,
String mimetype);
}
private static void internalDownloadImageToSaveAudio(
@NonNull final BaseActivity activity,
@NonNull final ImageInfo info,
@NonNull final CacheManager.ReadableCacheFile video,
@NonNull final DownloadImageToSaveSuccessCallback callback) {
final CacheManager cacheManager = CacheManager.getInstance(activity);
cacheManager.makeRequest(new CacheRequest(
info.urlAudioStream,
RedditAccountManager.getAnon(),
null,
new Priority(Constants.Priority.IMAGE_VIEW),
DownloadStrategyIfNotCached.INSTANCE,
Constants.FileType.IMAGE,
CacheRequest.DownloadQueueType.IMMEDIATE,
CacheRequest.RequestMethod.GET,
activity,
new CacheRequestCallbacks() {
@Override
public void onFailure(@NonNull final RRError error) {
General.showResultDialog(activity, error);
}
@Override
public void onCacheFileWritten(
@NonNull final CacheManager.ReadableCacheFile cacheFile,
final TimestampUTC timestamp,
@NonNull final UUID session,
final boolean fromCache,
final String mimetype) {
try {
final CacheManager.WritableCacheFile output
= cacheManager.openNewCacheFile(
new UriString(
"redreader://muxedmedia/"
+ UUID.randomUUID()
+ ".mp4"),
RedditAccountManager.getAnon(),
Constants.FileType.IMAGE,
session,
"video/mp4",
CacheCompressionType.NONE);
final File file = output.writeExternally();
MediaUtils.muxFiles(
file,
new File[]{
cacheFile.getFile().orThrow(() -> new RuntimeException(
"Audio file not found")),
video.getFile().orThrow(() -> new RuntimeException(
"Video file not found"))
},
() -> {
try {
output.onWriteFinished();
callback.onSuccess(
info,
output.getReadableCacheFile(),
"video/mp4");
} catch (final Exception e) {
General.showResultDialog(
activity,
General.getGeneralErrorForFailure(
activity,
CacheRequest.RequestFailureType.STORAGE,
e,
null,
info.original.url,
Optional.empty()));
}
},
e -> General.showResultDialog(
activity,
new RRError(
activity.getResources().getString(
R.string.error_title_muxing_failed),
activity.getResources().getString(
R.string.error_message_muxing_failed),
true,
e,
null,
info.original.url,
null)));
} catch (final Exception e) {
General.showResultDialog(
activity,
General.getGeneralErrorForFailure(
activity,
CacheRequest.RequestFailureType.STORAGE,
e,
null,
info.original.url,
Optional.empty()));
}
}
}));
}
public static void downloadImageToSave(
@NonNull final BaseActivity activity,
@NonNull final UriString uri,
@NonNull final DownloadImageToSaveSuccessCallback callback) {
LinkHandler.getImageInfo(
activity,
uri,
new Priority(Constants.Priority.IMAGE_VIEW),
new GetImageInfoListener() {
@Override
public void onFailure(@NonNull final RRError error) {
General.showResultDialog(activity, error);
}
@Override
public void onSuccess(final ImageInfo info) {
CacheManager.getInstance(activity).makeRequest(new CacheRequest(
info.original.url,
RedditAccountManager.getAnon(),
null,
new Priority(Constants.Priority.IMAGE_VIEW),
DownloadStrategyIfNotCached.INSTANCE,
Constants.FileType.IMAGE,
CacheRequest.DownloadQueueType.IMMEDIATE,
CacheRequest.RequestMethod.GET,
activity,
new CacheRequestCallbacks() {
@Override
public void onDownloadNecessary() {
General.quickToast(
activity,
R.string.download_downloading,
Toast.LENGTH_SHORT);
}
@Override
public void onFailure(@NonNull final RRError error) {
General.showResultDialog(activity, error);
}
@Override
public void onCacheFileWritten(
@NonNull final CacheManager.ReadableCacheFile cacheFile,
final TimestampUTC timestamp,
@NonNull final UUID session,
final boolean fromCache,
final String mimetype) {
if (info.urlAudioStream != null) {
Log.i(TAG, "Also downloading audio stream...");
internalDownloadImageToSaveAudio(
activity,
info,
cacheFile,
callback);
} else {
callback.onSuccess(info, cacheFile, mimetype);
}
}
}));
}
@Override
public void onNotAnImage() {
General.quickToast(activity, R.string.selected_link_is_not_image);
}
});
}
@NonNull
public static Optional<String> getExtensionFromPath(@NonNull final String path) {
final String[] pathSegments = path.split("/");
if (pathSegments.length == 0) {
return Optional.empty();
}
final String[] dotSegments = pathSegments[pathSegments.length - 1].split("\\.");
if (dotSegments.length < 2) {
return Optional.empty();
}
if (dotSegments.length == 2 && dotSegments[0].isEmpty()) {
return Optional.empty();
}
return Optional.of(dotSegments[dotSegments.length - 1]);
}
@NonNull
public static File buildPath(
@NonNull final File base,
@NonNull final String... components) {
File result = base;
for (final String component : components) {
result = new File(result, component);
}
return result;
}
@NonNull
private static final Object sMkdirsLock = new Object();
public static void mkdirs(@NonNull final File file) throws IOException {
synchronized (sMkdirsLock) {
if (file.isDirectory()) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {
Files.createDirectories(file.toPath());
} catch (final Exception e) {
throw new IOException(
"Failed to create dirs " + file.getAbsolutePath(),
e);
}
} else {
if (!file.mkdirs()) {
throw new IOException("Failed to create dirs " + file.getAbsolutePath());
}
}
}
}
}