Android Open Source - AStory File System






From Project

Back to project page AStory.

License

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.

Java Source Code

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);
    }
}




Java Source Code List

com.Litterfeldt.AStory.adapters.LibraryAdapter.java
com.Litterfeldt.AStory.customClasses.CoreApplication.java
com.Litterfeldt.AStory.customClasses.CustomMediaPlayer.java
com.Litterfeldt.AStory.dbConnector.dbBook.java
com.Litterfeldt.AStory.dbConnector.dbConnector.java
com.Litterfeldt.AStory.dbConnector.dbSave.java
com.Litterfeldt.AStory.fragments.LibraryFragment.java
com.Litterfeldt.AStory.fragments.PlayerFragment.java
com.Litterfeldt.AStory.models.Book.java
com.Litterfeldt.AStory.models.Chapter.java
com.Litterfeldt.AStory.models.FileSystem.java
com.Litterfeldt.AStory.models.SaveState.java
com.Litterfeldt.AStory.services.AudioplayerService.java
com.Litterfeldt.AStory.pagerView.java
com.handmark.pulltorefresh.library.ILoadingLayout.java
com.handmark.pulltorefresh.library.IPullToRefresh.java
com.handmark.pulltorefresh.library.LoadingLayoutProxy.java
com.handmark.pulltorefresh.library.OverscrollHelper.java
com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.java
com.handmark.pulltorefresh.library.PullToRefreshBase.java
com.handmark.pulltorefresh.library.PullToRefreshExpandableListView.java
com.handmark.pulltorefresh.library.PullToRefreshGridView.java
com.handmark.pulltorefresh.library.PullToRefreshHorizontalScrollView.java
com.handmark.pulltorefresh.library.PullToRefreshListView.java
com.handmark.pulltorefresh.library.PullToRefreshScrollView.java
com.handmark.pulltorefresh.library.PullToRefreshWebView.java
com.handmark.pulltorefresh.library.extras.PullToRefreshWebView2.java
com.handmark.pulltorefresh.library.extras.SoundPullEventListener.java
com.handmark.pulltorefresh.library.internal.EmptyViewMethodAccessor.java
com.handmark.pulltorefresh.library.internal.FlipLoadingLayout.java
com.handmark.pulltorefresh.library.internal.IndicatorLayout.java
com.handmark.pulltorefresh.library.internal.LoadingLayout.java
com.handmark.pulltorefresh.library.internal.RotateLoadingLayout.java
com.handmark.pulltorefresh.library.internal.Utils.java
com.handmark.pulltorefresh.library.internal.ViewCompat.java