Monday, April 23, 2012

Professional Android 4 Application Development: Because I only work in powers of 2

Professional Android 4 Application Development, started shipping today (Monday) from Amazon US - so those of you who pre-ordered should be seeing your copies in a couple of days. I'm really excited and can’t wait to find out what people think.

Professional Android 4 Application Development cover image
Where to buy

If you're interested in picking up a copy, you can get the paperback delivered to your door from these fine retailers:
If you prefer to travel light, there's an electronic version to suit your tastes:
I'm particularly pleased with the electronic editions of this release, which are significantly better than those of Professional Android 2. It's nice to see that this time they're  out at the same time as the paperbacks.

What's new?

This edition is a monster. Everything has been revised and expanded, with four new chapters and more than 300 extra pages (that's around 50% more) added since Professional Android 2.

Some of the highlights amongst the new content include:
  • Fragments and the Action Bar
  • CursorLoaders
  • The audio focus APIs
  • NFC, Wi-Fi Direct, and Android Beam
  • Using the Intent Service
  • A new chapter on publishing your app to Google Play
  • Introductions to LVL, IAB, and C2DM
  • Creating collection-based widgets and rich notifications
  • Using new sensors (including the barometer)
  • Property animations
  • Accessibility 
  • Implementing copy and paste
Some context

The whole thing took me a touch over a year to write. I started writing an update for Gingerbread and Honeycomb back in March of 2011 and before I'd finished, Ice Cream Sandwich dropped and I found myself doing some frantic rewrites and adding a few extra pages.

That means it's been two years between revisions, and as Professional Android 4 rolls off the presses there have been 8(!) platform releases.

Professional Android 2 was released within a few weeks of Android 2.1. As of now, 87% of devices are running a newer build of Android. The Android ecosystem has grown to include tablets, with more than 800 different Android devices created by 55 OEMs and available on over 300 carriers.

More than 850k new Android devices are activated daily, with the 450k+ apps in Google Play having been downloaded more than 10 billions time.

Support

You can download all the code snippets and sample projects used in the book from the Wrox Open Source site.

If you've got any questions related to the book, you can post them over at the Wrox P2P forums. For anything programming related, I'd recommend using Stack Overflow (and adding a PA4AD tag). I'll be monitoring both and endeavoring to answer promptly.

I've created a +Professional Android 4 Application Development Google+ Page, and you can always get in touch with me over at Twitter or on Google+.

Monday, March 26, 2012

Understanding Mobile Radio State to Build Apps that Don't Drain the Battery

tl;dr: Read the new Android Training Class, Transferring Data Without Draining the Battery, to learn how to potentially halve the battery life impact of your apps' data transfers based on the underlying radio architecture.
One of the beauties of modern smartphone platforms is the abstraction of underlying hardware.

I've been building mobile apps for almost 5 years, and had no idea how the underlying 3G radio worked. I didn't have to. I just open a connection and start downloading data.

Dalvik negotiates a transport mechanism to ensure I get the fastest and most efficient data connection possible. Wi-Fi or mobile, Edge or LTE, it doesn't matter. Or so I thought.

We all know that data transfers on mobile radios chews up a lot of battery, so we're careful to restrict how much we download. It's a balance between app latency and chewing up bandwidth and battery life.

Turns out it's not so much the amount you transfer, but how frequently you power up the radio.

The problem with abstractions is that hiding the complexities means disguising some possible optimizations—something I came to learn after speaking to the good folks at at AT&T and DoCoMo.


Optimizing Downloads for Efficient Network Access explains that to minimize the power drain associated with the mobile radio, it will go into standbye mode whenever it's not in use. Before you can upload or download data the mobile radio needs to be powered-up. Powering up from standby introduces around 2 seconds of latency when making data transfer requests.

No one wants to wait an extra 2s every time they try to follow a link, so rather than dropping straight back to standby, there is a tail-time during which the radio stays active to reduce that latency.

The exact numbers vary depending on the carrier, but once you stop transferring data the radio stays on—at full power—for around 5 seconds. Then stays at a "low energy" state (which introduces some latency, but uses less battery) for around another 12 seconds.

Every data transfer session will cause the radio to draw energy for almost 20 seconds.

As an app developer, knowing that every time you touch the network you can draw power for nearly 20 seconds should have a dramatic impact on the way you structure you data transfer routines.

That includes prefetching, batching your downloads, eliminating redundant downloads, and prefetching even more aggressively when using higher bandwidth (but more power-hungry) radios.


Learn more at Android Training

This is just a brief summary, my Android Training class: Transferring Data Without Draining the Battery teaches you more about the underlying radio hardware, how to use that knowledge to optimize your apps' battery impact, and how to analyze your apps' current transfer profile.

Monday, January 02, 2012

2011: My Year in Review

2011 was a big year for me. I moved from London to the Bay Area, got promoted, wrote Professional Android 4 Application Development, and had my first Thanksgiving.

In amongst all that I read some books, took some pictures, and played with some Android gadgets. Here's a little summary of the highlights.

Books

This year I used Google Books to store my 2011 reading list. My count was down somewhat (16 books compared to 23 last year), mainly due to the free-time spend writing the aforementioned book. This year's highlights:

  • Favorite Book: A Dance with Dragons by George R. R. Martin.
  • Most Read Author: I read two books by Courtney Summers and two by Wil Wheaton, but 16 books doesn't leave a lot of room for doubling up in a year when Feist, Pratchett, and Martin all release new books. 
  • Hardcover versus Paperback: 4 hardcovers, 5 Kindle eBooks, 7 paperbacks.
Apps

Lots of changes to the list this year. iPlayer, Ocado, and the London Cycle Hire Widget all drop off the list thanks to my move Stateside. Beluga gets dropped in favour of G+ Messenger, and TweetDeck gets the old uninstall thanks to increasingly poor performance and my shift away from Twitter towards Google+.

New to the list this year are Pandora and Google Music, which have revolutionized the way I listen to music since moving to the US. Cut the Rope, 3D Bowling, and the MX Video Player earned their striped keeping me entertained flying between California and Western Australia, and News Republic has come out on top when it comes to giving me news on the go.

Photography

A move to the Bay Area (and its hundreds of hiking trails through gorgeous nature reserves), a visit to NYC, and a trip back to Australia provided ample fodder for some photography. This year I tried my hand at some HDR processing too. A portfolio of my best pics is online here.


Gadgets

What am I carrying these days? Check out Reto Meier's Gadget Compendium.

The big changes this year were the introduction of the Galaxy Nexus to replace my Nexus S, and the inclusion of a Galaxy Tab 10.1 at the expense of the Xoom and a 7" Galaxy Tab. The 10" Tab is thin, light, and last forever -- indispensable for long haul travel.

Trends for 2011:
  • Lighter and thinner: The 10" Galaxy Tab probably weighs less than the 7" version. My Galaxy Nexus is the thinnest Android phone I've owned and is lighter than the smaller device it replaces.
  • Bigger screens: A 10" tablet replaced the 7" version and the Galaxy Nexus has a bigger screen than the Nexus S. People talk about screens being "too big", but provided they weigh less and don't suck up more power, I don't see a problem.
As for 2012? I'm hoping to see some Android @ Home gear this year. I think it might be time to upgrade my camera gear -- starting with a prime lens and going from there. I like the new Kindles, but the Kindle 3G I'm using right now does everything I need, so I don't see a Fire in my future.

Wednesday, November 09, 2011

Australia in High Dynamic Range

I've been really impressed with the work +Trey Ratcliff has been sharing on Google+, which led to me wondering if HDR might be a way to add some "punch" to my landscape photography. My recent holiday back to Oz offered the perfect excuse to experiment. I've put my best HDR photos so far into this Google+ album.

I've been working on my photography skills for a while now, but there are certain scenes that I've  found particularly challenging. Dramatic sunsets, photos taken in bright sunlight (or towards the sun), or photos taken on dark, gray days have been a struggle to capture adequately on camera.

HDR seemed like a technique that might help me capture on film what I could see with my eyes, and what better place to practice than on a Western Australian beach at sunset?

We also spent some time in Ballarat, over in Victoria. In recent years rural Australia has been known for a crippling drought followed by devastating floods, but when we visited it was a velvet field punctuated by saphires, with the dams and lakes all filled to capacity.

We were staying within the shadow of Mount Buninyong, which provided the perfect opportunity to experiment with some midday shots taken from a high vantage point.

Bird's eye views are always stunning in person, but I've had difficulty turning that view into  interesting photos -- particularly as I seldom make it to these places at dusk or dawn when the natural light would be more favorable.  My initial results were definitely encouraging.

To help experiment, my trusty Canon EOD 500D has an exposure bracketing option that lets me take three consecutive pictures using different exposures. Photoshop comes with an automation plugin that merges multiple exposures to produce HDR images.

I learned a few things from my experience so far. The first - somewhat obviously - is to look for scenes with an abundance of color depth. Rich greens offset by deep blues and grays look fantastic.

Somewhat less obvious is the effect that a hint of rich color can add to an otherwise monochromatic scene. HDR will add layers of depth to grey clouds and dark seas, so a small splash of red or green can produce dramatic results.

I also learned that taking portrait photos in HDR is much more difficult. Close-ups can be incredibly unflattering as skin tones are exaggerated and people start to creep into the uncanny valley.

It's also tricky to photograph scenes with movement. When you merge the images, slight differences are often shown up as artifacts or ghosting. A steady hand is a must (my best results used a tripod), and shooting toward the sun will minimize your exposure times. Looking at the images blown up on my 24" monitor, it's also clear that there are more annoying pixel artifacts, halos, and ghosting that I need to work on to improve the final effect.

Overall, I need to practice to get better results, but I the progress so far is promising and HDR is definitely a tool I'll be adding to my amateur photography tool-belt.

[I've disabled comments here in favour of using Google+. Feel free to join the conversation over there.]

Tuesday, November 08, 2011

Memories in the White Space

A distinct melancholy accompanies me as I sort through the images and artifacts of my youth.


My wife and I left Australia almost 7 years ago. We lived in London and now the Bay Area, but for me home is still Perth. We're back this month—the first time in three and a half years—and I'm using the opportunity to free my parents of some of the detritus I left with them before taking off in 2005.

Our visit has been timed to coincide with the wedding of one of my very best friends. I've been friends with the groom and most of his side of the wedding since our first year at Duncraig Senior High. We were all members of the Academic Extension program (a particularly nefarious way to target those of us most likely to be on the fringes of high school social life and stigmatize us further by segregating us into separate classes.)

When we all get together for some quiet drinks the night before the wedding it's only a matter of minutes before my accent has slowed and thickened, and we're poking fun and chatting as though I'd never left. The same pattern repeats as we catch up with close friends I'm lucky to see every few years. We share a hug and a beer and talk about their new kids, houses, fiances, spouses, and business ventures with an easy comfort that makes it seem like only a or two week has passed since we last hung out.

Back at my parent's house, amongst the polyhedral dice, Star Trek VHS tapes, and school assignments are 10 A3 scrapbooks filled with photographs of me, my friends, and family from birth until I moved out at 21.

I grew up in the age before digital cameras and smartphones captured every moment (magic or otherwise) ready for posting to Facebook. As teenage boys, my friends and I were particularly adept at avoiding my mum's instamatic. As a result, flipping through the stacks of photo albums is a surreal experience. Christmases, birthdays, high school balls, and graduation ceremonies are all captured in full colour—but what strikes me most is the memories that live in the white space between the photos.

A thumb-obscured image doesn't capture the experience of all-night LAN parties spent playing Doom 2. A single photo of us playing pickup basketball (without the hoop in frame) is a faint reminder of the hours spent on court and the four broken arms collected between me and the aforementioned groom during games; plaster-cast testimony to our passion for the game.

A shot of me posing, awkward and gangly, in my inter-school sports uniform captures nothing about the day, but brings back the crowd of apathetic high-schoolers gathering around the high-jump mats, and the rush (and not a small amount of surprise) I felt as they genuinely cheered me on to jump my own height and break the school record.


There aren't any photos to commemorate the long nights spent playing AD&D, or the Friday nights we all spent at WesTrek watching boot-leg videos of each new episode of TNG, but the Player's Handbooks and mountains of Star Trek videos, books, and technical manuals bring back the memories all the same.

20 years. That's how long I've known some of my closest friends. Two thirds of my life. I'm a proper geek, so don't find it easy to build these effortless friendships, so the comfort of sliding back into them is tempered by the knowledge that it'll be years until I can next hang out with some of my best friends.

Email, Facebook status updates, and Google+ will help us stay in touch until the next time we voyage the 8,000 miles back home. When that happens there'll be more kids to meet, new houses to tour, and new businesses to hear about. We'll hug, share a few beers, and it'll be like we never left.

[I've disabled comments here in favour of using Google+. Feel free to join the conversation over there.]

Thursday, July 07, 2011

Obligatory Post Speculating on Google+

I love product launches. It's the perfect time to speculate with no inconvenient research or history to get in the way.

This goes for everything on this blog, but it's probably worth highlighting in this instance that these opinions are my own. They do not represent the thoughts and opinions of Google, the Google+ team, or anyone else who works at Google.

My history of speculating on products tends to be bullish on Google and cynical of social. I thought Android and Wave were going to change the world, and that Twitter was a waste of time.

Twitter with conversations

Despite my initial reservations I'm a big user of Twitter, but I find that most of my interaction there is effectively anonymous - I'm either reading things by interesting people I don't know, or sharing things I think are interesting with people I've never met.

I've found that half my use of Google+ works similarly - by posting publicly and creating a "My Stream" circle full of interesting folks who I don't know personally.

Where I think Google+ adds value is with threaded conversations. By attaching the conversation that emerges from each post, anonymity is reduced and the process of sharing and reading are suddenly more social.

Facebook with sharing controls

I remember quite clearly the moment my use of Facebook went from regular to sporadic. My manager's passing comment on my most recent status update (something along the lines of "I'm so bored I'm considering setting myself on fire just to liven up my day") prompted this blog post.

I've always maintained a policy of only adding people I know and would recognize in person as Facebook friends. Nonetheless, when your extended family, school friends, and current / former work colleagues are all reading the same stream, and seeing the same pictures, the intersection of "appropriate material" rapidly tends towards zero.

Using circles to fragment my audience has been an elegant solution for me.

I've created the obvious circles like "friends", "family", and "Googlers" but I've found smaller adhoc circles particularly useful when socializing

Socializing+

In the paleolithic age we used email to arrange social events and share the photos afterwards, but it never really worked.

Facebook is a good alternative, but it requires adding people you don't necessarily know to your "friends" list.

Being able to create an adhoc circle (or just add individual people to a post) - makes it easy to work out the details for a 4th July BBQ - and then post the photos - all in one place.

I've not spent a lot of time with Huddles or Hangouts yet, but they seem a natural extension. I can see using Huddle instead of SMS to let folks know you're running late, to get parking advice, or confirm the orders for a lunch-run. I've used similar products (most notably Beluga and GroupMe) to coordinate amongst a large group at conferences or events like MWC or Google I/O.

Social photo sharing without wanting to punch your screen

Photos are probably the reason most folks joined Facebook to begin with. It's also the reason many people hate Facebook.

I'll happily rave about the Google+ photo experience which is awesome. It's easy to share photos with just a small group, or post your best amateur photography for the world to critique.

I don't want to be social at work

For all the good uses, let me highlight a couple that I don't see catching on.

I admit to having been a little skeptical of Google+ during the dogfooding stage. With 20/20 hindsight, I think a lot of that had to do with it being effectively a corporate social network. My email inbox is full enough as it is; I really don't need another stream to monitor in order to be involved in work conversations.

This is not a blog

You'll note that I haven't posted this directly on Google+.

I don't want to read your essay in my social stream, just give me an abstract and link to your blog. For added bonus points, make sure your blog links back to your Google+ profile.

In Conclusion

Twitter is entirely public and as a result my interactions there are regular but tend towards the impersonal. Facebook is limited to people I know so the interactions are more personal, but (increasingly) less frequent.

Google+  lets me choose which group of people I'm comfortable sharing something with to a degree that lets me have regular, personal conversations.

As many of you have no doubt noticed - that makes for an addictive combination.

Wednesday, July 06, 2011

London 2005-2011 in Photographs

I really like the photo sharing and viewing experience in Google+ so I decided to sort through my massive collection of "London" photographs and share some of my favorites.

Selecting and preparing photos to share has a way of focussing your attention and allowing you to really look at them critically. As I sifted through the thousands of photographs I'd taken in London it quickly became obvious that I've got some work to do before I'm competing with Romain Guy.

It was also clear that I had a couple of preferred sources of inspiration.

The Seasons

Grey skies and light rain don't make for great photos and an overcast Winter that starts in October and ends around April does little to provide inspiration.

London is blessed with real seasons though, and Autumn and Spring (however brief) are an entirely different matter. They offer some of the most amazing light and color for taking photos. And when it snows? London transforms briefly into a winter wonderland.

By 10am the skies cloud over and the snow turns to mush, so to take advantage you need to be out there at dawn. I worked in banking, so that was never a problem.

Each of the following thumbnails links to a gallery of my pictures of London in Winter, Spring, and Autumn respectively.


The Sights

London has some of the most easily recognized landmarks in the world. Because of the seemingly perpetually grey and overcast skies, lots of tourist snaps come out flat and dull. To get around that I've taken most of them at night or very early in the morning.

Tuesday, June 28, 2011

A Deep Dive Into Location Part 2: Being Psychic and Staying Smooth

This is part two of A Deep Dive into Location. This post focuses on making your apps psychic and smooth using the Backup Manager, AsyncTask, Intent Services, the Cursor Loader, and Strict Mode.
The code snippets used are available as part of the Android Protips: A Deep Dive Into Location open source project. More pro tips can be found in my Android Pro Tips presentation from Google I/O. 
Being Psychic

You've just had to factory reset your device - never a good day - but yay! You've opted in to "backup my settings" and Android is happily downloading all your previously installed apps. Good times! You open your favourite app and... all your settings are gone.


Backup Shared Preferences to the Cloud using the Backup Manager

If you're not using the Backup Manager to preserve user preference to the cloud I have a question for you: Why do you hate your users? The Backup Manager was added to Android in Froyo and it's about as trivial to implement as I can conceive.

All you need to do is extend the BackupAgentHelper and create a new SharedPreferencesBackupHelper within it's onCreate handler.

As shown in the PlacesBackupAgent, your Shared Preferences Backup Helper instance takes the name of your Shared Preference file, and you can specify the key for each of the preferences you want to backup. This should only be user specified preferences - it's poor practice to backup instance or state variables.

public class PlacesBackupAgent extends BackupAgentHelper {
  @Override
  public void onCreate() {
    SharedPreferencesBackupHelper helper = new
      SharedPreferencesBackupHelper(this, PlacesConstants.SHARED_PREFERENCE_FILE);
    addHelper(PlacesConstants.SP_KEY_FOLLOW_LOCATION_CHANGES, helper);
  }
}


To add your Backup Agent to your application you need to add an android:backupAgent attribute to the Application tag in your manifest.

<application android:icon="@drawable/icon" android:label="@string/app_name"
             android:backupAgent="PlacesBackupAgent">


You also need to specify an API key (which you can obtain from here: http://code.google.com/android/backup/signup.html)

<meta-data android:name="com.google.android.backup.api_key"
           android:value="Your Key Goes Here" />


To trigger a backup you just tell the Backup Manager that the data being backed up has changed. I do this within the SharedPreferenceSaver classes, starting with the FroyoSharedPreferenceSaver.

public void savePreferences(Editor editor, boolean backup) {
  editor.commit();
  backupManager.dataChanged();
}


Being Smooth: Make everything asynchronous. No exceptions.

Android makes it easy for us to write apps that do nothing on the main thread but update the UI.


Using AsyncTask

In this example, taken from PlaceActivity, I'm creating and executing an AsyncTask class to lookup the best previous known location. This isn't an operation that should be particularly expensive - but I don't care. It isn't directly updating the UI, so it has no business on the main application thread.

AsyncTask<void, void, void> findLastLocationTask = new AsyncTask<void, void, void>() {
  @Override
  protected Void doInBackground(Void... params) {
    Location lastKnownLocation =
      lastLocationFinder.getLastBestLocation(PlacesConstants.MAX_DISTANCE,
      System.currentTimeMillis()-PlacesConstants.MAX_TIME);

    updatePlaces(lastKnownLocation, PlacesConstants.DEFAULT_RADIUS, false);
    return null;
  }
};
findLastLocationTask.execute();


You'll note that I'm not touching the UI during the operation or at its completion, so in this instance I could have used normal Thread operations to background it rather than use AsyncTask.

Using the IntentService

Intent Services implement a queued asynchronous worker Service. Intent Services encapsulate all the best practices for writing services; they're short lived, perform a single task, default to Start Not Sticky (where supported), and run asynchronously.

To add a new task to the queue you call startService passing in an Intent that contains the data to act on. The Service will then run, executing onHandleIntent on each Intent in series until the queue is empty, at which point the Service kills itself.

I extended Intent Service for all my Service classes, PlacesUpdateService, PlaceDetailsUpdateService, PlaceCheckinService, and CheckinNotificationService.

Each implementation follows the same pattern, as shown in the PlacesUpdateService extract below.

@Override
protected void onHandleIntent(Intent intent) {
  String reference = intent.getStringExtra(PlacesConstants.EXTRA_KEY_REFERENCE);
  String id = intent.getStringExtra(PlacesConstants.EXTRA_KEY_ID);

  boolean forceCache = intent.getBooleanExtra(PlacesConstants.EXTRA_KEY_FORCEREFRESH, false);
  boolean doUpdate = id == null || forceCache;

  if (!doUpdate) {
    Uri uri = Uri.withAppendedPath(PlaceDetailsContentProvider.CONTENT_URI, id);
    Cursor cursor = contentResolver.query(uri, projection, null, null, null);

    try {
      doUpdate = true;
      if (cursor.moveToFirst()) {
        if (cursor.getLong( cursor.getColumnIndex(           PlaceDetailsContentProvider.KEY_LAST_UPDATE_TIME)) >
          System.currentTimeMillis()-PlacesConstants.MAX_DETAILS_UPDATE_LATENCY)
            doUpdate = false;
      }
    }
    finally {
      cursor.close();
    }
  }

  if (doUpdate)
    refreshPlaceDetails(reference, forceCache);
}


Note that the queue is processed on a background thread, so I can query the Content Provider without having to spawn another background thread.

CursorLoaders are awesome. Use them.

Loaders are awesome; and thanks to the compatibility library, they're supported on every platform back to Android 1.6 - that’s about 98% of the current Android device install base.

Using CursorLoaders is a no-brainer. They take a difficult common task - obtaining a Cursor of results from a Content Provider - and implement, encapsulate, and hide all the bits that are easy to get wrong.

I've already fragmented and encapsulated my UI elements by creating three Fragments -- PlaceListFragment, PlaceDetailFragment, and CheckinFragment. Each of these Fragments access a Content Provider to obtain the data they display.

The list of nearby places is handled within the PlaceListFragment, the relevant parts of which are shown below.

Note that it's entirely self contained; because the Fragment extends ListFragment the UI is already defined. Within onActivityCreated I define a Simple Cursor Adapter that specifies which Content Provider columns I want to display in my list (place name and my distance from it), and assign that Adapter to the underlying List View.

The final line initiates the Loader Manager.

public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  activity = (PlaceActivity)getActivity();

  adapter = new SimpleCursorAdapter(activity,
    android.R.layout.two_line_list_item,
    cursor,
    new String[]
      {PlacesContentProvider.KEY_NAME, PlacesContentProvider.KEY_DISTANCE},
    new int[] {android.R.id.text1, android.R.id.text2}, 0);

  // Allocate the adapter to the List displayed within this fragment.
  setListAdapter(adapter);

  // Populate the adapter / list using a Cursor Loader.
  getLoaderManager().initLoader(0, null, this);
}


When the Loader is initiated we specify the parameters we would normally pass in to the Content Resolver when making a Content Provider query. Instead, we pass those parameters in to a new CursorLoader.

public Loader<cursor> onCreateLoader(int id, Bundle args) {
  String[] projection = new String[]
    {PlacesContentProvider.KEY_ID,
    PlacesContentProvider.KEY_NAME,
    PlacesContentProvider.KEY_DISTANCE,
    PlacesContentProvider.KEY_REFERENCE};

  return new CursorLoader(activity, PlacesContentProvider.CONTENT_URI,
    projection, null, null, null);
}


The following callbacks are triggered when the Loader Manager is initiated, completed, and reset respectively. When the Cursor has been returned, all we need to do is apply it to the Adapter we assigned to the List View and our UI will automatically update.

The Cursor Loader will trigger onLoadFinished whenever the underlying Cursor changes, so there's no need to register a separate Cursor Observer or manage the Cursor lifecycle yourself.

public void onLoadFinished(Loader loader, Cursor data) {
  adapter.swapCursor(data);
}

public void onLoaderReset(Loader loader) {
  adapter.swapCursor(null);
}


The PlaceDetailFragment is a little different; in this case we don't have an Adapter backed ListView to handle our UI updates. We initiate the Loader and define the Cursor parameters as we did in the Place List Fragment, but when the Loader has finished we need to extract the data and update the UI accordingly.

Note that onLoadFinished is not synchronized to the main application thread, so I'm extracting the Cursor values on the same thread as the Cursor was loaded, before posting a new Runnable to the UI thread that assigns those new values to the UI elements - in this case a series of Text Views.

public void onLoadFinished(Loader loader, Cursor data) {
  if (data.moveToFirst()) {
    final String name = data.getString(
      data.getColumnIndex(PlaceDetailsContentProvider.KEY_NAME));
    final String phone = data.getString(
      data.getColumnIndex(PlaceDetailsContentProvider.KEY_PHONE));
    final String address = data.getString(
      data.getColumnIndex(PlaceDetailsContentProvider.KEY_ADDRESS));
    final String rating = data.getString(
      data.getColumnIndex(PlaceDetailsContentProvider.KEY_RATING));
    final String url = data.getString(
      data.getColumnIndex(PlaceDetailsContentProvider.KEY_URL));

    if (placeReference == null) {
      placeReference = data.getString(
        data.getColumnIndex(PlaceDetailsContentProvider.KEY_REFERENCE));
      updatePlace(placeReference, placeId, true);
    }

    handler.post(new Runnable () {
      public void run() {
        nameTextView.setText(name);
        phoneTextView.setText(phone);
        addressTextView.setText(address);
        ratingTextView.setText(rating);
        urlTextView.setText(url);
      }
    });
  }
}


Using Strict Mode will prevent you from feeling stupid

Strict Mode is how you know you've successfully moved everything off the main thread. Strict Mode was introduced in Gingerbread but some additional options were added in Honeycomb. I defined an IStrictMode Interface that includes an enableStrictMode method that lets me use whichever options are available for a given platform.

Below is the enableStrictMode implementation within the LegacyStrictMode class for Gingerbread devices.

public void enableStrictMode() {
  StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
    .detectDiskReads()
    .detectDiskWrites()
    .detectNetwork()
    .penaltyDialog()
    .build());
}


The only thing I hate more than modal dialogs in apps is apps that freeze because a network read or disk write is blocking the UI thread. As a result I've enabled detection of network and disk read/writes and reports using a modal dialog.

I've applied Strict Mode detection to the entire app by extending the Application class to instantiate the appropriate IStrictMode implementation and enable Strict Mode. Note that it is only turned on in developer mode. Be sure to flick that switch in the constants file when you launch.

public class PlacesApplication extends Application {
  @Override
  public final void onCreate() {
    super.onCreate();

    if (PlacesConstants.DEVELOPER_MODE) {
      if (PlacesConstants.SUPPORTS_HONEYCOMB)
        new HoneycombStrictMode().enableStrictMode();
      else if (PlacesConstants.SUPPORTS_GINGERBREAD)
        new LegacyStrictMode().enableStrictMode();
    }
  }
}

Thursday, June 23, 2011

How to Build Location-Based Apps That Don't Suck

If I were forced to choose between a smartphone that could make / receive voice calls, and one with Google Maps - I would choose Maps without blinking.

Here Back in London, getting a reliable 3G connection is a challenge at the best of times - getting one while sat in most venues is about as likely as a South West Trains running a good service. So it doesn't help when I go to view details for, checkin, or review a location and a lack of 3G signal thwarts my efforts.

Whether it's opening a FourSquare app to checkin, or Qype / Zagat / Where to choose where to eat, or the London Cycle Hire Widget to find a Boris Bike - I always feel like a douche standing around with my phone in my hand for half a minute while my phone gets a GPS fix and downloads the nearest locations.

High latency and a lack of offline support in location-based mobile apps is a blight that must be cleansed

Rather than (or indeed: after) shaking my fist at the sky in impudent rage, I wrote an open-source reference app that incorporates all of the tips, tricks, and cheats I know to reduce the time between opening an app and seeing an up-to-date list of nearby venues - as well as providing a reasonable level of offline support.

You can find out more in the associated deep-dive into location on the Android Developer Blog.

Android Protips: Location Best Pratices

It should came as no surprise to learn that I've borrowed heavily from my Android Protips presentation from Google I/O. Including (but not limited to) using Intents to receive location updates, using the Passive Location Provider, using Intents to passively receive location updates when your app isn't active, monitoring device state to vary refresh rate, toggling your manifest Receivers at runtime, and using the CursorLoader.

But Wait There's More!

The post on the Android Developer Blog focusses on freshness - I'll be posting another deep-dive into the code that examines how I've made the app psychic and smooth on this blog early next week. Stay tuned.

Wednesday, May 25, 2011

Answers to Unanswered Questions from the I/O Protips Q&A

There's never enough time for Q&A at the end of an I/O session - particularly when the session is immediately followed by lunch. In an effort to remedy this, here are the answers to most of the questions that were entered onto the Moderator page for my Android Protips talk.
    Eclipse is a wonderful development tool. However sometimes it is clunky. Generic error messages, mysteriously build problems solved by quitting & relaunching, etc. Do you ever get frustrated with Eclipse? Do you have any tips of working with Eclipse?
I do! Almost as much as I was frustrated by Visual Studio in a previous life spent writing C# Winform GUIs. I don't have any specific tips beyond the things that work with mode IDEs. Frequently restart and make sure you're using the latest stable build along with the latest version of the ADT.
    Do you have any tips for working with SQLite databases through eclipse? At the minute I rely on external tools to view data that is in the database on the phone to debug problem. This means manually copying it off the device. Any tips?
You can use the sqlite3 command line tool to examine SQLite databases on the phone. It's not built into Eclipse but might save you the extra work of pulling the database off the device first.
    Do the developers at Google use any hardware emulators to speed up development. If so, can you please recommend some. The soft emulator is too slow.
Unfortunately not. Generally we'll be working towards the release of the new platform on a given piece of hardware, so the internal teams will use that rather than an emulator where appropriate / applicable.
    Will there be a faster Android device emulator anytime soon?
Yes! Check out this session on Android Development Tools for a preview.
    Is there a suite of AVDs for Eclipse that emulate actual devices?
Some manufacturers make AVDs available for actual devices (I believe Samsung provide an AVD for the 7" Galaxy Tab). Generally speaking, no - there's no central repository or suite of all actual device AVDs.
    Will there soon be a legitimate way to replace the Android lockscreen (with a lockscreen application)?
Due to the security implications, I'm not aware of any plans to make the lock screen (or in-call screen) replaceable.
    You mentioned better not to loose your signing key. But how to update your app, when your certificate expired?
For now, certificates used to sign apps launched in the Android Market need to expire after 22 October 2033. We'll have a solution for replacing these certificates in place well before 2033 :)
    What's the recommended way to implement a horizontally scrolling, virtualized list?
No simple answer here as it depends on the kind of data you're displaying, how long your list is, and what the best user experience would be. There are some good articles online (including this answer on Stack Overflow) that explain how to create a virtual list in a ListView, but you can use a similar technique within a Gallery or even a HorizontalScrollView to achieve a horizontal virtualized scrolling list.
    Are Shared Preferences the bast way to store small piece of data?
It depends on what kind of small data you're storing. Shared Preferences are the best way to store user preferences and Activity / Application state information.
    A view from one app needs to be updated by another app. Can't use the widget paradigm, is there any other way?
This depends on a number of factors. Are both apps written by you, or is one a third party? How dramatic are the changes? New layouts or changed text in a TextView?

Generally speaking, the best approach is likely to be a Broadcast Intent. You can package the data that will be used to update the View in the "other" app by including them as extras. The "other" app simply registers a Broadcast Receiver that listens for the Intent, extracts the data, and updates its view accordingly.
    How would you test/optimize the apps that are not meant for the Android Market?
The principle of using Analytics for tracking bugs and doing A/B testing works just as well internally as it would on apps that will launch in Market. The biggest difference is distribution. Given the ability to side-load apps onto most Android devices, I'd most likely setup an internal website that would host the APKs you want to distribute for the Beta test.
    How can we know if a certain service is already running?
You can bind to a Service using bindService without starting the Service. The Service Connection you pass in to bindService will notify you using its onServiceConnected and onServiceDisconnected handlers when the Service starts and stops. You can use those callbacks to set a variable within your code to check if the Service is running at any given time.
    Is there any option to backup the default SharedPreferences via BackupManager? Do I have to use the packagename?
The default SharedPreferences file getSharedPreferences uses the Activity's class name as the preferences name.
    Is there a way for accessories to "push" an embedded app or service package to the device so that specialized services for certain types of accessories will be able to automatically add functionality to the device when connected?
As part of the process of starting the device in accessory mode, you send identifying string information to the device. This information allows the device to figure out an appropriate application for this accessory and also present the user with a URL if an appropriate application does not exist. It won't install the package for you, but it will prompt the user to download it.
    Just to make sure, now we can send requests to the devices in order to update the info, instead of having the refresh intervals?
That's right, you can use Cloud to Device Messaging to ping a device when it needs to perform an update.
    When will we have a UI Builder that is par to what we get for iPhone?!
Check out this session on Android Development Tools for a preview of some of the cool stuff the tools team have been working on.
    Can you describe your video and control hookup for your android tablet?
I wrote a blog post that describes the video and control hookup I used to do my presentation using a pair of Motorola Xooms.

Monday, May 23, 2011

My Attitude Towards Piracy of My Book

The short answer: I am against it.

Lest I be accused of bias, that goes for every book - not just the ones that result in a couple of bucks landing in my pocket.

The long answer

I was surprised recently when asked via email what my attitude was towards piracy of my book, and if an online donation might work as a "last resort" way for pirates to show their appreciation for my work.

For the record, I am a big supporter of making my book available in as many formats as possible. That's why I'm impressed that Wrox books are now available on Kindle, Google Books, Safari, and PDF eBooks. I get a royalty no-matter where you buy it from, so if you want a copy pick wherever offers the cheapest price for the format you prefer.

I'm also against DRM - I believe that DRM does nothing to prevent piracy while annoying the folks who legitimately paid for the content - so I was also thrilled with Wrox's decision to make their eBooks DRM free.

Your book is expensive: Do you have a donate link anywhere to show my appreciation but save a few bucks?

I can't speak for anyone else, but I don't write my books for the money. The advance and royalties go some way to compensating for the significant time and effort it takes to get the books written - but for me at least, it's not going to make me rich or let me give up my day job.

More importantly I don't write in a vacuum. Books cost money to make. I'm not talking about the paper, printing, and transport costs, I'm talking about all the people who were involved in making my book the best it could be.

There are 17 people on the "Credits" page for Professional Android 2 Application Development. They are not vanity credits. As an example, the following folks are the ones on that list who I had direct, repeated email contact with over the course of writing the book. Apologies to those I left out - they are equally important to the process.
  • Scott Meyers (Acquisitions Editor) suggested I write a second edition and shepherded it through the process.
  • William Bridges (Project Editor) made sure I handed in chapters on something resembling a schedule (without him I'd still be working on the 1st edition), as well as dispensing invaluable advice on everything from book and chapter structure to clarity and semi-colon use.
  • Milan Shah (Technical Editor) reduced the number of bugs in my code.
  • Sadie Kleinman (Copy Editor) corrected my comma use, spelling errors, grammatical issues, and generally ensured I didn't embarrass myself.
  • Mary Beth Wakefield (Editorial Manager) kept it all together when things got chaotic.
  • Kyle Schlesinger (Proofreader) ensured nothing slipped past us during the many edits and revisions before it went to print.
  • Michael Trent (Cover Designer) gave us the awesome Terminator cover (image by Linda Bucklin).
  • Robert Swanson (Indexer) provided a way to find things without a photographic memory.
  • David Mayhew (Marketing) made sure it was available from wherever people wanted to buy it.
These folks are an absolutely essential part of the writing process, and they don't work for free. Nor should they. There is a really simple way to show your appreciation for all the people involved in writing a book. Buy it.

Sure, but I need to know what the book teaches for my job / class, but I don't have the money to buy it.

Good news! You don't have to! At the risk of hurting my own sales, you don't need to buy my book to learn how to develop Android apps. I mentioned this during my Android Protips talk: Google aren't trying to keep this information a secret. There's a huge amount of information available online including:
Many people find the structured, consistent, and guided form of a book to be a great way to learn new material. Others find books a more useful reference while working. If you're one of those people, and you want to be able to continue using books in such a way, buying them is the only way to help ensure that will happen.

What are your thoughts on 2nd hand books and libraries?

Bring them on! I've probably bought 1,000 books in my life - of which more than half are 2nd hand, and I've probably borrowed a few hundred from libraries too.

Electronic books introduce new challenges to the 2nd hand book market and libraries. I'm firmly on the side that says that the rights we have with paper books should be mirrored in the electronic publishing age. If you're done with a book you should be able to sell it, loan it, or give it away without restriction.

If everyone downloads pirated PDFs of books instead of buying them, publishers will stop publishing them.

Without a publisher many books won't get written or released. I don't have the time, money, skills, or inclination necessary to produce a book of sufficient quality on my own. I need the 17 people on the credits page (and several more besides) to publish each new book or revision.

Writing isn't my livelihood, so as an author the end of publishing would be a serious disappointment, but I'd just stop writing and get on with my day job.

As a reader? I can barely imagine a future so grim.

Monday, May 16, 2011

Android Protips: Where to Download the Slides and Code Snippets

For those of you who want to take a closer look at my Android Protips session for Google I/O, you can now enjoy the video, slides, and code snippets in whichever format you prefer:
One of the nice things about SlideShare is that it lets you embed slideshows into your blog post:


I plan to do a series of more blog posts that dig into some of the topics I cover in the presentation in more detail. Where do you guys think I should start?

Android Protips at I/O: The Session Video (and How I Presented It)

[Update 16/May: Reposted after Blogger outage]
[Update 2: Working links to the sessions slides are available from here]

Another Google I/O, another jam-packed Android session room. This year they nearly doubled the room capacity for the main Android track making space for 1,000 seats. That still wasn't enough though - once again people were sitting on the floor and lining up to get in.

Android Protips: Advanced Topics for Expert Android Developers

After delivering my Android Best Practices for Beginners for the better part of last year, I was really excited to take things up a notch and deliver some real advanced content. To push things one step further, I presented my session using a pair of Xoom tablets. More on that after the video.



The awesome video content was created for me by an old friend of mine (he's still young, but we've been friends since high school) pandamusk - thanks panda!

How did you do that?

There were a lot of questions on Twitter asking:
  1. What app did I use to do the presentation using an Android tablet
  2. How did I live tweet my own presentation in real time?
  3. How did I not re-tweet everything when the tablet rebooted?
I'm an engineer so (of course) I took this as an excuse opportunity to write an app that does the former and built in functionality to do the latter (and come on — you think I didn't consider the case of having to restart? Please.)

How does it work?

One app, running on two tablets, both running Android 3.1 (with USB hostmode support) connected via Bluetooth.

Tablet one was wired up with HDMI out and a USB-connected clicker let me transition between slides. I added a "finger paint" View with a transparent background on top of the ImageView that displayed each slide which let me do the real-time annotations.


A second device (out of sight on the lectern) showed me my "Speaker View": My speaker notes, the current / next slide preview, my pre-written live tweets, and a countdown timer.


The two devices were paired and connected over Bluetooth, with the speaker view tablet set up as a slave to the presentation device. Whenever the display tablet transitioned slides, it transmitted the current slide to the speaker view tablet. It works the other way around too, so I can transition slides on the speaker view and have the live view update accordingly.

The tweeting happened on the speaker view tablet based on slide transitions (with a button for me to disable it if — for example — I had to restart half way through). I connected this one to a wired ethernet connection using a USB to ethernet dongle to avoid the notorious conference wifi syndrome.

I've got a bunch of ideas I'd like to incorporate (particularly around remote viewing), but ran out of time before I/O to get them implemented.

Can I Get the App? Can I See the Source?

Yes and yes. I need to make a few improvements before I release it on the Android Market and I need to refactor and tidy the code before I open source it. In the mean time I'll do a couple more posts the go into more detail on how each of the components work. Stay tuned.

Tuesday, May 10, 2011

How to Get Your Android Protips

I/O is always a great couple of days, and after last year's jam-packed Android room they've doubled our capacity and given us space for 1,000 folks in the audience. As if that wasn't enough, we're also live streaming the Android (and Chrome) sessions for your viewing pleasure.

What Am I Presenting and How Can You Watch?

I'm presenting Android Protips: Advanced Topics for Expert Android Developers at 11:30am PDT in room 11 up on the top floor (next to the keynote room) for those of you lucky enough to be at I/O in person.

If you're not here, you can still watch my session live as it's going to be live streamed across the intertubes (Stay tuned: that page will update when I/O starts in a few short hours).

If you are planning to tune in, that's 7:30pm BST and an ungodly 4:30am on the East coast of Australia.

If you've got questions, and won't be in the audience, you can pose them (and vote for others) on the session's moderator page.

I'm also going to try live-tweeting my own presentation using an Android app I've been working on for presentations (more on that later).

Once I'm done, the recorded video and slides will be available on the Android Protips session page. I'll also post a link to the code snippets for your copy/paste pleasure.

If You Just Can't Get Enough (or You Want to Know How to Avoid Bumping in to Me)

I'm also co-hosting "Web Apps versus Native" on Wednesday afternoon with Michael Mahemoff. Should be a good way to wind down after a long couple of days. When I'm not on stage I'll be hanging out at the Android office hours, so be sure to stop by and say hi!