use of androidx.annotation.WorkerThread in project ExoPlayer by google.
the class CacheFileMetadataIndex method remove.
/**
* Removes metadata.
*
* <p>This method may be slow and shouldn't normally be called on the main thread.
*
* @param name The name of the file whose metadata is to be removed.
* @throws DatabaseIOException If an error occurs removing the metadata.
*/
@WorkerThread
public void remove(String name) throws DatabaseIOException {
Assertions.checkNotNull(tableName);
try {
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
writableDatabase.delete(tableName, WHERE_NAME_EQUALS, new String[] { name });
} catch (SQLException e) {
throw new DatabaseIOException(e);
}
}
use of androidx.annotation.WorkerThread in project ExoPlayer by google.
the class CacheFileMetadataIndex method removeAll.
/**
* Removes metadata.
*
* <p>This method may be slow and shouldn't normally be called on the main thread.
*
* @param names The names of the files whose metadata is to be removed.
* @throws DatabaseIOException If an error occurs removing the metadata.
*/
@WorkerThread
public void removeAll(Set<String> names) throws DatabaseIOException {
Assertions.checkNotNull(tableName);
try {
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
writableDatabase.beginTransactionNonExclusive();
try {
for (String name : names) {
writableDatabase.delete(tableName, WHERE_NAME_EQUALS, new String[] { name });
}
writableDatabase.setTransactionSuccessful();
} finally {
writableDatabase.endTransaction();
}
} catch (SQLException e) {
throw new DatabaseIOException(e);
}
}
use of androidx.annotation.WorkerThread in project lottie-android by airbnb.
the class LottieCompositionFactory method fromZipStreamSyncInternal.
@WorkerThread
private static LottieResult<LottieComposition> fromZipStreamSyncInternal(ZipInputStream inputStream, @Nullable String cacheKey) {
LottieComposition composition = null;
Map<String, Bitmap> images = new HashMap<>();
try {
ZipEntry entry = inputStream.getNextEntry();
while (entry != null) {
final String entryName = entry.getName();
if (entryName.contains("__MACOSX")) {
inputStream.closeEntry();
} else if (entry.getName().equalsIgnoreCase("manifest.json")) {
// ignore .lottie manifest
inputStream.closeEntry();
} else if (entry.getName().contains(".json")) {
com.airbnb.lottie.parser.moshi.JsonReader reader = JsonReader.of(buffer(source(inputStream)));
composition = LottieCompositionFactory.fromJsonReaderSyncInternal(reader, null, false).getValue();
} else if (entryName.contains(".png") || entryName.contains(".webp") || entryName.contains(".jpg") || entryName.contains(".jpeg")) {
String[] splitName = entryName.split("/");
String name = splitName[splitName.length - 1];
images.put(name, BitmapFactory.decodeStream(inputStream));
} else {
inputStream.closeEntry();
}
entry = inputStream.getNextEntry();
}
} catch (IOException e) {
return new LottieResult<>(e);
}
if (composition == null) {
return new LottieResult<>(new IllegalArgumentException("Unable to parse composition"));
}
for (Map.Entry<String, Bitmap> e : images.entrySet()) {
LottieImageAsset imageAsset = findImageAssetForFileName(composition, e.getKey());
if (imageAsset != null) {
imageAsset.setBitmap(Utils.resizeBitmapIfNeeded(e.getValue(), imageAsset.getWidth(), imageAsset.getHeight()));
}
}
// Ensure that all bitmaps have been set.
for (Map.Entry<String, LottieImageAsset> entry : composition.getImages().entrySet()) {
if (entry.getValue().getBitmap() == null) {
return new LottieResult<>(new IllegalStateException("There is no image for " + entry.getValue().getFileName()));
}
}
if (cacheKey != null) {
LottieCompositionCache.getInstance().put(cacheKey, composition);
}
return new LottieResult<>(composition);
}
use of androidx.annotation.WorkerThread in project lottie-android by airbnb.
the class NetworkCache method fetch.
/**
* If the animation doesn't exist in the cache, null will be returned.
* <p>
* Once the animation is successfully parsed, {@link #renameTempFile(String, FileExtension)} must be
* called to move the file from a temporary location to its permanent cache location so it can
* be used in the future.
*/
@Nullable
@WorkerThread
Pair<FileExtension, InputStream> fetch(String url) {
File cachedFile;
try {
cachedFile = getCachedFile(url);
} catch (FileNotFoundException e) {
return null;
}
if (cachedFile == null) {
return null;
}
FileInputStream inputStream;
try {
inputStream = new FileInputStream(cachedFile);
} catch (FileNotFoundException e) {
return null;
}
FileExtension extension;
if (cachedFile.getAbsolutePath().endsWith(".zip")) {
extension = FileExtension.ZIP;
} else {
extension = FileExtension.JSON;
}
Logger.debug("Cache hit for " + url + " at " + cachedFile.getAbsolutePath());
return new Pair<>(extension, (InputStream) inputStream);
}
use of androidx.annotation.WorkerThread in project Signal-Android by WhisperSystems.
the class AttachmentCompressionJob method compressImage.
/**
* Compresses the images. Given that we compress every image, this has the fun side effect of
* stripping all EXIF data.
*/
@WorkerThread
private static MediaStream compressImage(@NonNull Context context, @NonNull Attachment attachment, @NonNull MediaConstraints mediaConstraints) throws UndeliverableMessageException {
Uri uri = attachment.getUri();
if (uri == null) {
throw new UndeliverableMessageException("No attachment URI!");
}
ImageCompressionUtil.Result result = null;
try {
for (int size : mediaConstraints.getImageDimensionTargets(context)) {
result = ImageCompressionUtil.compressWithinConstraints(context, attachment.getContentType(), new DecryptableStreamUriLoader.DecryptableUri(uri), size, mediaConstraints.getImageMaxSize(context), mediaConstraints.getImageCompressionQualitySetting(context));
if (result != null) {
break;
}
}
} catch (BitmapDecodingException e) {
throw new UndeliverableMessageException(e);
}
if (result == null) {
throw new UndeliverableMessageException("Somehow couldn't meet the constraints!");
}
return new MediaStream(new ByteArrayInputStream(result.getData()), result.getMimeType(), result.getWidth(), result.getHeight());
}
Aggregations