droidcon 2011: gingerbread and honeycomb, markus junginger, greenrobot

50
New Android APIs: Gingerbread and Honeycomb

Upload: droidcon-berlin

Post on 27-Jan-2015

106 views

Category:

Technology


0 download

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

Page 1: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

New Android APIs:Gingerbread and Honeycomb

Page 2: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

New Android APIs:Gingerbread and Honeycomb

24. März 2011Markus Junginger

Page 3: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

New Android APIs:Gingerbread and Honeycomb

Markus Junginger@greenrobot_de

Page 4: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

New Android APIs:Gingerbread and Honeycomb

@greenrobot_de

Page 6: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

New Android APIs:Gingerbread and Honeycomb

Page 7: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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

Page 8: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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, …

Page 9: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

API Changes History

Page 10: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

API Changes History

Page 11: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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

Page 12: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

Android 2.3 Gingerbread

Performance (Concurrent GC, ext4, …) Audio Effects SIP NFC StrictMode NDK revamped

Page 13: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

Audio Effects

MediaPlayer & AudioTrack Available Effects

– Equalizer– Bass boost– Environment reverb– Virtualizer– Visualizer

App: Real Time Audio FX

Page 14: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

Audio Effects

MediaPlayer & AudioTrack Available Effects

– Equalizer– Bass boost– Environment reverb– Virtualizer– Visualizer

App: Real Time Audio FX

Page 15: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

Audio Effects

aid = getAudioSessionId() // or 0 effect = new BassBoost(0, aid) effect.setStrength(1000) effect.setEnable(true) … effect.release()

Page 16: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

Audio Effects: Equalizer

android.media.audiofx.Equalizer Frequency bands

– Available number of bands getNumberOfBands()

– Min/Max Level using getBandLevelRange()– setBandLevel(band, level)

Page 17: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

SIP – Session Initiation Protocol

Popular for voice and videocalls over IP

General purposeIM, games, etc.

REGISTER INVITE BYE Media separate

Page 18: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

SIP – Android Classes

SipManager: central for SIP functionality SipProfile: user account data SipSession: session SipAudioCall: convenience for voice calls

Page 19: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

SIP – Basics steps

Get a SipManager Create a “me” and “you” SipProfile Use SipManager establish a call Once established, start audio

Page 20: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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]

Page 21: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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()

Page 22: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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

Page 23: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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

Page 24: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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

Page 25: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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

Page 26: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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

Page 27: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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)

Page 28: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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

Page 29: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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

Page 30: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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

Page 31: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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

Page 32: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

Android 3.0 Honeycomb

Fragments (conceptual) Hardware accelerated UIs Action Bar Loaders Animations Renderscript And more…

Page 33: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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)

Page 34: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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

Page 35: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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

Page 36: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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)

Page 37: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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,...

Page 38: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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

Page 39: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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 …

Page 40: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

So Honeycomb is cool, but…

(Nevertheless: being among the first may be a good strategy)

Page 41: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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+

Page 44: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

Fragments

Modular approach to arrange the UI Fragments can be (re)used in activities Target multiple device categories

Page 45: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

Fragment complexity

Activities are not trivial Lifecycle & process

Fragments have alifecycle, too

Interaction with activities Fragment transactions Android just got more

complicated powerful

Page 46: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

Action Bar

Part of Honeycomb’s Holographic theme Quick access to Options Menu items <item android:showAsAction="ifRoom" … No room left: overflow menu

Page 47: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

Action Bar

Part of Honeycomb’s Holographic theme Quick access to Options Menu items <item android:showAsAction="ifRoom" … No room left: overflow menu

Page 48: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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)

Page 49: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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)

Page 50: Droidcon 2011: Gingerbread and honeycomb, Markus Junginger,  Greenrobot

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