teaching machines

CS 491 Lecture 1 – Restaurand

September 6, 2011 by . Filed under cs491 mobile, fall 2011, lectures.

Welcome to the course notes for CS 491, Mobile Software Development. You can subscribe to RSS feed with your favorite reader. (I use Google Reader.)

Take a moment to check out the syllabus!

Introduction

Hi. My first cell phone had a cord. It was so big I couldn’t fit it under my car seat. It came in a bag and was about the size of a box of cereal, post-economic downturn. No, it didn’t have a rotary dial. My parents got it for me so that I could call after school and see if my mechanic father needed me to bring any car parts home from town. After graduating from high school, I jettisoned the phone. Mileage improved.

My second phone was a Motorola Droid 2. There’s a large span of time between these two phones! I held out a long time because I don’t like talking on the phone. But I do like writing code, and cell phones are finally starting to look like fully-programmable mini-computers. That’s what this class is about: writing software for mobile devices.

What platform?

There are several mobile platforms around that we could learn about:

We’d like this class to be practical and lucrative. iOS would be a good choice to achieve this, but Apple really complicates the development process. So, we’re going with Android. It’s very well-supported, doesn’t require a device, and is gaining market share at the time of this writing.

Android history

Android started off as the child of a startup named Android, Inc. Google slurped them up in 2005, retaining their engineers, and established a consortium of vendor-developers called the Open Handset Alliance to manage the platform.

Openness is one of the primary goals of the OHA. You can download the OS source code and have a look. And publication of custom software is super easy, with no gauntlet to pass through like Apple’s App Store. Android itself is licensed using the Apache v2 license, which permits licensees to distribute modified works under completely different licenses, either free or proprietary.

Here’s a little about what Android folks have to say about the platform:


Android OS’s gooey center is the Linux kernel. The OS provides a suite of libraries to make application development really, really slick. It’s also packaged with a bunch of third-party libraries, including OpenGL, WebKit, and SQLite.

Applications are typically written in Java and compiled into a super-compressed bytecode format called DEX. They are run on an instance of a virtual machine named Dalvik (DEX stands for Dalvik EXecutable). Each application is well-insulated against other applications: each app is run in its own process with its own VM.

Example app

Okay, let’s write an app. I’m new to this area, so I’d like your input on the best places to eat. Let’s assemble a list of restaurants, and for each restaurant, tell me what percent of the time we should go there. We’ll go until the percents add up to 100. And for variety’s sake, let’s not let any particular restaurant go above 25%.

[collect list]

Okay, let’s make our app. Assuming we have Eclipse and the Android SDK and Eclipse plugin installed, let’s get started:

  1. Make a new Android project in Eclipse. Each application is a single Eclipse project.
  2. Fill in the dialog:
    • Project name: randaurant.
    • Choose a reasonable build target. 2.1 is pretty reasonable, as it provides a really strong baseline of functionality and is supported by a good number of devices.
    • Application name: Randaraunt. This name will appear on the device under its icon.
    • Package name: edu.uwec.userid.randaurant. Each application has its own package.
    • Create Activity: Randaurant. Activitys represent a single task in an application. We may have many tasks, but the Activity we select here will be the main one that is run when the user starts the app.
  3. Click Finish.

The Android plugin generates a lot of project files for us. Let’s head directly to src and find our Activity class:

package edu.uwec.userid.randaurant;

import android.app.Activity;
import android.os.Bundle;

public class Randaurant extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
}

We’re not going to fill much in about what this is doing right now. The savedInstanceState code deals with restoring the state of a paused app and setContentView hooks up a UI for this activity. That UI is actually defined imperatively in XML. This way of defining UIs will get lots of our attention in forthcoming lectures, but for today, let’s just edit the res/layout/main.xml file to give the TextView an identifier:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/destination"
    android:text="@string/hello"
    />
</LinearLayout>

All right, let’s head back to the Activity class and add a method that returns a random restaurant. What should this look like?

[code]

Now, let’s allow the user to tap the screen to choose a restaurant:

@Override
public boolean onTouchEvent(MotionEvent event) {
  // Query for the TextView whose content we'd like to change. Since the
  // UI is defined in XML, we've got to have some way to get a reference
  // to it in code. This is the way.
  TextView label = (TextView) findViewById(R.id.destination);

  // Change the text!
  label.setText(getRandomRestaurant());

  // Mark that we consumed the event. Don't propogate it.
  return true;
}

Let’s run it. We can do so either on the emulator or on a real device. Preferably both.

Action items

Okay, we’ve gotten a taste of Android history and development. More to come! But you’ve got a few things to do first:

Haiku

Fell down well, called Ma
Line busy, shot pigs, phone died
I’m an angry bird