Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Surpport the different character encodings for lyrics files. #424

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ dependencies {
implementation('com.crashlytics.sdk.android:answers:1.3.13@aar') {
transitive = true
}
implementation 'com.googlecode.juniversalchardet:juniversalchardet:1.0.3'
testImplementation 'junit:junit:4.12'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
3 changes: 2 additions & 1 deletion app/src/main/java/com/kabouzeid/gramophone/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public void onBillingInitialized() {
}

public static boolean isProVersion() {
return BuildConfig.DEBUG || app.billingProcessor.isPurchased(PRO_VERSION_PRODUCT_ID);

//return BuildConfig.DEBUG || app.billingProcessor.isPurchased(PRO_VERSION_PRODUCT_ID);
}

public static App getInstance() {
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/java/com/kabouzeid/gramophone/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import com.kabouzeid.gramophone.loader.SortedCursor;
import com.kabouzeid.gramophone.model.Song;

import org.mozilla.universalchardet.UniversalDetector;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -179,13 +182,51 @@ public static String readFromStream(InputStream is) throws Exception {
return sb.toString();
}

public static String readFromStream(InputStream is, Charset charset) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
if (sb.length() > 0) sb.append("\n");
sb.append(line);
}
reader.close();
return sb.toString();
}

public static String read(File file) throws Exception {
FileInputStream fin = new FileInputStream(file);
String ret = readFromStream(fin);
fin.close();
return ret;
}

public static String readWithCharsetDet(File file) throws Exception {
//Detect the charset of the file
FileInputStream finForDet = new FileInputStream(file);
UniversalDetector detector = new UniversalDetector(null);
int nread;
byte[] buf = new byte[4096];
while ((nread = finForDet.read(buf)) > 0 && !detector.isDone()) {
detector.handleData(buf, 0, nread);
}
detector.dataEnd();
String encoding = detector.getDetectedCharset();
detector.reset();

Charset charset;
if(encoding == null){
charset = Charset.forName("UTF-8");
}else {
charset = Charset.forName(encoding);
}

FileInputStream fin = new FileInputStream(file);
String ret = readFromStream(fin, charset);
fin.close();
return ret;
}

public static String safeGetCanonicalPath(File file) {
try {
return file.getCanonicalPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public static String getLyrics(Song song) {
if (files != null && files.length > 0) {
for (File f : files) {
try {
String newLyrics = FileUtil.read(f);
String newLyrics = FileUtil.readWithCharsetDet(f);
if (newLyrics != null && !newLyrics.trim().isEmpty()) {
if (AbsSynchronizedLyrics.isSynchronized(newLyrics)) {
return newLyrics;
Expand Down