Tải bản đầy đủ

Pro Java ME Apps pot

www.it-ebooks.info
i
Pro Java ME Apps






■ ■ ■
Ovidiu Iliescu


www.it-ebooks.info
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
orders-ny@springer-sbm.com, or visit www.springeronline.com.
For information on translations, please e-mail rights@apress.com, 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.
www.it-ebooks.info
Mom, Dad, Grandpa – Thank you for everything. I love you.


www.it-ebooks.info

iv

Contents at a Glance
Contents v
About the Author xi
About the Technical Reviewer xii
Acknowledgments xiii
Introduction xiv

Chapter 1: Getting Started 1

Chapter 2: A Java ME Framework 25

Chapter 3: Defining Our Data 51

Chapter 4: The Networking Module 63

Chapter 5: The Persistence Module 93

Chapter 6: The UI Module 115

Chapter 7: The Localization Module 155

Chapter 8: Putting It All Together 169

Chapter 9: Device Fragmentation 193

Chapter 10: Optimizing Your Code 223

Chapter 11: Adding Fine Touches and User Interaction Improvements 251

Chapter 12: Java ME Application Testing 271

Chapter 13: Advanced Java ME Graphics 283

Chapter 14: The Proper Java ME Mindset 305

Chapter 15: Java ME and the Future 325

Chapter 16: Final Words 337
Index 341
www.it-ebooks.info

v

Contents
Contents at a Glance iv
About the Author xi
About the Technical Reviewer xii
Acknowledgments xiii
Introduction xiv


Chapter 1: Getting Started 1
Java ME, Smartphones, and Feature Phones 1

Java ME’s Advantages 1

Java ME’s Disadvantages 2

The Bottom Line 3

Creating a Java ME Application 3

The Idea for the Application 4

Goals, Features, Revenue Sources, and Target Devices 4

Identifying the Target Devices 6

Identifying Java ME Technical Limitations 7

Regular Java ME Applications vs. Pro Java ME Applications 13

Writing Flexible Java ME Applications 15

Coding Defensively 16

Avoiding Wrong Assumptions 18

Learning How to Handle Complexity 18

Trade-Offs Are Your Friends—Use Them Wisely 20

Aim for a Loose and Decentralized Architecture 22

Never Do On-Device What You Can Do Off-Device 23

Summary 24


Chapter 2: A Java ME Framework 25
The Importance of Using a Framework 25

Why a Custom Framework for Each Application? 26

Defining the Framework’s Structure 27

Basic Object Types 28

Events 29

Event Listeners 31

www.it-ebooks.info
■ CONTENTS
vi
Providers 31

Consumers 32

Managers 33

Models and Controllers 33

Views 34

Common Objects 35

The EventController Class 35

The EventControllerManager 37

Core Objects and Classes 39

The Application Class 39

The EventManagerThreads Class 40

The Bootstrap Class 44

A Simple Test Application 45

Summary 49


Chapter 3: Defining Our Data 51
Why Implement the Model Interface? 51

Making Data Types Immutable 52

Defining the Tweet Data Type 53

Defining the TwitterUser Data Type 54

Defining the TwitterServer Entity 55

Defining the UserCredentials Data Type 56

Defining the TweetFilter Data Type 58

Defining the Timeline Entity 58

Choosing Intelligent Representations for Your Data 59

Summary 61


Chapter 4: The Networking Module 63
Setting Up and Configuring the Library 63

Working with High-Level Objects 66

Using Your Own Data Types 66

Writing Our TwitterServer Implementation 67

Defining the General Structure 67

Initializing a ServerImplementation Instance 68

Providing Login Support 69

Posting Tweets 73

Retrieving Tweets 74

The getMyProfile() and getProfileFor() Methods 85

Best Practices for Java ME Networking 86

Do Not Reinvent the Wheel 86

Mobile Internet Is Special 87

Remember the Limitations of the Devices You’re Targeting 88

Support Network Throttling and Sleep Mode 88

Transmitting Your Data Efficiently 89

Keeping the Networking Code Light 90

Be Independent 91

Summary 91

www.it-ebooks.info
■ CONTENTS
vii

Chapter 5: The Persistence Module 93
Understanding Java ME Persistence Options 93

Designing the Persistence Module 95

The Persistence Providers 96

RecordReaders and RecordWriters 97

Serializers and Deserializers 99

Persistence Helpers 100

Implementing the Core Architecture 100

Implementing Serializers and Deserializers 101

Implementing RecordReaders and RecordWriters 103

Implementing the PersistenceProvider 104

Testing the Code 106

Writing Persistence Helpers 107

Using the Module in a Real-Life Scenario 112

Taking the Module Further 113

Summary 113


Chapter 6: The UI Module 115
Why Create a Custom UI Module? 116

Understanding the Basics of Creating a UI Module 116

Widgets 117

Containers 118

Clipping Rectangles 120

Views 124

Themes 124

Handling User Interaction 126

Implementing Basic Widget Support 127

The BaseWidget Class 127

BaseContainerWidget and BaseContainerManager 130

Implementing Concrete Widgets 135

The VerticalContainer and HorizontalContainer Classes 135

The SimpleTextButton Class 139

The StringItem Class 141

InputStringItem 143

GameCanvasView 146

Testing the UI Module 147

Implementing UIs on Touch-Only Devices 149

Final Words About the UI Module 151

Summary 153


Chapter 7: The Localization Module 155
Understanding the Features of a Good Localization Module 155

Understanding Native Java ME Localization 156

Adding Custom Localization Support to a Java ME Application 157

Processing Localization Files 159

Loading Localization Data on the Device 161

Testing the Localization Module 164

Implementing Advanced Localization Features 165

Summary 168

www.it-ebooks.info
■ CONTENTS
viii

Chapter 8: Putting It All Together 169
Jump-Starting the Application 169

Implementing the FlowController 170

Implementing the TweetsController 173

Implementing the WelcomeScreenController and the WelcomeForm 175

Implementing the MainForm and MainScreenController 179

Implementing the SettingsScreenController and the SettingsForm 184

Implementing the EVT Class 188

Improving the Application 189

Improving Error Handling 189

Increasing Functionality 191

Improving the UI Framework 191

Summary 192


Chapter 9: Device Fragmentation 193
Hardware Fragmentation 194

CPU Power 194

RAM 197

Screen 199

Other Hardware Considerations 201

Capabilities Fragmentation 202

API Inconsistencies 206

Localized API Inconsistencies 207

Global API Inconsistencies 210

Inconsistencies Open for Interpretation 212

Porting Frameworks 214

The Preprocessor 214

The Device Database 216

The Build Engine 216

The Abstraction API 217

Multi-platform Support 218

Utility Code and Tools 218

UI Library 219

Customer Support 219

Code License 220

Cross-Development and Porting Tools 220

Summary 221


Chapter 10: Optimizing Your Code 223
A Crash Course in Optimizing Code 224

Code Optimization Techniques 226

Fast Code Path Switching 226

Avoid Redundancy 227

Use Locality to Your Advantage 227

Optimize Your Math 228

Unroll Loops 231

Inline Code 231

Optimize Loop-Related Math 232

Keep Your Loops Conditional-Free 233

www.it-ebooks.info
■ CONTENTS
ix
Eliminate Special Iterations from Your Loops 234

Use Loop Fission 235

Avoiding High-Level Language Features 235

Stick to the Basics 236

Avoid Unnecessary Object Creation 237

Optimize Memory Access 240

Algorithm Optimization Techniques 241

Comparing Algorithms 241

Improving Your Algorithms 243

Summary 250


Chapter 11: Adding Fine Touches and User Interaction Improvements 251
Adding Fine Touches to Your Application 251

Understanding Fine Touches 252

Adding Proper In-Application Help 253

Adding Contextual Information 255

Adding Proper Feedback 256

Adding Adaptive Text Capabilities 257

Adding History and Auto-Complete Support 259

Adding Intent Detection 260

Synchronizing Your Data Between Devices 261

Improving User Interaction 262

Eliminating User Confusion 263

Keeping the Interface Simple 264

Allowing Customers to Reach You Easily 266

Creating Nonmobile Versions of Your Application 267

Deploying Continuous Application Updates 267

Adding Skin Support 268

Advertising Related Products 269

Deciding Which Fine Touches and Improvements to Implement 270

Summary 270


Chapter 12: Java ME Application Testing 271
Gathering Debug Information 271

Performing Unit Testing 273

Solving Common Unit Test–Related Problems 274

Gathering Quality Debug Data 275

Running the Tests in a Desktop Environment 276

Performing Visual Debugging 276

Performing Battery Testing 277

Testing the Application in a Variety of Scenarios 279

Testing Performance Improvement and Optimization Techniques 280

Summary 281


Chapter 13: Advanced Java ME Graphics 283
Using Pre-rendered Graphics 284

Using Image Masking 287

Using Image Blending Techniques 289

Rotating Images 294

Resizing Images 300

www.it-ebooks.info
■ CONTENTS
x
Implementing Other Graphical Effects 301

Combining Multiple Effects Together 302

Summary 303


Chapter 14: The Proper Java ME Mindset 305
Java ME Is Only As Powerful As the Devices It Runs On 306

Best Practices for Optimizing Your Application 309

Sticking to Your Priorities 311

Thinking Outside the Box Is Important 314

Keeping Things Simple 315

Standardizing the User Experience 317

Planning for the Worst-Case Scenario 320

Determining Your Application’s Limits 320

Summary 323


Chapter 15: Java ME and the Future 325
Java ME Hardware Evolution 325

The Evolution of the Java ME API 327

The Evolution of the Java ME Mindset and Development Philosophy 328

Java ME’s Target Market 329

Java ME and Other Platforms 330

Java ME Application Types 331

Java ME Innovation 332

Java ME’s Death 333

Summary 335


Chapter 16: Final Words 337
What we have covered in the book 337

What to research next 338
In closing 339

Index 341

www.it-ebooks.info

xi

About the Author
Ovidiu Iliescu is a self-taught software developer; he started to write computer programs when
he was 9 and now has almost 15 years of experience under his belt. After writing desktop
applications and web-based applications, Ovidiu shifted his attention to the field of mobile
development. With a passion for all things computer related, his main interests besides mobile
software are computer graphics, algorithms and optimizations.
Ovidiu lives in Romania and has recently started his own software company, November
Solutions, to turn his ideas into products – and maybe conquer the world. Currently, he is also
working as a software developer for Enough Software and occasionally takes on interesting
projects as a freelancer.
You can get in touch with Ovidiu by visiting his website at www.ovidiuiliescu.com .
www.it-ebooks.info

xii

About the Technical
Reviewer
Carol Hamer received her Ph.D. in Number Theory from Rutgers, the State University of New
Jersey. Since then, she has worked as a software engineer for twelve years in the U.S., France, and
Switzerland, including three years working for In-Fusio Mobile Games.
Carol has written three books on mobile game programming: J2ME Games with MIDP2,
Creating Mobile Games, and Learn BlackBerry Games Development, all from Apress.

www.it-ebooks.info

xiii

Acknowledgments
Douglas Pundick and Carol Hamer have provided valuable help and suggestions for the book’s
content. Adam Heath has kept an watchful eye on things and has extended my deadlines more
than once. Mary Ann Fugate and Sharon Wilkey have painstakingly copy edited the entire
manuscript. Without them, this book could not exist. Thank you!
My friends and loved ones have been very supportive of me during the entire writing process.
They have tolerated my busy schedule and my sometimes weary attitude. For this they have my
gratitude.
Special thanks go out to Robert Virkus and to everyone at Enough Software for their support
and for being an exceptionally awesome bunch of people.
Finally, a lot of people not mentioned here worked behind the scenes at Apress and at other
companies to bring this book to light. Thank you!
www.it-ebooks.info

xiv

Introduction
I find mobile phones to be amazing pieces of technology. They allow you to be almost anywhere
in the world and still be able to reach your friends, loved ones, colleagues or business partners
with a press of a button. If that wasn’t enough, they have grown so powerful that they are now
nothing short of pocket computers. Only a few decades ago the only place you could see a similar
devices was a Star Trek movie – now they’re reality.
Modern mobile phones run on a variety of software platforms, but by far the most popular
one in terms of reach and market share is Java ME. This once feature-limited platform has grown
and matured over the years along with the hardware supporting it; is now capable of delivering
truly amazing applications that have the potential to reach hundreds of millions (if not billions)
of people.
It is a bit disappointing, then, that the techniques developers use to write Java ME
applications have not kept up with the times. Many developers, especially less experienced ones,
tend to approach Java ME development in the same extremely conservative way that it was
approached almost a decade ago, when the platform was in its infancy. Or, even worse, they treat
Java ME like a desktop platform, with complete disregard to its nuances and to the fact that it is,
in fact, a mobile platform designed to run on mobile devices. As a consequence, many Java ME
applications fall short of their potential.
The book you are reading right now aims to correct this by presenting how modern Java ME
application development should be approached – complete with all the important tips and
techniques applicable to the platform today.
What is covered in this book
This book is somewhat different from other Java ME books, in that it is built around writing a
complete and fully-functional Java ME application from scratch, using modern day development
techniques. To this end, everything from writing an application framework to writing a UI
module (complete with widgets and touch support) will be discussed – no important application
component will be left uncovered.
Furthermore, you will get to see how all of these fit into the context of a real-world
application and how individual application components interact and work together. Decisions
made along the way will be explained, potential pitfalls will be highlighted and important advice
stemming from hands-on experience will be given - all in an effort to create a comprehensive
picture of what modern Java ME application development is all about.
Once the application is written, we will cover other aspects essential to Java ME
development. Code optimization, application testing, advanced graphics, improving the user’s
experience and the proper Java ME developer’s mindset will all be discussed.
Finally, we will take a look at Java ME’s future in order to get a better understanding of where
the platform is heading and a list of further research topics relevant to Java ME development will
be given.
www.it-ebooks.info
■ INTRODUCTIN


xv
What you will need for this book
First of all, you should be familiar with Java ME development on at least an intermediate level.
This is not a beginner’s book – in fact, it is assumed that you have worked on at least one
medium-sized Java ME application before.
Second, the target device for the source code presented in this book is the WTK emulator. For
best results, it is recommended that you have both versions 2.5.2 and 3.0 installed. The WTK
emulator has been chosen because of its ubiquitous nature and because it is pretty much the
“reference implementation” for Java ME. However, the code is designed to be as portable as
possible; running it on other emulators and on real devices should work just fine (but cannot be
guaranteed).
It is also recommended that you run all the code presented and experiment with it. Java ME
is all about subtleties, many of which have to be seen in action in order to be grasped. For
obvious reasons, this is not possible with static code listings.
Let the fun begin
Java ME development, for me, is always a fun endeavor. While not always easy, it is almost never
dull either; there’s always some new challenge to overcome and something new to learn from
overcoming it.
With this in mind, I hope that you will enjoy reading this book and that the information
contained within will help make your life as a Java ME developer easier, more fun and more
interesting.
www.it-ebooks.info

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×