/*
 * ServeStream: A HTTP stream browser/player for Android
 * Copyright 2010 William Seemann
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sourceforge.servestream.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import javax.net.ssl.HttpsURLConnection;

public class PlaylistHandler {

	URL m_targetURL = null;
    ArrayList<String> m_playlistFiles = null;
    
	/**
	 * Default constructor
	 */
	public PlaylistHandler(String targetURL) {

		try {
			
		m_targetURL = new URL(targetURL);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		m_playlistFiles = new ArrayList<String>();
	}
    
	/**
	 * Retrieve the files listed in the m3u file
	 */
    public void buildPlaylist() {
    	
		HttpURLConnection conn = null;
        String line = null;
        BufferedReader reader = null;;
        
        try {
        	
        	if (m_targetURL.getProtocol().equals("http")) {
        		conn = (HttpURLConnection) m_targetURL.openConnection();
        	} else if (m_targetURL.getProtocol().equals("https")) {
        		conn = (HttpsURLConnection) m_targetURL.openConnection();        		
        	}
        	
		    //conn = (HttpURLConnection) m_targetURL.openConnection();
		    conn.setRequestMethod("GET");
		    
		    // Start the query
		    reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		    conn.connect();
        
		    while ((line = reader.readLine()) != null) {
		    	if (!(line.contains("#EXTM3U") || line.contains("#EXTINF"))) {
		    		m_playlistFiles.add(line);
		    	}
                
            }

        } catch (Exception ex) {
        	ex.printStackTrace();
        } finally {
        	closeReader(reader);
        	closeHttpConnection(conn);
        }
    }
    
    /**
     * 
     */
    public ArrayList<String> getPlayListFiles() {
    	return m_playlistFiles;
    }

	/**
	 * Closes a BufferedReader
	 * 
	 * @param reader The reader to close
	 */
    private void closeReader(BufferedReader reader) {
    	
    	if (reader == null)
    		return;

    	try {
    	    reader.close();
    	} catch (IOException ex) {
    		
    	}
    }
    
	/**
	 * Closes a HttpURLConnection
	 * 
	 * @param conn The connection to close
	 */
    private void closeHttpConnection(HttpURLConnection conn) {
    	
    	if (conn == null)
    		return;
    	
    	conn.disconnect();
    }
	
}


@@ -952,14 +952,20 @@
 %7B%0A%0A%09
+private 
 URL 
-m_
 targ
@@ -982,16 +982,24 @@
 ll;%0A    
+private 
 ArrayLis
@@ -1008,18 +1008,16 @@
 String%3E 
-m_
 playlist
@@ -1124,22 +1124,25 @@
 try %7B%0A%09%09
-%09%0A%09%09m_
+    this.
 targetUR
@@ -1226,18 +1226,16 @@
 %09%7D%0A%09%09%0A%09%09
-m_
 playlist
@@ -1292,16 +1292,17 @@
 Retrieve
+s
  the fil
@@ -1314,20 +1314,19 @@
 sted in 
-the 
+a .
 m3u file
@@ -1513,26 +1513,24 @@
        %09if (
-m_
 targetURL.ge
@@ -1588,34 +1588,32 @@
 pURLConnection) 
-m_
 targetURL.openCo
@@ -1644,18 +1644,16 @@
 lse if (
-m_
 targetUR
@@ -1717,34 +1717,32 @@
 sURLConnection) 
-m_
 targetURL.openCo
@@ -1788,73 +1788,8 @@
   %09%0A
-%09%09    //conn = (HttpURLConnection) m_targetURL.openConnection();%0A
 %09%09  
@@ -2093,18 +2093,16 @@
 %09%09    %09%09
-m_
 playlist
@@ -2126,22 +2126,16 @@
 %09%09    %09%7D
-%0A     
         
@@ -2339,16 +2339,138 @@
 %0A     * 
+Returns the files in the playlist%0A     * %0A     * @return ArrayList%3CString%3E An array containing the files from the playlist
 %0A     */
@@ -2536,10 +2536,8 @@
 urn 
-m_
 play
@@ -3056,13 +3056,11 @@
 ;%0A    %7D%0A
-%09%0A
 %7D%0A%0A

Cleaned up code