Thursday, April 25, 2013

Setting Realistic Expectations For Mobile App Development

This article was originally seen in the May issue of SmartCEO. It was written by Mike Rappaport, CEO of Chariot Solutions.

One common misconception about mobile application development is that it's much easier and less expensive than traditional software creation.  We've all heard stories about an app that was developed overnight, released in the app store, with the app quickly becoming a big success. This is usually not the case.  In reality, developing a mobile solution is sometimes more costly and difficult to implement for most businesses.

Since mobile applications are delivered on smaller screens and tend to have a more focused purpose, the thought is that they should be less expensive to develop than traditional applications. So why isn't that the case?

The main reason is that mobile application development still needs to adhere to the same processes as traditional software development. All software development should include planning, requirements definition, design, development, testing, delivery and support.  Along with an effective development process, delivering a quality solution requires having the right people in the right roles.

Since mobile applications have a more focused purpose than traditional applications, it is even more important that business, creative and development resources work together to clearly define and deliver a successful application.

Moreover, mobile development faces additional challenges that most traditional applications may not experience.  The most significant challenges include: variety of devices, user interface paradigm shifts, network connectivity, testing and distribution.

Mobile devices come in a variety of sizes, shapes, and platforms.  The type of devices to be supported will have a direct impact on the cost of development.  The major platforms are iOS (iPhone and iPad) and Android. But what if the user base includes Windows Phone or BlackBerry? What about tablets versus phones? Even within tablets or phones, device sizes can vary from a design, development and testing perspective.

User interfaces on mobile devices rely on the user touching a small screen with their finger or thumb.  This is a very different experience from traditional applications.  This not only forces designers to rethink how users interact with applications, but it requires a design that is intuitive.  These designs also need to adapt to varying screen sizes and take into account the size of a finger as opposed to the point of a cursor.  A poorly designed (yet typical) web form in a traditional application might include 15 or more fields with descriptions on what acceptable values might be.

How does that fit on a 3.5 inch screen?  Challenges like this require more creative solutions, which subsequently have a direct impact on cost.

Imagine entering 15 fiels of data using thumbs, only to find that the network suddenly became unavailable during the submission.  How does the application handle that? As with traditional applications, data integrity and security are a major concern on mobile devices. However, network availability is much more variable on mobile devices.  Also, data can be more vulnerable on mobile devices, simply due to the fact that devices are mobile. Accessing and handling mobile data introduces additional complexity into the design and implementation of mobile applications.

As features are developed, testing of mobile applications becomes even more important.  How a touch-based application looks, feels and reacts can vary significantly from device to device.  It is critical to get the applications deployed onto devices and in the hands of users early and often.  Depending on the size of the application, this can lead to longer testing cycles.

Finally, once development is complete, it must be distributed.  This process varies from platform to platform.  There may be additional effort required to prepare the application for submission to an App Store. The time for submission and potential rejection need to be accounted for when planning the project.

The bottom line is that developing mobile applications is much like developing software for other platforms, with some additional complexities.  But the recipe for success is the same: It takes the right people and an effective process to be successful.  When all of this is done properly, it has been proven that mobile applications have the ability to dramatically improve, and in some cases transform businesses.  Business leaders need to understand that though the cost of mobile application development may be high, the ROI can be much higher.

Tuesday, April 9, 2013

The Pivotal Initiative - the future of Spring and open source at VMware/EMC



EMC and VMware have poured their open source software development workforce into a new wholly-owned subsidiary, dubbed "The Pivotal Initiative". This organization will focus on data science consulting and tooling from GreenPlum, including SpringSource/vFabric development teams.

It is being girded by a team of forward-looking developers at Pivotal, a recent EMC purchase and well known for their work with Ruby tools and testing software.

Until now, Pivotal has not yet discussed their strategy for the SpringSource teams. Adrian Colyer, one of SpringSource's spiritual leaders, here outlays what the future will hold, including support for Java 8 Lambdas (closures) in Spring 4, Groovy, Grails, and Cloud Foundry. 

Monday, March 18, 2013

Training Courses on Tap for Spring 2013

It's that time again. Flowers are sprouting, pollen is flowing, and, lo and behold, Chariot is running more training...

We have some exciting additions to our courses this quarter, including a guest course by Neosoft, our usual Spring training courses, a Groovy and Grails offering, and more. Here's our lineup:

  • March 26-28 - Comprehensive Maven 3 and Nexus training - We are running a 3 day maven intro, intermediate and advanced course starting on March 26. You'll learn how to build Java application with Maven, including JARs, WARs, and multi-module projects.
  • April 9-12 - Core Spring Developer Training/Certification - Recently updated, this course covers Spring 3.1 and above, all three styles of dependency injection (XML, annotations, JavaConfig), JDBC, Hibernate/JPA, an introduction to Spring Web MVC, Security, JMX and more. A deep discussion of the Spring lifecycle and features such as proxying, interface-driven development, JUnit-based testing and Aspect Oriented Programming provides a strong foundation for new Spring developers.
  • April 17-19 - Neo4j Tutorial by Neo Technology - We are featuring a guest training course by NeoSoft, discussing their NoSQL Graph database, Neo4J. This is a two-day course to get developers up to speed in using Neo4J.
  • April 30-May 3 - Groovy and Grails v2.0 - new for Chariot training, we're featuring a four-day Groovy and Grails training course, written by the developers of Grails at VMware. This course begins with a heavy introduction to the Groovy programming language, and then dives into Grails, the convention-driven Spring-and-Hibernate application framework. Configuration, controllers, services, scaffolding, testing, writing plugins, and GSP tags are covered.
  • May 9-10 - Advanced Scala - This Typesafe course, taught by Michael Pigg, takes our Scala training even further than Fast Track to Scala, and includes a deep dive into functional programming concepts, internal DSLs, custom Scala collections, and details on the Scala type system.
As usual, we offer discounts for Chariot training alumni. Contact us for information on obtaining a discount code - please provide your name, course, and estimated training date. We also provide private training and can offer courses in Hadoop, Java, Tomcat, and other technologies. Check our course catalog for more information.

Friday, March 15, 2013

Recap - Chariot Day 2013 - Our internal conference...

One thing Chariot people love to do is learn new technologies and techniques. Being consultants constantly challenged in the field, keeping our blades sharp is key to doing battle. Every year or so we get together and run our own internal conference to see what each of our current passions are. We call it Chariot Day.

Earlier this month, on a Saturday, we had two rooms going from 9-5PM, each with 45-minute sessions on subjects such as:
  • Becoming a better programmer, Mythbusters Style
  • Async networking for Android w/Robospice
  • A great session on zsh
  • Data Mining - it's not just for kids
  • Universal EventBus with Vert.x
  • Functional programming with Scala
  • Single-page Javascript with AngularJS
  • Billions of Things (you can imagine scalability and performance on a large scale)
  • Having fun with the Raspberry Pi
  • HTML5 Canvas
  • Understanding the Persistent Data Structures of Clojure
  • Get low w/HBase
  • Getting started with Phone Gap
  • 20 tools that turbo charge a 1.51 person development team
We will be reviewing the content from these sessions to see if we can share them with you. 

Wednesday, March 13, 2013

Take (and Manipulate!) a Photo with a Web Page

So not that long ago, if you wanted an app to take a photo, it had to be a native app -- such as a Windows/Mac app or a native mobile application.  But HTML5 has brought a number of new APIs that allow not only taking photos, but analyzing and manipulating them all within a browser.

Sadly, there is still inconsistent support for these features across browsers (both desktop and mobile).  So in practice, the best approach may be to target the APIs supported by iOS 6, which is more or less the minimal feature set across browsers that support any of this at all.

The three pieces we'll need are:
  • The Canvas
  • File I/O
  • Changes to the File Upload input and Media Capture
    • Note that Media Capture has been superceded by getUserMedia (more info) but we don't use that here because iOS doesn't support getUserMedia yet

The Canvas is a space on the page to draw into, using either 2-D or 3-D APIs.  Conveniently, it also lets you draw images into it, inspect and alter the pixel data, and export its content as an image. Canvas is supported by IE 9 and modern versions of Firefox, WebKit/Safari/iOS 6, Opera, and Chrome.  However, not all browsers support 3-D (via WebGL), and browsers don't generally support ALL of the Canvas spec (which has continued to evolve even after initial browser support).  Still, the parts we need are supported across browsers.

The additional "accept" and "capture" attributes on the file upload input element let us specify that the user should be able to select an image file, getting it from an attached camera if possible.  Crucially, on iOS you are given the option to select a photo from the photo library or take a photo on the spot (though some desktop browsers only let you browse for an image file, even if a Webcam is attached). Then the "files" property on the input element lets us access the selected files (unfortunately, not supported in IE < 10).

So the formula for taking and manipulating a photo is:
  1. Configure a file input using the new attributes
  2. When an image file is selected (or photo taken), access the image file
  3. Check the image EXIF data for rotation using File I/O
  4. Decide what size to scale the image to (if needed)
  5. Configure a Canvas for the selected image dimensions
  6. Draw the image to the Canvas, rotating and scaling as appropriate
  7. Access and manipulate the pixel data for the Canvas
  8. Write the pixel data back to the Canvas
  9. Export the image on the Canvas as an image file

Taking these one step at a time (and note that I use jQuery for convenience, but it is not needed for this to work):

1. Configure a file input using the new attributes

There are two ways to configure the input (and it makes no difference to desktop browsers). An input configured like this will prompt the user to take a photo or select a saved impage on either iOS or Android:
<input type="file" id="PhotoPicker"
       accept="image/*" />
With the additional capture="camera" attribute, Android goes directly to the camera without giving the option to use a saved photo (though the iOS behavior is the same):
<input type="file" id="PhotoPicker"
       accept="image/*" capture="camera" />
Note that if you don't like the default appearance, you can conceal the input widget and connect a button or link (styled any way you like) to activate it.  The input doesn't work if set to display: 'none', but we can put it inside a div of size 0 to effectively hide it:
<div style="width: 0; height: 0; overflow: hidden;">
    <input type="file" id="PhotoPicker" 
           accept="image/*" capture="camera" />
</div>
<button class="lovely" id="PhotoButton">1. Select Photo</button>
$('#PhotoButton').click(function() {
    $('#PhotoPicker').trigger('click');
    return false;
});

2. When an image file is selected (or photo taken), access the image file

We just use the new files property on the input:
$('#PhotoPicker').on('change', function(e) {
    e.preventDefault();
    if(this.files.length === 0) return;
    var imageFile = this.files[0];
    ...
});

3. Check the image EXIF data for rotation using File I/O:

All images may have a rotation recorded by the camera, such that no matter how the camera was oriented when the photo was taken, the image can be displayed appropriately on screen.  In particular, an iPad considers the landscape orientation to be normal (though it can still be upside-down), and the portrait orientation takes photos with a 90 degree rotation.  The EXIF header in an image records this rotation, so we can read it in order to display the image appropriately.

This part uses two open source (MPL) EXIF-reading scripts:
<script src="http://www.nihilogic.dk/labs/exif/exif.js"
       type="text/javascript"></script>
<script src="http://www.nihilogic.dk/labs/binaryajax/binaryajax.js"
       type="text/javascript"></script>
Then we use the new File API to read the image data (and there's a nice image here describing the EXIF Orientation codes):
var width;
var height;
var binaryReader = new FileReader();
binaryReader.onloadend=function(d) {
    var exif, transform = "none";
    exif=EXIF.readFromBinaryFile(createBinaryFile(d.target.result));

    if(exif.Orientation === 8) {
        width = img.height;
        height = img.width;
        transform = "left";
    } else if(exif.Orientation === 6) {
        width = img.height;
        height = img.width;
        transform = "right";
    }
    ...
};

binaryReader.readAsArrayBuffer(imageFile);

4. Decide what size to scale the image to (if needed)

If you want to limit the size of the image, you can scale the height and width accordingly. With mobile devices with high-resolution cameras, this may be necessary to limit memory usage:
var MAX_WIDTH = 1024;
var MAX_HEIGHT = 768;
if (width/MAX_WIDTH > height/MAX_HEIGHT) {
    if (width > MAX_WIDTH) {
        height *= MAX_WIDTH / width;
        width = MAX_WIDTH;
    }
} else {
    if (height > MAX_HEIGHT) {
        width *= MAX_HEIGHT / height;
        height = MAX_HEIGHT;
    }
}

5. Configure a Canvas for the selected image dimensions

If the canvas is on the page, you can just grab it:
var canvas = $('#PhotoEdit')[0];
Otherwise, you can use an offscreen canvas:
var canvas = document.createElement('canvas');
And then size it accordingly:
canvas.width = width;
canvas.height = height;

6. Draw the image to the Canvas, rotating and scaling as appropriate

In order to draw the image to a Canvas, first we have to load it in an <img />, and a convenient way is to create a URL to assign as the img.src from the selected file (using part of the File API):
var img = new Image();
var url = window.URL ? window.URL : window.webkitURL;
img.src = url.createObjectURL(imageFile);
img.onload = function(e) {
    url.revokeObjectURL(this.src);
    ...
};
Then inside the onload handler, we can draw the image to the canvas. (In practice, I put most of the logic we're discussing inside the onload handler.) A transformation matrix on the context handles rotating the image (if needed) and shifting it back into the viewable area.
var ctx = canvas.getContext("2d");
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, 700, 600);
if(transform === 'left') {
    ctx.setTransform(0, -1, 1, 0, 0, height);
    ctx.drawImage(img, 0, 0, height, width);
} else if(transform === 'right') {
    ctx.setTransform(0, 1, -1, 0, width, 0);
    ctx.drawImage(img, 0, 0, height, width);
} else if(transform === 'flip') {
    ctx.setTransform(1, 0, 0, -1, 0, height);
    ctx.drawImage(img, 0, 0, width, height);
} else {
    ctx.setTransform(1, 0, 0, 1, 0, 0);
    ctx.drawImage(img, 0, 0, width, height);
}
ctx.setTransform(1, 0, 0, 1, 0, 0);

7. Access and manipulate the pixel data for the Canvas

The getImageData method returns an array of pixel data, with one byte each for red, green, blue, and alpha. This example applies a "green screen" effect, setting pixels to transparent (alpha = 0) if they are mainly green.
var pixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
var r, g, b, i;
for (var py = 0; py < pixels.height; py += 1) {
    for (var px = 0; px < pixels.width; px += 1) {
        i = (py*pixels.width + px)*4;
        r = pixels.data[i];
        g = pixels.data[i+1];
        b = pixels.data[i+2];
        if(g > 100 && g > r*1.35 && g > b*1.6) pixels.data[i+3] = 0;
    }
}
There are of course other possibilities for filtering the image.

8. Write the pixel data back to the Canvas

Then we use putImageData to write the array of pixel data back to the canvas.
ctx.putImageData(pixels, 0, 0);

9. Export the image on the Canvas as an image file

Finally, the whole content of the canvas can be exported as an image. There may be a way to do this through the browser (in Firefox, for instance, right-clicking on any Canvas lets you view it as an image), but there is a canvas API call as well. Every browser supports saving the image as a PNG, and some may support other formats:
var data = canvas.toDataURL('image/png');
The resulting data URL looks like this:
data:image/png;base64,...
Where the "..." is the base 64 encoded version of the binary image file. So you can, for instance, upload the data URL to a server as a form field, and the server can strip off the data:image/png;base64, prefix and base 64 decode the rest and save it to a file like mycanvas.png.

Summary

So there you have it -- a Web page that takes a photograph, scales and orients it as appropriate, processes the image as desired, and then displays it to the user and/or uploads it to a server.
Unfortunately, there is not yet a way to present a "Save As" dialog to the user to save the file directly to their local disk -- the FileSystem and FileWriter APIs are not widely supported across browsers. But you can of course upload the image to the server and then redirect the user to a server URL to save it...

Consolidated Sample Code

Here's a single page with a working example using the code we've gone through here.

You can also use this sample image to see the green screen replacement effect.

Platform Support

Bold entries work fully:

Desktop Browsers:
  • Firefox 18
  • Safari 6
  • Chrome 25
  • IE 10
  • IE 9 (does not support getting selected files from file input)

Mobile Browsers:
  • iOS 6
  • iOS 5 (file input doesn't work)
  • BlackBerry 10
  • Android 4.1 (built-in browser)
  • Android 4.1 with Chrome from Play store
  • Android 2 (native browser; does not take photo)
  • Windows Phone 8 (file input doesn't work)
  • Surface RT (doesn't work)

Saturday, March 9, 2013

Configuring the Network used by Mac OS X Internet Connection Sharing

Changing the Subnet

On Mac OS X, Internet Connection Sharing by default hands out IPs on the 192.168.2.x subnet.  Normally this is fine, but sometimes you might want to change it (because you're at a hotel that uses 192.168.2.x or whatever).

The network to use is configured in /etc/bootpd.plist, but that file is overwritten every time you start Internet Connection Sharing (and deleted when you stop it).

The easiest way to change the network is to shut down Internet Connection Sharing, close System Preferences entirely (since it keeps settings in memory and overwrites configuration files), and run the following Terminal command (replacing the last parameter with the network you want it to use):

sudo defaults write \
   /Library/Preferences/SystemConfiguration/com.apple.nat \
   NAT -dict-add SharingNetworkNumberStart 192.168.3.0

Then you can go back into System Preferences and start Internet Connection Sharing again.  If you look at /etc/bootpd.plist you should see the new network listed.


Static IPs for Connected Devices

If you want to assign static IPs to any devices, you can create the file /etc/bootptab like this:

# Bootptab file
# Section 1 -- ignored
%%
# Section 2 -- used
# Hardware types: 1=Ethernet, 6=Wireless
#                 See http://www.ietf.org/rfc/rfc1700.txt
#
# machine entries have the following format:
#
# hostname         hwtype     hwaddr         ipaddr   bootfile
some-ethernet-machine 1 c4:2c:03:3a:7f:65 192.168.3.200 boot
a-wireless-machine    6 60:33:4b:27:6c:62 192.168.3.201 boot
another-ethernet      1 00:0d:93:72:e7:96 192.168.3.202 boot

You can man bootptab, though it isn't very helpful, and man bootpd has only a little more.  Bottom line, there must be a line with %% and the static IP definitions come after that.  The first parameter is the machine name, the second is the hardware type, the third is the Ethernet address of the relevant network interface of the machine, the fourth is the IP to assign, and the last is a boot file (which we aren't using, but it might be necessary to have something there).

Make sure to restart Internet Connection Sharing after editing this file.

Tuesday, February 26, 2013

Importing Data via Custom File Types in iOS

Introduction

I recently came across a situation where I needed the ability to share data between users of the same iOS application.  Unfortunately, these users can not share an iCloud account and there is no server side components to this application.  It is a standalone app.  Services like DropBox were not really an option either, as the devices are owned by an enterprise and will not have any service like that deployed onto them.

One solution is importing data via custom file types. Files can be sent to a device numerous ways (e.g. email, drop box, etc.), but for this post we will assume email. iOS makes it very simple to define a custom file type that your app can handle.  This way, when the device receives an email with that type as an attachment, the user can select our app, giving it access to the underlying file. This post will walk through creating a simple app to access the contents of a text file received via email.

Create the Project

Lets start by creating a new project in XCode.  I created a single view iPad application and called it "CustomUTIHandler".  I always use ARC.  You can choose to use storyboards or not, as the UI in this app will do nothing more than display the contents of our file.  The resulting project for this post can be found at https://github.com/stevenpsmith/CustomFileHandler.

Define the File Type

The next thing is to define a file type extension that our application will support.  This is also known as a Uniform Type Identifier (UTI).  For our purpose, we will use a filetype of 'csm' (for Chariot Solutions Mobile).  The contents of this file will be some string.  It could really be almost anything, but for simplicity we will use a string.  For a list of Apple defined UTIs, look here: http://developer.apple.com/library/ios/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1.

Once we know what our custom file type extension is, we can add support for it to the project by modifying the Info.plist file for the project.  We are going to be the owner/exporter of the new file type, so we add the "Exported Type UTIs" key to the Info.plist file.  The default definition that gets created looks like this:


Based on the bundle key definitions found here: https://developer.apple.com/library/mac/#documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html, we can fill in the required values.  Since our example only has one file type that it is defining and the underlying data is text, we can add the system defined identifier for text to the "Conforms to UITs" item:

public.text

Next we need to add the identifier for our custom UTI.  Apple recommends using the reverse DNS format in order to ensure our new UTI is unique.  I will be using the following:

com.chariotsolutions.customUTIHandler.csm

Finally, we want to identify the file extension that our app will be handling.  This is defined under the
"Equivalent Types" node.  Add a node under the equivalent types with a name of "public.filename-extension" and a value of "csm".  You can optionally add a "Description" element under the Item for the exported type.  My Info.plist now looks like this:



Support our new File Type

In order for our app to be recognized by the OS as supporting the newly defined file type, we need to add a "Document types" declaration to our Info.plist file.  Doing so gives us the following in our Info.plist file:



The possible keys for the CFBundleDocumentTypes property can be found here: http://developer.apple.com/library/ios/#documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/TP40009249-101685-TPXREF107.  We can add anything for the "Document Type Name" value.  The handler rank will affect the order that Launch Services uses to present apps that handle this file type.  Since our app is really the owner of this file type, we will use that as the handler rank.  Now we need to add the "Document Content Type UITs" that our app will handle.  In this case, we are only handling one type (as defined in the export key above): 

com.chariotsolutions.customUTIHandler.csm

Just for fun, we will also give our file type an image icon (included in the project).  The specs for the image are defined in the keys document referenced above (64x64 in this case).  So our final Info.plist looks like the following:



Now all we have to do is create a text file with a ".csm" extension and email it to our device.  Once there, long press the resulting icon and we should see our app listed as one of the "Open in" apps.



Once we select our app, it will launch and pass the file URL to this app delegate method:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

So, adding a simple NSLog statement to the method and we can display the contents of our text file:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if (url){
        NSString *str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"The file contained: %@",str);
    }
    return YES;

}

Important Safety Tip

The files that are being opened by the application are actually copied into a directory called "Inbox" within the apps Documents directory.  My file was called "This is a test.csm" (great name right, what am I a windows user?).  As shown in the image below, there are copies for each time I imported it on my device.



 These files should be cleaned up as required by your app.

Summary

So by adding some configuration to our apps Info.plist file and implementing a delegate method, we can handle any uniform types within our app.  The source code for this project can be found at https://github.com/stevenpsmith/CustomFileHandler.  I added some code so that it will display the contents of the file on the screen....very exciting.

Added Bonus

Since we are declaring to launch services that our app handles "csm" files, drop your custom file type into drop box.  Assuming you have the drop box app on you device, find the document in the app and try to open it.  You should get "Unable to view file".  But if you tap the icon in the upper right of the screen, you should get a list of apps that can open that file....including our app.



It's like magic :)  You see those other apps listed because we defined our UTI as text type, and those apps will handle text documents.