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 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;
}
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;
}
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.");
}
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.");
}
Aggregations