Tải bản đầy đủ (.pdf) (681 trang)

pro android wearables building apps for smartwatches and other wearables jackson 2015 06 30 Lập trình android

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (46.91 MB, 681 trang )

CuuDuongThanCong.com


Pro Android Wearables
Building Apps for Smartwatches

Wallace Jackson

CuuDuongThanCong.com


Pro Android Wearables: Building Apps for Smartwatches
Copyright © 2015 by Wallace Jackson
This work is subject to copyright. All rights are reserved by the Publisher, whether
the whole or part of the material is concerned, specifically the rights of translation,
reprinting, reuse of illustrations, recitation, broadcasting, reproduction on
microfilms or in any other physical way, and transmission or information storage
and retrieval, electronic adaptation, computer software, or by similar or dissimilar
methodology now known or hereafter developed. Exempted from this legal
reservation are brief excerpts in connection with reviews or scholarly analysis or
material supplied specifically for the purpose of being entered and executed on a
computer system, for exclusive use by the purchaser of the work. Duplication of
this publication or parts thereof is permitted only under the provisions of the
Copyright Law of the Publisher’s location, in its current version, and permission
for use must always be obtained from Springer. Permissions for use may be
obtained through RightsLink at the Copyright Clearance Center. Violations are
liable to prosecution under the respective Copyright Law.
ISBN-13 (pbk): 978-1-4302-6550-4
ISBN-13 (electronic): 978-1-4302-6551-1
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.
While the advice and information in this book are believed to be true and accurate
at the date of publication, neither the authors nor the editors nor the publisher can
accept any legal responsibility for any errors or omissions that may be made. The
publisher makes no warranty, express or implied, with respect to the material
contained herein.
Managing Director: Welmoed Spahr
Lead Editor: Steve Anglin
Technical Reviewer: Jeff Tang
Editorial Board: Steve Anglin, Louise Corrigan, Jim DeWolf, Jonathan
Gennick, Robert Hutchinson, Michelle Lowman, James Markham,
Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas

CuuDuongThanCong.com


Pundick, Ben Renow-Clarke, Gwenan Spearing, Steve Weiss
Coordinating Editor: Mark Powers
Copy Editor: Mary Bearden
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media
New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800SPRINGER, fax (201) 348-4505, e-mail ,
or visit www.springeronline.com. Apress Media, LLC is a California LLC
and the sole member (owner) is Springer Science + Business Media Finance Inc

(SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail , 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.
Any source code or other supplementary material referenced by the author in this
text is available to readers at www.apress.com/9781430265504. For
additional information about how to locate and download your book’s source
code, go to www.apress.com/source-code/. Readers can also access
source code at SpringerLink in the Supplementary Material section for each
chapter.

CuuDuongThanCong.com


The Pro Android Wearables book is dedicated to everyone in the open
source community who is working diligently to make professional new
media application development software and content development
tools freely available to rich application developers to utilize to
achieve our creative dreams and our financial goals. Last, but not
least, I dedicate this book to my father, Parker Jackson, my family, my
life-long friends, and all my ranch neighbors, for their constant help,
assistance, and those relaxing, late night BBQ parties.

CuuDuongThanCong.com


Contents at a Glance

About the Author
About the Technical Reviewer
Acknowledgments
Introduction
Chapter 1: Introduction to Android Wearables: Concepts, Types,
and Material Design
Chapter 2: Setting Up an Android 5 Wearables Application
Development Workstation
Chapter 3: A Foundation for Android Wearables: New Wearable
Features in Android 5
Chapter 4: Exploring Android Studio: Getting Familiar with the
IntelliJ IDEA
Chapter 5: Android Virtual Devices: Setting Up Wearables
Application Emulators
Chapter 6: Introduction to Android Watch Faces Design:
Considerations and Concepts
Chapter 7: Program Watch Faces for Wear: Creating the Watch
Face Code Foundation
Chapter 8: A Watch Faces Timing Engine: Using TimeZone, Time,
and BroadcastReceiver
Chapter 9: Implement a WatchFaces Engine: Core WatchFaces
API Methods

CuuDuongThanCong.com


Chapter 10: WatchFaces Vector Design: Using Vector Graphics
for WatchFaces
Chapter 11: WatchFaces Bitmap Design: Using Raster Graphics
for WatchFaces

Chapter 12: WatchFaces Digital Imaging: Developing Multiple
Mode Assets
Chapter 13: Watch Face Configuration Companion Activity:
Google Mobile Services
Chapter 14: Watch Face Configuration Companion Activity Utility
and Wearable API
Chapter 15: Wearables Application Testing: Using Hardware
Devices in Android Studio
Chapter 16: Wear API Deprecation: Updating Apps to Use New
Classes or Methods
Chapter 17: The Future of Android IoT APIs: Android TV, Glass,
Auto, and Wear
Index

CuuDuongThanCong.com


Contents
About the Author
About the Technical Reviewer
Acknowledgments
Introduction
Chapter 1: Introduction to Android Wearables: Concepts, Types,
and Material Design
Wearable Technology Defined: What Is a Wearable?
Wearable Application Development: What Types of Apps?
Android Wearable Fun: Augmented Reality, Edutainment, and Gamification

Mainstream Wearables: Smartwatches and Smartglasses
Smartwatches: Round Watch Face vs. Square Organic Light-emitting Diode

Smartglasses: Glasses and Other Smartglasses Manufacturers

Wearable Application Programming Interfaces
Android Studio 1.0: Android Wear SDK
Google Glass Development Kit: GDK for Android or Mirror

True Android or Android Peripheral: Bluetooth Link
Wearable Apps Design: Android 5 Material Design
The Android Material Design Themes: Light and Dark
Android Material Design View Widgets: Lists and Cards
Android Material Design Effects: Shadows and Animation
Android Material Design Graphics Processing: Drawables

What You Will Learn from This Book
Summary
Chapter 2: Setting Up an Android 5 Wearables Application
Development Workstation
Work Process for Creating an Android Workstation
CuuDuongThanCong.com


Android Development Workstation: Hardware Foundation
Android Development Workstation: Software Foundation

Java 7: Installing the Foundation for Android Studio
Android Studio 1.0: Download the Android 5 IDEA
Installing Android Studio: IntelliJ IDEA and Android SDK

Professional Digital Imaging Software: GIMP 2.8.14
Professional Digital Video Editing: Lightworks 12

Professional 3D Modeling and Animation: Blender
Professional Digital Audio Editing: Audacity 2.0.6
Professional UI Design Wireframing: Pencil Project 2.0.5

Professional Business Software Suite: OpenOffice 4
Other Open Source and Affordable Media Software
Summary
Chapter 3: A Foundation for Android Wearables: New Wearable
Features in Android 5
Android’s Project Volta: Power Management Tools
Android 5 Process Scheduler: JobScheduler and JobInfo
Android 5 Battery Optimizer: The BatteryHistorian Tool

Android’s Network Connection: NFC and Bluetooth
Android 5 Multiple Network Support: ConnectivityManager
Android 5 Low Energy Bluetooth: The Bluetooth LE API
Android 5 NFC Improvements: Near Field Communication

Android 5 Media: Adding Wow-Factor to Wearables
Digital Video Playback: MediaController and MediaSession
Digital Audio Playback: Enhanced AudioTrack Precision
Real-Time 3D Rendering: OpenGL ES and Extension Pack
WebKit Media: WebView, WebAudio, WebGL, and WebRTC
Android MediaBrowser Class: Browsing Third-Party Media
Android Camera 2 API: UHD Image Processing Support

Android 5 Notifications: LockScreen and MetaData
LockScreen Notifications: Privacy Safeguard Control APIs

CuuDuongThanCong.com



Notification MetaData: Intelligent Notification Classification

More Android 5 Operating System Enhancements
The Recents Screen: Concurrent Document Support
Data Storage: Directory Structure Selection Support
Second Screen: Screen Capturing and Screen Sharing

Summary
Chapter 4: Exploring Android Studio: Getting Familiar with the
IntelliJ IDEA
Updating IntelliJ IDEA: Using the Update Info Dialog
Exploring IntelliJ IDEA: Help, Tips, and Keymaps
Configure Android Studio: Using the SDK Manager
Run As Administrator: Installing Using Admin Privileges

Learning the IntelliJ IDEA Basics: Projects and SDK
IntelliJ Project Level: Developing Android Applications
IntelliJ Features: SDK, Language Support, and Auto-Coding

Creating an Android Wearable App: Using IntelliJ
Summary
Chapter 5: Android Virtual Devices: Setting Up Wearables
Application Emulators
Using the AVD Manager: Creating Wear Emulators
Using Wear Emulators: Testing Your Wearable App
Using IntelliJ Run: Running Wearable Apps in Round Wear
Switching AVDs: Running Apps in Round Wear ARM AVD
Switching AVDs: Running Apps in Square Wear ARM AVD


Summary
Chapter 6: Introduction to Android Watch Faces Design:
Considerations and Concepts
Watch Face Design: Considerations and Guidelines
A Watch Faces UI: Seamless Blending of Art and Function

CuuDuongThanCong.com


Watch Faces Power Usage: Interactive and Ambient Modes
Watch Face Power Conservation: Low-bit and Burn Protect
Watch Faces UI Design Shapes: Square vs. Round

Watch Faces Integration: Assimilating OS Functions
Android Notifications: CardView UI Layout Messaging
Android Hardware State Indicators: Hardware Mode Status
Android Hotword Placement: The OK Google Phrase
Android Peripheral Connection: The Wear Companion App

Watch Faces Function: Functional Data Integration
Data Visualization: The Data You Want a User to See
Data Integration: A Fusion of Watch Face Design and Data
Data Assimilation: Use a Simple, Unified Design Objective
Watch Face Development: Start Basic and Add as You Go

Watch Faces Graphic Design: Multimedia Concepts
Vector Watch Faces: Using SVG, Shapes, and Gradients
Bitmap Watch Faces: Bitmap Formats and Image Concepts
Animated Watch Faces: Animation and AnimationDrawable


Summary
Chapter 7: Program Watch Faces for Wear: Creating the Watch
Face Code Foundation
Gradle Scripts: Setting Gradle Build Dependencies
Android Permissions: Watch Face Uses-Permission
Canvas Watch Face Service: A Watch Face Engine
The CanvasWatchFaceService Class: An Overview
Creating a ProWatchFaceService Subclass: extends Keyword
The CanvasWatchFaceService.Engine Class: The Engine
Creating a Private Engine Class: Using onCreateEngine( )

Watch Face XML Assets: Create and Edit XML Files
Watch Face Wallpaper: Creating a Wallpaper Object in XML
Declaring a WatchFace Service: The XML <service> Tag

Watch Face Image Preview: Using Drawable Assets
Summary

CuuDuongThanCong.com


Chapter 8: A Watch Faces Timing Engine: Using TimeZone, Time,
and BroadcastReceiver
Your WatchFace Surface: Android SurfaceHolder
Android SurfaceHolder Interface: The Watch Face Surface
A SurfaceHolder Object: onCreate(SurfaceHolder surface)

Setting Watch Face Style: WatchFaceStyle.Builder
Android WatchFaceStyle Class: Styling Your Watch Face

Android WatchFaceStyle.Builder Class: Building the Style
Building Your Watch Face: Using .setWatchFaceStyle( )

Setting Watch Face Time: The Time-Related Classes
Java Time Utility Classes: TimeUnit and TimeZone
Keep Watch Face Time: WATCH_FACE_UPDATE Constant
Android Classes: Time, Handler, and BroadcastReceiver

Summary
Chapter 9: Implement a WatchFaces Engine: Core WatchFaces
API Methods
WatchFace Seconds Time Engine: Using a Handler
Android’s Handler Class: Handling Time Update Messages
Android’s Message Class: Create a Time Update Message
Creating a Second Hand Timer: The updateTimeHandler

Watch Faces Time Calculation: Using System Time
Java System Class: Accessing Time in Milliseconds
Watch Face Seconds: Calculating Second Hand Movement

WatchFaces API: Core Methods to Implement
Android WatchFaceService Class: Core Constants
Adding WatchFaceService Constants: Burn-In and Low-Bit
Android WatchFaceService.Engine Class: Core Methods
Adding WatchFaceService.Engine Methods: Core Function

Summary
Chapter 10: WatchFaces Vector Design: Using Vector Graphics
for WatchFaces


CuuDuongThanCong.com


WatchFace Painting Engine: Using the Paint Object
Android’s Paint Class: Paint Vector Shapes on the Canvas
WatchFaces Painting: Creating Watch Face Paint Objects

WatchFace Drawing Engine: The .onDraw( ) Method
The Android Canvas Class: Your Canvas Drawing Methods
Drawing Your WatchFace: Using the .drawLine( ) Method

Advanced Mode Support: Dynamic Paint Methods
Controlling Anti-Aliasing: Creating a setAntiAlias( ) Method
Controlling Burn-In: Creating a setBurnInProtect( ) Method
Ensuring Mode Support: An ensureModeSupport( ) Method

Invoking Mode Methods: onAmbientModeChanged( )
Returning to Interactive Mode: checkTimer( ) Method
Summary
Chapter 11: WatchFaces Bitmap Design: Using Raster Graphics
for WatchFaces
Testing a WatchFaces Design: Using the Round AVD
Sending the Whole Second Delay to Your Handler Object
Setting a Time Object to a Current Time in the Draw Logic

Testing a WatchFace Design: Using a Square AVD
AVD Crashes: Can’t Connect and Not Responding Panels
Special Screen Modes: Testing the Low-Bit Ambient Mode
Special Screen Modes: Testing Low-Bit and Burn-In Modes


Android WindowInsets Class: Polling Screen Shape
Detecting WatchFace Shape: Using WindowInsets
Android Bitmap Class: Using Digital Image Assets
Android Resources Class: Using Your Res Folder
Accessing Imagery: Using Bitmap and Resources
Android Drawable Class: Creating Drawable Objects
Loading the Drawable: Using the roundFlag Boolean
Android’s BitmapDrawable Class: Image Drawables
CuuDuongThanCong.com


Using BitmapDrawable Object: Extract and Scale
Scaling Bitmaps: Using the .createScaledBitmap( ) Method

Testing Background Bitmaps: Round vs. Square
Solving the roundFlag Problem: onCreate( ) to onDraw( )
Optimizing Your onDraw( ): First Draw vs. Every Draw

Summary
Chapter 12: WatchFaces Digital Imaging: Developing Multiple
Mode Assets
Ambient Mode Bitmap: GIMP Grayscale Image Mode
Low-Bit Mode Bitmaps: GIMP’s Posterize Algorithm
Dithering Low-Bit Imagery: Indexed Mode Conversion
Creating a Burn-In Mode Bitmap: Using an Invert Algorithm

Multimodal Bitmaps: Changing Bitmaps Using Java
Installing Bitmap Objects into Your Low-Bit Ambient Mode
Refining Interactive Mode: Set Tick Marks Color to Black
Testing Interactive and Ambient Modes in the Square AVD


Android Wear Burn-In Mode: Bitmap and Java Code
Creating Burn-In Mode Bitmaps: GIMP Brightness-Contrast
Burn-In Protection in Java: if(enableBurnInAmbientMode)
Testing the Burn-In Protect Mode Bitmap and Java Code

Summary
Chapter 13: Watch Face Configuration Companion Activity:
Google Mobile Services
Creating a ProWatchFaceCompanionConfigActivity
The Mobile App: Adding Your Activity to AndroidManifest
The Java Class: Creating a WatchFace Companion Activity
The Wear App: Adding Companion Metadata to Manifest

Google Play Services: The GoogleApiClient Class
Android’s GoogleApiClient: Using Google Mobile Services
Creating the Play Client: Coding Your .onCreate( ) method
The WatchFaceCompanion Class: Configuration Constants

CuuDuongThanCong.com


The ComponentName Class: Specify a Component
Setting Watch Face Identity: ComponentName and PeerId
The GoogleApiClient.Builder: Building a Google API Client
Building the GoogleApiClient: Using the Wearable API
Starting and Stopping a Play Client: onStart( ) and onStop( )

Connect a Client: Creating the onConnected Method
Android Uri Class: Uniform Resource Identifier Objects

Android Uri.Builder Class: Building an Android URI Object
Building a Uri for a Client: Finishing the onConnected( )
Android’s GMS DataApi Interface: Configuring a Data API
Using the DataApi Class: Configuring the Wearable.API
The Android PendingResult Class: Receiving the Result

Creating a Not Connected Dialog: Using AlertDialog
Android AlertDialog: Creating an Alert Dialog for Your App
Android AlertDialog.Builder: Building the Alert Dialog
Using AlertDialog.Builder: Coding the AlertDialog System

Coding an onResult Method: DataItem and DataMap
Android’s DataItem Interface: A Foundation for Wear Data
Loading a DataItem Object: Using a .getDataItem( ) Method
Android’s DataMapItem Class: A DataItem with a Map
Using a DataMapItem Object: The .fromDataItem( ) Method
Android Data Map
Creating a DataMap Object: Using a .getDataMap( ) Method

Creating a Listener Service: .onMessageReceived( )
The Android MessageEvent Class: Processing a Message
Implementing a MessageEvent Object: Extracting the Data
The ConnectionResult Class: Connecting to the Network
Implementing a ConnectionResult: Blocking a Connection

Summary
Chapter 14: Watch Face Configuration Companion Activity Utility
and Wearable API
The ProWatchFaceUtility Class: Managing the Data
Creating a ProWatchFaceUtility Class: Defining Constants

Loading DataItems into a DataMap: .putConfigDataItem( )

CuuDuongThanCong.com


Android PutDataMapRequest Class: Put in a Data Request
Using PutDataMapRequest to Put a Configuration DataItam

Android Wearable Class: Android’s Wearable APIs
Using the Wearable Class: Putting a DataApi Data Request
Using Android’s Node API: .fetchConfigDataMap( ) Method
Using Wearable DataApi: DataItemResultCallback( ) Class
Replacing Changed Data: overwriteKeysInConfigDataMap
Updating a DataMap Object: onConfigDataMapFetched( )

Connect the Maps: Call the Utility from the Listener
Finishing the Configuration Companion: UI Design
Choosing Color Using the Spinner Widget: XML UI Layout
Setting the Spinner Widget: setUpColorPickerSelection( )
Setting Up a Spinner Listener: setUpColorPickerListener( )
Setting Up All Four Spinners: A .setUpAllPickers( ) Method

Testing the WatchFaceCompanion Activity: Nexus 5
Summary
Chapter 15: Wearables Application Testing: Using Hardware
Devices in Android Studio
Interfacing a Device with a Computer: USB Drivers
Installing Wear API: Linking Smartwatch with Phone
Downloading and Installing Wear API: Google Play Store
Setting Up the Smartwatch: Sony SmartWatch 3


Using ADB: Linking a Smartphone with the AVD
Android Debug Bridge: Networking Hardware and Software
Using AVD Inside Android Studio: ADB Port Forwarding

Bluetooth Debugging: Linking to your Smartwatch
Smartwatch Set Up: Pair and Enable Bluetooth Debugging

Java Singleton: ProWatchFaceUtility( ) Constructor
Testing and Debugging: Creating Your .APK Files
The Android Studio Build System: An Overview
Configuring Gradle Builds: Creating Different APK Types
Building Your Project: Using the Gradle Build Engine

CuuDuongThanCong.com


Summary
Chapter 16: Wear API Deprecation: Updating Apps to Use New
Classes or Methods
Dealing with the Unexpected Update: Android 5.2
Dealing with Deprecation: .getDrawable( ) and Time
Android’s Resources Class: Two .getDrawable( ) Methods
The Resources.Theme Nested Class: Theme Attributes
The ResourcesCompat Class: Backward Compatibility
Dealing with Deprecated Classes: The Time Class

Upgrading Your Code: Calendar and .getDrawable( )
Upgrading the Time Class Code: Using the Calendar Class
Upgrading timeZoneReceiver: The .setTimeZone( ) Method

Upgrading the onDraw( ) Method: Using .setTimeInMillis( )
Loading Your Time Variables: Using the .get( ) Method
Upgrade the onVisibilityChanged( ) Method: .setTimeZone( )
Upgrading the Code: Using the .getDrawable(int, Theme)

Solving IDE Problems Introduced by SDK Upgrades
Upgrading Gradle Files: Adding Build Definition Sections
Using Invalidate and Restart: Rebuilding Project Structure
Using Import Project
Re-creating a Project from Scratch: Copy Code and Assets
The Moral of the Story: Android Is More than Java or XML

Summary
Chapter 17: The Future of Android IoT APIs: Android TV, Glass,
Auto, and Wear
HD and UHD Android TV: The Opposite of Wear
Android Auto: Android Apps for the Car Dashboard
Google Glass: Develop Apps for Smart Eyeglasses
Android Wear: Interesting API Elements to Explore
Detecting Location: GPS Data from Google Play Services
Voice Actions: Using Speech Recognition Technology

CuuDuongThanCong.com


Summary
Index

CuuDuongThanCong.com



About the Author

Wallace Jackson has been writing for international multimedia publications about his
content production work for major international brand manufacturers since the advent of
Multimedia Producer Magazine, nearly two decades ago, when he wrote about
advanced computer processor architecture for an issue centerfold (removable “miniissue” insert) distributed at the SIGGRAPH trade show. Since then, he has written for a
number of popular publications about his production work using interactive 3D and new
media advertising campaign design, including 3D Artist Magazine, Desktop Publishers
Journal, CrossMedia Magazine, AVvideo/Multimedia Producer Magazine, Digital
Signage Magazine, and Kiosk Magazine.
He has authored a half-dozen Android book titles for Apress, including four titles in the
popular Pro Android series. This particular Pro Android Wearables application
development title focuses on the Java 7 programming language that is used with Android
5 (and most other popular platforms as well) so that developers can “code once, deliver
everywhere.” Open source technologies such as Java, XML, WebKit, Gradle, SQL, and
others used in Android 5 allow free for commercial use applications in an open
environment that does not have to be approved and can make millions in profits.
He is currently the CEO of Mind Taffy Design, a new media content production and
digital campaign design and development agency, located in North Santa Barbara
County, halfway between its clientele in Silicon Valley to the north and in West Los
Angeles, Beverly Hills, Hollywood, “The OC” (Orange County) and San Diego to the

CuuDuongThanCong.com


south.
Mind Taffy Design has created open source technology-based (HTML5, JavaScript,
Java 8, JavaFX 8, and Android 5) digital new media content deliverables for more than
a quarter century (since 1991) for a large number of leading branded manufacturers

worldwide, including Sony, Tyco, Samsung, IBM, Dell, Epson, Nokia, TEAC, Sun,
Micron, SGI, and Mitsubishi.
He received his undergraduate degree in business economics from the University of
California at Los Angeles (UCLA). He received his graduate degree in MIS design and
implementation from the University of Southern California (USC). He also received his
postgraduate degree in marketing strategy at USC, and completed the USC graduate
entrepreneurship program. His USC degrees were completed while at the USC nighttime Marshall School of Business MBA program, which allowed him to work full time
as a programmer while he completed his graduate and his postgraduate business
degrees.

CuuDuongThanCong.com


About the Technical reviewer

Jeff Tang has successfully developed mobile, web, and enterprise apps on many
platforms. He became a Microsoft-certified developer and a Sun-certified Java
developer last century; had Apple-featured, top-selling iOS apps in the App Store; and
was recognized by Google as a top Android market developer. He has a master’s
degree in computer science with an emphasis on artificial intelligence and believes in
lifelong learning. He loves playing basketball (he once made 11 three-pointers and 28
free throws in a row), reading Ernest Hemingway and Mario Puzo, and fantasizing about
traveling around the world.

CuuDuongThanCong.com


Acknowledgments
I would like to acknowledge all my fantastic editors and the support staff at Apress who
worked long hours and toiled so diligently on this book to make it the ultimate Pro

Android Wearables title.
Steve Anglin, for his work as the Lead Editor for this book, and for hiring me to write
all of these Android and Java programming titles over the past decade.
Matthew Moodie, for his work as the Development Editor on the book, and for his
experience and guidance during the process of making this book one of the truly great
Pro Android smartwatch software development titles.
Mark Powers, for his work as the Coordinating Editor for this book, and for his constant
diligence in making sure I hit, or surpassed, my always looming writing and editing
deadlines.
Mary Bearden, for her work as the Copy Editor on this book, and for her close attention
to every detail, and also for conforming the text to the current Apress book writing
standards.
Jeff Tang, for his work as the Technical Reviewer on the book, and for making sure I
didn’t make any programming mistakes. Java code with mistakes does not run properly,
if at all, unless they are very lucky mistakes, which is quite rare in computer
programming these days.
Finally, I’d like to acknowledge Oracle for acquiring Sun Microsystems and for
continuing to enhance Java, so that it remains the premiere open source programming
language, and Google, for making 64-bit Android 5 the premiere open source operating
system and for acquiring ON2’s VP8 video codec and making it available to multimedia
producers on both the Android 5 and HTML5 interactive content development
platforms.

CuuDuongThanCong.com


Introduction
Welcome to the Pro Android Wearables book, where you will learn how to develop
applications for smartwatch devices. There will be a follow-on book called Pro
Android IoT (Internet of Things), which will cover the other Android APIs such as

Android TV, Android Auto, and Android Glass, so in this book I can focus only on an
exploring the smartwatch device market.
The reason that smartwatches, along with iTV sets, are continuing to explode is a case
of basic economics. There are now dozens of manufacturers, including traditional watch
brands, such as Citizen, Rolex, Casio, Tag Heuer, Timex and Fossil, making
smartwatches, as well as all of the major consumer electronics giants, including Sony,
Samsung, LGE, ASUS, Huawei and Motorola, who now have multiple smartwatch
models. This generates incredibly massive competition, which drives down pricing,
making this value proposition difficult to argue with. I Google searched Android Wear
watches today and found two of the most impressive smartwatches, the Motorola
MOTO 360 and the ASUS ZenWatch, priced at less than $200. For a computer on your
wrist, made with rose gold and calf leather (ZenWatch) or a beautiful carbon black steel
bracelet (MOTO), that is an exceptionally reasonable price point. I expect
smartwatches to range from $150 to $450 and to continue to generate increasing sales
into the future, while adding screen resolution (480 to 640 pixels), processor cores (two
to four), and system memory (1 to 2 GB).
This book will cover how to develop applications for an exploding smartwatch market,
and it includes the new Watch Faces API released by Google that allows developers to
create their application as the watch face design itself! Since that is what a watch is
used for, I will discuss the Watch Faces API in detail, so that your smartwatch
applications can offer their functions to the users while also telling them the time, date,
weather, activity, notifications, and so forth. You will learn how to use Google Play
Services and make Android Wear applications that have components running on your
smartwatch, as well as on the smartphone or tablet, called a companion activity
application.
Chapter 1 looks at Android Wear and wearable concepts and design considerations,
before you set up the Wear production workstation, including your IDE, SDKs, and
New Media Content Development applications in Chapter 2. I will discuss the new
features of Android Wear in Chapter 3, before you learn about the IntelliJ IDEA, and
create a foundation for your Wear project in Chapter 4. In Chapter 5 you will set up the

IntelliJ IDEA, also known as Android Studio, for production readiness, by making sure
all the SDKs and emulators are up to date and creating AVDs to use for round or square

CuuDuongThanCong.com


watch face testing.
In Chapter 6, you will get ready to start coding by looking at the Android Watch Faces
API and all of its functionality and UI design considerations. In Chapter 7, you will
actually code your Watch Face Service and Watch Face Engine classes. These drive the
Watch Face infrastructure which you will be putting into place in subsequent chapters.
In Chapter 8 you will put your Watch Face Timing Engine into place, learning about the
Time and TimeZone classes, as well as implementing a BroadcastReceiver object to
manage the implementation of these classes (objects). In Chapter 9, you will implement
core Watch Faces API methods that control different watch face rendering styles and
event processing.
In Chapter 10 you will learn about vector graphics and how to “render” designs on the
smartwatch face using the onDraw( ) method, and in Chapter 11 you will learn about
raster graphics and how to use BitmapDrawable objects along with PNG32 bitmap
assets to add digital imagery to your smartwatch designs. In Chapter 12 you will learn
digital imaging techniques that will allow you to optimize the number of colors used to
accommodate different smartwatch display color limitations, so you can get the most
photorealistic results for your smartwatch application design.
In Chapter 13 you will learn about the Google Mobile Services (GMS) APIs and how to
access Google Play Services so that your Wear apps can do even more than they can
using the native Android and Android Wear APIs. In Chapter 14 you will implement
Android Wear Data APIs in your code to create a Watch Face Utility class to manage
your users’ settings.
In Chapter 15 you will learn how to set up a testing environment for real-world
hardware devices and learn about the Android Debug Bridge, or ADB, as well as how

to implement USB device drivers for your hardware devices.
In Chapter 16, you will learn how to dealing with API deprecation and class and
method call code updates, as you remove the deprecated Time class and replace it with
the Calendar and GregorianCalendar class code to make your application more
efficient.
Finally, Chapter 17 goes over the Android IoT APIs and other Wear API features to
consider for your smartwatch applications, such as voice recognition and location
tracking using the Speech and GPS APIs, respectively. With the information in this
book, you will be well on your way to developing smartwatch applications using
Android Wear and Android Watch Faces APIs!

CuuDuongThanCong.com


Chapter 1
Introduction to Android Wearables:
Concepts, Types, and Material Design
Welcome to the Pro Android Wearables book! This book will show you how to
develop Android Studio applications for those Android devices that are outside your
normal smartphones and tablets. This book also includes Android development for
devices that can be worn on your person, which is why these Android devices are
commonly called “wearables.”
If you are looking to develop for Android appliances, such as iTV sets, 4K iTV, game
consoles, robots, or other appliances, then the book you want is the Pro Android IoT
(Apress, 2016) book. That title covers development for Android devices known as
“Internet of Things,” which include devices that are not worn on your person and are
beyond the more normal tablets and phones.
This chapter will look at the different types of wearables, as well as their features and
popular usage, and you will learn about the terminology, concepts, differentiating
factors, and important technology surrounding wearables and take a closer look at what

types of Android applications you can develop for wearable devices. I’ll get all of this
wearables-related learning out of the way here so you can focus on setting up your
workstation in Chapter 2, and then get into Android wearables features in Chapter 3. I
will also explain the distinction between wearable devices and Android wearable
peripherals.
I’ll also discuss the new material design features that have been added to Android 5, as
these are available for wearables’ application development, and you will see all the
cool things you can do with these!

Wearable Technology Defined: What Is
a Wearable?
The term wearables, as well as the terms wearable technology and wearable devices,

CuuDuongThanCong.com


×