MainActivity.java 3.61 KB
package com.upc.pbe.upcnews;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
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.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

//Finestra incial i activity principal del programa
public class MainActivity extends Activity implements OnClickListener
{
	
	private final static String TAG = "Main";
	private static String html;
	private ImageView alerta;
	private ImageButton buttonDescarrega;
	private TextView URLText, alertaText;
	private boolean libsFound = false;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		//Creem el layout
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		Log.d(TAG, "onCreated");
		//Especifiquem la ruta de descarrega
		File tempFolder = new File(((UpcApp)getApplication()).getLocalPath());
		if(tempFolder.mkdirs()) {
			Log.d(TAG,"Directori " + tempFolder.toString() + " creat");
		}
			
		//Creem els botons i text
		URLText = (TextView) findViewById(R.id.textViewUrl);
		buttonDescarrega = (ImageButton) findViewById(R.id.button);
		buttonDescarrega.setOnClickListener(this);
		updateURL();
		//Si vitamio no est� instal�lat, mostrem el missatge d'alerta
		List<PackageInfo> apps = getPackageManager().getInstalledPackages(0);
		for(int i = 0; i < apps.size(); i++)
		{
			if(apps.get(i).applicationInfo.loadLabel(getPackageManager()).toString().equals("Vitamio"))
			{
				libsFound = true;
			}
		}
		if(!libsFound)
		{
			alerta = (ImageView) findViewById(R.id.imageViewAlerta);
			alerta.setVisibility(View.VISIBLE);
			alertaText = (TextView) findViewById(R.id.textViewAlerta);
			alertaText.setVisibility(View.VISIBLE);
		}	
	}
	
	public void onClick(View v) {
		//Al polsar el boto s'inicia la descarrega
		Log.d(TAG, "onClicked");
		Descarrega d = new Descarrega(this);
		try
		{
			d.execute(new URL(((UpcApp)getApplication()).getUrl()));
			html = d.get();
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
		catch (ExecutionException e)
		{
			e.printStackTrace();
		}
		catch (MalformedURLException e)
		{
			Toast.makeText(this, "URL Malformada", Toast.LENGTH_LONG).show();
		}
		if(!html.equals("")) 
		{
			((UpcApp) getApplication()).setDesc(html);
			startActivity(new Intent(this, Directoris.class));
		}
	}
	
	public void onResume() {
		super.onResume();
		updateURL();
	}
	
	public void updateURL() {
		URLText.setText(((UpcApp)getApplication()).getUrl());
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		//Determina el funcionament al apretar la tecla d'opcions
			getMenuInflater().inflate(R.menu.menu, menu);
			Log.d(TAG, "Menu");
			return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		//Determina el funcionament al clickar en el menu d'opcions
		switch (item.getItemId()) {
			case R.id.itemprefs:
				startActivity(new Intent(this, Prefs.class));
				Log.d(TAG, "Preferencies");
				return true;
			case R.id.itemhelp:
				startActivity(new Intent(this, Help.class));
				Log.d(TAG, "Help");
				return true;
			default:
				return false;
		}
	}

	/*
	 * COSAS POR HACER
	 * (ABANS DE LA PRESENTACIÓ) Poner la defaultURL al server de PBE
	 * (WHISLIST FOR IMANOL) Hacer streaming DE VERDAD (appendeando los videos, seria IMPRESIONANTE)
	 * Borrar esta puta mierda
	 */
}