Search in sources :

Example 21 with WorkerThread

use of android.support.annotation.WorkerThread in project Shuttle by timusus.

the class SqlUtils method createQuery.

@WorkerThread
public static Cursor createQuery(Context context, Query query) {
    long time = System.currentTimeMillis();
    Cursor cursor = context.getContentResolver().query(query.uri, query.projection, query.selection, query.args, query.sort);
    if (ENABLE_LOGGING && BuildConfig.DEBUG) {
        Log.d(TAG, String.format("Query took %sms. %s", (System.currentTimeMillis() - time), query));
    }
    ThreadUtils.ensureNotOnMainThread();
    return cursor;
}
Also used : Cursor(android.database.Cursor) WorkerThread(android.support.annotation.WorkerThread)

Example 22 with WorkerThread

use of android.support.annotation.WorkerThread in project Shuttle by timusus.

the class ArtworkUtils method getFolderArtwork.

/**
     * Searches the parent directory of the passed in path for [cover/album/artwork].[png/jpg/jpeg]
     * using regex and returns a {@link InputStream} representing the artwork
     */
@WorkerThread
public static InputStream getFolderArtwork(@Nullable final String path) {
    InputStream fileInputStream = null;
    if (path != null) {
        File[] files;
        File file = new File(path);
        File parent = file.getParentFile();
        if (parent.exists() && parent.isDirectory()) {
            final Pattern pattern = Pattern.compile("(folder|cover|album).*\\.(jpg|jpeg|png)", Pattern.CASE_INSENSITIVE);
            files = parent.listFiles(file1 -> {
                return pattern.matcher(file1.getName()).matches();
            });
            if (files.length > 0) {
                try {
                    File artworkFile = Stream.of(files).filter(aFile -> aFile.exists() && aFile.length() > 1024).max((a, b) -> (int) (a.length() / 1024 - b.length() / 1024)).get();
                    fileInputStream = getFileArtwork(artworkFile);
                } catch (NoSuchElementException e) {
                    Log.e(TAG, "getFolderArtwork failed: " + e.toString());
                }
            }
        }
    }
    return fileInputStream;
}
Also used : ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) Album(com.simplecity.amp_library.model.Album) Stream(com.annimon.stream.Stream) Uri(android.net.Uri) NonNull(android.support.annotation.NonNull) ArrayList(java.util.ArrayList) Song(com.simplecity.amp_library.model.Song) AudioFile(org.jaudiotagger.audio.AudioFile) ByteArrayInputStream(java.io.ByteArrayInputStream) MediaStore(android.provider.MediaStore) NoSuchElementException(java.util.NoSuchElementException) Log(android.util.Log) Cursor(android.database.Cursor) IOException(java.io.IOException) Tag(org.jaudiotagger.tag.Tag) FileInputStream(java.io.FileInputStream) WorkerThread(android.support.annotation.WorkerThread) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) List(java.util.List) TagException(org.jaudiotagger.tag.TagException) AudioFileIO(org.jaudiotagger.audio.AudioFileIO) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) Pattern(java.util.regex.Pattern) Nullable(android.support.annotation.Nullable) ContentUris(android.content.ContentUris) InputStream(java.io.InputStream) Pattern(java.util.regex.Pattern) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AudioFile(org.jaudiotagger.audio.AudioFile) File(java.io.File) NoSuchElementException(java.util.NoSuchElementException) WorkerThread(android.support.annotation.WorkerThread)

Example 23 with WorkerThread

use of android.support.annotation.WorkerThread in project Shuttle by timusus.

the class ArtworkUtils method getMediaStoreArtwork.

/**
     * Retrieves the Artwork for the given album id from the MediaStore as an {@link InputStream}
     */
@WorkerThread
public static InputStream getMediaStoreArtwork(long albumId) {
    Uri contentUri = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, albumId);
    FileInputStream fileInputStream = null;
    Cursor cursor = null;
    try {
        cursor = ShuttleApplication.getInstance().getContentResolver().query(contentUri, new String[] { MediaStore.Audio.Albums.ALBUM_ART }, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            File file = new File(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)));
            if (file.exists()) {
                try {
                    fileInputStream = new FileInputStream(file);
                } catch (FileNotFoundException ignored) {
                }
            }
        }
    } catch (NullPointerException ignored) {
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return fileInputStream;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Cursor(android.database.Cursor) Uri(android.net.Uri) AudioFile(org.jaudiotagger.audio.AudioFile) File(java.io.File) FileInputStream(java.io.FileInputStream) WorkerThread(android.support.annotation.WorkerThread)

Example 24 with WorkerThread

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

the class TabbedModeTabPersistencePolicy method performMultiInstanceMigration.

/**
     * Upgrades users from an older version of Chrome when the state files for multi-instance
     * were each kept in separate subdirectories.
     */
@WorkerThread
private void performMultiInstanceMigration() {
    Log.w(TAG, "Starting to perform multi-instance migration.");
    // 0. Do not rename the old metadata file if the new metadata file already exists. This
    //    should not happen, but if it does and the metadata file is overwritten then users
    //    may lose tabs. See crbug.com/649384.
    File stateDir = getOrCreateStateDirectory();
    File newMetadataFile = new File(stateDir, getStateFileName());
    File oldMetadataFile = new File(stateDir, LEGACY_SAVED_STATE_FILE);
    if (newMetadataFile.exists()) {
        Log.e(TAG, "New metadata file already exists");
        if (LibraryLoader.isInitialized()) {
            RecordHistogram.recordBooleanHistogram("Android.MultiInstanceMigration.NewMetadataFileExists", true);
        }
    } else if (oldMetadataFile.exists()) {
        // 1. Rename tab metadata file for tab directory "0".
        if (!oldMetadataFile.renameTo(newMetadataFile)) {
            Log.e(TAG, "Failed to rename file: " + oldMetadataFile);
            if (LibraryLoader.isInitialized()) {
                RecordHistogram.recordBooleanHistogram("Android.MultiInstanceMigration.FailedToRenameMetadataFile", true);
            }
        }
    }
    // 2. Move files from other state directories.
    for (int i = TabModelSelectorImpl.CUSTOM_TABS_SELECTOR_INDEX; i < TabWindowManager.MAX_SIMULTANEOUS_SELECTORS; i++) {
        // Skip the directory we're migrating to.
        if (i == 0)
            continue;
        File otherStateDir = new File(TabPersistentStore.getOrCreateBaseStateDirectory(), Integer.toString(i));
        if (otherStateDir == null || !otherStateDir.exists())
            continue;
        // Rename tab state file.
        oldMetadataFile = new File(otherStateDir, LEGACY_SAVED_STATE_FILE);
        if (oldMetadataFile.exists()) {
            if (!oldMetadataFile.renameTo(new File(stateDir, getStateFileName(i)))) {
                Log.e(TAG, "Failed to rename file: " + oldMetadataFile);
            }
        }
        // Rename tab files.
        File[] files = otherStateDir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (TabState.parseInfoFromFilename(file.getName()) != null) {
                    // migrating.
                    if (i == TabModelSelectorImpl.CUSTOM_TABS_SELECTOR_INDEX) {
                        if (!file.delete()) {
                            Log.e(TAG, "Failed to delete file: " + file);
                        }
                        continue;
                    }
                    // If the tab was moved between windows in Android N multi-window, the tab
                    // file may exist in both directories. Keep whichever was modified more
                    // recently.
                    File newFileName = new File(stateDir, file.getName());
                    if (newFileName.exists() && newFileName.lastModified() > file.lastModified()) {
                        if (!file.delete()) {
                            Log.e(TAG, "Failed to delete file: " + file);
                        }
                    } else if (!file.renameTo(newFileName)) {
                        Log.e(TAG, "Failed to rename file: " + file);
                    }
                }
            }
        }
        // Delete other state directory.
        if (!otherStateDir.delete()) {
            Log.e(TAG, "Failed to delete directory: " + otherStateDir);
        }
    }
    setMultiInstanceFileMigrationPref();
    Log.w(TAG, "Finished performing multi-instance migration.");
}
Also used : File(java.io.File) WorkerThread(android.support.annotation.WorkerThread)

Example 25 with WorkerThread

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

the class TabbedModeTabPersistencePolicy method performLegacyMigration.

/**
     * Upgrades users from an old version of Chrome when the state file was still in the root
     * directory.
     */
@WorkerThread
private void performLegacyMigration() {
    Log.w(TAG, "Starting to perform legacy migration.");
    File newFolder = getOrCreateStateDirectory();
    File[] newFiles = newFolder.listFiles();
    // Attempt migration if we have no tab state file in the new directory.
    if (newFiles == null || newFiles.length == 0) {
        File oldFolder = ContextUtils.getApplicationContext().getFilesDir();
        File modelFile = new File(oldFolder, LEGACY_SAVED_STATE_FILE);
        if (modelFile.exists()) {
            if (!modelFile.renameTo(new File(newFolder, getStateFileName()))) {
                Log.e(TAG, "Failed to rename file: " + modelFile);
            }
        }
        File[] files = oldFolder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (TabState.parseInfoFromFilename(file.getName()) != null) {
                    if (!file.renameTo(new File(newFolder, file.getName()))) {
                        Log.e(TAG, "Failed to rename file: " + file);
                    }
                }
            }
        }
    }
    setLegacyFileMigrationPref();
    Log.w(TAG, "Finished performing legacy migration.");
}
Also used : File(java.io.File) WorkerThread(android.support.annotation.WorkerThread)

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