Search in sources :

Example 6 with WorkerThread

use of android.support.annotation.WorkerThread in project storio by pushtorefresh.

the class PreparedGetObject method executeAsBlocking.

/**
     * Executes Prepared Operation immediately in current thread.
     * <p>
     * Notice: This is blocking I/O operation that should not be executed on the Main Thread,
     * it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
     * So please, call this method on some background thread. See {@link WorkerThread}.
     *
     *  @return single instance of mapped result. Can be {@code null}, if no items are found.
     */
@SuppressWarnings({ "ConstantConditions", "NullableProblems" })
@WorkerThread
@Nullable
@Override
public T executeAsBlocking() {
    try {
        final GetResolver<T> getResolver;
        if (explicitGetResolver != null) {
            getResolver = explicitGetResolver;
        } else {
            final ContentResolverTypeMapping<T> typeMapping = storIOContentResolver.lowLevel().typeMapping(type);
            if (typeMapping == null) {
                throw new IllegalStateException("This type does not have type mapping: " + "type = " + type + "," + "ContentProvider was not touched by this operation, please add type mapping for this type");
            }
            getResolver = typeMapping.getResolver();
        }
        final Cursor cursor = getResolver.performGet(storIOContentResolver, query);
        try {
            final int count = cursor.getCount();
            if (count == 0) {
                return null;
            }
            cursor.moveToFirst();
            return getResolver.mapFromCursor(cursor);
        } finally {
            cursor.close();
        }
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Get operation. query = " + query, exception);
    }
}
Also used : StorIOException(com.pushtorefresh.storio.StorIOException) Cursor(android.database.Cursor) StorIOException(com.pushtorefresh.storio.StorIOException) WorkerThread(android.support.annotation.WorkerThread) Nullable(android.support.annotation.Nullable)

Example 7 with WorkerThread

use of android.support.annotation.WorkerThread in project storio by pushtorefresh.

the class PreparedPutCollectionOfObjects method executeAsBlocking.

/**
     * Executes Put Operation immediately in current thread.
     * <p>
     * Notice: This is blocking I/O operation that should not be executed on the Main Thread,
     * it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
     * So please, call this method on some background thread. See {@link WorkerThread}.
     *
     * @return non-null result of Put Operation.
     */
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public PutResults<T> executeAsBlocking() {
    try {
        final StorIOContentResolver.LowLevel lowLevel = storIOContentResolver.lowLevel();
        // Nullable
        final List<SimpleImmutableEntry<T, PutResolver<T>>> objectsAndPutResolvers;
        if (explicitPutResolver != null) {
            objectsAndPutResolvers = null;
        } else {
            objectsAndPutResolvers = new ArrayList<SimpleImmutableEntry<T, PutResolver<T>>>(objects.size());
            for (final T object : objects) {
                final ContentResolverTypeMapping<T> typeMapping = (ContentResolverTypeMapping<T>) lowLevel.typeMapping(object.getClass());
                if (typeMapping == null) {
                    throw new IllegalStateException("One of the objects from the collection does not have type mapping: " + "object = " + object + ", object.class = " + object.getClass() + "," + "ContentProvider was not affected by this operation, please add type mapping for this type");
                }
                objectsAndPutResolvers.add(new SimpleImmutableEntry<T, PutResolver<T>>(object, typeMapping.putResolver()));
            }
        }
        final Map<T, PutResult> results = new HashMap<T, PutResult>(objects.size());
        if (explicitPutResolver != null) {
            for (final T object : objects) {
                final PutResult putResult = explicitPutResolver.performPut(storIOContentResolver, object);
                results.put(object, putResult);
            }
        } else {
            for (final SimpleImmutableEntry<T, PutResolver<T>> objectAndPutResolver : objectsAndPutResolvers) {
                final T object = objectAndPutResolver.getKey();
                final PutResolver<T> putResolver = objectAndPutResolver.getValue();
                final PutResult putResult = putResolver.performPut(storIOContentResolver, object);
                results.put(object, putResult);
            }
        }
        return PutResults.newInstance(results);
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Put operation. objects = " + objects, exception);
    }
}
Also used : HashMap(java.util.HashMap) ContentResolverTypeMapping(com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping) StorIOException(com.pushtorefresh.storio.StorIOException) StorIOException(com.pushtorefresh.storio.StorIOException) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 8 with WorkerThread

use of android.support.annotation.WorkerThread in project storio by pushtorefresh.

the class PreparedPutContentValuesIterable method executeAsBlocking.

/**
     * Executes Put Operation immediately in current thread.
     * <p>
     * Notice: This is blocking I/O operation that should not be executed on the Main Thread,
     * it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
     * So please, call this method on some background thread. See {@link WorkerThread}.
     *
     * @return non-null results of Put Operation.
     */
@WorkerThread
@NonNull
@Override
public PutResults<ContentValues> executeAsBlocking() {
    try {
        final Map<ContentValues, PutResult> putResultsMap = new HashMap<ContentValues, PutResult>();
        for (final ContentValues cv : contentValues) {
            final PutResult putResult = putResolver.performPut(storIOContentResolver, cv);
            putResultsMap.put(cv, putResult);
        }
        return PutResults.newInstance(putResultsMap);
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Put operation. contentValues = " + contentValues, exception);
    }
}
Also used : ContentValues(android.content.ContentValues) StorIOException(com.pushtorefresh.storio.StorIOException) HashMap(java.util.HashMap) StorIOException(com.pushtorefresh.storio.StorIOException) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 9 with WorkerThread

use of android.support.annotation.WorkerThread in project storio by pushtorefresh.

the class PreparedDeleteCollectionOfObjects method executeAsBlocking.

/**
     * Executes Delete Operation immediately in current thread.
     * <p>
     * Notice: This is blocking I/O operation that should not be executed on the Main Thread,
     * it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
     * So please, call this method on some background thread. See {@link WorkerThread}.
     *
     * @return non-null results of Delete Operation.
     */
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public DeleteResults<T> executeAsBlocking() {
    try {
        final StorIOContentResolver.LowLevel lowLevel = storIOContentResolver.lowLevel();
        // Nullable
        final List<SimpleImmutableEntry> objectsAndDeleteResolvers;
        if (explicitDeleteResolver != null) {
            objectsAndDeleteResolvers = null;
        } else {
            objectsAndDeleteResolvers = new ArrayList<SimpleImmutableEntry>(objects.size());
            for (final T object : objects) {
                final ContentResolverTypeMapping<T> typeMapping = (ContentResolverTypeMapping<T>) lowLevel.typeMapping(object.getClass());
                if (typeMapping == null) {
                    throw new IllegalStateException("One of the objects from the collection does not have type mapping: " + "object = " + object + ", object.class = " + object.getClass() + "," + "ContentProvider was not affected by this operation, please add type mapping for this type");
                }
                objectsAndDeleteResolvers.add(new SimpleImmutableEntry(object, typeMapping.deleteResolver()));
            }
        }
        final Map<T, DeleteResult> results = new HashMap<T, DeleteResult>(objects.size());
        if (explicitDeleteResolver != null) {
            for (final T object : objects) {
                final DeleteResult deleteResult = explicitDeleteResolver.performDelete(storIOContentResolver, object);
                results.put(object, deleteResult);
            }
        } else {
            for (final SimpleImmutableEntry<T, DeleteResolver<T>> objectAndDeleteResolver : objectsAndDeleteResolvers) {
                final T object = objectAndDeleteResolver.getKey();
                final DeleteResolver<T> deleteResolver = objectAndDeleteResolver.getValue();
                final DeleteResult deleteResult = deleteResolver.performDelete(storIOContentResolver, object);
                results.put(object, deleteResult);
            }
        }
        return DeleteResults.newInstance(results);
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Delete operation. objects = " + objects, exception);
    }
}
Also used : HashMap(java.util.HashMap) ContentResolverTypeMapping(com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping) StorIOException(com.pushtorefresh.storio.StorIOException) StorIOException(com.pushtorefresh.storio.StorIOException) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 10 with WorkerThread

use of android.support.annotation.WorkerThread in project storio by pushtorefresh.

the class PreparedDeleteObject method executeAsBlocking.

/**
     * Executes Delete Operation immediately in current thread.
     * <p>
     * Notice: This is blocking I/O operation that should not be executed on the Main Thread,
     * it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
     * So please, call this method on some background thread. See {@link WorkerThread}.
     *
     * @return non-null result of Delete Operation.
     */
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public DeleteResult executeAsBlocking() {
    try {
        final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
        final DeleteResolver<T> deleteResolver;
        if (explicitDeleteResolver != null) {
            deleteResolver = explicitDeleteResolver;
        } else {
            final SQLiteTypeMapping<T> typeMapping = lowLevel.typeMapping((Class<T>) object.getClass());
            if (typeMapping == null) {
                throw new IllegalStateException("Object does not have type mapping: " + "object = " + object + ", object.class = " + object.getClass() + ", " + "db was not affected by this operation, please add type mapping for this type");
            }
            deleteResolver = typeMapping.deleteResolver();
        }
        final DeleteResult deleteResult = deleteResolver.performDelete(storIOSQLite, object);
        if (deleteResult.numberOfRowsDeleted() > 0) {
            lowLevel.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables()));
        }
        return deleteResult;
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Delete operation. object = " + object, exception);
    }
}
Also used : StorIOException(com.pushtorefresh.storio.StorIOException) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) StorIOException(com.pushtorefresh.storio.StorIOException) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Aggregations

WorkerThread (android.support.annotation.WorkerThread)52 NonNull (android.support.annotation.NonNull)21 Cursor (android.database.Cursor)17 StorIOException (com.pushtorefresh.storio.StorIOException)15 File (java.io.File)11 ArrayList (java.util.ArrayList)11 Uri (android.net.Uri)8 ContentValues (android.content.ContentValues)6 Nullable (android.support.annotation.Nullable)6 StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)6 HashMap (java.util.HashMap)6 HistoryItem (acr.browser.lightning.database.HistoryItem)5 IOException (java.io.IOException)5 FileInputStream (java.io.FileInputStream)4 Intent (android.content.Intent)3 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)3 ContentUris (android.content.ContentUris)2