Android Open Source - Billy Media List






From Project

Back to project page Billy.

License

The source code is released under:

GNU General Public License

If you think the Android project Billy 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

/*****************************************************************************
 * MediaList.java//w w  w  .  j av  a 2s . com
 *****************************************************************************
 * Copyright  2013 VLC authors and VideoLAN
 * Copyright  2013 Edward Wang
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
package org.videolan.libvlc;

import java.util.ArrayList;

import android.os.Bundle;

/**
 * Java/JNI wrapper for the libvlc_media_list_t structure.
 */
public class MediaList {
    private static final String TAG = "VLC/LibVLC/MediaList";

    /* Since the libvlc_media_t is not created until the media plays, we have
     * to cache them here. */
    private static class MediaHolder {
        Media m;
        boolean noVideo; // default false
        boolean noHardwareAcceleration; // default false

        public MediaHolder(Media media) {
            m = media; noVideo = false; noHardwareAcceleration = false;
        }
        public MediaHolder(Media m_, boolean noVideo_, boolean noHardwareAcceleration_) {
            m = m_; noVideo = noVideo_; noHardwareAcceleration = noHardwareAcceleration_;
        }
    }

    /* TODO: add locking */
    private ArrayList<MediaHolder> mInternalList;
    private LibVLC mLibVLC; // Used to create new objects that require a libvlc instance
    private EventHandler mEventHandler;

    public MediaList(LibVLC libVLC) {
        mEventHandler = new EventHandler(); // used in init() below to fire events at the correct targets
        mInternalList = new ArrayList<MediaHolder>();
        mLibVLC = libVLC;
    }

    /**
     * Adds a media URI to the media list.
     *
     * @param mrl
     *            The MRL to add. Must be a location and not a path.
     *            {@link LibVLC#PathToURI(String)} can be used to convert a path
     *            to a MRL.
     */
    public void add(String mrl) {
        add(new Media(mLibVLC, mrl));
    }
    public void add(Media media) {
        add(media, false, false);
    }
    public void add(Media media, boolean noVideo) {
        add(media, noVideo, false);
    }
    public void add(Media media, boolean noVideo, boolean noHardwareAcceleration) {
        mInternalList.add(new MediaHolder(media, noVideo, noHardwareAcceleration));
        signal_list_event(EventHandler.CustomMediaListItemAdded, mInternalList.size() - 1, media.getLocation());
    }

    /**
     * Clear the media list. (remove all media)
     */
    public void clear() {
        // Signal to observers of media being deleted.
        for(int i = 0; i < mInternalList.size(); i++) {
            signal_list_event(EventHandler.CustomMediaListItemDeleted, i, mInternalList.get(i).m.getLocation());
        }
        mInternalList.clear();
    }

    private boolean isValid(int position) {
        return position >= 0 && position < mInternalList.size();
    }

    /**
     * This function checks the currently playing media for subitems at the given
     * position, and if any exist, it will expand them at the same position
     * and replace the current media.
     *
     * @param position The position to expand
     * @return -1 if no subitems were found, 0 if subitems were expanded
     */
    public int expandMedia(int position) {
        ArrayList<String> children = new ArrayList<String>();
        int ret = expandMedia(mLibVLC, position, children);
        if(ret == 0) {
            mEventHandler.callback(EventHandler.CustomMediaListExpanding, new Bundle());
            this.remove(position);
            for(String mrl : children) {
                this.insert(position, mrl);
            }
            mEventHandler.callback(EventHandler.CustomMediaListExpandingEnd, new Bundle());
        }
        return ret;
    }
    private native int expandMedia(LibVLC libvlc_instance, int position, ArrayList<String> children);

    public void loadPlaylist(String mrl) {
        ArrayList<String> items = new ArrayList<String>();
        loadPlaylist(mLibVLC, mrl, items);
        this.clear();
        for(String item : items) {
            this.add(item);
        }
    }
    private native void loadPlaylist(LibVLC libvlc_instance, String mrl, ArrayList<String> items);

    public void insert(int position, String mrl) {
        insert(position, new Media(mLibVLC, mrl));
    }
    public void insert(int position, Media media) {
        mInternalList.add(position, new MediaHolder(media));
        signal_list_event(EventHandler.CustomMediaListItemAdded, position, media.getLocation());
    }

    /**
     * Move a media from one position to another
     *
     * @param startPosition start position
     * @param endPosition end position
     * @throws IndexOutOfBoundsException
     */
    public void move(int startPosition, int endPosition) {
        if (!(isValid(startPosition)
              && endPosition >= 0 && endPosition <= mInternalList.size()))
            throw new IndexOutOfBoundsException("Indexes out of range");

        MediaHolder toMove = mInternalList.get(startPosition);
        mInternalList.remove(startPosition);
        if (startPosition >= endPosition)
            mInternalList.add(endPosition, toMove);
        else
            mInternalList.add(endPosition - 1, toMove);
        Bundle b = new Bundle();
        b.putInt("index_before", startPosition);
        b.putInt("index_after", endPosition);
        mEventHandler.callback(EventHandler.CustomMediaListItemMoved, b);
    }

    public void remove(int position) {
        if (!isValid(position))
            return;
        String uri = mInternalList.get(position).m.getLocation();
        mInternalList.remove(position);
        signal_list_event(EventHandler.CustomMediaListItemDeleted, position, uri);
    }

    public void remove(String location) {
        for (int i = 0; i < mInternalList.size(); ++i) {
            String uri = mInternalList.get(i).m.getLocation();
            if (uri.equals(location)) {
                mInternalList.remove(i);
                signal_list_event(EventHandler.CustomMediaListItemDeleted, i, uri);
                i--;
            }
        }
    }

    public int size() {
        return mInternalList.size();
    }

    public Media getMedia(int position) {
        if (!isValid(position))
            return null;
        return mInternalList.get(position).m;
    }

    /**
     * @param position The index of the media in the list
     * @return null if not found
     */
    public String getMRL(int position) {
        if (!isValid(position))
            return null;
        return mInternalList.get(position).m.getLocation();
    }

    public String[] getMediaOptions(int position) {
        boolean noHardwareAcceleration = false;
        boolean noVideo = false;
        if (isValid(position))
        {
            noHardwareAcceleration = mInternalList.get(position).noHardwareAcceleration;
            noVideo = mInternalList.get(position).noVideo;
        }

        return mLibVLC.getMediaOptions(noHardwareAcceleration, noVideo);
    }

    public EventHandler getEventHandler() {
        return mEventHandler;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("LibVLC Media List: {");
        for(int i = 0; i < size(); i++) {
            sb.append(((Integer)i).toString());
            sb.append(": ");
            sb.append(getMRL(i));
            sb.append(", ");
        }
        sb.append("}");
        return sb.toString();
    }

    private void signal_list_event(int event, int position, String uri) {
        Bundle b = new Bundle();
        b.putString("item_uri", uri);
        b.putInt("item_index", position);
        mEventHandler.callback(event, b);
    }
}




Java Source Code List

com.vibin.billy.BillyApplication.java
com.vibin.billy.BillyItem.java
com.vibin.billy.BitmapLruCache.java
com.vibin.billy.ChangelogDialog.java
com.vibin.billy.CustomBaseAdapter.java
com.vibin.billy.CustomDatabaseAdapter.java
com.vibin.billy.CustomFragmentAdapter.java
com.vibin.billy.CustomListPreference.java
com.vibin.billy.CustomShareActionProvider.java
com.vibin.billy.CustomStringRequest.java
com.vibin.billy.DetailView.java
com.vibin.billy.LicensesFragment.java
com.vibin.billy.MainActivity.java
com.vibin.billy.MediaControl.java
com.vibin.billy.NotifyingScrollView.java
com.vibin.billy.PPlayerService.java
com.vibin.billy.PlayerService.java
com.vibin.billy.ProcessingTask.java
com.vibin.billy.ReorderedListPreference.java
com.vibin.billy.Settings.java
com.vibin.billy.SongsFragment.java
com.vibin.billy.SwingBottomInAnimationAdapter.java
com.vibin.billy.draglistview.DynamicListView.java
com.vibin.billy.draglistview.StableArrayAdapter.java
com.vibin.billy.swipeable.ActivitySwipeDismissListener.java
com.vibin.billy.swipeable.AnimationUtils.java
com.vibin.billy.swipeable.SwipeDismissViewGroup.java
com.vibin.billy.swipeable.SwipeListener.java
com.vibin.billy.swipeable.SwipeableActivity.java
com.vibin.billy.swipeable.WindowDimens.java
com.vibin.billy.swipeable.WindowUtils.java
org.videolan.libvlc.AudioOutput.java
org.videolan.libvlc.EventHandler.java
org.videolan.libvlc.HWDecoderUtil.java
org.videolan.libvlc.IVideoPlayer.java
org.videolan.libvlc.LibVLC.java
org.videolan.libvlc.LibVlcException.java
org.videolan.libvlc.LibVlcUtil.java
org.videolan.libvlc.MediaList.java
org.videolan.libvlc.Media.java
org.videolan.libvlc.TrackInfo.java