Designing Apps for the Motorola XOOM


Published on

Presentation from #andevcon by Anna Schaller
Peter van der Linden, Android Technology Evangelists from the Developer Platforms and Services team at Motorola Mobility. More info at

Published in: Technology, Art & Photos
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Designing Apps for the Motorola XOOM

  1. 1. Motorola Mobility Anna Schaller Peter van der Linden Android Technology Evangelists Developer Platforms and Services Coding Practices Designing Apps for the Motorola XOOM
  2. 2. Motorola Mobility @ Andevcon © 2010 Motorola Mobility, Inc. 01 DESIGNING APPS FOR THE MOTOROLA XOOM (session 103 @ 8:30) Anna Schaller 02 CODING FOR ANDROID IN C/C++ and RENDERSCRIPT Peter van der Linden 03 TUESDAY KEYNOTE – “A view from Mototola Mobility” (@ 10:00) Christy Wyatt, VP Software and Services 04 TOP TIPS FOR ANDROID UIs (session 205 @ 11:15 Tues) Suzanne Alexandra 05 YOUR FIRST FLASH APPLICATION FOR ANDROID (session 405 @ 3:45 Tues) Suzanne Alexandra 06 BEYOND ENGLISH: Make Your Android App a Global Success (session 801 @ 3:00 Wed) Eric Cloninger
  3. 3. Introducing the tablet <ul><li>Screen : 10.1” diagonal (xlarge), WXGA (1280 x 800), 160 dpi (mdpi) </li></ul><ul><li>Memory : 32 GB flash storage </li></ul><ul><li>Processor : nVidia Tegra 2 dual core </li></ul><ul><li>Sensors : accelerometer, barometer, gyroscope, compass, ambient light, GPS, no proximity or vibration, 4-way rotation </li></ul><ul><li>Connectivity : 802.11 a/b/g/n, Bluetooth, HDMI </li></ul><ul><li>Camera : 2mp fixed focus front / 5mp auto focus rear </li></ul><ul><li>Media : 720p HD video recording, 1080p HD playback </li></ul><ul><li>External storage : 32 GB SD card post-launch </li></ul><ul><li>Platform : Android 3.0 (Honeycomb) </li></ul><ul><li>App Environments: Adobe AIR 2.6, Android, NDK, (Flash 10.1 coming) </li></ul><ul><li>Lots and lots of software changes…….. </li></ul>
  4. 4. Where did everything go? Status bar Menu Key Home Key Back Button Bonus Button (usually search) Volume Keys (up/down) Power Button
  5. 5. Interacting with the tablet Volume Keys (up/down) Power Button
  6. 6. Interacting (con’t) System Bar Not a menu button
  7. 7. Application UI Design Changes <ul><li>Holographic Theme </li></ul><ul><li>System Bar Dimming </li></ul><ul><li>Action bar </li></ul><ul><li>Fragments </li></ul><ul><li>New widgets </li></ul><ul><li>Updated Notifications </li></ul><ul><li>System Clipboard </li></ul><ul><li>Drag-and-drop </li></ul>Page 7 Presentation Title Version 1.0 02.24.09
  8. 8. Holographic Theme <ul><li>Inherited by all apps built against Android 3.0 </li></ul><ul><ul><li>minSdkVersion or targetSdkVersion = 11 * and * enable hardware acceleration ( android:hardwareAccelerated=“true” ) </li></ul></ul><ul><ul><li>If SDK version <= 9 then your application uses the widget designs from Android 2.3 and does not inherit the holographic theme </li></ul></ul><ul><li>Two versions exist </li></ul><ul><ul><li>Default dark background with white letters @android:style/Theme.Holo </li></ul></ul><ul><ul><li>Inversed holographic white with dark letters @android:style/Theme.Holo.Light </li></ul></ul>
  9. 9. Dimming the System Bar <ul><li>Lights out mode </li></ul><ul><ul><li>Dims the system bar by replacing the icons with holographic “dots” (system bar is not removed) </li></ul></ul><ul><li> View v = findViewById( view_id); </li></ul><ul><li>v.setSystemUiVisibility( View. STATUS_BAR_HIDDEN ); </li></ul>
  10. 10. Action Bar – Simple <ul><li>Replaces traditional title bar at top of app </li></ul><ul><li>Application Icon with title </li></ul><ul><li>Overflow menu </li></ul><ul><ul><li>Creating a menu has not changed </li></ul></ul><ul><ul><li>public boolean onCreateOptionsMenu(Menu menu) {…} </li></ul></ul><ul><ul><li>public boolean onOptionsItemSelected(MenuItem item) { </li></ul></ul><ul><ul><li>case …} </li></ul></ul><ul><ul><li>/res/menu/ menu_definitions.xml </li></ul></ul>
  11. 11. Action Bar – Tabbed with Action Items <ul><li>Application Icon can be made actionable </li></ul><ul><li>Tabbed interface on Action Bar </li></ul><ul><li>Individual action items rather than listed on overflow menu </li></ul><ul><ul><li>With or without text; with or without icon </li></ul></ul><ul><li>See </li></ul>“ Actionable” Icon Tabs Action Items
  12. 12. Going full screen – removing the action bar <ul><li>You can hide the action bar *however* your application loses access to your menu </li></ul><ul><li><activity android:theme= &quot;@android:style/Theme.NoTitleBar&quot; > </li></ul><ul><li>Another way to hide action bar is programmatically then allow user to toggle </li></ul><ul><li>ActionBar bar = getActionBar(); </li></ul><ul><li>if (bar != null) { </li></ul><ul><li>if (bar. isShowing() ) bar. hide() ; </li></ul><ul><li> else bar. show() ; </li></ul><ul><li>} </li></ul>
  13. 13. Menus <ul><li>To support forward compatibility added soft menu button along system bar </li></ul><ul><ul><li>minSdkVersion and targetSdkVersion < 11 </li></ul></ul>
  14. 14. Fragments <ul><li>Allows an application to be divided into subcomponents that can be individually managed and combined in various ways, such as creating a multi-pane UI. </li></ul>
  15. 15. Fragments (con’t) <ul><li>A fragment must always be embedded in an Activity </li></ul>List Fragment Fragment Activity
  16. 16. Fragments (base fragment) <ul><li>Create class that extends Fragment </li></ul><ul><li>Displays content similar to an Activity (images, text, application widgets) </li></ul><ul><li>Can be attached to another fragment or tab </li></ul><ul><li>Can add items to the Activity’s overflow menu (action bar) </li></ul><ul><li>Does not have to be visible (can operate behind the scenes) </li></ul><ul><li>If it is visible will require it’s own layout file </li></ul>(Base) Fragment
  17. 17. Fragments (adding to an activity) XOOM_hc_app /res/layout/main.xml (List) Fragment Activity layout (Content) Fragment with embedded image
  18. 18. Fragments (con’t) <ul><li>How-to information in 3.0 Developer Guide on Fragments </li></ul><ul><li>See 3.0 Reference for APIs </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><ul><li>Displays a list similar to ListActivity </li></ul></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><ul><li>Displays a floating dialog </li></ul></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><ul><li>Interface for interacting with the fragment ( findFragmentById ) </li></ul></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><ul><li>APIs for fragment operations (add, remove, replace, hide, show) </li></ul></ul></ul><ul><li>The Android 3.0 Fragments API blog post </li></ul><ul><ul><li>more detail on Fragments for all releases of Android 1.6 and above </li></ul></ul>
  19. 19. New Views <ul><li>Application Widgets </li></ul><ul><ul><li>PopupMenu ( android.widget.PopupMenu ) </li></ul></ul><ul><ul><ul><li>Menu is attached to a view (button for instance) </li></ul></ul></ul><ul><li>Home Screen Widgets </li></ul><ul><ul><li>StackView ( android.widget.StackView ) </li></ul></ul><ul><ul><ul><li>A view that displays its children in a 3D stack and allows users to discretely swipe through the children. </li></ul></ul></ul><ul><li>Rich Notifications ( ) </li></ul><ul><ul><li>build notification layouts </li></ul></ul>
  20. 20. Working example <ul><li>Honeycomb Gallery in SDK samples showcases….. </li></ul><ul><ul><li>Action Bar with tabbed interface, action item menu, and overflow menu </li></ul></ul><ul><ul><li>FragmentList and fragment in portrait and landscape </li></ul></ul><ul><ul><li>New theme </li></ul></ul><ul><ul><li>Drag and drop </li></ul></ul><ul><ul><li>Animation </li></ul></ul><ul><ul><li>Hide/Show </li></ul></ul><ul><ul><li>Action Bar </li></ul></ul><ul><ul><li>Lights Out </li></ul></ul><ul><ul><li>Mode </li></ul></ul>
  21. 21. Graphics and Multimedia <ul><li>Hardware acceleration for 2D Graphics </li></ul><ul><li>New animation framework </li></ul><ul><li>Texture compression </li></ul><ul><li>Sensors </li></ul><ul><li>Media Support </li></ul>Presentation Title Version 1.0 02.24.09
  22. 22. Hardware Acceleration <ul><li>Built in support for acceleration on a subset of 2D graphics (Canvas API) </li></ul><ul><li>Globally for entire application or individual activity by setting appropriate node in manifest </li></ul><ul><ul><li>android:hardwareAccelerated=“true” </li></ul></ul><ul><li>Locally on each activity and/or window programmatically through WindowManager.LayoutParams </li></ul><ul><li> Window win = getWindow(); </li></ul><ul><li> win.setFlags </li></ul><ul><li> (WindowManager.LayoutParams. FLAG_HARDWARE_ACCELERATED, </li></ul><ul><li>WindowManager.LayoutParams. FLAG_HARDWARE_ACCELERATED ); </li></ul><ul><li>Test to see if canvas is hardware accelerated with isHardwareAcclerated() </li></ul>
  23. 23. Animation <ul><li>Two animation systems you can choose from: </li></ul><ul><li>View animations ( android.view.animation ) </li></ul><ul><ul><li>Tween animations </li></ul></ul><ul><ul><ul><li>Performs a series of simple transformations on the contents of a View object. </li></ul></ul></ul><ul><ul><ul><li>Basic transitions include rotating, fading, moving, and stretching on a graphic. </li></ul></ul></ul><ul><ul><li>Frame animations </li></ul></ul><ul><ul><ul><li>Frame-by-frame animation lets you load a series of Drawable resources one after another to create an animation. </li></ul></ul></ul><ul><li>Property animations ( android.animation // new in honeycomb) </li></ul><ul><ul><li>Can animate almost anything (not just views such as drawables!) </li></ul></ul><ul><ul><li>Extends the set of properties that can be animated (group and play) </li></ul></ul><ul><ul><li>View animations are simpler & quicker, property animations do more </li></ul></ul><ul><ul><li>See </li></ul></ul>
  24. 24. Texture Compression <ul><li>Different chipsets support different compression types </li></ul><ul><li>Formats for mobile: </li></ul><ul><ul><li>ETC1 supported on all Android devices with OpenGL ES 2.0 but no alpha channel </li></ul></ul><ul><ul><li>PVRTC (PowerVR SGX 530 GPU from Imagination Technologies) </li></ul></ul><ul><ul><li>ATITC (Adreno GPU from Qualcomm) </li></ul></ul><ul><ul><li>S3TC or DXT1, 3, 5 (nVidia GPU) </li></ul></ul><ul><li>Solution: Texture compression filtering </li></ul><ul><ul><li><supports-gl-textures android:name=“string”> </li></ul></ul><ul><ul><li>where string is the OpenGL ES extension for a compression type </li></ul></ul><ul><ul><li>for instance android:name=“GL_EXT_texture_compression_s3tc” </li></ul></ul>
  25. 25. Texture Compression (con’t) <ul><li>Works with every API level, not just Honeycomb </li></ul><ul><li>Need to define one tag for every format supported </li></ul><ul><li>List of supported compression types in Android Dev Guide - Android Manifest tags </li></ul><ul><li>See MotoDev Technical Article for tools </li></ul>
  26. 26. Sensors <ul><li>Accelerometer, Gyroscope, Magnetometer, Barometer, Compass, Ambient Light, GPS, no proximity sensor, no vibration </li></ul><ul><li>Device Orientation </li></ul><ul><ul><li>Natural orientation is landscape with 4-way rotation </li></ul></ul>-y e +y es +x -x +z +y e +y e -x +x -y e -y e +x -x Rotation 0 Rotation 0 Rotation 90
  27. 27. Sensors (con’t) <ul><li>Use android.view.Display.getRotation() to get current direction </li></ul><ul><li>Adjust x/y values for all four rotation types </li></ul><ul><ul><li>ROTATION_0 </li></ul></ul><ul><ul><li>ROTATION_90 </li></ul></ul><ul><ul><li>ROTATION_180 </li></ul></ul><ul><ul><li>ROTATION_270 </li></ul></ul><ul><li>See AccelerometerPlay sample in SDK </li></ul><ul><li>More detail in Android blog post One Screen Turn Deserves Another </li></ul>
  28. 28. Sensors -- barometer sensor reading Page Version 1.0 © 2010 Motorola Mobility, Inc. // // lower by ~ 1.2 gm /cm2 for each 10 meter gain in elevation
  29. 29. Media Support -- Getting content on the device <ul><li>Digital media file transfer </li></ul><ul><ul><li>Built in support for Media/Picture Transport Protocol (MTP/PTP). </li></ul></ul><ul><ul><li>User plugs USB cable in and the Android File Transfer application launches. </li></ul></ul><ul><ul><li>Users can copy pictures, movies, videos, ringtones, podcasts, and more to predefined folders, or create new folders for other content. </li></ul></ul>
  30. 30. 10 things you can do right now <ul><li>Download the Honeycomb SDK (and updated tools) and test drive your app on the emulator. </li></ul><ul><ul><li>Note if you have the preview SDK replace it with the updated public SDK. </li></ul></ul><ul><li>Change the targetSdkVersion to “ 11 ” and see what changes. </li></ul><ul><li>Add /res/layout-xlarge and /res/layout-xlarge-port folders to your project with layouts optimized for the larger screen. </li></ul><ul><li>Update error handling to exit gracefully rather than defaulting to ANRs or letting the user hang. </li></ul>
  31. 31. <ul><li>Revisit your graphics in /res/drawable-< density > . </li></ul><ul><ul><li>Put background into ImageView and set scaleType=centerCrop </li></ul></ul><ul><ul><li>Patterned images can be tiled </li></ul></ul><ul><ul><li>Read into createScaledBitmap() using the dimensions from DisplayMetrics </li></ul></ul><ul><ul><li>Use the 9-patch tool to create a “stretchable” region </li></ul></ul>10 things you can do right now
  32. 32. 10 things you can do right now <ul><li>Determine if you are going with one code base or two. </li></ul><ul><li>Build your app against Android 3.0 and install on the emulator. </li></ul><ul><li>Begin testing the features of the Action Bar. </li></ul><ul><li>Add a fragment to your activity. </li></ul><ul><li>Download the Honeycomb SDK (and updated tools) and test drive your app on the emulator. </li></ul>
  33. 33. <ul><li>CODING FOR ANDROID IN C/C++ </li></ul><ul><li>01 INSTALLING THE TOOLS </li></ul><ul><li>02 WRITE YOUR C CODE </li></ul><ul><li>03 BUILD YOUR C CODE </li></ul><ul><li>04 CALLING C FROM JAVA (the JNI) </li></ul><ul><li>05 SUMMARY - Wrap up, Q & A </li></ul>© 2011 Motorola Mobility, Inc.
  34. 34. Why C/C++ Code on Android? <ul><li>Open GL </li></ul><ul><li>Performance </li></ul><ul><li>Porting! </li></ul>
  35. 35. NDK C++ limitations <ul><li>Originally for writing performance-critical parts of code in C </li></ul><ul><li>You don’t need OOP abstraction for that. </li></ul><ul><li>(And Dalvik VM is pretty fast, with JIT compiling since 2.2) </li></ul><ul><li>C++ support is somewhat restricted </li></ul><ul><li>exceptions are not supported </li></ul><ul><li>known bugs in static constructor/destructor invocations. </li></ul><ul><li>So NDK code is more likely to be written in C rather than C++ . </li></ul>
  36. 36. Supported CPU architectures <ul><li>NDK supports two ARM instruction sets: </li></ul><ul><li>ARMv5TE (includes Thumb-1 instructions). This is the default target, and code compiled for this will run on all ARM-based Android devices. Choose other target, by changing one line in the makefile. </li></ul><ul><li>ARMv7-A (includes Thumb-2 and VFPv3-D16 instructions, with optional support for NEON/VFPv3-D32 instructions). Code compiled for ARMv7-A will run only on devices such as the Verizon Droid or Motorola XOOM that have a v7 CPU. Code for NEON will only run on an ARMv7 with support for NEON. </li></ul><ul><li>There’s a third architecture “in the works” (experimental, no support) </li></ul><ul><li>x86 instruction set </li></ul>
  37. 37. Installing the Tools
  38. 38. Downloading the Native Development Kit <ul><li>Check compatibility: XP or Vista, MacOS X 10.5, Ubuntu </li></ul><ul><li>Download the 50 MB zip or tar.bz2 file from: </li></ul><ul><li> </li></ul><ul><li>Uncompress it anywhere; remember where in $NDK </li></ul><ul><li>export NDK=~/android-ndk-r5b </li></ul><ul><li>Requires GNU make and GNU awk! Windows users – install Cygwin 1.7 or later </li></ul>
  39. 39. Windows users <ul><li>Download and install Cygwin 1.7 or later </li></ul><ul><li>Unix/Linux command line tools for Windows users </li></ul><ul><li> </li></ul>
  40. 40. Windows and MacOS X tips <ul><li>Don’t use pathnames with spaces in them! Anywhere! </li></ul><ul><li>Cygwin only installs the base packages </li></ul><ul><li>You need the development packages. So install the entire Devel branch. </li></ul><ul><li>Run setup.exe </li></ul><ul><li>Click the word “Default” next to the root Devel node </li></ul><ul><li>“ Default” changes to “Install” for the Devel node </li></ul><ul><li>Click “next” to install. Go to lunch while it installs </li></ul><ul><li>Let it create a cygwin console icon on your desktop </li></ul><ul><li>Type “make –v” in the cygwin console to check it’s OK. </li></ul>
  41. 41. Unpack the Native Development Kit <ul><li>Gives you 7 folders, and a bunch of things to read </li></ul>
  42. 42. NDK folders <ul><li>build – 4 folders of awk & shell scripts, makefiles to build your C/C++ files </li></ul>
  43. 43. Add C/C++ support into Eclipse <ul><li>Eclipse has a plugin for C/C++, just like for Android </li></ul><ul><li>Get it using Eclipse </li></ul><ul><li>Help > Install new software </li></ul><ul><li>Type </li></ul><ul><li>in “work with” field (or whatever release you use) </li></ul><ul><li>Usual caveats about network proxies at work (set them under MOTODEV Studio > Preferences > Network Conn.) </li></ul><ul><li>Click Programming Langs > C/C++ Development Tools </li></ul>
  44. 44. <ul><li>There’s a plugin for C/C++, just like for Android </li></ul><ul><li>Get it using Eclipse </li></ul><ul><li>Help > Install new software </li></ul>
  45. 45. Write your C code
  46. 46. General Approach <ul><li>You start with a Java Android app, and add NDK pieces to it. </li></ul><ul><li>Put your native C/C++ sources in $PROJECT/jni: </li></ul><ul><li>Build your native code into libraries </li></ul><ul><li>Your Android Java code can call into these native libraries </li></ul><ul><li>Native code can also call back into Java, and access Java objects </li></ul>
  47. 48. Create the jni folder for your project <ul><li>Highlight the Eclipse project that will use native code </li></ul><ul><li>Your package name is used to access the native code, so keep it short and simple. e.g. “com.greet” for training purposes </li></ul><ul><li>File > New > Folder give it the name “jni” </li></ul><ul><li>Inside that newly-created jni folder, we will add </li></ul><ul><li>a makefile </li></ul><ul><li>some C files </li></ul>
  48. 49. Populate the jni folder in your project <ul><li>File > New > Other … </li></ul><ul><li>Select “General” then “file” </li></ul><ul><li>Choose the jni folder, with a filename of “” > Finish </li></ul><ul><li>Repeat, creating a file “myhello.c”, also in the jni folder </li></ul>
  49. 50. greet
  50. 52. Populate the jni folder in your project - mkfile <ul><li>Edit file jni/ that you just created </li></ul><ul><li>Put these magic words into the makefile </li></ul>LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # our source file and the library built from it LOCAL_MODULE := hello LOCAL_SRC_FILES := hello.c include $(BUILD_SHARED_LIBRARY)
  51. 53. Diversion about make <ul><li>Make was created by Stu Feldman in 1977 at Bell Labs </li></ul><ul><li>In 2003, Dr. Feldman received the ACM Software System Award for authoring this widespread tool. </li></ul><ul><li>You didn’t hear it from me, but software lore has it that … </li></ul># Each command that follows dependency line , # i.e. the TARGET to the left, must be indented by a TAB character. target: component-to-build <TAB>commands-to-build-it
  52. 54. Populate the jni folder in your project – C code <ul><li>Edit file jni/myhello.c that you just created </li></ul><ul><li>Put these magic words into the C file </li></ul><ul><li>Uses the JNIEnv object to create a Java string from a literal </li></ul>#include <string.h> #include <jni.h>   jstring Java_com_greet_MyActivity_myCFunction (JNIEnv* env, jobject javaThis) {    return (*env)->NewStringUTF(env, &quot;Hello from native code!&quot;); }
  53. 55. C code explained <ul><li>When the JVM invokes a native function, it passes </li></ul><ul><li>a JNIEnv pointer, (structure that has the interface to JVM for C) </li></ul><ul><li>a jobject pointer, (the “this” object) </li></ul><ul><li>any Java arguments declared by the Java method. </li></ul><ul><li>These types come from #include <jni.h> (look under “platforms”) </li></ul><ul><li>Your C method must be written to expect these args </li></ul><ul><li>The C method is named “Java” _ classname _ activity_methodname </li></ul><ul><li>… and did you notice the memory leak? </li></ul>#include <string.h> #include <jni.h>   jstring Java_com_greet_MyActivity_myCFunction (JNIEnv* env, jobject javaThis) {    return (*env)->NewStringUTF(env, &quot;Hello from native code!&quot;); }
  54. 56. Build your C code
  55. 57. Calling Between Languages <ul><li>Android follows the standard way to talk between Java and C </li></ul><ul><li>JNI – Java Native Interface </li></ul><ul><li>Link to JNI docs </li></ul><ul><li>That includes a complete book, a tutorial, examples etc. </li></ul>
  56. 58. Build your C library <ul><li>Bring up a terminal window, and cd into your project jni folder </li></ul><ul><li>Use “ndk-build” script from the NDK directory (just a wrapper round GNU Make) (can put $NDK in your path, if you want) </li></ul><ul><li>The script compiles your C code, makes it into a shared library </li></ul><ul><li>Moves the into a new project folder libs/armeabi </li></ul>
  57. 59. General approach for building a library <ul><li>Save files in Eclipse </li></ul><ul><li>Bring up a terminal window, and cd into your project jni folder </li></ul><ul><li>Run the ndk-build script to turn your C code into a .so </li></ul><ul><li>That lib*.so will be added into the app’s .apk file </li></ul><ul><li>File > Refresh in Eclipse (to sync with filesystem) </li></ul>$ cd MYECLIPSEPROJECT /jni $ $NDK/ndk-build Compile thumb : hello <= hello.c SharedLibrary : Install : => libs/armeabi/libhello.s
  58. 60. Build Your Library - Summary <ul><li>Eclipse doesn’t build your NDK code, GNU Make does </li></ul><ul><li>The C/C++ plugin gives you syntax colors and editing (only) </li></ul><ul><li>Run the “ndk-build” script after each edit of your C code </li></ul><ul><li>Then click File > Refresh, (F5), to keep Eclipse in sync with your native files. </li></ul>
  59. 61. The Java end of JNI
  60. 62. Java code that calls to the native code <ul><li>Start with the basic app that Eclipse gives you </li></ul>package com.greet; import; import android.os.Bundle; public class MyActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
  61. 63. Add the library load <ul><li>Add the call to load our native library </li></ul><ul><li>Name must match library name without “lib” and “.so” </li></ul><ul><li>So is “hello” </li></ul>static { System.loadLibrary(”hello&quot;); }
  62. 64. Add the declaration of the native function <ul><li>Add the prototype declaration of the C function </li></ul><ul><li>On the C side, it has two explicit arguments, an envp, a this </li></ul><ul><li>But on the Java side, those arguments are implicit </li></ul>private native String myCFunction();
  63. 65. Add the call to the native function <ul><li>We’re going to get the string from C, and toast it. </li></ul>import static android.widget.Toast.*; import android.widget.Toast; . . . String msg = myCFunction(); Toast.makeText(this, msg, LENGTH_LONG).show();
  64. 66. Putting it all together package com.greet; import; import android.os.Bundle; import static android.widget.Toast.*; import android.widget.Toast; public class MyActivity extends Activity { static { System.loadLibrary(&quot;hello&quot;); } private native String myCFunction(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String msg = myCFunction(); Toast.makeText(this, msg, LENGTH_LONG).show(); } }
  65. 68. Summary
  66. 69. The Beginning <ul><li>This may be a beautiful beginning to a long road </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>
  67. 70. Page Version 1.0 © 2010 Motorola Mobility, Inc. Renderscript <ul><ul><li>New, high performance 3D graphics API </li></ul></ul><ul><ul><li>Was used in Froyo for Live Wallpaper </li></ul></ul><ul><ul><li>Is used in Honeycomb for Books and YouTube app </li></ul></ul><ul><ul><li>Honeycomb introduces Renderscript as a public API </li></ul></ul><ul><ul><li>For performance critical code where the traditional Open GL ES framework APIs are not fast enough! </li></ul></ul>
  68. 71. Page Version 1.0 © 2010 Motorola Mobility, Inc. YouTube and Renderscript
  69. 72. Page Version 1.0 © 2010 Motorola Mobility, Inc. Books and Renderscript
  70. 73. Page Version 1.0 © 2010 Motorola Mobility, Inc. Renderscript API <ul><ul><li>A “computing” API for locating moving points in 3D </li></ul></ul><ul><ul><li>A “rendering” API for drawing textures over the points </li></ul></ul><ul><ul><li>A C-based scripting language to access this API </li></ul></ul><ul><ul><li>Only goal: squeeze last ounce of graphics performance! </li></ul></ul><ul><ul><li>Approach: API is a thin layer over features that are hardware-supported </li></ul></ul><ul><ul><li> </li></ul></ul>
  71. 74. Page Renderscript API <ul><ul><li>Android build tools compile your Renderscript .rs file to intermediate bytecode </li></ul></ul><ul><ul><li>and package it inside your application's .apk file </li></ul></ul><ul><ul><li>On the device, the bytecode is compiled (just-in-time) to machine code that is optimized for the exact device that it is running on </li></ul></ul><ul><ul><li>Machine code is cached for future use </li></ul></ul><ul><ul><li>This eliminates the need to target a specific architecture during the development process. Excellent! </li></ul></ul>
  72. 75. Page Version 1.0 Example .rs file (from Honeycomb samples) <ul><ul><li>#pragma rs java_package_name( </li></ul></ul><ul><ul><li>#include &quot;rs_graphics.rsh” // header with graphics API’s </li></ul></ul><ul><ul><li>// gTouchX and gTouchY are variables that will be reflected for use </li></ul></ul><ul><ul><li>// by the java API. We can use them to notify the script of touch events. </li></ul></ul><ul><ul><li>int gTouchX; </li></ul></ul><ul><ul><li>int gTouchY; </li></ul></ul><ul><ul><li>// This is invoked automatically when the script is created </li></ul></ul><ul><ul><li>void init() { </li></ul></ul><ul><ul><li>gTouchX = 50.0f; </li></ul></ul><ul><ul><li>gTouchY = 50.0f; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>int root(int launchID) { </li></ul></ul><ul><ul><li>// Clear the background color </li></ul></ul><ul><ul><li>rsgClearColor(0.0f, 0.0f, 0.0f, 0.0f); </li></ul></ul><ul><ul><li>// Tell the runtime what the font color should be </li></ul></ul><ul><ul><li>rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f); </li></ul></ul><ul><ul><li>// Introduce ourselves to the world by drawing a greeting </li></ul></ul><ul><ul><li>// at the position user touched on the screen </li></ul></ul><ul><ul><li>rsgDrawText(&quot;Hello World!&quot;, gTouchX, gTouchY); </li></ul></ul><ul><ul><li>// Return value tells RS roughly how often to redraw </li></ul></ul><ul><ul><li>// in this case 20 ms </li></ul></ul><ul><ul><li>return 20; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  73. 76. Q & A
  74. 77. GET STARTED NOW! <ul><li>Test Your Apps on the MOTODEV App Validator at appvalidator </li></ul><ul><li>Join the MOTODEV Community: Ask questions, read our blogs, and network at joinmotodev </li></ul><ul><li>Join our live chat with technical experts during the MOTODEV Office Hours March 16 2pm PST - Details at officehours </li></ul>
  75. 78. thank you © 2010 Motorola Mobility, Inc.
  76. 79. LEGAL <ul><li>LICENSE NOTICES </li></ul><ul><li>Except where noted, sample source code written by Motorola Mobility Inc. and provided to you is licensed as described below. </li></ul><ul><li>Copyright © 2010-2011, Motorola, Inc. All rights reserved except as otherwise explicitly indicated. </li></ul><ul><li>Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: </li></ul><ul><li>Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. </li></ul><ul><li>Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. </li></ul><ul><li>Neither the name of the Motorola, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. </li></ul><ul><li>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. </li></ul><ul><li>  </li></ul><ul><li>Other source code displayed in this presentation may be offered under other licenses. 
 </li></ul><ul><li>Apache 2.0 </li></ul><ul><li>Copyright © 2010, Android Open Source Project. All rights reserved unless otherwise explicitly indicated. </li></ul><ul><li>Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); you may not use this file except in compliance with the License. You may obtain a copy of the License at . </li></ul><ul><li>Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. </li></ul><ul><li>Creative Commons 3.0 Attribution License </li></ul><ul><li>Portions of this presentation are reproduced from work created and shared by Google ( and used according to terms described in the Creative Commons 3.0 Attribution License ( </li></ul>