Rackspace Cloud for Android Technical Overview

Filed in by Mike Mayo | May 20, 2010 12:12 pm

Rackspace Cloud for Android is a simple tool for managing Cloud Servers[1] in your Rackspace Cloud account. It is available in the Android Market and the source code is available at http://github.com/rackspace/android-rackspacecloud[2]

This post will explain the architecture and design of the app. To build the app on your machine, you’ll need the Android SDK, available at http://developer.android.com/sdk/index.html[3]

Root File and Folder Structure

AndroidManifest.xml: This file is the main descriptor for the app. It includes details such as the name of the app, the minimum SDK version, and any Activities that exist in the app.

src: Java source code for the app

gen: Android-generated Java classes. Notably, R.java, which contains constants that reference various assets in the app, such as images, buttons, labels, and layouts.

res: This is the resources folder. It contains images and icons in the drawable folders, XML user interface descriptions for layouts and menus, and string values for labels.

The Source Code

There are two main packages in the Android app:

The API binding code is fairly self explanatory, so we’ll focus on the rackspacecloud.android package.

rackspacecloud.android

There are five Activities in this package. Each Activity class corresponds to a screen in the Android app (such as the login screen or the server view screen).

RackspaceCloudActivity

This is the first Activity you see in the app: the login screen.

If you look at AndroidManifest.xml, you’ll see that RackspaceCloudActivity has an intent-filter category setting of android.intent.category.LAUNCHER, while all the others are android.intent.category.APPLICATION.

I’ll explain some core Android concepts here that will also apply to all of the other Activities in this package.

The first thing Android OS does when an Activity is launched is call the onCreate method. I’ve added extra comments to the actual method to explain what happens:

public void onCreate(Bundle savedInstanceState) {
    // always call the parent class's method
    super.onCreate(savedInstanceState);

    // set our layout to R.layout.main.
    // this corresponds to res/layout/main.xml, where the login screen UI is
    // defined.
    setContentView(R.layout.main);

    // set this Activity to be the click handler for the login button
    ((Button) findViewById(R.id.button)).setOnClickListener(this);

    // set this Activity to be the edit handler for the api key text field
    // we will look for the return key as a signal to login
    ((EditText) findViewById(R.id.login_apikey)).setOnEditorActionListener(this);

    // call a private method to pre-load the login and api key text fields
    // with the previously used login
    loadLoginPreferences();

    // i'll explain this later
    restoreState(savedInstanceState);

    // we'll load the ListServersActivity on a successful login
    tabViewIntent = new Intent(this, ListServersActivity.class);
}

The next methods to keep in mind are restoreState and onSaveInstanceState. When an Android device changes orientations between portrait and landscape mode, the Activity is destroyed and recreated. Use these methods to maintain state when this happens. In the case of the RackspaceCloudActivity, I save whether or not the “Authenticating…” label should be displayed when someone has clicked the login button.

Most other methods in this class are self-explanatory, but I would like to cover how HTTP requests are made asynchronously to the Rackspace Cloud API.

In the login() method, you’ll see the following line:

new AuthenticateTask().execute((Void[]) null);

Near the bottom of the file, you’ll see a private subclass of AsyncTask called AuthenticateTask. AsyncTask objects are used to perform operations in the background without having to worry about thread management.

In the AuthenticateTask class, there are two methods: doInBackground(), which is actual operation, and then onPostExecute(), which is called when the task is complete. In this case, we load the Cloud Servers images, and then the flavors before proceeding to the server list.

TabViewActivity

This Activity is not currently being used. As of this writing, the app only provides Cloud Servers support, so upon login we launch the ListServersActivity. When Cloud Files is added, we’ll use this Activity on login instead, and it will have two tabs: Cloud Servers and Cloud Files.

ListServersActivity

This is, obviously, the server list view. It subclasses ListActivity, which includes some extra methods for list management. Most of it should be self-explanatory if you followed the RackspaceCloudActivity explanation, but one thing worth noting is the ServerAdapter inner class. This is the class that defines what a row looks like in the list (the OS logo, server name, and flavor and image details). When a row is clicked, the ViewServerActivity class is launched.

AddServerActivity

This is the screen for creating a new server.

ViewServerActivity

This is the screen for viewing a server’s details. It contains AsyncTask classes for all server actions, such as rebooting, resizing, and deleting.

Hopefully this was a helpful introduction to the design of the Rackspace Cloud Android app. I highly encourage you to fork the code on github and even contribute if you have an idea that you’d love to see in the app.

Endnotes:
  1. Cloud Servers: http://www.rackspacecloud.com/cloud_hosting_products/servers
  2. http://github.com/rackspace/android-rackspacecloud: http://github.com/rackspace/android-rackspacecloud
  3. http://developer.android.com/sdk/index.html: http://developer.android.com/sdk/index.html

Source URL: http://www.rackspace.com/blog/rackspace-cloud-for-android-technical-overview/