Search in sources :

Example 36 with WorkerThread

use of android.support.annotation.WorkerThread in project materialistic by hidroh.

the class FavoriteManager method toFile.

@WorkerThread
private Uri toFile(Context context, Cursor cursor) throws IOException {
    if (cursor.getCount() == 0) {
        return null;
    }
    File dir = new File(context.getFilesDir(), PATH_SAVED);
    if (!dir.exists() && !dir.mkdir()) {
        return null;
    }
    File file = new File(dir, FILENAME_EXPORT);
    if (!file.exists() && !file.createNewFile()) {
        return null;
    }
    BufferedSink bufferedSink = Okio.buffer(Okio.sink(file));
    do {
        Favorite item = cursor.getFavorite();
        bufferedSink.writeUtf8(item.getDisplayedTitle()).writeByte('\n').writeUtf8(item.getUrl()).writeByte('\n').writeUtf8(String.format(HackerNewsClient.WEB_ITEM_PATH, item.getId()));
        if (!cursor.isLast()) {
            bufferedSink.writeByte('\n').writeByte('\n');
        }
    } while (cursor.moveToNext());
    Util.closeQuietly(bufferedSink);
    return getUriForFile(context, file);
}
Also used : BufferedSink(okio.BufferedSink) File(java.io.File) WorkerThread(android.support.annotation.WorkerThread)

Example 37 with WorkerThread

use of android.support.annotation.WorkerThread in project materialistic by hidroh.

the class SessionManager method insert.

@WorkerThread
private void insert(ContentResolver cr, String itemId) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(MaterialisticProvider.ViewedEntry.COLUMN_NAME_ITEM_ID, itemId);
    cr.insert(MaterialisticProvider.URI_VIEWED, contentValues);
}
Also used : ContentValues(android.content.ContentValues) WorkerThread(android.support.annotation.WorkerThread)

Example 38 with WorkerThread

use of android.support.annotation.WorkerThread in project materialistic by hidroh.

the class SessionManager method isViewed.

@WorkerThread
@NonNull
Observable<Boolean> isViewed(ContentResolver contentResolver, String itemId) {
    if (TextUtils.isEmpty(itemId)) {
        return Observable.just(false);
    }
    Cursor cursor = contentResolver.query(MaterialisticProvider.URI_VIEWED, null, MaterialisticProvider.ViewedEntry.COLUMN_NAME_ITEM_ID + " = ?", new String[] { itemId }, null);
    boolean result = false;
    if (cursor != null) {
        result = cursor.getCount() > 0;
        cursor.close();
    }
    return Observable.just(result);
}
Also used : Cursor(android.database.Cursor) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 39 with WorkerThread

use of android.support.annotation.WorkerThread in project materialistic by hidroh.

the class AdBlocker method loadFromAssets.

@WorkerThread
private static Void loadFromAssets(Context context) throws IOException {
    InputStream stream = context.getAssets().open(AD_HOSTS_FILE);
    BufferedSource buffer = Okio.buffer(Okio.source(stream));
    String line;
    while ((line = buffer.readUtf8Line()) != null) {
        AD_HOSTS.add(line);
    }
    buffer.close();
    stream.close();
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BufferedSource(okio.BufferedSource) WorkerThread(android.support.annotation.WorkerThread)

Example 40 with WorkerThread

use of android.support.annotation.WorkerThread in project k-9 by k9mail.

the class AttachmentController method getBestViewIntentAndSaveFile.

@WorkerThread
private Intent getBestViewIntentAndSaveFile() {
    Uri intentDataUri;
    try {
        intentDataUri = AttachmentTempFileProvider.createTempUriForContentUri(context, attachment.internalUri);
    } catch (IOException e) {
        Timber.e(e, "Error creating temp file for attachment!");
        return null;
    }
    String displayName = attachment.displayName;
    String inferredMimeType = MimeUtility.getMimeTypeByExtension(displayName);
    IntentAndResolvedActivitiesCount resolvedIntentInfo;
    String mimeType = attachment.mimeType;
    if (MimeUtility.isDefaultMimeType(mimeType)) {
        resolvedIntentInfo = getBestViewIntentForMimeType(intentDataUri, inferredMimeType);
    } else {
        resolvedIntentInfo = getBestViewIntentForMimeType(intentDataUri, mimeType);
        if (!resolvedIntentInfo.hasResolvedActivities() && !inferredMimeType.equals(mimeType)) {
            resolvedIntentInfo = getBestViewIntentForMimeType(intentDataUri, inferredMimeType);
        }
    }
    if (!resolvedIntentInfo.hasResolvedActivities()) {
        resolvedIntentInfo = getBestViewIntentForMimeType(intentDataUri, MimeUtility.DEFAULT_ATTACHMENT_MIME_TYPE);
    }
    Intent viewIntent;
    if (resolvedIntentInfo.hasResolvedActivities() && resolvedIntentInfo.containsFileUri()) {
        try {
            File tempFile = TemporaryAttachmentStore.getFileForWriting(context, displayName);
            writeAttachmentToStorage(tempFile);
            viewIntent = createViewIntentForFileUri(resolvedIntentInfo.getMimeType(), Uri.fromFile(tempFile));
        } catch (IOException e) {
            Timber.e(e, "Error while saving attachment to use file:// URI with ACTION_VIEW Intent");
            viewIntent = createViewIntentForAttachmentProviderUri(intentDataUri, MimeUtility.DEFAULT_ATTACHMENT_MIME_TYPE);
        }
    } else {
        viewIntent = resolvedIntentInfo.getIntent();
    }
    return viewIntent;
}
Also used : Intent(android.content.Intent) IOException(java.io.IOException) Uri(android.net.Uri) File(java.io.File) WorkerThread(android.support.annotation.WorkerThread)

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