diff --git a/res/layout/help.xml b/res/layout/help.xml index 20bc5e6..4ecc007 100644 --- a/res/layout/help.xml +++ b/res/layout/help.xml @@ -23,6 +23,7 @@ android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="0dp" + android:gravity="center_horizontal" android:text="@string/helpText" /> diff --git a/src/com/upc/pbe/upcnews/BandwidthMeasurer.java b/src/com/upc/pbe/upcnews/BandwidthMeasurer.java index 4ab1e94..c9f6675 100644 --- a/src/com/upc/pbe/upcnews/BandwidthMeasurer.java +++ b/src/com/upc/pbe/upcnews/BandwidthMeasurer.java @@ -2,22 +2,20 @@ package com.upc.pbe.upcnews; import android.net.TrafficStats; -public class BandwidthMeasurer -{ - public double Measure(long rxBytes, long Time) - { +//Mesura la velocitat (Bandwidth) d'Internet +public class BandwidthMeasurer{ + + public double Measure(long rxBytes, long Time){ long AfterTime = System.currentTimeMillis(); double bps; long TotalRxAfterTest = TrafficStats.getTotalRxBytes(); double TimeDifference = AfterTime - Time; double rxDiff = TotalRxAfterTest - rxBytes; - if(rxDiff != 0) - { + if(rxDiff != 0){ bps = ((rxDiff*8) / (TimeDifference/1000)); // total rx bits per second. } - else - { + else{ bps = -1; //No transmitted data } return bps; diff --git a/src/com/upc/pbe/upcnews/Descarrega.java b/src/com/upc/pbe/upcnews/Descarrega.java index 1ffd999..05597d8 100644 --- a/src/com/upc/pbe/upcnews/Descarrega.java +++ b/src/com/upc/pbe/upcnews/Descarrega.java @@ -14,47 +14,43 @@ import java.net.URLConnection; import android.os.AsyncTask; import android.util.Log; +//Descarrega un arxiu i el guarda o retorna en String public class Descarrega extends AsyncTask { + final static String TAG = "Descarrega"; - private String url; - private String carpeta; - private String nomarxiu; - private String html; + private String url, carpeta, nomarxiu, html; private File arxiu; public Descarrega() { nomarxiu = "ejemplo.xml"; carpeta = "Environment.getExternalStorageDirectory.getPath()"; - this.carpeta = null; this.html = ""; this.arxiu = null; } public void descarregarguardar(String u, String nom, String car) { + //Descarrega un arxiu i el guarda this.nomarxiu = nom; this.carpeta = car; this.url = u; - // Creem el directori + //Creem el directori File dir = new File(carpeta); if (!dir.exists()) { if (!dir.mkdir()) { return; // No s'ha pogut crear (ja existeix) } } - - // Creem l'arxiu + //Creem l'arxiu arxiu = new File(carpeta + nomarxiu); - - // Iniciem la descàrrega + //Iniciem la descarrega try { URLConnection conn = new URL(url).openConnection(); conn.connect(); Log.d(TAG, "\nDescarregant: \n"); Log.d(TAG, " >> URL: " + url + " >> Nom: " + nomarxiu + " >> Tamany: " + conn.getContentLength() + " bytes"); - - // Llegeix cada byte i l'escriu en un arxiu fins que arriba a -1 + //Llegeix cada byte i l'escriu en un arxiu fins que arriba a -1 InputStream in = conn.getInputStream(); OutputStream out = new FileOutputStream(arxiu); int b = 0; @@ -66,7 +62,6 @@ public class Descarrega extends AsyncTask { } out.close(); in.close(); - } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { @@ -75,30 +70,27 @@ public class Descarrega extends AsyncTask { } protected String doInBackground(String u) throws IOException { + //Descarrega un arxiu i el retorna en un String this.url = u; html = ""; - - URLConnection conn = new URL(url).openConnection(); - BufferedReader in = new BufferedReader(new InputStreamReader( - conn.getInputStream())); - Log.d(TAG, "\nDescarregant: \n"); - Log.d(TAG, - ">> URL: " + url + " >> Tamany: " + conn.getContentLength() - + " bytes"); - String inputLine; - while ((inputLine = in.readLine()) != null) { - html = html + "\n" + inputLine; - } - Log.d(TAG, "Descarrega finalitzada"); - - in.close(); + //Iniciem la connexió i creem els Streams + URLConnection conn = new URL(url).openConnection(); + BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); + //Iniciem la descarrega i anem guardant al String + Log.d(TAG, "\nDescarregant: \n"); + Log.d(TAG, ">> URL: " + url + " >> Tamany: " + conn.getContentLength()+ " bytes"); + String inputLine; + while ((inputLine = in.readLine()) != null) { + html = html + "\n" + inputLine; + } + Log.d(TAG, "Descarrega finalitzada"); + in.close(); return html; - } @Override protected Object doInBackground(Object... arg0) { - + //Metode Overrided, no te utilitat return null; } } diff --git a/src/com/upc/pbe/upcnews/Directoris.java b/src/com/upc/pbe/upcnews/Directoris.java index ac3a96d..19eb8ea 100644 --- a/src/com/upc/pbe/upcnews/Directoris.java +++ b/src/com/upc/pbe/upcnews/Directoris.java @@ -17,71 +17,44 @@ import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Toast; -public class Directoris extends Activity implements OnItemClickListener -{ +//Segona Activity, mostra els directoris i arxius del servidor +public class Directoris extends Activity implements OnItemClickListener { + final static String TAG = "Directoris"; private Descarrega d = new Descarrega(); - private String URL; - private String currentFolder; + private String URL, currentFolder; private HTMLParser parser; - public void onCreate(Bundle savedInstanceState) - { + public void onCreate(Bundle savedInstanceState){ + //Creem el layout Log.d(TAG, "onCreated"); - - super.onCreate(savedInstanceState); + //Passem del layout anterior al actual setContentView(R.layout.dirs); - + //Canviem la direccio de la URL a la actual currentFolder = ((UpcApp) getApplication()).getUrl() + "/"; URL = currentFolder; + //Creem un HTMLParser parser = new HTMLParser(URL); - + //Fem una llista ListView i la omplim amb la informacio trobada ((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() - { + public void showResources(){ + //Actualitza la informacio que mostra la ListView a mesura que ens desplacem pels directoris ArrayList entries; - try - { + try{ entries = parser.parse(d.doInBackground(currentFolder)); this.createEntries(entries); } - catch (IOException e) - { - // Nunca llegará aquí la cosa, y si lo hace ES CULPA DEL SERVIDOR!! + catch (IOException e){ + // Nunca llegara aqui la cosa, y si lo hace ES CULPA DEL SERVIDOR!! } } - public void createEntries(ArrayList directories) - { + public void createEntries(ArrayList directories){ + //Crea les entrades de la ListView String[] entries = directories.toArray(new String[directories.size()]); ListView listView = (ListView) findViewById(R.id.listView1); listView.setAdapter(null); @@ -89,68 +62,79 @@ public class Directoris extends Activity implements OnItemClickListener listView.setAdapter(adapter); } - public void onItemClick(AdapterView parent, View view, int position, long id) - { + public void onItemClick(AdapterView parent, View view, int position, long id) { + //Determina el funcionament al clickar en una de les entrades de al ListView String path = ((TextView)view).getText().toString(); - if(path.endsWith(".m3u8")) - { + //Si es un m3u8, el descarrega, parseja i inicia la reproducció + if(path.endsWith(".m3u8")){ path = currentFolder + path; String playlist; - try - { - playlist = d.doInBackground(path); - + try{ + playlist = d.doInBackground(path); Parser p = new Parser(path.substring(0, path.lastIndexOf("/") + 1), this); - try - { + try{ ArrayList m3u8parsed = p.parseFile(playlist); Log.d(TAG, "parsing completed"); HLS h = new HLS(m3u8parsed,((UpcApp)getApplication()).getLocalPath()); h.start(); } - catch (ErrorException e) - { + catch (ErrorException e){ Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); Log.d(TAG, e.getMessage()); } - catch (WarningException e) - { + catch (WarningException e){ Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); Log.d(TAG, e.getMessage()); } - catch (InfoException e) - { + catch (InfoException e){ Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); Log.d(TAG, e.getMessage()); } } - catch (IOException e1) - { - // Nunca llegará aquí la cosa, y si lo hace ES CULPA DEL SERVIDOR!! + catch (IOException e1){ + // Nunca llegara aqui la cosa, y si lo hace ES CULPA DEL SERVIDOR!! } } - else //Se trata de una carpeta - { - if(!path.startsWith("http://")) - { + //Si es una carpeta, hi entra i mostra el seu contingut + else{ + if(!path.startsWith("http://")){ currentFolder += path; } - else - { + else{ currentFolder = path; } showResources(); } - } - + } + + 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, "Preferences"); + return true; + case R.id.itemhelp: + startActivity(new Intent(this, Help.class)); + Log.d(TAG, "Help"); + return true; + default: + return false; + } + } - public boolean onKeyDown(int keyCode, KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) - { - if(currentFolder.equals(URL)) - { + //Determina el funcionament al apretar la tecla de tornar enrere + if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){ + if(currentFolder.equals(URL)){ super.finish(); return true; } diff --git a/src/com/upc/pbe/upcnews/ErrorException.java b/src/com/upc/pbe/upcnews/ErrorException.java index abd185c..e6ef7ff 100644 --- a/src/com/upc/pbe/upcnews/ErrorException.java +++ b/src/com/upc/pbe/upcnews/ErrorException.java @@ -1,10 +1,11 @@ package com.upc.pbe.upcnews; -public class ErrorException extends Exception -{ +//Excepcio d'error critic, finalitza l'aplicacio +public class ErrorException extends Exception { + private static final long serialVersionUID = 1L; - public ErrorException(String message) - { + + public ErrorException(String message){ super(message); } } diff --git a/src/com/upc/pbe/upcnews/HLS.java b/src/com/upc/pbe/upcnews/HLS.java index d21ff38..a7fb2ae 100644 --- a/src/com/upc/pbe/upcnews/HLS.java +++ b/src/com/upc/pbe/upcnews/HLS.java @@ -4,20 +4,20 @@ import java.util.ArrayList; import android.util.Log; +//Gestor del protocol HTTP Live Streaming public class HLS { private static final String TAG = "HLS"; private ArrayList videos; private int currentVideo ; - private int currentQuality; //0 es la más alta + private int currentQuality; //0 es la mas alta private int currentSegment; private boolean endReached; private String localFolder; //private fileDownloader fd; private BandwidthMeasurer bm; - public HLS(ArrayList parsed, String localFolder) - { + public HLS(ArrayList parsed, String localFolder){ currentVideo = currentQuality = currentSegment = 0; this.videos = parsed; endReached = false; @@ -25,32 +25,26 @@ public class HLS { bm = new BandwidthMeasurer(); } - public void start() - { - for(currentVideo = 0; currentVideo < videos.size(); currentVideo++) - { + public void start(){ + for(currentVideo = 0; currentVideo < videos.size(); currentVideo++){ ArrayList