Commit cfb7213ed846fe78f1f4a998e20a3aeaa619bc99

Authored by Imanol-Mikel Barba Sabariego
1 parent af26ffa5

Navegador HTTP funcional, falta detectar las listas (en un minuto lo hago)

gen/com/upc/pbe/upcnews/R.java
... ... @@ -16,24 +16,21 @@ public final class R {
16 16 public static final int ic_menu_name=0x7f020002;
17 17 }
18 18 public static final class id {
19   - public static final int AbsoluteLayout1=0x7f08000a;
20   - public static final int ImageView01=0x7f08000c;
21   - public static final int ImageView1=0x7f08000b;
  19 + public static final int AbsoluteLayout1=0x7f080006;
  20 + public static final int ImageView01=0x7f080008;
  21 + public static final int ImageView1=0x7f080007;
22 22 public static final int LinearLayout1=0x7f080001;
23   - public static final int button=0x7f08000f;
24   - public static final int button1=0x7f080003;
25   - public static final int button2=0x7f080004;
26   - public static final int button3=0x7f080005;
27   - public static final int button4=0x7f080006;
28   - public static final int button5=0x7f080007;
29   - public static final int itemhelp=0x7f080012;
30   - public static final int itemprefs=0x7f080011;
31   - public static final int menu_settings=0x7f080010;
32   - public static final int textView1=0x7f080008;
33   - public static final int textView2=0x7f080009;
34   - public static final int textViewRoute=0x7f08000d;
  23 + public static final int button=0x7f08000b;
  24 + public static final int itemhelp=0x7f08000f;
  25 + public static final int itemprefs=0x7f08000e;
  26 + public static final int listView1=0x7f080003;
  27 + public static final int menu_settings=0x7f08000d;
  28 + public static final int rowTextView=0x7f08000c;
  29 + public static final int textView1=0x7f080004;
  30 + public static final int textView2=0x7f080005;
  31 + public static final int textViewRoute=0x7f080009;
35 32 public static final int textViewTitle=0x7f080002;
36   - public static final int textViewXml=0x7f08000e;
  33 + public static final int textViewXml=0x7f08000a;
37 34 public static final int videoView1=0x7f080000;
38 35 }
39 36 public static final class layout {
... ... @@ -41,6 +38,7 @@ public final class R {
41 38 public static final int dirs=0x7f030001;
42 39 public static final int help=0x7f030002;
43 40 public static final int main_activity=0x7f030003;
  41 + public static final int rowlayout=0x7f030004;
44 42 }
45 43 public static final class menu {
46 44 public static final int activity_main=0x7f070000;
... ...
res/layout/dirs.xml
... ... @@ -15,35 +15,14 @@
15 15 android:layout_marginLeft="14dp"
16 16 android:text="@string/title"
17 17 android:textAppearance="?android:attr/textAppearanceMedium" />
18   -
19   - <Button
20   - android:id="@+id/button1"
21   - android:layout_width="match_parent"
22   - android:layout_height="wrap_content"
23   - android:layout_marginTop="38dp" />
24   -
25   - <Button
26   - android:id="@+id/button2"
27   - android:layout_width="match_parent"
28   - android:layout_height="wrap_content"
29   - android:layout_marginTop="38dp" />
30   -
31   - <Button
32   - android:id="@+id/button3"
33   - android:layout_width="match_parent"
34   - android:layout_height="wrap_content"
35   - android:layout_marginTop="38dp" />
36   -
37   - <Button
38   - android:id="@+id/button4"
  18 +
  19 + <ListView
  20 + android:id="@+id/listView1"
39 21 android:layout_width="match_parent"
40 22 android:layout_height="wrap_content"
41   - android:layout_marginTop="38dp" />
  23 + android:clickable="true"
  24 + android:visibility="visible" >
42 25  
43   - <Button
44   - android:id="@+id/button5"
45   - android:layout_width="match_parent"
46   - android:layout_height="wrap_content"
47   - android:layout_marginTop="38dp" />
  26 + </ListView>
48 27  
49 28 </LinearLayout>
50 29 \ No newline at end of file
... ...
res/layout/rowlayout.xml 0 → 100644
  1 +<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  2 + android:id="@+id/rowTextView"
  3 + android:layout_width="fill_parent"
  4 + android:layout_height="wrap_content"
  5 + android:padding="10dp"
  6 + android:textSize="16sp" >
  7 +</TextView>
0 8 \ No newline at end of file
... ...
res/values/strings.xml
... ... @@ -14,7 +14,7 @@
14 14 <string name="directoris">Directoris</string>
15 15 <string name="button">Button</string>
16 16 <string name="button1">Play</string>
17   - <string name="title">Directories found:</string>
  17 + <string name="title">Resource List</string>
18 18 <string name="logodescription">Logo Image</string>
19 19 <string name="hello_world">Hello world!</string>
20 20 <string name="title_activity_video">VideoActivity</string>
... ...
src/com/upc/pbe/upcnews/Directoris.java
... ... @@ -6,59 +6,52 @@ import android.app.Activity;
6 6 import android.content.Intent;
7 7 import android.os.Bundle;
8 8 import android.util.Log;
  9 +import android.view.KeyEvent;
9 10 import android.view.Menu;
10 11 import android.view.MenuItem;
11 12 import android.view.View;
12   -import android.view.View.OnClickListener;
13   -import android.widget.Button;
14   -import android.widget.Toast;
15   -
16   -public class Directoris extends Activity implements OnClickListener {
17   -
  13 +import android.widget.AdapterView;
  14 +import android.widget.ArrayAdapter;
  15 +import android.widget.ListView;
  16 +import android.widget.TextView;
  17 +import android.widget.AdapterView.OnItemClickListener;
  18 +
  19 +public class Directoris extends Activity implements OnItemClickListener
  20 +{
18 21 final static String TAG = "Directoris";
19   - String s;
20   - Descarrega d;
21   - String url;
22   -
23   - ArrayList<Button> buttons = new ArrayList<Button>();
24   -
25   - public void onCreate(Bundle savedInstanceState) {
  22 + private Descarrega d = new Descarrega();
  23 + private String URL;
  24 + private String currentFolder;
  25 + private HTMLParser parser;
26 26  
  27 + public void onCreate(Bundle savedInstanceState)
  28 + {
27 29 Log.d(TAG, "onCreated");
28   - url = ((UpcApp) getApplication()).getUrl();
  30 +
  31 +
29 32 super.onCreate(savedInstanceState);
30 33 setContentView(R.layout.dirs);
31 34  
32   -
33   - //Descarrega del HTML de l'index, busqueda de directoris i creacio dels botons
34   - this.crearButtons();
35   - s = ((UpcApp) getApplication()).getDesc();
36   -
37   - HTMLParser pars = new HTMLParser();
38   - ArrayList<String> dirs = pars.parse(s);
39   -
40   - for (int i = 0; i < dirs.size(); i++) {
41   - Log.d(TAG, "Directori " + dirs.get(i).toString());
42   - buttons.get(i).setVisibility(View.VISIBLE);
43   - if(dirs.get(i).equals(""))
44   - {
45   - buttons.get(i).setText("<Current Folder>");
46   - }
47   - else
48   - {
49   - buttons.get(i).setText(dirs.get(i));
50   - }
51   - }
  35 + currentFolder = ((UpcApp) getApplication()).getUrl() + "/";
  36 + URL = currentFolder;
  37 + parser = new HTMLParser(URL);
  38 +
  39 + ((ListView)findViewById(R.id.listView1)).setOnItemClickListener(this);
  40 +
  41 + this.showResources();
52 42 }
53 43  
54   - public boolean onCreateOptionsMenu(Menu menu) {
  44 + public boolean onCreateOptionsMenu(Menu menu)
  45 + {
55 46 getMenuInflater().inflate(R.menu.menu, menu);
56 47 Log.d(TAG, "Menu");
57 48 return true;
58 49 }
59 50  
60   - public boolean onOptionsItemSelected(MenuItem item) {
61   - switch (item.getItemId()) {
  51 + public boolean onOptionsItemSelected(MenuItem item)
  52 + {
  53 + switch (item.getItemId())
  54 + {
62 55 case R.id.itemprefs:
63 56 startActivity(new Intent(this, Prefs.class));
64 57 Log.d(TAG, "Preferences");
... ... @@ -71,48 +64,44 @@ public class Directoris extends Activity implements OnClickListener {
71 64 return false;
72 65 }
73 66 }
74   -
75   - public void onClick(View c) {
76   - switch (c.getId()) {
77   - case R.id.button1: {
78   - this.buttonClicked(buttons.get(0));
79   - break;
80   -
81   - }
82   - case R.id.button2: {
83   - this.buttonClicked(buttons.get(1));
84   - break;
85   - }
86   - case R.id.button3: {
87   - this.buttonClicked(buttons.get(2));
88   - break;
89   - }
90   - case R.id.button4: {
91   - this.buttonClicked(buttons.get(3));
92   - break;
  67 +
  68 + public void createEntries(ArrayList<String> directories)
  69 + {
  70 + String[] entries = directories.toArray(new String[directories.size()]);
  71 + ListView listView = (ListView) findViewById(R.id.listView1);
  72 + listView.setAdapter(null);
  73 + ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.rowlayout, entries);
  74 + listView.setAdapter(adapter);
  75 + }
  76 +
  77 + public void onItemClick(AdapterView<?> parent, View view, int position, long id)
  78 + {
  79 + String path = ((TextView)view).getText().toString();
  80 + if(!path.startsWith("http://"))
  81 + {
  82 + currentFolder += path;
93 83 }
  84 + else
  85 + {
  86 + currentFolder = path;
94 87 }
  88 + showResources();
  89 + }
  90 +
  91 + public void showResources()
  92 + {
  93 + ArrayList<String> entries = parser.parse(d.doInBackground(currentFolder));
  94 + this.createEntries(entries);
95 95 }
96 96  
97   - public void crearButtons() {
98   - buttons.add((Button) findViewById(R.id.button1));
99   - buttons.add((Button) findViewById(R.id.button2));
100   - buttons.add((Button) findViewById(R.id.button3));
101   - buttons.add((Button) findViewById(R.id.button4));
102   - buttons.add((Button) findViewById(R.id.button5));
103   -
104   - for (int i = 0; i < buttons.size(); i++) {
105   - buttons.get(i).setOnClickListener(this);
106   - buttons.get(i).setVisibility(View.GONE);
107   - }
108   - }
  97 + /*public void buttonClicked(Button b)
  98 + {
109 99  
110   - public void buttonClicked(Button b) {
111   -
112   - //Descarrega de la p�gina HTML del directori i busqueda d'un .m3u8 en ella
  100 + // Descarrega de la p�gina HTML del directori i busqueda d'un .m3u8 en
  101 + // ella
113 102 Log.d(TAG, "Click on " + b.getText());
114 103 String path = b.getText().toString();
115   - if(path.equals("<Current Folder>"))
  104 + if (path.equals("<Current Folder>"))
116 105 {
117 106 path = "";
118 107 }
... ... @@ -120,43 +109,68 @@ public class Directoris extends Activity implements OnClickListener {
120 109 d = new Descarrega();
121 110 String str = d.doInBackground(url + "/" + path);
122 111 HTMLParser pars = new HTMLParser();
  112 +
123 113 String urlvideo = pars.findvideo(str, url + "/" + path);
124 114 Log.d(TAG, urlvideo);
125   -
126   - //Descarrega de l'arxiu .m3u8 (si existeix) i parseig
127   - if (urlvideo.equalsIgnoreCase("No s'ha trobat")) {
128   - Toast.makeText(this, "The directory " + b.getText() + " does not contain any '.m3u8' file", Toast.LENGTH_LONG).show();
129   - } else {
  115 +
  116 + // Descarrega de l'arxiu .m3u8 (si existeix) i parseig
  117 + if (urlvideo.equalsIgnoreCase("No s'ha trobat"))
  118 + {
  119 + Toast.makeText(this, "The directory " + b.getText()
  120 + + " does not contain any '.m3u8' file", Toast.LENGTH_LONG).show();
  121 + }
  122 + else
  123 + {
130 124 d = new Descarrega();
131 125 String m3u8 = d.doInBackground(urlvideo);
132 126 Log.d(TAG, m3u8);
133   - Parser p = new Parser(urlvideo.substring(0,urlvideo.lastIndexOf("/")+1),this);
134   - try {
  127 + Parser p = new Parser(urlvideo.substring(0, urlvideo.lastIndexOf("/") + 1), this);
  128 + try
  129 + {
135 130 ArrayList<ParentList> m3u8parsed = p.parseFile(m3u8);
136 131 Log.d(TAG, "parsed completed");
137 132 HLS h = new HLS(m3u8parsed);
138 133 h.start();
139   -
140   - } catch (ErrorException e) {
  134 +
  135 + }
  136 + catch (ErrorException e)
  137 + {
141 138 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
142 139 Log.d(TAG, e.getMessage());
143   - } catch (WarningException e) {
  140 + }
  141 + catch (WarningException e)
  142 + {
144 143 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
145 144 Log.d(TAG, e.getMessage());
146   - } catch (InfoException e) {
  145 + }
  146 + catch (InfoException e)
  147 + {
147 148 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
148 149 Log.d(TAG, e.getMessage());
149 150 }
150   -
151   - }
152   -
153   -
154   -
155   - //VideoPlayer
  151 +
  152 + }*/
  153 +
156 154 /*
157   - String urlvid = null;
158   - ((UpcApp) getApplication()).setDesc(urlvid);
159   -
160   - startActivity(new Intent(this, VideoActivity.class));*/
  155 + * String urlvid = null; ((UpcApp) getApplication()).setDesc(urlvid);
  156 + *
  157 + * startActivity(new Intent(this, VideoActivity.class));
  158 +
  159 + }*/
  160 +
  161 + public boolean onKeyDown(int keyCode, KeyEvent event) {
  162 + if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
  163 + {
  164 + if(currentFolder.equals(URL))
  165 + {
  166 + super.finish();
  167 + return true;
  168 + }
  169 + currentFolder = currentFolder.substring(0, currentFolder.length()-1);
  170 + currentFolder = currentFolder.substring(0,currentFolder.lastIndexOf("/")+1);
  171 + showResources();
  172 + return true;
  173 + }
  174 + return super.onKeyDown(keyCode, event);
161 175 }
162 176 }
163 177 \ No newline at end of file
... ...
src/com/upc/pbe/upcnews/HTMLParser.java
... ... @@ -2,40 +2,108 @@ package com.upc.pbe.upcnews;
2 2  
3 3 import java.util.ArrayList;
4 4  
5   -public class HTMLParser {
6   -
7   - String code;
8   - ArrayList<String> directoris;
9   -
10   - public HTMLParser(){
11   - this.code = null;
12   - directoris = new ArrayList<String>();
13   - directoris.add("");
14   - }
15   -
16   - public ArrayList<String> parse(String code){
17   - //Separem el codi en linies
18   - String[] split = code.split("\n");
19   - //Mirem a cada linia si cont� un href, i hi haur� un directori si cont� el conjunt /"
20   - //Si hi ha un directori, tallem l'string per obtenir-ne el nom.
21   - for(int i = 0; i < split.length; i++){
22   - if(split[i].contains("href") && split[i].contains("/index.html\"")){
23   - directoris.add((split[i].substring((split[i].indexOf("href="))+6, (split[i].indexOf("/index.html\"")))));
24   - }
25   - }
26   - return directoris;
27   - }
28   -
29   - public String findvideo(String code, String direccio){
30   - //Donada la direcci� del directori i el html del mateix, troba l'arxiu .m3u8 i retorna
31   - //la url directe.
32   - String[] split = code.split("\n");
33   - for(int i = 0; i < split.length; i++){
34   - if(split[i].contains(".m3u8") && !split[i].contains("._")){
35   - String nomarxiu = split[i].substring((split[i].indexOf("href="))+6, (split[i].indexOf('"',(split[i].indexOf("href="))+6)));
36   - return direccio+nomarxiu;
37   - }
38   - }
39   - return "No s'ha trobat";
40   - }
  5 +import android.util.Log;
  6 +
  7 +public class HTMLParser
  8 +{
  9 + private final static String TAG = "HTMLParser";
  10 + private ArrayList<String> resources;
  11 + private String server;
  12 +
  13 + public HTMLParser(String u)
  14 + {
  15 + String host = u.substring(7);
  16 + server = u.substring(0,host.indexOf("/")+7);
  17 + resources = new ArrayList<String>();
  18 + }
  19 +
  20 + public ArrayList<String> parse(String code)
  21 + {
  22 + if(!resources.isEmpty())
  23 + {
  24 + resources.clear();
  25 + }
  26 + parseDirectories(resources,code);
  27 + //parseFiles(resources,code);
  28 + return resources;
  29 + }
  30 +
  31 + public void parseDirectories(ArrayList<String> resources, String code)
  32 + {
  33 + // Separem el codi en linies
  34 + String[] split = code.split("\n");
  35 + /*
  36 + * Busquemos el tag <a>, descartando comentarios:
  37 + */
  38 + for (int i = 0; i < split.length; i++)
  39 + {
  40 + while (split[i].startsWith("<!--"))
  41 + {
  42 + if (split[i++].endsWith("-->"))
  43 + {
  44 + break;
  45 + }
  46 + }
  47 + // Posible comentario descartado
  48 + if (split[i].indexOf("<a ") != -1) //Enlace
  49 + {
  50 + if (split[i].indexOf("href=") != -1) //Esto tiene buena pinta
  51 + {
  52 + String dirpath = split[i].substring(split[i].indexOf("href=\"") + 6); //Eliminamos morralla del principio.
  53 + dirpath = dirpath.substring(0, dirpath.indexOf("\"")); //Quitamos la morralla del final, nos queda el valor de href sólo.
  54 + if(dirpath.lastIndexOf("/") != -1) //Mira que sea una carpeta y no un fichero suelto.
  55 + {
  56 + dirpath = dirpath.substring(0,dirpath.lastIndexOf("/")+1); //Elimina parte que no es carpeta
  57 + if(dirpath.startsWith("/"))
  58 + {
  59 + dirpath = server + dirpath;
  60 + }
  61 + if(!resources.contains(dirpath)) //No seamos repetitivos
  62 + {
  63 + resources.add(dirpath);
  64 + }
  65 + Log.d(TAG, "DIRECTORY FOUND: " + dirpath);
  66 + }
  67 + }
  68 + }
  69 + }
  70 + }
  71 +
  72 + /*public ArrayList<String> findvideo(String code, String direccio)
  73 + {
  74 + // Donada la direcci� del directori i el html del mateix, troba l'arxiu
  75 + // .m3u8 i retorna
  76 + // la url directe.
  77 + String[] split = code.split("\n");
  78 +
  79 + for (int i = 0; i < split.length; i++)
  80 + {
  81 + while (split[i].startsWith("<!--"))
  82 + {
  83 + if (split[i++].endsWith("-->"))
  84 + {
  85 + break;
  86 + }
  87 + }
  88 + // Posible comentario descartado
  89 + if (split[i].contains("<a ")) //Enlace
  90 + {
  91 + if (split[i].contains("href=")) //Esto tiene buena pinta
  92 + {
  93 + String dirpath = split[i].substring(split[i].indexOf("href=\"") + 6); //Eliminamos morralla del principio.
  94 + dirpath = dirpath.substring(0, dirpath.indexOf("\"")); //Quitamos la morralla del final, nos queda el valor de href sólo.
  95 + if(dirpath.lastIndexOf("/") != -1) //Mira que sea una carpeta y no un fichero suelto.
  96 + {
  97 + dirpath = dirpath.substring(0,dirpath.lastIndexOf("/")+1); //Elimina parte que no es carpeta
  98 + if(!directoris.contains(dirpath)) //No seamos repetitivos
  99 + {
  100 + directoris.add(dirpath);
  101 + }
  102 + Log.d(TAG, "DIRECTORY FOUND: " + dirpath);
  103 + }
  104 + }
  105 + }
  106 + }
  107 + return "No s'ha trobat";
  108 + }*/
41 109 }
... ...
src/com/upc/pbe/upcnews/Parser.java
... ... @@ -7,20 +7,21 @@ import android.app.Activity;
7 7 import android.widget.Toast;
8 8  
9 9 public class Parser
10   -{
  10 +{
11 11 private static final String STARTWORD = "#EXTM3U";
12   - private String path; //CWD, completes implicit paths such as "file.m3u8" appending the CWD in order to download the file
13   - private int fileType; //indicates if segment list or media list
  12 + private String path; // CWD, completes implicit paths such as "file.m3u8"
  13 + // appending the CWD in order to download the file
  14 + private int fileType; // indicates if segment list or media list
14 15 private int currentLine;
15   - private int currentSegment; //Cada segmento de cada calidad distinta
16   - private int currentList; //Indice de cada recurso distinto
  16 + private int currentSegment; // Cada segmento de cada calidad distinta
  17 + private int currentList; // Indice de cada recurso distinto
17 18 private boolean expectSegment = false;
18 19 private boolean expectList = false;
19 20 private Descarrega download;
20   - private Activity caller; //Where to send notifications and warnings
21   - private boolean validated; //Wether if list starts with #EXTM3U or not
22   -
23   - public Parser(String p,Activity caller)
  21 + private Activity caller; // Where to send notifications and warnings
  22 + private boolean validated; // Wether if list starts with #EXTM3U or not
  23 +
  24 + public Parser(String p, Activity caller)
24 25 {
25 26 this.caller = caller;
26 27 path = p;
... ... @@ -29,109 +30,110 @@ public class Parser
29 30 currentList = 0;
30 31 fileType = -1;
31 32 /*
32   - * -1 indicates UNDETERMINED
33   - * 0 indicates SEGMENTS
34   - * 1 indicates MEDIA
  33 + * -1 indicates UNDETERMINED 0 indicates SEGMENTS 1 indicates MEDIA
35 34 */
36 35 download = new Descarrega();
37 36 validated = false;
38 37 }
39   -
  38 +
40 39 public ArrayList<ParentList> parseFile(String file) throws ErrorException, WarningException, InfoException
41 40 {
42 41 ArrayList<ParentList> lists = new ArrayList<ParentList>();
43 42 lists.add(new ParentList(""));
44 43 lists.get(0).getLists().add(new List(-1));
45 44 String[] lines = file.split("\n");
46   - for(int i = 0; i < lines.length; i++)
  45 + for (int i = 0; i < lines.length; i++)
47 46 {
48   - if(lines[i].endsWith("\\"))
  47 + if (lines[i].endsWith("\\"))
49 48 {
50 49 /*
51   - * Merge multiline entries, which are those lines ending with backslash.
  50 + * Merge multiline entries, which are those lines ending with
  51 + * backslash.
52 52 */
53   - lines[++i] = lines[i-1].substring(0, lines[i-1].indexOf("\\")-1) + lines[i];
  53 + lines[++i] = lines[i - 1].substring(0, lines[i - 1].indexOf("\\") - 1)
  54 + + lines[i];
54 55 }
55 56 try
56 57 {
57   - parseLine(lines[i],lists);
  58 + parseLine(lines[i], lists);
58 59 }
59   - catch(InfoException iE)
  60 + catch (InfoException iE)
60 61 {
61 62 Toast.makeText(caller, iE.getMessage(), Toast.LENGTH_SHORT).show();
62 63 }
63   - catch(WarningException wE)
  64 + catch (WarningException wE)
64 65 {
65 66 Toast.makeText(caller, wE.getMessage(), Toast.LENGTH_SHORT).show();
66 67 }
67 68 }
68   - /*
69   - * Esto contiene una lista de RECURSOS,
70   - * que a su vez contiene una lista de CALIDADES.
71   - * Si no te gusta mis VERSOS,
72   - * te mando a la mierda sin SUTILIDADES.
  69 + /*
  70 + * Esto contiene una lista de RECURSOS, que a su vez contiene una lista
  71 + * de CALIDADES. Si no te gusta mis VERSOS, te mando a la mierda sin
  72 + * SUTILIDADES.
73 73 *
74 74 * -- Imanol, hasta las cejas de cafeína.
75 75 *
76   - * PD: En el último nivel de profundidad hay una lista de segmentos (debajo de calidades).
77   - * Allí está el auténtico tesoro de Narnia.
  76 + * PD: En el último nivel de profundidad hay una lista de segmentos
  77 + * (debajo de calidades). Allí está el auténtico tesoro de Narnia.
78 78 */
79   - for(int i = 0; i < lists.size(); i++)
  79 + for (int i = 0; i < lists.size(); i++)
80 80 {
81 81 sortQuality(lists.get(i));
82 82 }
83   - return lists;
  83 + return lists;
84 84 }
85   -
  85 +
86 86 private int searchID(String ID, ArrayList<ParentList> lists)
87 87 {
88   - for(int i = 0; i < lists.size(); i++)
  88 + for (int i = 0; i < lists.size(); i++)
89 89 {
90   - if(lists.get(i).getID().equals(ID))
  90 + if (lists.get(i).getID().equals(ID))
91 91 {
92 92 return i;
93 93 }
94 94 }
95 95 return -1;
96 96 }
97   -
  97 +
98 98 private void sortQuality(ParentList ppls)
99 99 {
100 100 ArrayList<List> lists = ppls.getLists();
101   - if(lists.get(0).getQuality() == -1)
  101 + if (lists.get(0).getQuality() == -1)
102 102 {
103 103 /*
104   - * That's the case of Media lists and Segment lists, they don't have distinct qualities.
  104 + * That's the case of Media lists and Segment lists, they don't have
  105 + * distinct qualities.
105 106 */
106 107 return;
107 108 }
108   - //Bubblesort
109   - while(true)
  109 + // Bubblesort
  110 + while (true)
110 111 {
111 112 boolean sorted = true;
112 113 int i = 0;
113 114 do
114 115 {
115   - if(lists.get(i).getQuality() < lists.get(i+1).getQuality())
  116 + if (lists.get(i).getQuality() < lists.get(i + 1).getQuality())
116 117 {
117 118 sorted = false;
118 119 List aux = lists.get(i);
119   - lists.set(i, lists.get(i+1));
120   - lists.set(i+1, aux);
  120 + lists.set(i, lists.get(i + 1));
  121 + lists.set(i + 1, aux);
121 122 }
122   -
123   - }while(++i != lists.size()-1);
124   - if(sorted)
  123 +
  124 + }
  125 + while (++i != lists.size() - 1);
  126 + if (sorted)
125 127 {
126 128 break;
127 129 }
128 130 }
129 131  
130 132 }
131   -
  133 +
132 134 public void parseLine(String line, ArrayList<ParentList> lists) throws ErrorException, WarningException, InfoException
133 135 {
134   - if(line.isEmpty())
  136 + if (line.isEmpty())
135 137 {
136 138 currentLine++;
137 139 return;
... ... @@ -139,10 +141,10 @@ public class Parser
139 141 else
140 142 {
141 143 ParentList ppls = lists.get(currentList);
142   - List pls = ppls.getLists().get(ppls.getLists().size()-1);
143   - if(!validated)
  144 + List pls = ppls.getLists().get(ppls.getLists().size() - 1);
  145 + if (!validated)
144 146 {
145   - if(line.equals(STARTWORD))
  147 + if (line.equals(STARTWORD))
146 148 {
147 149 validated = true;
148 150 }
... ... @@ -153,47 +155,48 @@ public class Parser
153 155 }
154 156 else
155 157 {
156   - if(line.charAt(0) == '#')
  158 + if (line.charAt(0) == '#')
157 159 {
158   - if(line.substring(0,4).equals("#EXT"))
  160 + if (line.substring(0, 4).equals("#EXT"))
159 161 {
160   - if(expectSegment)
  162 + if (expectSegment)
161 163 {
162 164 throw new ErrorException("Expected segment URI, got an EXT-TAG instead");
163 165 }
164   - if(expectList)
  166 + if (expectList)
165 167 {
166 168 throw new ErrorException("Expected List, got an EXT-TAG instead");
167 169 }
168 170 String[] extTag = line.split(":", 2);
169   - if(extTag[0].equals("#EXT-X-MEDIA-SEQUENCE"))
  171 + if (extTag[0].equals("#EXT-X-MEDIA-SEQUENCE"))
170 172 {
171 173 pls.setSequence(Integer.parseInt(extTag[1]));
172 174 }
173   - else if(extTag[0].equals("#EXT-X-TARGETDURATION"))
  175 + else if (extTag[0].equals("#EXT-X-TARGETDURATION"))
174 176 {
175 177 pls.setMaxDuration(Integer.parseInt(extTag[1]));
176   - }
177   - else if(extTag[0].equals("#EXTINF"))
  178 + }
  179 + else if (extTag[0].equals("#EXTINF"))
178 180 {
179   - if(fileType == -1)
  181 + if (fileType == -1)
180 182 {
181 183 fileType = 0;
182 184 }
183   - else if(fileType == 1)
  185 + else if (fileType == 1)
184 186 {
185   - throw new ErrorException("Invalid file, should contain Lists, " +
186   - "but segments were found.");
  187 + throw new ErrorException("Invalid file, should contain Lists, "
  188 + + "but segments were found.");
187 189 }
188 190 String[] args = extTag[1].split(",");
189 191 float duration = Float.parseFloat(args[0]);
190 192 if (duration > pls.getMaxDuration())
191 193 {
192   - throw new ErrorException("Segment " + currentSegment+1 + " on line " + currentLine
193   - +" exceeds max duration");
  194 + throw new ErrorException("Segment "
  195 + + currentSegment + 1 + " on line "
  196 + + currentLine + " exceeds max duration");
194 197 }
195 198 Segment s = new Segment(duration);
196   - if(args.length == 1)
  199 + if (args.length == 1)
197 200 {
198 201 s.setName("");
199 202 }
... ... @@ -204,42 +207,46 @@ public class Parser
204 207 pls.getSegments().add(s);
205 208 expectSegment = true;
206 209 }
207   -
208   - else if(extTag[0].equals("#EXT-X-STREAM-INF"))
  210 +
  211 + else if (extTag[0].equals("#EXT-X-STREAM-INF"))
209 212 {
210   - if(fileType == -1)
  213 + if (fileType == -1)
211 214 {
212 215 fileType = 1;
213 216 }
214   - else if(fileType == 0)
  217 + else if (fileType == 0)
215 218 {
216   - throw new ErrorException("Invalid file, should contain Segments, " +
217   - "but lists were found.");
  219 + throw new ErrorException("Invalid file, should contain Segments, "
  220 + + "but lists were found.");
218 221 }
219 222 expectList = true;
220 223 String programID = "";
221 224 int bandwidth = -1;
222 225 String[] arguments = extTag[1].split(",");
223   - for(int i = 0; i < arguments.length; i++)
  226 + for (int i = 0; i < arguments.length; i++)
224 227 {
225 228 String[] argument = arguments[i].split("=");
226 229 /*
227   - * In case current argument contains several values like "val1,val2,val3"
228   - * detect the " character and merge them all in one single string
  230 + * In case current argument contains several
  231 + * values like "val1,val2,val3" detect the "
  232 + * character and merge them all in one single
  233 + * string
229 234 */
230   - if(argument[1].startsWith("\"") && !argument[1].endsWith("\""))
  235 + if (argument[1].startsWith("\"")
  236 + && !argument[1].endsWith("\""))
231 237 {
232 238 int j = i;
233   - while(true)
  239 + while (true)
234 240 {
235   - if(i == arguments.length-1)
  241 + if (i == arguments.length - 1)
236 242 {
237   - throw new ErrorException("Malformed argument on line " + currentLine);
  243 + throw new ErrorException("Malformed argument on line "
  244 + + currentLine);
238 245 }
239   - if(arguments[++i].endsWith("\""))
  246 + if (arguments[++i].endsWith("\""))
240 247 {
241 248 String end = arguments[i];
242   - for(int k = j+1; k < i; k++)
  249 + for (int k = j + 1; k < i; k++)
243 250 {
244 251 argument[1] += arguments[k];
245 252 }
... ... @@ -248,51 +255,62 @@ public class Parser
248 255 }
249 256 }
250 257 }
251   - if(argument[0].equals("PROGRAM-ID"))
  258 + if (argument[0].equals("PROGRAM-ID"))
252 259 {
253 260 programID = argument[1];
254 261 }
255   - else if(argument[0].equals("BANDWIDTH"))
  262 + else if (argument[0].equals("BANDWIDTH"))
256 263 {
257 264 bandwidth = Integer.parseInt(argument[1]);
258 265 }
259 266 }
260   - if(programID.equals("")|| bandwidth == -1)
  267 + if (programID.equals("") || bandwidth == -1)
261 268 {
262   - throw new ErrorException("Playlist on line " + currentLine +
263   - " has missing arguments");
  269 + throw new ErrorException("Playlist on line "
  270 + + currentLine
  271 + + " has missing arguments");
264 272 }
265   - if(!programID.equals(ppls.getID())) //If Program-ID doesn't match, search it or create a new one.
  273 + if (!programID.equals(ppls.getID()))
266 274 {
267   - if(ppls.getID().equals(""))
  275 + /*
  276 + * If Program-ID doesn't match, search it or
  277 + * create a new one.
  278 + */
  279 + if (ppls.getID().equals(""))
268 280 {
269 281 /*
270   - * If working with the default ParentList, then modify it, don't create a new one.
  282 + * If working with the default ParentList,
  283 + * then modify it, don't create a new one.
271 284 */
272   - lists.get(0).setID(programID);
  285 + lists.get(0).setID(programID);
273 286 }
274 287 else
275 288 {
276 289 currentList = searchID(programID, lists);
277   - if(currentList == -1)
  290 + if (currentList == -1)
278 291 {
279 292 /*
280   - * ParentList not found, creating a new one.
  293 + * ParentList not found, creating a new
  294 + * one.
281 295 */
282 296 lists.add(new ParentList(programID));
283   - currentList = lists.size() -1;
  297 + currentList = lists.size() - 1;
284 298 }
285 299 ppls = lists.get(currentList);
286   - pls = ppls.getLists().get(ppls.getLists().size()-1);
  300 + pls = ppls.getLists().get(ppls.getLists().size() - 1);
287 301 }
288 302 }
289 303 /*
290   - * Now we create a new List inside the correct ParentList.
  304 + * Now we create a new List inside the correct
  305 + * ParentList.
291 306 */
292   - if((ppls.getLists().size() == 1) && (pls.getQuality() == -1) && pls.getSegments().isEmpty())
  307 + if ((ppls.getLists().size() == 1)
  308 + && (pls.getQuality() == -1)
  309 + && pls.getSegments().isEmpty())
293 310 {
294 311 /*
295   - * If working with the default List, then modify it, don't create a new one.
  312 + * If working with the default List, then modify
  313 + * it, don't create a new one.
296 314 */
297 315 pls.setQuality(bandwidth);
298 316 }
... ... @@ -301,16 +319,16 @@ public class Parser
301 319 ppls.getLists().add(new List(bandwidth));
302 320 }
303 321 }
304   - else if(extTag[0].equals("#EXT-X-MEDIA"))
  322 + else if (extTag[0].equals("#EXT-X-MEDIA"))
305 323 {
306   - if(fileType == -1)
  324 + if (fileType == -1)
307 325 {
308 326 fileType = 1;
309 327 }
310   - else if(fileType == 0)
  328 + else if (fileType == 0)
311 329 {
312   - throw new ErrorException("Invalid file, should contain Segments, " +
313   - "but lists were found.");
  330 + throw new ErrorException("Invalid file, should contain Segments, "
  331 + + "but lists were found.");
314 332 }
315 333 String Type = "";
316 334 String Name = "";
... ... @@ -318,26 +336,30 @@ public class Parser
318 336 String URI = "";
319 337 boolean Default = false;
320 338 String[] arguments = extTag[1].split(",");
321   - for(int i = 0; i < arguments.length; i++)
  339 + for (int i = 0; i < arguments.length; i++)
322 340 {
323 341 String[] argument = arguments[i].split("=");
324 342 /*
325   - * In case current argument contains several values like "val1,val2,val3"
326   - * detect the " character and merge them all in one single string
  343 + * In case current argument contains several
  344 + * values like "val1,val2,val3" detect the "
  345 + * character and merge them all in one single
  346 + * string
327 347 */
328   - if(argument[1].startsWith("\"") && !argument[1].endsWith("\""))
  348 + if (argument[1].startsWith("\"")
  349 + && !argument[1].endsWith("\""))
329 350 {
330 351 int j = i;
331   - while(true)
  352 + while (true)
332 353 {
333   - if(i == arguments.length-1)
  354 + if (i == arguments.length - 1)
334 355 {
335   - throw new ErrorException("Malformed argument on line " + currentLine);
  356 + throw new ErrorException("Malformed argument on line "
  357 + + currentLine);
336 358 }
337   - if(arguments[++i].endsWith("\""))
  359 + if (arguments[++i].endsWith("\""))
338 360 {
339 361 String end = arguments[i];
340   - for(int k = j+1; k < i; k++)
  362 + for (int k = j + 1; k < i; k++)
341 363 {
342 364 argument[1] += arguments[k];
343 365 }
... ... @@ -346,62 +368,66 @@ public class Parser
346 368 }
347 369 }
348 370 }
349   - if(argument[0].equals("NAME"))
  371 + if (argument[0].equals("NAME"))
350 372 {
351   - Name = argument[1].substring(1,argument[1].length()-1);
  373 + Name = argument[1].substring(1, argument[1].length() - 1);
352 374 }
353   - else if(argument[0].equals("TYPE"))
  375 + else if (argument[0].equals("TYPE"))
354 376 {
355 377 Type = argument[1];
356 378 }
357   - else if(argument[0].equals("GROUP-ID"))
  379 + else if (argument[0].equals("GROUP-ID"))
358 380 {
359   - GroupID = argument[1].substring(1,argument[1].length()-1);
  381 + GroupID = argument[1].substring(1, argument[1].length() - 1);
360 382 }
361   - else if(argument[0].equals("DEFAULT"))
  383 + else if (argument[0].equals("DEFAULT"))
362 384 {
363   - if(argument[1].equals("YES"))
  385 + if (argument[1].equals("YES"))
364 386 {
365 387 Default = true;
366 388 }
367   - else if(argument[1].equals("NO"))
  389 + else if (argument[1].equals("NO"))
368 390 {
369 391 Default = true;
370 392 }
371 393 else
372 394 {
373   - throw new ErrorException("Invalid value for argument DEFAULT on line " +
374   - currentLine);
  395 + throw new ErrorException("Invalid value for argument DEFAULT on line "
  396 + + currentLine);
375 397 }
376 398 }
377   - else if(argument[0].equals("URI"))
  399 + else if (argument[0].equals("URI"))
378 400 {
379   - if(!argument[1].contains("http://"))
  401 + if (!argument[1].contains("http://"))
380 402 {
381 403 /*
382 404 * In case the path read is RELATIVE
383 405 */
384   - URI = path + argument[1].substring(1,argument[1].length()-1);
  406 + URI = path
  407 + + argument[1].substring(1, argument[1].length() - 1);
385 408 }
386 409 else
387 410 {
388   - URI = argument[1].substring(1,argument[1].length()-1);
  411 + URI = argument[1].substring(1, argument[1].length() - 1);
389 412 }
390 413 }
391 414 }
392   - if(Type.equals("") || Name.equals("") || URI.equals("") || GroupID.equals(""))
  415 + if (Type.equals("") || Name.equals("")
  416 + || URI.equals("") || GroupID.equals(""))
393 417 {
394   - throw new ErrorException("Playlist on line " + currentLine +
395   - " has missing arguments");
  418 + throw new ErrorException("Playlist on line "
  419 + + currentLine
  420 + + " has missing arguments");
396 421 }
397   - Parser p = new Parser(URI.substring(0, URI.lastIndexOf("/")+1),caller);
  422 + Parser p = new Parser(URI.substring(0, URI.lastIndexOf("/") + 1), caller);
398 423 List newList = p.parseFile(download.doInBackground(URI)).get(0).getLists().get(0);
399   - if(!GroupID.equals(ppls.getID()))
  424 + if (!GroupID.equals(ppls.getID()))
400 425 {
401   - if(ppls.getID().equals(""))
  426 + if (ppls.getID().equals(""))
402 427 {
403 428 /*
404   - * If working with the default ParentList, then modify it, don't create a new one.
  429 + * If working with the default ParentList,
  430 + * then modify it, don't create a new one.
405 431 */
406 432 ppls.setDefault(Default);
407 433 ppls.setName(Name);
... ... @@ -413,14 +439,14 @@ public class Parser
413 439 {
414 440  
415 441 currentList = searchID(GroupID, lists);
416   - if(currentList == -1)
  442 + if (currentList == -1)
417 443 {
418 444 ParentList pl = new ParentList(GroupID);
419 445 pl.setDefault(Default);
420 446 pl.setName(Name);
421 447 pl.setType(Type);
422 448 lists.add(pl);
423   - currentList = lists.size()-1;
  449 + currentList = lists.size() - 1;
424 450 }
425 451 ppls = lists.get(currentList);
426 452 ppls.getLists().add(newList);
... ... @@ -431,40 +457,42 @@ public class Parser
431 457 ppls.getLists().add(newList);
432 458 }
433 459 }
434   -
435   - else if(extTag[0].equals("#EXT-X-ENDLIST"))
  460 +
  461 + else if (extTag[0].equals("#EXT-X-ENDLIST"))
436 462 {
437   - if(expectSegment || expectList)
  463 + if (expectSegment || expectList)
438 464 {
439 465 throw new ErrorException("Unexpected end of list!");
440   - }
  466 + }
441 467 }
442 468  
443   - else if(extTag[0].equals("#EXT-X-VERSION"))
  469 + else if (extTag[0].equals("#EXT-X-VERSION"))
444 470 {
445   - //IGNORE
  471 + // IGNORE
446 472 }
447 473  
448   - else if(extTag[0].equals("#EXT-X-PLAYLIST-TYPE"))
  474 + else if (extTag[0].equals("#EXT-X-PLAYLIST-TYPE"))
449 475 {
450   - //IGNORE
  476 + // IGNORE
451 477 }
452   -
  478 +
453 479 else
454 480 {
455 481 currentLine++;
456   - throw new WarningException("Tag not implemented: " + extTag[0]);
  482 + throw new WarningException("Tag not implemented: "
  483 + + extTag[0]);
457 484 }
458 485 }
459 486 else
460 487 {
461 488 String comment = line.substring(1);
462   - throw new InfoException("Comment on line " + currentLine++ + " " + comment);
  489 + throw new InfoException("Comment on line "
  490 + + currentLine++ + " " + comment);
463 491 }
464 492 }
465 493 else
466 494 {
467   - if(expectSegment)
  495 + if (expectSegment)
468 496 {
469 497 expectSegment = false;
470 498 try
... ... @@ -477,28 +505,30 @@ public class Parser
477 505 }
478 506 currentSegment++;
479 507 }
480   - else if(expectList)
  508 + else if (expectList)
481 509 {
482 510 /*
483   - * Download list and merge the entries with the current list
  511 + * Download list and merge the entries with the current
  512 + * list
484 513 */
485 514 expectList = false;
486   - Parser p = new Parser(line.substring(0,line.lastIndexOf("/")+1),caller);
  515 + Parser p = new Parser(line.substring(0, line.lastIndexOf("/") + 1), caller);
487 516 List newList = p.parseFile(download.doInBackground(line)).get(0).getLists().get(0);
488 517 pls.setMaxDuration(newList.getMaxDuration());
489 518 pls.setSequence(newList.getSequence());
490   - for(int i = 0; i < newList.getSegments().size(); i++)
  519 + for (int i = 0; i < newList.getSegments().size(); i++)
491 520 {
492 521 pls.getSegments().add(newList.getSegments().get(i));
493 522 }
494 523 }
495 524 else
496 525 {
497   - throw new ErrorException("ERROR: Unexpected string " + line);
  526 + throw new ErrorException("ERROR: Unexpected string "
  527 + + line);
498 528 }
499 529 }
500 530 }
501 531 currentLine++;
502   - }
  532 + }
503 533 }
504 534 }
505 535 \ No newline at end of file
... ...