<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" version="2.0">

<channel>
	<title>Paolo Casarini</title>
	
	<link>http://www.casarini.org/blog</link>
	<description>happy programming</description>
	<pubDate>Tue, 27 Sep 2011 05:52:58 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
	<language>en</language>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/casarini" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="casarini" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Perché javascript migliora l’accessibilità di EPUB 3</title>
		<link>http://www.casarini.org/blog/2011/perche-javascript-migliora-laccessibilita-di-epub-3/</link>
		<comments>http://www.casarini.org/blog/2011/perche-javascript-migliora-laccessibilita-di-epub-3/#comments</comments>
		<pubDate>Sun, 06 Mar 2011 20:33:56 +0000</pubDate>
		<dc:creator>paolo</dc:creator>
		
		<category><![CDATA[digital publishing]]></category>

		<category><![CDATA[eBook]]></category>

		<category><![CDATA[EPUB 3]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[MathJax]]></category>

		<guid isPermaLink="false">http://www.casarini.org/blog/?p=525</guid>
		<description><![CDATA[Di recente è uscito un post di Strahinja Marković che mette sotto accusa l&#8217;introduzione di javascript nel formato EPUB 3. In un primo momento mi sono trovato d&#8217;accordo con le posizioni sostenute in tale post, ma poi mi sono accorto che, per chi ha a cuore i libri scolastici o i manuali universitari del futuro, [...]]]></description>
			<content:encoded><![CDATA[<p>Di recente è uscito un <a href="http://sigildev.blogspot.com/2011/02/analysis-of-epub3-and-uh-bit-more.html">post</a> di Strahinja Marković che mette sotto accusa l&#8217;introduzione di javascript nel formato <code>EPUB 3</code>. In un primo momento mi sono trovato d&#8217;accordo con le posizioni sostenute in tale post, ma poi mi sono accorto che, per chi ha a cuore i libri scolastici o i manuali universitari del futuro, può trovare in javascript un grande alleato, sia per far evolvere il libro da punto di vista didattico (come ho spiegato in un precedente <a href="/blog/2011/grafici-interattivi-nei-libri-del-futuro-grazie-ad-epub-3-e-jsxgraph/">articolo</a>), sia per migliorare la sua accessibilità.</p>
<p>L&#8217;esempio che porterò è quello delle formule: non c&#8217;è nulla di meno accessibile di una formula in un libro digitale realizzata con una immagine bitmap. Anche in testi non matematici quando si usa una formula, tale elemento è spesso chiave per la comprensione di un concetto e l&#8217;impossibilità di poterla leggere può rendere un testo inutile a fini didattici.</p>
<p><span id="more-525"></span><br />
Per risolvere il problema dell&#8217;accessibilità delle formule, nel recente <a href="http://idpf.org/epub/30"><em>draft</em> del formato <code>EPUB 3</code></a> è stato introdotta la possibilità di rappresentare queste ultime nel formato MathML:</p>
<blockquote><p>
<strong>2.1.4.1 Embedded MathML</strong><br />
<em>› 2.1.4.1.1 Introduction</em></p>
<p>XHTML Content Documents support embedded MathML <a href="http://www.w3.org/TR/MathML/">[MATHML]</a> but limit its usage to a restricted subset of the full MathML markup language.</p>
<p>This subset is designed to ease the implementation burden on Reading Systems and to promote accessibility, while retaining compatibility with HTML5 User Agents <a href="http://www.w3.org/TR/html5/">[HTML5]</a>.
</p></blockquote>
<p>Se però in questo modo abbiamo finalmente la possibilità di rappresentare le formule in un linguaggio formale tale da rendere l&#8217;interno testo leggibile da chiunque, con ogni probabilità rendiamo le cose più difficili alla stragrande maggioranza degli studenti poiché non vi è nessun eReader attualmente in commercio in grado di disegnare nativamente MathML. Sappiamo infatti che non basta che una possibilità tecnica venga messa a disposizione per fare in modo che questa venga utilizzata e correttamente gestita. Per fare un esempio nel campo ben più competitivo dei browser Internet, basta pensare al fatto che attualmente solo Mozilla Firefox è in grado di disegnare correttamente MathML.</p>
<p>Disegnare formule non è un mestiere semplice e la non disponibilità di eReader in grado di mostrare correttamente MathML all&#8217;interno di libri in formato <code>EPUB 3</code> potrebbe portare gli editori a continuare a realizzare le formule facendo l&#8217;embedding di immagini bitmap.</p>
<p>Per colmare il gap tra la possibilità tecnica offerta dal nuovo standard e l&#8217;effettiva capacità degli eReader di supportare tale nuovo formato, ci può venire in aiuto <a href="http://www.mathjax.org/">MathJax</a>, libreria <em>javascript</em> che funziona piuttosto bene e capace di renderizzare formule espresse in Tex e MathML all&#8217;interno di codice HTML5. In questo senso, la possibilità di inserire del codice <em>javascript</em> all&#8217;interno dei libri in formato <code>EPUB 3</code> permetterà di sfruttare immediatamente la possibilità di descrivere le formule in un linguaggio formale e di creare, quindi, fin da subito, materiale didattico totalmente accessibile.</p>
<p>Per fare qualche esempio di come potrebbe risultare un testo di nuova generazione con delle formule, di seguito scriverò due formule in MathML che verranno disegnate dalla libreria MathJax. La libreria, opportunamente configurata, intercetta automaticamente le formule all&#8217;interno del codice HTML e si occupa del loro rendering senza modificare l&#8217;accessibilità del documento.</p>
<p>Formula per la soluzione di una equazione di secondo grado:</p>
<math display='block'><mrow><mi>x</mi><mo>=</mo><mfrac><mrow><mo>&#x2212;</mo><mi>b</mi><mo>&#x00B1;</mo><msqrt><mrow><msup><mi>b</mi><mn>2</mn></msup><mo>&#x2212;</mo><mn>4</mn><mi>a</mi><mi>c</mi></mrow></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mrow></math>
<p>Legge di gravitazione universale:</p>
<math display='block'><mi>F</mi><mo>=</mo><mi>G</mi><mfrac><mrow><msub><mi>m</mi><mn>1</mn></msub><msub><mi>m</mi><mn>2</mn></msub></mrow><msup><mi>r</mi><mn>2</mn</msup></mfrac></math>
<p><br/><br />
<br/></p>
]]></content:encoded>
			<wfw:commentRss>http://www.casarini.org/blog/2011/perche-javascript-migliora-laccessibilita-di-epub-3/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Grafici interattivi nei libri del futuro grazie ad EPUB 3 e JSXGraph</title>
		<link>http://www.casarini.org/blog/2011/grafici-interattivi-nei-libri-del-futuro-grazie-ad-epub-3-e-jsxgraph/</link>
		<comments>http://www.casarini.org/blog/2011/grafici-interattivi-nei-libri-del-futuro-grazie-ad-epub-3-e-jsxgraph/#comments</comments>
		<pubDate>Sat, 05 Mar 2011 22:11:52 +0000</pubDate>
		<dc:creator>paolo</dc:creator>
		
		<category><![CDATA[digital publishing]]></category>

		<category><![CDATA[eBook]]></category>

		<category><![CDATA[EPUB 3]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[JSXGraph]]></category>

		<guid isPermaLink="false">http://www.casarini.org/blog/?p=488</guid>
		<description><![CDATA[Nel dibattito tra governo ed editori, si parla spesso di digitalizzazione dei libri scolastici e spesso questo dibattito si ferma all&#8217;analisi dei costi, ovvero di quanto i libri debbano costare meno perché non stampati su carta, senza entrare nel merito di come un libro scolastico dovrebbe essere uno strumento migliore per la didattica sfruttando i [...]]]></description>
			<content:encoded><![CDATA[<p>Nel dibattito tra governo ed editori, si parla spesso di digitalizzazione dei libri scolastici e spesso questo dibattito si ferma all&#8217;analisi dei costi, ovvero di quanto i libri debbano costare meno perché non stampati su carta, senza entrare nel merito di come un libro scolastico dovrebbe essere uno strumento migliore per la didattica sfruttando i potenziali offerti dai nuovi media.</p>
<p>Fino ad ora era difficile pensare a un libro scolastico in formato digitale che non fosse il pdf preparato per la stampa poiché i formati alternativi, come il formato ePub o il formato Mobipocket, non erano assolutamente all&#8217;altezza di un compito difficile come quello di rappresentare la complessità espositiva di un libro scolastico.</p>
<p>Di recente, però, è stato presentato il primo <a href="http://idpf.org/epub/30"><em>draft</em> del formato <code>EPUB 3</code></a>, evoluzione del formato che attualmente viene utilizzato per la vendita di e-book dai principali editori e piattaforme per il delivery di libri in formato digitale.</p>
<p>Tale evoluzione introduce numerosi elementi innovativi in questo campo che permettono di pensare al libro scolastico o al manuale universitario in modo nuovo, creando i presupposti per far evolvere lo strumento libro anche dal punto di vista didattico.<br />
In questa direzione un esempio è quello di integrare apparati dinamici come possono essere i grafici di una funzione matematica alla quale si possono cambiare i parametri che ne determinano l&#8217;andamento.</p>
<p><span id="more-488"></span><br />
Dal punto di vista tecnico ciò che ci permette di poter ipotizzare tali interazioni è la possibilità di inserire all&#8217;interno di libri in formato <code>EPUB 3</code> del codice <em>javascript</em>, come viene descritto nelle <a href="http://idpf.org/epub/30/spec/epub30-contentdocs.html#sec-scripted-content">specifiche</a> attuali del formato:</p>
<blockquote><p>
<strong>2.4.1 Introduction</strong></p>
<p>EPUB Content Documents may contain scripting using the facilities defined for this in the respective underlying specifications ([HTML5] and [SVG]). When an EPUB Content Document contains scripting, it is referred to in this specification and its sibling specifications as a Scripted Content Document.
</p></blockquote>
<p>Per fare un esempio concreto pensiamo alla possibilità di poter controllare le variabili in gioco in una delle più comuni curve che si studiano in geometria analitica e in analisi matematica come la parabola. L&#8217;espressione per rappresentare matematicamente la parabola è piuttosto semplice ma per capire come possa essere disegnata la curva nel piano in base al segno e ai valori delle variabili in gioco, i libri stampati, solitamente propongono diversi grafici statici (almeno 3 o 4).</p>
<p>Utilizzando un grafico interattivo realizzato, ad esempio, con la libreria javascript <a href="http://jsxgraph.uni-bayreuth.de/wp/">JSXGraph</a>, è possibile esprimere la formula della parabola generica:</p>
<dd><img src="https://chart.googleapis.com/chart?cht=tx&#038;chl=y%20%3D%20ax%5E2%20%2B%20bx%20%2Bc" alt="y = ax^2 + bx +c"/></p>
<p>parametrizzandone il disegno del grafico rispetto ai parametri <strong><em>a</em></strong>, <strong><em>b</em></strong> , <strong><em>c</em></strong></p>
<pre><div id='box' class='jxgbox' style='width:500px; height:500px;'></div><script type='text/javascript'>

var brd = JXG.JSXGraph.initBoard('box',{boundingbox:[-5,5,5,-5], keepaspectratio:true, axis:true});
brd.suspendUpdate();
var a = brd.create('slider',[[0.5,-3],[3.5,-3],[-5,1,5]], {name:'a'});
var b = brd.create('slider',[[0.5,-3.5],[3.5,-3.5],[-5,0,5]], {name:'b'});
var c = brd.create('slider',[[0.5,-4],[3.5,-4],[-5,0.5,5]], {name:'c'});
var p = brd.create('curve',
             [function(t){ return t},
              function(t){ return a.Value()*t*t + b.Value()*t + c.Value();},
              -20, 20
             ],
             {strokeWidth:1, strokeColor:'black'});
brd.unsuspendUpdate()
</script></pre>
<p>I vantaggi di utilizzo, dal punto di vista didattico, di un apparato di questo genere penso siano immediati sia per l&#8217;uso che si può fare dell&#8217;e-book in classe, sia per lo studio individuale. Concetti astratti come lo spostamento del fuoco della parabola e l&#8217;inversione del verso della concavità sono sperimentabili direttamente su grafico offrendo una lettura visiva immediata con notevoli benefici sulla semplicità di apprendimento.</p>
<p>Questo è solo un primo esempio delle potenzialità che il nuovo formato <code>EPUB 3</code> ha introdotto. Se fino a poco tempo fa non era possibile immaginare una evoluzione dell&#8217;e-book tale da poter rispondere alle esigenze didattiche attuali, ora è tecnicamente possibile e starà agli editori saper sfruttare tali potenzialità e alle istituzioni creare il contesto per cui l&#8217;evoluzione del libro elettronico come strumento didattico migliore del libro stampato abbia luogo.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.casarini.org/blog/2011/grafici-interattivi-nei-libri-del-futuro-grazie-ad-epub-3-e-jsxgraph/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Android local HTML viewer</title>
		<link>http://www.casarini.org/blog/2011/android-local-html-viewer/</link>
		<comments>http://www.casarini.org/blog/2011/android-local-html-viewer/#comments</comments>
		<pubDate>Sat, 29 Jan 2011 17:15:29 +0000</pubDate>
		<dc:creator>paolo</dc:creator>
		
		<category><![CDATA[programming]]></category>

		<category><![CDATA[android]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[WebView]]></category>

		<guid isPermaLink="false">http://www.casarini.org/blog/?p=478</guid>
		<description><![CDATA[I recently made a very simple Activity to display an HTML file stored within an Android app. This task could be useful to add an help page to your app without dealing with layouts.
First you need the HTML file you want to display and you have to put it in the res/raw directory of your [...]]]></description>
			<content:encoded><![CDATA[<p>I recently made a very simple <em>Activity</em> to display an HTML file stored within an Android app. This task could be useful to add an help page to your app without dealing with layouts.</p>
<p>First you need the HTML file you want to display and you have to put it in the <code>res/raw</code> directory of your project. We'll access to this directory with the <code>getResources().openRawResource(int id)</code> method.</p>
<p>Now we need a widget that is able to display an HTML string like the <code>android.webkit.WebView</code> and a layout with this widget inside that will be used by our  <em>Activity</em>:</p>
<div class="igBar"><span id="lxml-4"><a href="#" onclick="javascript:showPlainTxt('xml-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-4">
<div class="xml">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.0"</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">"utf-8"</span>?<span style="font-weight: bold; color: black;">&gt;</span></span>&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;LinearLayout</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; xmlns:<span style="color: #000066;">android</span>=<span style="color: #ff0000;">"http://schemas.android.com/apk/res/android"</span>&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; android:<span style="color: #000066;">orientation</span>=<span style="color: #ff0000;">"vertical"</span>&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; android:layout_<span style="color: #000066;">width</span>=<span style="color: #ff0000;">"fill_parent"</span>&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; android:layout_<span style="color: #000066;">height</span>=<span style="color: #ff0000;">"fill_parent"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&nbsp; &nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;WebView</span> android:<span style="color: #000066;">id</span>=<span style="color: #ff0000;">"@+id/web_engine"</span>&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; android:layout_<span style="color: #000066;">width</span>=<span style="color: #ff0000;">"fill_parent"</span>&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; android:layout_<span style="color: #000066;">height</span>=<span style="color: #ff0000;">"fill_parent"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/LinearLayout<span style="font-weight: bold; color: black;">&gt;</span></span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><br/><br />
<span id="more-478"></span><br />
Finally, we can implement an <em>Activity</em> that loads our HTML file from resources when it starts putting in the <code>onCreate</code> method something like the following:</p>
<div class="igBar"><span id="ljava-5"><a href="#" onclick="javascript:showPlainTxt('java-5'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-5">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">WebView engine = <span style="color: #66cc66;">&#40;</span>WebView<span style="color: #66cc66;">&#41;</span> findViewById<span style="color: #66cc66;">&#40;</span>R.<span style="color: #006600;">id</span>.<span style="color: #006600;">web_engine</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.google.com/search?q=allinurl%3AInputStream+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">InputStream</span></a> in = getResources<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">openRawResource</span><span style="color: #66cc66;">&#40;</span>rawFileRes<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>in != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> size = in.<span style="color: #006600;">available</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> buffer = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span>size<span style="color: #66cc66;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; in.<span style="color: #006600;">read</span><span style="color: #66cc66;">&#40;</span>buffer<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; in.<span style="color: #006600;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; htmldata = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#40;</span>buffer<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AIOException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> ioe<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006600;">e</span><span style="color: #66cc66;">&#40;</span>TAG, <span style="color: #ff0000;">"Error while loading the raw HTML resource : "</span> + ioe.<span style="color: #006600;">getMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">engine.<span style="color: #006600;">loadData</span><span style="color: #66cc66;">&#40;</span>htmldata, mimetype, encoding<span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><br/><br />
To be more general I implemented this <em>Activity</em> to accept two parameters:<br />
 * a title to put as Activity title;<br />
 * a resId that identify the specific HTML resource to load.<br />
You can find the complete source of this <em>Activity</em> here: <a href="/blog/wp-content/uploads/2011/01/HtmlViewer.java">HtmlViewer.java</a></p>
<p>Now that our work is finished, assuming that the HTML file that we want to display is <code>res/raw/quickref.html</code>, we can invoke our HTML Viewer via <em>Intent</em> with somthing like this:</p>
<div class="igBar"><span id="ljava-6"><a href="#" onclick="javascript:showPlainTxt('java-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-6">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Intent i = <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, HtmlViewer.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">i.<span style="color: #006600;">putExtra</span><span style="color: #66cc66;">&#40;</span>HtmlViewer.<span style="color: #006600;">TITLERES_PARAM</span>, <span style="color: #ff0000;">"Quick Reference"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">i.<span style="color: #006600;">putExtra</span><span style="color: #66cc66;">&#40;</span>HtmlViewer.<span style="color: #006600;">RAWFILERES_PARAM</span>, R.<span style="color: #006600;">raw</span>.<span style="color: #006600;">quickref</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">startActivity<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
<br/></p>
]]></content:encoded>
			<wfw:commentRss>http://www.casarini.org/blog/2011/android-local-html-viewer/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Adding delay to C port knocking client</title>
		<link>http://www.casarini.org/blog/2011/adding-delay-to-c-port-knocking-client/</link>
		<comments>http://www.casarini.org/blog/2011/adding-delay-to-c-port-knocking-client/#comments</comments>
		<pubDate>Sat, 29 Jan 2011 15:46:28 +0000</pubDate>
		<dc:creator>paolo</dc:creator>
		
		<category><![CDATA[4]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[C]]></category>

		<category><![CDATA[port-knocking]]></category>

		<guid isPermaLink="false">http://www.casarini.org/blog/?p=469</guid>
		<description><![CDATA[I often use the C port knocking client developed by Judd Vinet and I also took inspiration from its code to write a PHP implementation of that.
I recently noticed that, sometimes and more often when I use it on 3G Internet connections, the port knocking authentication fails because (I suppose) the connects tried are too [...]]]></description>
			<content:encoded><![CDATA[<p>I often use the C port knocking client developed by <a href="http://www.zeroflux.org/projects/knock">Judd Vinet</a> and I also took inspiration from its code to write a <a href="/blog/free-software/phpknockclient/">PHP implementation</a> of that.</p>
<p>I recently noticed that, sometimes and more often when I use it on 3G Internet connections, the port knocking authentication fails because (I suppose) the <code>connects</code> tried are too fast and arrive at server in the wrong order. So I thought that inserting a delay between connections could help and, based on my experience, it works great!</p>
<p>So I patched the <a href="http://www.zeroflux.org/proj/knock/files/knock-0.5.tar.gz">0.5 version</a> of Judd's code to add a new parameter (-d) to specify a delay in millis between connections.</p>
<p><span id="more-469"></span></p>
<p>Download binaries:</p>
<ul>
<li><a href="/blog/wp-content/uploads/2011/01/knock.macosx">MacOSX (x86) patched Client</a></li>
<li><a href="/blog/wp-content/uploads/2011/01/knock.linux64">Linux 64bit (x86) patched Client</a></li>
</ul>
<p>And this is the patch to apply to the original code:<br />
<code><br />
*** knock-0.5/src/knock.c	2005-06-27 07:11:34.000000000 +0200<br />
--- knock-0.5.1/src/knock.c	2011-01-19 22:07:30.000000000 +0100<br />
***************<br />
*** 32,38 ****<br />
  #include <getopt.h><br />
  #include <fcntl.h></p>
<p>! static char version[] = "0.5";</p>
<p>  #define PROTO_TCP 1<br />
  #define PROTO_UDP 2<br />
--- 32,38 ----<br />
  #include <getopt.h><br />
  #include <fcntl.h></p>
<p>! static char version[] = "0.5.1";</p>
<p>  #define PROTO_TCP 1<br />
  #define PROTO_UDP 2<br />
***************<br />
*** 44,49 ****<br />
--- 44,50 ----</p>
<p>  int o_verbose = 0;<br />
  int o_udp     = 0;<br />
+ int o_delay   = 0;</p>
<p>  int main(int argc, char** argv)<br />
  {<br />
***************<br />
*** 55,66 ****<br />
  	{<br />
  		{"verbose",   no_argument,       0, 'v'},<br />
  		{"udp",       no_argument,       0, 'u'},<br />
  		{"help",      no_argument,       0, 'h'},<br />
  		{"version",   no_argument,       0, 'V'},<br />
  		{0, 0, 0, 0}<br />
  	};</p>
<p>! 	while((opt = getopt_long(argc, argv, "vuhV", opts, &#038;optidx))) {<br />
  		if(opt < 0) {<br />
  			break;<br />
  		}<br />
--- 56,68 ----<br />
  	{<br />
  		{"verbose",   no_argument,       0, 'v'},<br />
  		{"udp",       no_argument,       0, 'u'},<br />
+ 		{"delay",     no_argument,       0, 'd'},<br />
  		{"help",      no_argument,       0, 'h'},<br />
  		{"version",   no_argument,       0, 'V'},<br />
  		{0, 0, 0, 0}<br />
  	};</p>
<p>! 	while((opt = getopt_long(argc, argv, "vud:hV", opts, &#038;optidx))) {<br />
  		if(opt < 0) {<br />
  			break;<br />
  		}<br />
***************<br />
*** 68,73 ****<br />
--- 70,76 ----<br />
  			case 0:   break;<br />
  			case 'v': o_verbose = 1; break;<br />
  			case 'u': o_udp = 1; break;<br />
+ 			case 'd': o_delay = atoi(optarg); break;<br />
  			case 'V': ver();<br />
  			case 'h': /* fallthrough */<br />
  			default: usage();<br />
***************<br />
*** 128,133 ****<br />
--- 131,138 ----<br />
  			connect(sd, (struct sockaddr*)&#038;addr, sizeof(struct sockaddr));<br />
  		}<br />
  		close(sd);<br />
+ 		vprint("applying a delay of %u milliseconds\n", o_delay);<br />
+ 		usleep(o_delay * 1000);<br />
  	}</p>
<p>  	return(0);<br />
***************<br />
*** 148,153 ****<br />
--- 153,159 ----<br />
  	printf("usage: knock [options] <host>
<port[:proto]> [port[:proto]] ...\n");<br />
  	printf("options:\n");<br />
  	printf("  -u, --udp            make all ports hits use UDP (default is TCP)\n");<br />
+ 	printf("  -d, --delay          add a delay of the specified milliseconds between syns\n");<br />
  	printf("  -v, --verbose        be verbose\n");<br />
  	printf("  -V, --version        display version\n");<br />
  	printf("  -h, --help           this help\n");<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.casarini.org/blog/2011/adding-delay-to-c-port-knocking-client/feed/</wfw:commentRss>
		</item>
		<item>
		<title>package refactoring with sed</title>
		<link>http://www.casarini.org/blog/2010/package-refactoring-with-sed/</link>
		<comments>http://www.casarini.org/blog/2010/package-refactoring-with-sed/#comments</comments>
		<pubDate>Sat, 25 Dec 2010 09:29:51 +0000</pubDate>
		<dc:creator>paolo</dc:creator>
		
		<category><![CDATA[programming]]></category>

		<category><![CDATA[agile]]></category>

		<category><![CDATA[android]]></category>

		<category><![CDATA[bash]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.casarini.org/blog/?p=461</guid>
		<description><![CDATA[I know I'm a strange boy, with strange problems (at least wrt software development), but recently I had to solve the problem to build a Java project many many times programmatically changing only few parameters on it. One of the change to do was to rename the main package of the project, the package under [...]]]></description>
			<content:encoded><![CDATA[<p>I know I'm a strange boy, with strange problems (at least wrt software development), but recently I had to solve the problem to build a Java project many many times programmatically changing only few parameters on it. One of the change to do was to rename the main package of the project, the package under which all the classes are declared.<br />
To solve this problem I build a very simple bash script that use sed to rename all the occurrences of the old package name with the new one:<br />
<code><br />
#!/bin/bash</p>
<p>PROJECTPATH=$1</p>
<p>mv $PROJECTPATH/src/it/casarini/oldpackage $PROJECT/src/it/casarini/newpackage<br />
export MYLIST=`find $PROJECTPATH/src -type f -name *.java`<br />
for a in $MYLIST; do<br />
&nbsp;&nbsp;&nbsp;&nbsp;echo $a<br />
&nbsp;&nbsp;&nbsp;&nbsp;mv $a $a.orig<br />
&nbsp;&nbsp;&nbsp;&nbsp;sed s/it.casarini.oldpackage/it.casarini.newpackage/g $a.orig > $a<br />
&nbsp;&nbsp;&nbsp;&nbsp;rm $a.orig<br />
done;<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.casarini.org/blog/2010/package-refactoring-with-sed/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Riconoscimento vocale in AiC Mobile</title>
		<link>http://www.casarini.org/blog/2010/riconoscimento-vocale-in-aic-mobile/</link>
		<comments>http://www.casarini.org/blog/2010/riconoscimento-vocale-in-aic-mobile/#comments</comments>
		<pubDate>Sun, 24 Oct 2010 18:27:17 +0000</pubDate>
		<dc:creator>paolo</dc:creator>
		
		<category><![CDATA[family]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[AiC mobile]]></category>

		<category><![CDATA[android]]></category>

		<category><![CDATA[google]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[mobile]]></category>

		<guid isPermaLink="false">http://www.casarini.org/blog/?p=447</guid>
		<description><![CDATA[Ho aggiunto una nuova funzionalità di ricerca all'interno dell'applicazione AiC Mobile per Android (rilasciata con la versione 1.2.0). La funzionlità aggiunta è la possibilità di ricercare nel prontuario dei prodotti senza glutine fornito da AIC semplicemente pronunciando le parole che si vogliono usare come chiavi di ricerca.
La funzionalità introdotta sfrutta le API per il riconoscimento [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="" src="/images/ico_150.png" title="AiC mobile" align="left" width="150" height="150" />Ho aggiunto una nuova funzionalità di ricerca all'interno dell'applicazione <a href="http://code.google.com/p/aicmobile/">AiC Mobile</a> per Android (rilasciata con la versione 1.2.0). La funzionlità aggiunta è la possibilità di ricercare nel prontuario dei prodotti senza glutine fornito da <a href="http://www.celiachia.it/">AIC</a> semplicemente pronunciando le parole che si vogliono usare come chiavi di ricerca.<br />
La funzionalità introdotta sfrutta le API per il riconoscimento vocale introdotte sulla piattaforma Android dalla versione 1.5 e usa come motore per l'analisi fonetica quello introdotto con Google Search. In particolare, per usufruire della nuova funzionalità si deve avere almeno una app in grado di soddisfare una richiesta di <a href="http://developer.android.com/resources/articles/speech-input.html">Speech Input</a> (<code>RecognizerIntent.ACTION_RECOGNIZE_SPEECH</code>).<br />
Il miglioramento in usabilità della applicazione è notevole e per darne un'idea, ho realizzato un breve video che ne mostra l'utilizzo:</p>
<p><object width="512" height="320"><param name="movie" value="http://www.youtube.com/v/lBi39805-a0&#038;hl=it_IT&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/lBi39805-a0&#038;hl=it_IT&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="512" height="320"></embed></object></p>
<p>AiC Mobile è disponibile da agosto sul Market Google e viene costantemente migliorata e aggiornata. Per vedere AiC Mobile per Android sul Market o per scaricarla sul proprio telefonino è possibile usufruire del servizio AppBrain qui sotto evidenziato:</p>
<div id='app419' class='appbrain-app'><a href='http://www.appbrain.com/app/aic-mobile/org.casarini.android.aicmobile' style='font-size: 11px; color: #555; font-family: Arial, sans-serif;'>AiC Mobile for Android on AppBrain</a></div>
<p> <script type='text/javascript' language='javascript' src='http://www.appbrain.com/api/api.nocache.js'></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.casarini.org/blog/2010/riconoscimento-vocale-in-aic-mobile/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Android home and refresh menu icons</title>
		<link>http://www.casarini.org/blog/2010/android-home-and-refresh-menu-icons/</link>
		<comments>http://www.casarini.org/blog/2010/android-home-and-refresh-menu-icons/#comments</comments>
		<pubDate>Fri, 15 Oct 2010 19:55:10 +0000</pubDate>
		<dc:creator>paolo</dc:creator>
		
		<category><![CDATA[programming]]></category>

		<category><![CDATA[android]]></category>

		<category><![CDATA[icons]]></category>

		<guid isPermaLink="false">http://www.casarini.org/blog/?p=443</guid>
		<description><![CDATA[I love hacking on my Android device, the SDK is well designed and there's always a new technology to study, but to give a complete user experience to our applications we need icons! In particular, I found very useful (even if discouraged by the Android guidelines) to use, for the option menu, the icons included [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="" src="/images/ic_menu_home.png" title="ic_menu_home" class="alignleft" width="48" height="48" /><img alt="" src="/images/ic_menu_refresh.png" title="ic_menu_refresh" class="alignleft" width="48" height="48" />I love hacking on my Android device, the SDK is well designed and there's always a new technology to study, but to give a complete user experience to our applications we need icons! In particular, I found very useful (even if discouraged by the Android guidelines) to use, for the option menu, the icons included in the SDK.<br />
The better web resource I found where you can see which icons are include in the SDK is <a href="http://androiddrawableexplorer.appspot.com/">androiddrawableexplorer</a> but, as his author says, some of them are not public. I don't know why they are not public but for two of them (<code>ic_menu_refresh</code> and <code>ic_menu_home</code>)  I found the source so I've been able to include them in a recent work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.casarini.org/blog/2010/android-home-and-refresh-menu-icons/feed/</wfw:commentRss>
		</item>
		<item>
		<title>AiC mobile per Android esce sul Market</title>
		<link>http://www.casarini.org/blog/2010/aic-mobile-per-android-esce-sul-market/</link>
		<comments>http://www.casarini.org/blog/2010/aic-mobile-per-android-esce-sul-market/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 20:40:29 +0000</pubDate>
		<dc:creator>paolo</dc:creator>
		
		<category><![CDATA[family]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[AiC mobile]]></category>

		<category><![CDATA[android]]></category>

		<category><![CDATA[google]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[mobile]]></category>

		<category><![CDATA[senza glutine]]></category>

		<guid isPermaLink="false">http://www.casarini.org/blog/?p=419</guid>
		<description><![CDATA[GlutenBuster è diventata l'applicazione ufficiale per la piattaforma Android dell'Associazione Italiana Celiachia e ora si chiama AiC mobile.
Con questo post segnalo che ho rilascio la versione 1.0.0 di AiC mobile e che ieri l'ho pubblicata sul Market Google in modo che chiunque sia interessato possa installarla e usufruire del prontuario AIC dei prodotti senza glutine [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="" src="/images/ico_150.png" title="AiC mobile" align="left" width="150" height="150" /><em>GlutenBuster</em> è diventata l'applicazione ufficiale per la piattaforma Android dell'<a href="http://www.celiachia.it/">Associazione Italiana Celiachia</a> e ora si chiama <em><strong>AiC mobile</strong></em>.<br />
Con questo post segnalo che ho rilascio la versione 1.0.0 di <em>AiC mobile</em> e che ieri l'ho pubblicata sul Market Google in modo che chiunque sia interessato possa installarla e usufruire del prontuario AIC dei prodotti senza glutine sul proprio smartphone.<br />
<em>AiC mobile</em> per Android è sviluppata come <a href="http://it.wikipedia.org/wiki/Software_libero">software libero</a> (Free Software) ed è distribuita gratuitamente. Per scaricarsi il database dei prodotti, bisogna però essere registrati sul sito <a href="http://www.celiachia.it/res/etc/FormRegistrazione.aspx">www.celiachia.it</a> per creare un legame con l'associazione che certifica i prodotti.</p>
<p>Come ogni prodotto del volontariato, anche <em>AiC mobile</em> per Android, è il risultato della collaborazione di più persone che ringrazio per il loro aiuto e l'incoraggiamento che mi hanno donato: Luca Agostini, Alberto Carlana, Antonio D'Errico, Anna Ferrandino, Davide Fiorello, Mario Mestice, Antonio Pascuccio, Lorenzo Pino.</p>
<p>Per continuare:</p>
<ul>
<li><a href="http://www.casarini.org/blog/aicmobile">Presentazione del programma</a>, delle sue caratteristiche principali, screenshots, video dimostrativo e link per il download.</li>
<li>Gruppo per avere <a href="http://groups.google.it/group/android-aicmobile">supporto tecnico</a> nell'utilizzo dell'applicazione e per segnalare eventuali problemi;</li>
<li><a href="http://www.casarini.org/trac/glutenbuster">Sito di sviluppo collaborativo</a> dove è possibile accedere ai sorgenti e contribuire alle idee di sviluppo e alla stesura del codice.</li>
<li>Per installare l'applicazione:<br/><img alt="" src="/images/qr-aicmobile.png" title="aic mobile qr-code" class="alignnone" width="120" height="120" /></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.casarini.org/blog/2010/aic-mobile-per-android-esce-sul-market/feed/</wfw:commentRss>
		</item>
		<item>
		<title>GlutenBuster-0.3.1 released, si cercano tester!</title>
		<link>http://www.casarini.org/blog/2010/glutenbuster-031-released-si-cercano-tester/</link>
		<comments>http://www.casarini.org/blog/2010/glutenbuster-031-released-si-cercano-tester/#comments</comments>
		<pubDate>Sat, 22 May 2010 20:59:26 +0000</pubDate>
		<dc:creator>paolo</dc:creator>
		
		<category><![CDATA[family]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[android]]></category>

		<category><![CDATA[celiachia]]></category>

		<category><![CDATA[GlutenBuster]]></category>

		<category><![CDATA[google]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[mobile]]></category>

		<category><![CDATA[senza glutine]]></category>

		<guid isPermaLink="false">http://www.casarini.org/blog/?p=392</guid>
		<description><![CDATA[Ho rilasciato la prima versione pubblica di GlutenBuster, app per la piattaforma Android per la consultazione del prontuario AIC degli alimenti senza glutine.
GlutenBuster è sviluppato come software libero  (Free Software) ed è distribuito gratuitamente da sito di sviluppo collaborativo.
Sono alla ricerca di persone interessate a partecipare alla fase di test prima della pubblicazione nel [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="" src="http://www.casarini.org/images/glutenbuster-icon.png" title="GlutenBuster" align="left"/>Ho rilasciato la prima versione pubblica di <a href="http://www.casarini.org/blog/glutenbuster">GlutenBuster</a>, app per la piattaforma Android per la consultazione del prontuario <a href="http://www.celiachia.it">AIC</a> degli alimenti senza glutine.</p>
<p>GlutenBuster è sviluppato come <a href="http://it.wikipedia.org/wiki/Software_libero" target="_new">software libero</a>  (Free Software) ed è distribuito gratuitamente da <a href="http://www.casarini.org/trac/glutenbuster">sito di sviluppo collaborativo</a>.</p>
<p><strong>Sono alla ricerca di persone interessate a partecipare alla fase di test</strong> prima della pubblicazione nel Google Market. Se qualcuno fosse interassato può scrivere a <a href="mailto:glutenbuster@casarini.org">glutenbuster@casarini.org</a>.</p>
<p>Per continuare:</p>
<ul>
<li><a href="/blog/glutenbuster">www.casarini.org/blog/glutenbuster</a> - Presentazione del programma, delle sue caratteristiche principali e accesso all'ultima versione disponibile pronta per essere installata sul proprio cellulare;</li>
<li><a href="/blog/glutenbuster/glutenbuster-screenshots/">www.casarini.org/blog/glutenbuster/glutenbuster-screenshots/</a> - Screenshots che mostrano alcune schermate catturate durante l'ultizzo delle principali funzionalità;</li>
<li><a href="/trac/glutenbuster">www.casarini.org/trac/glutenbuster</a> - Sito di sviluppo collaborativo dove è possibile accedere ai sorgenti e contribuire alle idee di sviluppo e alla stesura del codice.</li>
</ul>
<p><object width="512" height="320"><param name="movie" value="http://www.youtube.com/v/K2YTq_3Awik&#038;hl=it_IT&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/K2YTq_3Awik&#038;hl=it_IT&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="512" height="320"></embed></object><br />
<br/></p>
]]></content:encoded>
			<wfw:commentRss>http://www.casarini.org/blog/2010/glutenbuster-031-released-si-cercano-tester/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to download a file and update an Android ProgressDialog</title>
		<link>http://www.casarini.org/blog/2010/how-to-download-a-file-and-update-an-android-progressdialog/</link>
		<comments>http://www.casarini.org/blog/2010/how-to-download-a-file-and-update-an-android-progressdialog/#comments</comments>
		<pubDate>Fri, 21 May 2010 21:05:58 +0000</pubDate>
		<dc:creator>paolo</dc:creator>
		
		<category><![CDATA[english]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[android]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[mobile]]></category>

		<category><![CDATA[networking]]></category>

		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.casarini.org/blog/?p=372</guid>
		<description><![CDATA[I recently made an helper class to download a file from the web and save it to the file sytem while updating a ProgressDialog to let the user know the percentage of the download made. This class has the responsibility to manage the network communication and to send messages to the UI to update the [...]]]></description>
			<content:encoded><![CDATA[<p><img src="/blog/wp-content/uploads/2009/04/android_developer.png" alt="" title="android developer" width="150" height="150" align="left" />I recently made an helper class to download a file from the web and save it to the file sytem while updating a ProgressDialog to let the user know the percentage of the download made. This class has the responsibility to manage the network communication and to send messages to the UI to update the status of the ProgressDialog previously built and displayed.</p>
<p>Before starting to write some code it may be useful to remember to declare in the AndroidManifest.xml  the request for the permission to make connections over the Internet and, if we want to save the file on the sd card, the permission to write to the external storage:</p>
<div class="igBar"><span id="lxml-11"><a href="#" onclick="javascript:showPlainTxt('xml-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-11">
<div class="xml">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;uses</span>-permission android:<span style="color: #000066;">name</span>=<span style="color: #ff0000;">"android.permission.INTERNET"</span><span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;uses</span>-permission android:<span style="color: #000066;">name</span>=<span style="color: #ff0000;">"android.permission.WRITE_EXTERNAL_STORAGE"</span><span style="font-weight: bold; color: black;">/&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The class I wrote is called <tt>DownloadThread</tt>, extends th <tt>java.lang.Thread</tt> and a constructor with four parameters:</p>
<ul>
<li>the <em>url</em> of the file to download;</li>
<li>the <em>path</em> where the downloaded file has to be written;</li>
<li>the instance of the ProgressDialog to update;</li>
<li>the handler that will receove the messages to update the UI.</li>
</ul>
<div class="igBar"><span id="ljava-12"><a href="#" onclick="javascript:showPlainTxt('java-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-12">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DownloadThread <span style="color: #000000; font-weight: bold;">extends</span> <a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Thread</span></a> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> TAG = Home.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006600;">getSimpleName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">int</span> BUFFER_LENGTH = <span style="color: #cc66cc;color:#800000;">8192</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Handler handler;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ProgressDialog progressDialog;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> fileurl;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> filepath;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> DownloadThread<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> fileurl, <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> filepath, Handler handler, ProgressDialog dialog<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">fileurl</span> = fileurl;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">filepath</span> = filepath;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">handler</span> = handler;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">progressDialog</span> = dialog;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><span id="more-372"></span><br />
The download process has to be in a separate <em>thread</em> and it has to be started soon after the dialog is displayed. The core of this class is its <em>run</em> method where we first make the connection to the specified <em>url</em> and build the input/output streams to realize the communicaton channel and then we read from the network the file content.</p>
<div class="igBar"><span id="ljava-13"><a href="#" onclick="javascript:showPlainTxt('java-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-13">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> run<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AURL+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">URL</span></a> url = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AURL+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">URL</span></a><span style="color: #66cc66;">&#40;</span>fileurl<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AFile+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">File</span></a> file = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AFile+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">File</span></a><span style="color: #66cc66;">&#40;</span>filepath<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Open a connection to that URL. */</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AURLConnection+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">URLConnection</span></a> ucon = url.<span style="color: #006600;">openConnection</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> length = ucon.<span style="color: #006600;">getContentLength</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; progressDialog.<span style="color: #006600;">setMax</span><span style="color: #66cc66;">&#40;</span>length<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/*</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Define InputStreams to read from the URLConnection and</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* OutputStream to write to file</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AInputStream+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">InputStream</span></a> is = ucon.<span style="color: #006600;">getInputStream</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ABufferedInputStream+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">BufferedInputStream</span></a> bis = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3ABufferedInputStream+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">BufferedInputStream</span></a><span style="color: #66cc66;">&#40;</span>is<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AFileOutputStream+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">FileOutputStream</span></a> fos = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AFileOutputStream+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">FileOutputStream</span></a><span style="color: #66cc66;">&#40;</span>file<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/*</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Read bytes to the Buffer until there is nothing more to read(-1).</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> buffer = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span>BUFFER_LENGTH<span style="color: #66cc66;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> read;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> count = <span style="color: #cc66cc;color:#800000;">0</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>read = bis.<span style="color: #006600;">read</span><span style="color: #66cc66;">&#40;</span>buffer, <span style="color: #cc66cc;color:#800000;">0</span>, BUFFER_LENGTH<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> != -<span style="color: #cc66cc;color:#800000;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fos.<span style="color: #006600;">write</span><span style="color: #66cc66;">&#40;</span>buffer, <span style="color: #cc66cc;color:#800000;">0</span>, read<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count += read; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Message msg = handler.<span style="color: #006600;">obtainMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bundle b = <span style="color: #000000; font-weight: bold;">new</span> Bundle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.<span style="color: #006600;">putInt</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"count"</span>, count<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg.<span style="color: #006600;">setData</span><span style="color: #66cc66;">&#40;</span>b<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler.<span style="color: #006600;">sendMessage</span><span style="color: #66cc66;">&#40;</span>msg<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; fos.<span style="color: #006600;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AIOException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.<span style="color: #006600;">d</span><span style="color: #66cc66;">&#40;</span>TAG, <span style="color: #ff0000;">"Error: "</span> + e<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>If we delete lines 9 and 28-32 the code could run in any Java standard environment, the highlighted lines have the resposibility to communicate with the UI and to update it. In particular line 9 sets the upper boung of the progress bar in the ProgressDialog and lines 28-32 update throught the Handler instance the current value of the progress bar.</p>
<p>What we need now is the code of build the ProgressDialog in our Activity e and the related Handler that manage messages from the Thread we have just written.</p>
<div class="igBar"><span id="ljava-14"><a href="#" onclick="javascript:showPlainTxt('java-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-14">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyActvity <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">int</span> DOWNLOAD_DIALOG = <span style="color: #cc66cc;color:#800000;">0</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; ....</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #006600;">DownloadThread</span> downloadThread;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; ...</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; @Override</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <a href="http://www.google.com/search?q=allinurl%3ADialog+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Dialog</span></a> onCreateDialog<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> id<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> DOWNLOADDB_DIALOG:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> ProgressDialog downloadDialog = <span style="color: #000000; font-weight: bold;">new</span> ProgressDialog<span style="color: #66cc66;">&#40;</span>MyActvity.<span style="color: #000000; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; downloadDialog.<span style="color: #006600;">setProgressStyle</span><span style="color: #66cc66;">&#40;</span>ProgressDialog.<span style="color: #006600;">STYLE_HORIZONTAL</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; downloadDialog.<span style="color: #006600;">setMessage</span><span style="color: #66cc66;">&#40;</span>getText<span style="color: #66cc66;">&#40;</span>R.<span style="color: #006600;">string</span>.<span style="color: #006600;">home_progress_message</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; downloadDialog.<span style="color: #006600;">setCancelable</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> Handler handler = <span style="color: #000000; font-weight: bold;">new</span> Handler<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> handleMessage<span style="color: #66cc66;">&#40;</span>Message msg<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> count = msg.<span style="color: #006600;">getData</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getInt</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"count"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; downloadDialog.<span style="color: #006600;">setProgress</span><span style="color: #66cc66;">&#40;</span>count<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>count&gt;= downloadDialog.<span style="color: #006600;">getMax</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dismissDialog<span style="color: #66cc66;">&#40;</span>DOWNLOAD_DIALOG<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeDialog<span style="color: #66cc66;">&#40;</span>DOWNLOADDB_DIALOG<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.<span style="color: #006600;">makeText</span><span style="color: #66cc66;">&#40;</span>MyActvity.<span style="color: #000000; font-weight: bold;">this</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getText<span style="color: #66cc66;">&#40;</span>R.<span style="color: #006600;">string</span>.<span style="color: #006600;">download_ok_message</span><span style="color: #66cc66;">&#41;</span>, Toast.<span style="color: #006600;">LENGTH_SHORT</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; downloadThread = <span style="color: #000000; font-weight: bold;">new</span> DownloadThread<span style="color: #66cc66;">&#40;</span>url, path</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler, downloadDialog<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; downloadThread.<span style="color: #006600;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> downloadDialog;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; ... </div>
</li>
</ol>
</div>
</div>
</div>
<p>
<br/>It's all, folks! If you want to see this class in action you can see it in a small free software project called <a href="http://www.casarini.org/trac/glutenbuster">GlutenBuster</a>. <br/></p>
]]></content:encoded>
			<wfw:commentRss>http://www.casarini.org/blog/2010/how-to-download-a-file-and-update-an-android-progressdialog/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

