Search in sources :

Example 46 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)

Example 47 with WorkerThread

use of android.support.annotation.WorkerThread in project sqlbrite by square.

the class BriteDatabase method delete.

/**
   * Delete rows from the specified {@code table} and notify any subscribed queries. This method
   * will not trigger a notification if no rows were deleted.
   *
   * @see SQLiteDatabase#delete(String, String, String[])
   */
@WorkerThread
public int delete(@NonNull String table, @Nullable String whereClause, @Nullable String... whereArgs) {
    SQLiteDatabase db = getWritableDatabase();
    if (logging) {
        log("DELETE\n  table: %s\n  whereClause: %s\n  whereArgs: %s", table, whereClause, Arrays.toString(whereArgs));
    }
    int rows = db.delete(table, whereClause, whereArgs);
    if (logging)
        log("DELETE affected %s %s", rows, rows != 1 ? "rows" : "row");
    if (rows > 0) {
        // Only send a table trigger if rows were affected.
        sendTableTrigger(Collections.singleton(table));
    }
    return rows;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) WorkerThread(android.support.annotation.WorkerThread)

Example 48 with WorkerThread

use of android.support.annotation.WorkerThread in project sqlbrite by square.

the class BriteDatabase method update.

/**
   * Update rows in the specified {@code table} and notify any subscribed queries. This method
   * will not trigger a notification if no rows were updated.
   *
   * @see SQLiteDatabase#updateWithOnConflict(String, ContentValues, String, String[], int)
   */
@WorkerThread
public int update(@NonNull String table, @NonNull ContentValues values, @ConflictAlgorithm int conflictAlgorithm, @Nullable String whereClause, @Nullable String... whereArgs) {
    SQLiteDatabase db = getWritableDatabase();
    if (logging) {
        log("UPDATE\n  table: %s\n  values: %s\n  whereClause: %s\n  whereArgs: %s\n  conflictAlgorithm: %s", table, values, whereClause, Arrays.toString(whereArgs), conflictString(conflictAlgorithm));
    }
    int rows = db.updateWithOnConflict(table, values, whereClause, whereArgs, conflictAlgorithm);
    if (logging)
        log("UPDATE affected %s %s", rows, rows != 1 ? "rows" : "row");
    if (rows > 0) {
        // Only send a table trigger if rows were affected.
        sendTableTrigger(Collections.singleton(table));
    }
    return rows;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) WorkerThread(android.support.annotation.WorkerThread)

Example 49 with WorkerThread

use of android.support.annotation.WorkerThread in project AndroidChromium by JackyAndroid.

the class DeferredStartupHandler method removeSnapshotDatabase.

/**
     * Deletes the snapshot database which is no longer used because the feature has been removed
     * in Chrome M41.
     */
@WorkerThread
private void removeSnapshotDatabase() {
    synchronized (SNAPSHOT_DATABASE_LOCK) {
        SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
        if (!prefs.getBoolean(SNAPSHOT_DATABASE_REMOVED, false)) {
            mAppContext.deleteDatabase(SNAPSHOT_DATABASE_NAME);
            prefs.edit().putBoolean(SNAPSHOT_DATABASE_REMOVED, true).apply();
        }
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) WorkerThread(android.support.annotation.WorkerThread)

Example 50 with WorkerThread

use of android.support.annotation.WorkerThread in project AndroidChromium by JackyAndroid.

the class DeferredStartupHandler method cacheIsChromeDefaultBrowser.

/**
     * Caches whether Chrome is set as a default browser on the device.
     */
@WorkerThread
private void cacheIsChromeDefaultBrowser() {
    // Retrieve whether Chrome is default in background to avoid strict mode checks.
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.madeupdomainforcheck123.com/"));
    ResolveInfo info = mAppContext.getPackageManager().resolveActivity(intent, 0);
    boolean isDefault = (info != null && info.match != 0 && mAppContext.getPackageName().equals(info.activityInfo.packageName));
    ChromePreferenceManager.getInstance(mAppContext).setCachedChromeDefaultBrowser(isDefault);
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) Intent(android.content.Intent) WorkerThread(android.support.annotation.WorkerThread)

Aggregations

WorkerThread (android.support.annotation.WorkerThread)59 NonNull (android.support.annotation.NonNull)21 Cursor (android.database.Cursor)17 StorIOException (com.pushtorefresh.storio.StorIOException)15 ArrayList (java.util.ArrayList)14 File (java.io.File)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