CS 491 Lecture 1 – Restaurand
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:
- Research in Motion’s Blackberry OS has been around for a while, since the late 1990s, and you can develop software for it in Java. My mother has a Blackberry device.
- Microsoft has had a couple of platforms, Windows Mobile and Windows Phone. The former was replaced by the latter, which came out in late 2010. It’s pretty new. We’ll have to see what happens with it.
- Apple’s iOS debuted in 2007, has a very successful app store, requires you to develop in Objective-C using MacOS, and charges you a developer fee to actually put software on your device. Running on a simulator is free.
- Symbian is an OS from Nokia, the same folks that provide the Qt cross-platform windowing toolkit. Typically one writes apps for this OS in C++. Historically Symbian has been a powerful player in the mobile market, but Nokia’s recent partnership with Microsoft kind of makes further Symbian-development a poor use of one’s time.
- Like the Blackberry OS, Palm OS has been around for a while. It was developed for Palm’s personal digital assistants in the mid 1990s. Palm was purchased by HP in 2010, and the new HP-branded devices use webOS, which is based on Linux. The webOS platform can developed for using C/C++ or JavaScript.
- Android is a product of Google and the Open Handset Alliance. It’s scrappy and open and Linux-based. You develop in Java, though you can use C or C++ for computationally-intensive code. With Android Market, you can publish both bad and good software in a matter of seconds.
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:
- Make a new Android project in Eclipse. Each application is a single Eclipse project.
- 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.
Activity
s represent a single task in an application. We may have many tasks, but theActivity
we select here will be the main one that is run when the user starts the app.
- 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:
- Install Eclipse.
- Install the Android SDK.
- Recreate our app.
- Log in to the course discussion board.
- Make a thoughtful list of 5 apps you’d like to write or be prepared to write before the semester’s over. These represent your goals. Turn them by submitting a post to the course discussion board. I will use your answers to guide our course content. This goal-setting is so important it’s worth 1% of your final grade!
Haiku
Line busy, shot pigs, phone died
I’m an angry bird