[Android] [PATCH 19/24] Add a HistoryFragment and HistoryAdapter

Edward Wang edward.c.wang at compdigitec.com
Wed Aug 22 23:15:19 CEST 2012


---
 .../src/org/videolan/vlc/gui/HistoryAdapter.java   |  161 ++++++++++++++++++++
 .../src/org/videolan/vlc/gui/HistoryFragment.java  |   71 +++++++++
 .../src/org/videolan/vlc/gui/SidebarAdapter.java   |    2 +
 3 files changed, 234 insertions(+), 0 deletions(-)
 create mode 100644 vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
 create mode 100644 vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java

diff --git a/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java b/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
new file mode 100644
index 0000000..c5cb561
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
@@ -0,0 +1,161 @@
+/*****************************************************************************
+ * HistoryAdapter.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.vlc.gui;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.videolan.vlc.EventManager;
+import org.videolan.vlc.LibVLC;
+import org.videolan.vlc.Media;
+import org.videolan.vlc.R;
+import org.videolan.vlc.Util;
+import org.videolan.vlc.VLCApplication;
+import org.videolan.vlc.WeakHandler;
+
+import android.os.Message;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.ImageView;
+import android.widget.TextView;
+
+public class HistoryAdapter extends BaseAdapter {
+    public final static String TAG = "VLC/HistoryAdapter";
+
+    private LayoutInflater mInflater;
+    private List<String> mHistory;
+
+    public HistoryAdapter() {
+        mInflater = LayoutInflater.from(VLCApplication.getAppContext());
+        mHistory = new ArrayList<String>();
+        LibVLC.getExistingInstance().getMediaListItems((ArrayList<String>)mHistory);
+        EventManager em = EventManager.getInstance();
+        em.addHandler(new HistoryEventHandler(this));
+    }
+
+    @Override
+    public int getCount() {
+        return mHistory.size();
+    }
+
+    @Override
+    public Object getItem(int arg0) {
+        return mHistory.get(arg0);
+    }
+
+    @Override
+    public long getItemId(int arg0) {
+        // TODO Auto-generated method stub
+        return 0;
+    }
+
+    @Override
+    public View getView(int position, View convertView, ViewGroup parent) {
+        String selected = mHistory.get(position);
+        DirectoryAdapter.DirectoryViewHolder holder;
+        View v = convertView;
+
+        /* If view not created */
+        if (v == null) {
+            v = mInflater.inflate(R.layout.directory_view_item, parent, false);
+            holder = new DirectoryAdapter.DirectoryViewHolder();
+            holder.layout = v.findViewById(R.id.layout_item);
+            holder.title = (TextView) v.findViewById(R.id.title);
+            holder.text = (TextView) v.findViewById(R.id.text);
+            holder.icon = (ImageView) v.findViewById(R.id.dvi_icon);
+            v.setTag(holder);
+        } else
+            holder = (DirectoryAdapter.DirectoryViewHolder) v.getTag();
+
+        Util.setItemBackground(holder.layout, position);
+
+        String holderText = "";
+        Log.d(TAG, "Loading media position " + position + " - " + selected);
+        Media m = new Media(selected, position);
+        holder.title.setText(m.getTitle());
+        holderText = m.getArtist() + " - " + m.getAlbum();
+
+        holder.text.setText(holderText);
+        holder.icon.setImageResource(R.drawable.icon);
+
+        return v;
+    }
+
+    /**
+     * The media list changed.
+     *
+     * @param added Set to true if the media list was added to
+     * @param uri The URI added/removed
+     * @param index The index added/removed at
+     */
+    public void updateEvent(Boolean added, String uri, int index) {
+        if(added) {
+            Log.v(TAG, "Added index " + index + ": " + uri);
+            mHistory.add(index, uri);
+        } else {
+            Log.v(TAG, "Removed index " + index + ": " + uri);
+            mHistory.remove(index);
+        }
+        notifyDataSetChanged();
+    }
+
+    public List<String> getAllURIs() {
+        return Collections.unmodifiableList(mHistory);
+    }
+
+    public void refresh() {
+        ArrayList<String> s = new ArrayList<String>();
+        LibVLC.getExistingInstance().getMediaListItems(s);
+        mHistory.clear();
+        mHistory = s;
+        this.notifyDataSetChanged();
+    }
+
+    /**
+     *  Handle changes to the media list
+     */
+    private static class HistoryEventHandler extends WeakHandler<HistoryAdapter> {
+        public HistoryEventHandler(HistoryAdapter owner) {
+            super(owner);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            HistoryAdapter adapater = getOwner();
+            if(adapater == null) return;
+
+            String item_uri = msg.getData().getString("item_uri");
+            int item_index = msg.getData().getInt("item_index");
+            switch (msg.getData().getInt("event")) {
+                case EventManager.MediaListItemAdded:
+                    adapater.updateEvent(true, item_uri, item_index);
+                    break;
+                case EventManager.MediaListItemDeleted:
+                    adapater.updateEvent(false, item_uri, item_index);
+                    break;
+            }
+        }
+    };
+}
diff --git a/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java b/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java
new file mode 100644
index 0000000..8232d69
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java
@@ -0,0 +1,71 @@
+/*****************************************************************************
+ * HistoryFragment.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.vlc.gui;
+
+import org.videolan.vlc.AudioServiceController;
+import org.videolan.vlc.R;
+import org.videolan.vlc.gui.audio.AudioPlayerActivity;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ListView;
+
+import com.actionbarsherlock.app.SherlockListFragment;
+
+public class HistoryFragment extends SherlockListFragment {
+    public final static String TAG = "VLC/HistoryFragment";
+
+    private HistoryAdapter mHistoryAdapter;
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        mHistoryAdapter = new HistoryAdapter();
+        Log.d(TAG, "HistoryFragment()");
+    }
+
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
+    {
+        View v = inflater.inflate(R.layout.directory_view, container, false);
+        setListAdapter(mHistoryAdapter);
+        return v;
+    }
+
+    @Override
+    public void onListItemClick(ListView l, View v, int p, long id) {
+        AudioServiceController audioController = AudioServiceController.getInstance();
+
+        audioController.load(mHistoryAdapter.getAllURIs(), p, true);
+        Intent intent = new Intent(getActivity(), AudioPlayerActivity.class);
+        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+        startActivity(intent);
+    }
+
+    public void refresh() {
+        Log.d(TAG, "Refreshing view!");
+        mHistoryAdapter.refresh();
+    }
+}
diff --git a/vlc-android/src/org/videolan/vlc/gui/SidebarAdapter.java b/vlc-android/src/org/videolan/vlc/gui/SidebarAdapter.java
index 0854cfb..c24b3b8 100644
--- a/vlc-android/src/org/videolan/vlc/gui/SidebarAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/SidebarAdapter.java
@@ -128,6 +128,8 @@ public class SidebarAdapter extends BaseAdapter {
             f = new VideoListFragment();
         } else if(id.endsWith("directories")) {
             f = new DirectoryViewFragment();
+        } else if(id.equals("history")) {
+            f = new HistoryFragment();
         } else { /* TODO */
             f = new AboutLicenceFragment();
         }
-- 
1.7.5.4



More information about the Android mailing list