www.it-ebooks.info Under Construction: The book you’re reading is still under development. As part of our Beta book program, we’re releasing ß this copy well before a normal book would be released. That way you’re able to get this content a couple of months before it’s available in finished form, and we’ll get feedback to make the book even better. The idea is that everyone wins! Be warned: The book has not had a full technical edit, so it will contain errors. It has not been copyedited, so it will be full of typos, spelling mistakes, and the occasional creative piece of grammar. And there’s been no effort spent doing layout, so you’ll find bad page breaks, over-long code lines, incorrect hyphen- ation, and all the other ugly things that you wouldn’t expect to see in a finished book. It also doesn't have an index. We can’t be held liable if you use this book to try to create a spiffy application and you somehow end up with a strangely shaped farm implement instead. Despite all this, we think you’ll enjoy it! Download Updates: Throughout this process you’ll be able to get updated ebooks from your account at pragprog.com/my_account. When the book is com- plete, you’ll get the final version (and subsequent updates) from the same ad- dress. Send us your feedback: In the meantime, we’d appreciate you sending us your feedback on this book at pragprog.com/titles/erpgg2/errata, or by using the links at the bottom of each page. Thank you for being part of the Pragmatic community! Andy & Dave www.it-ebooks.info Programming Google Glass, Second Edition Build Great Glassware Apps with the Mirror API and GDK Eric Redmond The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina www.it-ebooks.info Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at https://pragprog.com. For international rights, please contact [email protected]. Copyright © 2015 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-13: 978-1-941222-18-8 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—February 13, 2015 www.it-ebooks.info Contents Changes in the Beta Releases . . . . . . . . vii Preface . . . . . . . . . . . . . . ix 1. Wrapping Your Head Around Glass . . . . . . . 1 Getting to Know Glass 1 Learning to Navigate 2 Glass Hardware 7 Glass Software 8 Wrap-Up 9 Part I — The Mirror API 2. The Google App Engine PaaS . . . . . . . . 13 Setting Up GAE 14 Making a Web App 16 Deploying to the Web 20 Fancy Templates 22 Wrap-Up 24 3. Authorizing Your Glassware . . . . . . . . . 27 Activating Your Mirror API 28 A Short Primer on OAuth 2.0 30 Applying OAuth to Create Glassware 34 Wrap-Up 40 4. Building the Timeline . . . . . . . . . . 41 Mirror HTTP Requests 41 Timeline Items 45 Multicards: Bundles and Paginating 51 Menus 54 www.it-ebooks.info • Contents iv Cron Jobs 56 Wrap-Up 58 5. Tracking Movement and User Responses . . . . . . 59 Geolocation 59 Using Location 62 Subscriptions 66 Accepting Notifications 68 Custom Menu Items 72 Wrap-Up 74 6. Making Glass Social . . . . . . . . . . . 75 Creating Contacts 75 Sharing Assets with Glassware 77 Getting and Setting Attachments 81 Wrap-Up 85 7. Designing for Glass . . . . . . . . . . . 87 A Little UX 87 Design Layout 89 Look and Feel 92 Wireframes and Mock-Ups 94 Wrap-Up 97 8. Turning a Web App to Glassware . . . . . . . . 99 ChittrChattr App 99 Glassifying the Actions 103 The Mirror Code 105 Wrap-Up 109 Part II — Glass Development Kit 9. Introducing the GDK . . . . . . . . . . 113 Choosing the GDK by Use-Case 114 Setup a GDK Dev Environment 116 An Android Primer 120 Wrap-Up 122 10. An Android Introduction on Glass . . . . . . . 125 Generating a Simple Android App 125 Basic Android SDK+GDK 131 The Activity Component 135 www.it-ebooks.info • Contents v Service, Broadcast Receiver, and Content Provider 139 Wrap-Up 145 11. Live Cards . . . . . . . . . . . . . 147 Planning a Live Card Project 147 Implementing a Live Card Glassware 152 A Card Menu 156 Launching an App with Voice 159 Updating RemoteViews 162 Wrap-Up 164 12. Advanced Rendering and Navigation . . . . . . 167 High Frequency Rendering 167 Scrolling Through Multiple Cards 176 Gestures and Sound Effects 180 Launching the Balloon Count Scroller from the Menu 183 Taking Photos with the Camera 185 Wrap-Up 188 13. Voice and Video Immersions . . . . . . . . 189 An Immersion App 189 A Speech-to-Text Caption App 192 Sending Data to a Web Service 197 Adding Geolocation 202 A QR Code Reading Camera 205 Wrap-Up 216 14. Creating an Immersive Game . . . . . . . . 219 Start The Game with a Splash Screen 220 Gesture and Sensor Inputs 223 Rendering Visuals and Playing Audio 229 Game Logic 235 Wrap-Up 242 15. Preparing For the Real World . . . . . . . . 245 Testing Glass Apps 245 Debugging and Profiling 251 Command-line Power Tools 255 Monkeyrunner 260 Signing your Glassware 262 Wrap-Up 263 www.it-ebooks.info • Contents vi 16. Turning an Android App to Glassware . . . . . . 265 Notoriety App 266 Designing a GDK Glassware UI 270 The GDK Code 274 Wrap-Up 282 A1. HTTP and HTML Resources . . . . . . . . 283 Timeline 283 Timeline Attachments 288 Locations 288 Subscriptions 289 Contacts 291 Map Parameters 293 HTML 293 Index . . . . . . . . . . . . . . 295 www.it-ebooks.info Changes in the Beta Releases P1.0, Feb 13, 2015 • Replaced all ADT examples in Part Two with Android Studio. • Due to Google’s recent announcement that the Glass Explorer program has ended, thus suspending any further purchase of Google Glass, we are following suit and suspending production of our book, Programming Google Glass. Thanks to everyone who took the leap of faith with us and reviewed or purchased the beta. Beta 3, Dec 15 • Added the final chapter, Chapter 16, Turning an Android App to Glassware, on page 265. Beta 2, November 13 • Added Chapter 15, Preparing For the Real World, on page 245. • Updated with XE changes and added a few minor clarifications Beta 1, October 21 www.it-ebooks.info report erratum • discuss Preface Google Glass is the new wearable computer that everyone is talking about. Some people love it, some hate it, but folks can’t seem to help but talk about it. In short, Glass is a head-mounted computer with an optical display, a camera, several sensors, and a voice or touch interface. You can walk around, hands-free, check your email, watch or take videos, play games, and most anything else you can do with a smartphone. Equally important, it’s gaining traction for enterprise purposes, from fast-food restaurant employee training to use in surgical settings. You can program Glassware (Glass applications) in two ways: by using the HTTP-based Mirror API web-service, or by creating native applications using the Glass Development Kit (GDK). This book covers how to program both. You’ll get a glimpse of what Glass is and what it is not, and how users can interface with Glass. In Part One of this book you’ll learn how to develop a Glass application fast, by using the Mirror API to manipulate timeline cards and menus, track a Glass’s geolocation, create rich interactions by responding to user inputs, and capture or serve user images and videos. In Part Two you’ll learn how to shape user experience with the GDK by interacting with Glass hardware, from voice-to-text inputs, to QR code reading with the live camera, to building your own video game with fine-grained sensor inputs. You’ll see how to properly design new Glassware or update existing applica- tions to become Glassware. This is the book to read if you want a shortcut to this brave new world. What’s the Big Deal with Glass? Imagine a world where computers did not exist, but the abilities that comput- ers provided did. Rather than pulling a phone out of your pocket to talk with someone, you’d simply speak their name and you’d be connected. Or instead of taking out a laptop to play a video game, you would merely ask to play a game and it would appear. Or rather than sitting in front of a television screen www.it-ebooks.info report erratum • discuss
Description: