Search in sources :

Example 41 with WorkerThread

use of android.support.annotation.WorkerThread in project Signal-Android by WhisperSystems.

the class WebRtcCallService method isCallActive.

@WorkerThread
public static boolean isCallActive(Context context) {
    Log.w(TAG, "isCallActive()");
    HandlerThread handlerThread = null;
    try {
        handlerThread = new HandlerThread("webrtc-callback");
        handlerThread.start();
        final SettableFuture<Boolean> future = new SettableFuture<>();
        ResultReceiver resultReceiver = new ResultReceiver(new Handler(handlerThread.getLooper())) {

            protected void onReceiveResult(int resultCode, Bundle resultData) {
                Log.w(TAG, "Got result...");
                future.set(resultCode == 1);
            }
        };
        Intent intent = new Intent(context, WebRtcCallService.class);
        intent.setAction(ACTION_IS_IN_CALL_QUERY);
        intent.putExtra(EXTRA_RESULT_RECEIVER, resultReceiver);
        context.startService(intent);
        Log.w(TAG, "Blocking on result...");
        return future.get();
    } catch (InterruptedException | ExecutionException e) {
        Log.w(TAG, e);
        return false;
    } finally {
        if (handlerThread != null)
            handlerThread.quit();
    }
}
Also used : SettableFuture(org.whispersystems.signalservice.internal.util.concurrent.SettableFuture) HandlerThread(android.os.HandlerThread) Bundle(android.os.Bundle) Handler(android.os.Handler) Intent(android.content.Intent) ExecutionException(java.util.concurrent.ExecutionException) ResultReceiver(android.os.ResultReceiver) WorkerThread(android.support.annotation.WorkerThread)

Example 42 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 DeleteResolver<T> deleteResolver;
        if (explicitDeleteResolver != null) {
            deleteResolver = explicitDeleteResolver;
        } else {
            final ContentResolverTypeMapping<T> typeMapping = storIOContentResolver.lowLevel().typeMapping((Class<T>) object.getClass());
            if (typeMapping == null) {
                throw new IllegalStateException("Object 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");
            }
            deleteResolver = typeMapping.deleteResolver();
        }
        return deleteResolver.performDelete(storIOContentResolver, object);
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Delete operation. object = " + object, exception);
    }
}
Also used : StorIOException(com.pushtorefresh.storio.StorIOException) StorIOException(com.pushtorefresh.storio.StorIOException) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 43 with WorkerThread

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

the class PreparedPutObject 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.
     */
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public PutResult executeAsBlocking() {
    try {
        final PutResolver<T> putResolver;
        if (explicitPutResolver != null) {
            putResolver = explicitPutResolver;
        } else {
            final ContentResolverTypeMapping<T> typeMapping = storIOContentResolver.lowLevel().typeMapping((Class<T>) object.getClass());
            if (typeMapping == null) {
                throw new IllegalStateException("Object 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");
            }
            putResolver = typeMapping.putResolver();
        }
        return putResolver.performPut(storIOContentResolver, object);
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Put operation. object = " + object, exception);
    }
}
Also used : StorIOException(com.pushtorefresh.storio.StorIOException) StorIOException(com.pushtorefresh.storio.StorIOException) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 44 with WorkerThread

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

the class PreparedPutObject 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 PutResult executeAsBlocking() {
    try {
        final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
        final PutResolver<T> putResolver;
        if (explicitPutResolver != null) {
            putResolver = explicitPutResolver;
        } 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");
            }
            putResolver = typeMapping.putResolver();
        }
        final PutResult putResult = putResolver.performPut(storIOSQLite, object);
        if (putResult.wasInserted() || putResult.wasUpdated()) {
            lowLevel.notifyAboutChanges(Changes.newInstance(putResult.affectedTables()));
        }
        return putResult;
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Put 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)

Example 45 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 StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
        // Nullable
        final List<SimpleImmutableEntry<T, DeleteResolver<T>>> objectsAndDeleteResolvers;
        if (explicitDeleteResolver != null) {
            objectsAndDeleteResolvers = null;
        } else {
            objectsAndDeleteResolvers = new ArrayList<SimpleImmutableEntry<T, DeleteResolver<T>>>(objects.size());
            for (final T object : objects) {
                final SQLiteTypeMapping<T> typeMapping = (SQLiteTypeMapping<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() + "," + "db was not affected by this operation, please add type mapping for this type");
                }
                objectsAndDeleteResolvers.add(new SimpleImmutableEntry<T, DeleteResolver<T>>(object, typeMapping.deleteResolver()));
            }
        }
        if (useTransaction) {
            lowLevel.beginTransaction();
        }
        final Map<T, DeleteResult> results = new HashMap<T, DeleteResult>(objects.size());
        boolean transactionSuccessful = false;
        try {
            if (explicitDeleteResolver != null) {
                for (final T object : objects) {
                    final DeleteResult deleteResult = explicitDeleteResolver.performDelete(storIOSQLite, object);
                    results.put(object, deleteResult);
                    if (!useTransaction && deleteResult.numberOfRowsDeleted() > 0) {
                        lowLevel.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables()));
                    }
                }
            } 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(storIOSQLite, object);
                    results.put(object, deleteResult);
                    if (!useTransaction && deleteResult.numberOfRowsDeleted() > 0) {
                        lowLevel.notifyAboutChanges(Changes.newInstance(deleteResult.affectedTables()));
                    }
                }
            }
            if (useTransaction) {
                lowLevel.setTransactionSuccessful();
                transactionSuccessful = true;
            }
        } finally {
            if (useTransaction) {
                lowLevel.endTransaction();
                // if delete was in transaction and it was successful -> notify about changes
                if (transactionSuccessful) {
                    // in most cases it will be one table
                    final Set<String> affectedTables = new HashSet<String>(1);
                    for (final T object : results.keySet()) {
                        final DeleteResult deleteResult = results.get(object);
                        if (deleteResult.numberOfRowsDeleted() > 0) {
                            affectedTables.addAll(results.get(object).affectedTables());
                        }
                    }
                    // It'll reduce number of possible deadlock situations
                    if (!affectedTables.isEmpty()) {
                        lowLevel.notifyAboutChanges(Changes.newInstance(affectedTables));
                    }
                }
            }
        }
        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) StorIOException(com.pushtorefresh.storio.StorIOException) StorIOException(com.pushtorefresh.storio.StorIOException) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) SQLiteTypeMapping(com.pushtorefresh.storio.sqlite.SQLiteTypeMapping) HashSet(java.util.HashSet) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Aggregations

WorkerThread (android.support.annotation.WorkerThread)58 NonNull (android.support.annotation.NonNull)21 Cursor (android.database.Cursor)17 StorIOException (com.pushtorefresh.storio.StorIOException)15 File (java.io.File)13 ArrayList (java.util.ArrayList)13 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 InputStream (java.io.InputStream)3 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)3 List (java.util.List)3 ContentUris (android.content.ContentUris)2