\documentclass[final,slideColor,default]{prosper}

\title{Google's Android: An Overview}
\author{Yoni Rabkin}
\email{yonirabkin@member.fsf.org}
\slideCaption{Google's Android: An Overview}

\makeatletter
\renewcommand\@makefnmark{\@textsuperscript{\normalfont(\@thefnmark)}}
\makeatother

%% License ----------------------------------------------------- START

%% This work is licensed under the Creative Commons Attribution 2.5
%% License. To view a copy of this license, visit
%% http://creativecommons.org/licenses/by/2.5/ or send a letter to
%% Creative Commons, 543 Howard Street, 5th Floor, San Francisco,
%% California, 94105, USA.

%% Creative Common license on the bottom of each slide
\slideCaption{This work is licensed under the Creative Commons
  Attribution 2.5 License. To view a copy of this license, visit
  http://creativecommons.org/licenses/by/2.5/.}
%% Creative Common logo on the bottom of each slide
\usepackage{graphics}
\usepackage{listings}
\lstset{language=java}
\Logo{\includegraphics[height=0.35cm]{somerights20.eps}}

%% License ------------------------------------------------------- END

\begin{document}
\maketitle

\begin{slide}{Abstract}
  This lecture is an overview of developing applications for Google's
  Android. We start by introducing Android and its components, we look
  at the anatomy of an Android application, we explain basic
  components of the Android application API including UI design and
  finally we say some things about the development environment.
\end{slide}

\begin{slide}{Should I be giving this lecture?}
  I should because I...
  \begin{itemize}
  \item wrote one Android application from start to finish
  \end{itemize}
  I shouldn't because I...
  \begin{itemize}
  \item wrote only one Android application from start to finish
  \item don't know Java
  \item don't use Eclipse
  \end{itemize}
\end{slide}

\begin{slide}{Legal Stuff}
  Portions of this work are reproduced from work created and shared by
  the Android Open Source Project and used according to terms
  described in the Creative Commons 2.5 Attribution License.
\end{slide}

\begin{slide}{What is Android?}
  Android is a bunch of software\footnote{``platform'' blah blah
    ``framework'' blah blah} released by Google and the Open Handset
  Alliance\footnote{The alliance includes diverse members such as:
    China Mobile, Asus, T-Mobile (U.S.), Softbank Mobile (Japan),
    Motorola, Samsung, etc.}.
\end{slide}

\begin{slide}{What isn't Android?}
  \begin{itemize}
    \item any specific piece of hardware
    \item ``written entirely in Java'' truth: C, C++, Python, etc
    \item ``runs only Java'' truth: Android NDK
  \end{itemize}
\end{slide}

\begin{slide}{What does Android include?}
  \begin{itemize}
  \item Linux Kernel (2.6) + drivers
  \item Android Runtime: core libraries + Dalvik virtual machine
  \item Libraries: OpenGL, SGL\footnote{2D graphics}, Freetype, SSL,
    Sqlite, Webkit, libc
  \item $X$Manager where $X$ is some API component
  \item Pre-built and packaged applications: Contacts, Phone, Browser,
    Calendar, etc.\footnote{http://source.android.com/}
  \item Android SDK
  \end{itemize}
\end{slide}

%% \includegraphics[height=3cm]{dalvik.eps}\footnote{Photo Copyright, Borsteinn V. Jonsson, Attribution-Noncommercial 2.0 Generic}
\begin{slide}{The Dalvik Virtual Machine}
  The Dalvik virtual machine...
  \begin{itemize}
  \item is a \emph{register-based} Java VM
  \item is memory efficient
  \item is designed to run multiple VMs efficiently (one per
    application)\footnote{communication via AIDL (Android Interface Definition Language)}
  \item has no Just In Time compilation
  \item uses special byte-code
  \item relies on the Linux kernel for low-level stuff like threading
  \end{itemize}
\end{slide}

\begin{slide}{Low-level Libraries}
  \begin{itemize}
  \item BSD-derived libc tuned for small devices
  \item PacketVideo's OpenCore multimedia
    code\footnote{http://www.packetvideo.com/products/core/index.html}
    which supports: MPEG4, H.264, MP3, OGG, AAC, AMR, JPG, and
    PNG\footnote{PNG is the standard for applications}
  \item libWebCore: Webkit based library to support the browser and
    web-views
  \item 3D support for hardware or software rendering via OpenGL
  \item Freetype font rendering
  \item Sqlite: relational database for application use
  \end{itemize}
\end{slide}

\begin{slide}{App Anatomy: file structure}
  Selected files from a simple Android application structure.
  \ptsize{9}
\begin{verbatim}
    ./AndroidManifest.xml
    ./build.xml
    ./bin/Work.apk
    ./bin/classes/deliverator/foo/Work.class
    ./bin/classes/deliverator/foo/R.class
    ./res/layout/main.xml
    ./res/layout/setup.xml
    ./res/values/theme.xml
    ./res/values/strings.xml
    ./res/drawable/background.png
    ./res/drawable/overlay.png
    ./src/deliverator/foo/R.java
    ./src/deliverator/foo/Work.java
\end{verbatim}
\end{slide}

\begin{slide}{App Anatomy: file structure detail}
  Android can infer which resource to load by directory structure.
  \ptsize{9}
\begin{verbatim}
./res/values-en/strings.xml
./res/values-fr/strings.xml
\end{verbatim}
..for languages or...
\begin{verbatim}
./res/drawable-en-rUS-finger/
./res/drawable-port/
./res/drawable-port-160dpi/
./res/drawable-qwerty/
\end{verbatim}
...for layouts and graphics, which can lead to...
\begin{verbatim}
./res/drawable-en-rUS-port-160dpi-...-qwerty-dpad-480x320/
\end{verbatim}
\end{slide}

\begin{slide}{App Anatomy: AndroidManifest.xml}
  AndroidManifest.xml is an essential part of the application because
  it (amongst other things):
  \begin{itemize}
  \item names the Java package for the application
  \item describes activities, services, content providers, etc.
  \item declares permissions the application must have
  \item declares the minimum Android API level
  \item declares \emph{Intents}
  \end{itemize}
\end{slide}

\begin{slide}{App Anatomy: Intents}
  Intents convey requests between all different components of the
  system.

  The \emph{Intent Resolution} mechanism revolves around matching an
  Intent against all of the <intent-filter> descriptions in the
  installed applications and
  \emph{BroadcastReceiver}s.\footnote{multiple intents all get
    called!}
  \begin{itemize}
    \item defined in AndroidManifest.xml or dynamically
    \item belong to an \emph{Activity} or \emph{BroadcastReceiver}
    \item can deliver data via RFC2396 URIs
  \end{itemize}
\end{slide}

\begin{slide}{Intents: Code Example}
  \ptsize{9}
  \emph{AndroidManifest.xml}
  \begin{lstlisting}
<activity android:name=".Work" ... >
 <intent-filter>
  <action android:name="android.intent.action.MAIN" />
  <action android:name="android.intent.action.SENDTO" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:scheme="smsto" /> ...
  \end{lstlisting}
  \emph{Work.java}
  \begin{lstlisting}
Intent intent = getIntent();
String action = intent.getAction();
if (Intent.ACTION\_SENDTO.equals(action)) {
  doStuff(intent.getData().getEncodedSchemeSpecificPart() ...
  \end{lstlisting}
\end{slide}

\begin{slide}{Activities}
  An activity presents a visual user interface for a single task.
  \begin{itemize}
  \item always a subclass of the \emph{Activity} base class
  \item one application, many activities
  \item one activity calls another (stack)\footnote{An activity can return a value on exit, but doesn't have to.}
  \item given a default window to draw in
  \item visual content defined by \emph{Views}
  \item have a \emph{life-cycle}
  \end{itemize}
\end{slide}

\begin{slide}{Activities: Code Example}
  Suppose that your application has a message composition
  screen. This is an \emph{Activity}. The UI would be implemented in
  its own file, say: \emph{src/blah/blooey/Compose.java}:
  \ptsize{9}
  \begin{lstlisting}
    public class Compose extends Activity
    {
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	...
  \end{lstlisting}
\end{slide}

\begin{slide}{Activities Life-Cycle: Code Example}
  Activities call one another, so our Compose activity
  would need to define what to do if it gets interrupted, calls
  another activity or gets called:
  \ptsize{9}
  \begin{lstlisting}
    @Override
    protected void onResume() {
      super.onResume();
      dosomething();}

    @Override
    protected void onPause() {
      super.onPause();
      dosomethingelse();}

    protected void onDestroy() { ... }
    protected void onStop() { ... }
  \end{lstlisting}
\end{slide}

\begin{slide}{Threads}
  Activity UI and your code run in the same process. So non-trivial
  computations require a separate thread.
  \ptsize{9}
  \begin{lstlisting}
    final Handler mHandler = new Handler();
    final Runnable mUpdateResults = new Runnable() {
      public void run() {
	updateResultsInUi();}};
    ...
    Thread t = new Thread() {
      public void run() {
	mResults = doSomeThingHeavy(input);
	mHandler.post(mUpdateResults);}};
    t.start();
    ...
    private void updateResultsInUi() {
      if (mResults == Something) {updateUI(mResults);}}
  \end{lstlisting}
\end{slide}

\begin{slide}{Services}
  A service runs in the background for an indefinite period of time. A
  service is like an \emph{Activity} without a UI.
  \begin{itemize}
  \item examples: getting data from the network, playing a video
  \item an \emph{activity} can start a service
  \item starts explicitly with \emph{Context.startService(intent)}
  \item starts implicitly with \emph{Context.bindService(intent)}
  \item provides a callback via its \emph{onBind()} method.
  \item runs until \emph{Context.stopService()} or \emph{stopSelf()}
  \end{itemize}
\end{slide}

\begin{slide}{Broadcast receivers}
  A broadcast receives and reacts to broadcast announcements.
  \begin{itemize}
  \item one application, many broadcast receivers
  \item always a subclass of \emph{BroadcastReceiver} base class
  \item example broadcasts: low battery, call incoming, sms arrives etc.
  \item can start an \emph{activity} or the \emph{NotificationManager}
  \item as fickle and subtle as any asynchronous process is\footnote{example: registering and unregistering in onResume() and onPause()}
  \end{itemize}
\end{slide}

\begin{slide}{Broadcast receivers: Code Example}
  \ptsize{9}
  in some service of the system:
  \begin{lstlisting}
    Intent intent = new Intent(SOMEACTION);
    intent.putExtra("name", "value");
    sendBroadcast(intent);
  \end{lstlisting}
  ...then in the app:
  \begin{lstlisting}
    private BroadcastReceiver receiver = new BroadcastReceiver() {
      public void onReceive(Context context, Intent intent) {
	String value = intent.getStringExtra("name");
	SomeActivityMethod(value);
  \end{lstlisting}
\end{slide}

\begin{slide}{Content providers}
  A content provider makes an application's data available to other
  applications.
  \begin{itemize}
  \item data can be from files, sqlite DB or anything else\footnote{\emph{preferences} are lightweight name-value storage}
  \item always a subclass of \emph{ContentProvider} base class
  \item use a \emph{ContentResolver} object and call its methods
  \end{itemize}
\end{slide}

\begin{slide}{Content providers: Code Example}
  \ptsize{9}
  \begin{lstlisting}
    String[] projection = new String[] {
      People.\_ID,
      People.\_COUNT,
      People.NAME,
      People.NUMBER};
    Uri contacts =  People.CONTENT\_URI;
    Cursor managedCursor = managedQuery(contacts,
    projection,
    null,
    null,
    People.NAME + " ASC");
  \end{lstlisting}
  \emph{managedCursor} and \emph{managedQuery} manage the cursor life-cycle for you.
\end{slide}

\begin{slide}{UI: somelayout.xml}
  \ptsize{9}
  Stack some layouts with widgets in boxes:
\begin{verbatim}
  <LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <TextView
  android:id="@+id/sometext"
  android:text="@string/howareyougentlemen"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" />
  ...
\end{verbatim}
\end{slide}

\begin{slide}{UI: SomeActivity.java}
  \ptsize{9}
  Call the layout and connect to the widgets:
  \begin{lstlisting}
    public void setSomeText(String text) {
      TextView somewidget
      = (TextView) findViewById(R.id.sometext);
      somewidget.setText(text);
    }
  \end{lstlisting}
\end{slide}

\begin{slide}{UI: gefingerpoken und mittengrabben}
  \ptsize{9}
  Find a button in the layout and connect it to a listener:
  \begin{lstlisting}
    final Button somebutton
    = (Button) findViewById(R.id.somebutton);
    somebutton.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
	doSomething();
    }});
  \end{lstlisting}
\end{slide}

\begin{slide}{The Development environment}
  Eclipse\footnote{I tried to load Eclipse for this lecture... it's
    still loading.} is the natural environment for developing Android
  applications. However, \emph{real} editors can be also used if you
  are a \emph{real programmer}.

  \begin{itemize}
  \item The Android SDK integrates into Eclipse and helps the
    otherwise addled programmer write code.
  \item There are different versions of the SDK (1.0, 1.1,
    1.5)\footnote{at the time of writing}
  \item Three important command line tools: "android", "emulator" and
    "adb"
  \end{itemize}
\end{slide}

\begin{slide}{tools/android}
  An \emph{avd} is an Android Virtual Device. Use \emph{tools/android} to
  create, list, modify and remove \emph{avd}s.
\begin{verbatim}
  $ ./android create avd -t 3 -n "1.5lvl3" \
  --sdcard /path/to/disk/image
  $ ./android list
  Name: 1.5lvl3
  Path: /home/foo/.android/avd/1.5lvl3.avd
  Target: Google APIs (Google Inc.)
  Based on Android 1.5 (API level 3)
  Skin: HVGA
  Sdcard: /path/to/disk/image
\end{verbatim}
\end{slide}

\begin{slide}{tools/emulator and the Console}
  The emulator runs a virtual Android device.
\begin{verbatim}
  $ ./emulator @1.5lvl3
\end{verbatim}
Once the emulator is running, the Console (optionally) connects to an
emulator instance and can issues commands or retrieve status.
\begin{verbatim}
  $ telnet localhost 5554
  Android Console: type 'help'
  OK
\end{verbatim}
\end{slide}

\begin{slide}{Console Examples}
  Example Console commands:
  \begin{itemize}
  \item geo nmea GPGGA,123519,4807.038,N,01131.000,E ...
  \item redir add tcp:5000:5554
  \item power display/ac/status/present/health/capactiy
  \item network delay gprs/edge/umts/none
  \item network speed gsm/hscsd/gprs/.../full
  \item gsm call/accept/busy/.../list/voice/status
  \item sms send ...
  \end{itemize}
  Also from the command line:
\begin{verbatim}
  $ ./emulator -netspeed gprs
\end{verbatim}
\end{slide}

\begin{slide}{tools/adb aka Android Debug Bridge}
  The Android Debug Bridge connects to a device or emulator.
\begin{verbatim}
  $ ./adb -s HT93LLZ00513 shell
  $ ./adb -s HT93LLZ00513 install /path/app.apk
\end{verbatim}
... or connect to the device logs\footnote{useful with Log.X(TAG, info)}
\begin{verbatim}
  adb # logcat
  I/DEBUG(551): debuggerd: Apr 21 2009
  I/vold (550): Android Volume Daemon
  ...
\end{verbatim}
... or connect to the device's sqlite DB
\begin{verbatim}
  # sqlite3
  SQLite version 3.5.9
  sqlite>
\end{verbatim}
\end{slide}

\begin{slide}{Publishing}
  Publishing allows others to install your software. Publishing
  includes:
  \begin{itemize}
    \item sign the application (not with the debug key)\footnote{same signature: permissions and upgrades made easy}
    \item version the app: \emph{android:versionCode="2"} and \emph{android:versionName="1.1"}\footnote{accessible via: getPackageInfo(java.lang.String, int)}
    \item provide \emph{android:label="@string/app\_name"} and \emph{android:icon="@drawable/icon"}
    \item turn off debugging/logging, compile, sign and test
    \item upload to the Android market
  \end{itemize}
\end{slide}

\begin{slide}{Getting the Source}
  The source (about 2GB) is hosted at
  \emph{http://source.android.com/}, using Repo (some Python) and Git.\\
\end{slide}

\begin{slide}{References and Adieu}
  \begin{itemize}
  \item http://developer.android.com/
  \item http://android-developers.blogspot.com/
  \item http://groups.google.com/group/android-beginners
  \item http://groups.google.com/group/android-developers
  \end{itemize}
  \vspace{7em}
  \hfill
  \huge{Happy Hacking}
\end{slide}

%% fixme: how do we install from a Web site?

%% fixme: add file types for application anatomy section from:
%% [http://developer.android.com/guide/appendix/faq/commontasks.html#filelist]

\end{document}
% END Document
