@Override
  public boolean onContextItemSelected(MenuItem item) {
    if (!getUserVisibleHint()) return super.onContextItemSelected(item);

    AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
    if (menuInfo == null) // getMenuInfo can be NULL
    return super.onContextItemSelected(item);

    int startPosition;
    List<String> medias;
    int id = item.getItemId();

    boolean useAllItems =
        (id == R.id.audio_list_browser_play_all || id == R.id.audio_list_browser_append_all);
    boolean append =
        (id == R.id.audio_list_browser_append || id == R.id.audio_list_browser_append_all);

    if (id == R.id.audio_list_browser_delete) {
      final Media media = mSongsAdapter.getItem(menuInfo.position);
      AlertDialog dialog =
          CommonDialogs.deleteMedia(
              getActivity(),
              media.getLocation(),
              new VlcRunnable(media) {
                @Override
                public void run(Object o) {
                  mMediaLibrary.getMediaItems().remove(media);
                  updateList();
                }
              });
      dialog.show();
      return true;
    }

    if (id == R.id.audio_list_browser_set_song) {
      AudioUtil.setRingtone(mSongsAdapter.getItem(menuInfo.position), getActivity());
      return true;
    }

    if (useAllItems) {
      startPosition = menuInfo.position;
      medias = mSongsAdapter.getLocations();
    } else {
      startPosition = 0;
      medias = mSongsAdapter.getLocation(menuInfo.position);
    }
    if (append) mAudioController.append(medias);
    else {
      mAudioController.load(medias, startPosition);
      AudioPlayerFragment.start(getActivity());
    }

    return super.onContextItemSelected(item);
  }
  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    if (getListAdapter() == mHistoryAdapter) {
      String selection = ((TextView) v.findViewById(android.R.id.text1)).getText().toString();
      mSearchText.setText(selection);
      mSearchText.setSelection(selection.length());
      mSearchText.requestFocus();
    } else if (getListAdapter() == mResultAdapter) {
      // add search text to the database (history)
      MediaDatabase db = MediaDatabase.getInstance(this);
      db.addSearchhistoryItem(mSearchText.getText().toString());

      // open media in the player
      Media item = (Media) getListAdapter().getItem(position - 1);
      if (item.getType() == Media.TYPE_VIDEO) {
        VideoPlayerActivity.start(this, item.getLocation());
      } else {
        ArrayList<String> arr = new ArrayList<String>();
        for (int i = 0; i < getListAdapter().getCount(); i++) {
          Media audioItem = (Media) getListAdapter().getItem(i);
          if (audioItem.getType() == Media.TYPE_AUDIO) arr.add(audioItem.getLocation());
        }
        AudioServiceController.getInstance().load(arr, arr.indexOf(item.getLocation()));
        AudioPlayerActivity.start(this);
      }
      super.onListItemClick(l, v, position, id);
    }
  };
  private void updateList() {
    final Bundle b = getArguments();
    String name = b.getString(EXTRA_NAME);
    String name2 = b.getString(EXTRA_NAME2);
    int mode = b.getInt(EXTRA_MODE, 0);

    List<Media> audioList;
    List<String> itemList;
    String currentItem = null;
    int currentIndex = -1;

    if (name == null || mode == AudioBrowserFragment.MODE_SONG) {
      mTitle.setText(R.string.songs);
      itemList = mAudioController.getItems();
      currentItem = mAudioController.getItem();
      audioList = MediaLibrary.getInstance(getActivity()).getMediaItems(itemList);
    } else {
      mTitle.setText(name2 != null ? name2 : name);
      audioList = MediaLibrary.getInstance(getActivity()).getAudioItems(name, name2, mode);
    }

    mSongsAdapter.clear();
    switch (mSortBy) {
      case SORT_BY_LENGTH:
        Collections.sort(audioList, byLength);
        break;
      case SORT_BY_TITLE:
      default:
        Collections.sort(audioList, byMRL);
        break;
    }
    if (mSortReverse) {
      Collections.reverse(audioList);
    }

    for (int i = 0; i < audioList.size(); i++) {
      Media media = audioList.get(i);
      if (currentItem != null && currentItem.equals(media.getLocation())) currentIndex = i;
      mSongsAdapter.add(media);
    }
    mSongsAdapter.setCurrentIndex(currentIndex);
    getListView().setSelection(currentIndex);

    mSongsAdapter.notifyDataSetChanged();
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mAudioController = AudioServiceController.getInstance();

    mMediaLibrary = MediaLibrary.getInstance(getActivity());

    mSongsAdapter = new AudioListAdapter(getActivity());
    setListAdapter(mSongsAdapter);
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAlbumsAdapter =
        new AudioBrowserListAdapter(getActivity(), AudioBrowserListAdapter.ITEM_WITH_COVER);
    mSongsAdapter =
        new AudioBrowserListAdapter(getActivity(), AudioBrowserListAdapter.ITEM_WITH_COVER);

    mAlbumsAdapter.setContextPopupMenuListener(mContextPopupMenuListener);
    mSongsAdapter.setContextPopupMenuListener(mContextPopupMenuListener);

    mAudioController = AudioServiceController.getInstance();
    mMediaLibrary = MediaLibrary.getInstance(getActivity());
  }
 @Override
 protected void onPause() {
   super.onPause();
   AudioServiceController.getInstance().unbindAudioService(this);
 }
  public void updateMedia() {
    AudioServiceController audioController = AudioServiceController.getInstance();
    if (audioController == null) return;

    removeAllViews();

    hasPrevious = false;
    previousPosition = 0;

    LayoutInflater inflater =
        (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (audioController.hasPrevious()) {
      addMediaView(
          inflater,
          audioController.getTitlePrev(),
          audioController.getArtistPrev(),
          audioController.getCoverPrev());
      hasPrevious = true;
    }
    if (audioController.hasMedia())
      addMediaView(
          inflater,
          audioController.getTitle(),
          audioController.getArtist(),
          audioController.getCover());
    if (audioController.hasNext())
      addMediaView(
          inflater,
          audioController.getTitleNext(),
          audioController.getArtistNext(),
          audioController.getCoverNext());

    if (audioController.hasPrevious() && audioController.hasMedia()) {
      previousPosition = 1;
      scrollTo(1);
    } else scrollTo(0);
  }
 @Override
 public void onListItemClick(ListView l, View v, int position, long id) {
   mAudioController.load(mSongsAdapter.getLocations(), position);
   AudioPlayerFragment.start(getActivity());
   super.onListItemClick(l, v, position, id);
 }
 @Override
 public void onItemClick(AdapterView<?> av, View v, int p, long id) {
   ArrayList<String> mediaLocation = mSongsAdapter.getLocations(p);
   mAudioController.load(mediaLocation, 0);
 }
  private boolean handleContextItemSelected(MenuItem item, int position) {
    ContextMenuInfo menuInfo = item.getMenuInfo();

    int startPosition;
    int groupPosition;
    List<String> medias;
    int id = item.getItemId();

    boolean useAllItems = id == R.id.audio_list_browser_play_all;
    boolean append = id == R.id.audio_list_browser_append;

    if (ExpandableListContextMenuInfo.class.isInstance(menuInfo)) {
      ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo;
      groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
    } else groupPosition = position;

    if (id == R.id.audio_list_browser_delete) {
      AlertDialog alertDialog =
          CommonDialogs.deleteMedia(
              getActivity(),
              mSongsAdapter.getLocations(groupPosition).get(0),
              new VlcRunnable(mSongsAdapter.getItem(groupPosition)) {
                @Override
                public void run(Object o) {
                  AudioBrowserListAdapter.ListItem listItem = (AudioBrowserListAdapter.ListItem) o;
                  Media media = listItem.mMediaList.get(0);
                  mMediaLibrary.getMediaItems().remove(media);
                  mSongsAdapter.removeMedia(media);
                  mAlbumsAdapter.removeMedia(media);
                  mAudioController.removeLocation(media.getLocation());
                }
              });
      alertDialog.show();
      return true;
    }

    if (id == R.id.audio_list_browser_set_song) {
      AudioUtil.setRingtone(mSongsAdapter.getItem(groupPosition).mMediaList.get(0), getActivity());
      return true;
    }

    if (useAllItems) {
      medias = new ArrayList<String>();
      startPosition = mSongsAdapter.getListWithPosition(medias, groupPosition);
    } else {
      startPosition = 0;
      switch (mTabHost.getCurrentTab()) {
        case 0: // albums
          medias = mAlbumsAdapter.getLocations(groupPosition);
          break;
        case 1: // songs
          medias = mSongsAdapter.getLocations(groupPosition);
          break;
        default:
          return true;
      }
    }

    if (append) mAudioController.append(medias);
    else mAudioController.load(medias, startPosition);

    return super.onContextItemSelected(item);
  }
 @Override
 protected void onResume() {
   AudioServiceController.getInstance().bindAudioService(this);
   super.onResume();
 }