droidcon 2011: gingerbread and honeycomb, markus junginger, greenrobot
DESCRIPTION
Gingerbread and HoneycombMarkus Junginger, greenrobotsGoogle 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!TRANSCRIPT
New Android APIs:Gingerbread and Honeycomb
New Android APIs:Gingerbread and Honeycomb
24. März 2011Markus Junginger
New Android APIs:Gingerbread and Honeycomb
Markus Junginger@greenrobot_de
New Android APIs:Gingerbread and Honeycomb
@greenrobot_de
New Android APIs:Gingerbread and Honeycomb
New Android APIs:Gingerbread and Honeycomb
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
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, …
API Changes History
API Changes History
Releases & TimeAPI Version API
LevelSDK Released
Previews since
- 2007-11
1.0 1 2008-09
1.1 2 2009-02
1.5 3 2009-04
1.6 4 2009-09
2.0 5 2009-10
2.0.1 6 2009-12
2.1 7 2010-01
2.2 8 2010-05
2.3 9 2010-12
2.3.3 10 2011-02
3.0 11 2011-02
Release early & often 11 official releases 29 months Every ~3 months a new
release Android developers:
constant progress isyour chance
Android 2.3 Gingerbread
Performance (Concurrent GC, ext4, …) Audio Effects SIP NFC StrictMode NDK revamped
Audio Effects
MediaPlayer & AudioTrack Available Effects
– Equalizer– Bass boost– Environment reverb– Virtualizer– Visualizer
App: Real Time Audio FX
Audio Effects
MediaPlayer & AudioTrack Available Effects
– Equalizer– Bass boost– Environment reverb– Virtualizer– Visualizer
App: Real Time Audio FX
Audio Effects
aid = getAudioSessionId() // or 0 effect = new BassBoost(0, aid) effect.setStrength(1000) effect.setEnable(true) … effect.release()
Audio Effects: Equalizer
android.media.audiofx.Equalizer Frequency bands
– Available number of bands getNumberOfBands()
– Min/Max Level using getBandLevelRange()– setBandLevel(band, level)
SIP – Session Initiation Protocol
Popular for voice and videocalls over IP
General purposeIM, games, etc.
REGISTER INVITE BYE Media separate
SIP – Android Classes
SipManager: central for SIP functionality SipProfile: user account data SipSession: session SipAudioCall: convenience for voice calls
SIP – Basics steps
Get a SipManager Create a “me” and “you” SipProfile Use SipManager establish a call Once established, start audio
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: “[email protected]”
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()
SIP – Availability & permissions
Check if SIP is available on deviceisApiSupported, isVoipSupported
Basic permissions– INTERNET, USE_SIP
Audio call permissions– RECORD_AUDIO, ACCESS_WIFI_STATE,
WAKE_LOCK, MODIFY_AUDIO_SETTINGS
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
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
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
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
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)
Strict mode
Tool for developers (helper classes) Detect performance violations in threads
– Network operations– Disk reads/writes
Detect leaks (VM wide)– Activites– Closeable– SQLite objects
Strict mode
Ways to react to violations (penalties)– Log, drop box, dialog, or crash
Strict mode activated & configured in code android.os.StrictMode and inner classes
– Policies– Builders
Strict mode – thread policy code
p= Builder().detectAll().penaltyLog().build() StrictMode.setThreadPolicy(p) Example log with stack trace
03-19 20:21:44.292: DEBUG/StrictMode(32242): StrictMode policy violation; ~duration=336 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=279 violation=203-19
Note: MapActivity triggers violations
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
Android 3.0 Honeycomb
Fragments (conceptual) Hardware accelerated UIs Action Bar Loaders Animations Renderscript And more…
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)
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
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
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)
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,...
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
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 …
So Honeycomb is cool, but…
(Nevertheless: being among the first may be a good strategy)
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+
Fragments
Modular approach to arrange the UI Fragments can be (re)used in activities Target multiple device categories
Fragment complexity
Activities are not trivial Lifecycle & process
Fragments have alifecycle, too
Interaction with activities Fragment transactions Android just got more
complicated powerful
Action Bar
Part of Honeycomb’s Holographic theme Quick access to Options Menu items <item android:showAsAction="ifRoom" … No room left: overflow menu
Action Bar
Part of Honeycomb’s Holographic theme Quick access to Options Menu items <item android:showAsAction="ifRoom" … No room left: overflow menu
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)
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)
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