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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations