Android 1.5 to 3.0               a compatibility journey               Emanuele Di Saverio               Senior Technologi...
Who?        • Emanuele Di Saverio           – Senior Technologist for Android Platform @              – Open Source Commit...
Objectives                   • Code Compatibility                   • Screen Compatibility                   • User Interf...
DISCLAIMER!                       bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ...
1.5 - Cupcake                   • Virtual Keyboard: IME                   • AppWidget                   • 2 New WidgetMond...
1.6 - Donut                   • Screen Fragmentation                   • Resources Framework                   • Search In...
2.0 - Eclair                   • Support for HTML5                   • Bluetooth Support                   • Sync Services...
2.1 - Eclair (again)                   • Live Wallpapers                   • Enrich WebKit                   • lots of bug...
2.2 - Froyo                   • App on SD                   • Cloud 2 Device                       Messaging              ...
2.3 - Gingerbread                   • Near Field                       Communication                   • Gyroscope,       ...
2.3.4 - Gingerbread                              (again)                   • more NFC Api                   • more BT Api ...
3.0/3.1 - HoneycombMonday, May 23, 2011
• Every release brings new classes, new                       methods, changes and deprecations                   • We Wan...
DOCSMonday, May 23, 2011
Target                         API                        LevelMonday, May 23, 2011
Android does no                       magic here                        We compile against                             onl...
Code Fragmentation                   • 3 Techniques                    • Canary                    • Switch by Code/Resour...
Canary                   • Canaries are use in cave digging as a                       “probes” for venomous gases        ...
• We can’t replace or modify Java                       Classloader in a Dalvik VM :(                   • classes are veri...
If this code is executed on a VM where                       ScaleGestureDetector class is not                       avail...
...by catching the VerifyError!                       (explain please...)                                   try {         ...
Switch by Code                   • use info from android.os.Build                   • can be used on the fly               ...
ad-hoc          public class RomaSkiFragmentActivity extends          FragmentActivity {          	 private final boolean ...
factory          	     public static synchronized LocalDevice getInstance() {          	     	 if (instance == null) {    ...
Switch by Resource                   • leverage the Resource framework                   • the -v su x is for versions    ...
Reflection                   • Java allows us to:                    • instantiate classes starting from their             ...
If you miss                              something...            private static int R_id_message_handle = -1;            	...
android-bluetooth library                       void bindAndListen() {                       	   	  	   try {             ...
disclaimer on Reflection             • It’s dangerous: depends on names, avoids                       compiler checks, make...
Screen Fragmentation                   • Google ha a   rontato aggressivamente il                       problema della fra...
small   medium   large   xlarge                  ldpi                mdpi                 hdpi                xhdpiMonday,...
small     medium       large   xlarge                         cheapo,                  ldpi    highly                     ...
• It’s not a universal solution!                   • What happens if I have a extra-large                       screen wit...
Monday, May 23, 2011
• But it works for real-life devices                   • Pragmatic Balance between                       performances and ...
The Real Reason               • behind screen compat framework is...               • keeping the physical size the same (d...
Real Estate                   • so I just have to provide image for ldpi                       mdpi and hdpi and I am set,...
•       800x480 (240 dpi)= 5/3                                         but DIPs are scaled       •       480x320 (160 dpi)...
Solutions               • Modal Controls:                          SlidingDrawer, FlipViews, Alerts,                      ...
Some solutionsMonday, May 23, 2011
Some solutions/2Monday, May 23, 2011
...but don’t do Least Common                       Denominator :(Monday, May 23, 2011
Design Patterns                   • Google doesn’t enforce constraints on                       Application UI Design     ...
Android UI DesignPatternsMonday, May 23, 2011
Twitter App                       GoogleIO 2010Monday, May 23, 2011
Twitter App                       Home         Action Bar                         GoogleIO 2010                           ...
Resources               • Home, QuickActions, ActionBar:                                                          Cyril Mo...
Designing Android UIs   for Phones and Tablets                   • Pattern consolidated and implemented in                ...
Monday, May 23, 2011
Monday, May 23, 2011
Home                                   Action Bar                              DashboardMonday, May 23, 2011
So...                   • Use the patterns - they are designed for                       delivering YOUR content          ...
A real life example:                            RomaSki                   • Application for keeping under control         ...
for PhoneMonday, May 23, 2011
TabletMonday, May 23, 2011
Fragments breakdown                     1Monday, May 23, 2011
Fragments breakdown 1IMonday, May 23, 2011
Action Bar (was: “Title”)                   • title Bar under HoneyComb is so powerful! Also has                       tab...
non-HC TabletsMonday, May 23, 2011
More Compatible than                             Google :)                        “early” 2.2                          tab...
Versioned Scale                       • Android uses GestureDetectors for decoupling                       • ScaleGestureD...
private OnScaleGestureListener mListener;            private ScaleGestureDetector internalDelegate;            private Can...
GML Diagram                       (galactic modeling language, Craig Larman)Monday, May 23, 2011
Design API for change               • The public interfaces of the Versioned                       ScaleGestureDetector ar...
Monday, May 23, 2011
Resources               • GoogleIO Schedule App src:                                         http://code.google.com/p/iosc...
What was left out               • AndroidManifest filters: <supports_screen> will suprise                       you,       ...
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"   a:orientation="vertical" a:layout_width="fill_parent"...
Upcoming SlideShare
Loading in...5
×

Android 1.5 to 3.0: a compatibility journey

3,655

Published on

Having problems with Android application compatibility? You're not alone.

From the very beginning of the Android platform, developers always had to cope with a very difficult target: developing on a moving platform, a very fast moving one.

Release after release, features and goodies piled up, but, to a minor extent, so did API differentiation. This talk will step through how platform releases can risk jeopardizing professional development of Android applications, analyzing how to keep consistent UI patterns and implementation, and ending with the biggest release since 1.0: Honeycomb.

Published in: Technology, Education
1 Comment
8 Likes
Statistics
Notes
No Downloads
Views
Total Views
3,655
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
1
Likes
8
Embeds 0
No embeds

No notes for slide

Android 1.5 to 3.0: a compatibility journey

  1. 1. Android 1.5 to 3.0 a compatibility journey Emanuele Di Saverio Senior Technologist frogMonday, May 23, 2011
  2. 2. Who? • Emanuele Di Saverio – Senior Technologist for Android Platform @ – Open Source CommitterMonday, May 23, 2011
  3. 3. Objectives • Code Compatibility • Screen Compatibility • User Interface Patterns • a Real Life ExampleMonday, May 23, 2011
  4. 4. DISCLAIMER! bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla the following is result of my professional experience and will be technical and advanced topics, I will give for granted a good level of familiarity with the platform bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla blaMonday, May 23, 2011
  5. 5. 1.5 - Cupcake • Virtual Keyboard: IME • AppWidget • 2 New WidgetMonday, May 23, 2011
  6. 6. 1.6 - Donut • Screen Fragmentation • Resources Framework • Search Integration for AppMonday, May 23, 2011
  7. 7. 2.0 - Eclair • Support for HTML5 • Bluetooth Support • Sync ServicesMonday, May 23, 2011
  8. 8. 2.1 - Eclair (again) • Live Wallpapers • Enrich WebKit • lots of bug fixesMonday, May 23, 2011
  9. 9. 2.2 - Froyo • App on SD • Cloud 2 Device Messaging • Device Manager • Data backupMonday, May 23, 2011
  10. 10. 2.3 - Gingerbread • Near Field Communication • Gyroscope, Barometer, various Sensors • StrictMode • SIPMonday, May 23, 2011
  11. 11. 2.3.4 - Gingerbread (again) • more NFC Api • more BT Api • lots of Bug fixes • fix for ClientLogin • Accessory DevKitMonday, May 23, 2011
  12. 12. 3.0/3.1 - HoneycombMonday, May 23, 2011
  13. 13. • Every release brings new classes, new methods, changes and deprecations • We Want New Features • We Want to target ALL devices • We Don’t Want to constantly rewrite!Monday, May 23, 2011
  14. 14. DOCSMonday, May 23, 2011
  15. 15. Target API LevelMonday, May 23, 2011
  16. 16. Android does no magic here We compile against only one JAR: choose our feature set! On device we will have di erent classesMonday, May 23, 2011
  17. 17. Code Fragmentation • 3 Techniques • Canary • Switch by Code/Resource • ReflectionMonday, May 23, 2011
  18. 18. Canary • Canaries are use in cave digging as a “probes” for venomous gases • If they don’t come back, you can’t use that cave • We want a Code Canary!Monday, May 23, 2011
  19. 19. • We can’t replace or modify Java Classloader in a Dalvik VM :( • classes are verified as soon as somebody new them • ..so classloaders load classes lazily!Monday, May 23, 2011
  20. 20. If this code is executed on a VM where ScaleGestureDetector class is not available, we can “gracefully degrade” class Canary { android.view.ScaleGestureDetector delegatto; static void tryNewClass() { android.view.ScaleGestureDetector foo = new android.view.ScaleGestureDetector(null, null); } /* use the delegatto */ }Monday, May 23, 2011
  21. 21. ...by catching the VerifyError! (explain please...) try { Canary.tryNewClass(); initalizeNative(ctx); } catch (VerifyError th) { th.printStackTrace(); }Monday, May 23, 2011
  22. 22. Switch by Code • use info from android.os.Build • can be used on the fly • or via Factory pattern if performance is needed (i.e. operations on UI Thread)Monday, May 23, 2011
  23. 23. ad-hoc public class RomaSkiFragmentActivity extends FragmentActivity { private final boolean isHoneycomb = android.os.Build.VERSION.SDK_INT > 10; @Override protected void onCreate(Bundle arg0) { super.onCreate(arg0); if (!isHoneycomb) { requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); } /* cut */Monday, May 23, 2011
  24. 24. factory public static synchronized LocalDevice getInstance() { if (instance == null) { int vInt = LocalDevice.getVersionNumber(); log.i(LocalDevice.class, "Parsed version number is " + vInt); if (vInt < SDK_NUM_2_0) { instance = new it.gerdavax.easybluetooth.LocalDevice1Impl(); } else { instance = new it.gerdavax.easybluetooth.LocalDevice2Impl(); } } log.i(LocalDevice.class, "Returning: " + instance); return instance; } private static int getVersionNumber() { return Integer.parseInt(Build.VERSION.SDK.trim()); }Monday, May 23, 2011
  25. 25. Switch by Resource • leverage the Resource framework • the -v su x is for versions • es. -v4 resource will be selected for Api level 4 (Donut)Monday, May 23, 2011
  26. 26. Reflection • Java allows us to: • instantiate classes starting from their name in a String • invoke methods, even private • inspect fields, even private • Some problems with final and static though • With big power comes big...risksMonday, May 23, 2011
  27. 27. If you miss something... private static int R_id_message_handle = -1; static { try { Class clazz = Class.forName("com.android.internal.R$id"); R_id_message_handle = clazz.getField("message").getInt(clazz); } catch (Throwable e) { // who cares e.printStackTrace(); } } public static void setRightAlignToMessageField(Dialog d) { if (R_id_message_handle != -1) { TextView tv = (TextView) d.findViewById(R_id_message_handle); if (tv != null) { tv.setGravity(Gravity.RIGHT);Monday, May 23, 2011
  28. 28. android-bluetooth library void bindAndListen() { try { Method bindMethod = bluetoothSocketClass.getMethod (METHOD_BIND, new Class[] { String.class }); bindMethod.invoke(bluetoothSocketObject, new Object[] { "devicename-tobeignored" }); Method listenMethod = bluetoothSocketClass.getMethod (METHOD_LISTEN, new Class[] { int.class }); listenMethod.invoke(bluetoothSocketObject, new Object[] { this.port }); Method getPortMethod = bluetoothSocketClass.getMethod (METHOD_GET_PORT, new Class[] {}); Integer getPortMethodReturnValue = (Integer) getPortMethod.invoke(bluetoothSocketObject, new Object[] {}); this.port = getPortMethodReturnValue.intValue(); } catch (Throwable t) { throw new RuntimeException(t); } }Monday, May 23, 2011
  29. 29. disclaimer on Reflection • It’s dangerous: depends on names, avoids compiler checks, makes your code ugly without remedy • It allows to access @hide functionalities, and break the public API • Google hates it: for good reasons • ...and makes you feel quite a hacker :)Monday, May 23, 2011
  30. 30. Screen Fragmentation • Google ha a rontato aggressivamente il problema della frammentazione - 1.6 • Approccio pragmatico basato sui “bucket”: eventualmente correttoMonday, May 23, 2011
  31. 31. small medium large xlarge ldpi mdpi hdpi xhdpiMonday, May 23, 2011
  32. 32. small medium large xlarge cheapo, ldpi highly portable midrange, mdpi old school iPhone one hdpi like (or handed tablet wannabe) tablet xhdpiMonday, May 23, 2011
  33. 33. • It’s not a universal solution! • What happens if I have a extra-large screen with low density???Monday, May 23, 2011
  34. 34. Monday, May 23, 2011
  35. 35. • But it works for real-life devices • Pragmatic Balance between performances and image definition • That’s why “eventually correct”Monday, May 23, 2011
  36. 36. The Real Reason • behind screen compat framework is... • keeping the physical size the same (dpi = dot per inch) • has to do with human size factor ...with the size of the thumb actually :)Monday, May 23, 2011
  37. 37. Real Estate • so I just have to provide image for ldpi mdpi and hdpi and I am set, right? • NOPE • you real estate will change!Monday, May 23, 2011
  38. 38. • 800x480 (240 dpi)= 5/3 but DIPs are scaled • 480x320 (160 dpi) = 3/2 homogeneously! • 320x240 (120 dpi) = 4/3Monday, May 23, 2011
  39. 39. Solutions • Modal Controls: SlidingDrawer, FlipViews, Alerts, Notifications • or simply cut the ancillary content!Monday, May 23, 2011
  40. 40. Some solutionsMonday, May 23, 2011
  41. 41. Some solutions/2Monday, May 23, 2011
  42. 42. ...but don’t do Least Common Denominator :(Monday, May 23, 2011
  43. 43. Design Patterns • Google doesn’t enforce constraints on Application UI Design • No Censorship, No Certification, No Approval! • ....but strongly suggests themMonday, May 23, 2011
  44. 44. Android UI DesignPatternsMonday, May 23, 2011
  45. 45. Twitter App GoogleIO 2010Monday, May 23, 2011
  46. 46. Twitter App Home Action Bar GoogleIO 2010 Dashboard Live Wallpaper Quick ActionsMonday, May 23, 2011
  47. 47. Resources • Home, QuickActions, ActionBar: Cyril Motter’s GreenDroid https://github.com/cyrilmottier/GreenDroid • Dashboard: Roman Nurik’s Layout https://gist.github.com/882650 • Live Wallpapers: SDK Samples: CubeLiveWallpaper http://developer.android.com/resources/articles/live-wallpapers.htmlMonday, May 23, 2011
  48. 48. Designing Android UIs for Phones and Tablets • Pattern consolidated and implemented in platform • Direct support for ActionBar and Home • Extension to tablets • IO Schedule App is again reference implementation - Fragment Compat!!!Monday, May 23, 2011
  49. 49. Monday, May 23, 2011
  50. 50. Monday, May 23, 2011
  51. 51. Home Action Bar DashboardMonday, May 23, 2011
  52. 52. So... • Use the patterns - they are designed for delivering YOUR content • Use the libraries - they provide consistent and compatible implementations • Concentrate on the “painting” instead of the “frame”Monday, May 23, 2011
  53. 53. A real life example: RomaSki • Application for keeping under control webcams from Ski Resorts in the Rome area (I needed it) • Compatibility Fragment Library • Versioned ScaleMonday, May 23, 2011
  54. 54. for PhoneMonday, May 23, 2011
  55. 55. TabletMonday, May 23, 2011
  56. 56. Fragments breakdown 1Monday, May 23, 2011
  57. 57. Fragments breakdown 1IMonday, May 23, 2011
  58. 58. Action Bar (was: “Title”) • title Bar under HoneyComb is so powerful! Also has tabs! is ACTION Bar • ...forget it! Fragments Compability Library doesn’t allow • You have to inherit by compat FragmentActivity, not the HoneyComb one, same classes but di erent pkg!Monday, May 23, 2011
  59. 59. non-HC TabletsMonday, May 23, 2011
  60. 60. More Compatible than Google :) “early” 2.2 tabletMonday, May 23, 2011
  61. 61. Versioned Scale • Android uses GestureDetectors for decoupling • ScaleGestureDetector is in 2.2 and up • 2.0 already had multitouch though: class copypasted from source • 1.6 single finger gesture: custom ScaleGestureDetectorMonday, May 23, 2011
  62. 62. private OnScaleGestureListener mListener; private ScaleGestureDetector internalDelegate; private Canary nativeDelegate; public ScaleGestureDetector(Context ctx, OnScaleGestureListener listener) { mListener = listener; int buildInt = android.os.Build.VERSION.SDK_INT; if (buildInt == android.os.Build.VERSION_CODES.DONUT) { internalDelegate = new ScaleGestureDetector16(ctx, this); } else if (buildInt == android.os.Build.VERSION_CODES.ECLAIR) { internalDelegate = new ScaleGestureDetector21(ctx, this); } else if (buildInt >= android.os.Build.VERSION_CODES.FROYO) { try { Canary.tryNewClass(ctx); initalizeNative(ctx); } catch (Throwable th) { th.printStackTrace(); internalDelegate = new ScaleGestureDetector16(ctx, this); } } else { throw new RuntimeException("Version not supported! " + buildInt); }}Monday, May 23, 2011
  63. 63. GML Diagram (galactic modeling language, Craig Larman)Monday, May 23, 2011
  64. 64. Design API for change • The public interfaces of the Versioned ScaleGestureDetector are same as SDK one • In the future will be easy to switch back to SDK! since • IceCream Sandwich is expected for Q4 2011, with new (unspecified) compatibility features!Monday, May 23, 2011
  65. 65. Monday, May 23, 2011
  66. 66. Resources • GoogleIO Schedule App src: http://code.google.com/p/iosched/ • RomaSki src: https://github.com/hazam/RomaSki https://github.com/hazam/hAndy • Dan Morrill: Android Open Source & Compatibility Tech Lead twitter: @morrildl • HC Design Patterns: https://docs.google.com/present/view? id=ah8kttmgmwgb_21d9rnw7d6Monday, May 23, 2011
  67. 67. What was left out • AndroidManifest filters: <supports_screen> will suprise you, • 2.3 adds <compatible-screens> • Fragments Compatibility Library :/ • Accessory DevKit support for 2.3.4 library :/ • Google TV! (10ft experience, but mostly D-Pad)Monday, May 23, 2011
  68. 68. <LinearLayout xmlns:a="http://schemas.android.com/apk/res/android" a:orientation="vertical" a:layout_width="fill_parent" a:layout_height="fill_parent"> <TextView a:text= "Emanuele Di Saverio" a:layout_width="fill_parent" a:layout_height="wrap_content"/> <TextView a:text= "twitter: @hazam" a:layout_width="fill_parent" a:layout_height="wrap_content"/> <TextView a:text= "email: emanuele.disaverio@gmail.com" a:layout_width="fill_parent" a:layout_height="wrap_content"/> </LinearLayout>Monday, May 23, 2011

×