Directoris.java
5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.upc.pbe.upcnews;
import java.io.IOException;
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;
//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, currentFolder;
private HTMLParser parser;
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 void showResources(){
//Actualitza la informacio que mostra la ListView a mesura que ens desplacem pels directoris
ArrayList<String> entries;
try{
entries = parser.parse(d.doInBackground(currentFolder));
this.createEntries(entries);
}
catch (IOException e){
// Nunca llegara aqui la cosa, y si lo hace ES CULPA DEL SERVIDOR!!
}
}
public void createEntries(ArrayList<String> 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);
ResourceAdapter adapter = new ResourceAdapter(this, R.layout.rowlayout, entries);
listView.setAdapter(adapter);
}
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();
//Si es un m3u8, el descarrega, parseja i inicia la reproduccio
if(path.endsWith(".m3u8")){
path = currentFolder + path;
String playlist;
try{
//Descarreguem m3u8
playlist = d.doInBackground(path);
Parser p = new Parser(path.substring(0, path.lastIndexOf("/") + 1), this);
try{
//Parsing m3u8
ArrayList<ParentList> m3u8parsed = p.parseFile(playlist);
Log.d(TAG, "Parsing completat");
//Creem un gestor HLS
HLS h = new HLS(m3u8parsed,((UpcApp)getApplication()).getLocalPath());
((UpcApp)getApplication()).setHLS(h);
//Iniciem la reproduccio
Intent mIntent = new Intent(this, VideoActivity.class);
if(((UpcApp)getApplication()).getHLS() != null) {
startActivity(mIntent);
}
else {
Toast.makeText(this, "Error en gestor HLS", Toast.LENGTH_LONG).show();
}
}
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());
}
}
catch (IOException e1){
// Nunca llegara aqui la cosa, y si lo hace ES CULPA DEL SERVIDOR!!
}
}
//Si es una carpeta, hi entra i mostra el seu contingut
else{
if(!path.startsWith("http://")){
currentFolder += path;
}
else{
currentFolder = path;
}
showResources();
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
//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;
}
currentFolder = currentFolder.substring(0, currentFolder.length()-1);
currentFolder = currentFolder.substring(0,currentFolder.lastIndexOf("/")+1);
showResources();
return true;
}
return super.onKeyDown(keyCode, event);
}
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;
}
}
}