Search in sources :

Example 11 with WorkerThread

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);
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) DatabaseIOException(com.google.android.exoplayer2.database.DatabaseIOException) WorkerThread(androidx.annotation.WorkerThread)

Example 12 with WorkerThread

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);
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) DatabaseIOException(com.google.android.exoplayer2.database.DatabaseIOException) WorkerThread(androidx.annotation.WorkerThread)

Example 13 with WorkerThread

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);
}
Also used : HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) Bitmap(android.graphics.Bitmap) JsonReader(com.airbnb.lottie.parser.moshi.JsonReader) HashMap(java.util.HashMap) Map(java.util.Map) WorkerThread(androidx.annotation.WorkerThread)

Example 14 with WorkerThread

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);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream) Pair(android.util.Pair) WorkerThread(androidx.annotation.WorkerThread) Nullable(androidx.annotation.Nullable)

Example 15 with WorkerThread

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());
}
Also used : MediaStream(org.thoughtcrime.securesms.mms.MediaStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageCompressionUtil(org.thoughtcrime.securesms.util.ImageCompressionUtil) UndeliverableMessageException(org.thoughtcrime.securesms.transport.UndeliverableMessageException) Uri(android.net.Uri) NetworkConstraint(org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint) BitmapDecodingException(org.thoughtcrime.securesms.util.BitmapDecodingException) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

WorkerThread (androidx.annotation.WorkerThread)204 NonNull (androidx.annotation.NonNull)77 IOException (java.io.IOException)45 Recipient (org.thoughtcrime.securesms.recipients.Recipient)41 Nullable (androidx.annotation.Nullable)26 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)23 Cursor (android.database.Cursor)22 ArrayList (java.util.ArrayList)22 Context (android.content.Context)20 Uri (android.net.Uri)18 LinkedList (java.util.LinkedList)15 List (java.util.List)15 HashMap (java.util.HashMap)14 RecipientDatabase (org.thoughtcrime.securesms.database.RecipientDatabase)14 Stream (com.annimon.stream.Stream)13 Log (org.signal.core.util.logging.Log)13 HashSet (java.util.HashSet)12 InputStream (java.io.InputStream)11 Bitmap (android.graphics.Bitmap)10 Collections (java.util.Collections)10