Posted by & filed under Programming.

Let’s say you want to make a static list view, perhaps because all the items in the list fit on the screen (no matter how small). Android doesn’t offer an easy way to accomplish this with something like myListView.setEnableScrolling(false); There is a StackOverflow question that provides an easy way to do it using a custom view, as well as a few other options that work by preventing the move motion event from reaching the ListView. That works well, but if you touch one of the items in the list, and move your finger away from the view, the event bound to that item view still fires. The behavior the user usually expects is to cancel the event in this case – think of touching a button, and changing your mind mid-press. That can be done using a slightly more complex dispatchTouchEvent method. The complete class is below:

 

Now replace your ListView in the layout by the custom implementation, and you have a properly functioning static ListView.

 

Posted by & filed under Programming.

Update 10/31/2013: The WebViewChromium is now implemented in Android 4.4 KitKat!

If you’ve ever used WebView in Android, you know that its a double edged sword. On the one hand being able to bring the full power of HTML and Javascript into a section of your application frees you up from vastly more complicated native coding that you would have to do to replicate something that can easily be done with standard web technologies. On the other hand, WebView typically uses the standard browser implementation on the device you’re using, which is typically tied to manufacturer’s updates, and therefore lags modern browsers, like Chrome, by a significant margin in terms of HTML5 feature support. Debugging rendering issues in a WebView is also painful to say the least.

To combat this problem, Google has promised “Chromium” implementation for the WebView – essentially using the Chrome rendering engine to power WebView. This was first talked about at Google IO 2012. Android 4.3 shipped yesterday, and WebViewFactory shows experimental support for Chromium. However, the crucial webviewchromium.jar is still missing from the /system/framework directory to enable this support.

So for now, we’re still left where we were. One can only hope for the next release to finally have this working out of the box.

Posted by & filed under Programming.

You may have needed to see all commmunication for your app to the server. Perhaps you’ve inherited the codebase from someone else, or it relies on a library and you want to see how the server requests look. Or you’re just curious to see why your device is accessing the network, and if a questionable app is sending some private information without your consent. The easiest way I found is to use mitmproxy, a python man-in-the-middle proxy, which logs all requests. Here is a step by step guide to get it working in Android.

1. Make sure you have your Python environment setup. The exact details differ depending on what OS you’re using. Make sure to install pip as well.

2. Install mitmproxy. If you have installed pip in the previous step, this should be as simple as running:

pip install mitmproxy

3. Run mitmproxy. I had to provide a different port number than the default, because that one was used by apache I have running locally.

mitmproxy -p 9500

4. Take a note of your computer’s local IP

ifconfig

5. Make sure your Android device is connected to the same network as your computer running mitmproxy. Go into Settings -> Wi-Fi and long press the currently selected network.

6. Select “Modify Network config” in the dialog that pops up. Check “Show advanced options” checkbox.

Android Wi-Fi proxy settings

7. Under “Proxy Settings” select “Manual”. Under “Proxy host” enter your computer IP address. Under “Proxy port” enter the port that mitmproxy runs on – in my case 9500.

Android Wi-Fi proxy settings

8. You will now see requests listed in the mitmproxy window as they are generated by the device. You can navigate to the request you want to examine using arrow keys, and view more details, such as full request headers and response from the server.

mitmproxy screenshot

The mitmproxy script has a lot more options that you can explore as you need them – for example, you can inject an SSL certificate into the device to capture SSL requests. Check out the mitmproxy documentation for more information.

Posted by & filed under Programming.

The last step in signing the API request to AWS is calculating the signature using the secret key. Here is a subroutine to calculate it in a bash script. Make sure openssl is at least 1.0.0

function sign {
  kSecret=$(printf "AWS4$1" | xxd -p -c 256)
  kDate=$(printf "$2" | openssl dgst -binary -sha256 -mac HMAC -macopt hexkey:$kSecret | xxd -p -c 256)
  kRegion=$(printf "$3" | openssl dgst -binary -sha256 -mac HMAC -macopt hexkey:$kDate | xxd -p -c 256)
  kService=$(printf "$4" | openssl dgst -binary -sha256 -mac HMAC -macopt hexkey:$kRegion | xxd -p -c 256)
  kSigning=$(printf "aws4_request" | openssl dgst -binary -sha256 -mac HMAC -macopt hexkey:$kService | xxd -p -c 256)
  signedString=$(printf "$5" | openssl dgst -binary -hex -sha256 -mac HMAC -macopt hexkey:$kSigning | sed 's/^.* //')
  $(printf $signedString > $6)
}

Call it using

sign $KEY $DATE $REGION $SERVICE "$STRING" $OUTPUT