/* * Copyright (c) 2019 Hemanth Savarala. * * Licensed under the GNU General Public License v3 * * This is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by * the Free Software Foundation either version 3 of the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. */ package code.name.monkey.retromusic.helper; import android.content.Context; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import code.name.monkey.retromusic.model.Playlist; import code.name.monkey.retromusic.model.Song; public class M3UWriter implements M3UConstants { @Nullable public static File write(@NonNull Context context, @NonNull File dir, @NonNull Playlist playlist) throws IOException { if (!dir.exists()) //noinspection ResultOfMethodCallIgnored dir.mkdirs(); File file = new File(dir, playlist.name.concat("." + EXTENSION)); ArrayList songs = playlist.getSongs(context); if (songs.size() > 0) { BufferedWriter bw = new BufferedWriter(new FileWriter(file)); bw.write(HEADER); for (Song song : songs) { bw.newLine(); bw.write(ENTRY + song.getDuration() + DURATION_SEPARATOR + song.getArtistName() + " - " + song.getTitle()); bw.newLine(); bw.write(song.getData()); } bw.close(); } return file; } }