package com.step.upon;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.database.Cursor;
import android.os.Environment;
import android.provider.MediaStore;

public class FileUtility {

    public byte[] trimWAV(String path, String name, int offset, int size) {
        File f = new File(path, "/" + name);
        if (f.exists()) {
            byte[] bytes = new byte[size];
            try {
                BufferedInputStream buf = new BufferedInputStream(new FileInputStream(f));
                buf.read(bytes, offset, bytes.length);
                buf.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bytes;
        } else {
            return null;
        }
    }

    public JSONArray getMp3InfoList(Context context) {
        try {
            Cursor cursor = context.getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                    MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

            JSONArray jsonArray = new JSONArray();
            JSONObject tmpObj = null;

            if (cursor != null) {
                for (int i = 0; i < cursor.getCount(); i++) {
                    cursor.moveToNext();

                    //int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));  //是否为音乐
                    //long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));  //音乐id
                    //String title = cursor.getString((cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));  //音乐标题
                    //String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));  //艺术家
                    //String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));  //专辑
                    String displayName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    //long albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                    //long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));  //时长
                    long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));  //文件大小
                    String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));  //文件路径

                    tmpObj = new JSONObject();
                    tmpObj.put("url" , url);
                    tmpObj.put("size" , size);
                    tmpObj.put("name" , displayName);

                    jsonArray.put(tmpObj);
                    tmpObj = null;
                }
                cursor.close();
                return jsonArray;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
