diff --git a/src/com/upc/pbe/upcnews/Descarrega.java b/src/com/upc/pbe/upcnews/Descarrega.java index 89b1047..b7971c0 100644 --- a/src/com/upc/pbe/upcnews/Descarrega.java +++ b/src/com/upc/pbe/upcnews/Descarrega.java @@ -10,7 +10,11 @@ import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; +import android.app.DownloadManager; +import android.app.DownloadManager.Request; +import android.net.Uri; import android.os.AsyncTask; +import android.os.Environment; import android.util.Log; //Descarrega un arxiu i el guarda o retorna en String @@ -18,11 +22,16 @@ public class Descarrega extends AsyncTask { final static String TAG = "Descarrega"; private String html; + DownloadManager manager; public Descarrega() { html = ""; } + + public Descarrega(DownloadManager man){ + manager = man; + } public void descarregarguardar(String url, String path) throws IOException { @@ -49,7 +58,17 @@ public class Descarrega extends AsyncTask out.close(); in.close(); + } + public void descarrega(String url, String path, String name) throws IOException{ + + Request req = new Request(Uri.parse(url)); + + //req.setVisibleInDownloadsUi(false); + req.setDestinationInExternalPublicDir(path, name); + manager.enqueue(req); + Log.d(TAG, "\nDescarregant: \n"); + Log.d(TAG, " >> URL: " + url + " >> Desti: " + path); } diff --git a/src/com/upc/pbe/upcnews/Directoris.java b/src/com/upc/pbe/upcnews/Directoris.java index c112a70..e037c49 100644 --- a/src/com/upc/pbe/upcnews/Directoris.java +++ b/src/com/upc/pbe/upcnews/Directoris.java @@ -4,6 +4,8 @@ import java.io.IOException; import java.util.ArrayList; import android.app.Activity; +import android.app.DownloadManager; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; @@ -12,9 +14,9 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; 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 @@ -78,7 +80,8 @@ public class Directoris extends Activity implements OnItemClickListener { ArrayList m3u8parsed = p.parseFile(playlist); Log.d(TAG, "Parsing completat"); //Creem un gestor HLS - HLS h = new HLS(m3u8parsed,((UpcApp)getApplication()).getLocalPath()); + DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); + HLS h = new HLS(m3u8parsed,((UpcApp)getApplication()).getLocalPath(), manager); ((UpcApp)getApplication()).setHLS(h); //Iniciem la reproduccio Intent mIntent = new Intent(this, VideoActivity.class); diff --git a/src/com/upc/pbe/upcnews/HLS.java b/src/com/upc/pbe/upcnews/HLS.java index e3b45c2..9d3cda2 100644 --- a/src/com/upc/pbe/upcnews/HLS.java +++ b/src/com/upc/pbe/upcnews/HLS.java @@ -3,6 +3,8 @@ package com.upc.pbe.upcnews; import java.io.File; import java.io.IOException; import java.util.ArrayList; + +import android.app.DownloadManager; import android.net.TrafficStats; import android.util.Log; @@ -22,7 +24,8 @@ public class HLS private Descarrega d; private BandwidthMeasurer bm; - public HLS(ArrayList parsed, String localFolder) + + public HLS(ArrayList parsed, String localFolder, DownloadManager manager) { // Neteja el directori i inicialitza les variables File dir = new File(localFolder); @@ -39,7 +42,8 @@ public class HLS endReached = false; this.localFolder = localFolder; bm = new BandwidthMeasurer(); - d = new Descarrega(); + + d = new Descarrega(manager); } public void loadVideo() diff --git a/src/com/upc/pbe/upcnews/MainActivity.java b/src/com/upc/pbe/upcnews/MainActivity.java index a3fb9f0..aaa0c1b 100644 --- a/src/com/upc/pbe/upcnews/MainActivity.java +++ b/src/com/upc/pbe/upcnews/MainActivity.java @@ -7,12 +7,10 @@ import java.util.List; import android.app.Activity; import android.app.DownloadManager; -import android.app.DownloadManager.Request; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; -import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.util.Log; @@ -35,6 +33,7 @@ public class MainActivity extends Activity implements OnClickListener { private TextView URLText, alertaText; private boolean libsFound = false; + @Override public void onCreate(Bundle savedInstanceState) { @@ -48,14 +47,16 @@ public class MainActivity extends Activity implements OnClickListener { Log.d(TAG,"Directori " + tempFolder.toString() + " creat"); } - String url = "http://revistes.upc.es/~imanol/PBE/sample_ep_128k-00001.ts"; - Request req = new Request(Uri.parse(url)); - req.setTitle("TestTitle"); - req.setDescription("TestDescription"); - //req.setVisibleInDownloadsUi(false); - req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "a.ts"); + //Prova descarrega DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); - manager.enqueue(req); + Descarrega desc = new Descarrega(manager); + try { + desc.descarrega("http://revistes.upc.es/~imanol/PBE/sample_ep_128k-00004.ts", Environment.DIRECTORY_DOWNLOADS, "marc.ts"); + } catch (IOException e) { + e.printStackTrace(); + } + + //Creem els botons i text