Back to project page AStory.
The source code is released under:
Apache License
If you think the Android project AStory listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.Litterfeldt.AStory.models; //from w ww.j a va2s. c om import android.media.MediaMetadataRetriever; import android.os.Environment; import android.util.Log; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class FileSystem { private static FileSystem mInstance = null; public static FileSystem getInstance(){ if (mInstance == null) { mInstance = new FileSystem(); } return mInstance; } private File defaultDir; private static MediaMetadataRetriever mmr; private static final Set<String> acceptedFormats = new HashSet<String>(Arrays.asList( new String[]{"mp3", "m4a", "aac", "flac"} )); public FileSystem(){ defaultDir = new File(Environment.getExternalStorageDirectory() +"/Audiobooks"); if(!defaultDir.exists()){ defaultDir.mkdir(); defaultDir = new File(Environment.getExternalStorageDirectory() +"/Audiobooks"); } mmr = new MediaMetadataRetriever(); } public ArrayList<ArrayList<String>> allocateBookFolderContent(){ ArrayList<ArrayList<String>> bookPathList = new ArrayList<ArrayList<String>>(); File[] files = defaultDir.listFiles(); if (files == null){ return null; }else { for (File f : files){ if(f.isDirectory()){ ArrayList<String> dirList = new ArrayList<String>(); for (File d : f.listFiles()){ String fullPath = d.getAbsolutePath(); String fileType = fullPath.substring(fullPath.lastIndexOf(".")+1); if(acceptedFormats.contains(fileType.toLowerCase())){ dirList.add(fullPath); } } bookPathList.add(dirList); } else if (f.isFile()){ String fullpath = f.getAbsolutePath(); String filetype = fullpath.substring(fullpath.lastIndexOf(".")+1); if(acceptedFormats.contains(filetype.toLowerCase())){ ArrayList<String> book = new ArrayList<String>(); book.add(fullpath); bookPathList.add(book); }}} return bookPathList; } } //Todo rewrite algorithm public Book mockBookFromPath(ArrayList<String> paths){ if(paths.size() < 1) { return null; } Collections.sort(paths); String firstPath = paths.get(0); mmr.setDataSource(firstPath); String bookName = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM); if (bookName == null || bookName == ""){ bookName = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE); } if (bookName == null || bookName == ""){ bookName = firstPath.substring(firstPath.lastIndexOf("/")+1,firstPath.lastIndexOf(".")).replaceAll("[^a-zA-Z ]", "").trim(); } if (bookName == null || bookName == ""){ return null; } String author = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST); if (author == null || author == ""){ author = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_WRITER); } if (author == null || author == ""){ author = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_AUTHOR); } if (author == null || author == ""){ author = "Unknown Author"; } byte[] img = mmr.getEmbeddedPicture(); ArrayList<Chapter> chapters = new ArrayList<Chapter>(); for (String s : paths){ mmr.setDataSource(s); String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); String chapterNum = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER); if(chapterNum == null) { chapterNum = "" + paths.indexOf(s)+1; } Chapter c = new Chapter(0,s,toInteger(chapterNum), toInteger(duration)); chapters.add(c); } Book book = new Book(0,bookName,author,chapters,img); return book; } private Integer toInteger(String str) { String s = ""; for( char c : str.toCharArray()) { if (!s.isEmpty() && !Character.isDigit(c)){ break; } if (Character.isDigit(c)){ s+=c; } } return s.isEmpty() ? 0 : Integer.valueOf(s); } }