|
1
2
|
package com.upc.pbe.upcnews;
|
|
3
|
import java.io.IOException;
|
|
4
|
import java.net.URL;
|
|
5
|
import java.util.ArrayList;
|
|
6
|
import java.util.concurrent.ExecutionException;
|
|
7
8
|
import android.app.Activity;
|
|
9
|
import android.content.Intent;
|
|
10
|
import android.content.res.Configuration;
|
|
11
12
|
import android.os.Bundle;
import android.util.Log;
|
|
13
|
import android.view.KeyEvent;
|
|
14
15
|
import android.view.Menu;
import android.view.MenuItem;
|
|
16
|
import android.view.View;
|
|
17
|
import android.widget.AdapterView;
|
|
18
|
import android.widget.AdapterView.OnItemClickListener;
|
|
19
|
import android.widget.ListView;
|
|
20
|
import android.widget.RelativeLayout;
|
|
21
|
import android.widget.TextView;
|
|
22
|
import android.widget.Toast;
|
|
23
|
|
|
24
|
//Segona Activity, mostra els directoris i arxius del servidor
|
|
25
|
public class Directoris extends Activity implements OnItemClickListener {
|
|
26
|
|
|
27
|
final static String TAG = "Directoris";
|
|
28
|
private String URL, currentFolder;
|
|
29
|
private HTMLParser parser;
|
|
30
|
|
|
31
|
public void onCreate(Bundle savedInstanceState) {
|
|
32
|
// Creem el layout
|
|
33
|
Log.d(TAG, "onCreated");
|
|
34
|
super.onCreate(savedInstanceState);
|
|
35
|
// Passem del layout anterior al actual
|
|
36
|
setContentView(R.layout.dirs);
|
|
37
|
// Canviem la direccio de la URL a la actual
|
|
38
39
|
currentFolder = ((UpcApp) getApplication()).getUrl() + "/";
URL = currentFolder;
|
|
40
|
// Creem un HTMLParser
|
|
41
|
parser = new HTMLParser(URL);
|
|
42
43
|
// Fem una llista ListView i la omplim amb la informacio trobada
((ListView) findViewById(R.id.listView1)).setOnItemClickListener(this);
|
|
44
|
this.showResources();
|
|
45
|
}
|
|
46
|
|
|
47
|
public void showResources() {
|
|
48
49
|
// Actualitza la informacio que mostra la ListView a mesura que ens
// desplacem pels directoris
|
|
50
|
ArrayList<String> entries;
|
|
51
|
Descarrega d = new Descarrega(this);
|
|
52
|
try {
|
|
53
54
|
d.execute(new URL(currentFolder));
entries = parser.parse(d.get());
|
|
55
|
this.createEntries(entries);
|
|
56
|
} catch (InterruptedException e) {
|
|
57
|
e.printStackTrace();
|
|
58
|
} catch (ExecutionException e) {
|
|
59
|
e.printStackTrace();
|
|
60
|
} catch (IOException e) {
|
|
61
|
// Nunca llegara aqui la cosa, y si lo hace ES CULPA DEL SERVIDOR!!
|
|
62
|
}
|
|
63
|
}
|
|
64
|
|
|
65
|
public void createEntries(ArrayList<String> directories) {
|
|
66
|
// Crea les entrades de la ListView
|
|
67
68
69
|
String[] entries = directories.toArray(new String[directories.size()]);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(null);
|
|
70
71
|
ResourceAdapter adapter = new ResourceAdapter(this, R.layout.rowlayout,
entries);
|
|
72
73
|
listView.setAdapter(adapter);
}
|
|
74
|
|
|
75
76
77
78
79
80
81
82
|
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) ((RelativeLayout) view)
.findViewById(R.id.rowTextView)).getText().toString();
// Si es un m3u8, el descarrega, parseja i inicia la reproduccio
if (path.endsWith(".m3u8")) {
|
|
83
|
path = currentFolder + path;
|
|
84
|
String playlist;
|
|
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
try {
Descarrega d = new Descarrega(this);
// Descarreguem m3u8
d.execute(new URL(path));
playlist = d.get();
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();
|
|
107
|
}
|
|
108
109
110
111
112
113
114
115
116
117
118
119
|
} 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());
|
|
120
|
}
|
|
121
122
123
124
125
126
127
|
} catch (IOException e1) {
// Nunca llegara aqui la cosa, y si lo hace ES CULPA DEL
// SERVIDOR!!
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (ExecutionException e1) {
e1.printStackTrace();
|
|
128
|
}
|
|
129
130
131
132
|
}
// Si es una carpeta, hi entra i mostra el seu contingut
else {
if (!path.startsWith("http://")) {
|
|
133
|
currentFolder += path;
|
|
134
|
} else {
|
|
135
136
137
138
|
currentFolder = path;
}
showResources();
}
|
|
139
|
}
|
|
140
|
|
|
141
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
142
|
// Determina el funcionament al apretar la tecla de tornar enrere
|
|
143
144
|
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
if (currentFolder.equals(URL)) {
|
|
145
146
147
|
super.finish();
return true;
}
|
|
148
149
150
151
|
currentFolder = currentFolder.substring(0,
currentFolder.length() - 1);
currentFolder = currentFolder.substring(0,
currentFolder.lastIndexOf("/") + 1);
|
|
152
153
154
155
|
showResources();
return true;
}
return super.onKeyDown(keyCode, event);
|
|
156
|
}
|
|
157
|
|
|
158
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
159
|
// Determina el funcionament al apretar la tecla d'opcions
|
|
160
161
162
163
164
|
getMenuInflater().inflate(R.menu.menu, menu);
Log.d(TAG, "Menu");
return true;
}
|
|
165
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
166
|
// Determina el funcionament al clickar en el menu d'opcions
|
|
167
|
switch (item.getItemId()) {
|
|
168
169
170
171
172
173
174
175
176
177
|
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;
|
|
178
179
|
}
}
|
|
180
|
|
|
181
182
183
184
|
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
|
|
185
|
}
|