Friday, January 27, 2012

Samsung Galaxy Tab 10.1 - Stuck on Boot (link to article)

My Samsung Tablet "has died" today. It got stuck on boot for some reason.
I hooked it up to the computer and checked the logcat (adb logcat). 
It was trying to open the account database on the device, that for some reason got corrupted and after the failing to do so (unable to open database file, db=/data/system/accounts.db) it rebooted over and over again. The database can be easily fixed on the rooted device, but mine was not rooted yet.

After a bit of googling I found this useful article that describes exactly the same problem on exactly the same device and provides the solution:

Essentially, the solution comes down to deleting all the user data (aka factory reset), here are the steps from the article:

"In case you are wondering how to hard-reset your tab, you can follow these steps:
1) Power off the device by holding the power key for about 8 seconds
2) Press the volume down key (the one to the left, as I always confuse them) and then the power key
3) Within a couple of seconds, you'll see two logos on the device, one for USB and another, that looks like a Android dude shoveling earth
4) Press the the volume down button to select the the USB icon, and then press the volume up button to make the actual selection
5) Your device is now in the "fastboot" mode
6) Finally, open up your terminal/shell and type fastboot -w, which will completely erase your userdata and cache partitions (took about 12.5 seconds for me). I got the fastboot by building it from source, but you can download pre-built binaries from various sources on the web
7) Restart your device
8) You should now be prompted to set it up... all over again"

Many thanks to the author of that article Aleksandar Gargenta. 

Javascript from command-line on Mac OS X

Here is a "trick" to get the javascript command-line tool "jsc" available on Mac OS X,  just add a link, for example like this:
sudo ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc ~/bin/jsc
then just run "jsc".

Friday, January 13, 2012

YAML parser for Android

I needed YAML parser for Android this week and after a bit of research I found that SnakeYAML might work: http://code.google.com/p/snakeyaml
The only problem was if you try to use the latest code the application would fail with exception that looks something like this in the logcat:

VFY: unable to resolve exception class 21 (Ljava/beans/IntrospectionException;)


This happens due to the dependency on java.bean package APIs that is used to parse getters/setters types of properties (methods). 
There is a prebuilt library already available in the download section for version 1.8 adjusted for Android: http://snakeyaml.googlecode.com/files/snakeyaml-android-1.8-SNAPSHOT.jar (thanks to Alexander Maslov), but I wanted to use the latest code. 
The adjustment for Android is quite straight forward:
* Remove all the imports of java.bean package components
* Remove the throws IntrospectionException from methods declarations in few files (grep, there are only 4 .java files to change 
* In the introspector/PropertyUtils.java adjust the getPropertiesMap method implementation, throw away the use of Introspector. Yep, the library will not support java getters/setters methods serialization after that, only fields, but that worked fine for me.
* Remove introspector/MethodProperty.java file
* If you want to save some time and don't care building and running tests (I know it's wrong, but still), then you can build the .jar library with mvn package -DskipTests=true command.
* Add the final .jar file into your Android project and enjoy YAML on Android

Update (02-22-2012):
Alexander just landed the patches for Android (http://code.google.com/p/snakeyaml/source/list).
Now the latest source code can be easily built with one command: mvn package -Pandroid or to skip the tests mvn package -Pandroid -DskipTests=true. After the build,  just use snakeyaml-1.11-SNAPSHOT-android.jar.

PipedInputStream default size on Android

The PipedInputStream on Android is 1024 bytes by default.

protected static final int PIPE_SIZE = 1024;

In many cases this value is too small, so the constructor with the size parameter should be used instead.

Thursday, January 12, 2012

Android application signing with PKCS12 (.p12) certificate

Here are few useful commands to sign the Android application with PKCS12 certificate.

* Import PKCS12 cert into keystore file:
keytool -importkeystore -srckeystore mycert.p12 -destkeystore mycert.keystore -srcstoretype pkcs12

* Change the certificate alias in the keystore if you like:
keytool -changealias -keystore mycert.keystore -destalias new_cert_alias -alias original_cert_alias

* List the keystore certificates (-v for verbose)
keytool -list -v -keystore mycert.keystore

* Sign the Android APK
jarsigner -verbose -keystore mycert.keystore myapp.apk cert_alias

* Verify that the APK is signed
jarsigner -verify -verbose -certs myapp.apk



OAuth2 with Google C2DM (push)

I had to implement the server prototype for Google C2DM (push) the other day.
The easiest option to get the auth token is to use ClientLogin API: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html  that returns the auth token in one call. One of the problems with this approach was that I either had to store the credentials on the server in order to refresh the auth token if it expires or notify the system administrator to go ahead and re-authenticate through the dedicated web page on the server. 

The better approach is to use OAuth2 API instead: http://code.google.com/apis/accounts/docs/OAuth2WebServer.html
There are couple of things are not well documented:
* When you register the app on the https://code.google.com/apis/console/ there is no C2DM service API to choose. That's fine for now, though it might change later probably.
* One of the parameters "scope" for the https://accounts.google.com/o/oauth2/auth call needs to be set to 
scope=https%3A%2F%2Fandroid.apis.google.com%2Fc2dm (https://android.apis.google.com/c2dm url-encoded)
* Set parameter access_type=offline.This will provide you with the refresh_token in the authentication response, that you can use to renew the auth token when it expires. 

Hope this will save somebody few minutes. 

UPDATE (January 19, 2012):
If you stumble upon the MismatchSenderID error, make sure that when you start authentication (https://accounts.google.com/o/oauth2/auth) that you are not already signed in under your personal google account (hint: the browser cookies) . You should be signed under the google account that you created and that got approved for C2DM.



UPDATE (July 2, 2012):
As of the Google IO 2012 the C2DM API is officially deprecated and this article is not really that relevant anymore.
The new sign ups and the quota increase requests are not going to be processed anymore and Google highly recommends to migrate over to the Google Cloud Messaging (GCM), which is still free and has lots of improvements and additional features. I personally think GCM is awesome. 
Please refer to the official GCM page for more details:
http://developer.android.com/guide/google/gcm/index.html
I'm looking forward to change my code to work with GCM pretty soon and will try to write another post on the transition process. Enjoy!

Wednesday, January 04, 2012