Directoris.java 3.5 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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Directoris extends Activity implements OnClickListener {

	final static String TAG = "Directoris";
	String s;
	Descarrega d;
	String url;
	String file = "ejemplo.xml";
	String folder = "Environment.getExternalStorageDirectory.getPath()";

	ArrayList<Button> buttons = new ArrayList<Button>();

	public void onCreate(Bundle savedInstanceState) {

		Log.d(TAG, "onCreated");
		url = ((UpcApp) getApplication()).getUrl();

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

		this.crearButtons();

		s = ((UpcApp) getApplication()).getDesc();
		HTMLParser pars = new HTMLParser();
		ArrayList<String> dirs = pars.parse(s);

		for (int i = 0; i < dirs.size(); i++) {
			Log.d(TAG, "Directori " + i);
			buttons.get(i).setVisibility(View.VISIBLE);
			buttons.get(i).setText(dirs.get(i));

		}

	}

	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 onClick(View c) {

		switch (c.getId()) {

		case R.id.button1: {
			Log.d(TAG, "Click on button1");
			d = new Descarrega(url + "/" + buttons.get(0).getText() + "/",
					file, folder);
			String str = d.doInBackground();

			HTMLParser pars = new HTMLParser();
			String urlvideo = pars.findvideo(str, url + "/"
					+ buttons.get(0).getText() + "/");

			Log.d(TAG, urlvideo);

			if (urlvideo == "No s'ha trobat") {
				Toast.makeText(this, "El directori no conté cap .m3u8",
						Toast.LENGTH_LONG).show();
			} else {
				d = new Descarrega(urlvideo, file, folder);
				String m3u8 = d.doInBackground();
			}
			// m3u8 --> PARSE!
			break;

		}
		case R.id.button2: {
			Log.d(TAG, "Click on button2");
			d = new Descarrega(url + "/" + buttons.get(1).getText() + "/",
					file, folder);
			String str = d.doInBackground();

			HTMLParser pars = new HTMLParser();
			String urlvideo = pars.findvideo(str, url + "/"
					+ buttons.get(1).getText() + "/");

			Log.d(TAG, urlvideo);

			if (urlvideo == "No s'ha trobat") {
				Toast.makeText(this, "El directori no conté cap .m3u8",
						Toast.LENGTH_LONG).show();
			} else {

				d = new Descarrega(urlvideo, file, folder);
				String m3u8 = d.doInBackground();
			}
			// m3u8 --> PARSE!
			break;
		}
		case R.id.button3: {

		}
		case R.id.button4: {

		}
		}
	}

	public void crearButtons() {

		buttons.add((Button) findViewById(R.id.button1));
		buttons.add((Button) findViewById(R.id.button2));
		buttons.add((Button) findViewById(R.id.button3));
		buttons.add((Button) findViewById(R.id.button4));
		buttons.add((Button) findViewById(R.id.button5));

		for (int i = 0; i < buttons.size(); i++) {
			buttons.get(i).setOnClickListener(this);
			buttons.get(i).setVisibility(View.GONE);
		}
	}
}