Search in sources :

Example 21 with ReadOnlyFileException

use of org.jaudiotagger.audio.exceptions.ReadOnlyFileException 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;
}
Also used : CannotWriteException(org.jaudiotagger.audio.exceptions.CannotWriteException) DocumentFile(android.support.v4.provider.DocumentFile) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) IOException(java.io.IOException) TagUpdate(com.simplecity.amp_library.model.TagUpdate) AudioFile(org.jaudiotagger.audio.AudioFile) TagException(org.jaudiotagger.tag.TagException) FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) Tag(org.jaudiotagger.tag.Tag) File(java.io.File) AudioFile(org.jaudiotagger.audio.AudioFile) DocumentFile(android.support.v4.provider.DocumentFile)

Example 22 with ReadOnlyFileException

use of org.jaudiotagger.audio.exceptions.ReadOnlyFileException 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)));
}
Also used : ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) Uri(android.net.Uri) NonNull(android.support.annotation.NonNull) Song(com.simplecity.amp_library.model.Song) AudioFile(org.jaudiotagger.audio.AudioFile) MediaStore(android.provider.MediaStore) Observable(io.reactivex.Observable) Cursor(android.database.Cursor) FieldKey(org.jaudiotagger.tag.FieldKey) LogUtils(com.simplecity.amp_library.utils.LogUtils) BackpressureStrategy(io.reactivex.BackpressureStrategy) IntentFilter(android.content.IntentFilter) TextUtils(android.text.TextUtils) IOException(java.io.IOException) Tag(org.jaudiotagger.tag.Tag) RxBroadcast(com.cantrowitz.rxbroadcast.RxBroadcast) File(java.io.File) Query(com.simplecity.amp_library.model.Query) Presenter(com.simplecity.amp_library.ui.presenters.Presenter) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) MusicService(com.simplecity.amp_library.playback.MusicService) TagException(org.jaudiotagger.tag.TagException) AudioFileIO(org.jaudiotagger.audio.AudioFileIO) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) SqlUtils(com.simplecity.amp_library.sql.SqlUtils) Query(com.simplecity.amp_library.model.Query) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) IOException(java.io.IOException) Cursor(android.database.Cursor) AudioFile(org.jaudiotagger.audio.AudioFile) TagException(org.jaudiotagger.tag.TagException) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) Tag(org.jaudiotagger.tag.Tag) AudioFile(org.jaudiotagger.audio.AudioFile) File(java.io.File)

Aggregations

ReadOnlyFileException (org.jaudiotagger.audio.exceptions.ReadOnlyFileException)22 CannotReadException (org.jaudiotagger.audio.exceptions.CannotReadException)21 InvalidAudioFrameException (org.jaudiotagger.audio.exceptions.InvalidAudioFrameException)21 IOException (java.io.IOException)20 TagException (org.jaudiotagger.tag.TagException)20 AudioFile (org.jaudiotagger.audio.AudioFile)17 File (java.io.File)14 Tag (org.jaudiotagger.tag.Tag)14 CannotWriteException (org.jaudiotagger.audio.exceptions.CannotWriteException)9 Cursor (android.database.Cursor)6 Paint (android.graphics.Paint)6 FieldDataInvalidException (org.jaudiotagger.tag.FieldDataInvalidException)5 KeyNotFoundException (org.jaudiotagger.tag.KeyNotFoundException)5 ContentValues (android.content.ContentValues)4 AlertDialog (android.app.AlertDialog)3 DialogInterface (android.content.DialogInterface)3 OnClickListener (android.content.DialogInterface.OnClickListener)3 CompoundButton (android.widget.CompoundButton)3 OnCheckedChangeListener (android.widget.CompoundButton.OnCheckedChangeListener)3 Query (com.simplecity.amp_library.model.Query)3