public static final String favFile = "favlist.txt";
private void getOrCreateFavFile() {
File fFile = new File(getFilesDir(), favFile);
if (!fFile.isFile()){
if(!isWriteTextFileToInternalStorage(favFile,"")) {
Toast.makeText(this, R.string.ERROR_FILE_CREATE, Toast.LENGTH_LONG).show();
finish();
} else
{
Toast.makeText(this, "File created", Toast.LENGTH_LONG).show();
}
} else {
//favList = getListFromTextFile(favFile);
favList = getTextFileData(favFile);
}
}
private boolean isWriteTextFileToExternalStorage(String FILENAME, String CONTENT_TXT) {
try {
File myFile = new File("/sdcard/" + FILENAME);
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(CONTENT_TXT);
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'favFile.txt'",
Toast.LENGTH_SHORT).show();
return true;
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
return false;
}
private boolean isWriteTextFileToInternalStorage(String FILENAME, String CONTENT_TXT) {
FileOutputStream fos = null;
Boolean lResult=false;
try {
fos = openFileOutput(FILENAME, MODE_PRIVATE);
fos.write(CONTENT_TXT.getBytes());
// Toast.makeText(this,"Save to " + getFilesDir() + "/" + FILENAME, Toast.LENGTH_LONG).show();
lResult = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return lResult;
}
public List<Song> getTextFileData(String fileName) {
List<Song> songs = new ArrayList<>();
StringBuilder text = new StringBuilder();
try {
FileInputStream fIS = getApplicationContext().openFileInput(fileName);
InputStreamReader isr = new InputStreamReader(fIS, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
text.append(line).append('\n');
String Filepath = new File(new URI(line).getPath()).getCanonicalPath();
songs.add(SongProvider.retrieveSongByPath(this,Filepath));
}
br.close();
} catch (IOException | URISyntaxException e) {
Log.e("Error!", "Error occured while reading text file from Internal Storage!");
}
return songs;
}
private List<Song> getListFromTextFile(String FILENAME){
List<Song> songs = new ArrayList<>();
FileInputStream fis = null;
try {
fis = openFileInput(FILENAME);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String textLine = br.readLine();
while (textLine != null) {
sb.append(textLine).append("\n"); // all text
//Uri songUri = Uri.parse(textLine);
String Filepath = new File(new URI(textLine).getPath()).getCanonicalPath();
songs.add(SongProvider.retrieveSongByPath(this,Filepath));
}
// Toast.makeText(this,sb.toString(), Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return songs;
}
private void SaveListToTextFile() {
if (isWriteTextFileToInternalStorage(favFile,false,"")) {
for(int i=0; i<favList.size();i++) {
final Song favSong = favList.get(i);
if (!isWriteTextFileToInternalStorage(favFile,true,favSong.path + "\n")) {
//Toast.makeText(this,favSong.path, Toast.LENGTH_LONG).show();
return;
}
}
}
if(favList.size()>0) {
String data = getTextFileData(favFile);
songLyrics.setText(data);
}
}
public String getTextFileData(String fileName) {
StringBuilder text = new StringBuilder();
try {
FileInputStream fIS = getApplicationContext().openFileInput(fileName);
InputStreamReader isr = new InputStreamReader(fIS, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
text.append(line + '\n');
}
br.close();
} catch (IOException e) {
Log.e("Error!", "Error occured while reading text file from Internal Storage!");
}
return text.toString();
}
private boolean isWriteTextFileToInternalStorage(String FILENAME, Boolean isAppend, String CONTENT_TXT ) {
FileOutputStream fos = null;
Boolean lResult=false;
try {
if(isAppend) {
fos = openFileOutput(FILENAME, MODE_APPEND);
} else {
fos = openFileOutput(FILENAME, MODE_PRIVATE);
}
fos.write(CONTENT_TXT.getBytes());
// Toast.makeText(this,"Save to " + getFilesDir() + "/" + FILENAME, Toast.LENGTH_LONG).show();
lResult = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return lResult;
}
/* DOWNLOAD PROCEDURES
Async Task to read textfile from url and save to internal storage... */
private class GetTextFileFromServer extends AsyncTask<String, Void, Void> {
String lineTextHolder,allTextHolder = null, finalContents = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String... params) {
String TextFileURL = params[0]; // Get Path from params
finalContents = "";
try {
URL url = new URL(TextFileURL);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(url.openStream()));
while ((lineTextHolder = bufferReader.readLine()) != null) {
allTextHolder += lineTextHolder + "\n";
}
bufferReader.close();
} catch (MalformedURLException malformedURLException) {
// TODO Auto-generated catch block
malformedURLException.printStackTrace();
} catch (IOException iOException) {
// TODO Auto-generated catch block
iOException.printStackTrace();
} finally {
finalContents = allTextHolder;
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
writeFileExternalStorage(finalContents);
songLyrics.setText(finalContents);
}
}
private String getOreadTextFile(File FILE_NAME) {
if(FILE_NAME.isFile()){
StringBuilder finalContents = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(FILE_NAME));
String lineTextHolder;
int currentLine = 0;
while ((lineTextHolder = br.readLine()) != null){
currentLine++;
String s;
if(isOdd(currentLine)) {
s = "<font color='##000000'>" + lineTextHolder + "</font>" + "<br>";
} else {
s = "<font color='#1B5E20'>" + lineTextHolder + "</font>" + "<br>";
}
finalContents.append(s);
}
for(int i=0; i<6;i++) {
finalContents.append('\n');
}
br.close();
} catch (IOException e) {
e.printStackTrace();
finalContents = new StringBuilder();
}
return finalContents.toString();
}
return "";
}
public void writeFileExternalStorage(String finalContents) {
FileOutputStream outputStream = null;
try {
currentTextFilePath.createNewFile();
//second argument of FileOutputStream constructor indicates whether to append or create new file if one exists
outputStream = new FileOutputStream(currentTextFilePath, true);
outputStream.write(finalContents.getBytes());
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private String GetAssetTextFile(String FILE_NAME){
BufferedReader reader;
StringBuilder finalContents = new StringBuilder();
try{
final InputStream file = getAssets().open(FILE_NAME);
reader = new BufferedReader(new InputStreamReader(file));
String lineTextHolder = reader.readLine();
int currentLine = 0;
while(lineTextHolder != null){
currentLine++;
String s;
if(isOdd(currentLine)) {
s = "<font color='##000000'>" + lineTextHolder + "</font>" + "<br>";
} else {
s = "<font color='#1B5E20'>" + lineTextHolder + "</font>" + "<br>";
}
finalContents.append(s).append("\n");;
lineTextHolder = reader.readLine();
}
} catch(IOException ioe){
ioe.printStackTrace();
finalContents = new StringBuilder();
}
if(finalContents.toString().isEmpty()) finalContents.append("lyrics not found.");
return finalContents.toString();
}
private void showLyrics(){
imageDisk.setVisibility(INVISIBLE);
Linear_Resize.setVisibility(VISIBLE);
scrollView.setBackgroundColor(Color.WHITE);
songLyrics.setVisibility(VISIBLE);
songLyrics.setTextColor(Color.BLACK);
String sl = getOreadTextFile(currentTextFilePath);
if(sl.isEmpty()){
sl = GetAssetTextFile( "SongAndLyrics/" + currentTitle + ".txt");
}
songLyrics.setText(Html.fromHtml(sl));
}
boolean isOdd( int val ) { return (val & 0x01) != 0; }
private void hideLyrics(){
imageDisk.setVisibility(VISIBLE);
songLyrics.setVisibility(INVISIBLE);
scrollView.setBackgroundColor(Color.TRANSPARENT);
Linear_Resize.setVisibility(INVISIBLE);
}