New Mobile Blog

I haven’t been blogging regularly here, and most of the older content is related to .Net, which I don’t really work with anymore. I am mostly concerned with mobile development (Android, iOS, Mobile Web, Web Services). So I am curating new content about mobility at my new mobile blog:

Mobile Notes

Thanks for reading here and please visit me at my new blog!

Posted on:
Posted in Mobile | Comments Off on New Mobile Blog

Moving Docker to Another Drive on RHEL

Docker containers can take up quite a bit of disk space, especially if used in a development environment. We create containers for Tomcat and they are upwards of 600MB. If you are developing and pushing code multiple times a day space gets chewed up fast. Our dev servers are in the cloud, and by default they don’t have tons of disk space. So using Docker with big images meant lots of regular maintenance on disk space.

I alleviated the problem by moving Docker’s directory structure to an attached drive instead of on the operating system drive. We are running Docker on Red Hat Enterprise Linux 7. In AWS I provisioned a larger drive and attached it to my RHEL instance, linking it to the /data directory. I want my Docker files in /data/docker. My first inclination was to make a symbolic link and move Docker, but I have found reports of this going awry with Docker. So reading some StackOverflow posts got me to this approach using /etc/sysconfig. Here are the steps for RHEL (it may be different for other Linux distributions):

  1. Create a file at /etc/sysconfig/docker. Add this line to the file: DOCKER_OPTS=”-g /data/docker”
  2. Edit /lib/systemd/system/docker.service
    1. add EnvironmentFile=-/etc/sysconfig/docker under [Service]
    2. Add $DOCKER_OPTS to ExecStart=/usr/bin/docker daemon $DOCKER_OPTS -H fd://
    3. Save the file
  3. Reload the daemon configuration: sudo systemctl daemon-reload
  4. Restart Docker: sudo systemctl restart docker

After you restart Docker, check the /data/docker directory, it should have lots of content now. Any images you pull and create will now be stored there.


If you installed Docker via yum, this configuration change is lost when you perform a yum update where Docker gets updated.

Posted on:
Posted in Cloud, Tools | Comments Off on Moving Docker to Another Drive on RHEL

HMAC Algorithm in Swift

In your Xcode project, make a bridging header and include this header file:

#import <CommonCrypto/CommonHMAC.h>

Then, create a Swift file. We will create the hashing function as an extension method to String.

import Foundation

extension String {

    func digestHMac256(key: String) -> String! {

        let str = self.cStringUsingEncoding(NSUTF8StringEncoding)
        let strLen = self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)

        let digestLen = Int(CC_SHA256_DIGEST_LENGTH)
        let result = UnsafeMutablePointer.alloc(digestLen)

        let keyStr = key.cStringUsingEncoding(NSUTF8StringEncoding)
        let keyLen = key.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)

        let algorithm = CCHmacAlgorithm(kCCHmacAlgSHA256)

        CCHmac(algorithm, keyStr!, keyLen, str!, strLen, result)

        let data = NSData(bytesNoCopy: result, length: digestLen)

        let hash = data.base64EncodedStringWithOptions(.allZeros)

        return hash

The function takes in the string to use as the key. In HMAC, this key is shared between the client and server.

Use the function like this, where result ends up being the Base64 encoded string:

    let data = "Message"
    let result = data.digestHMac256("secret")

GitHub Gist

Other references:

CommonHMAC in Swift

Examples of creating base64 hashes using HMAC SHA256 in different languages

Posted on:
Posted in Mobile | Comments Off on HMAC Algorithm in Swift

Michigan Google Developers DevFest 2014

I will again be speaking a the Michigan Google Developers DevFest this year. I enjoyed myself last year and made contacts that have affected my career.

This year I am talking about dependency injection in Android. Unit testing is important, and dependency injection is vital for designing testable classes. Come see me and learn about it.

Google Developers Logo

Posted on:
Posted in Mobile, Programming, Speaking | Comments Off on Michigan Google Developers DevFest 2014

Android Best Practices Book

As of this week I am a published technical author.

As a result of my speaking at Michigan Google Developers DevFest in April of 2013, I met Godfrey Nolan, who was also speaking at the event. In June he invited me to contribute to his upcoming book. The result is Android Best Practices, published by Apress. I contributed the chapter on Web Services. I am honored that Godfrey chose me, and I am pleased with the result. I got my physical copy of the book yesterday, it was published officially on the 14th.

Deploy a Windows Service on Elastic Beanstalk

I recently needed to add a Windows Service to a web app solution to handle some long-processing operations asynchronously. The web app is being deployed to Elastic Beanstalk (EB). You can log into the EC2 instance that EB creates and install the service manually, but this won’t last. EB can delete your instance at any time and create another one, and redeploy the app from it’s own git repository. So in order for the service to persist, it also has to be installed during the deployment. Fortunately for us, Amazon already supports this and there is a introductory article explaining how to do it and some sparse documentation.

Basically, you are adding a new directory called .ebextensions to your .Net project.  In that directory you place YAML files with a .config extension. The YAML files contain the commands you can execute, as described in the docs. You can have more than one file, they execute in alphabetical order. The series of commands you create allow you to install the Windows Service when deploying a web application to EB.

Unfortunately, the procedure in the article only works on the initial install. If you need to push incremental updates, the script just doesn’t work because you can’t copy files over the versions currently in use by the Windows Service. Stopping the service wasn’t working for me because the account performing the deployment didn’t seem to have enough permissions to stop the service.  But it did have enough permissions to uninstall and install the service.

The solution ended up being similar to the article, but with some variation. The correct steps for me turned out to be:

  1. Fetch the zipped files from S3, unzipping to a temp location
  2. Create the destination directory, ignoring errors (it already exists for updates)
  3. Uninstall the existing Windows Service, also ignoring errors (the service doesn’t exist on the first install)
  4. Copy the unzipped files in the temp directory to the destination directory
  5. Install the Windows Service
  6. Start the Windows Service

This translates to the following YAML file:

command: mkdir c:\\service\\serviceName\\Release
    cwd: “c:/service/temp/”
    waitAfterCompletion: 0
    ignoreErrors: true
    command: C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\installutil NameOfService.exe /u > “Remove.txt”
    cwd: “c:/service/serviceNamer/Release”
    waitAfterCompletion: 11
    ignoreErrors: true
    command: copy c:\\service\\temp\\Release c:\\service\\vserviceName\\Release
    cwd: “c:/service/temp/”
    waitAfterCompletion: 0
    command: C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\installutil NameOfService.exe > “Install.txt”
    cwd: “c:/service/serviceName/Release”
    waitAfterCompletion: 0
    enabled: true
    ensureRunning: true
    commands: d-install-service
The service uninstall/install commands are piping out their results to text files, so that if there is an error during execution I can RDP to the server and take a look at the specific error message. Also of note for troubleshooting, there is a directory created by the EB install process at c:\cfn. It contains a log directory, and in there is cfn-init.txt. If your install process fails, the errors can be found there.

Posted on:
Posted in .Net 4.5, ASP.NET, Cloud | Comments Off on Deploy a Windows Service on Elastic Beanstalk

Mobile Development Is New or Different?

The Mobile Problem

In a mobile device, we have four factors that are different from contemporary desktop computers. They are:

  • Less memory
  • Less CPU capacity
  • Constrained network bandwidth
  • Small screen size

These thing may seem difficult, but really we experienced this before in the early days of the web explosion. PC’s were much less powerful, common screen resolutions were 640 x 480, and network access was over a 14.4k modem or less. We’ve just gotten lazy in the days of blazing networks and a dozen iterations of Moore’s law, and some of us developing for mobile today never even lived through any of that.

Memory constraints certainly keep us on our toes. We can’t be sure our app will survive consistently in the phone OS, it could be terminated at any time due to memory constraints based on user activity. So we solve problems by making objects parcelable in Android, or persisting the state of any given network call in a database so we can pick up again at a later time if necessary.

These constraints are driving how we design apps and the systems that support them. REST + JSON gives us smaller network messages. We minimize CPU usage by preferring JSON over XML so there is less effort serializing, and we offload complex or data-intensive calculations to a server somewhere in the cloud.

Mobile has rightfully caused the rise of graphic design as a regular component in app development, not just something start-ups are doing. Apps are getting more user-friendly despite the small screen size, and focused in their functionality as a result of good design. Users like apps, no longer is it something they are forced to use because they have choices. Also, users finally get a vote (in $$) on whether or not an app succeeds. We are getting away from monolithic web apps. Smaller apps have fewer paths which make them more testable and have fewer bugs  as a result which is better for users.

This time around developers have the benefit of having lived through the web explosion and learned what not to do. I hope more companies build enterprise-level mobile apps keeping this in mind and don’t recreate the nightmarish internal web apps on phones.

Posted on:
Posted in Mobile | Comments Off on Mobile Development Is New or Different?

Using the Camera from Mobile Web

In the early days of mobile web, it was not possible to access the camera from a web page. While Android added the functionality a while ago in Honeycomb, it seems to have quietly slipped in to iOS in 6.0. So now, the majority of smartphone users can use the camera from a web site.

The HTML code is to access the camera is surprisingly simple:

<input type='file' accept="image/*" capture="camera" onchange="previewImage(this);" />

The accept and capture attributes do all the work. In my example I’ve also added an onchange handler to add a preview of the image to the HTML page:

<div style="height: 400px; width:200px"><img id="preview" src="" alt="your image here" /></div>
<script type="text/javascript">
     $('#preview').on('load',function() {
         var css;
         var ratio=$(this).width() / $(this).height();
         var pratio=$(this).parent().width() / $(this).parent().height();
         if (ratio<pratio) {
             css={width:'auto', height:'100%'};
         } else {
             css={width:'100%', height:'auto'};
     function previewImage(input) {
         if (input.files && input.files[0]) {
             var reader = new FileReader();
             reader.onload = function (e) {

Try it out

Interestingly, on iOS the photo stays in memory and is not persisted to the device. On Android, the photo is automatically persisted to the gallery. I suspect this is for memory management.

I have tested this code successfully on the following platforms:

  • Android 3.0+ browser
  • Chrome on Android
  • Mobile Safari on iOS6+
  • BlackBerry 10

The only large group of smartphone users left out in the cold are Android users still on Gingerbread.

I tested this code on WP8, and it didn’t work. There may be a work around, but I haven’t had a chance to work that out yet.

Posted on:
Posted in JavaScript/AJAX, Mobile | Comments Off on Using the Camera from Mobile Web

Michigan Google Developers DevFest 2013

I will be speaking at this event on Saturday, March 23rd. I will be talking about how to consume web services using Android, covering two different patterns for doing this. This is a hands-on event, so I’ll have starter code ready for participants and even web services ready for consumption. I’m looking forward to another round of public speaking. You can register here.

Posted on:
Posted in Mobile, Speaking | Comments Off on Michigan Google Developers DevFest 2013

Hospice of Michigan Mobile App

My team built an app for Hospice of Michigan to help families stay connected when they have a loved one in hospice. The app keeps them connected with the care their loved one is receiving. Read about it at CBS Detroit. You can also see a demo video on YouTube.

Posted on:
Posted in Mobile | Comments Off on Hospice of Michigan Mobile App