Gingerbread and Honeycomb
Markus Junginger, greenrobots
Google is developing Android rapidly: Since the release of the Android 1.0 SDK two and a half years ago, Honeycomb is the 9th (!) release of the SDK. Having catched up with its competition in previous releases, Android begins to innovate with new APIs like Near-Field-Communication (NFC). This session keeps developers up-to-date with the new APIs introduced in Android 2.3 Gingerbread and Android 3.0 Honeycomb. Developers will learn how to use state-of-the-art features while maintaining compatibility with devices running older versions of the OS.
Besides NFC, performance is probably the most important advancement in Gingerbread: Android 2.3 got a new parallel garbage collection, an improved JIT compiler and lots of new NDK features for high performance native apps. Also, the SIP API may trigger a new breed of IP telephony apps.
Honeycomb is perceived as the first “tablet version” of Android. One of the most important features are Activity fragments, which become the new building blocks for apps that target both smartphone and tablet screens. Nevertheless, tablets are just one aspect to Android 3.0. For example, developers can now speed up the UI dramatically by activating hardware accelerated rendering. The GPU is also the central part of the new animation framework and the Renderscript engine allowing 3D content and high performance shaders. Together with multicore CPU support, Honeycomb sets the stage for next-generation apps that exploit the desktop-like processing power.
The new APIs in 2.3 and 3.0 are a plentiful resource for developers to make their Android apps unique. This is the session you need to get started!
7. About me
Markus Junginger, founder of greenrobot
2007: First Android app
2001: First mobile project
12 years of Java experience
20 years of development experience
Android & Mobile Entwicklung
Android Technology Usergroup München
8. Outline
Some history
Technical look at new APIs (plus code)
Gingerbread APIs
– AudioFX, SIP, NFC, strict mode, NDK
Honeycomb APIs
– Hardware accelerated UIs, Animations,
Loaders, Renderscript, …
13. Audio Effects
MediaPlayer & AudioTrack
Available Effects
– Equalizer
– Bass boost
– Environment reverb
– Virtualizer
– Visualizer
App: Real Time Audio FX
14. Audio Effects
MediaPlayer & AudioTrack
Available Effects
– Equalizer
– Bass boost
– Environment reverb
– Virtualizer
– Visualizer
App: Real Time Audio FX
15. Audio Effects
aid = getAudioSessionId() // or 0
effect = new BassBoost(0, aid)
effect.setStrength(1000)
effect.setEnable(true)
…
effect.release()
16. Audio Effects: Equalizer
android.media.audiofx.Equalizer
Frequency bands
– Available number of bands
getNumberOfBands()
– Min/Max Level using getBandLevelRange()
– setBandLevel(band, level)
17. SIP – Session Initiation Protocol
Popular for voice and video
calls over IP
General purpose
IM, games, etc.
REGISTER
INVITE
BYE
Media separate
18. SIP – Android Classes
SipManager: central for SIP functionality
SipProfile: user account data
SipSession: session
SipAudioCall: convenience for voice calls
19. SIP – Basics steps
Get a SipManager
Create a “me” and “you” SipProfile
Use SipManager establish a call
Once established, start audio
20. SIP – Code for setup
manager = SipManager.newInstance(ctx)
builder = new
SipProfile.Builder(username, domain)
me = builder.setPassword(password)
me = builder.build()
manager.open(me)
you = SipProfile.Builder(uri).build()
// URI Example: “+49891234@sipgate.de”
21. SIP – Code for making a call
call = manager.makeAudioCall(me, you,
listener, timeoutInSeconds)
SipAudioCall.Listener
– onCallEstablished(SipAudioCall call)
– call.startAudio()
call.setSpeakerMode(true)
call.toggleMute()
22. SIP – Availability & permissions
Check if SIP is available on device
isApiSupported, isVoipSupported
Basic permissions
– INTERNET, USE_SIP
Audio call permissions
– RECORD_AUDIO, ACCESS_WIFI_STATE,
WAKE_LOCK, MODIFY_AUDIO_SETTINGS
23. Near Field Communication
NFC: wireless communication within ~4cm
13 – 106 Kbyte/s
NFC tags variants (e.g. RFID)
– Low-end: Read-only and powerless (cheap!)
–…
– High-end: full OS, interactive
Device-to-Device (P2P)
Major API additions in Android 2.3.3
24. NFC: Getting notified by Intent
android.permission.NFC
<intent-filter><action android:name =
"android.nfc.action.TAG_DISCOVERED" /
></intent-filter
android.nfc.extra.NDEF_MESSAGES:
Array of NdefMessage object
Works with API Level 9
25. NfcAdapter
API Level 9
– static getDefaultAdapter() (deprecated in 10)
– isEnabled: NFC disabled in system settings?
API Level 10
– NfcAdapter.getDefaultAdapter(context)
– Methods for foreground tag discovery/push
26. NFC: Foreground actions
Multiple apps can register for NFC tags
Foreground NFC: Activity is running
has priority over other NFC apps
enableForegroundDispatch: fires Intent
(PendingIntent) when NFC tag is read
enableForegroundNdefPush: makes a
NDEF tag available
27. NFC: Notes
API Level 10: more tag technologies
– Mifare, A, B, F, V, ISO-DEP
– Filters defined in meta-data XML file
Filtering by MIME type (e.g. text/plain)
Working with tags is pretty low level
bits & bytes
Nexus S must be really close to tag (<4cm)
31. NDK in Gingerbread
GCC 4.4.3, ./configure && make
C++ STL (Standard template library)
Native Activities
Less Java wrappers nessesary
– Input and sensor access
– Audio (OpenSL)
– Windows management / pixel buffers
– Direct access to assets in APK
33. Hardware accelerated UIs
Graphics library Skia: CPU based until 2.3
Software rendering, sometimes slow
GPU based rendering is faster (mostly)
Switched off by default
Manifest entries for application & activity:
android:hardwareAccelerated=„true“
Control for windows and views in code:
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
34. Hardware accelerated UIs, but...
Hard to test
– No Android 3.0 device in Germany yet
– Emulator falls back to software rendering
(which is really sloooooooooooooooow)
Not all operations are supported
Some operations render differently
Bitmaps must be uploaded as textures
35. Loader: Asynchronous loading
Loaders address problems of AsynchTask
– Don’t restart after configuration changes
– Ability to monitor data changes
AsyncTaskLoader http://code.google.com/p/android/issues/detail?id=14944
CursorLoader for ContentProviders
LoaderManager from activity or fragment
36. Using a CursorLoader
getLoaderManager().initLoader(0, null, l)
Implement LoaderCallbacks
– onCreateLoader: create loader here
– onLoadFinished: receive the result
– onLoaderReset: clear the result
Creating the CursorLoader: like
ContentResolver.query
(URI, projection, selection, sort order)
37. Property Animations
ValueAnimator: changes value over time
ObjectAnimator: applies value to an object
Animation Hello World:
View text = findViewById(R.id.text);
ObjectAnimator anim = ObjectAnimator
.ofFloat(text, "alpha", 0f, 1f);
anim.setDuration(2000);
anim.start();
Much more: sets, listeners, interpolators,...
38. Renderscript
Portable high performance (vs. NDK)
Intermediate LLVM byte code
Written in C (C99 standard)
Compiled and cached on the device
May use CPU or GPU (or even a DSP)
Native Renderscript APIs
– Logging, Graphics, Memory
Doesn’t run on emulator, hard to debug
39. HC: A lot more to explore…
New UI widgets, e.g. StackView
Improved home screen widgets
System clipboard framework
Drag & Drop
Media, e.g. HTTP adaptive streaming
JSON streaming
…
40. So Honeycomb is cool, but…
(Nevertheless: being among the
first may be a good strategy)
41. Android Compatibility package
Downloadable through SDK Manager
Some of 3.0 APIs for Android 1.6+
– Fragments
– Loader
– Utils
Include as jar/source (~100k)
Package android.support.v4
won’t use updated APIs on level 11+
44. Fragments
Modular approach to arrange the UI
Fragments can be (re)used in activities
Target multiple device categories
45. Fragment complexity
Activities are not trivial
Lifecycle & process
Fragments have a
lifecycle, too
Interaction with activities
Fragment transactions
Android just got more
complicated powerful
46. Action Bar
Part of Honeycomb’s Holographic theme
Quick access to Options Menu items
<item android:showAsAction="ifRoom" …
No room left: overflow menu
47. Action Bar
Part of Honeycomb’s Holographic theme
Quick access to Options Menu items
<item android:showAsAction="ifRoom" …
No room left: overflow menu
48. Action Bar, take me home!
App icon (by default)
when onOptionsItemSelected() is called
– Check for android.R.id.home
– Go to „home“ activity
(Intent.FLAG_ACTIVITY_CLEAR_TOP)
49. Action Bar, take me home!
App icon (by default)
when onOptionsItemSelected() is called
– Check for android.R.id.home
– Go to „home“ activity
(Intent.FLAG_ACTIVITY_CLEAR_TOP)
50. Action Bar: Views and Tabs
Place custom layouts in the action bar
<item android:actionLayout="…" … />
And views (use fully qualified class name)
<item android:actionViewClass="…" … />
Tabs use Fragments
ActionBar: newTab(), addTab()
TabListener adds/removes Fragements