2019-03-03 09:29:03 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-07-27 13:07:33 +00:00
|
|
|
package code.name.monkey.retromusic.util;
|
|
|
|
|
|
|
|
import android.util.Base64;
|
2021-09-08 18:30:20 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2019-11-12 14:19:33 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2019-12-19 19:26:06 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2021-09-08 18:30:20 +00:00
|
|
|
|
2018-07-27 13:07:33 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
2021-09-08 18:30:20 +00:00
|
|
|
import java.io.FileReader;
|
2018-07-27 13:07:33 +00:00
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2020-08-21 14:19:15 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2021-09-08 18:30:20 +00:00
|
|
|
import code.name.monkey.retromusic.model.Song;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by hefuyi on 2016/11/8.
|
|
|
|
*/
|
2018-07-27 13:07:33 +00:00
|
|
|
public class LyricUtil {
|
|
|
|
|
2021-09-08 18:30:20 +00:00
|
|
|
private static final String lrcRootPath =
|
|
|
|
android.os.Environment.getExternalStorageDirectory().toString() + "/RetroMusic/lyrics/";
|
|
|
|
private static final String TAG = "LyricUtil";
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public static File writeLrcToLoc(
|
|
|
|
@NonNull String title, @NonNull String artist, @NonNull String lrcContext) {
|
|
|
|
FileWriter writer = null;
|
|
|
|
try {
|
|
|
|
File file = new File(getLrcPath(title, artist));
|
|
|
|
if (!file.getParentFile().exists()) {
|
|
|
|
file.getParentFile().mkdirs();
|
|
|
|
}
|
|
|
|
writer = new FileWriter(getLrcPath(title, artist));
|
|
|
|
writer.write(lrcContext);
|
|
|
|
return file;
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (writer != null) writer.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//So in Retro, Lrc file can be same folder as Music File or in RetroMusic Folder
|
|
|
|
// In this case we pass location of the file and Contents to write to file
|
|
|
|
public static void writeLrc(@NonNull Song song, @NonNull String lrcContext) {
|
|
|
|
FileWriter writer = null;
|
|
|
|
File location;
|
|
|
|
try {
|
|
|
|
if (isLrcOriginalFileExist(song.getData())) {
|
|
|
|
location = getLocalLyricOriginalFile(song.getData());
|
|
|
|
} else if (isLrcFileExist(song.getTitle(), song.getArtistName())) {
|
|
|
|
location = getLocalLyricFile(song.getTitle(), song.getArtistName());
|
|
|
|
} else {
|
|
|
|
location = new File(getLrcPath(song.getTitle(), song.getArtistName()));
|
|
|
|
if (!location.getParentFile().exists()) {
|
|
|
|
location.getParentFile().mkdirs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writer = new FileWriter(location);
|
|
|
|
writer.write(lrcContext);
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (writer != null) writer.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean deleteLrcFile(@NonNull String title, @NonNull String artist) {
|
|
|
|
File file = new File(getLrcPath(title, artist));
|
|
|
|
return file.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isLrcFileExist(@NonNull String title, @NonNull String artist) {
|
|
|
|
File file = new File(getLrcPath(title, artist));
|
|
|
|
return file.exists();
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|
2021-09-08 18:30:20 +00:00
|
|
|
|
|
|
|
public static boolean isLrcOriginalFileExist(@NonNull String path) {
|
|
|
|
File file = new File(getLrcOriginalPath(path));
|
|
|
|
return file.exists();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public static File getLocalLyricFile(@NonNull String title, @NonNull String artist) {
|
|
|
|
File file = new File(getLrcPath(title, artist));
|
|
|
|
if (file.exists()) {
|
|
|
|
return file;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public static File getLocalLyricOriginalFile(@NonNull String path) {
|
|
|
|
File file = new File(getLrcOriginalPath(path));
|
|
|
|
if (file.exists()) {
|
|
|
|
return file;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String getLrcPath(String title, String artist) {
|
|
|
|
return lrcRootPath + title + " - " + artist + ".lrc";
|
2020-06-30 17:45:47 +00:00
|
|
|
}
|
2021-09-08 18:30:20 +00:00
|
|
|
|
|
|
|
private static String getLrcOriginalPath(String filePath) {
|
|
|
|
return filePath.replace(filePath.substring(filePath.lastIndexOf(".") + 1), "lrc");
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 18:30:20 +00:00
|
|
|
@NonNull
|
|
|
|
public static String decryptBASE64(@NonNull String str) {
|
|
|
|
if (str == null || str.length() == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
byte[] encode = str.getBytes(StandardCharsets.UTF_8);
|
|
|
|
// base64 解密
|
|
|
|
return new String(
|
|
|
|
Base64.decode(encode, 0, encode.length, Base64.DEFAULT), StandardCharsets.UTF_8);
|
|
|
|
}
|
2020-06-30 17:45:47 +00:00
|
|
|
|
2021-09-08 18:30:20 +00:00
|
|
|
@NonNull
|
|
|
|
public static String getStringFromFile(@NonNull String title, @NonNull String artist)
|
|
|
|
throws Exception {
|
|
|
|
File file = new File(getLrcPath(title, artist));
|
|
|
|
FileInputStream fin = new FileInputStream(file);
|
|
|
|
String ret = convertStreamToString(fin);
|
|
|
|
fin.close();
|
|
|
|
return ret;
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2021-09-08 18:30:20 +00:00
|
|
|
private static String convertStreamToString(InputStream is) throws Exception {
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
sb.append(line).append("\n");
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return sb.toString();
|
2020-06-30 17:45:47 +00:00
|
|
|
}
|
2021-09-08 18:30:20 +00:00
|
|
|
|
|
|
|
public static String getStringFromLrc(File file) {
|
|
|
|
try {
|
|
|
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
sb.append(line).append("\n");
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return sb.toString();
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.i("Error", "Error Occurred");
|
|
|
|
}
|
|
|
|
return "";
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|
2021-09-08 18:30:20 +00:00
|
|
|
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|