Directoris.java 3.77 KB
package com.upc.pbe.upcnews;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class Directoris extends Activity implements OnItemClickListener
{
	final static String TAG = "Directoris";
	private Descarrega d = new Descarrega();
	private String URL;
	private String currentFolder;
	private HTMLParser parser;

	public void onCreate(Bundle savedInstanceState)
	{
		Log.d(TAG, "onCreated");

		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dirs);

		currentFolder = ((UpcApp) getApplication()).getUrl() + "/";
		URL = currentFolder;
		parser = new HTMLParser(URL);
		
		((ListView)findViewById(R.id.listView1)).setOnItemClickListener(this);
		
		this.showResources();
	}

	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.menu, menu);
		Log.d(TAG, "Menu");
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.itemprefs:
			startActivity(new Intent(this, Prefs.class));
			Log.d(TAG, "Preferences");
			return true;
		case R.id.itemhelp:
			startActivity(new Intent(this, Help.class));
			Log.d(TAG, "Help");
			return true;
		default:
			return false;
		}
	}
	
	public void showResources()
	{
		ArrayList<String> entries = parser.parse(d.doInBackground(currentFolder));
		this.createEntries(entries);
	}
	
	public void createEntries(ArrayList<String> directories)
	{
		String[] entries = directories.toArray(new String[directories.size()]);
		ListView listView = (ListView) findViewById(R.id.listView1);
		listView.setAdapter(null);
		ResourceAdapter adapter = new ResourceAdapter(this, R.layout.rowlayout, entries);
		listView.setAdapter(adapter);
	}
	
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
	{
		String path = ((TextView)view).getText().toString();
		if(path.endsWith(".m3u8"))
		{
			path = currentFolder + path;
			String playlist =  d.doInBackground(path);
			Parser p = new Parser(path.substring(0, path.lastIndexOf("/") + 1), this);
			try
			{
				ArrayList<ParentList> m3u8parsed = p.parseFile(playlist);
				Log.d(TAG, "parsing completed");
				HLS h = new HLS(m3u8parsed,((UpcApp)getApplication()).getLocalPath());
				h.start();
			}
			catch (ErrorException e)
			{
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
				Log.d(TAG, e.getMessage());
			}
			catch (WarningException e)
			{
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
				Log.d(TAG, e.getMessage());
			}
			catch (InfoException e)
			{
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
				Log.d(TAG, e.getMessage());
			}

		}
		else //Se trata de una carpeta
		{	
			if(!path.startsWith("http://"))
			{
				currentFolder += path;
			}
			else
			{
				currentFolder = path;
			}
			showResources();
		}
	} 
	

	
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
	    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
	    {
	    	if(currentFolder.equals(URL))
	    	{
	    		super.finish();
		        return true;
	    	}
	    	currentFolder = currentFolder.substring(0, currentFolder.length()-1);
	        currentFolder = currentFolder.substring(0,currentFolder.lastIndexOf("/")+1);
	        showResources();
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
}