Blame view

src/com/upc/pbe/upcnews/Directoris.java 5.8 KB
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;
Imanol-Mikel Barba Sabariego authored
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;
Imanol-Mikel Barba Sabariego authored
14
15
import android.view.Menu;
import android.view.MenuItem;
Imanol-Mikel Barba Sabariego authored
16
import android.view.View;
17
import android.widget.AdapterView;
Imanol-Mikel Barba Sabariego authored
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
Imanol-Mikel Barba Sabariego authored
24
//Segona Activity, mostra els directoris i arxius del servidor
Imanol-Mikel Barba Sabariego authored
25
public class Directoris extends Activity implements OnItemClickListener {
26
27
	final static String TAG = "Directoris";
Imanol-Mikel Barba Sabariego authored
28
	private String URL, currentFolder;
29
	private HTMLParser parser;
Imanol-Mikel Barba Sabariego authored
30
Imanol-Mikel Barba Sabariego authored
31
	public void onCreate(Bundle savedInstanceState) {
32
		// Creem el layout
Imanol-Mikel Barba Sabariego authored
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
Imanol-Mikel Barba Sabariego authored
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);
Imanol-Mikel Barba Sabariego authored
52
		try {
53
54
			d.execute(new URL(currentFolder));
			entries = parser.parse(d.get());
55
			this.createEntries(entries);
Imanol-Mikel Barba Sabariego authored
56
		} catch (InterruptedException e) {
57
			e.printStackTrace();
Imanol-Mikel Barba Sabariego authored
58
		} catch (ExecutionException e) {
59
			e.printStackTrace();
Imanol-Mikel Barba Sabariego authored
60
		} catch (IOException e) {
Imanol-Mikel Barba Sabariego authored
61
			// Nunca llegara aqui la cosa, y si lo hace ES CULPA DEL SERVIDOR!!
62
		}
Imanol-Mikel Barba Sabariego authored
63
	}
64
Imanol-Mikel Barba Sabariego authored
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);
Imanol-Mikel Barba Sabariego authored
70
71
		ResourceAdapter adapter = new ResourceAdapter(this, R.layout.rowlayout,
				entries);
72
73
		listView.setAdapter(adapter);
	}
74
Imanol-Mikel Barba Sabariego authored
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;
Imanol-Mikel Barba Sabariego authored
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
					}
Imanol-Mikel Barba Sabariego authored
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
				}
Imanol-Mikel Barba Sabariego authored
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
			}
Imanol-Mikel Barba Sabariego authored
129
130
131
132
		}
		// Si es una carpeta, hi entra i mostra el seu contingut
		else {
			if (!path.startsWith("http://")) {
133
				currentFolder += path;
Imanol-Mikel Barba Sabariego authored
134
			} else {
135
136
137
138
				currentFolder = path;
			}
			showResources();
		}
Imanol-Mikel Barba Sabariego authored
139
	}
140
Imanol-Mikel Barba Sabariego authored
141
	public boolean onKeyDown(int keyCode, KeyEvent event) {
142
		// Determina el funcionament al apretar la tecla de tornar enrere
Imanol-Mikel Barba Sabariego authored
143
144
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			if (currentFolder.equals(URL)) {
145
146
147
				super.finish();
				return true;
			}
Imanol-Mikel Barba Sabariego authored
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);
Imanol-Mikel Barba Sabariego authored
156
	}
157
Imanol-Mikel Barba Sabariego authored
158
	public boolean onCreateOptionsMenu(Menu menu) {
159
		// Determina el funcionament al apretar la tecla d'opcions
Imanol-Mikel Barba Sabariego authored
160
161
162
163
164
		getMenuInflater().inflate(R.menu.menu, menu);
		Log.d(TAG, "Menu");
		return true;
	}
Imanol-Mikel Barba Sabariego authored
165
	public boolean onOptionsItemSelected(MenuItem item) {
166
		// Determina el funcionament al clickar en el menu d'opcions
Imanol-Mikel Barba Sabariego authored
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;
Imanol-Mikel Barba Sabariego authored
178
179
		}
	}
Imanol-Mikel Barba Sabariego authored
180
181
182
183
184
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}
Imanol-Mikel Barba Sabariego authored
185
}