use of org.jaudiotagger.tag.Tag in project Shuttle by timusus.
the class TaggerTask method doInBackground.
@Override
protected Boolean doInBackground(Object... params) {
boolean success = false;
boolean requiresPermission = TaggerUtils.requiresPermission(paths);
for (int i = 0; i < paths.size(); i++) {
final String path = paths.get(i);
try {
File orig = new File(path);
AudioFile audioFile = AudioFileIO.read(orig);
Tag tag = audioFile.getTag();
if (tag == null) {
break;
}
TagUpdate tagUpdate = new TagUpdate(tag);
tagUpdate.softSetArtist(artistText);
tagUpdate.softSetAlbumArtist(albumArtistText);
tagUpdate.softSetGenre(genreText);
tagUpdate.softSetYear(yearText);
if (showAlbum) {
tagUpdate.softSetAlbum(albumText);
tagUpdate.softSetDiscTotal(discTotalText);
}
if (showTrack) {
tagUpdate.softSetTitle(titleText);
tagUpdate.softSetTrack(trackText);
tagUpdate.softSetTrackTotal(trackTotalText);
tagUpdate.softSetDisc(discText);
tagUpdate.softSetLyrics(lyricsText);
tagUpdate.softSetComment(commentText);
}
File temp = null;
if (tagUpdate.hasChanged()) {
if (TaggerUtils.requiresPermission(paths)) {
temp = new File(ShuttleApplication.getInstance().getFilesDir(), orig.getName());
tempFiles.add(temp);
TaggerUtils.copyFile(orig, temp);
audioFile = AudioFileIO.read(temp);
tag = audioFile.getTag();
if (tag == null) {
break;
}
}
tagUpdate.updateTag(tag);
AudioFileIO.write(audioFile);
if (requiresPermission && temp != null) {
DocumentFile documentFile = documentFiles.get(i);
if (documentFile != null) {
ParcelFileDescriptor pfd = ShuttleApplication.getInstance().getContentResolver().openFileDescriptor(documentFile.getUri(), "w");
if (pfd != null) {
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
TaggerUtils.copyFile(temp, fileOutputStream);
pfd.close();
}
if (temp.delete()) {
if (tempFiles.contains(temp)) {
tempFiles.remove(temp);
}
}
}
}
}
publishProgress(i);
success = true;
} catch (CannotWriteException | IOException | CannotReadException | InvalidAudioFrameException | TagException | ReadOnlyFileException e) {
e.printStackTrace();
} finally {
// Try to clean up our temp files
if (tempFiles != null && tempFiles.size() != 0) {
for (int j = tempFiles.size() - 1; j >= 0; j--) {
File file = tempFiles.get(j);
file.delete();
tempFiles.remove(j);
}
}
}
}
return success;
}
use of org.jaudiotagger.tag.Tag in project Shuttle by timusus.
the class ShuttleApplication method onCreate.
@Override
public void onCreate() {
super.onCreate();
if (LeakCanary.isInAnalyzerProcess(this)) {
// You should not init your app in this process.
return;
}
instance = this;
if (BuildConfig.DEBUG) {
// Traceur.enableLogging();
// enableStrictMode();
}
appComponent = initDagger(this);
refWatcher = LeakCanary.install(this);
// workaround to fix InputMethodManager leak as suggested by LeakCanary lib
InputMethodManagerLeaks.fixFocusedViewLeak(this);
// Crashlytics
CrashlyticsCore crashlyticsCore = new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build();
Fabric.with(this, new Crashlytics.Builder().core(crashlyticsCore).answers(new Answers()).build());
// Firebase
FirebaseApp.initializeApp(this);
FirebaseAnalytics.getInstance(this);
VideoCastManager.initialize(this, new CastConfiguration.Builder(Config.CHROMECAST_APP_ID).enableLockScreen().enableNotification().build());
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
// files with readAgain flag set to false, so always check KEY_HAS_SET_DEFAULT_VALUES
if (!prefs.getBoolean(PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES, false)) {
PreferenceManager.setDefaultValues(this, R.xml.settings_headers, true);
PreferenceManager.setDefaultValues(this, R.xml.settings_artwork, true);
PreferenceManager.setDefaultValues(this, R.xml.settings_blacklist, true);
PreferenceManager.setDefaultValues(this, R.xml.settings_display, true);
PreferenceManager.setDefaultValues(this, R.xml.settings_headset, true);
PreferenceManager.setDefaultValues(this, R.xml.settings_scrobbling, true);
PreferenceManager.setDefaultValues(this, R.xml.settings_themes, true);
}
// Turn off logging for jaudiotagger.
jaudioTaggerLogger1.setLevel(Level.OFF);
jaudioTaggerLogger2.setLevel(Level.OFF);
TagOptionSingleton.getInstance().setPadNumbers(true);
SettingsManager.getInstance().incrementLaunchCount();
Completable.fromAction(() -> {
Query query = new Query.Builder().uri(CustomArtworkTable.URI).projection(new String[] { CustomArtworkTable.COLUMN_ID, CustomArtworkTable.COLUMN_KEY, CustomArtworkTable.COLUMN_TYPE, CustomArtworkTable.COLUMN_PATH }).build();
SqlUtils.createActionableQuery(ShuttleApplication.this, cursor -> userSelectedArtwork.put(cursor.getString(cursor.getColumnIndexOrThrow(CustomArtworkTable.COLUMN_KEY)), new UserSelectedArtwork(cursor.getInt(cursor.getColumnIndexOrThrow(CustomArtworkTable.COLUMN_TYPE)), cursor.getString(cursor.getColumnIndexOrThrow(CustomArtworkTable.COLUMN_PATH)))), query);
}).subscribeOn(Schedulers.io()).subscribe();
Completable.timer(5, TimeUnit.SECONDS).andThen(Completable.defer(this::repairMediaStoreYearFromTags)).doOnError(throwable -> LogUtils.logException(TAG, "Failed to update year from tags", throwable)).onErrorComplete().subscribeOn(Schedulers.io()).subscribe();
Completable.timer(10, TimeUnit.SECONDS).andThen(Completable.defer(this::cleanGenres)).doOnError(throwable -> LogUtils.logException(TAG, "Failed to clean genres", throwable)).onErrorComplete().subscribeOn(Schedulers.io()).subscribe();
Completable.timer(15, TimeUnit.SECONDS).andThen(Completable.defer(this::cleanMostPlayedPlaylist)).doOnError(throwable -> LogUtils.logException(TAG, "Failed to clean most played", throwable)).onErrorComplete().subscribeOn(Schedulers.io()).subscribe();
Completable.timer(20, TimeUnit.SECONDS).andThen(Completable.defer(LegacyUtils::deleteOldResources)).doOnError(throwable -> LogUtils.logException(TAG, "Failed to delete old resources", throwable)).onErrorComplete().subscribeOn(Schedulers.io()).subscribe();
}
use of org.jaudiotagger.tag.Tag in project Shuttle by timusus.
the class LyricsPresenter method updateLyrics.
private void updateLyrics() {
addDisposable(Observable.fromCallable(() -> {
String lyrics = "";
String path = MusicUtils.getFilePath();
if (TextUtils.isEmpty(path)) {
return lyrics;
}
if (path.startsWith("content://")) {
Query query = new Query.Builder().uri(Uri.parse(path)).projection(new String[] { MediaStore.Audio.Media.DATA }).build();
Cursor cursor = SqlUtils.createQuery(ShuttleApplication.getInstance(), query);
if (cursor != null) {
try {
int colIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
if (cursor.moveToFirst()) {
path = cursor.getString(colIndex);
}
} finally {
cursor.close();
}
}
}
File file = new File(path);
if (file.exists()) {
try {
AudioFile audioFile = AudioFileIO.read(file);
if (audioFile != null) {
Tag tag = audioFile.getTag();
if (tag != null) {
String tagLyrics = tag.getFirst(FieldKey.LYRICS);
if (tagLyrics != null && tagLyrics.length() != 0) {
lyrics = tagLyrics.replace("\r", "\n");
}
}
}
} catch (CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException | UnsupportedOperationException ignored) {
}
}
return lyrics;
}).subscribe(lyrics -> {
LyricsView lyricsView = getView();
if (lyricsView != null) {
lyricsView.updateLyrics(lyrics);
lyricsView.showNoLyricsView(TextUtils.isEmpty(lyrics));
lyricsView.showQuickLyricInfoButton(!QuickLyricUtils.isQLInstalled());
}
}, error -> LogUtils.logException(TAG, "Error getting lyrics", error)));
}
Aggregations