ebook img

Pro Java ME Apps: Building Commercial Quality Java ME Apps PDF

355 Pages·2011·14.91 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Pro Java ME Apps: Building Commercial Quality Java ME Apps

Pro Java ME Apps ■ ■ ■ Ovidiu Iliescu i Pro Java ME Apps Copyright © 2011 by Ovidiu Iliescu 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- 3327-5 ISBN-13 (electronic): 978-1-4302- 3328-2 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Steve Anglin Development Editor: Douglas Pundick Technical Reviewer: Carol Hamer Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Adam Heath Copy Editor: Mary Ann Fugate, Sharon Wilkey Compositor: MacPS, LLC Indexer: SPi Global Artist: SPi Gobal Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit 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 www.apress.com/bulk-sales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution 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 indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. Mom, Dad, Grandpa – Thank you for everything. I love you. Contents at a Glance Contents .............................................................................................................. v About the Author ................................................................................................ xi About the Technical Reviewer .......................................................................... xii Acknowledgments ........................................................................................... xiii Introduction ...................................................................................................... xiv ■Chapter 1: Getting Started ............................................................................... 1(cid:2) ■Chapter 2: A Java ME Framework ................................................................. 25(cid:2) ■Chapter 3: Defining Our Data ......................................................................... 51(cid:2) ■Chapter 4: The Networking Module ............................................................... 63(cid:2) ■Chapter 5: The Persistence Module ............................................................... 93(cid:2) ■Chapter 6: The UI Module ............................................................................ 115(cid:2) ■Chapter 7: The Localization Module ............................................................ 155(cid:2) ■Chapter 8: Putting It All Together ................................................................ 169(cid:2) ■Chapter 9: Device Fragmentation ................................................................ 193(cid:2) ■Chapter 10: Optimizing Your Code .............................................................. 223(cid:2) ■Chapter 11: Adding Fine Touches and User Interaction Improvements ...... 251(cid:2) ■Chapter 12: Java ME Application Testing .................................................... 271(cid:2) ■Chapter 13: Advanced Java ME Graphics .................................................... 283(cid:2) ■Chapter 14: The Proper Java ME Mindset ................................................... 305(cid:2) ■Chapter 15: Java ME and the Future ........................................................... 325(cid:2) ■Chapter 16: Final Words .............................................................................. 337(cid:2) Index ............................................................................................................... 341 iv Contents Contents at a Glance .......................................................................................... iv(cid:2) About the Author ................................................................................................ xi(cid:2) About the Technical Reviewer .......................................................................... xii(cid:2) Acknowledgments ........................................................................................... xiii(cid:2) Introduction ...................................................................................................... xiv ■Chapter 1: Getting Started ............................................................................... 1(cid:2) (cid:2) Java ME, Smartphones, and Feature Phones ......................................................................................................... 1 (cid:2) Java ME’s Advantages ....................................................................................................................................... 1 (cid:2) Java ME’s Disadvantages .................................................................................................................................. 2 (cid:2) The Bottom Line ................................................................................................................................................. 3 (cid:2) Creating a Java ME Application .............................................................................................................................. 3 (cid:2) The Idea for the Application ............................................................................................................................... 4 (cid:2) Goals, Features, Revenue Sources, and Target Devices .................................................................................... 4 (cid:2) Identifying the Target Devices ........................................................................................................................... 6 (cid:2) Identifying Java ME Technical Limitations ......................................................................................................... 7 (cid:2) Regular Java ME Applications vs. Pro Java ME Applications .......................................................................... 13 (cid:2) Writing Flexible Java ME Applications .................................................................................................................. 15 (cid:2) Coding Defensively .......................................................................................................................................... 16 (cid:2) Avoiding Wrong Assumptions .......................................................................................................................... 18 (cid:2) Learning How to Handle Complexity ................................................................................................................ 18 (cid:2) Trade-Offs Are Your Friends—Use Them Wisely ............................................................................................. 20 (cid:2) Aim for a Loose and Decentralized Architecture ............................................................................................. 22 (cid:2) Never Do On-Device What You Can Do Off-Device .......................................................................................... 23 (cid:2) Summary .............................................................................................................................................................. 24 ■Chapter 2: A Java ME Framework ................................................................. 25(cid:2) (cid:2) The Importance of Using a Framework ................................................................................................................. 25 (cid:2) Why a Custom Framework for Each Application? ................................................................................................. 26 (cid:2) Defining the Framework’s Structure ..................................................................................................................... 27 (cid:2) Basic Object Types ................................................................................................................................................ 28 (cid:2) Events .............................................................................................................................................................. 29 (cid:2) Event Listeners ................................................................................................................................................ 31 v ■ CONTENTS (cid:2) Providers .......................................................................................................................................................... 31 (cid:2) Consumers ....................................................................................................................................................... 32 (cid:2) Managers ......................................................................................................................................................... 33 (cid:2) Models and Controllers .................................................................................................................................... 33 (cid:2) Views ............................................................................................................................................................... 34 (cid:2) Common Objects ................................................................................................................................................... 35 (cid:2) The EventController Class ................................................................................................................................ 35 (cid:2) The EventControllerManager ........................................................................................................................... 37 (cid:2) Core Objects and Classes ..................................................................................................................................... 39 (cid:2) The Application Class ....................................................................................................................................... 39 (cid:2) The EventManagerThreads Class ..................................................................................................................... 40 (cid:2) The Bootstrap Class ......................................................................................................................................... 44 (cid:2) A Simple Test Application ..................................................................................................................................... 45 (cid:2) Summary .............................................................................................................................................................. 49 ■Chapter 3: Defining Our Data ......................................................................... 51(cid:2) (cid:2) Why Implement the Model Interface? ................................................................................................................... 51 (cid:2) Making Data Types Immutable ............................................................................................................................. 52 (cid:2) Defining the Tweet Data Type ............................................................................................................................... 53 (cid:2) Defining the TwitterUser Data Type ...................................................................................................................... 54 (cid:2) Defining the TwitterServer Entity .......................................................................................................................... 55 (cid:2) Defining the UserCredentials Data Type ............................................................................................................... 56 (cid:2) Defining the TweetFilter Data Type ....................................................................................................................... 58 (cid:2) Defining the Timeline Entity .................................................................................................................................. 58 (cid:2) Choosing Intelligent Representations for Your Data ............................................................................................. 59 (cid:2) Summary .............................................................................................................................................................. 61 ■Chapter 4: The Networking Module ............................................................... 63(cid:2) (cid:2) Setting Up and Configuring the Library ................................................................................................................. 63 (cid:2) Working with High-Level Objects ..................................................................................................................... 66 (cid:2) Using Your Own Data Types ............................................................................................................................. 66 (cid:2) Writing Our TwitterServer Implementation ........................................................................................................... 67 (cid:2) Defining the General Structure ........................................................................................................................ 67 (cid:2) Initializing a ServerImplementation Instance ................................................................................................... 68 (cid:2) Providing Login Support ................................................................................................................................... 69 (cid:2) Posting Tweets ................................................................................................................................................ 73 (cid:2) Retrieving Tweets ............................................................................................................................................ 74 (cid:2) The getMyProfile() and getProfileFor() Methods .............................................................................................. 85 (cid:2) Best Practices for Java ME Networking ................................................................................................................ 86 (cid:2) Do Not Reinvent the Wheel .............................................................................................................................. 86 (cid:2) Mobile Internet Is Special ................................................................................................................................ 87 (cid:2) Remember the Limitations of the Devices You’re Targeting ............................................................................ 88 (cid:2) Support Network Throttling and Sleep Mode ................................................................................................... 88 (cid:2) Transmitting Your Data Efficiently ................................................................................................................... 89 (cid:2) Keeping the Networking Code Light ................................................................................................................ 90 (cid:2) Be Independent ................................................................................................................................................ 91 (cid:2) Summary .............................................................................................................................................................. 91 vi ■ CONTENTS ■Chapter 5: The Persistence Module ............................................................... 93(cid:2) (cid:2) Understanding Java ME Persistence Options ....................................................................................................... 93 (cid:2) Designing the Persistence Module ....................................................................................................................... 95 (cid:2) The Persistence Providers ............................................................................................................................... 96 (cid:2) RecordReaders and RecordWriters .................................................................................................................. 97 (cid:2) Serializers and Deserializers ........................................................................................................................... 99 (cid:2) Persistence Helpers ....................................................................................................................................... 100 (cid:2) Implementing the Core Architecture ................................................................................................................... 100 (cid:2) Implementing Serializers and Deserializers .................................................................................................. 101 (cid:2) Implementing RecordReaders and RecordWriters ......................................................................................... 103 (cid:2) Implementing the PersistenceProvider .......................................................................................................... 104 (cid:2) Testing the Code ............................................................................................................................................ 106 (cid:2) Writing Persistence Helpers ................................................................................................................................ 107 (cid:2) Using the Module in a Real-Life Scenario ........................................................................................................... 112 (cid:2) Taking the Module Further .................................................................................................................................. 113 (cid:2) Summary ............................................................................................................................................................ 113 ■Chapter 6: The UI Module ............................................................................ 115(cid:2) (cid:2) Why Create a Custom UI Module? ....................................................................................................................... 116 (cid:2) Understanding the Basics of Creating a UI Module ............................................................................................. 116 (cid:2) Widgets .......................................................................................................................................................... 117 (cid:2) Containers ...................................................................................................................................................... 118 (cid:2) Clipping Rectangles ....................................................................................................................................... 120 (cid:2) Views ............................................................................................................................................................. 124 (cid:2) Themes .......................................................................................................................................................... 124 (cid:2) Handling User Interaction .............................................................................................................................. 126 (cid:2) Implementing Basic Widget Support .................................................................................................................. 127 (cid:2) The BaseWidget Class ................................................................................................................................... 127 (cid:2) BaseContainerWidget and BaseContainerManager ....................................................................................... 130 (cid:2) Implementing Concrete Widgets ......................................................................................................................... 135 (cid:2) The VerticalContainer and HorizontalContainer Classes ................................................................................ 135 (cid:2) The SimpleTextButton Class .......................................................................................................................... 139 (cid:2) The StringItem Class ...................................................................................................................................... 141 (cid:2) InputStringItem .............................................................................................................................................. 143 (cid:2) GameCanvasView .......................................................................................................................................... 146 (cid:2) Testing the UI Module ......................................................................................................................................... 147 (cid:2) Implementing UIs on Touch-Only Devices .......................................................................................................... 149 (cid:2) Final Words About the UI Module ........................................................................................................................ 151 (cid:2) Summary ............................................................................................................................................................ 153 ■Chapter 7: The Localization Module ............................................................ 155(cid:2) (cid:2) Understanding the Features of a Good Localization Module ............................................................................... 155 (cid:2) Understanding Native Java ME Localization ....................................................................................................... 156 (cid:2) Adding Custom Localization Support to a Java ME Application .......................................................................... 157 (cid:2) Processing Localization Files ......................................................................................................................... 159 (cid:2) Loading Localization Data on the Device ....................................................................................................... 161 (cid:2) Testing the Localization Module ......................................................................................................................... 164 (cid:2) Implementing Advanced Localization Features .................................................................................................. 165 (cid:2) Summary ............................................................................................................................................................ 168 vii ■ CONTENTS ■Chapter 8: Putting It All Together ................................................................ 169(cid:2) (cid:2) Jump-Starting the Application ............................................................................................................................ 169 (cid:2) Implementing the FlowController ........................................................................................................................ 170 (cid:2) Implementing the TweetsController .................................................................................................................... 173 (cid:2) Implementing the WelcomeScreenController and the WelcomeForm ................................................................ 175 (cid:2) Implementing the MainForm and MainScreenController .................................................................................... 179 (cid:2) Implementing the SettingsScreenController and the SettingsForm ................................................................... 184 (cid:2) Implementing the EVT Class ............................................................................................................................... 188 (cid:2) Improving the Application ................................................................................................................................... 189 (cid:2) Improving Error Handling ............................................................................................................................... 189 (cid:2) Increasing Functionality ................................................................................................................................. 191 (cid:2) Improving the UI Framework ......................................................................................................................... 191 (cid:2) Summary ............................................................................................................................................................ 192 ■Chapter 9: Device Fragmentation ................................................................ 193(cid:2) (cid:2) Hardware Fragmentation .................................................................................................................................... 194 (cid:2) CPU Power ..................................................................................................................................................... 194 (cid:2) RAM ............................................................................................................................................................... 197 (cid:2) Screen ............................................................................................................................................................ 199 (cid:2) Other Hardware Considerations ..................................................................................................................... 201 (cid:2) Capabilities Fragmentation ................................................................................................................................. 202 (cid:2) API Inconsistencies ............................................................................................................................................. 206 (cid:2) Localized API Inconsistencies ........................................................................................................................ 207 (cid:2) Global API Inconsistencies ............................................................................................................................. 210 (cid:2) Inconsistencies Open for Interpretation ......................................................................................................... 212 (cid:2) Porting Frameworks ........................................................................................................................................... 214 (cid:2) The Preprocessor ........................................................................................................................................... 214 (cid:2) The Device Database ..................................................................................................................................... 216 (cid:2) The Build Engine ............................................................................................................................................ 216 (cid:2) The Abstraction API ........................................................................................................................................ 217 (cid:2) Multi-platform Support .................................................................................................................................. 218 (cid:2) Utility Code and Tools .................................................................................................................................... 218 (cid:2) UI Library ........................................................................................................................................................ 219 (cid:2) Customer Support .......................................................................................................................................... 219 (cid:2) Code License .................................................................................................................................................. 220 (cid:2) Cross-Development and Porting Tools ................................................................................................................ 220 (cid:2) Summary ............................................................................................................................................................ 221 ■Chapter 10: Optimizing Your Code .............................................................. 223(cid:2) (cid:2) A Crash Course in Optimizing Code .................................................................................................................... 224 (cid:2) Code Optimization Techniques ........................................................................................................................... 226 (cid:2) Fast Code Path Switching .............................................................................................................................. 226 (cid:2) Avoid Redundancy ......................................................................................................................................... 227 (cid:2) Use Locality to Your Advantage ..................................................................................................................... 227 (cid:2) Optimize Your Math ....................................................................................................................................... 228 (cid:2) Unroll Loops ................................................................................................................................................... 231 (cid:2) Inline Code ..................................................................................................................................................... 231 (cid:2) Optimize Loop-Related Math ......................................................................................................................... 232 (cid:2) Keep Your Loops Conditional-Free ................................................................................................................. 233 viii ■ CONTENTS (cid:2) Eliminate Special Iterations from Your Loops ................................................................................................ 234 (cid:2) Use Loop Fission ............................................................................................................................................ 235 (cid:2) Avoiding High-Level Language Features ............................................................................................................ 235 (cid:2) Stick to the Basics ......................................................................................................................................... 236 (cid:2) Avoid Unnecessary Object Creation ............................................................................................................... 237 (cid:2) Optimize Memory Access .............................................................................................................................. 240 (cid:2) Algorithm Optimization Techniques .................................................................................................................... 241 (cid:2) Comparing Algorithms ................................................................................................................................... 241 (cid:2) Improving Your Algorithms ............................................................................................................................ 243 (cid:2) Summary ............................................................................................................................................................ 250 ■Chapter 11: Adding Fine Touches and User Interaction Improvements ...... 251(cid:2) (cid:2) Adding Fine Touches to Your Application ........................................................................................................... 251 (cid:2) Understanding Fine Touches ......................................................................................................................... 252 (cid:2) Adding Proper In-Application Help ................................................................................................................. 253 (cid:2) Adding Contextual Information ...................................................................................................................... 255 (cid:2) Adding Proper Feedback ................................................................................................................................ 256 (cid:2) Adding Adaptive Text Capabilities ................................................................................................................. 257 (cid:2) Adding History and Auto-Complete Support .................................................................................................. 259 (cid:2) Adding Intent Detection ................................................................................................................................. 260 (cid:2) Synchronizing Your Data Between Devices ................................................................................................... 261 (cid:2) Improving User Interaction ................................................................................................................................. 262 (cid:2) Eliminating User Confusion ............................................................................................................................ 263 (cid:2) Keeping the Interface Simple ......................................................................................................................... 264 (cid:2) Allowing Customers to Reach You Easily ....................................................................................................... 266 (cid:2) Creating Nonmobile Versions of Your Application ......................................................................................... 267 (cid:2) Deploying Continuous Application Updates ................................................................................................... 267 (cid:2) Adding Skin Support ...................................................................................................................................... 268 (cid:2) Advertising Related Products ......................................................................................................................... 269 (cid:2) Deciding Which Fine Touches and Improvements to Implement ........................................................................ 270 (cid:2) Summary ............................................................................................................................................................ 270 ■Chapter 12: Java ME Application Testing .................................................... 271(cid:2) (cid:2) Gathering Debug Information .............................................................................................................................. 271 (cid:2) Performing Unit Testing ...................................................................................................................................... 273 (cid:2) Solving Common Unit Test–Related Problems ............................................................................................... 274 (cid:2) Gathering Quality Debug Data ........................................................................................................................ 275 (cid:2) Running the Tests in a Desktop Environment ................................................................................................ 276 (cid:2) Performing Visual Debugging ............................................................................................................................. 276 (cid:2) Performing Battery Testing ................................................................................................................................. 277 (cid:2) Testing the Application in a Variety of Scenarios ............................................................................................... 279 (cid:2) Testing Performance Improvement and Optimization Techniques ..................................................................... 280 (cid:2) Summary ............................................................................................................................................................ 281 ■Chapter 13: Advanced Java ME Graphics .................................................... 283(cid:2) (cid:2) Using Pre-rendered Graphics .............................................................................................................................. 284 (cid:2) Using Image Masking ......................................................................................................................................... 287 (cid:2) Using Image Blending Techniques ...................................................................................................................... 289 (cid:2) Rotating Images .................................................................................................................................................. 294 (cid:2) Resizing Images .................................................................................................................................................. 300 ix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.