content: package me.lancer.airfree.util;
  
  import java.io.FileDescriptor;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
  
  import android.content.ContentResolver;
  import android.content.ContentUris;
  import android.content.Context;
  import android.database.Cursor;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.BitmapFactory.Options;
  import android.net.Uri;
  import android.os.ParcelFileDescriptor;
  import android.provider.MediaStore;
  
  import me.lancer.airfree.bean.MusicBean;
  import me.lancer.distance.R;
  
  public class MusicUtil {
      
      private static final Uri albumArtUri = Uri.parse("content://media/external/audio/albumart");
  
      public static List<MusicBean> getMusicInfos(Context context) {
          Cursor cursor = context.getContentResolver().query(
                  MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                  MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
          List<MusicBean> musicList = new ArrayList<MusicBean>();
          for (int i = 0; i < cursor.getCount(); i++) {
              cursor.moveToNext();
              MusicBean music = new MusicBean();
              long id = cursor.getLong(cursor
                      .getColumnIndex(MediaStore.Audio.Media._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));
              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 path = cursor.getString(cursor
                      .getColumnIndex(MediaStore.Audio.Media.DATA));
              int isMusic = cursor.getInt(cursor
                      .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
              if (isMusic != 0) {
                  music.setId(id);
                  music.setTitle(title);
                  music.setArtist(artist);
                  music.setAlbum(album);
                  music.setAlbumId(albumId);
                  music.setDuration(duration);
                  music.setSize(size);
                  music.setPath(path);
                  musicList.add(music);
              }
          }
          return musicList;
      }
  
      public static List<HashMap<String, String>> getMusicMaps(
              List<MusicBean> musicList) {
          List<HashMap<String, String>> mp3list = new ArrayList<HashMap<String, String>>();
          for (Iterator iterator = musicList.iterator(); iterator.hasNext(); ) {
              MusicBean music = (MusicBean) iterator.next();
              HashMap<String, String> map = new HashMap<String, String>();
              map.put("title", music.getTitle());
              map.put("Artist", music.getArtist());
              map.put("album", music.getAlbum());
              map.put("albumId", String.valueOf(music.getAlbumId()));
              map.put("duration", formatTime(music.getDuration()));
              map.put("size", String.valueOf(music.getSize()));
              map.put("url", music.getPath());
              mp3list.add(map);
          }
          return mp3list;
      }
  
      public static String formatTime(long time) {
          String min = time / (1000 * 60) + "";
          String sec = time % (1000 * 60) + "";
          if (min.length() < 2) {
              min = "0" + time / (1000 * 60) + "";
          } else {
              min = time / (1000 * 60) + "";
          }
          if (sec.length() == 4) {
              sec = "0" + (time % (1000 * 60)) + "";
          } else if (sec.length() == 3) {
              sec = "00" + (time % (1000 * 60)) + "";
          } else if (sec.length() == 2) {
              sec = "000" + (time % (1000 * 60)) + "";
          } else if (sec.length() == 1) {
              sec = "0000" + (time % (1000 * 60)) + "";
          }
          return min + ":" + sec.trim().substring(0, 2);
      }
  
      public static Bitmap getDefaultArtwork(Context context, boolean small) {
          BitmapFactory.Options opts = new BitmapFactory.Options();
          opts.inPreferredConfig = Bitmap.Config.RGB_565;
          if (small) {
              return BitmapFactory.decodeStream(context.getResources().openRawResource(R.raw.ic_fm_icon_music), null, opts);
          }
          return BitmapFactory.decodeStream(context.getResources().openRawResource(R.raw.ic_fm_icon_music), null, opts);
      }
  
      private static Bitmap getArtworkFromFile(Context context, long songid, long albumid) {
          Bitmap bm = null;
          if (albumid < 0 && songid < 0) {
              throw new IllegalArgumentException("Must specify an album or a song id");
          }
          try {
              BitmapFactory.Options options = new BitmapFactory.Options();
              FileDescriptor fd = null;
              if (albumid < 0) {
                  Uri uri = Uri.parse("content://media/external/audio/media/"
                          + songid + "/albumart");
                  ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
                  if (pfd != null) {
                      fd = pfd.getFileDescriptor();
                  }
              } else {
                  Uri uri = ContentUris.withAppendedId(albumArtUri, albumid);
                  ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
                  if (pfd != null) {
                      fd = pfd.getFileDescriptor();
                  }
              }
              options.inSampleSize = 1;
              options.inJustDecodeBounds = true;
              BitmapFactory.decodeFileDescriptor(fd, null, options);
              options.inSampleSize = 100;
              options.inJustDecodeBounds = false;
              options.inDither = false;
              options.inPreferredConfig = Bitmap.Config.ARGB_8888;
              bm = BitmapFactory.decodeFileDescriptor(fd, null, options);
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          }
          return bm;
      }
  
      public static Bitmap getArtwork(Context context, long songid, long albumid, boolean allowdefalut, boolean small) {
          if (albumid < 0) {
              if (songid < 0) {
                  Bitmap bm = getArtworkFromFile(context, songid, -1);
                  if (bm != null) {
                      return bm;
                  }
              }
              if (allowdefalut) {
                  return getDefaultArtwork(context, small);
              }
              return null;
          }
          ContentResolver res = context.getContentResolver();
          Uri uri = ContentUris.withAppendedId(albumArtUri, albumid);
          if (uri != null) {
              InputStream in = null;
              try {
                  in = res.openInputStream(uri);
                  BitmapFactory.Options options = new BitmapFactory.Options();
                  options.inSampleSize = 1;
                  options.inJustDecodeBounds = true;
                  BitmapFactory.decodeStream(in, null, options);
                  if (small) {
                      options.inSampleSize = computeSampleSize(options, 40);
                  } else {
                      options.inSampleSize = computeSampleSize(options, 600);
                  }
                  options.inJustDecodeBounds = false;
                  options.inDither = false;
                  options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                  in = res.openInputStream(uri);
                  return BitmapFactory.decodeStream(in, null, options);
              } catch (FileNotFoundException e) {
                  Bitmap bm = getArtworkFromFile(context, songid, albumid);
                  if (bm != null) {
                      if (bm.getConfig() == null) {
                          bm = bm.copy(Bitmap.Config.RGB_565, false);
                          if (bm == null && allowdefalut) {
                              return getDefaultArtwork(context, small);
                          }
                      }
                  } else if (allowdefalut) {
                      bm = getDefaultArtwork(context, small);
                  }
                  return bm;
              } finally {
                  try {
                      if (in != null) {
                          in.close();
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
          return null;
      }
  
      public static int computeSampleSize(Options options, int target) {
          int w = options.outWidth;
          int h = options.outHeight;
          int candidateW = w / target;
          int candidateH = h / target;
          int candidate = Math.max(candidateW, candidateH);
          if (candidate == 0) {
              return 1;
          }
          if (candidate > 1) {
              if ((w > target) && (w / candidate) < target) {
                  candidate -= 1;
              }
          }
          if (candidate > 1) {
              if ((h > target) && (h / candidate) < target) {
                  candidate -= 1;
              }
          }
          return candidate;
      }
  }  