org.videolan.myvlc.core.gui.NewSidebarAdapter.java Source code

Java tutorial

Introduction

Here is the source code for org.videolan.myvlc.core.gui.NewSidebarAdapter.java

Source

/*****************************************************************************
 * SidebarAdapter.java
 *****************************************************************************
 * Copyright  2012 VLC authors and VideoLAN
 * Copyright  2012 Edward Wang
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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.myvlc.core.gui;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Semaphore;

import org.videolan.myvlc.MyVLCApp;
import org.videolan.myvlc.R;
import org.videolan.myvlc.core.gui.about.AboutLicenceFragment;
import org.videolan.myvlc.core.gui.dirctory.DirectoryViewFragment;
import org.videolan.myvlc.core.gui.history.HistoryFragment;
import org.videolan.myvlc.tool.Util;

import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class NewSidebarAdapter extends BaseAdapter {
    public final static String TAG = "VLC/SidebarAdapter";

    public static class SidebarEntry {
        public String id;
        public String name;
        public int drawableID;

        public SidebarEntry(String _id, int _name, int _drawableID) {
            this.id = _id;
            this.name = MyVLCApp.getAppContext().getString(_name);
            this.drawableID = _drawableID;
        }
    }

    private LayoutInflater mInflater;
    public static final List<SidebarEntry> lstEntrie;
    private HashMap<String, Fragment> mMapFragment;
    private HashMap<String, Boolean> mMapFragmentAdded;
    private Semaphore mSemaphore;

    static {
        SidebarEntry entries2[] = { new SidebarEntry("video", R.string.video, R.drawable.header_icon_video),
                new SidebarEntry("audio", R.string.audio, R.drawable.header_icon_audio),
                new SidebarEntry("directories", R.string.directories, R.drawable.ic_folder),
                new SidebarEntry("history", R.string.history, android.R.drawable.ic_menu_recent_history),
                new SidebarEntry("about", R.string.about, R.drawable.ic_folder) };
        lstEntrie = Arrays.asList(entries2);
    }

    public NewSidebarAdapter() {
        mInflater = LayoutInflater.from(MyVLCApp.getAppContext());
        mMapFragment = new HashMap<String, Fragment>(lstEntrie.size());
        mMapFragmentAdded = new HashMap<String, Boolean>(lstEntrie.size());
        mSemaphore = new Semaphore(1, true);
    }

    @Override
    public int getCount() {
        return lstEntrie.size();
    }

    @Override
    public Object getItem(int position) {
        return lstEntrie.get(position);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {
        SidebarEntry sidebarEntry = lstEntrie.get(position);

        /* If view not created */
        if (v == null) {
            v = mInflater.inflate(R.layout.sidebar_item, parent, false);
        }
        TextView textView = (TextView) v;
        textView.setText(sidebarEntry.name);
        Drawable img = MyVLCApp.getAppResources().getDrawable(sidebarEntry.drawableID);
        if (img != null) {
            int dp_32 = Util.convertDpToPx(32);
            img.setBounds(0, 0, dp_32, dp_32);
            textView.setCompoundDrawables(img, null, null, null);
        }

        return v;
    }

    public Fragment fetchFragment(String id) {
        if (mMapFragment.containsKey(id) && mMapFragment.get(id) != null) {
            return mMapFragment.get(id);
        }
        Fragment fragment = null;
        if (id.equals("audio")) {
            //f = new AudioBrowserFragment();
        } else if (id.equals("video")) {
            //f = new VideoGridFragment();
        } else if (id.endsWith("directories")) {
            fragment = new DirectoryViewFragment();
        } else if (id.equals("history")) {
            fragment = new HistoryFragment();
        } else { /* TODO */
            fragment = new AboutLicenceFragment();
        }
        fragment.setRetainInstance(true);
        mMapFragment.put(id, fragment);
        mMapFragmentAdded.put(id, false);
        return fragment;
    }

    public boolean isFragmentAdded(String id) {
        return mMapFragmentAdded.get(id);
    }

    public void setFragmentAdded(String id) {
        mMapFragmentAdded.put(id, true);
    }

    public void lockSemaphore() {
        mSemaphore.acquireUninterruptibly();
    }

    public void unlockSemaphore() {
        mSemaphore.release();
    }

    public void restoreFragment(String id, Fragment f) {
        if (f == null) {
            Log.e(TAG, "Can't set null fragment for " + id + "!");
            return;
        }
        mMapFragment.put(id, f);
        mMapFragmentAdded.put(id, true);
    }
}