2019-04-20 05:29:45 +00:00
|
|
|
package code.name.monkey.retromusic.activities.tageditor;
|
2018-07-27 13:07:33 +00:00
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.media.MediaScannerConnection;
|
2019-07-31 16:42:19 +00:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
2018-08-22 17:54:07 +00:00
|
|
|
|
2019-07-31 16:42:19 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
2018-08-22 17:54:07 +00:00
|
|
|
|
2018-07-27 13:07:33 +00:00
|
|
|
import org.jaudiotagger.audio.AudioFile;
|
|
|
|
import org.jaudiotagger.audio.AudioFileIO;
|
|
|
|
import org.jaudiotagger.tag.FieldKey;
|
|
|
|
import org.jaudiotagger.tag.Tag;
|
|
|
|
import org.jaudiotagger.tag.images.Artwork;
|
|
|
|
import org.jaudiotagger.tag.images.ArtworkFactory;
|
|
|
|
|
2018-08-22 17:54:07 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
2019-07-31 16:42:19 +00:00
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
import java.util.ArrayList;
|
2018-08-22 17:54:07 +00:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import code.name.monkey.retromusic.R;
|
|
|
|
import code.name.monkey.retromusic.misc.DialogAsyncTask;
|
|
|
|
import code.name.monkey.retromusic.misc.UpdateToastMediaScannerCompletionListener;
|
|
|
|
import code.name.monkey.retromusic.util.MusicUtil;
|
2019-07-31 16:42:19 +00:00
|
|
|
import code.name.monkey.retromusic.util.SAFUtil;
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2018-08-22 17:54:07 +00:00
|
|
|
public class WriteTagsAsyncTask extends
|
|
|
|
DialogAsyncTask<WriteTagsAsyncTask.LoadingInfo, Integer, String[]> {
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2019-07-31 16:42:19 +00:00
|
|
|
private WeakReference<Activity> activity;
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2019-07-31 16:42:19 +00:00
|
|
|
public WriteTagsAsyncTask(@NonNull Activity activity) {
|
|
|
|
super(activity);
|
|
|
|
this.activity = new WeakReference<>(activity);
|
2018-08-22 17:54:07 +00:00
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2018-08-22 17:54:07 +00:00
|
|
|
@Override
|
|
|
|
protected String[] doInBackground(LoadingInfo... params) {
|
2018-07-27 13:07:33 +00:00
|
|
|
try {
|
2018-08-22 17:54:07 +00:00
|
|
|
LoadingInfo info = params[0];
|
|
|
|
|
|
|
|
Artwork artwork = null;
|
|
|
|
File albumArtFile = null;
|
2018-12-06 08:52:57 +00:00
|
|
|
if (info.artworkInfo != null && info.artworkInfo.getArtwork() != null) {
|
2018-08-22 17:54:07 +00:00
|
|
|
try {
|
|
|
|
albumArtFile = MusicUtil.createAlbumArtFile().getCanonicalFile();
|
2018-12-06 08:52:57 +00:00
|
|
|
info.artworkInfo.getArtwork()
|
2018-08-22 17:54:07 +00:00
|
|
|
.compress(Bitmap.CompressFormat.PNG, 0, new FileOutputStream(albumArtFile));
|
|
|
|
artwork = ArtworkFactory.createArtworkFromFile(albumArtFile);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2018-08-22 17:54:07 +00:00
|
|
|
int counter = 0;
|
|
|
|
boolean wroteArtwork = false;
|
|
|
|
boolean deletedArtwork = false;
|
|
|
|
for (String filePath : info.filePaths) {
|
|
|
|
publishProgress(++counter, info.filePaths.size());
|
|
|
|
try {
|
2019-07-31 16:42:19 +00:00
|
|
|
Uri safUri = null;
|
|
|
|
if (filePath.contains(SAFUtil.SEPARATOR)) {
|
|
|
|
String[] fragments = filePath.split(SAFUtil.SEPARATOR);
|
|
|
|
filePath = fragments[0];
|
|
|
|
safUri = Uri.parse(fragments[1]);
|
|
|
|
}
|
|
|
|
|
2018-08-22 17:54:07 +00:00
|
|
|
AudioFile audioFile = AudioFileIO.read(new File(filePath));
|
|
|
|
Tag tag = audioFile.getTagOrCreateAndSetDefault();
|
|
|
|
|
|
|
|
if (info.fieldKeyValueMap != null) {
|
|
|
|
for (Map.Entry<FieldKey, String> entry : info.fieldKeyValueMap.entrySet()) {
|
|
|
|
try {
|
|
|
|
tag.setField(entry.getKey(), entry.getValue());
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.artworkInfo != null) {
|
2018-12-06 08:52:57 +00:00
|
|
|
if (info.artworkInfo.getArtwork() == null) {
|
2018-08-22 17:54:07 +00:00
|
|
|
tag.deleteArtworkField();
|
|
|
|
deletedArtwork = true;
|
|
|
|
} else if (artwork != null) {
|
|
|
|
tag.deleteArtworkField();
|
|
|
|
tag.setField(artwork);
|
|
|
|
wroteArtwork = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 16:42:19 +00:00
|
|
|
Activity activity = this.activity.get();
|
|
|
|
SAFUtil.write(activity, audioFile, safUri);
|
|
|
|
|
|
|
|
} catch (@NonNull Exception e) {
|
2018-08-22 17:54:07 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|
2018-08-22 17:54:07 +00:00
|
|
|
|
|
|
|
Context context = getContext();
|
|
|
|
if (context != null) {
|
|
|
|
if (wroteArtwork) {
|
2018-12-06 08:52:57 +00:00
|
|
|
MusicUtil.insertAlbumArt(context, info.artworkInfo.getAlbumId(), albumArtFile.getPath());
|
2018-08-22 17:54:07 +00:00
|
|
|
} else if (deletedArtwork) {
|
2018-12-06 08:52:57 +00:00
|
|
|
MusicUtil.deleteAlbumArt(context, info.artworkInfo.getAlbumId());
|
2018-08-22 17:54:07 +00:00
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 16:42:19 +00:00
|
|
|
Collection<String> paths = info.filePaths;
|
|
|
|
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
|
|
|
|
paths = new ArrayList<>(info.filePaths.size());
|
|
|
|
for (String path : info.filePaths) {
|
|
|
|
if (path.contains(SAFUtil.SEPARATOR))
|
|
|
|
path = path.split(SAFUtil.SEPARATOR)[0];
|
|
|
|
paths.add(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths.toArray(new String[paths.size()]);
|
2018-08-22 17:54:07 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|
2018-08-22 17:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(String[] toBeScanned) {
|
|
|
|
super.onPostExecute(toBeScanned);
|
|
|
|
scan(toBeScanned);
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2018-08-22 17:54:07 +00:00
|
|
|
@Override
|
|
|
|
protected void onCancelled(String[] toBeScanned) {
|
|
|
|
super.onCancelled(toBeScanned);
|
|
|
|
scan(toBeScanned);
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|
2018-08-22 17:54:07 +00:00
|
|
|
|
|
|
|
private void scan(String[] toBeScanned) {
|
2019-07-31 16:42:19 +00:00
|
|
|
Activity activity = this.activity.get();
|
|
|
|
if (activity != null) {
|
|
|
|
MediaScannerConnection.scanFile(activity, toBeScanned, null, new UpdateToastMediaScannerCompletionListener(activity, toBeScanned));
|
|
|
|
}
|
2018-08-22 17:54:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 18:51:04 +00:00
|
|
|
@NonNull
|
2018-08-22 17:54:07 +00:00
|
|
|
@Override
|
|
|
|
protected Dialog createDialog(@NonNull Context context) {
|
2019-07-31 16:42:19 +00:00
|
|
|
return new MaterialAlertDialogBuilder(context)
|
|
|
|
.setTitle(R.string.saving_changes)
|
|
|
|
.setCancelable(false)
|
|
|
|
.setView(R.layout.loading)
|
|
|
|
.create();
|
2018-08-22 17:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onProgressUpdate(@NonNull Dialog dialog, Integer... values) {
|
|
|
|
super.onProgressUpdate(dialog, values);
|
2019-02-19 10:38:51 +00:00
|
|
|
//((MaterialDialog) dialog).setMaxProgress(values[1]);
|
|
|
|
//((MaterialDialog) dialog).setProgress(values[0]);
|
2018-08-22 17:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class LoadingInfo {
|
|
|
|
|
|
|
|
final Collection<String> filePaths;
|
|
|
|
@Nullable
|
|
|
|
final Map<FieldKey, String> fieldKeyValueMap;
|
|
|
|
@Nullable
|
|
|
|
private AbsTagEditorActivity.ArtworkInfo artworkInfo;
|
|
|
|
|
|
|
|
public LoadingInfo(Collection<String> filePaths,
|
|
|
|
@Nullable Map<FieldKey, String> fieldKeyValueMap,
|
|
|
|
@Nullable AbsTagEditorActivity.ArtworkInfo artworkInfo) {
|
|
|
|
this.filePaths = filePaths;
|
|
|
|
this.fieldKeyValueMap = fieldKeyValueMap;
|
|
|
|
this.artworkInfo = artworkInfo;
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|
|
|
|
}
|