Search in sources :

Example 81 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 SupportSQLiteDatabase#delete(String, String, Object[])
 */
@WorkerThread
public int delete(@NonNull String table, @Nullable String whereClause, @Nullable String... whereArgs) {
    SupportSQLiteDatabase 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 : SupportSQLiteDatabase(android.arch.persistence.db.SupportSQLiteDatabase) WorkerThread(android.support.annotation.WorkerThread)

Example 82 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 83 with WorkerThread

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

the class FileBrowser method loadDir.

/**
 * Loads the specified folder.
 *
 * @param directory The file object to points to the directory to load.
 * @return An {@link List<BaseFileObject>} object that holds the data of the specified directory.
 */
@WorkerThread
public List<BaseFileObject> loadDir(File directory) {
    ThreadUtils.ensureNotOnMainThread();
    currentDir = directory;
    List<BaseFileObject> folderObjects = new ArrayList<>();
    List<BaseFileObject> fileObjects = new ArrayList<>();
    // Grab a list of all files/subdirs within the specified directory.
    File[] files = directory.listFiles(FileHelper.getAudioFilter());
    if (files != null) {
        for (File file : files) {
            BaseFileObject baseFileObject;
            if (file.isDirectory()) {
                baseFileObject = new FolderObject();
                baseFileObject.path = FileHelper.getPath(file);
                baseFileObject.name = file.getName();
                File[] listOfFiles = file.listFiles(FileHelper.getAudioFilter());
                if (listOfFiles != null && listOfFiles.length > 0) {
                    for (File listOfFile : listOfFiles) {
                        if (listOfFile.isDirectory()) {
                            ((FolderObject) baseFileObject).folderCount++;
                        } else {
                            ((FolderObject) baseFileObject).fileCount++;
                        }
                    }
                } else {
                    continue;
                }
                if (!folderObjects.contains(baseFileObject)) {
                    folderObjects.add(baseFileObject);
                }
            } else {
                baseFileObject = new FileObject();
                baseFileObject.path = FileHelper.getPath(file);
                baseFileObject.name = FileHelper.getName(file.getName());
                baseFileObject.size = file.length();
                ((FileObject) baseFileObject).extension = FileHelper.getExtension(file.getName());
                if (TextUtils.isEmpty(((FileObject) baseFileObject).extension)) {
                    continue;
                }
                ((FileObject) baseFileObject).tagInfo = new TagInfo(baseFileObject.path);
                if (!fileObjects.contains(baseFileObject)) {
                    fileObjects.add(baseFileObject);
                }
            }
        }
    }
    sortFileObjects(fileObjects);
    sortFolderObjects(folderObjects);
    if (!settingsManager.getFolderBrowserFilesAscending()) {
        Collections.reverse(fileObjects);
    }
    if (!settingsManager.getFolderBrowserFoldersAscending()) {
        Collections.reverse(folderObjects);
    }
    folderObjects.addAll(fileObjects);
    if (!FileHelper.isRootDirectory(currentDir)) {
        FolderObject parentObject = new FolderObject();
        parentObject.fileType = FileType.PARENT;
        parentObject.name = FileHelper.PARENT_DIRECTORY;
        parentObject.path = FileHelper.getPath(currentDir) + "/" + FileHelper.PARENT_DIRECTORY;
        folderObjects.add(0, parentObject);
    }
    return folderObjects;
}
Also used : BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) TagInfo(com.simplecity.amp_library.model.TagInfo) ArrayList(java.util.ArrayList) FolderObject(com.simplecity.amp_library.model.FolderObject) FileObject(com.simplecity.amp_library.model.FileObject) BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) File(java.io.File) WorkerThread(android.support.annotation.WorkerThread)

Example 84 with WorkerThread

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

the class FileBrowser method getInitialDir.

@WorkerThread
public File getInitialDir() {
    ThreadUtils.ensureNotOnMainThread();
    File dir;
    String[] files;
    String settingsDir = settingsManager.getFolderBrowserInitialDir();
    if (settingsDir != null) {
        File file = new File(settingsDir);
        if (file.exists()) {
            return file;
        }
    }
    dir = new File("/");
    files = dir.list((dir1, filename) -> dir1.isDirectory() && filename.toLowerCase().contains("storage"));
    if (files != null && files.length > 0) {
        dir = new File(dir + "/" + files[0]);
        // If there's an extsdcard path in our base dir, let's navigate to that. External SD cards are cool.
        files = dir.list((dir1, filename) -> dir1.isDirectory() && filename.toLowerCase().contains("extsdcard"));
        if (files != null && files.length > 0) {
            dir = new File(dir + "/" + files[0]);
        } else {
            // If we have external storage, use that as our initial dir
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                dir = Environment.getExternalStorageDirectory();
            }
        }
    } else {
        // If we have external storage, use that as our initial dir
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            dir = Environment.getExternalStorageDirectory();
        }
    }
    // Whether or not there was an sdcard, let's see if there's a 'music' dir for us to navigate to
    if (dir != null) {
        files = dir.list((dir1, filename) -> dir1.isDirectory() && filename.toLowerCase().contains("music"));
    }
    if (files != null && files.length > 0) {
        dir = new File(dir + "/" + files[0]);
    }
    return dir;
}
Also used : R(com.simplecity.amp_library.R) TagInfo(com.simplecity.amp_library.model.TagInfo) Environment(android.os.Environment) FolderObject(com.simplecity.amp_library.model.FolderObject) TextUtils(android.text.TextUtils) SortManager(com.simplecity.amp_library.utils.sorting.SortManager) WorkerThread(android.support.annotation.WorkerThread) File(java.io.File) ArrayList(java.util.ArrayList) List(java.util.List) FileObject(com.simplecity.amp_library.model.FileObject) BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) Nullable(android.support.annotation.Nullable) Comparator(java.util.Comparator) Collections(java.util.Collections) FileType(com.simplecity.amp_library.interfaces.FileType) File(java.io.File) WorkerThread(android.support.annotation.WorkerThread)

Example 85 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 -> 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) Context(android.content.Context) 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) FileInputStream(java.io.FileInputStream) Tag(org.jaudiotagger.tag.Tag) WorkerThread(android.support.annotation.WorkerThread) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) 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)

Aggregations

WorkerThread (android.support.annotation.WorkerThread)152 Cursor (android.database.Cursor)37 ArrayList (java.util.ArrayList)36 NonNull (android.support.annotation.NonNull)34 File (java.io.File)20 HashMap (java.util.HashMap)17 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)15 StorIOException (com.pushtorefresh.storio.StorIOException)15 IOException (java.io.IOException)13 ContentValues (android.content.ContentValues)11 Resources (android.content.res.Resources)10 Bitmap (android.graphics.Bitmap)10 Context (android.content.Context)8 Intent (android.content.Intent)7 SharedPreferences (android.content.SharedPreferences)7 Uri (android.net.Uri)7 Nullable (android.support.annotation.Nullable)7 VisibleForTesting (android.support.annotation.VisibleForTesting)7 Media (com.doctoror.fuckoffmusicplayer.domain.queue.Media)7 DashboardCategory (com.android.settingslib.drawer.DashboardCategory)6