ScanUtils.java 10.3 KB
package com.gimi.common.cinema.utils;

import android.content.Context;
import com.gimi.common.cinema.model.FolderItem;
import com.xgimi.gimicinema.R;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by pc on 2014/12/9
 */
public class ScanUtils {

    private Context context;
    String[] picExtensions;
    String[] mediaExtensions;

    public ScanUtils(Context context) {
        this.context = context;
        picExtensions = context.getResources().getStringArray(R.array.photo_filter);
        mediaExtensions = context.getResources().getStringArray(R.array.video_filter);
    }

    public ArrayList<FolderItem> getTypes(String rootPath) {
        ArrayList<FolderItem> types = new ArrayList<FolderItem>();
        File file = new File(rootPath);
        String[] fileDirs = file.list();

        for (String fileDir : fileDirs) {
            File curFile = new File(rootPath + fileDir);
            if (curFile.isDirectory()) {
                FolderItem folderItem = new FolderItem();
                folderItem.setPosterPath(getPoster(rootPath + fileDir + "/"));
                folderItem.setFolderName(fileDir);
                folderItem.setFolderPath(rootPath + fileDir + "/");
                types.add(folderItem);
            }
        }
        return types;
    }

    //get one picture from path folder
    public String getPoster(String path) {
        File file = new File(path);
        String[] files = file.list();
        String posterPath = null;
        for (int i = 0; i < files.length; i++) {
            if (checkType(path + files[i], picExtensions)) {
                posterPath = path + files[i];
                break;
            }
        }
        return posterPath;
    }

    private boolean checkType(String name, String[] extensions) {
        for (String end : extensions) {
            // Name never to null, without exception handling
            if (name.toLowerCase().endsWith(end)) {
                return true;
            }
        }
        return false;
    }

//    public ArrayList<MoviesItem> getMovies1(String rootPath) {
//        ArrayList<MoviesItem> movies = new ArrayList<MoviesItem>();
//        File file = new File(rootPath);
//        String[] fileDirs = file.list();
//        //遍历
//        for (String fileDir : fileDirs) {
//            HashMap<String, String> nameId = getNameId(rootPath + fileDir + "/");
//            if (!checkType(rootPath + fileDir, picExtensions)) {
//                File curFiles = new File(rootPath + fileDir);
//                if (curFiles.isDirectory()) {
//                    String curPath = curFiles.getAbsoluteFile().toString();
//                    MoviesItem moviesItem = new MoviesItem();
//                    //poster add douban id
//                    moviesItem.setMoviePoster(nameId.get(fileDir));
//                    //path as hash key
//                    String media = getMedia(curPath + "/", mediaExtensions);
//                    if (!TextUtils.isEmpty(media)) {
//                        File mFile = new File(media);
//                        long length = mFile.length();
//                        moviesItem.setMoviePath(length + "");
//                    }
//                    moviesItem.setMovieName(fileDir);
////                    File mmPathFile = new File(curPath + "/" + "mm.txt");
////                    if (mmPathFile.exists()) {
////                        moviesItem.setSummary(mmPathFile.getAbsolutePath());
////                    }
//                    movies.add(moviesItem);
//                }
//            }
//        }
//        return movies;
//    }

    //get the name Id
    private HashMap<String, String> getNameId(String rootPath) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        File file = new File(rootPath + "name-id.txt");
        if (file.exists()) {
            ArrayList<String> a = FileReadUtils.readLines(file.getAbsolutePath());
            for (String s : a) {
                if (s.contains("#")) {
                    String[] split = s.split("#");
                    if (split.length >= 2) {
                        hashMap.put(split[0], split[1]);
                    }
                }
            }
        }
        return hashMap;
    }

    private String getMedia(String path, String[] extensions) {
        File file = new File(path);
        String[] files = file.list();
        String posterPath = null;
        for (int i = 0; i < files.length; i++) {
            if (checkType(path + files[i], extensions)) {
                posterPath = path + files[i];
                break;
            }
        }
        return posterPath;
    }
//
//    public ArrayList<MoviesItem> searchAll(String rootPath, String keyWord) {
//        ArrayList<MoviesItem> moviesItems = new ArrayList<MoviesItem>();
//        ArrayList<FolderItem> types = getTypes(rootPath);
//        for (FolderItem type : types) {
//            moviesItems.addAll(getRightMovies(type.getFolderPath(), keyWord));
//        }
//        return moviesItems;
//    }
//
//    public ArrayList<MoviesItem> getRightMovies(String rootPath, String keyWord) {
//        ArrayList<MoviesItem> movies = new ArrayList<MoviesItem>();
//        File file = new File(rootPath);
//        String[] fileDirs = file.list();
//        //遍历
//        for (String fileDir : fileDirs) {
//            if (!checkType(rootPath + fileDir, picExtensions)) {
//                File curFiles = new File(rootPath + fileDir);
//                if (curFiles.isDirectory()) {
//                    String curPath = curFiles.getAbsoluteFile().toString();
//                    MoviesItem moviesItem = new MoviesItem();
//                    moviesItem.setMoviePoster(getPoster(curPath + "/"));
//                    moviesItem.setMoviePath(getMedia(curPath + "/", mediaExtensions));
//                    moviesItem.setMovieName(fileDir);
//
//                    //to pinyin and charge
//                    if (isRight(moviesItem.getMovieName(), keyWord)) {
//                        movies.add(moviesItem);
//                    }
//                }
//            }
//        }
//        return movies;
//    }

//    public ArrayList<MoviesItem> getAllMovies(String rootPath) {
//        ArrayList<MoviesItem> moviesItems = new ArrayList<MoviesItem>();
//        ArrayList<FolderItem> types = getTypes(rootPath);
//        for (FolderItem type : types) {
//            moviesItems.addAll(getTypeMovies(type.getFolderPath()));
//        }
//        return moviesItems;
//    }

//    public ArrayList<MoviesItem> getAllMovies1(String rootPath) {
//        ArrayList<MoviesItem> moviesItems = new ArrayList<MoviesItem>();
//        ArrayList<FolderItem> types = getTypes(rootPath);
//        for (FolderItem type : types) {
//            moviesItems.addAll(getTypeMovies1(type.getFolderPath()));
//        }
//        return moviesItems;
//    }


//    public ArrayList<MoviesItem> getTypeMovies(String rootPath) {
//        ArrayList<MoviesItem> movies = new ArrayList<MoviesItem>();
//        File file = new File(rootPath);
//        String[] fileDirs = file.list();
//        //遍历
//        for (String fileDir : fileDirs) {
//            if (!checkType(rootPath + fileDir, picExtensions)) {
//                File curFiles = new File(rootPath + fileDir);
//                if (curFiles.isDirectory()) {
//                    String curPath = curFiles.getAbsoluteFile().toString();
//                    MoviesItem moviesItem = new MoviesItem();
//                    moviesItem.setMoviePoster(getPoster(curPath + "/"));
//                    moviesItem.setMoviePath(getMedia(curPath + "/", mediaExtensions));
//                    moviesItem.setMovieName(fileDir);
//
//                    movies.add(moviesItem);
//                }
//            }
//        }
//        return movies;
//    }

//    public ArrayList<MoviesItem> getTypeMovies1(String rootPath) {
//        ArrayList<MoviesItem> movies = new ArrayList<MoviesItem>();
//        File file = new File(rootPath);
//        String[] fileDirs = file.list();
//        HashMap<String, String> nameId = getNameId(rootPath + "/");
//        //遍历
//        for (String fileDir : fileDirs) {
//            if (!checkType(rootPath + fileDir, picExtensions)) {
//                File curFiles = new File(rootPath + fileDir);
//                if (curFiles.isDirectory()) {
//                    String curPath = curFiles.getAbsoluteFile().toString();
//                    MoviesItem moviesItem = new MoviesItem();
//                    moviesItem.setMovieName(fileDir);
//                    //poster as douban id
//                    moviesItem.setMoviePoster(nameId.get(fileDir));
//                    //path as md5
//                    String media = getMedia(curPath + "/", mediaExtensions);
//                    if (!TextUtils.isEmpty(media)) {
//                        File mFile = new File(media);
//                        long length = mFile.length();
//                        moviesItem.setMoviePath(MD5Utils.stringMD5(length + ""));
//                        movies.add(moviesItem);
//                    }
//                }
//            }
//        }
//        return movies;
//    }

    private boolean isRight(String movieName, String keyWord) {
        String firstSpell = PinyinUtil.getFirstSpell(movieName);
        if (firstSpell.toLowerCase().contains(keyWord.toLowerCase())) {
            return true;
        }

        return false;
    }

    public ArrayList<String> getVideo(String rootPath) {
        ArrayList<String> movies = new ArrayList<String>();
        File file = new File(rootPath);
        String[] fileDirs = file.list();
        if (fileDirs == null || fileDirs.length == 0) {
            return movies;
        }
        //遍历
        for (String fileDir : fileDirs) {
            String curPath = rootPath + "/" + fileDir;
            if (checkType(curPath, mediaExtensions)) {
                movies.add(curPath);
            }
        }
        return movies;
    }

    public ArrayList<String> getPicture(String rootPath) {
        ArrayList<String> movies = new ArrayList<String>();
        File file = new File(rootPath);
        String[] fileDirs = file.list();
        //遍历
        for (String fileDir : fileDirs) {
            String curPath = rootPath + "/" + fileDir;
            if (checkType(curPath, picExtensions)) {
                movies.add(curPath);
            }
        }
        return movies;
    }
}