<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mike Plate</title>
	<atom:link href="http://www.mikeplate.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikeplate.com</link>
	<description>Freelance web and mobile developer</description>
	<lastBuildDate>Wed, 09 May 2012 14:43:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Extract regular expression group match using grep or sed</title>
		<link>http://www.mikeplate.com/2012/05/09/extract-regular-expression-group-match-using-grep-or-sed/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=extract-regular-expression-group-match-using-grep-or-sed</link>
		<comments>http://www.mikeplate.com/2012/05/09/extract-regular-expression-group-match-using-grep-or-sed/#comments</comments>
		<pubDate>Wed, 09 May 2012 14:25:30 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[gnu]]></category>
		<category><![CDATA[grep]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=595</guid>
		<description><![CDATA[I&#8217;ve been looking for a GNU/Linux command that matches lines in a text file using a regular expression and extracts a group match for that expression (a sub-match within parenthesis). It would be really handy if the grep command supported this as an output option, but unfortunately it does not. However there are at least [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been looking for a GNU/Linux command that matches lines in a text file using a regular expression and extracts a group match for that expression (a sub-match within parenthesis). It would be really handy if the grep command supported this as an output option, but unfortunately it does not. However there are at least two ways to approach this problem. I&#8217;ll show you one with grep and one with sed.<span id="more-595"></span></p>
<p>So, here is the situation. I have a text file where I want to extract a sub-match for lines that match the whole regular expression. As an example, we can take the output from mysqldump which is the sql statements to recreate a database. It can look like this:</p>
<pre>CREATE TABLE `offercalc_fields` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `price` double NOT NULL,
  `offer_slug` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;</pre>
<p>Now I&#8217;d like to match all lines that contains &#8220;CREATE TABLE&#8221; and then extract the string between ` and `. A regular expression with group matching is easy:</p>
<pre>CREATE TABLE `(.+)`</pre>
<p>Getting just the matching lines is easy with grep, assuming the text file is named &#8220;dump.sql&#8221;:</p>
<pre>grep -Po 'CREATE TABLE `(.+)`' dump.sql</pre>
<p>The grep options means:</p>
<ul>
<li>-P to turn on Perl-compatible regular expressions (which I always want)</li>
<li>-o to specify that grep should not output the whole matching line, but only the matching part of the line</li>
</ul>
<p>However, this still outputs too much. &#8220;CREATE TABLE&#8221; and the enclosing ` and ` characters will still be included in the output from grep. Here it would be really handy if you could specify a group match to the -o option. Like &#8220;-o1&#8243; to output the first group match. Unfortunately this is not supported.</p>
<p>So how to solve this problem?</p>
<h2>Use look-ahead and look-behind with grep</h2>
<p>My first solution is to put everything before the group match inside a look-ahead and everything after the group match inside a look-behind. The downside is that look-ahead and look-behinds in regular expressions must be constant strings (no regular expressions inside the look-ahead/behind).</p>
<p>Look-ahead in regular expressions look like &#8220;(?&lt;=Hello)&#8221; where &#8220;Hello&#8221; is the string to match. Look-behind in regular expressions look like &#8220;(?=Hello)&#8221; where &#8220;Hello&#8221; is the string to match. So the solution is:</p>
<pre>grep -Po '(?&lt;=CREATE TABLE `).+(?=`)' dump.sql</pre>
<p>Now grep only prints the matching part of the line and with look-ahead/behind I&#8217;ve managed to exclude the strings before and after my target from the match.</p>
<h2>Use substitute and print commands inside of sed</h2>
<p>Another command that can help me is sed. Sed knows about group matches in the context of the substitute command inside of sed. Combining this with the print command in sed, I get the same result with the following command:</p>
<pre>sed -nr 's/CREATE TABLE `(.+)`.*/\1/p' dump.sql</pre>
<p>The regular expression is easier to write here since we are not constrained to look-ahead/behind. If you don&#8217;t know the sed command I first need to explain the following parts:</p>
<ul>
<li>-n option prevents the default behavior of sed to print all lines (not just matching)</li>
<li>-r option turns on &#8220;extended regular expression&#8221; which means I don&#8217;t have to escape as much inside of the regular expression (roughly equivalent to the &#8220;-P&#8221; option I used for grep)</li>
<li>s/ is the substitute command</li>
<li>Everything after this up until the next / is the matching regular expression</li>
<li>/\1/ is the substitution that replaces the match in the line</li>
<li>\1 in the substitution means the first group match, which is our target here</li>
<li>/p at the end tells sed to print the resulted line (after the substition), this is needed since I turned off the default output of all lines with &#8220;-n&#8221;</li>
</ul>
<p>Note that since it is a substitution operation (and not an extract operation), I need to make sure that the regular expression matches the whole line by ending it with &#8220;.*&#8221;. Otherwise any text before or after the regular expression would still be included in the output.</p>
<p>That means that I&#8217;m also assuming that &#8220;CREATE TABLE&#8221; is at the beginning of the line. I could improve the command by being explicit about this using &#8220;^&#8221;:</p>
<pre>sed -nr 's/^CREATE TABLE `(.+)`.*/\1/p' dump.sql</pre>
<h2>Conclusion</h2>
<p>Which solution is the best depends on the original text lines. I think that grep is easier if there is a lot more before and/or after the group match that might be harder to match against in sed (since sed must match the whole line in the regular expression). But for all other cases, I think the sed solution looks a little cleaner when I don&#8217;t have to use the look-ahead/behind expressions.</p>
<p>Please feel free to give feedback in the comments if there are even better ways to do this in Bash with standard GNU commands.</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=595&amp;md5=87761a7e10b3e82145ae3c9868603e5d" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2012/05/09/extract-regular-expression-group-match-using-grep-or-sed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2012%2F05%2F09%2Fextract-regular-expression-group-match-using-grep-or-sed%2F&amp;language=en_GB&amp;category=text&amp;title=Extract+regular+expression+group+match+using+grep+or+sed&amp;description=I%26%238217%3Bve+been+looking+for+a+GNU%2FLinux+command+that+matches+lines+in+a+text+file+using+a+regular+expression+and+extracts+a+group+match+for+that+expression+%28a+sub-match+within+parenthesis%29....&amp;tags=gnu%2Cgrep%2Clinux%2Cregex%2Csed%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Retrieve single file from remote repository</title>
		<link>http://www.mikeplate.com/2012/05/04/retrieve-single-file-from-remote-repository/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=retrieve-single-file-from-remote-repository</link>
		<comments>http://www.mikeplate.com/2012/05/04/retrieve-single-file-from-remote-repository/#comments</comments>
		<pubDate>Fri, 04 May 2012 08:27:42 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[micro]]></category>
		<category><![CDATA[git]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=591</guid>
		<description><![CDATA[If you want to retrieve a single file from a specific repository without cloning or checking out anything, you can do this: git archive --remote git@bitbucket.org:myusername/myrepo.git HEAD file-to-retrieve.rb &#124; tar -x]]></description>
			<content:encoded><![CDATA[<p>If you want to retrieve a single file from a specific repository without cloning or checking out anything, you can do this:</p>
<pre>git archive --remote git@bitbucket.org:myusername/myrepo.git HEAD file-to-retrieve.rb | tar -x</pre>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=591&amp;md5=eecf492fe79be0623200bfd179816a74" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2012/05/04/retrieve-single-file-from-remote-repository/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2012%2F05%2F04%2Fretrieve-single-file-from-remote-repository%2F&amp;language=en_GB&amp;category=text&amp;title=Retrieve+single+file+from+remote+repository&amp;description=If+you+want+to+retrieve+a+single+file+from+a+specific+repository+without+cloning+or+checking+out+anything%2C+you+can+do+this%3A+git+archive+--remote+git%40bitbucket.org%3Amyusername%2Fmyrepo.git+HEAD+file-to-retrieve.rb+%7C+tar...&amp;tags=git%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Rewind master if you forgot to create new branch in Git</title>
		<link>http://www.mikeplate.com/2012/04/21/rewind-master-if-you-forgot-to-create-new-branch-in-git/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rewind-master-if-you-forgot-to-create-new-branch-in-git</link>
		<comments>http://www.mikeplate.com/2012/04/21/rewind-master-if-you-forgot-to-create-new-branch-in-git/#comments</comments>
		<pubDate>Sat, 21 Apr 2012 13:06:47 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[git]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=586</guid>
		<description><![CDATA[My situation in Git was this: I&#8217;d continued to work in the master branch and made a few commits when I realized that I should do this new stuff in a separate branch and keep the master where it was several commits ago. Turns out this is very simple to do, but I thought that [...]]]></description>
			<content:encoded><![CDATA[<p>My situation in Git was this: I&#8217;d continued to work in the master branch and made a few commits when I realized that I should do this new stuff in a separate branch and keep the master where it was several commits ago. Turns out this is very simple to do, but I thought that it was worth a blog post anyway.<span id="more-586"></span></p>
<p>The steps are quite simple. Assuming I&#8217;m in the master branch with a few commits I&#8217;d like to rewind but keep the code that I have in a new branch.</p>
<p>1. Create a new branch for the current code</p>
<blockquote>
<pre>git branch new-experiments</pre>
</blockquote>
<p>2. Determine how far back I&#8217;d like to rewind master</p>
<blockquote>
<pre>git log</pre>
</blockquote>
<p>When looking at the log, find the last commit I want to keep and make a note of its hash tag. It will be used in the next step.</p>
<p>3. Rewind/move master back to found commit</p>
<blockquote>
<pre>git reset --hard ab15cd26</pre>
</blockquote>
<p>4. Go back to where I was by checking out the newly created branch</p>
<blockquote>
<pre>git checkout new-experiments</pre>
</blockquote>
<p>5. Also, I got an error message when I tried to push these changes to a central repository (Github) for the master branch. So I needed to force the push with:</p>
<blockquote>
<pre>git push --force github master</pre>
</blockquote>
<p>That&#8217;s it!</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=586&amp;md5=8fe432c8181e28bc242d4a13a8442fa8" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2012/04/21/rewind-master-if-you-forgot-to-create-new-branch-in-git/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2012%2F04%2F21%2Frewind-master-if-you-forgot-to-create-new-branch-in-git%2F&amp;language=en_GB&amp;category=text&amp;title=Rewind+master+if+you+forgot+to+create+new+branch+in+Git&amp;description=My+situation+in+Git+was+this%3A+I%26%238217%3Bd+continued+to+work+in+the+master+branch+and+made+a+few+commits+when+I+realized+that+I+should+do+this+new+stuff+in...&amp;tags=git%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Work in progress Ubuntu Admin at Github</title>
		<link>http://www.mikeplate.com/2012/04/13/this-is-just-a-test/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=this-is-just-a-test</link>
		<comments>http://www.mikeplate.com/2012/04/13/this-is-just-a-test/#comments</comments>
		<pubDate>Fri, 13 Apr 2012 16:49:14 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[micro]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=534</guid>
		<description><![CDATA[Work in progress. Bash scripts for automating downloading and building of Nginx with Passenger and more on Ubuntu servers. https://github.com/mikeplate/ubuntu-admin]]></description>
			<content:encoded><![CDATA[<p>Work in progress. Bash scripts for automating downloading and building of Nginx with Passenger and more on Ubuntu servers. <a title="Source code at Github" href="https://github.com/mikeplate/ubuntu-admin">https://github.com/mikeplate/ubuntu-admin</a></p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=534&amp;md5=238f31b4d522361fd5c2658baaadb010" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2012/04/13/this-is-just-a-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2012%2F04%2F13%2Fthis-is-just-a-test%2F&amp;language=en_GB&amp;category=text&amp;title=Work+in+progress+Ubuntu+Admin+at+Github&amp;description=Work+in+progress.+Bash+scripts+for+automating+downloading+and+building+of+Nginx+with+Passenger+and+more+on+Ubuntu+servers.+https%3A%2F%2Fgithub.com%2Fmikeplate%2Fubuntu-admin&amp;tags=bash%2Cnginx%2Cubuntu%2Cblog" type="text/html" />
	</item>
		<item>
		<title>CSS table spacing and box-sizing</title>
		<link>http://www.mikeplate.com/2012/04/11/css-table-spacing-and-box-sizing/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=css-table-spacing-and-box-sizing</link>
		<comments>http://www.mikeplate.com/2012/04/11/css-table-spacing-and-box-sizing/#comments</comments>
		<pubDate>Wed, 11 Apr 2012 15:00:16 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[micro]]></category>
		<category><![CDATA[css]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=543</guid>
		<description><![CDATA[Discovered two things about css today: you can turn off table spacing with &#8220;border-spacing: 0px&#8221; and the default box-sizing for tables differs between browsers, it is &#8220;box-sizing: content-box&#8221; on WebKit and &#8220;box-sizing: border-box&#8221; on all other browsers. Note though that box-sizing is not relevant until you set &#8220;display: block&#8221; for the table.]]></description>
			<content:encoded><![CDATA[<p>Discovered two things about css today: you can turn off table spacing with &#8220;border-spacing: 0px&#8221; and the default box-sizing for tables differs between browsers, it is &#8220;box-sizing: content-box&#8221; on WebKit and &#8220;box-sizing: border-box&#8221; on all other browsers. Note though that box-sizing is not relevant until you set &#8220;display: block&#8221; for the table.</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=543&amp;md5=fc5a768403a4e94945240b245c90b6ea" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2012/04/11/css-table-spacing-and-box-sizing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2012%2F04%2F11%2Fcss-table-spacing-and-box-sizing%2F&amp;language=en_GB&amp;category=text&amp;title=CSS+table+spacing+and+box-sizing&amp;description=Discovered+two+things+about+css+today%3A+you+can+turn+off+table+spacing+with+%26%238220%3Bborder-spacing%3A+0px%26%238221%3B+and+the+default+box-sizing+for+tables+differs+between+browsers%2C+it+is+%26%238220%3Bbox-sizing%3A+content-box%26%238221%3B+on+WebKit...&amp;tags=css%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Creating a closure in JavaScript</title>
		<link>http://www.mikeplate.com/2011/11/24/creating-a-closure-in-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=creating-a-closure-in-javascript</link>
		<comments>http://www.mikeplate.com/2011/11/24/creating-a-closure-in-javascript/#comments</comments>
		<pubDate>Thu, 24 Nov 2011 15:34:31 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=526</guid>
		<description><![CDATA[I thought that I knew how closures worked in JavaScript, but then I was bitten by a bug that made me realize I had some more learning to do. This article is the result of that learning. It describes one particular situation where you need to explicitly create a closure  in JavaScript, but I also [...]]]></description>
			<content:encoded><![CDATA[<p>I thought that I knew how closures worked in JavaScript, but then I was bitten by a bug that made me realize I had some more learning to do. This article is the result of that learning. It describes one particular situation where you need to explicitly create a closure  in JavaScript, but I also think it illustrates closures in general.<span id="more-526"></span></p>
<h2>The problem</h2>
<p>Take a very simple html page with three elements, like these:</p>
<blockquote>
<pre>&lt;h1&gt;A&lt;/h1&gt;
&lt;h1&gt;B&lt;/h1&gt;
&lt;h1&gt;C&lt;/h1&gt;</pre>
</blockquote>
<p>Now assume you would like to bind the click event to all of the h1-elements on the page. To prove that something happens, I will just add a word to the contents of the h1 element when it is clicked. The code looks like this:</p>
<blockquote>
<pre>var elements = document.getElementsByTagName("h1");
for (var i = 0; i&lt;elements.length; i++) {
	var el = elements[i];
	el.addEventListener("click", function() {
		el.innerHTML += ", clicked";
	}, false);
}</pre>
</blockquote>
<p>I thought this would work. Turns out it doesn&#8217;t. If you run it, all clicks will add the word to the last element. Even when you click the A, the word will be added to the end of the last element with a C.</p>
<p>You can try it out using jsFiddle <a title="jsFiddle example" href="http://jsfiddle.net/mikeplate/UVDUH/1/">here</a>.</p>
<p>My mistake was assuming that a closure was made inside of my function doing the &#8220;el.innerHTML&#8221; assignment, making sure that the &#8220;el&#8221; variable was frozen it time and would always refer to the value/element I had when the code was executed. But that is not so. The rule here is that no matter where you declare a variable in JavaScript with the &#8220;var&#8221; keyword, it will always act as though it was declared at the beginning of the innermost function anyway.</p>
<p>As a side example, these two code sections are the same when it comes to the scope of the &#8220;str&#8221; variable.</p>
<blockquote>
<pre>function my() {
    for (var i = 0; i&lt;10; i++) {
        var str = i.toString();
    }
}</pre>
</blockquote>
<blockquote>
<pre>function my() {
    var str;
    for (var i = 0; i&lt;10; i++) {
        str = i.toString();
    }
}</pre>
</blockquote>
<h2>The solution</h2>
<p>The way to freeze the &#8220;el&#8221; variable at the time of calling addEventLister, is to explicitly create a closure. A closure is a JavaScript function. The function is the only container that will freeze a variable at the time of execution, which is what a closure is, really.</p>
<p>This is one way of ensuring that the code works as expected:</p>
<blockquote>
<pre>var elements = document.getElementsByTagName("h1");
for (var i = 0; i&lt;elements.length; i++) {
    (function(el) {
        el.addEventListener("click", function() {
            el.innerHTML += ", clicked";
        }, false);
    })(elements[i]);
}</pre>
</blockquote>
<p>Notice how the call to addEventListener is now wrapped in an anonymous function, and how that anonymous function is also called in the same statement, by putting the &#8220;(elements[i])&#8221; at the end.</p>
<p>You can verify that the solution works at jsFiddle <a title="jsFiddle example, the solution" href="http://jsfiddle.net/mikeplate/UVDUH/2/">here</a>.</p>
<p>If you like, you can also extract the closure part into a separate function, which may improve the readability of the code. One such alternative solutions would be:</p>
<blockquote>
<pre>function handleClick(el, func) {
    el.addEventListener("click", function(ev) { func(el); }, false);
}

var elements = document.getElementsByTagName("h1");
for (var i = 0; i&lt;elements.length; i++) {
    handleClick(elements[i], function(el) {
        el.innerHTML += ", clicked";
    });
}</pre>
</blockquote>
<p>Or even slightly more generic and succinct:</p>
<blockquote>
<pre>function createHandler(el) { return function(ev) { el.innerHTML += ", clicked"; }; };
var elements = document.getElementsByTagName("h1");
for (var i = 0; i&lt;elements.length; i++) {
	var el = elements[i];
	el.addEventListener("click", createHandler(el), false);
}</pre>
</blockquote>
<p>One difference in the last example is that the closure encloses less of the code (only the assignment of innerHTML). Also note that the function &#8220;createHandler&#8221; is return an anonymous function in itself. It could be called a &#8220;factory&#8221; since it doesn&#8217;t execute any code, but just returns created functions that will be called later.</p>
<p>I guess what solution you use is dependent on what you are trying to do.</p>
<p>&nbsp;</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=526&amp;md5=066e15b57e007dc38b810dafda912638" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2011/11/24/creating-a-closure-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2011%2F11%2F24%2Fcreating-a-closure-in-javascript%2F&amp;language=en_GB&amp;category=text&amp;title=Creating+a+closure+in+JavaScript&amp;description=I+thought+that+I+knew+how+closures+worked+in+JavaScript%2C+but+then+I+was+bitten+by+a+bug+that+made+me+realize+I+had+some+more+learning+to+do.+This...&amp;tags=javascript%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Linux file permissions to only delete owned files</title>
		<link>http://www.mikeplate.com/2011/11/18/linux-file-permissions-to-only-delete-owned-files/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linux-file-permissions-to-only-delete-owned-files</link>
		<comments>http://www.mikeplate.com/2011/11/18/linux-file-permissions-to-only-delete-owned-files/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 16:36:42 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=510</guid>
		<description><![CDATA[Coming from a Windows background, I was surprised to find out that when it comes to Linux file permissions, a user can delete a file that they don&#8217;t own and have no write permissions to. Well, that is the default behavior if you don&#8217;t set up your ownership and permissions exactly correct. Find out how [...]]]></description>
			<content:encoded><![CDATA[<p>Coming from a Windows background, I was surprised to find out that when it comes to Linux file permissions, a user can delete a file that they don&#8217;t own and have no write permissions to. Well, that is the default behavior if you don&#8217;t set up your ownership and permissions exactly correct. Find out how here.<span id="more-510"></span></p>
<h2>The goal</h2>
<p>This is what I want for a specific directory:</p>
<ul>
<li>The user can create new directories and files</li>
<li>The user can delete directories and files that the user has created (and thereby is owner of)</li>
<li>The user cannot write to or delete specific pre-existing protected files and directories</li>
</ul>
<p>My practical use case was an ftp+web server where I wanted users to upload and create directories all they want, but protect a readme.txt in the root and a pre-made sample in the demo directory.</p>
<p>First of all, removing write permissions and thereby blocking any updates is easy. That follows what I consider normal logic. By setting myself as owner and removing write permission for others, that was no problem. However, to my surprise, the user could still delete the now write-protected file and directory!</p>
<p>The problem is that on Linux, if you have write permissions to the directory where the files reside (or owns the directory), you always have permission to delete anything in there, regardless of anything else. And you need to give the user write permissions to the directory in order for them to create new files and update the files they have created.</p>
<h2>The solution</h2>
<p>There are two things you need to do besides setting the right permissions of the actual file or directory that you want to protect. Make sure that you:</p>
<ul>
<li>Set the <a title="Sticky bit reference" href="http://en.wikipedia.org/wiki/Sticky_bit">sticky bit</a> of the directory</li>
<li>Set the owner of the directory to yourself (or any other higher authority)</li>
</ul>
<p>So let&#8217;s make a few assumptions in order to show you the commands:</p>
<ul>
<li>The file to protect in the directory is named &#8220;readme.txt&#8221;</li>
<li>The user that should have access to the directory is named &#8220;www-data&#8221;</li>
<li>I&#8217;m an administrator on the machine and my user name is &#8220;mikael&#8221;</li>
</ul>
<p>Having changed to the directory in question, I do the following as root:</p>
<blockquote><p>chmod 1775 .<br />
chown mikael:www-data .<br />
chown mikael:www-data readme.txt</p></blockquote>
<p>Note that the &#8220;1&#8243; in the chmod command is what sets the sticky bit.</p>
<p>With the user having uploaded an image and created a directory named &#8220;test&#8221;, a listing should look like this:</p>
<blockquote>
<pre>drwxrwxr-t 3 mikael   www-data  4096 2011-11-16 04:43 .
drwxrwxr-x 5 mikael   mikael    4096 2011-11-08 16:13 ..
-rw-r--r-- 1 www-data www-data 79383 2011-11-16 03:51 logo.jpg
-rw-r--r-- 1 mikael   www-data    15 2011-11-16 04:11 readme.txt
drwx---r-x 2 www-data www-data  4096 2011-11-15 17:55 test</pre>
</blockquote>
<p>This works just the way I want. The ftp server runs as user &#8220;www-data&#8221; and the users logging in can upload files and create directories all they want but they can&#8217;t delete &#8220;readme.txt&#8221;.</p>
<p>I was looking at installing and configure ACLs on Linux (Access Control Lists) to solve the problem, but since this works I didn&#8217;t have to do that.</p>
<p>&nbsp;</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=510&amp;md5=d24d8f0cfa2ae8efb6d15cedfee47d6a" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2011/11/18/linux-file-permissions-to-only-delete-owned-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2011%2F11%2F18%2Flinux-file-permissions-to-only-delete-owned-files%2F&amp;language=en_GB&amp;category=text&amp;title=Linux+file+permissions+to+only+delete+owned+files&amp;description=Coming+from+a+Windows+background%2C+I+was+surprised+to+find+out+that+when+it+comes+to+Linux+file+permissions%2C+a+user+can+delete+a+file+that+they+don%26%238217%3Bt+own+and...&amp;tags=linux%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Making web site certificate work for Android</title>
		<link>http://www.mikeplate.com/2011/11/16/making-web-site-certificate-work-for-android/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=making-web-site-certificate-work-for-android</link>
		<comments>http://www.mikeplate.com/2011/11/16/making-web-site-certificate-work-for-android/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 21:10:00 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=515</guid>
		<description><![CDATA[I installed a web site certificate called GeoTrust QuickSSL from http://cheapssls.com on a site of mine. That wasn&#8217;t too hard I think. Everything looked fine when I tested it on my desktop browsers. However inside of the Android emulator (version 2.1 and others) and also on my newest Android device (running 2.3.5) I got a [...]]]></description>
			<content:encoded><![CDATA[<p>I installed a web site certificate called GeoTrust QuickSSL from <a title="CheapSSLs" href="http://cheapssls.com">http://cheapssls.com</a> on a site of mine. That wasn&#8217;t too hard I think. Everything looked fine when I tested it on my desktop browsers. However inside of the Android emulator (version 2.1 and others) and also on my newest Android device (running 2.3.5) I got a warning that the certificate wasn&#8217;t trusted.<span id="more-515"></span></p>
<p>Turns out I had some more learning to do regarding SSL and certificates. At first, I thought that maybe the root certificate for this GeoTrust QuickSSL simply wasn&#8217;t available on Android. The sites explaining mobile browser support are pretty vague on where they will work. However, that was not the case. It was actually not that hard to fix once I found the correct information. The online chat support at CheapSSLs pointed me in the right direction, although it took some further investigation.</p>
<h2>The solution</h2>
<p>Two things need to be fixed for older Android versions, like 2.1. (The first one was sufficient for my newest version of 2.3.5.)</p>
<ul>
<li>Add GeoTrust intermediate certificate data to the end of your web site certificate</li>
<li>Add GeoTrust CA certificate data to the end of previous step</li>
</ul>
<p>Both of these certificate&#8217;s data are plain text that can be added to the end of your current web site certificate, probably named &#8220;.pem&#8221; or &#8220;.crt&#8221;. The GeoTrust intermediate certificate data (at time of writing) is:</p>
<blockquote>
<pre>-----BEGIN CERTIFICATE-----
MIID+jCCAuKgAwIBAgIDAjbSMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT
MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i
YWwgQ0EwHhcNMTAwMjI2MjEzMjMxWhcNMjAwMjI1MjEzMjMxWjBhMQswCQYDVQQG
EwJVUzEWMBQGA1UEChMNR2VvVHJ1c3QgSW5jLjEdMBsGA1UECxMURG9tYWluIFZh
bGlkYXRlZCBTU0wxGzAZBgNVBAMTEkdlb1RydXN0IERWIFNTTCBDQTCCASIwDQYJ
KoZIhvcNAQEBBQADggEPADCCAQoCggEBAKa7jnrNpJxiV9RRMEJ7ixqy0ogGrTs8
KRMMMbxp+Z9alNoGuqwkBJ7O1KrESGAA+DSuoZOv3gR+zfhcIlINVlPrqZTP+3RE
60OUpJd6QFc1tqRi2tVI+Hrx7JC1Xzn+Y3JwyBKF0KUuhhNAbOtsTdJU/V8+Jh9m
cajAuIWe9fV1j9qRTonjynh0MF8VCpmnyoM6djVI0NyLGiJOhaRO+kltK3C+jgwh
w2LMpNGtFmuae8tk/426QsMmqhV4aJzs9mvIDFcN5TgH02pXA50gDkvEe4GwKhz1
SupKmEn+Als9AxSQKH6a9HjQMYRX5Uw4ekIR4vUoUQNLIBW7Ihq28BUCAwEAAaOB
2TCB1jAOBgNVHQ8BAf8EBAMCAQYwHQYDVR0OBBYEFIz02ZMKR7wAoErOS3VuoLaw
sn78MB8GA1UdIwQYMBaAFMB6mGiNifurBWQMEX2qfWW4ysxOMBIGA1UdEwEB/wQI
MAYBAf8CAQAwOgYDVR0fBDMwMTAvoC2gK4YpaHR0cDovL2NybC5nZW90cnVzdC5j
b20vY3Jscy9ndGdsb2JhbC5jcmwwNAYIKwYBBQUHAQEEKDAmMCQGCCsGAQUFBzAB
hhhodHRwOi8vb2NzcC5nZW90cnVzdC5jb20wDQYJKoZIhvcNAQEFBQADggEBADOR
NxHbQPnejLICiHevYyHBrbAN+qB4VqOC/btJXxRtyNxflNoRZnwekcW22G1PqvK/
ISh+UqKSeAhhaSH+LeyCGIT0043FiruKzF3mo7bMbq1vsw5h7onOEzRPSVX1ObuZ
lvD16lo8nBa9AlPwKg5BbuvvnvdwNs2AKnbIh+PrI7OWLOYdlF8cpOLNJDErBjgy
YWE5XIlMSB1CyWee0r9Y9/k3MbBn3Y0mNhp4GgkZPJMHcCrhfCn13mZXCxJeFu1e
vTezMGnGkqX2Gdgd+DYSuUuVlZzQzmwwpxb79k1ktl8qFJymyFWOIPllByTMOAVM
IIi0tWeUz12OYjf+xLQ=
-----END CERTIFICATE-----</pre>
</blockquote>
<p>The GeoTurst CA certificate data (at the time of writing) is:</p>
<blockquote>
<pre>-----BEGIN CERTIFICATE-----
MIIDfTCCAuagAwIBAgIDErvmMA0GCSqGSIb3DQEBBQUAME4xCzAJBgNVBAYTAlVT
MRAwDgYDVQQKEwdFcXVpZmF4MS0wKwYDVQQLEyRFcXVpZmF4IFNlY3VyZSBDZXJ0
aWZpY2F0ZSBBdXRob3JpdHkwHhcNMDIwNTIxMDQwMDAwWhcNMTgwODIxMDQwMDAw
WjBCMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNR2VvVHJ1c3QgSW5jLjEbMBkGA1UE
AxMSR2VvVHJ1c3QgR2xvYmFsIENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
CgKCAQEA2swYYzD99BcjGlZ+W988bDjkcbd4kdS8odhM+KhDtgPpTSEHCIjaWC9m
OSm9BXiLnTjoBbdqfnGk5sRgprDvgOSJKA+eJdbtg/OtppHHmMlCGDUUna2YRpIu
T8rxh0PBFpVXLVDviS2Aelet8u5fa9IAjbkU+BQVNdnARqN7csiRv8lVK83Qlz6c
JmTM386DGXHKTubU1XupGc1V3sjs0l44U+VcT4wt/lAjNvxm5suOpDkZALeVAjmR
Cw7+OC7RHQWa9k0+bw8HHa8sHo9gOeL6NlMTOdReJivbPagUvTLrGAMoUgRx5asz
PeE4uwc2hGKceeoWMPRfwCvocWvk+QIDAQABo4HwMIHtMB8GA1UdIwQYMBaAFEjm
aPkr0rKV10fYIyAQTzOYkJ/UMB0GA1UdDgQWBBTAephojYn7qwVkDBF9qn1luMrM
TjAPBgNVHRMBAf8EBTADAQH/MA4GA1UdDwEB/wQEAwIBBjA6BgNVHR8EMzAxMC+g
LaArhilodHRwOi8vY3JsLmdlb3RydXN0LmNvbS9jcmxzL3NlY3VyZWNhLmNybDBO
BgNVHSAERzBFMEMGBFUdIAAwOzA5BggrBgEFBQcCARYtaHR0cHM6Ly93d3cuZ2Vv
dHJ1c3QuY29tL3Jlc291cmNlcy9yZXBvc2l0b3J5MA0GCSqGSIb3DQEBBQUAA4GB
AHbhEm5OSxYShjAGsoEIz/AIx8dxfmbuwu3UOx//8PDITtZDOLC5MH0Y0FWDomrL
NhGc6Ehmo21/uBPUR/6LWlxz/K7ZGzIZOKuXNBSqltLroxwUCEm2u+WR74M26x1W
b8ravHNjkOR/ez4iyz0H7V84dJzjA1BOoa+Y7mHyhD8S
-----END CERTIFICATE-----</pre>
</blockquote>
<p>(<a title="Reference" href="https://knowledge.geotrust.com/support/knowledge-base/index?page=content&amp;id=AR1426&amp;actp=search&amp;viewlocale=en_US&amp;searchid=1283360269668">Reference</a>)</p>
<p>So now your pem or crt file should have 3 certificate sections in it. You need to reload you configuration to pick up the new certificate (nginx). I&#8217;m sure this can be applied for other certificate authorities too, just with other data that you&#8217;ll have to find for yourself.</p>
<h2>Background</h2>
<p>Evidently, desktop browsers, can find and download the intermediate certificate by their own and so it worked there from the start. I don&#8217;t know what the status of this issue is on iOS or other devices.</p>
<p>If I look at the properties of my web site certificate when I visit the site in a desktop browser I can clearly see that there are two parents to my certficiate. The first one being the &#8220;intermediate&#8221; certificate. And since they don&#8217;t seem to be found any other way by Android, it need to be added to the web site certificate as per the solution above.</p>
<p>This tool also helped me verify what I was missing from the start: <a title="SSL Checker" href="http://www.sslshopper.com/ssl-checker.html">http://www.sslshopper.com/ssl-checker.html</a></p>
<p>For the GeoTrust CA certificate (the root, the parent of the intermediate certificate) it seems it was changed 2010-12-09 which I guess is the reason why it was already available on Android 2.3.5 but not on 2.1 (at least not in the emulator). However, the new CA certificate can be chained in its turn to the &#8220;Equifax Secure Certificate Authority Root CA&#8221; which IS available on older Android versions like 2.1 and therefore it will work if you just add it to the end of your other two certificates. At least, this is how I assume it can be explained.</p>
<p>So now I can visit my site over https and not get any warnings neither on Android 2.3.5 or Android 2.1. And more importantly, my visitors can too! Happy days.</p>
<h2>But no SNI, so not multiple certificates with same ip</h2>
<p>Well, not perfect still, unfortunately. On my nginx box I have managed to install multiple certificates for the same ip address. I only have one single ip address on that virtual server (for the time being anyway). It has worked fine with desktop browsers, but while investigating the issue above, I also came across information about how this actually works. And why it doesn&#8217;t work on Android until version 4.0, of which we have no devices yet (at time of writing).</p>
<p>This works thanks to an extension called Server Name Indication, explained here <a href="http://en.wikipedia.org/wiki/Server_Name_Indication">http://en.wikipedia.org/wiki/Server_Name_Indication</a>. This makes it possible for the browser to send the server name (host name / domain name of site) before the actual protection begins. And therefore it is possible for the web server to disinguish between sites and certificates on the same server and ip.</p>
<p>The bad news is that SNI has been slow to be incorporated in the Android OS and is simply not available on older versions than 4.0. At least it should be included in version 4.0 now.</p>
<p>What will happen when an pre-4.0 Android browser connects to the server, is that the browser will always only see the first/default certificate (however you might configure it in the web server). So it will still work for one https site, but for the others the user will get a warning that the certificate doesn&#8217;t correspond to the domain name.</p>
<p>&nbsp;</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=515&amp;md5=9745ab6d0a70e2f1df61e885fdd1ac63" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2011/11/16/making-web-site-certificate-work-for-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2011%2F11%2F16%2Fmaking-web-site-certificate-work-for-android%2F&amp;language=en_GB&amp;category=text&amp;title=Making+web+site+certificate+work+for+Android&amp;description=I+installed+a+web+site+certificate+called+GeoTrust+QuickSSL+from+http%3A%2F%2Fcheapssls.com+on+a+site+of+mine.+That+wasn%26%238217%3Bt+too+hard+I+think.+Everything+looked+fine+when+I+tested+it+on...&amp;tags=android%2Cnginx%2Cssl%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Change virtual console resolution on Ubuntu</title>
		<link>http://www.mikeplate.com/2011/11/08/change-virtual-console-resolution-on-ubuntu/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=change-virtual-console-resolution-on-ubuntu</link>
		<comments>http://www.mikeplate.com/2011/11/08/change-virtual-console-resolution-on-ubuntu/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 09:23:13 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=506</guid>
		<description><![CDATA[When installing Ubuntu Server 11.10 on my Windows-hosted VirtualBox the default console resolution is very small (640&#215;480 I think). But changing it is very easy. The solution In the Ubuntu console window, open the following file in your editor of choice: sudo vim /etc/default/grub Uncomment the following line and set another resolution for it, for [...]]]></description>
			<content:encoded><![CDATA[<p>When installing Ubuntu Server 11.10 on my Windows-hosted VirtualBox the default console resolution is very small (640&#215;480 I think). But changing it is very easy.<span id="more-506"></span></p>
<h2>The solution</h2>
<p>In the Ubuntu console window, open the following file in your editor of choice:</p>
<blockquote><p>sudo vim /etc/default/grub</p></blockquote>
<p>Uncomment the following line and set another resolution for it, for instance 1024&#215;768:</p>
<blockquote><p>GRUB_GFXMODE=1024&#215;768</p></blockquote>
<p>Next, write this new information for the next boot with the command:</p>
<blockquote><p>sudo update-grub2</p></blockquote>
<p>And then reboot to see the effect:</p>
<blockquote><p>sudo shutdown -r 0</p></blockquote>
<p>&nbsp;</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=506&amp;md5=a5e56ce886c14999a360ce75607722df" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2011/11/08/change-virtual-console-resolution-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2011%2F11%2F08%2Fchange-virtual-console-resolution-on-ubuntu%2F&amp;language=en_GB&amp;category=text&amp;title=Change+virtual+console+resolution+on+Ubuntu&amp;description=When+installing+Ubuntu+Server+11.10+on+my+Windows-hosted+VirtualBox+the+default+console+resolution+is+very+small+%28640%26%23215%3B480+I+think%29.+But+changing+it+is+very+easy.+The+solution+In+the+Ubuntu...&amp;tags=ubuntu%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Stop http.sys from listening on port 80 in Windows</title>
		<link>http://www.mikeplate.com/2011/11/06/stop-http-sys-from-listening-on-port-80-in-windows/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=stop-http-sys-from-listening-on-port-80-in-windows</link>
		<comments>http://www.mikeplate.com/2011/11/06/stop-http-sys-from-listening-on-port-80-in-windows/#comments</comments>
		<pubDate>Sun, 06 Nov 2011 14:10:03 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=500</guid>
		<description><![CDATA[On Windows 7 there is a system service called http.sys that listens on port 80. That makes it impossible to run applications and services that haven&#8217;t been integrated with this feature to also listen on port 80, like Apache HTTP Server or Nginx. Here is one way to solve that problem. Background information First of [...]]]></description>
			<content:encoded><![CDATA[<p>On Windows 7 there is a system service called http.sys that listens on port 80. That makes it impossible to run applications and services that haven&#8217;t been integrated with this feature to also listen on port 80, like Apache HTTP Server or Nginx. Here is one way to solve that problem.<span id="more-500"></span></p>
<h2>Background information</h2>
<p>First of all, the way http.sys works on newer Windows versions is a very useful feature. The reason behind http.sys is to have a single system service that listens on the all-important port 80 and be a mediator to different applications and services in the system that might like to handle incoming requests on port 80 but for different urls. What an integrated Windows application does, is use a Windows API for registering its own urls and then have them directed to itself. At the same time, there can be other applications also getting incoming requests to port 80 but for different registered urls.</p>
<h2>The problem</h2>
<p>The problem is that some (many?) open source project that have been compiled for Windows, does not use this http.sys API. Therefore, when they start up and try to bind to port 80, they will fail since it is occupied by http.sys. And I have to admit that I have no idea how easy or hard it would be to patch such a project to make that call on Windows and get the tcp/ip network stack to work with it.</p>
<p>Examples of such projects are:</p>
<ul>
<li>Apache HTTP Server</li>
<li>Nginx</li>
</ul>
<p>(Haven&#8217;t looked at how nodejs does it.)</p>
<h2>The solution</h2>
<p>Some solutions I&#8217;ve found suggests disabling http.sys, but that only works if you are sure you won&#8217;t have any use for Windows applications that do know about http.sys. I didn&#8217;t want to do that.</p>
<p>My suggested solution is instead to tell http.sys to bind to a specific ip-address, and then use any other ip-addresses on your system for Nginx (or whatever you are looking to install). It turns out this is possible via the netsh command line configuration tool in Windows (from version 7 anyway).</p>
<p>So, one way would be to add an extra ip address to your network configuration. But I chose instead to use the fact that every system nowadays has both an ipv4 and an ipv6 address. My solution is to bind http.sys to the ipv6 general address (named &#8220;::&#8221;) and thereby free up the local general ipv4 address (named &#8220;127.0.0.1&#8243;) for my Nginx server.</p>
<p>Telling http.sys to bind to the ipv6 general address is easy. Just run the follow command from a command prompt window as Administrator:</p>
<blockquote><p>netsh http add iplisten ipaddress=::</p></blockquote>
<p>It seems that the name &#8220;localhost&#8221; binds to the ipv6 address, actually &#8220;::1&#8243;, so from now on you can still get the http.sys applications and services via &#8220;http://localhost&#8221;. And if you install and run Nginx on port 80 it should start fine and be available at &#8220;http://127.0.0.1&#8243;. Or you could even edit your &#8220;C:\Windows\System32\drivers\etc\hosts&#8221; file and add the line:</p>
<blockquote><p>127.0.0.1 local</p></blockquote>
<p>Now having &#8220;localhost&#8221; for ipv6/http.sys and &#8220;local&#8221; for ipv4/Nginx.</p>
<p>&nbsp;</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=500&amp;md5=4ffabcf6829a9935f96ca9cb2c9ead23" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2011/11/06/stop-http-sys-from-listening-on-port-80-in-windows/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2011%2F11%2F06%2Fstop-http-sys-from-listening-on-port-80-in-windows%2F&amp;language=en_GB&amp;category=text&amp;title=Stop+http.sys+from+listening+on+port+80+in+Windows&amp;description=On+Windows+7+there+is+a+system+service+called+http.sys+that+listens+on+port+80.+That+makes+it+impossible+to+run+applications+and+services+that+haven%26%238217%3Bt+been+integrated+with+this...&amp;tags=nginx%2Cwindows%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Nginx configuration for hiding directories with underscore</title>
		<link>http://www.mikeplate.com/2011/10/20/nginx-configuration-for-hiding-directories-with-underscore/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nginx-configuration-for-hiding-directories-with-underscore</link>
		<comments>http://www.mikeplate.com/2011/10/20/nginx-configuration-for-hiding-directories-with-underscore/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 16:32:48 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[micro]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=582</guid>
		<description><![CDATA[Nginx configuration for making sure no directories or files starting with an underscore (_) in root can be downloaded from the web server: location ~ ^/_ { return 404; }]]></description>
			<content:encoded><![CDATA[<p>Nginx configuration for making sure no directories or files starting with an underscore (_) in root can be downloaded from the web server: location ~ ^/_ { return 404; }</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=582&amp;md5=b93e47b97a7ac587849934ada7c84ff3" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2011/10/20/nginx-configuration-for-hiding-directories-with-underscore/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2011%2F10%2F20%2Fnginx-configuration-for-hiding-directories-with-underscore%2F&amp;language=en_GB&amp;category=text&amp;title=Nginx+configuration+for+hiding+directories+with+underscore&amp;description=Nginx+configuration+for+making+sure+no+directories+or+files+starting+with+an+underscore+%28_%29+in+root+can+be+downloaded+from+the+web+server%3A+location+%7E+%5E%2F_+%7B+return+404%3B+%7D&amp;tags=nginx%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Nginx map subdomains to subdirectories</title>
		<link>http://www.mikeplate.com/2011/10/18/nginx-map-subdomains-to-subdirectories/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nginx-map-subdomains-to-subdirectories</link>
		<comments>http://www.mikeplate.com/2011/10/18/nginx-map-subdomains-to-subdirectories/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 12:50:43 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[micro]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=580</guid>
		<description><![CDATA[Nginx configuration setting to automatically map subdomains to subdirectories: server_name ~^(.*)\.mysite\.com$; root /srv/www/$1;]]></description>
			<content:encoded><![CDATA[<p>Nginx configuration setting to automatically map subdomains to subdirectories: server_name ~^(.*)\.mysite\.com$; root /srv/www/$1;</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=580&amp;md5=ff7be76418feafb870ca5934f918e8a6" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2011/10/18/nginx-map-subdomains-to-subdirectories/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2011%2F10%2F18%2Fnginx-map-subdomains-to-subdirectories%2F&amp;language=en_GB&amp;category=text&amp;title=Nginx+map+subdomains+to+subdirectories&amp;description=Nginx+configuration+setting+to+automatically+map+subdomains+to+subdirectories%3A+server_name+%7E%5E%28.%2A%29%5C.mysite%5C.com%24%3B+root+%2Fsrv%2Fwww%2F%241%3B&amp;tags=nginx%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Reorganizing commits in Git branches</title>
		<link>http://www.mikeplate.com/2011/04/13/reorganizing-commits-in-git-branches/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=reorganizing-commits-in-git-branches</link>
		<comments>http://www.mikeplate.com/2011/04/13/reorganizing-commits-in-git-branches/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 08:29:25 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[sourcecontrol]]></category>

		<guid isPermaLink="false">http://www2.mikeplate.com/?p=460</guid>
		<description><![CDATA[It may take some time digging around the Git commands to do exactly what you want. I had a situation where I&#8217;ve gone too far in a version branch and needed some reorganization. This is a short description of what commands I ended up running. My situation was pretty basic and maybe typical of a [...]]]></description>
			<content:encoded><![CDATA[<p>It may take some time digging around the Git commands to do exactly what you want. I had a situation where I&#8217;ve gone too far in a version branch and needed some reorganization. This is a short description of what commands I ended up running.<span id="more-460"></span></p>
<p>My situation was pretty basic and maybe typical of a beginner in Git land. I had created a single extra branch from my master branch, named after the version I was working on. &#8220;3.1&#8243; in this case. I had like 40 commits to the &#8220;3.1&#8243; branch and no changes/commits to the master branch. The problem was that the version 3.1 had been released several commits ago. I had forgotten how to work with branches and continued to commit changes in the 3.1 branch, even though they belonged to the next 3.2 version.</p>
<p>So here are a few commands I ended up running to fix this. My goal was to get the master branch up to date, remove the commits from 3.1 branch that actually belonged to the next version and create the new 3.2 branch with those commits.</p>
<h2>Merging branches</h2>
<p>First step was to get the master branch up to date with all changes currently made to 3.1 branch. This is easy:</p>
<ul>
<li><a title="git checkout reference" href="http://www.kernel.org/pub/software/scm/git/docs/git-checkout.html">git checkout</a> master</li>
<li><a title="git merge reference" href="http://www.kernel.org/pub/software/scm/git/docs/git-merge.html">git merge</a> 3.1</li>
</ul>
<p>Note that you first change to the branch that should be updated and then tell Git to merge in the commits from another branch, in this case named &#8220;3.1&#8243;.</p>
<p>Also, since I hadn&#8217;t made any commits to the master branch, the merge is also called &#8220;fast-forward&#8221; since all changes can just be copied onto the master branch.</p>
<h2>Listing commits and find a specific date</h2>
<p>Next question was where I actually released version 3.1 and thereby where the 3.1 branch should be &#8220;cut&#8221;. For this I did:</p>
<ul>
<li><a title="git show-branch reference" href="http://www.kernel.org/pub/software/scm/git/docs/git-show-branch.html">git show-branch</a> --more=20 3.1</li>
<li>git checkout 3.1</li>
<li><a title="git log reference" href="http://www.kernel.org/pub/software/scm/git/docs/git-log.html">git log</a> --pretty=&#8221;format:%ci %h %s&#8221; -20</li>
</ul>
<p>Actually, the &#8220;git show-branch&#8221; didn&#8217;t help as much as &#8220;git log&#8221;, but I kept them both here for reference. I find that Git is sometimes a bit inconsistent, like specifying the number of commits to show as &#8220;--more=20&#8243; in one case and &#8220;-20&#8243; in another case.</p>
<p>Using &#8220;git log&#8221; and the format specifier I found the last commit that truly belonged to 3.1. I then used the short commit name from the output (code %h, 7 hex digits) for the next step.</p>
<h2>Removing commits from a branch</h2>
<p>Note that I already have copied all of my commits, both before and after the release of 3.1, onto the master branch. Therefore I will not remove anything important in this step. However, in other situations I am permanently removing code changes from a branch with this command so be careful when you do it yourself.</p>
<p>Using the commit shortname from the previous step, the removal of all commits after a specific one can be done in a single command.</p>
<ul>
<li><a title="git reset reference" href="http://www.kernel.org/pub/software/scm/git/docs/git-reset.html">git reset</a> --hard 08d99c4</li>
</ul>
<p>Also note that there will be no trace of the commits now removed. This was exactly what I wanted in this case. There is the &#8220;git revert&#8221; command for other scenarios (I think) where the actual change in the commit tree itself will be logged as a commit.</p>
<p>Since this repository is also hosted on a private server, there was an issue with pushing these changes to that server. The now changed branch (3.1) refused to be pushed. I guess this is to stop you from destroying other peoples work in that branch. Anyway, since I knew it was ok, I forced the push like:</p>
<ul>
<li><a title="git push reference" href="file:///C:/Users/Mikael/Applications/PortableGit/doc/git/html/git-push.html">git push</a> --all --force origin</li>
</ul>
<h2>Create a new version branch from master</h2>
<p>This step I had done before, but for completeness my last commands were:</p>
<ul>
<li>git checkout master</li>
<li><a title="git branch reference" href="http://www.kernel.org/pub/software/scm/git/docs/git-branch.html">git branch</a> 3.2</li>
</ul>
<p>And off I went to continue development on the 3.2 branch.</p>
<p>Side note: I&#8217;m not really sure what the best practice is here regarding &#8220;leaving the master branch behind&#8221; when moving on with the 3.2 branch. Of course, once I release 3.2 I&#8217;ll repeat updating master with all 3.2 changes, but during the development the master branch is left alone in my scenario. Maybe is should be moved forward too? Something to investigate for me, or for the initiated reader to add as a comment.</p>
<p>&nbsp;</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=460&amp;md5=4398dc4eb23332d2eced433c34e9fe15" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2011/04/13/reorganizing-commits-in-git-branches/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2011%2F04%2F13%2Freorganizing-commits-in-git-branches%2F&amp;language=en_GB&amp;category=text&amp;title=Reorganizing+commits+in+Git+branches&amp;description=It+may+take+some+time+digging+around+the+Git+commands+to+do+exactly+what+you+want.+I+had+a+situation+where+I%26%238217%3Bve+gone+too+far+in+a+version+branch+and...&amp;tags=git%2Csourcecontrol%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Beauty of Vim key combinations</title>
		<link>http://www.mikeplate.com/2011/01/23/beauty-of-vim-key-combinations/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=beauty-of-vim-key-combinations</link>
		<comments>http://www.mikeplate.com/2011/01/23/beauty-of-vim-key-combinations/#comments</comments>
		<pubDate>Sun, 23 Jan 2011 14:29:41 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[editor]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://www2.mikeplate.com/?p=19</guid>
		<description><![CDATA[I can&#8217;t yet say that I prefer Vim for editing text files in all situations. But I can say that I like it enough this far to continue on improving my Vim skills. One of the beautiful aspects of Vim is how keyboard shortcuts can be combined and this article describes some of those combinations. [...]]]></description>
			<content:encoded><![CDATA[<p>I can&#8217;t yet say that I prefer Vim for editing text files in all situations. But I can say that I like it enough this far to continue on improving my Vim skills. One of the beautiful aspects of Vim is how keyboard shortcuts can be combined and this article describes some of those combinations.<span id="more-19"></span></p>
<p>Lets look at a few actions that can be performed in Vim:</p>
<ul>
<li>d to delete</li>
<li>c to change (meaning delete followed by going into insert mode)</li>
<li>v to select</li>
<li>y to yank (meaning copy)</li>
</ul>
<p>Lets then look at a few movements that can be performed in Vim:</p>
<ul>
<li>w for next word</li>
<li>j for next line (or down arrow)</li>
<li>f followed by single character for next occurrence on the current line, including the character</li>
<li>t followed by single character for next occurrence on the current line, excluding the character</li>
<li>0 (zero) for start of current line (or home key)</li>
<li>$ for end of current line (or end key)</li>
</ul>
<p>Now consider that every action can be combined with every movement. So for every action, you can use any movement, and perform that action on the content covered by the movement.</p>
<p>A few examples:</p>
<ul>
<li>dw to delete the next word</li>
<li>vt) to select everything until the next closing parenthesis on the current line</li>
<li>c0 to change everything from the start of the current line to the cursor position</li>
<li>yj to copy the rest of the current line and the beginning of the next line up to the cursor position</li>
</ul>
<p>Also, every movement can be repeated by prefixing the movement with a number, describing the movement count. Note that some movements make no sense in repeating, like the start and end of the current line (the count will not have any extra effect). A few examples:</p>
<ul>
<li>d3w to delete the next three words</li>
<li>v2t) to select everything until the second closing parenthesis on the current line</li>
</ul>
<p>Isn&#8217;t it beautiful? So even though the chosen keyboard shortcuts, and their combinations, might seem strange in a single case, there is order and thought to how they was constructed. And this is also what makes vim kind of fun to use. You can always find new combinations to enhance your editing, combinations you&#8217;ve not thought of before. Constant room for improving your skills.</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=19&amp;md5=4ab70a443b24e1a5eaf21bf30045030a" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2011/01/23/beauty-of-vim-key-combinations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2011%2F01%2F23%2Fbeauty-of-vim-key-combinations%2F&amp;language=en_GB&amp;category=text&amp;title=Beauty+of+Vim+key+combinations&amp;description=I+can%26%238217%3Bt+yet+say+that+I+prefer+Vim+for+editing+text+files+in+all+situations.+But+I+can+say+that+I+like+it+enough+this+far+to+continue+on+improving...&amp;tags=editor%2Cvim%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Bash script to copy permissions</title>
		<link>http://www.mikeplate.com/2010/12/04/bash-script-to-copy-permissions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=bash-script-to-copy-permissions</link>
		<comments>http://www.mikeplate.com/2010/12/04/bash-script-to-copy-permissions/#comments</comments>
		<pubDate>Sat, 04 Dec 2010 21:31:29 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[bashscript]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www2.mikeplate.com/?p=5</guid>
		<description><![CDATA[This is a bash script that I&#8217;ve used on Ubuntu in order to copy the permissions from a file or directory, onto the whole structure of another directory. I&#8217;ve found that this is something I like to do quite often and wanted some automation for this operation. First of all I needed a few commands [...]]]></description>
			<content:encoded><![CDATA[<p>This is a bash script that I&#8217;ve used on Ubuntu in order to copy the permissions from a file or directory, onto the whole structure of another directory. I&#8217;ve found that this is something I like to do quite often and wanted some automation for this operation.<span id="more-5"></span></p>
<p>First of all I needed a few commands for my script. They were:</p>
<pre>chown -R username:groupname dirname
chmod -R flags dirname</pre>
<p>Where</p>
<ul>
<li>dirname is the name of the destination directory</li>
<li>username is the name of the owner that will own the directory</li>
<li>groupname is the name of the group that will own the directory</li>
<li>flags is the numerical value for the permissions of the directory</li>
<li>-R specifies that the operation will occur recursively for all children of the directory</li>
</ul>
<p>Next step was to retrieve the current values for an existing file or directory that I know I wanted to copy the permissons from. For that, I can use the stat command with a format string:</p>
<pre>stat -c%u:%g filename
stat -c%a filename</pre>
<p>Where</p>
<ul>
<li>filename is the existing file (or directory)</li>
<li>-c specifices that the output of the command should include and be formatted according to the following format</li>
<li>%u means the id of the user that owns the file</li>
<li>%g means the id of the group that owns the file</li>
<li>%a means the numerical value for the permissions of the file</li>
</ul>
<p>Next, I needed to provide the output of the stat commands as input (or part of the input) to the chown and chmod commands. This is done with Bash using the following notation:</p>
<pre>command $(command-with-output)</pre>
<p>This led me to the following commands:</p>
<pre>chown -R $(stat -c%u:%g source) destination
chmod -R $(stat -c%a source) destination</pre>
<p>If you specify your own source and destination, these commands can be run from the Bash prompt. But I also wanted to put them inside a Bash script so I didn&#8217;t have to do all this typing. I also added some validation and came up with the following script:</p>
<pre>#!/bin/bash
if [ -z $1 ] || ([ ! -d $1 ] &amp;&amp; [ ! -f $1 ]); then
  echo "Not a file or directory as first argument"
  exit 0
fi
if [ -z $2 ] || [ ! -d $2 ]; then
  echo "Not a directory as second argument"
  exit 0
fi
chown -R $(stat -c%u:%g $1) $2
chmod -R $(stat -c%a $1) $2</pre>
<p>Some things to note from the script:</p>
<ul>
<li>-z is true if the following variable is empty (zero length)</li>
<li>-d is true if the following variable is a directory</li>
<li>-f is true if the following variable is a directory</li>
<li>! negates the condition</li>
<li>|| means &#8220;or&#8221; and &amp;&amp; means &#8220;and&#8221;</li>
<li>The semicolon before &#8220;then&#8221; is important, but can be omitted if &#8220;then&#8221; is moved to a line of its own</li>
</ul>
<p>This script has served me well. I hope you&#8217;ll find it useful too.</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=5&amp;md5=e109834878479626b81265cf08aa2687" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2010/12/04/bash-script-to-copy-permissions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2010%2F12%2F04%2Fbash-script-to-copy-permissions%2F&amp;language=en_GB&amp;category=text&amp;title=Bash+script+to+copy+permissions&amp;description=This+is+a+bash+script+that+I%26%238217%3Bve+used+on+Ubuntu+in+order+to+copy+the+permissions+from+a+file+or+directory%2C+onto+the+whole+structure+of+another+directory.+I%26%238217%3Bve+found...&amp;tags=bash%2Cbashscript%2Clinux%2Cubuntu%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Install Android development environment on Windows 7</title>
		<link>http://www.mikeplate.com/2010/11/01/install-android-development-environment-on-windows-7/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=install-android-development-environment-on-windows-7</link>
		<comments>http://www.mikeplate.com/2010/11/01/install-android-development-environment-on-windows-7/#comments</comments>
		<pubDate>Mon, 01 Nov 2010 20:37:25 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=367</guid>
		<description><![CDATA[About a year ago I wrote a blog post about how to get started with Android development on Windows. At lot of new versions has been released since then, even though the basic packages needed are the same. This is a recap of what to download and how to set it up in order to [...]]]></description>
			<content:encoded><![CDATA[<p>About a year ago I wrote a blog post about how to get started with Android development on Windows. At lot of new versions has been released since then, even though the basic packages needed are the same. This is a recap of what to download and how to set it up in order to develop Android applications on Windows 7 (64-bit).<span id="more-367"></span></p>
<p>My background is primarily in Microsoft.NET and Microsoft Visual Studio, so it did feel a little different when I first entered the world of Java and Android development. But once you get past those initial hurdles, it isn&#8217;t as different as you first might have thought.</p>
<p>These are the steps to get started with Android development on Windows. Should work on any Windows version, but I&#8217;m using Windows 7 64-bit.</p>
<ol>
<li>Download and install Java SDK.</li>
<li>Download and unzip Android SDK.</li>
<li>Run &#8220;SDK Manager&#8221; from Android SDK to download the platform versions.</li>
<li>Download and unzip Eclipse.</li>
<li>Install the Android plugin for Eclipse and point to the Android SDK.</li>
</ol>
<p>You can read the instructions here, or watch a screencast I made describing the same procedure:</p>
<p><a href="http://vimeo.com/16392228">http://vimeo.com/16392228</a></p>
<h3>Download and install Java SDK</h3>
<p>You&#8217;ll find the Java SDK here:</p>
<p><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">http://www.oracle.com/technetwork/java/javase/downloads/index.html</a></p>
<p>Click the &#8220;Download JDK&#8221; button and choose the Windows version you have from the Platform combobox. You don&#8217;t have to register to download. I ended up downloading a file called &#8220;jdk-6u22-windows-x64.exe&#8221;. Run it and accept all defaults to complete the installation.</p>
<h3>Download and unzip Android SDK</h3>
<p>The Android SDK doesn&#8217;t have an install executable, but rather a zip file and a utility called &#8220;SDK Manager.exe&#8221;. Begin by downloading the sdk from here:</p>
<p><a href="http://developer.android.com/sdk/index.html">http://developer.android.com/sdk/index.html</a></p>
<p>Unzip the file to any location you want. I put it in &#8220;C:\Android\SDK&#8221;. Inside that folder, you&#8217;ll find the executable &#8220;SDK Manager.exe&#8221;. Run it. Every time you run that application, it will look for updates to anything you&#8217;ve downloaded previously with the same tool. The first time it will default to download all platform versions of Android except the Google specific ones. I&#8217;d recommend selecting Cancel at this initial update dialog, and then download what you need from &#8220;Available packages&#8221; instead.</p>
<p>The Android platform has been released in several versions since its initial offering of version 1.0. Every Android application will require a specific version, but will of course work on all subsequent versions too (at least in a perfect world). Today, I recommend that you target version 2.1 as the least common denominator. But note that there might still be users and phones of at least version 1.6 that might be of interest to you. Anyway, this means that I would download version 2.1 and the latest version 2.2. They don&#8217;t really take up that much disk space (maybe ~100MB per version?), so it won&#8217;t hurt you too much to download them all either.</p>
<p>Also, every Android platform version comes in a &#8220;Android plain vanilla&#8221; variant and a &#8220;Google APIs&#8221; variant. The only differences between those two are that &#8220;Google APIs&#8221; variant will include the ability to use Google Maps component and some other Google specific APIs that some Android devices might not support. I found this naming to be a little strange at first, but note that &#8220;Google APIs&#8221; includes everything in &#8220;Android plain vanilla&#8221; too.</p>
<p>The point is that if your application doesn&#8217;t need things such as Google Map component, you will be targeting more devices if you choose to develop for &#8220;Android plain vanilla&#8221;. As far as I know, all commonly sold Android phones support the Google APIs and it is only simpler media players and maybe cheap tablets that might not have chosen to support Google APIs (since I assume the manufacturer pays licensing fees to Google for that).</p>
<h3>Run &#8220;SDK Manager&#8221; from Android SDK to download the platform versions</h3>
<p>So, running &#8220;SDK Manager&#8221;, cancelling on the first update dialog box, you&#8217;ll move on to &#8220;Available packages&#8221;. I recommend checking the following items and then click &#8220;Install selected&#8221;: (revision numbers can of course have changed since I wrote this)</p>
<ul>
<li>SDK Platform Android 2.2, API 8, revision 2</li>
<li>SDK Platform Android 2.1, API 7, revision 2</li>
<li>Samples for SDK API 8, revision 1</li>
<li>Google APIs by Google Inc., Android API 8, revision 2</li>
<li>Google APIs by Google Inc., Android API 7, revision 1</li>
<li>Usb Driver package, revision 3</li>
<li>Market Licensing package, revision 1</li>
</ul>
<p>You&#8217;ll need to &#8220;Accept all&#8221; licenses and then the download should start. You might get a question if it is ok to restart ADB at the end of the installation procedure, which it is!</p>
<h3>Download and unzip Eclipse</h3>
<p>You don&#8217;t have to use Eclipse. You could stop here and use a command line utility from the Android SDK to create project skeletons and edit the files with any text file editor. But I think you&#8217;ll enjoy the full development environment of Eclipse, even though it seems to have gotten its share of complaints over the years.</p>
<p>Download Eclipse from here:</p>
<p><a href="http://www.eclipse.org/downloads/">http://www.eclipse.org/downloads/</a></p>
<p>The first option of &#8220;Eclipse IDE for Java Developers&#8221; is what you want, and click the link to the right depending on what Windows version you&#8217;re on. I ended up downloading &#8220;eclipse-java-helios-SR1-win32-x86_64.zip&#8221;.</p>
<p>Eclipse doesn&#8217;t have an install at all. Just unzip the files into any folder you like and start the environment with &#8220;eclipse.exe&#8221;.</p>
<h3>Install the Android plugin for Eclipse and point to the Android SDK</h3>
<p>Inside Eclipse, you need to do a few things before you can create your first Android project. When you start Eclipse, you choose your &#8220;Workspace&#8221;. Just accept the default. This is the base folder where you will put all of your projects, and can of course be changed later on.</p>
<p>Now, do the following:</p>
<ol>
<li>Select Help menu</li>
<li>Install new software</li>
<li>Add</li>
<li>Type any Name, like &#8220;Android&#8221;</li>
<li>Paste this address in Location: https://dl-ssl.google.com/android/eclipse/</li>
<li>OK</li>
<li>After a few seconds, the list should contain &#8220;Developer Tools&#8221; and there you&#8217;ll find &#8220;Android DDMS&#8221; and &#8220;Android Development Tools&#8221;.</li>
<li>Select both and then Next</li>
<li>Continue with the download and install. You&#8217;ll be asked to restart Eclipse at the end. Do that.</li>
</ol>
<p>Back in Eclipse with the Android plugin now installed, you also need to:</p>
<ol>
<li>Select Window menu</li>
<li>Preferences</li>
<li>Android</li>
<li>Click Browse button next to &#8220;SDK Location&#8221;</li>
<li>Point to the folder where you unzipped the Android SDK previously. For me, that would be &#8220;C:\Android\SDK&#8221; (where the &#8220;SDK Manager.exe&#8221; is located)</li>
<li>OK</li>
<li>Apply</li>
<li>After a few seconds the list should fill with the Android platforms you chose to download with &#8220;SDK Manager&#8221; previously</li>
<li>OK</li>
</ol>
<p>That&#8217;s it!</p>
<h3>Create your first Android project</h3>
<p>Now you should be able to create your first Android project. Still in Eclipse:</p>
<ol>
<li>Select File menu</li>
<li>New</li>
<li>Project</li>
<li>Android, Android Project</li>
<li>Next</li>
<li>Project name: My first project</li>
<li>Build Target, Target Name: check any platform version you&#8217;d like to start with</li>
<li>Application name: MyFirstProject</li>
<li>Package name: com.myname.MyFirstProject</li>
<li>Create Activity: (checked) MainActivity</li>
<li>Min SDK Version: (can be left empty)</li>
<li>Next</li>
<li>Finish (no test project for your first attempt)</li>
</ol>
<p>Now, right-click the project name in the left pane and choose &#8220;Run as&#8221; and then &#8220;1 Android Application&#8221;. The emulator should start and after some time (it IS really slow to start), you&#8217;ll be running your first Android application. If the emulator starts, but not your application, just keep the emulator running and switch back to Eclipse and execute the &#8220;Run as&#8221; command again. Sometimes it fails on the first attempt.</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=367&amp;md5=ea2cab87c2eb6afe51a6c892a00d0be3" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2010/11/01/install-android-development-environment-on-windows-7/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2010%2F11%2F01%2Finstall-android-development-environment-on-windows-7%2F&amp;language=en_GB&amp;category=text&amp;title=Install+Android+development+environment+on+Windows+7&amp;description=About+a+year+ago+I+wrote+a+blog+post+about+how+to+get+started+with+Android+development+on+Windows.+At+lot+of+new+versions+has+been+released+since+then%2C+even...&amp;tags=android%2Ceclipse%2Cwindows%2Cblog" type="text/html" />
	</item>
		<item>
		<title>CSS3 Playground</title>
		<link>http://www.mikeplate.com/2010/07/28/css3-playground/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=css3-playground</link>
		<comments>http://www.mikeplate.com/2010/07/28/css3-playground/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 11:36:39 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[webapps]]></category>
		<category><![CDATA[webdesign]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=322</guid>
		<description><![CDATA[After being excited about css3 and using some of its features in my web pages, I realized that I needed some simple tool (editor) for experimenting.]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://css3.mikeplate.com">CSS3 Playground</a> is a web application for experimenting with some new css3 capabilities. Primarily those than conform to the progressive enhancement philosophy, which means that the styles can be used on all sites without severely changing/limiting the experience on browsers that does not support them (=Internet Explorer 8.0 and older). But there are also styles that may need better replacements on Internet Explorer such as rotation, and I hope I will have the time to add more in the future.</p>
<p><span id="more-322"></span></p>
<p><a href="http://css3.mikeplate.com/"><img class="alignnone size-full wp-image-337 screendump" title="CSS3 Playground" src="http://www2.mikeplate.com/wp-content/uploads/2010/07/css3playground.png" alt="Screen dump of the site" width="547" height="395" /></a></p>
<h2>Inspiration</h2>
<p>I&#8217;m a developer and not a web designer, but that doesn&#8217;t keep me from learning and experimenting with html5 and css3 stuff. The inspiration for this web application came from some really good talks at <a href="http://futureofwebdesign.com/london-2010/">Future of Web Design in London 2010</a>. I wasn&#8217;t there, but purchased the recorded videos which I can recommend to all interested in web design (and web developers too, obviously). The recorded quality isn&#8217;t perhaps the best, but the speakers and the content made up for that. Especially <a href="http://simplebits.com/">Dan Cederholm</a> and <a href="http://denisejacobs.com/">Denise Jacobs</a>.</p>
<p>I wasn&#8217;t aware of the philosophies &#8220;Progressive enhancement&#8221; and/or &#8220;Graceful degradation&#8221; previously so this was a bit of a late awakening for me. Now I see what has been brewing in the web design and browser world for a while and why Microsoft finally was pushed to comply a lot better to all things css3 with Internet Explorer 9.</p>
<p>Then, of course, it&#8217;s another story that html5 and css3 are not standardized yet.</p>
<h2>Building</h2>
<p>Building this simple web application was also a good opportunity for me to really use a JavaScript user interface library since the application is client side only. I was choosing between <a href="http://jqueryui.com/">jQuery UI</a> and <a href="http://www.dojotoolkit.org/reference-guide/dijit/index.html">Dijit</a> (Dojo&#8217;s ui library) and what made to go for Dijit was the fact that the mouse scroll wheel can be used on slider controls. I thought that made a nice feature when moving shadows and testing other numbers based properties.</p>
<p>I have mostly looked at jQuery previously, but I will investigate <a href="http://dojotoolkit.org/">Dojo</a> even further now with the good experiences I&#8217;ve had buildning CSS3 Playground.</p>
<p>The best specification of all css3 properties and browser support that I&#8217;ve found is <a href="http://www.caniuse.com">Where can I use</a>. I used that specification and the linked samples and documentation to learn about the specifics of the css3 styles that I wanted to support in the web application.</p>
<h2>Use</h2>
<p>So I recommend you try it out. I hope it will be a better way to try out some color and shadow combinations than to type the css manually and use your ordinary tools and previews. It will of course be of most use to you who know css pretty well, but has not had the time to check out css3 yet.</p>
<p>Add more preview boxes with the <img class="alignnone" src="http://css3.mikeplate.com/add.png" alt="" width="16" height="16" /> button. When changing the styles, only the last box clicked will be affected. I didn&#8217;t want to distort the preview with a visual selection effect for the current box, so besides a small transparency animation when clicking other boxes, you won&#8217;t be able to see which box is actually the current one. Remove the current box with the <img class="alignnone" src="http://css3.mikeplate.com/delete.png" alt="" width="16" height="16" /> button.</p>
<p>Below the preview, you will see two collections of styles. The left one is the one you should use (copy and paste). It contains the styles for different browsers. The right one is read live from the browser of your choice and it might be interesting to see how different browsers actually chose to store the styles, but you should not copy and paste that part in your web pages and stylesheet files.</p>
<p>My favorite styles that I will start to use are the easy ones such as border radius and box shadow. Considering how easy they are, the difficulty will probably be not to overuse them.</p>
<p><a href="http://css3.mikeplate.com">http://css3.mikeplate.com</a></p>
<h2>Updates</h2>
<p>Web application was updated to version 1.1 on august 17 with the following:</p>
<ul>
<li>&#8220;Add&#8221;-button for adding as many boxes as you like and change their styles independently.</li>
<li>&#8220;Delete&#8221;-button for removing any extra added boxes.</li>
<li>Font selection. Either type the font(s) manually, or select from a few font stacks.</li>
<li>Bold and/or italic styles.</li>
<li>Bug fix where changing the contents removed the resizing capability.</li>
</ul>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=322&amp;md5=8d95f5927cde4ef8a3232d59ddd7c963" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2010/07/28/css3-playground/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2010%2F07%2F28%2Fcss3-playground%2F&amp;language=en_GB&amp;category=text&amp;title=CSS3+Playground&amp;description=The+CSS3+Playground+is+a+web+application+for+experimenting+with+some+new+css3+capabilities.+Primarily+those+than+conform+to+the+progressive+enhancement+philosophy%2C+which+means+that+the+styles+can+be...&amp;tags=css3%2Cwebapps%2Cwebdesign%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Scandinavian Web Developers Conference Day 2</title>
		<link>http://www.mikeplate.com/2010/06/07/scandinavian-web-developers-conference-day-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=scandinavian-web-developers-conference-day-2</link>
		<comments>http://www.mikeplate.com/2010/06/07/scandinavian-web-developers-conference-day-2/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 14:57:25 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[swdc]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=312</guid>
		<description><![CDATA[This is a short summary of the talks during the second day of Scandinavian Web Developers Conference in Stockholm, June 3, 2010. Please note that it is a very short summary with abbreviations and contexts that might only make sense to me (if even that). The second day is about the mobile web development. Wolfram [...]]]></description>
			<content:encoded><![CDATA[<p>This is a short summary of the talks during the second day of  Scandinavian Web Developers Conference in Stockholm, June 3, 2010. Please  note that it is a very short summary with abbreviations and contexts  that might only make sense to me (if even that).</p>
<p><span id="more-312"></span></p>
<p>The second day is about the mobile web development.</p>
<h2>Wolfram Kriesing &#8211; Cross-platform mobile apps.</h2>
<ul>
<li>From Uxebu.</li>
<li>App vs widget.</li>
<li>Chosen name is &#8220;Html5 Apps&#8221;. Named by PPK. Better sale with html5 in the name.</li>
<li>Makers of Event Ninja.</li>
<li>bit.ly/distimo-appstores</li>
<li>eventninja.net/webkit/</li>
<li>Feels like iPhone native.</li>
<li>TouchScroll is a JavaScript and css3-based scroller.</li>
<li>yourappshop.com selling web apps for mobile, just iPhone currently.</li>
<li>W3C Widget contains alyout, design, ajax, config.xml, icon in a zip renamed to &#8220;.wgt&#8221; file extension.</li>
<li>Using Dojo.</li>
<li>No great tools.</li>
<li>You can stay on your desktop as long as you need.</li>
<li>Opera DragonFly can debug on the phone.</li>
<li>Runtime coming to Android, N60. TV widget has appeared.</li>
<li>W3C Specification, JIL Specification, BONDI.</li>
<li>Vodafone offering JIL.</li>
<li>JavaScript is becoming first class citizen on phones via widgets.</li>
<li>Platform adaptions.</li>
<li>Blackberry will switch to WebKit.</li>
<li>PhoneGap filling the gap since all phones don&#8217;t support everything natively.</li>
<li>PhoneGap is open source. PhoneGap+SDK is about 240 kB.</li>
<li>Dojo-mobile. Have to optimize js-libs. Took apart dojo, Just what you need. dojo-webkit-mobile.js.</li>
<li>bit.ly/webdev-events</li>
<li>[Mentions YQL.]</li>
<li>Can stay offline &#8211; no roaming charges.</li>
<li>Is loving Google Calendar as a data store.</li>
<li>Titanium. Custom tags, not as good. Just iPhone, Android and Blackberry.</li>
</ul>
<h2>Mikael Kindborg &#8211; The New Mobile Web &#8211; A Web of Scripted Applications</h2>
<ul>
<li>From Dynamic.</li>
<li>MoSync, tools for cross platform programs.</li>
<li>Magic Words game written in Smalltalk.</li>
<li>JavaScript rediscovered, similar to Lisp.</li>
<li>CoffeeScript.</li>
<li>Theme: What happens when you go late bound.</li>
<li>Showing artist doing lego sculptures. brickartist.com. Nathan Sawaya.</li>
<li>Spredsheets are actuall an example of a functional programming model.</li>
<li>wpri.org doing research.</li>
<li>DroidScript by Micke.</li>
<li>Clamato project is Smalltalk implemented in JavaScript.</li>
<li>Seaside, code browser in web browser. [Nice demo. Would be nice to do in .NET]</li>
<li>DroidScript is open source. Based on Mozilla Rhino.</li>
<li>No byte code generation on Android. Would be needed for subclassing.</li>
<li>Typing code on desktop and sending via tcp to phote to run it. Can open urls. [Smart!]</li>
<li>DragonFly editor.</li>
<li>Access to native via packages.</li>
<li>With Smalltalk you select some thing and run it. Considered old today.</li>
<li>divineprogrammer.se.</li>
</ul>
<h2>Claes Nilsson and Thomas Bailey &#8211; The phone in the cloud</h2>
<p>From Sony Ericsson, main sponsor.</p>
<ul>
<li>Claes Nilsson first. Doing device APIs.</li>
<li>The phone as a service.</li>
<li>Main place is W3C DAP APIs.</li>
<li>All define JavaScript API.</li>
<li>One approach is to use trusted control widgets, like Android, BONDI, JIL.</li>
<li>Implicit user consent is other way to go.</li>
<li>Just drafts. No candidates yet.</li>
<li>In addition policy based.</li>
<li>Google suggested REST http as API to local phone.</li>
<li>[Interesting! Why not all APIs accessible by REST?]</li>
<li>Powerbox another suggestion/submission by Google.</li>
<li>Example, user selects images from cloud services or local gallery in phone.</li>
<li>Same code in web application.</li>
<li>Powerbox gives access to resources posted anywhere, including local.</li>
<li>Thomas Bailey. What to do here and now. PlayNow is Sony Ericsson app store.</li>
<li>PhoneGap only Android+Symbian [In Sony Ericsson products.]</li>
<li>Simplifying html development with PhoneGap. Sony Ericsson WebSDK has a small ide in the browser. Not necessary with Eclipse for PhoneGap development. Proof of concept (beta).</li>
<li>WARP = Web Applicaiton Runtime</li>
</ul>
<h2>Tom Hughes-Croucher &#8211; Mobile Data &#8211; How to avoid the latency trap when using web services</h2>
<ul>
<li>From Yahoo.</li>
<li>Microwaves and baby monitors fuck up wifi. Interference.</li>
<li>Ofcom in UK = Swedish Post och Telestyrelsen.</li>
<li>Up to 90% bandwidth used on protocol.</li>
<li>1, 6 and 11 channels for every device. All others overlap.</li>
<li>Reduce sequential requests. Gzip.</li>
<li>Yahoo Performenace Guidelines.</li>
<li>Mike Belshe researches page load times.</li>
<li>Browserscope.org</li>
<li>Maximize concurrent requests with labjs.com.</li>
<li>Packet research from Yahoo.</li>
<li>YQL bindings to services. (Don&#8217;t like XML/RPC based Dave Winer.)</li>
<li>YQL is a unified interface to web services. Self-describing. [Feels a bit like fluiddb?]</li>
<li>Help bundle lots of requests into a single one.</li>
</ul>
<h2>Tom Blackmore &#8211; Handling spatial data on the web</h2>
<ul>
<li>Formed own company Arctic Tiger. Did mapping for hitta.se. Geobased solutions.</li>
<li>We can remove a lot of questions in user interfaces with knowing geolocation data.</li>
<li>Your phone has better accuracy than car navigator, thanks to AGPS.</li>
<li>W3C Geolocation API (the html5 name).</li>
<li>navigator.geolocation, watchPosition. Don&#8217;t forget clearWatch.</li>
<li>getCurrentPosition(usePos, posErr, { enableHighAccuracy&#8221; : true });</li>
<li>geo.js code.google.com/p/geo-location-javascript</li>
<li>Done via TurboGears in older Android.</li>
<li>Lantmäteriet, cost for single internet public facing site, one purpose, one company, is 1,3 MSEK.</li>
<li>Google Maps. Yahoo Geoplanet Data. OpenStreetMap (creative commons). GeoNames (good zips).</li>
<li>Spatial databases, PostGIS, SpatialLite, GeoCouchDB.</li>
<li>github.com/vmx/couchdb</li>
<li>ST-Contains, ST-GeometryFromText</li>
<li>2 decimals in lat/long is about 1 kilometer precision.</li>
</ul>
<h2>Nikolai Onken &#8211; Human APIs, expanding the mobile web to the real world</h2>
<ul>
<li>From Uxebo.</li>
<li>Dojo committer.</li>
<li>Gordon JavaScript for Flash.</li>
<li>Raphael cool lib.</li>
<li>blog.uxebo.com, humanapi.org</li>
<li>Arduino board.</li>
</ul>
<h2>Michael Mahemoff &#8211; HTML5 gives you wings</h2>
<ul>
<li>From ajaxpatterns and ajaxian.org.</li>
<li>Just been to uxcamp Europe.</li>
<li>Automatic persistance is new ux pattern.</li>
<li>piratepad.</li>
<li>Jolicloud.</li>
<li>Cross-origin Resource Sharing (html5).</li>
<li>Application Cache (html5).</li>
<li>3.ly/timer</li>
<li>&lt;html manifest=&#8221;timer.manifest&#8221;&gt;</li>
<li>Cache manifest. Fallback network.</li>
<li>window.applicationCache.status, localStorage, sessionStorage tied to the domain.</li>
<li>WebSQL Data. IndexedDB soon.</li>
<li>JSON.stringify, JSON.parse</li>
<li>localStorage strings only right now. Events.</li>
<li>Same application can be opened in multiple tabs and communicate with each other.</li>
<li>&lt;a rel=&#8221;pingback&#8221;&gt;</li>
</ul>
<h2>Henk Jurriens &#8211; Building linked data applications for the iPhone</h2>
<ul>
<li>Data sets. DBPedia, GeoName, FADF.</li>
<li>Linked data is about semantic web.</li>
<li>URI. RDF. SPARQL.</li>
<li>RDFa names things with URIs.</li>
<li>Dublin Core. FOAF.</li>
<li>data.gov. BBC.</li>
<li>Drupal and WordPress uses linked data. Also Facebook.</li>
<li>jQTouch.js, iProcessing.js, Lodsy.</li>
</ul>
<h2>Tim Caswell &#8211; Node.js powered mobile apps for end-to-end JavaScript development</h2>
<ul>
<li>Ryan is creator of Node.js.</li>
<li>Creator of node framework Connect.</li>
<li>Built-in filter modules. Data providers.</li>
<li>Single stack model.</li>
</ul>
<p>A bit shorter in the wrap-up of the conference, but a good two days.</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=312&amp;md5=93c8740c00405d4c0b26372fe64b7fe3" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2010/06/07/scandinavian-web-developers-conference-day-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2010%2F06%2F07%2Fscandinavian-web-developers-conference-day-2%2F&amp;language=en_GB&amp;category=text&amp;title=Scandinavian+Web+Developers+Conference+Day+2&amp;description=This+is+a+short+summary+of+the+talks+during+the+second+day+of+Scandinavian+Web+Developers+Conference+in+Stockholm%2C+June+3%2C+2010.+Please+note+that+it+is+a+very+short...&amp;tags=swdc%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Scandinavian Web Developers Conference Day 1</title>
		<link>http://www.mikeplate.com/2010/06/02/scandinavian-web-developers-conference-day-1/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=scandinavian-web-developers-conference-day-1</link>
		<comments>http://www.mikeplate.com/2010/06/02/scandinavian-web-developers-conference-day-1/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 20:42:36 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[swdc]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=303</guid>
		<description><![CDATA[This is a short summary of the talks during the first day of Scandinavian Web Developers Conference in Stockholm, June 2 2010. Please note that it is a very short summary with abbreviations and contexts that might only make sense to me (if even that). Peter Svensson welcomes us to the conference. No wifi for [...]]]></description>
			<content:encoded><![CDATA[<p>This is a short summary of the talks during the first day of Scandinavian Web Developers Conference in Stockholm, June 2 2010. Please note that it is a very short summary with abbreviations and contexts that might only make sense to me (if even that).<br />
<span id="more-303"></span><br />
Peter Svensson welcomes us to the conference. No wifi for all, unfortunately. 3G coverage inside the cinema was just ok to work.</p>
<p>The first day is about the front-end and back-end of web development.</p>
<h2>Robert Nyman &#8211; HTML5</h2>
<p>Overview of HTML5 with a slight focus to the actual HTML and not so much all the new apis.</p>
<ul>
<li>In 200 the WhatWG group was formed from Mozilla, Opera and Apple out of dissatisfaction with the W3C work on next HTML. Now Google is also a member of WhatWG.</li>
<li>HTML5 is about semantics, accessibility and apis.</li>
<li>&lt;!DOCTYPE html&gt; Note that there is no version in doctype declaration.</li>
<li>HTML5 isn&#8217;t necessarily XHTML. HTML5 interprets the html in a friendly mannor.</li>
<li>For XHTML no doctype is needed. Must have correct contenttype from server.</li>
<li>Many new input types: color, week range, search. Default to text if type is unknown/unimplemented by browser.</li>
<li>&lt;meta charset=&#8221;utf-8&#8243;&gt;</li>
<li>&lt;header role=&#8221;banner&#8221;&gt; Headers can occur multiple times in the page (for sections within the page).</li>
<li>Role attribute can be used by screen readers.</li>
<li>&lt;nav role=&#8221;navigation&#8221;&gt; Encloses the primary navigation structure of the site.</li>
<li>&lt;article&gt; &lt;section&gt; &lt;header&gt; &lt;hgroup&gt; &lt;h2&gt;title&lt;/h2&gt;</li>
<li>An article element&#8217;s content should make sense outside the page context.</li>
<li>&lt;aside role=&#8221;complimentary&#8221;&gt;, &lt;footer&gt;</li>
<li>Problem with ajax and accessibility. aria-attributes. aria-live. aria-relevant. WAI-ARIA Live Regions.</li>
<li>Video. &#8220;Without Flash we wouldn&#8217;t have youtube&#8221;, &#8220;Things can actually coexist&#8221;, &#8220;Not everything has to kill something&#8221;</li>
<li>video element can follow with several source elements for different codecs. First one supported will be used by browser. object-element fallback can also be used for Flash etc.</li>
<li>WebM project made everyone happy. Will get hardware acceleration. Matters how content providers will support WebM. &#8220;Is optimistic about WebM&#8221;. Kaltura and more.</li>
<li>Canvas. getContent(&#8220;2d&#8221;). A &#8220;3d&#8221; context is also coming. shadowBlur. rotate.</li>
<li>explorercanvas by Emil is canvas for MSIE, translates canvas to VML. Now hired by Google.</li>
<li>Mozilla Bespin is text editor on the web with syntax highlighting.</li>
<li>Geolocation sort of bundled but not part of official specifikation.</li>
<li>Watch dot box on Google Maps if your browser (Firefox) supports geolocation.</li>
<li>Cross document messaging.</li>
<li>Drag&#8217;n drop. Reverse engineered from old MSIE. Not liked. May change.</li>
<li>Nice that HTML5 is here today. Google.com uses HTML5. Hired the guy working on the specification.</li>
<li>Sjoerd Visscher, html5 shiv(?). IE8 does not recognize new elements like article as css styleable. But if the page calls document.createElement for the element name, it does! The return value of createElement can be thrown away.</li>
<li>IE9 looks much better. Nice stuff.</li>
</ul>
<h2>Stefan Pettersson &#8211; Developing Large-Scale JavaScript Web Sites</h2>
<p>Not so technical/concret, but still a good overview of some things to think about for web sites with heavy loads (and I would think, even good for small sites).</p>
<ul>
<li>Worked with Netlight, Aftonbladet. Did the map for http://www.hitta.se.</li>
<li>Getting questions from customers wanting to &#8220;do it like Google does&#8221;. Sites with lots of traffic, much content, much functionality, complex mess.</li>
<li>Read books by Steve Souders on JavaScript performance (by O&#8217;Reilly).</li>
<li>Establish an architecture. Best practices. A way to work.</li>
<li>New job type &#8220;front-end engineer&#8221; in the middle.</li>
<li>Load scripts on demand.</li>
<li>Load scripts after rendering.</li>
<li>Cache renewal, append build number to url.</li>
<li>XHR or JSONP? Go JSONP. For cross-site-scripting solutions.</li>
<li>Go with REST.</li>
<li>Use YUI3 for dependencies, module loading. Has sandbox.</li>
<li>Static content servers like LiteHttp. Cookieless. Cache friendly headers. Gzip.</li>
<li>Don&#8217;t put onclick in tags, attach event by jquery or other framework.</li>
<li>Progressive enhancements.</li>
<li>Pixelperfect in all browsers? Drop it! Too muc work. Sample with pin &#8220;bubble&#8221; for hitta.se.</li>
<li>Avoid putting things in global namespace.</li>
<li>Module pattern. Simulating missing private/public distinction in JavaScript.</li>
<li>Know the client. Embrace the client. Peer code reviews.</li>
<li>Build and deploy: Aggregate. Minify. (Preprocessor.) Automation.</li>
<li> Now working for Expansive Worlds.</li>
</ul>
<h2>Alexander Lang &#8211; Designing domain models with document databases</h2>
<p>Some patterns on how to design the domain model for a document database.</p>
<ul>
<li>Creator of <a href="http://github.com/langalex/couch_potato">Couch Potato</a>.</li>
<li>Don&#8217;t understands why MongoDB gets so much attension.</li>
<li>Every document has an id.</li>
<li>Get info by url. CouchDB has builtin http server.</li>
<li>Polymorphic associations.</li>
<li>Reporting.</li>
<li>Map function. function(doc) { &#8230; emit(doc.id, &lt;value&gt;); }</li>
<li>Reduce function. function(key, values) { }</li>
<li>Nested attributes (3D forms).</li>
<li>Sample with survery, questions, choices.</li>
<li>Copy versus associations.</li>
<li>Because map reduce is built in, query code can analyze.</li>
<li>Always fast through static indexes.</li>
<li>Indexes are built on reads, not writes. Writes not expensive at all.</li>
</ul>
<h2>Rik Arends &#8211; Developing applications in the cloud</h2>
<p>Showing a complete development IDE in the browser. Not available publicly just yet.</p>
<ul>
<li>From ajax.org, which are 15 people.</li>
<li>Doing JavaScript Charting and Live Markup.</li>
<li>Collaborative applications in the cloud. Refering to Google Docs.</li>
<li>Google BigTable map/reduce.</li>
<li>MongoDB soon autosharding. Redis.</li>
<li>[Nosql is actually sql without transactions, joins and predetermined schemas.]</li>
<li>Yahoo Geolocation Service.</li>
<li>Timeslider.</li>
<li>EtherPad.</li>
<li>Ajax.org Cloud IDE. Integrated with github.com. Demonstration a user interface in the browser. Looks a lot like Visual Studio with forms editor, property lists etc. Debugging.</li>
<li>Testing ui is hard. Selenium.</li>
<li>Chrome has socket open over which you can do debugging.</li>
<li>Sample with node.js and socket. Using GoogleGears like extension for now. Hoping that browsers will expose internal debug apis.</li>
<li>Next: read/write github, php, ruby, python, addon tooling, share code fragments.</li>
<li>Editor bindings.</li>
<li>@cloudeide for preview.</li>
</ul>
<h2>Daniel Glazman &#8211; Browser War 2010</h2>
<p>A frenchmen showing languages skills in Swedish too. Inside W3C working groups.</p>
<ul>
<li>Creator of Nvu. Did the styling engine of Netscape/Mozilla.</li>
<li>CEO of Disruptive Innovations.</li>
<li>W3C CSS working group co-chairman.</li>
<li>Blink and marqee tags are two proofs of the existence of the devil.</li>
<li>Handling bloody disputes from company strategists.</li>
<li>Must admin IE9 is very promising.</li>
<li>Never so many contributions to the working groups as today.</li>
<li>XHTML2 was big failure. Not backwards compatible.</li>
<li>&#8220;Reinventing HTML&#8221; by Tim Berners-Lee</li>
<li>HTML-WG =&gt; WhatWG 2006-10-27</li>
<li>HTML5 2009-10-06</li>
<li>HTML5: The most unreadable specification I have read in 20 years of standardization. Lots of internal links. The language.</li>
<li>Bluegriffin is new editor.</li>
<li>CSS2 object model was really bad with holes in the specification.</li>
<li>Canvas is going to shake the game industry too.</li>
<li>Impressed by Mozilla Bespin. Using frame buffer.</li>
<li>Properietary formats are doomed.</li>
<li>No flexible box model (yet).</li>
<li>Please stop supporting IE6.</li>
<li>JavaScript toolkits will have a hard time fighting for existence will new CSS3 support for transitions and animations.</li>
<li>Tensions in the standards communities.</li>
<li>Mozilla gets 80% of its revenue from Google. What will happen with that with regards to Chrome?</li>
<li>Infamous Apple tag &lt;meta name=&#8221;viewport&#8221; /&gt;</li>
</ul>
<h2>Dylan Schiemann &#8211; Programming to patterns</h2>
<ul>
<li>Working with SitePen, Dojo.</li>
<li>Dojo 2.0 drops support for IE6/7. 50% less code.</li>
<li>Comparisions between Dojo and MooTools.</li>
<li>Mixins with Dojo.</li>
</ul>
<h2>Sergey Ilinsky &#8211; Managing complex client-side GUI Apps the right way</h2>
<ul>
<li>Creator of Ample SDK.</li>
<li>XBL2 doesn&#8217;t look to happen.</li>
<li>Put everything in script tags.</li>
<li>Declarative UI in the xml.</li>
<li>Open Source GUI frameworks ExtJS, Dojo, Qooxdoo, jQuery UI.</li>
<li>Events for removal in the document level.</li>
<li>Ample has a DOM and the browser has its DOM.</li>
<li>Div elements are not enough for datepickers and listviews.</li>
</ul>
<h2>Mark Wubben &#8211; Building browser extensions with Chrome.</h2>
<ul>
<li>Much easier than on Firefox with XUL and XPCom.</li>
<li>chrome://extensions/</li>
<li>An extension is a folder with a manifest.json file in it. Must have a name and a version.</li>
<li>Content scripts = User scripts from Greasemonkey (Aaron Boodman).</li>
<li>Isolated worlds.</li>
<li>Content scripts run in separate contexts, but shares the DOM.</li>
<li>Use events to communicate between scripts/extensions.</li>
<li>Sample: expand bit.ly urls on Twitter.</li>
<li>Background pages.</li>
<li>[Maybe someone should make a Firefox extension to read/run Chrome extensions for compatiblity between the browsers.]</li>
<li>[Secure also means less flexible.]</li>
<li>Chrome APIs.</li>
<li>Page actions. Browser actions. Popups.</li>
<li>Browser actions can talk between extensions.</li>
<li>Desktop notifications (in WebKit).</li>
<li>Url permissions shown on install.</li>
<li>[What's "Allow incognito" in extension configuration.]</li>
<li>11born.net/swdc</li>
<li>Someone has done something for Mozilla JetPack.</li>
</ul>
<h2>Malte Ubl &#8211; Getting started with node.js</h2>
<ul>
<li>The web is changing. We&#8217;re doing i/o wrong.</li>
<li>Comet.</li>
<li>JavaScript is popping up as a library language for the first time.</li>
<li>CommonJS, goal of server side JavaScript.</li>
<li>Node.js is all about non-blocking operations on the server. [Like .NET asynchronous requests.]</li>
<li>No breaking changes anymore to Node.js.</li>
<li>nonblocking.io</li>
<li>Cloud provider for Node.js.</li>
<li>Dependent on Python.</li>
<li>No Windows binary (yet), not a true Posix platform.</li>
</ul>
<h2>Patrick Chanezon &#8211; Google for developers</h2>
<ul>
<li>AppEngine for business.</li>
<li>Infrastructure As A Service = Amazon.</li>
<li>Platform As A Servuce = Google AppEngine [Windows Azure].</li>
<li>If you have an OpenID support and a notion of separate domains, you can integrate with Google App Marketplace.</li>
<li>Prediction API.</li>
<li>The APIs for Google Storage is the same as for Amazon S3, just change the domain. Adds a few features.</li>
</ul>
<h2>Chris Heilmann &#8211; Clever re-use of web technologies</h2>
<ul>
<li>From Yahoo.</li>
<li>Nobody cares what the html looks like.</li>
<li>Problems is that we love our technologies. Truism.</li>
<li>Play to push the envelope.</li>
<li>Not everyone has JavaScript on in their browsers.</li>
<li>Drag and drop is still confusing for a lot of users.</li>
<li>iPad is not the end of web design as we know it.</li>
<li>Not everything is solved with jQuery.</li>
<li>Issues in html5+css3: security, hardware access, native rich controls, layout, internationalization, accessibility, media.</li>
<li>A lot of &#8220;using newest technology&#8221; is a lame excuse for not architecting our solutions.</li>
<li>Yeah, we annoy people with open technology [not better than to annoy with closed technologies].</li>
<li>Progressive enhancement.</li>
<li>If you put a button on a web page that doesn&#8217;t work, you&#8217;ve broken a promise to your user [cause JavaScript is off].</li>
<li>Companies happy about Facebook blocking IE6.</li>
<li>developer-evangelism.com, wait-till-i.com.</li>
<li>Doesn&#8217;t matter what Apple makes since is is a religion and not a company. I buy 2nd and 3rd generation products these days.</li>
</ul>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=303&amp;md5=12f5ad4ed6a930483b2a7dfbd2bdf893" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2010/06/02/scandinavian-web-developers-conference-day-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2010%2F06%2F02%2Fscandinavian-web-developers-conference-day-1%2F&amp;language=en_GB&amp;category=text&amp;title=Scandinavian+Web+Developers+Conference+Day+1&amp;description=This+is+a+short+summary+of+the+talks+during+the+first+day+of+Scandinavian+Web+Developers+Conference+in+Stockholm%2C+June+2+2010.+Please+note+that+it+is+a+very+short...&amp;tags=swdc%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Show a context menu for long-clicks in an Android ListView</title>
		<link>http://www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=show-a-context-menu-for-long-clicks-in-an-android-listview</link>
		<comments>http://www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 12:26:28 +0000</pubDate>
		<dc:creator>mikeplate</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[contextmenu]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[listview]]></category>
		<category><![CDATA[ListViewDemo]]></category>

		<guid isPermaLink="false">http://www.mikeplate.com/?p=246</guid>
		<description><![CDATA[Coming from a Windows and .NET background, I had some trouble understanding how to interact with the ListView control and context menu creation in Android. Context menus are supposed to be shown on your mobile device when you touch/click the screen and hold on for a longer time. So here is how to determine which [...]]]></description>
			<content:encoded><![CDATA[<p>Coming from a Windows and .NET background, I had some trouble understanding how to interact with the ListView control and context menu creation in Android. Context menus are supposed to be shown on your mobile device when you touch/click the screen and hold on for a longer time. So here is how to determine which item is long-clicked and how to show a context menu for it.<span id="more-246"></span></p>
<p>Source code for this blog post is available as a complete Eclipse project at<a title="Link to source code on Github" href="http://github.com/mikeplate/ListViewDemo"> http://github.com/mikeplate/ListViewDemo</a> (zip download link in upper right corner).</p>
<h2>An Activity with an expanding ListView and a docked TextView</h2>
<p>If you have an activity that will only contain a single ListView control, you can derive your activity from the ListActivity instead of Activity. However, I think I might like to show some extra info below my ListView so I chose to have a separate ListView object. My activity layout looks like this:</p>
<pre class="brush:xml;gutter:false">&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  &gt;
  &lt;ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    /&gt;
  &lt;TextView
    android:id="@+id/footer"
    android:layout_width="fill_parent"
    android:layout_height="60dip"
    android:text="@string/footer"
    android:padding="4dip"
    android:background="#FF666666"
    /&gt;
&lt;/LinearLayout&gt;</pre>
<p>And I need the layout for items in the ListView (listitem.xml):</p>
<pre class="brush:xml;gutter:false">&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:textSize="24dip"
  android:padding="8dip"
  /&gt;</pre>
<p>Note a nice trick that I&#8217;ve used to get the TextView to &#8220;dock&#8221; at the bottom with a definied height, and have the ListView automatically fill out the rest of the height. This kind of thinking is important since Android devices can have different resolutions. The trick is to set the layout_height to zero pixels and the layout_weight to one (default is zero). Not sure about the logic behind that, but it works!</p>
<p><a href="http://www2.mikeplate.com/wp-content/uploads/2010/01/ListViewDemo01.png"><img class="alignnone size-full wp-image-260" title="ListViewDemo01" src="http://www2.mikeplate.com/wp-content/uploads/2010/01/ListViewDemo01.png" alt="Screen capture of a selected=" height="480" /></a></p>
<p>In order to have something to put into my ListView, I created a few country names in a string array as a resource and I sort that array before adding it to the ListView with the ArrayAdapter object. (Check out source code link above for this content.)</p>
<pre class="brush:java">public class ListViewDemoActivity extends Activity {
  private String[] Countries;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Countries = getResources().getStringArray(R.array.countries);
    Arrays.sort(Countries);

    ListView list = (ListView)findViewById(R.id.list);
    ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, R.layout.listitem, Countries);
    list.setAdapter(adapter);
    registerForContextMenu(list);
  }
}</pre>
<h2>Creating a ContextMenu in Android</h2>
<p>When the user long-clicks, the event onCreateContextMenu is fired for the control that the user is clicking. For me, that is the ListView control. But since I don&#8217;t want to write a custom ListView-derived class, I want to catch that event in my activity. There does not seem to be any bubbling going on. Events fired in a child control does not bubble up to the parent if they are unhandled.</p>
<p>But obviously, the api designers have thought of this since there is a special method for this situation. Call the registerForContextMenu in your activity for this! This will actually make sure your overridden methods for both onCreateContextMenu and onContextItemSelected is called for the ListView-events as we&#8217;ll see soon.</p>
<p>Next, we&#8217;ll provide the implementation of onCreateContextMenu. Here I want to ensure that the event comes from the ListView and if so, I want to determine on which item in the ListView the user long-clicked.</p>
<pre class="brush:java">@Override
public void onCreateContextMenu(ContextMenu menu, View v,
    ContextMenuInfo menuInfo) {
  if (v.getId()==R.id.list) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
    menu.setHeaderTitle(Countries[info.position]);
    String[] menuItems = getResources().getStringArray(R.array.menu);
    for (int i = 0; i&lt;menuItems.length; i++) {
      menu.add(Menu.NONE, i, i, menuItems[i]);
    }
  }
}</pre>
<p>As you can see, the argument of type ContextMenuInfo can actually change depending on what type of control is sending the event. For ListViews, the class you need to type cast into is AdapterView.AdapterContextMenuInfo. From there I used the position, which in my case corresponds to the index into the string-array. From the array I retrieve the string for that particular item and use as title for the menu. Then you can of course add all the menu commands you like. For the demo, I defined another string array as a resource with the commands I want to add.</p>
<p>When creating the menu items with the add-call, I specify that I don&#8217;t want any grouping of the items (Menu.NONE) and that the order and id of the item is the same (i). The last argument to add is the text to display for the item.</p>
<p><a href="http://www2.mikeplate.com/wp-content/uploads/2010/01/ListViewDemo02.png"><img class="alignnone size-full wp-image-262" title="ListViewDemo02" src="http://www2.mikeplate.com/wp-content/uploads/2010/01/ListViewDemo02.png" alt="Screen capture of the context menu with commands" width="320" height="480" /></a></p>
<h2>Responding to selected MenuItem</h2>
<p>If the user dismisses the context menu (for instance, by back button) you don&#8217;t need to do anything. But for catching the actual selection of one of the items, you need to override onContextItemSelected.</p>
<pre class="brush:java">@Override
public boolean onContextItemSelected(MenuItem item) {
  AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
  int menuItemIndex = item.getItemId();
  String[] menuItems = getResources().getStringArray(R.array.menu);
  String menuItemName = menuItems[menuItemIndex];
  String listItemName = Countries[info.position];

  TextView text = (TextView)findViewById(R.id.footer);
  text.setText(String.format("Selected %s for item %s", menuItemName, listItemName));
  return true;
}</pre>
<p>The MenuItem argument holds all information that you need. The ContextMenuInfo object that got sent to onCreateContextMenu is still there and still needs type casting. Or I guess you could have saved that info in the activity between the calls, but I didn&#8217;t.</p>
<p>The id of the menu item selected is the same as the index into the string array of menu item texts for me. Instead of just outputting the menu command name and the list item text in a TextView, you would most likely have a big switch statement on menuItemIndex.</p>
<p>This was my first blog post and code demo for the Android platform. I hope it won&#8217;t be the last! The goal is to build upon this demo and/or other demos in my investigations of the Android platform. Please let me know in the comments if you have even better methods or code patterns that solves problems like this.</p>
 <p><a href="http://www.mikeplate.com/?flattrss_redirect&amp;id=246&amp;md5=c01a2d2cce5b7f2681d75e6b57fe4271" title="Flattr" target="_blank"><img src="https://www.mikeplate.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/feed/</wfw:commentRss>
		<slash:comments>39</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=mikeplate&amp;popout=1&amp;url=http%3A%2F%2Fwww.mikeplate.com%2F2010%2F01%2F21%2Fshow-a-context-menu-for-long-clicks-in-an-android-listview%2F&amp;language=en_GB&amp;category=text&amp;title=Show+a+context+menu+for+long-clicks+in+an+Android+ListView&amp;description=Coming+from+a+Windows+and+.NET+background%2C+I+had+some+trouble+understanding+how+to+interact+with+the+ListView+control+and+context+menu+creation+in+Android.+Context+menus+are+supposed+to...&amp;tags=android%2Ccontextmenu%2Cjava%2Clistview%2CListViewDemo%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: www.mikeplate.com @ 2012-05-18 08:34:00 -->
