Blame view

src/com/upc/pbe/upcnews/Directoris.java 5.89 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
25
26
27
public class Directoris extends Activity implements OnItemClickListener
{
28
	final static String TAG = "Directoris";
Imanol-Mikel Barba Sabariego authored
29
	private String URL, currentFolder;
30
	private HTMLParser parser;
Imanol-Mikel Barba Sabariego authored
31
32
33
34
	public void onCreate(Bundle savedInstanceState)
	{
		// Creem el layout
Imanol-Mikel Barba Sabariego authored
35
		Log.d(TAG, "onCreated");
36
		super.onCreate(savedInstanceState);
37
		// Passem del layout anterior al actual
38
		setContentView(R.layout.dirs);
39
		// Canviem la direccio de la URL a la actual
40
41
		currentFolder = ((UpcApp) getApplication()).getUrl() + "/";
		URL = currentFolder;
42
		// Creem un HTMLParser
43
		parser = new HTMLParser(URL);
44
45
		// Fem una llista ListView i la omplim amb la informacio trobada
		((ListView) findViewById(R.id.listView1)).setOnItemClickListener(this);
46
		this.showResources();
47
	}
48
49
50
51
52

	public void showResources()
	{
		// Actualitza la informacio que mostra la ListView a mesura que ens
		// desplacem pels directoris
53
		ArrayList<String> entries;
54
55
56
57
58
		Descarrega d = new Descarrega(this);
		try
		{
			d.execute(new URL(currentFolder));
			entries = parser.parse(d.get());
59
60
			this.createEntries(entries);
		}
61
62
63
64
65
66
67
68
69
70
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
		catch (ExecutionException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
Imanol-Mikel Barba Sabariego authored
71
			// Nunca llegara aqui la cosa, y si lo hace ES CULPA DEL SERVIDOR!!
72
		}
Imanol-Mikel Barba Sabariego authored
73
	}
74
75
76
77

	public void createEntries(ArrayList<String> directories)
	{
		// Crea les entrades de la ListView
78
79
80
		String[] entries = directories.toArray(new String[directories.size()]);
		ListView listView = (ListView) findViewById(R.id.listView1);
		listView.setAdapter(null);
Imanol-Mikel Barba Sabariego authored
81
		ResourceAdapter adapter = new ResourceAdapter(this, R.layout.rowlayout, entries);
82
83
		listView.setAdapter(adapter);
	}
84
Imanol-Mikel Barba Sabariego authored
85
86
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		//Determina el funcionament al clickar en una de les entrades de al ListView
87
		String path = ((TextView)((RelativeLayout)view).findViewById(R.id.rowTextView)).getText().toString();
Imanol-Mikel Barba Sabariego authored
88
		//Si es un m3u8, el descarrega, parseja i inicia la reproduccio
89
90
		if(path.endsWith(".m3u8"))
		{
91
			path = currentFolder + path;
92
			String playlist;
93
94
95
96
97
98
99
100
101
102
103
104
105
			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
106
						HLS h = new HLS(m3u8parsed,((UpcApp)getApplication()).getLocalPath());
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
						((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());
Imanol-Mikel Barba Sabariego authored
128
					}
129
130
131
132
					catch (InfoException e)
					{
						Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
						Log.d(TAG, e.getMessage());
Imanol-Mikel Barba Sabariego authored
133
					}
134
				}
135
136
137
				catch (IOException e1)
				{
					// Nunca llegara aqui la cosa, y si lo hace ES CULPA DEL SERVIDOR!!
138
				}
139
140
141
				catch (InterruptedException e1)
				{
					e1.printStackTrace();
142
				}
143
144
145
				catch (ExecutionException e1)
				{
					e1.printStackTrace();
146
				}
147
			}
Imanol-Mikel Barba Sabariego authored
148
		//Si es una carpeta, hi entra i mostra el seu contingut
149
150
151
152
		else
		{
			if(!path.startsWith("http://"))
			{
153
154
				currentFolder += path;
			}
155
156
			else
			{
157
158
159
160
				currentFolder = path;
			}
			showResources();
		}
Imanol-Mikel Barba Sabariego authored
161
	}
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

	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);
Imanol-Mikel Barba Sabariego authored
179
	}
180
181
182
183

	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Determina el funcionament al apretar la tecla d'opcions
Imanol-Mikel Barba Sabariego authored
184
185
186
187
188
		getMenuInflater().inflate(R.menu.menu, menu);
		Log.d(TAG, "Menu");
		return true;
	}
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
	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;
Imanol-Mikel Barba Sabariego authored
204
205
		}
	}
206
207
208
209
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}
Imanol-Mikel Barba Sabariego authored
210
}