Pro Android Sayed Y. Hashimi and Satya Komatineni 15967fm.indd 1 6/5/09 11:14:50 AM Pro Android Copyright © 2009 by Sayed Y. Hashimi and Satya Komatineni All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-1596-7 ISBN-13 (electronic): 978-1-4302-1597-4 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Java™ and all Java™-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the United States and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement from Sun Microsystems, Inc. Lead Editor: Steve Anglin Development Editor: Douglas Pundick Technical Reviewer: Vikram Goyal Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Richard Dal Porto Copy Editor: Nina Goldschlager Perry Associate Production Director: Kari Brooks-Copony Production Editor: Candace English Compositor: Patrick Cunningham Proofreader: Lisa Hamilton Indexer: Ron Strauss Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www. apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precau- tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indi- rectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. 15967fm.indd 2 6/5/09 11:14:50 AM v Contents at a Glance About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi CHAPTER 1 Introducing the Android Computing Platform 1 CHAPTER 2 Getting Your Feet Wet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 CHAPTER 3 Using Resources, Content Providers, and Intents 43 CHAPTER 4 Building User Interfaces and Using Controls . . . . . . . . . . . . . . . . . . . . 107 CHAPTER 5 Working with Menus and Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 CHAPTER 6 Unveiling 2D Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 CHAPTER 7 Exploring Security and Location-Based Services 225 CHAPTER 8 Building and Consuming Services 263 CHAPTER 9 Using the Media Framework and Telephony APIs . . . . . . . . . . . . . . . 301 CHAPTER 10 Programming 3D Graphics with OpenGL 325 CHAPTER 11 Managing and Organizing Preferences . . . . . . . . . . . . . . . . . . . . . . . . . 363 CHAPTER 12 Coming to Grips with 1.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 CHAPTER 13 Simplifying OpenGL and Exploring Live Folders . . . . . . . . . . . . . . . . . 395 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 15967fm.indd 5 6/5/09 11:14:50 AM vii Contents About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi CHAPTER 1 Introducing the Android Computing Platform 1 History of Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Delving into the Dalvik VM 4 Comparing Android and Java ME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Understanding the Android Software Stack . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Developing an End-User Application with the Android SDK 9 The Android Emulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 The Android UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 The Android Foundational Components 11 Advanced UI Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Android Service Components 13 Android Media and Telephony Components . . . . . . . . . . . . . . . . . . . . 14 Android Java Packages 15 Taking Advantage of Android Source Code 18 Summary 19 CHAPTER 2 Getting Your Feet Wet 21 Setting Up Your Environment 21 Downloading JDK 6 and Eclipse 3.4 21 Downloading the Android SDK 22 Installing Android Development Tools (ADT) 22 Learning the Fundamental Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Activity 24 Intent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Content Provider 25 Service 25 AndroidManifest.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 15967fm.indd 7 6/5/09 11:14:50 AM ■CONTENTS viii Hello World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Exploring the Structure of an Android Application 28 Analyzing the Notepad Application 30 Loading and Running the Notepad Application 31 Dissecting the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Examining the Application Lifecycle 38 Debugging Your App 41 Summary 42 CHAPTER 3 Using Resources, Content Providers, and Intents . . . . . . . . 43 Understanding Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 String Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Layout Resources 45 Resource-Reference Syntax 47 Defining Your Own Resource IDs for Later Use. . . . . . . . . . . . . . . . . . 48 Compiled and Noncompiled Android Resources 48 Enumerating Key Android Resources 49 Working with Arbitrary XML Resource Files . . . . . . . . . . . . . . . . . . . . 57 Working with Raw Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Working with Assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Reviewing the Resources Directory Structure 60 Understanding Content Providers 60 Exploring Android’s Built-in Providers . . . . . . . . . . . . . . . . . . . . . . . . . 61 Architecture of Content Providers 67 Implementing Content Providers 79 Understanding Intents 91 Available Intents in Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Intents and Data URIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Generic Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Using Extra Information 96 Using Components to Directly Invoke an Activity 97 Best Practice for Component Designers . . . . . . . . . . . . . . . . . . . . . . . 99 Understanding Intent Categories 99 The Rules for Resolving Intents to Their Components 102 Exercising the ACTION_PICK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Exercising the GET_CONTENT Action 104 Summary 106 15967fm.indd 8 6/5/09 11:14:50 AM ■CONTENTS ix CHAPTER 4 Building User Interfaces and Using Controls 107 UI Development in Android 107 Understanding Android’s Common Controls 113 Text Controls 113 Button Controls 117 List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Grid Controls 126 Date and Time Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Other Interesting Controls in Android 130 The MapView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 The Gallery Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Understanding Layout Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 The LinearLayout Layout Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 The TableLayout Layout Manager 134 The RelativeLayout Layout Manager 139 The AbsoluteLayout Layout Manager 141 The FrameLayout Layout Manager 143 Customizing Layout for Various Screen Configurations 145 Understanding Adapters 146 Getting to Know SimpleCursorAdapter 146 Getting to Know ArrayAdapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Creating Custom Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Debugging and Optimizing Layouts with the Hierarchy Viewer 149 Summary 150 CHAPTER 5 Working with Menus and Dialogs 151 Understanding Android Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Creating a Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Responding to Menu Items 154 Creating a Test Harness for Testing Menus . . . . . . . . . . . . . . . . . . . 156 Working with Other Menu Types 163 Expanded Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Working with Icon Menus 163 Working with Submenus 164 Provisioning for System Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Working with Context Menus 165 Working with Alternative Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Working with Menus in Response to Changing Data 172 15967fm.indd 9 6/5/09 11:14:50 AM ■CONTENTS x Loading Menus Through XML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Structure of an XML Menu Resource File . . . . . . . . . . . . . . . . . . . . . 172 Inflating XML Menu Resource Files 173 Responding to XML-Based Menu Items 174 A Brief Introduction to Additional XML Menu Tags 175 Using Dialogs in Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Designing an Alert Dialog 177 Designing a Prompt Dialog 179 Nature of Dialogs in Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Rearchitecting the Prompt Dialog 185 Working with Managed Dialogs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Understanding the Managed-Dialog Protocol 186 Recasting the Nonmanaged Dialog as a Managed Dialog . . . . . . . 186 Simplifying the Managed-Dialog Protocol . . . . . . . . . . . . . . . . . . . . . 188 Summary 196 CHAPTER 6 Unveiling 2D Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Frame-by-Frame Animation 198 Planning for Frame-by-Frame Animation . . . . . . . . . . . . . . . . . . . . . 198 Creating the Activity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Adding Animation to the Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Layout Animation 204 Basic Tweening Animation Types 204 Planning the Layout-Animation Test Harness 205 Creating the Activity and the ListView . . . . . . . . . . . . . . . . . . . . . . . . 206 Animating the ListView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Using Interpolators 212 View Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Understanding View Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Adding Animation 216 Using Camera to Provide Depth Perception in 2D . . . . . . . . . . . . . . 220 Exploring the AnimationListener Class 221 Some Notes on Transformation Matrices . . . . . . . . . . . . . . . . . . . . . 222 Summary 223 CHAPTER 7 Exploring Security and Location-Based Services 225 Understanding the Android Security Model 225 Overview of Security Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Signing Applications for Deployment . . . . . . . . . . . . . . . . . . . . . . . . . 226 15967fm.indd 10 6/5/09 11:14:50 AM ■CONTENTS xi Performing Runtime Security Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Understanding Security at the Process Boundary . . . . . . . . . . . . . . 229 Declaring and Using Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Understanding and Using Custom Permissions . . . . . . . . . . . . . . . . 232 Working with Location-Based Services 238 Understanding the Mapping Package 238 Understanding the Location Package 249 Summary 262 CHAPTER 8 Building and Consuming Services . . . . . . . . . . . . . . . . . . . . . . . . 263 Consuming HTTP Services 263 Using the HttpClient for HTTP GET Requests 264 Using the HttpClient for HTTP POST Requests . . . . . . . . . . . . . . . . . 266 Dealing with Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Addressing Multithreading Issues 272 Doing Interprocess Communication 276 Creating a Simple Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Understanding Services in Android 277 Understanding Local Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 Understanding AIDL Services 282 Defining a Service Interface in AIDL 283 Implementing an AIDL Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Calling the Service from a Client Application . . . . . . . . . . . . . . . . . . 288 Passing Complex Types to Services 292 Summary 300 CHAPTER 9 Using the Media Framework and Telephony APIs . . . . . . . 301 Using the Media APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 Understanding the setDataSource Method . . . . . . . . . . . . . . . . . . . . 305 Playing Video Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Understanding the MediaPlayer Oddities 311 Exploring Audio Recording . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Using the Telephony APIs 316 Working with SMS 316 Working with the Telephony Manager . . . . . . . . . . . . . . . . . . . . . . . . 323 Summary 324 15967fm.indd 11 6/5/09 11:14:50 AM ■CONTENTS xii CHAPTER 10 Programming 3D Graphics with OpenGL . . . . . . . . . . . . . . . . . 325 Understanding OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 OpenGL ES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 OpenGL ES and Java ME 327 M3G: Another Java ME 3D Graphics Standard 328 Using OpenGL ES 328 Essential Drawing with OpenGL ES 329 Understanding the Camera and Coordinates 334 Interfacing OpenGL ES with Android 338 Creating and Using the OpenGL Test Harness . . . . . . . . . . . . . . . . . . . . . . 342 Designing the Test Harness 343 OpenGLTestHarnessActivity.java 345 OpenGLTestHarness.java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 OpenGLDrawingThread.java 348 EglHelper.java 352 Renderer.java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 AbstractRenderer.java 354 SimpleTriangleRenderer.java 355 Changing Camera Settings 358 Using Indices to Add Another Triangle . . . . . . . . . . . . . . . . . . . . . . . 360 Summary 362 CHAPTER 11 Managing and Organizing Preferences 363 Exploring the Preferences Framework 363 Understanding CheckBoxPreference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 Understanding EditTextPreference 370 Understanding RingtonePreference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 Organizing Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Summary 376 15967fm.indd 12 6/5/09 11:14:50 AM ■CONTENTS xiii CHAPTER 12 Coming to Grips with 1.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 Installing the ADT Plug-in for Android 1.5 Development . . . . . . . . . . . . . 377 Getting Started with Android 1.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 Creating an Android Virtual Device 383 Exploring Improvements to the Media Framework . . . . . . . . . . . . . . . . . . 384 Using the MediaRecorder Class for Video Capture 385 Exploring the MediaStore Class 386 Scanning the Media Store for Media Content 390 Exploring Voice Recognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 Introducing the Input-Method Framework 394 Summary 394 CHAPTER 13 Simplifying OpenGL and Exploring Live Folders . . . . . . . . . 395 Simplifying OpenGL 396 Reimplementing the Simple Triangle OpenGL Drawing . . . . . . . . . 398 OpenGL Animation Example 401 Exploring Live Folders 404 How a User Experiences Live Folders 405 Building a Live Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 The Future of Android and the 1.5 SDK 421 Key Online Resources for the 1.5 SDK 423 Summary 424 INDEX 425 15967fm.indd 13 6/5/09 11:14:51 AM [...]... 19 The Android projects also include the “Provider” projects “Provider” projects are like databases in Android that wrap their data into RESTful services These projects are CalendarProvider, ContactsProvider, DownloadProvider, DrmProvider, GoogleContactsProvider, GoogleSubscribedFeedsProvider, ImProvider, MediaProvider, SettingsProvider, SubscribedFeedsProvider, and TelephonyProvider As a programmer,... xmlns :android= "http://schemas .android. com/apk/res /android" > Although Android supports dialogs, all dialogs in Android are asynchronous These asynchronous dialogs present... xmlns :android= "http://schemas .android. com/apk/res /android" android: oneshot="false"> …… The underlying graphics libraries support the standard transformation matrices, allowing scaling, movement, and rotation A Camera object in the graphics library provides... android: versionName="1.0.0"> The Android manifest file is where activities... • android. text: Contains text-processing classes • android. text.method: Provides classes for entering text input for a variety of controls • android. text.style: Provides a number of styling mechanisms for a span of text • android. utils: Contains the classes Log, DebugUtils, TimeUtils, and Xml • android. view: Contains the classes Menu, View, ViewGroup, and a series of listeners and callbacks • android. view.animation:... complete list of Android projects in the Git repository appears at http:/ /android. git.kernel.org/ You can download any of these projects using the tools provided by Git and described at the product’s web site Some of the primary projects include Dalvik, frameworks/base (the android. jar file), Linux kernel, and a number of external libraries such as Apache HTTP libraries (apache-http) The core Android applications... simplicity of the Java programming language coupled with Android s extensive class library makes Android a compelling platform to write programs for Figure 1-2 provides an overview of the Android software stack (We’ll provide further details in the section “Understanding the Android Software Stack.”) User Applications Java Libraries Activities/Services UI/Graphics/Views Resources/Content Providers Telephone/Camera... of development in Android are compelling reasons for Java developers to program for it • Java SE support: Compared to the support for Java SE in CDC, the Android support for Java SE is a bit more complete, except for the AWT and Swing As we mentioned earlier, Android has its own UI approach instead In fact, Android s declarative UI resembles the JavaFX approach Understanding the Android Software Stack... out that XML plays a role in describing the Android UI Look at an example of how XML does this for a simple layout containing a text view: ... platform and not just one project You can see the scope and the number of projects at http://source .android. com/projects The source code of Android and all its projects is managed by the Git source-code control system Git (http://git.or.cz/) is an open source source-control system designed to handle large and small projects with speed and convenience The Linux kernel and Ruby on Rails projects also rely on . coupled with Android s extensive class library makes Android a compelling platform to write programs for. Figure 1-2 provides an overview of the Android software stack. (We’ll provide further. this chapter, we’ll provide an overview of Android and its SDK, show you how to take advantage of Android source code, and highlight the benefits of programming for the Android Platform. The. 60 Understanding Content Providers 60 Exploring Android s Built-in Providers . . . . . . . . . . . . . . . . . . . . . . . . . 61 Architecture of Content Providers 67 Implementing Content Providers 79 Understanding