androidsmith

experiences & encouters from behind the android development furnace

July 15, 2011
by hermandroid
0 comments

How to copy files between a Mac and Samsung Galaxytab 10.1

So I have now almost had my Samsung Galaxy Tab 10.1 for a week now and am loving it (apart from the fact that it will probably break into a trillion tiny pieces if I would happen to drop it).  Alas it is everything I expected it to be and the potential that it holds as a development testing device really excites me.

However it really is hard to believe what conundrum is held within the prospect of simply transferring files between a Mac and this kickass device.  Google has provided a solution in the form of Android file Transfer but this has not turned out to be successful facilitating the process in my case (and I have found quite a few other such cases online).  Samsung also has a little utility application called Kies but this too ended up being a futile attempt.

This got me thinking a bit and I recollected during the days I tinkered with the jail breaking of iPhones that a little trick in getting files to the device was to either install an SSH / FTP server on it and then connect to it by means of either protocol and simply copying the files over this way.  Well unless you have a rooted Android device you dont have access to SSH so this therefore got me thinking in terms of FTP.  My first search on google for “file explorer ftp on android” turned up a really nice little file explorer in the market called File Expert and because its free it fitted right into my criteria.

After installing File Expert it is simply a matter of starting up the FTP server and connecting to it with an FTP client such as Cyberduck. BE SURE TO BE ON THE SAME LOCAL NETWORK WITH BOTH YOUR TABLET & THE MAC YOU ARE COPYING THE FILES FROM!!!

Starting up the FTP server from File Expert:

1.) From the home screen click on the “Share” icon
2.) Select “Enable FTP Sharing” from the Network Share context menu
3.) Use the Network Share FTP account details in your favourite FTP client (I like Cyberduck but FireFTP or any other FTP client will work too for that matter)
4.) BANG! Bob’s your Uncle, you can now simply FTP the files you want to your Tablet at your heart’s content.

July 13, 2011
by hermandroid
0 comments

Launching an Android application from a URL

In order for an Android application to be launch-able from a URL it is required to register an intent-filter to do so.

A good suggestion would be to stay away from defining your own URL schemes if the linking is to take place from a publicly accessible location on the Internet.   Primarily because this might lead to confusion for users accessing these web pages from outside of an Android phone with your application installed on it.  For scenarios like these it is possible to register your intent filter to not only be on the lookout for a specific scheme (in this case http or https) but to also be specific to the domain part of the link and potentially even a path prefix.

eg.

<intent-filter>
 <action android:name="android.intent.action.VIEW" />
 <category android:name="android.intent.category.DEFAULT" />
 <category android:name="android.intent.category.BROWSABLE" />
 <data android:scheme="http" android:host="api.androidsmith.com" android:pathPrefix="/details" />
</intent-filter>

However for cases where you can be sure to only be navigated to your the designated URL (for example in the case of OAuth2.0 redirection) you can define your own URL scheme and so eliminate the confirmation for application to be used step.  Unless of course somebody else already has that scheme registered on the Android device it is occurring on.

So for the case of -> oauth2client://test

<intent-filter>
 <action android:name="android.intent.action.VIEW" />
 <category android:name="android.intent.category.DEFAULT" />
 <category android:name="android.intent.category.BROWSABLE" />
 <data android:scheme="oauth2client" android:host="test" />
 </intent-filter>

An additional caveat to look out for however is the fact that as the intent-filter’s stand above your application will not be launched as expected when done so from the icon or from your development environment.  For that you should define a separate intent filter.

eg.

<intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>

 <intent-filter>
 <action android:name="android.intent.action.VIEW" />
 <category android:name="android.intent.category.DEFAULT" />
 <category android:name="android.intent.category.BROWSABLE" />
 <data android:scheme="oauth2client" android:host="test" />
 </intent-filter>

For more on Intents & Intent filters go read the android documents on the subject.

July 4, 2011
by hermandroid
0 comments

Using GSON to parse JSON on Android

Although mobile devices have on a hardware level become akin to what hardcore gaming machines were within the last decade their core strength has to be the ability to connect to resources over the Network.  I guess what the previous sentence tries to convey is the fact that although more and more processing can happen client-side without a doubt this is only a supplement to the ability to leverage “off-site” resources like web services etc.

Generally the trend for major players who offer  Application Programmer Interface’s (API) is to supply their data either in Extensible Markup Language (XML) or in Javascript Object Notation (JSON).  In general JSON wins over XML on mobile perhaps more for the reason that it is lighter (less tags and other semantic baggage) than for any other reason.  On the other hand XML does lend itself more towards context aware data in terms of the ability to deduce information from it’s eXtensibility.

Both formats can easily be processed on Android by means of opensource libraries, however because I find that in general JSON wins for the transfer format 90% of the time I figured to write a post on the ease of use in terms of using what I rate the best JSON library for Android google-GSON.

Obviously the first thing one needs to be able to retrieve some JSON from a service would be the API endpoint.  Usually these come with significant security in the form of token passing etc, however for the purpose of this example it accepts only an HTTP GET and returns a JSON representation of the Southern African countries and each of their capital cities.

You can find the call at http://api.androidsmith.com/capitals.php or if you want to do something similar the script looks like this:

<?php
 $countryCapitalsJson =
'{"data":
 [
 {"country":"Angola", "capital": "Luanda"},
 {"country":"Botswana", "capital": "Gaborone"},
 {"country":"Democratic Republic of the Congo", "capital": "Kinshasa"},
 {"country":"Lesotho", "capital": "Maseru"},
 {"country":"Madagascar", "capital": "Antananarivo"},
 {"country":"Malawi", "capital": "Lilongwe"},
 {"country":"Mauritius", "capital": "Port Louis"},
 {"country":"Mozambique", "capital": "Maputo"},
 {"country":"Namibia", "capital": "Windhoek"},
 {"country":"South Africa", "capital": "Pretoria"},
 {"country":"Swaziland", "capital": "Lobamba"},
 {"country":"Tanzania", "capital": "Dodoma"},
 {"country":"Zambia", "capital": "Lusaka"},
 {"country":"Zimbabwe", "capital": "Harare"}
 ]
 }';

 header('Content-type: application/json');
 echo $countryCapitalsJson;
?>

As you can see from the JSON that is returned it is essentially an array of Country name & Capital City pairs. In order for the JSON library to magically transform this JSON represented dataset into POJO (Plain Old Java Object) models you need to create the classes which are to be used to represent these domain objects.

In the case of the example I created a Response object which needs to contain a member variable for the data field as it is the next element in the JSON document’s data structure.  Note that the data member variable in our Response class is an arraylist of type Country which facilitates simple parcelability but more about that in a later post.

package com.androidsmith.sacc;

import java.util.ArrayList;

import com.androidsmith.sacc.model.Country;

public class Response {

 ArrayList<Country> data;

 public Response() {
 data = new ArrayList<Country>();
 }

}

Next of course the Country model object:

package com.androidsmith.sacc.model;

public class Country {

 String country;
 String capital;

 public Country() {

 this.country = "";
 this.capital = "";
 }

 public String getCountry() {
 return this.country;
 }

 public String getCapital() {
 return this.capital;
 }
}

After you have your model layer setup to represent the nested domain objects represented in JSON it is merely a matter of utilizing the magic of the GSON library.  In essence all you need to do is call the relevant method on an instance of the Gson class and pass it a reference to the reader containing your JSON response’s InputStream as well as the Class of your root level entity (in our case Response.class):


Gson gson = new Gson();
 this.response = gson.fromJson(inputStreamReader, Response.class);

In the case of an exception not having been raised this.response should now contain your model hierarchy as represented in the JSON retrieved ready for you to do whatever you want to with.

Please feel free to download the sample project for the complete eclipse project.

Libraries do bring magic to your sourcecode and while it works it is great, however when a library starts behaving strangely it can become VERY frustrating.  However in our lives we do not always have time to or want to re-invent the wheel.  Luckily this magical library is opensource and therefore offers a very helpful and debuggable option as a JSON parsing library.

“Any sufficiently advanced technology is indistinguishable from magic”
Arthur C. Clarke, “Profiles of The Future”, 1961 (Clarke’s third law)
English physicist & science fiction author (1917 – )

May 14, 2011
by hermandroid
0 comments

Google Technology User Group event of 04 May 2011

A few weeks ago the members of MIH SWAT‘s mobile development team received invites to attend the upcoming Google Technology User Group (GTUG) event in Johannesburg on 04 May 2011. Upon accepting the invitation it became apparent that there was a similar event scheduled in Cape Town (where the South-African office of MIH SWAT is located) but that registration had already closed.

Being firm believers in the Android platform and in what possibilities it holds we registered and excitedly awaited for the time to attend.

At the venue a large mob of very excited Android enthusiasts were brimming with anticipation and I was enveloped by the camaraderie and energy of the community.

After a brief welcome and a few spot prizes by Brett St Clair (Google Head of Mobile SA) the evening kicked off with a keynote from Hans V Stockhausen the founder of the Johannesburg chapter of the GTUG.  To summarize what a GTUG entails in my own words would encompass that it is “an alliance of like-minded passionate software developers who want to be involved in changing the world for the better by combining their own skills & ideas with infrastructure & technology promoted and or provided by Google”.

The technical talk of the evening was by Toby Kurien.  Toby is making a name for himself in the Android community with 3 of his own applications in the Android market including batteryfu which has been tremendously successful.  It was very insightful to hear what gotchas you can expect to encounter upon entering a diverse international market such as exposed by the Android operating system.  These gotchas range from the expected technical aspects such as different ports being used on mobile network variations to the fact that client’s priorities are incredibly diverse.

The fact remains that Android is still relatively unknown in the South African market and through “evangelism” awareness needs to be cultivated.  Especially within the developer community in terms of what potential opportunities exist but also within the consumer market who still cling to their Blackberry’s.

In the East and in Africa the rise of the “Android Army” is becoming increasingly obvious with cheap Android devices flooding the market.  For example in Kenya the Huawei Ideos has made its debut and from what I am told retailers simply cannot stay ahead of demand.  Essentially it is a R700 smartphone running Android 2.2 with all the bells & whistles including a capacitive touch screen (see the full specifications here).  This impressive little device has for example revolutionized the way in which rural farmers in Kenya operate in that where in the past an Agricultural Official had to drive out to these farms capture a myriad of data on paper and then later remember to upload it.  It is now possible for them to simply capture the data into the device offline and as soon as a reliable data connection becomes available it automatically uploads it to a central server utilizing the likes of Open Data Kit (ODK).

But the Android revolution is not just limited to cheap phones as more and more tablet devices are appearing, some for as cheap as US$100.  Although the quality of these devices are said to vary significantly they do offer great opportunities for course material to be distributed electronically for example by the Khan Academy etc.

After a short interlude with free drinks and snacks Brett St Clair took the floor elaborating on what Google envisages for Africa.

With in excess of 1 billion people residing in Africa it does pose a significant market if it can be accessed.  As we are busy seeing the available international bandwidth in Africa blossoming from a meager 120Gbit/s to 15000Gbit/s therefore it can easily be extrapolated that the price of bandwidth will continue dropping significantly.  Needless to say this will bring with it lots of international opportunities for African businesses.   The anecdote was for example brought up relating to the advent of a global presence for African business which could mean that as small as a 1% increase in Internet users could result in as much as a 4.3% increase in exports.

Google is already involved at tertiary level throughout Africa and intends to play a further empowering role from the ground up.  They intend to do this by means of educating developers about efficient implementation of technologies as well as reducing Internet access barriers to all potential users.  By strategic placement of Google caches the severe dependency on International bandwidth can be subdued in addition to supporting the expansion of Internet Exchange Points (IXP’s) around Africa.

Further it is also prevalent that a large set of content is not currently relevant to all users because of language and other such barriers.  Search is for example being further localized and the topic of products specifically geared for the African landscape is seriously being promoted.   Furthering the opportunity of using technology to solve the problems in Africa mention was also made to Google’s incubator unit Umbono based in Cape Town where mentorship, office space as well as bandwidth for local budding entrepreneurs is sure to attract prospective talent.

It is quite obvious that the potential for development utilizing Google technologies including the Android platform is virtually limitless and that all industries have areas that are waiting to benefit from this open benevolent technology.

Recently at Google IO Google announced that Android now also supports the Open Accessory Development Kit that will allow not just software development to take place but opening up the possibilities of hardware development as well.

My first GTUG experience was definitely a positive one topped off with a good stack of Google schwag that got distributed amongst my SWAT cohorts.  I greatly look forward to the next GTUG event and really hope it measures up.