Add Spotless

This commit is contained in:
Hemanth S 2020-10-06 14:16:04 +05:30
parent 2af13a4e6c
commit defcd86152
286 changed files with 15604 additions and 13757 deletions

View file

@ -1,30 +1,26 @@
package code.name.monkey.retromusic.lyrics;
/**
* Desc : 歌词实体
* Author : Lauzy
* Date : 2017/10/13
* Blog : http://www.jianshu.com/u/e76853f863a9
* Email : freedompaladin@gmail.com
* Desc : 歌词实体 Author : Lauzy Date : 2017/10/13 Blog : http://www.jianshu.com/u/e76853f863a9 Email :
* freedompaladin@gmail.com
*/
public class Lrc {
private long time;
private String text;
private long time;
private String text;
public long getTime() {
return time;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public void setTime(long time) {
this.time = time;
}
public String getText() {
return text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
public void setText(String text) {
this.text = text;
}
}

View file

@ -19,99 +19,94 @@ import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
/**
* 一行歌词实体
*/
/** 一行歌词实体 */
class LrcEntry implements Comparable<LrcEntry> {
public static final int GRAVITY_CENTER = 0;
public static final int GRAVITY_LEFT = 1;
public static final int GRAVITY_RIGHT = 2;
private long time;
private String text;
private String secondText;
private StaticLayout staticLayout;
/**
* 歌词距离视图顶部的距离
*/
private float offset = Float.MIN_VALUE;
public static final int GRAVITY_CENTER = 0;
public static final int GRAVITY_LEFT = 1;
public static final int GRAVITY_RIGHT = 2;
private long time;
private String text;
private String secondText;
private StaticLayout staticLayout;
/** 歌词距离视图顶部的距离 */
private float offset = Float.MIN_VALUE;
LrcEntry(long time, String text) {
this.time = time;
this.text = text;
LrcEntry(long time, String text) {
this.time = time;
this.text = text;
}
LrcEntry(long time, String text, String secondText) {
this.time = time;
this.text = text;
this.secondText = secondText;
}
void init(TextPaint paint, int width, int gravity) {
Layout.Alignment align;
switch (gravity) {
case GRAVITY_LEFT:
align = Layout.Alignment.ALIGN_NORMAL;
break;
default:
case GRAVITY_CENTER:
align = Layout.Alignment.ALIGN_CENTER;
break;
case GRAVITY_RIGHT:
align = Layout.Alignment.ALIGN_OPPOSITE;
break;
}
staticLayout = new StaticLayout(getShowText(), paint, width, align, 1f, 0f, false);
LrcEntry(long time, String text, String secondText) {
this.time = time;
this.text = text;
this.secondText = secondText;
offset = Float.MIN_VALUE;
}
long getTime() {
return time;
}
StaticLayout getStaticLayout() {
return staticLayout;
}
int getHeight() {
if (staticLayout == null) {
return 0;
}
return staticLayout.getHeight();
}
void init(TextPaint paint, int width, int gravity) {
Layout.Alignment align;
switch (gravity) {
case GRAVITY_LEFT:
align = Layout.Alignment.ALIGN_NORMAL;
break;
public float getOffset() {
return offset;
}
default:
case GRAVITY_CENTER:
align = Layout.Alignment.ALIGN_CENTER;
break;
public void setOffset(float offset) {
this.offset = offset;
}
case GRAVITY_RIGHT:
align = Layout.Alignment.ALIGN_OPPOSITE;
break;
}
staticLayout = new StaticLayout(getShowText(), paint, width, align, 1f, 0f, false);
String getText() {
return text;
}
offset = Float.MIN_VALUE;
void setSecondText(String secondText) {
this.secondText = secondText;
}
private String getShowText() {
if (!TextUtils.isEmpty(secondText)) {
return text + "\n" + secondText;
} else {
return text;
}
}
long getTime() {
return time;
@Override
public int compareTo(LrcEntry entry) {
if (entry == null) {
return -1;
}
StaticLayout getStaticLayout() {
return staticLayout;
}
int getHeight() {
if (staticLayout == null) {
return 0;
}
return staticLayout.getHeight();
}
public float getOffset() {
return offset;
}
public void setOffset(float offset) {
this.offset = offset;
}
String getText() {
return text;
}
void setSecondText(String secondText) {
this.secondText = secondText;
}
private String getShowText() {
if (!TextUtils.isEmpty(secondText)) {
return text + "\n" + secondText;
} else {
return text;
}
}
@Override
public int compareTo(LrcEntry entry) {
if (entry == null) {
return -1;
}
return (int) (time - entry.getTime());
}
}
return (int) (time - entry.getTime());
}
}

View file

@ -1,7 +1,6 @@
package code.name.monkey.retromusic.lyrics;
import android.content.Context;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
@ -18,120 +17,121 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Desc : 歌词解析
* Author : Lauzy
* Date : 2017/10/13
* Blog : http://www.jianshu.com/u/e76853f863a9
* Email : freedompaladin@gmail.com
* Desc : 歌词解析 Author : Lauzy Date : 2017/10/13 Blog : http://www.jianshu.com/u/e76853f863a9 Email :
* freedompaladin@gmail.com
*/
public class LrcHelper {
private static final String CHARSET = "utf-8";
//[03:56.00][03:18.00][02:06.00][01:07.00]原谅我这一生不羁放纵爱自由
private static final String LINE_REGEX = "((\\[\\d{2}:\\d{2}\\.\\d{2}])+)(.*)";
private static final String TIME_REGEX = "\\[(\\d{2}):(\\d{2})\\.(\\d{2})]";
private static final String CHARSET = "utf-8";
// [03:56.00][03:18.00][02:06.00][01:07.00]原谅我这一生不羁放纵爱自由
private static final String LINE_REGEX = "((\\[\\d{2}:\\d{2}\\.\\d{2}])+)(.*)";
private static final String TIME_REGEX = "\\[(\\d{2}):(\\d{2})\\.(\\d{2})]";
public static List<Lrc> parseLrcFromAssets(Context context, String fileName) {
try {
return parseInputStream(context.getResources().getAssets().open(fileName));
} catch (IOException e) {
e.printStackTrace();
}
return null;
public static List<Lrc> parseLrcFromAssets(Context context, String fileName) {
try {
return parseInputStream(context.getResources().getAssets().open(fileName));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static List<Lrc> parseLrcFromFile(File file) {
try {
return parseInputStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
public static List<Lrc> parseLrcFromFile(File file) {
try {
return parseInputStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
private static List<Lrc> parseInputStream(InputStream inputStream) {
List<Lrc> lrcs = new ArrayList<>();
InputStreamReader isr = null;
BufferedReader br = null;
try {
isr = new InputStreamReader(inputStream, CHARSET);
br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
List<Lrc> lrcList = parseLrc(line);
if (lrcList != null && lrcList.size() != 0) {
lrcs.addAll(lrcList);
}
}
sortLrcs(lrcs);
return lrcs;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (isr != null) {
isr.close();
}
if (br != null) {
br.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
private static List<Lrc> parseInputStream(InputStream inputStream) {
List<Lrc> lrcs = new ArrayList<>();
InputStreamReader isr = null;
BufferedReader br = null;
try {
isr = new InputStreamReader(inputStream, CHARSET);
br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
List<Lrc> lrcList = parseLrc(line);
if (lrcList != null && lrcList.size() != 0) {
lrcs.addAll(lrcList);
}
return lrcs;
}
sortLrcs(lrcs);
return lrcs;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (isr != null) {
isr.close();
}
if (br != null) {
br.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
return lrcs;
}
private static void sortLrcs(List<Lrc> lrcs) {
Collections.sort(lrcs, new Comparator<Lrc>() {
@Override
public int compare(Lrc o1, Lrc o2) {
return (int) (o1.getTime() - o2.getTime());
}
private static void sortLrcs(List<Lrc> lrcs) {
Collections.sort(
lrcs,
new Comparator<Lrc>() {
@Override
public int compare(Lrc o1, Lrc o2) {
return (int) (o1.getTime() - o2.getTime());
}
});
}
private static List<Lrc> parseLrc(String lrcLine) {
if (lrcLine.trim().isEmpty()) {
return null;
}
List<Lrc> lrcs = new ArrayList<>();
Matcher matcher = Pattern.compile(LINE_REGEX).matcher(lrcLine);
if (!matcher.matches()) {
return null;
}
private static List<Lrc> parseLrc(String lrcLine) {
if (lrcLine.trim().isEmpty()) {
return null;
}
List<Lrc> lrcs = new ArrayList<>();
Matcher matcher = Pattern.compile(LINE_REGEX).matcher(lrcLine);
if (!matcher.matches()) {
return null;
}
String time = matcher.group(1);
String content = matcher.group(3);
Matcher timeMatcher = Pattern.compile(TIME_REGEX).matcher(time);
String time = matcher.group(1);
String content = matcher.group(3);
Matcher timeMatcher = Pattern.compile(TIME_REGEX).matcher(time);
while (timeMatcher.find()) {
String min = timeMatcher.group(1);
String sec = timeMatcher.group(2);
String mil = timeMatcher.group(3);
Lrc lrc = new Lrc();
if (content != null && content.length() != 0) {
lrc.setTime(Long.parseLong(min) * 60 * 1000 + Long.parseLong(sec) * 1000
+ Long.parseLong(mil) * 10);
lrc.setText(content);
lrcs.add(lrc);
}
}
return lrcs;
while (timeMatcher.find()) {
String min = timeMatcher.group(1);
String sec = timeMatcher.group(2);
String mil = timeMatcher.group(3);
Lrc lrc = new Lrc();
if (content != null && content.length() != 0) {
lrc.setTime(
Long.parseLong(min) * 60 * 1000
+ Long.parseLong(sec) * 1000
+ Long.parseLong(mil) * 10);
lrc.setText(content);
lrcs.add(lrc);
}
}
return lrcs;
}
public static String formatTime(long time) {
int min = (int) (time / 60000);
int sec = (int) (time / 1000 % 60);
return adjustFormat(min) + ":" + adjustFormat(sec);
}
public static String formatTime(long time) {
int min = (int) (time / 60000);
int sec = (int) (time / 1000 % 60);
return adjustFormat(min) + ":" + adjustFormat(sec);
}
private static String adjustFormat(int time) {
if (time < 10) {
return "0" + time;
}
return time + "";
private static String adjustFormat(int time) {
if (time < 10) {
return "0" + time;
}
}
return time + "";
}
}

View file

@ -17,7 +17,6 @@ package code.name.monkey.retromusic.lyrics;
import android.animation.ValueAnimator;
import android.text.TextUtils;
import android.text.format.DateUtils;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
@ -36,198 +35,186 @@ import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 工具类
*/
/** 工具类 */
class LrcUtils {
private static final Pattern PATTERN_LINE = Pattern.compile("((\\[\\d\\d:\\d\\d\\.\\d{2,3}\\])+)(.+)");
private static final Pattern PATTERN_TIME = Pattern.compile("\\[(\\d\\d):(\\d\\d)\\.(\\d{2,3})\\]");
private static final Pattern PATTERN_LINE =
Pattern.compile("((\\[\\d\\d:\\d\\d\\.\\d{2,3}\\])+)(.+)");
private static final Pattern PATTERN_TIME =
Pattern.compile("\\[(\\d\\d):(\\d\\d)\\.(\\d{2,3})\\]");
/**
* 从文件解析双语歌词
*/
static List<LrcEntry> parseLrc(File[] lrcFiles) {
if (lrcFiles == null || lrcFiles.length != 2 || lrcFiles[0] == null) {
return null;
}
File mainLrcFile = lrcFiles[0];
File secondLrcFile = lrcFiles[1];
List<LrcEntry> mainEntryList = parseLrc(mainLrcFile);
List<LrcEntry> secondEntryList = parseLrc(secondLrcFile);
if (mainEntryList != null && secondEntryList != null) {
for (LrcEntry mainEntry : mainEntryList) {
for (LrcEntry secondEntry : secondEntryList) {
if (mainEntry.getTime() == secondEntry.getTime()) {
mainEntry.setSecondText(secondEntry.getText());
}
}
}
}
return mainEntryList;
/** 从文件解析双语歌词 */
static List<LrcEntry> parseLrc(File[] lrcFiles) {
if (lrcFiles == null || lrcFiles.length != 2 || lrcFiles[0] == null) {
return null;
}
/**
* 从文件解析歌词
*/
private static List<LrcEntry> parseLrc(File lrcFile) {
if (lrcFile == null || !lrcFile.exists()) {
return null;
}
File mainLrcFile = lrcFiles[0];
File secondLrcFile = lrcFiles[1];
List<LrcEntry> mainEntryList = parseLrc(mainLrcFile);
List<LrcEntry> secondEntryList = parseLrc(secondLrcFile);
List<LrcEntry> entryList = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(lrcFile), StandardCharsets.UTF_8));
String line;
while ((line = br.readLine()) != null) {
List<LrcEntry> list = parseLine(line);
if (list != null && !list.isEmpty()) {
entryList.addAll(list);
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
if (mainEntryList != null && secondEntryList != null) {
for (LrcEntry mainEntry : mainEntryList) {
for (LrcEntry secondEntry : secondEntryList) {
if (mainEntry.getTime() == secondEntry.getTime()) {
mainEntry.setSecondText(secondEntry.getText());
}
}
}
}
return mainEntryList;
}
Collections.sort(entryList);
return entryList;
/** 从文件解析歌词 */
private static List<LrcEntry> parseLrc(File lrcFile) {
if (lrcFile == null || !lrcFile.exists()) {
return null;
}
/**
* 从文本解析双语歌词
*/
static List<LrcEntry> parseLrc(String[] lrcTexts) {
if (lrcTexts == null || lrcTexts.length != 2 || TextUtils.isEmpty(lrcTexts[0])) {
return null;
List<LrcEntry> entryList = new ArrayList<>();
try {
BufferedReader br =
new BufferedReader(
new InputStreamReader(new FileInputStream(lrcFile), StandardCharsets.UTF_8));
String line;
while ((line = br.readLine()) != null) {
List<LrcEntry> list = parseLine(line);
if (list != null && !list.isEmpty()) {
entryList.addAll(list);
}
String mainLrcText = lrcTexts[0];
String secondLrcText = lrcTexts[1];
List<LrcEntry> mainEntryList = parseLrc(mainLrcText);
List<LrcEntry> secondEntryList = parseLrc(secondLrcText);
if (mainEntryList != null && secondEntryList != null) {
for (LrcEntry mainEntry : mainEntryList) {
for (LrcEntry secondEntry : secondEntryList) {
if (mainEntry.getTime() == secondEntry.getTime()) {
mainEntry.setSecondText(secondEntry.getText());
}
}
}
}
return mainEntryList;
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
/**
* 从文本解析歌词
*/
private static List<LrcEntry> parseLrc(String lrcText) {
if (TextUtils.isEmpty(lrcText)) {
return null;
}
Collections.sort(entryList);
return entryList;
}
if (lrcText.startsWith("\uFEFF")) {
lrcText = lrcText.replace("\uFEFF", "");
}
List<LrcEntry> entryList = new ArrayList<>();
String[] array = lrcText.split("\\n");
for (String line : array) {
List<LrcEntry> list = parseLine(line);
if (list != null && !list.isEmpty()) {
entryList.addAll(list);
}
}
Collections.sort(entryList);
return entryList;
/** 从文本解析双语歌词 */
static List<LrcEntry> parseLrc(String[] lrcTexts) {
if (lrcTexts == null || lrcTexts.length != 2 || TextUtils.isEmpty(lrcTexts[0])) {
return null;
}
/**
* 获取网络文本需要在工作线程中执行
*/
static String getContentFromNetwork(String url, String charset) {
String lrcText = null;
try {
URL _url = new URL(url);
HttpURLConnection conn = (HttpURLConnection) _url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
is.close();
bos.close();
lrcText = bos.toString(charset);
}
} catch (Exception e) {
e.printStackTrace();
String mainLrcText = lrcTexts[0];
String secondLrcText = lrcTexts[1];
List<LrcEntry> mainEntryList = parseLrc(mainLrcText);
List<LrcEntry> secondEntryList = parseLrc(secondLrcText);
if (mainEntryList != null && secondEntryList != null) {
for (LrcEntry mainEntry : mainEntryList) {
for (LrcEntry secondEntry : secondEntryList) {
if (mainEntry.getTime() == secondEntry.getTime()) {
mainEntry.setSecondText(secondEntry.getText());
}
}
return lrcText;
}
}
return mainEntryList;
}
/** 从文本解析歌词 */
private static List<LrcEntry> parseLrc(String lrcText) {
if (TextUtils.isEmpty(lrcText)) {
return null;
}
/**
* 解析一行歌词
*/
private static List<LrcEntry> parseLine(String line) {
if (TextUtils.isEmpty(line)) {
return null;
}
line = line.trim();
// [00:17.65]让我掉下眼泪的
Matcher lineMatcher = PATTERN_LINE.matcher(line);
if (!lineMatcher.matches()) {
return null;
}
String times = lineMatcher.group(1);
String text = lineMatcher.group(3);
List<LrcEntry> entryList = new ArrayList<>();
// [00:17.65]
Matcher timeMatcher = PATTERN_TIME.matcher(times);
while (timeMatcher.find()) {
long min = Long.parseLong(timeMatcher.group(1));
long sec = Long.parseLong(timeMatcher.group(2));
String milString = timeMatcher.group(3);
long mil = Long.parseLong(milString);
// 如果毫秒是两位数需要乘以10
if (milString.length() == 2) {
mil = mil * 10;
}
long time = min * DateUtils.MINUTE_IN_MILLIS + sec * DateUtils.SECOND_IN_MILLIS + mil;
entryList.add(new LrcEntry(time, text));
}
return entryList;
if (lrcText.startsWith("\uFEFF")) {
lrcText = lrcText.replace("\uFEFF", "");
}
/**
* 转为[:]
*/
static String formatTime(long milli) {
int m = (int) (milli / DateUtils.MINUTE_IN_MILLIS);
int s = (int) ((milli / DateUtils.SECOND_IN_MILLIS) % 60);
String mm = String.format(Locale.getDefault(), "%02d", m);
String ss = String.format(Locale.getDefault(), "%02d", s);
return mm + ":" + ss;
List<LrcEntry> entryList = new ArrayList<>();
String[] array = lrcText.split("\\n");
for (String line : array) {
List<LrcEntry> list = parseLine(line);
if (list != null && !list.isEmpty()) {
entryList.addAll(list);
}
}
static void resetDurationScale() {
try {
Field mField = ValueAnimator.class.getDeclaredField("sDurationScale");
mField.setAccessible(true);
mField.setFloat(null, 1);
} catch (Exception e) {
e.printStackTrace();
Collections.sort(entryList);
return entryList;
}
/** 获取网络文本,需要在工作线程中执行 */
static String getContentFromNetwork(String url, String charset) {
String lrcText = null;
try {
URL _url = new URL(url);
HttpURLConnection conn = (HttpURLConnection) _url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
is.close();
bos.close();
lrcText = bos.toString(charset);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return lrcText;
}
/** 解析一行歌词 */
private static List<LrcEntry> parseLine(String line) {
if (TextUtils.isEmpty(line)) {
return null;
}
line = line.trim();
// [00:17.65]让我掉下眼泪的
Matcher lineMatcher = PATTERN_LINE.matcher(line);
if (!lineMatcher.matches()) {
return null;
}
String times = lineMatcher.group(1);
String text = lineMatcher.group(3);
List<LrcEntry> entryList = new ArrayList<>();
// [00:17.65]
Matcher timeMatcher = PATTERN_TIME.matcher(times);
while (timeMatcher.find()) {
long min = Long.parseLong(timeMatcher.group(1));
long sec = Long.parseLong(timeMatcher.group(2));
String milString = timeMatcher.group(3);
long mil = Long.parseLong(milString);
// 如果毫秒是两位数需要乘以10
if (milString.length() == 2) {
mil = mil * 10;
}
long time = min * DateUtils.MINUTE_IN_MILLIS + sec * DateUtils.SECOND_IN_MILLIS + mil;
entryList.add(new LrcEntry(time, text));
}
return entryList;
}
/** 转为[分:秒] */
static String formatTime(long milli) {
int m = (int) (milli / DateUtils.MINUTE_IN_MILLIS);
int s = (int) ((milli / DateUtils.SECOND_IN_MILLIS) % 60);
String mm = String.format(Locale.getDefault(), "%02d", m);
String ss = String.format(Locale.getDefault(), "%02d", s);
return mm + ":" + ss;
}
static void resetDurationScale() {
try {
Field mField = ValueAnimator.class.getDeclaredField("sDurationScale");
mField.setAccessible(true);
mField.setFloat(null, 1);
} catch (Exception e) {
e.printStackTrace();
}
}
}