Cardboard VR Projects for Android

567 860 0
Cardboard VR Projects for Android

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Pro Android Wearables Building Apps for Smartwatches Wallace Jackson For your convenience Apress has placed some of the front matter material after the index Please use the Bookmarks and Contents at a Glance links to access them Contents at a Glance About the Author����������������������������������������������������������������������������������������������������xix About the Technical Reviewer��������������������������������������������������������������������������������xxi Acknowledgments������������������������������������������������������������������������������������������������xxiii Introduction�����������������������������������������������������������������������������������������������������������xxv ■■Chapter 1: Introduction to Android Wearables: Concepts, Types, and Material Design����������������������������������������������������������������������������������������������������� ■■Chapter 2: Setting Up an Android Wearables Application Development Workstation��������������������������������������������������������������������������������������������������������� 25 ■■Chapter 3: A Foundation for Android Wearables: New Wearable Features in Android 5��������������������������������������������������������������������������������������������������������� 47 ■■Chapter 4: Exploring Android Studio: Getting Familiar with the IntelliJ IDEA����� 71 ■■Chapter 5: Android Virtual Devices: Setting Up Wearables Application Emulators����������������������������������������������������������������������������������������� 97 ■■Chapter 6: Introduction to Android Watch Faces Design: Considerations and Concepts����������������������������������������������������������������������������������������������������� 117 ■■Chapter 7: Program Watch Faces for Wear: Creating the Watch Face Code Foundation������������������������������������������������������������������������������������������������ 143 ■■Chapter 8: A Watch Faces Timing Engine: Using TimeZone, Time, and BroadcastReceiver�������������������������������������������������������������������������������������������� 169 v vi Contents at a Glance ■■Chapter 9: Implement a WatchFaces Engine: Core WatchFaces API Methods������������������������������������������������������������������������������������������������������ 203 ■■Chapter 10: WatchFaces Vector Design: Using Vector Graphics for WatchFaces�������������������������������������������������������������������������������������������������� 235 ■■Chapter 11: WatchFaces Bitmap Design: Using Raster Graphics for WatchFaces�������������������������������������������������������������������������������������������������� 275 ■■Chapter 12: WatchFaces Digital Imaging: Developing Multiple Mode Assets������������������������������������������������������������������������������������������������������ 317 ■■Chapter 13: Watch Face Configuration Companion Activity: Google Mobile Services������������������������������������������������������������������������������������� 345 ■■Chapter 14: Watch Face Configuration Companion Activity Utility and Wearable API���������������������������������������������������������������������������������������������� 395 ■■Chapter 15: Wearables Application Testing: Using Hardware Devices in Android Studio����������������������������������������������������������������������������������������������� 451 ■■Chapter 16: Wear API Deprecation: Updating Apps to Use New Classes or Methods������������������������������������������������������������������������������������������� 487 ■■Chapter 17: The Future of Android IoT APIs: Android TV, Glass, Auto, and Wear�������������������������������������������������������������������������������������������������� 521 Index��������������������������������������������������������������������������������������������������������������������� 539 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 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 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 I will discuss the new features of Android Wear in Chapter 3, before you learn about the IntelliJ IDEA, and create a foundation for xxv xxvi Introduction your Wear project in Chapter In Chapter 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 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 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 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! Chapter 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 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 with these! CHAPTER 1: Introduction to Android Wearables: Concepts, Types, and Material Design Wearable Technology Defined: What Is a Wearable? The term wearables, as well as the terms wearable technology and wearable devices, is indicative of consumer electronics technology based on embedded computer hardware that is built inside products that are worn on the outside of one’s body This includes clothing and accessories, including jewelry, such as watches, and protective wear, such as glasses, as well as items of clothing such as socks, shoes, hats, and gloves and sports, health, and fitness products that can be comfortably worn somewhere on your body Wearable devices often have some modicum of communications capability, and this will allow the device wearer to access information in real time Data input capability is a necessary feature for wearable devices, as it allows users to access the features of the wearable device and use it to run the applications you are going to be learning to develop over the course of this book Data input is usually in the form of touch screen interfaces, voice recognition (also known as voice input), or sometimes through use of hardware buttons built right into the wearable device itself Thanks to the cloud, local storage is not necessary for a wearable device, although some feature micro SD (secure digital) cards or store data on linked, companion devices Wearable devices are able to perform many of the same application tasks as mobile phones and tablets; in fact, many wearable devices require that the wearable device be “married” to another Android device (more on this later on in the chapter) within the operating range of Bluetooth 4.x technology Android wearable devices tend to be more sophisticated on the “sensor input” side of the equation than hand-held technologies on the market today This is because wearable devices will provide sensory and scanning features not typically seen with smartphone or tablet devices Examples of this include features such as biofeedback and the tracking of physiological functions, such as pulse, heart rate, workout intensity, sleep monitoring, and so on Examples of wearable hardware devices include smartwatches, smartglasses, textiles, also called smart fabrics, hats, caps, shoes, socks, contact lenses, ear rings, headbands, hearing aids, and jewelry, such as rings, bracelets, and necklaces Wearable technology tends to refer to things that can be put on and taken off effortlessly It’s important to note that there are versions of the wearables concept that are more radical in nature, for instance the implanted devices, such as microchips or even smart-tattoos I will not be covering application development for any of these nonmainstream device types in this book Because the general public will primarily be using smartwatches, I’ll be focusing on that type of wearable device Ultimately whether a device is worn on, or incorporated into, the body, the purpose of these Android wearable devices is providing constant, convenient, portable, seamless, and hands-free access to consumer electronics Wearable Application Development: What Types of Apps? The uses of wearable technology are limited only by your imagination, and the implications of these applications will be far reaching, which is why you have purchased this Pro Android Wearables book in the first place! CHAPTER 1: Introduction to Android Wearables: Concepts, Types, and Material Design Wearable applications will influence a wide spectrum of industry verticals in a large number of ways Some of the many industries that are embracing Android wearable devices include the health care, pharmaceutical, education, fitness, entertainment, music, aging, disability, transportation, finance, enterprise, insurance, automotive, and gaming industries, and the list grows larger daily The goal of wearable applications in each of these industries should be to seamlessly incorporate functional, portable electronics and computers into an individual’s daily life Prior to their presence in the consumer market, wearable devices were primarily utilized in military operations as well as in the pharmaceutical, health care, sports, and medicine industries More than ten years ago, medical engineers started talking about a wearable device that could monitor the health of their patients in the form of a smart-shirt or smart-wristband At least one of these is a mainstream technology today, and its aim is to monitor vital signs and send that biofeedback information to an application or a web site for data tracking and analysis The types of Android applications that you can create for use on wearable devices is limited only to your imagination This is because wearables are “paired” with more advanced hardware and thus have those same hardware capabilities that smartphones or tablets have, plus some sensors that smartphones and tablets not have! One of the logical application verticals is health and welfare monitoring; because of these heart-rate sensors, Android wearables applications can be created that help with health-related areas of users’ lives, such as their work out in the gym, tracking their diet on the go while working or traveling, getting enough sleep, walking or running enough miles in a day, and similar applications that will help users improve their health or at least stay healthy Another logical application vertical is social media, as the current trend these days is staying connected to everyone at all times of the day, while also making new friends or business associates Androids are connected to the Internet, via Wi-Fi or 4G, at all times, so these types of wear apps are also going to be very popular for use on wearable devices Of course, games, watch faces or skins, and entertainment consumption will also be a massive application vertical for wearable devices Let’s look at this aspect so you can get some idea about how to apply what you’ll be learning! Android Wearable Fun: Augmented Reality, Edutainment, and Gamification Although wearables technology could potentially have a significant impact in the areas of social media connectivity, health, dieting, or fitness, the area of wearable technology also promises to have a major impact on the casual gaming, audio video (AV) entertainment, edutainment, and augmented reality (AR) markets Wearable applications that make everyday tasks into fun to play games, commonly termed gamification, is also a major market opportunity AR, originally called mixed reality, can leverage wearable technology AR uses i3D OpenGL capabilities, found in the Android platform, to create a realistic and immersive 3D environment that syncs up with the real world around you in real time, thanks to Java code in your Android application Whereas Android 4.4 and earlier used Java 6, Android uses CHAPTER 1: Introduction to Android Wearables: Concepts, Types, and Material Design Java Mixing virtual reality or interactive 3D (i3D) with actual reality, using digital video, digital imaging, or global positioning satellite (GPS) location-based technology, is not new by any stretch of the imagination AR apps are the most advanced type of wearable apps AR delivered through the use of wearable devices has been contemplated since before the turn of the century What’s important is that AR hardware prototypes are morphing from bulky technology, such as the massive goggles used by the Oculus Rift, into small, lightweight, comfortable, highly mobile 3D virtual reality systems The next most complex type of application that will soon be appearing on a wearable device will be wearable games You can expect to see casual games created for smartwatches and smartglasses on the market very soon Careful optimization is the key to creating a game application that will work well on a wearable device, and I will be covering that topic within this book Another complex type of entertainment application for a wearable device is the AV application Playing digital audio or digital video on a wearable device also requires careful optimization, as well as a user who owns a good pair of Bluetooth audiophile headphones, which fortunately are made by more than a dozen major electronics manufacturers these days Finally, one of the more complex types of wearable applications is custom smartwatch faces or skins These turn the watch face that a user looks at all day long into something they want their watch to look like Of course you can also create loads of text-based apps, like office utilities or handy recipe managers, for instance; these will work great on wearables! The future of Android wearables applications needs to reflect the seamless integration of fashion, functionality, practicality, visual, and user interface (UI) design I’ll discuss how to this throughout the book, after you have put together the development workstation in Chapter and created the emulators in Chapter 5, so you have a foundation in place for wearable development Mainstream Wearables: Smartwatches and Smartglasses There are two primary (i.e., mainstream) types of wearable devices that popular consumer electronics manufacturers are scrambling to manufacture The smartwatch is currently the most popular type of wearable device, with hundreds of affordable models already available in the marketplace As the centuries have passed by, watches have become the international fashion statement Thus, it is no surprise that this is the most popular and useful type of Android wearable device The other type of popular wearable genre is smartglasses, and dozens of products have already been released by companies such as Google (Glass) and Vuzix (M100) Let’s take a closer look at these two types of wearables hardware, since they are going to be the majority of the device types that run the pro Android wearables applications you will be creating during the course of this book Contents About the Author����������������������������������������������������������������������������������������������������xix About the Technical Reviewer��������������������������������������������������������������������������������xxi Acknowledgments������������������������������������������������������������������������������������������������xxiii Introduction�����������������������������������������������������������������������������������������������������������xxv ■■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��������������������������������������������������������������������� vii viii Contents True Android or Android Peripheral: Bluetooth Link���������������������������������������������������������� Wearable Apps Design: Android Material Design���������������������������������������������������������� The Android Material Design Themes: Light and Dark���������������������������������������������������������������������������� Android Material Design View Widgets: Lists and Cards���������������������������������������������������������������������� 12 Android Material Design Effects: Shadows and Animation������������������������������������������������������������������� 14 Android Material Design Graphics Processing: Drawables������������������������������������������������������������������� 19 What You Will Learn from This Book������������������������������������������������������������������������������� 22 Summary������������������������������������������������������������������������������������������������������������������������ 23 ■■Chapter 2: Setting Up an Android Wearables Application Development Workstation��������������������������������������������������������������������������������������������������������� 25 Work Process for Creating an Android Workstation�������������������������������������������������������� 26 Android Development Workstation: Hardware Foundation������������������������������������������������������������������� 27 Android Development Workstation: Software Foundation�������������������������������������������������������������������� 28 Java 7: Installing the Foundation for Android Studio������������������������������������������������������ 28 Android Studio 1.0: Download the Android IDEA��������������������������������������������������������� 32 Installing Android Studio: IntelliJ IDEA and Android SDK���������������������������������������������������������������������� 34 Professional Digital Imaging Software: GIMP 2.8.14������������������������������������������������������ 36 Professional Digital Video Editing: Lightworks 12���������������������������������������������������������� 37 Professional 3D Modeling and Animation: Blender�������������������������������������������������������� 39 Professional Digital Audio Editing: Audacity 2.0.6���������������������������������������������������������� 41 Professional UI Design Wireframing: Pencil Project 2.0.5�������������������������������������������������������������������� 43 Professional Business Software Suite: OpenOffice 4����������������������������������������������������� 44 Other Open Source and Affordable Media Software������������������������������������������������������� 45 Summary������������������������������������������������������������������������������������������������������������������������ 45 ■■Chapter 3: A Foundation for Android Wearables: New Wearable Features in Android 5��������������������������������������������������������������������������������������������������������� 47 Android’s Project Volta: Power Management Tools�������������������������������������������������������� 48 Android Process Scheduler: JobScheduler and JobInfo�������������������������������������������������������������������� 48 Android Battery Optimizer: The BatteryHistorian Tool������������������������������������������������������������������������ 49 Contents ix Android’s Network Connection: NFC and Bluetooth������������������������������������������������������� 50 Android Multiple Network Support: ConnectivityManager����������������������������������������������������������������� 50 Android Low Energy Bluetooth: The Bluetooth LE API������������������������������������������������������������������������ 51 Android NFC Improvements: Near Field Communication������������������������������������������������������������������� 52 Android Media: Adding Wow-Factor to Wearables������������������������������������������������������ 53 Digital Video Playback: MediaController and MediaSession����������������������������������������������������������������� 53 Digital Audio Playback: Enhanced AudioTrack Precision���������������������������������������������������������������������� 54 Real-Time 3D Rendering: OpenGL ES and Extension Pack������������������������������������������������������������������� 56 WebKit Media: WebView, WebAudio, WebGL, and WebRTC������������������������������������������������������������������� 59 Android MediaBrowser Class: Browsing Third-Party Media����������������������������������������������������������������� 63 Android Camera API: UHD Image Processing Support����������������������������������������������������������������������� 63 Android Notifications: LockScreen and MetaData������������������������������������������������������� 64 LockScreen Notifications: Privacy Safeguard Control APIs������������������������������������������������������������������� 64 Notification MetaData: Intelligent Notification Classification��������������������������������������������������������������� 65 More Android Operating System Enhancements��������������������������������������������������������� 65 The Recents Screen: Concurrent Document Support��������������������������������������������������������������������������� 65 Data Storage: Directory Structure Selection Support��������������������������������������������������������������������������� 67 Second Screen: Screen Capturing and Screen Sharing������������������������������������������������������������������������ 68 Summary������������������������������������������������������������������������������������������������������������������������ 68 ■■Chapter 4: Exploring Android Studio: Getting Familiar with the IntelliJ IDEA����� 71 Updating IntelliJ IDEA: Using the Update Info Dialog������������������������������������������������������ 72 Exploring IntelliJ IDEA: Help, Tips, and Keymaps������������������������������������������������������������ 72 Configure Android Studio: Using the SDK Manager�������������������������������������������������������� 75 Run As Administrator: Installing Using Admin Privileges���������������������������������������������������������������������� 78 Learning the IntelliJ IDEA Basics: Projects and SDK������������������������������������������������������ 82 IntelliJ Project Level: Developing Android Applications������������������������������������������������������������������������ 82 IntelliJ Features: SDK, Language Support, and Auto-Coding���������������������������������������������������������������� 83 Creating an Android Wearable App: Using IntelliJ���������������������������������������������������������� 86 Summary������������������������������������������������������������������������������������������������������������������������ 96 x Contents ■■Chapter 5: Android Virtual Devices: Setting Up Wearables Application Emulators����������������������������������������������������������������������������������������� 97 Using the AVD Manager: Creating Wear Emulators�������������������������������������������������������� 97 Using Wear Emulators: Testing Your Wearable App������������������������������������������������������ 104 Using IntelliJ Run: Running Wearable Apps in Round Wear���������������������������������������������������������������� 107 Switching AVDs: Running Apps in Round Wear ARM AVD������������������������������������������������������������������� 109 Switching AVDs: Running Apps in Square Wear ARM AVD������������������������������������������������������������������ 113 Summary���������������������������������������������������������������������������������������������������������������������� 116 ■■Chapter 6: Introduction to Android Watch Faces Design: Considerations and Concepts����������������������������������������������������������������������������������������������������� 117 Watch Face Design: Considerations and Guidelines���������������������������������������������������� 118 A Watch Faces UI: Seamless Blending of Art and Function���������������������������������������������������������������� 118 Watch Faces Power Usage: Interactive and Ambient Modes�������������������������������������������������������������� 118 Watch Face Power Conservation: Low-bit and Burn Protect�������������������������������������������������������������� 119 Watch Faces UI Design Shapes: Square vs Round���������������������������������������������������������������������������� 121 Watch Faces Integration: Assimilating OS Functions��������������������������������������������������� 122 Android Notifications: CardView UI Layout Messaging����������������������������������������������������������������������� 122 Android Hardware State Indicators: Hardware Mode Status�������������������������������������������������������������� 123 Android Hotword Placement: The OK Google Phrase�������������������������������������������������������������������������� 123 Android Peripheral Connection: The Wear Companion App���������������������������������������������������������������� 123 Watch Faces Function: Functional Data Integration����������������������������������������������������� 124 Data Visualization: The Data You Want a User to See�������������������������������������������������������������������������� 125 Data Integration: A Fusion of Watch Face Design and Data���������������������������������������������������������������� 125 Data Assimilation: Use a Simple, Unified Design Objective���������������������������������������������������������������� 125 Watch Face Development: Start Basic and Add as You Go����������������������������������������������������������������� 126 Watch Faces Graphic Design: Multimedia Concepts���������������������������������������������������� 126 Vector Watch Faces: Using SVG, Shapes, and Gradients�������������������������������������������������������������������� 127 Bitmap Watch Faces: Bitmap Formats and Image Concepts�������������������������������������������������������������� 127 Animated Watch Faces: Animation and AnimationDrawable�������������������������������������������������������������� 139 Summary���������������������������������������������������������������������������������������������������������������������� 141 Contents xi ■■Chapter 7: Program Watch Faces for Wear: Creating the Watch Face Code Foundation������������������������������������������������������������������������������������������������ 143 Gradle Scripts: Setting Gradle Build Dependencies������������������������������������������������������ 144 Android Permissions: Watch Face Uses-Permission���������������������������������������������������� 146 Canvas Watch Face Service: A Watch Face Engine������������������������������������������������������ 148 The CanvasWatchFaceService Class: An Overview���������������������������������������������������������������������������� 149 Creating a ProWatchFaceService Subclass: extends Keyword����������������������������������������������������������� 149 The CanvasWatchFaceService.Engine Class: The Engine������������������������������������������������������������������� 151 Creating a Private Engine Class: Using onCreateEngine( )������������������������������������������������������������������ 152 Watch Face XML Assets: Create and Edit XML Files����������������������������������������������������� 160 Watch Face Wallpaper: Creating a Wallpaper Object in XML�������������������������������������������������������������� 160 Declaring a WatchFace Service: The XML Tag������������������������������������������������������������������� 163 Watch Face Image Preview: Using Drawable Assets���������������������������������������������������� 165 Summary���������������������������������������������������������������������������������������������������������������������� 167 ■■Chapter 8: A Watch Faces Timing Engine: Using TimeZone, Time, and BroadcastReceiver�������������������������������������������������������������������������������������������� 169 Your WatchFace Surface: Android SurfaceHolder��������������������������������������������������������� 170 Android SurfaceHolder Interface: The Watch Face Surface���������������������������������������������������������������� 170 A SurfaceHolder Object: onCreate(SurfaceHolder surface)���������������������������������������������������������������� 171 Setting Watch Face Style: WatchFaceStyle.Builder������������������������������������������������������ 173 Android WatchFaceStyle Class: Styling Your Watch Face������������������������������������������������������������������� 174 Android WatchFaceStyle.Builder Class: Building the Style����������������������������������������������������������������� 175 Building Your Watch Face: Using setWatchFaceStyle( )��������������������������������������������������������������������� 177 Setting Watch Face Time: The Time-Related Classes��������������������������������������������������� 182 Java Time Utility Classes: TimeUnit and TimeZone����������������������������������������������������������������������������� 182 Keep Watch Face Time: WATCH_FACE_UPDATE Constant������������������������������������������������������������������� 185 Android Classes: Time, Handler, and BroadcastReceiver�������������������������������������������������������������������� 186 Summary���������������������������������������������������������������������������������������������������������������������� 201 xii Contents ■■Chapter 9: Implement a WatchFaces Engine: Core WatchFaces API Methods������������������������������������������������������������������������������������������������������ 203 WatchFace Seconds Time Engine: Using a Handler����������������������������������������������������� 204 Android’s Handler Class: Handling Time Update Messages���������������������������������������������������������������� 204 Android’s Message Class: Create a Time Update Message���������������������������������������������������������������� 206 Creating a Second Hand Timer: The updateTimeHandler������������������������������������������������������������������� 207 Watch Faces Time Calculation: Using System Time����������������������������������������������������� 212 Java System Class: Accessing Time in Milliseconds�������������������������������������������������������������������������� 212 Watch Face Seconds: Calculating Second Hand Movement��������������������������������������������������������������� 213 WatchFaces API: Core Methods to Implement�������������������������������������������������������������� 217 Android WatchFaceService Class: Core Constants����������������������������������������������������������������������������� 217 Adding WatchFaceService Constants: Burn-In and Low-Bit��������������������������������������������������������������� 219 Android WatchFaceService.Engine Class: Core Methods�������������������������������������������������������������������� 220 Adding WatchFaceService.Engine Methods: Core Function���������������������������������������������������������������� 222 Summary���������������������������������������������������������������������������������������������������������������������� 233 ■■Chapter 10: WatchFaces Vector Design: Using Vector Graphics for WatchFaces�������������������������������������������������������������������������������������������������� 235 WatchFace Painting Engine: Using the Paint Object����������������������������������������������������� 236 Android’s Paint Class: Paint Vector Shapes on the Canvas����������������������������������������������������������������� 236 WatchFaces Painting: Creating Watch Face Paint Objects����������������������������������������������������������������� 239 WatchFace Drawing Engine: The onDraw( ) Method���������������������������������������������������� 248 The Android Canvas Class: Your Canvas Drawing Methods���������������������������������������������������������������� 248 Drawing Your WatchFace: Using the drawLine( ) Method������������������������������������������������������������������� 248 Advanced Mode Support: Dynamic Paint Methods������������������������������������������������������� 256 Controlling Anti-Aliasing: Creating a setAntiAlias( ) Method��������������������������������������������������������������� 257 Controlling Burn-In: Creating a setBurnInProtect( ) Method���������������������������������������������������������������� 258 Ensuring Mode Support: An ensureModeSupport( ) Method��������������������������������������������������������������� 262 Invoking Mode Methods: onAmbientModeChanged( )�������������������������������������������������� 267 Returning to Interactive Mode: checkTimer( ) Method�������������������������������������������������� 270 Summary���������������������������������������������������������������������������������������������������������������������� 273 Contents xiii ■■Chapter 11: WatchFaces Bitmap Design: Using Raster Graphics for WatchFaces�������������������������������������������������������������������������������������������������� 275 Testing a WatchFaces Design: Using the Round AVD��������������������������������������������������� 276 Sending the Whole Second Delay to Your Handler Object������������������������������������������������������������������ 279 Setting a Time Object to a Current Time in the Draw Logic���������������������������������������������������������������� 280 Testing a WatchFace Design: Using a Square AVD������������������������������������������������������� 281 AVD Crashes: Can’t Connect and Not Responding Panels������������������������������������������������������������������ 285 Special Screen Modes: Testing the Low-Bit Ambient Mode��������������������������������������������������������������� 287 Special Screen Modes: Testing Low-Bit and Burn-In Modes�������������������������������������������������������������� 288 Android WindowInsets Class: Polling Screen Shape���������������������������������������������������� 293 Detecting WatchFace Shape: Using WindowInsets������������������������������������������������������ 293 Android Bitmap Class: Using Digital Image Assets������������������������������������������������������� 296 Android Resources Class: Using Your Res Folder��������������������������������������������������������� 297 Accessing Imagery: Using Bitmap and Resources������������������������������������������������������� 298 Android Drawable Class: Creating Drawable Objects��������������������������������������������������� 299 Loading the Drawable: Using the roundFlag Boolean��������������������������������������������������� 301 Android’s BitmapDrawable Class: Image Drawables���������������������������������������������������� 303 Using BitmapDrawable Object: Extract and Scale�������������������������������������������������������� 305 Scaling Bitmaps: Using the createScaledBitmap( ) Method��������������������������������������������������������������� 306 Testing Background Bitmaps: Round vs Square���������������������������������������������������������� 309 Solving the roundFlag Problem: onCreate( ) to onDraw( )������������������������������������������������������������������� 311 Optimizing Your onDraw( ): First Draw vs Every Draw����������������������������������������������������������������������� 313 Summary���������������������������������������������������������������������������������������������������������������������� 315 ■■Chapter 12: WatchFaces Digital Imaging: Developing Multiple Mode Assets������������������������������������������������������������������������������������������������������ 317 Ambient Mode Bitmap: GIMP Grayscale Image Mode�������������������������������������������������� 318 Low-Bit Mode Bitmaps: GIMP’s Posterize Algorithm���������������������������������������������������� 321 Dithering Low-Bit Imagery: Indexed Mode Conversion���������������������������������������������������������������������� 323 Creating a Burn-In Mode Bitmap: Using an Invert Algorithm�������������������������������������������������������������� 326 xiv Contents Multimodal Bitmaps: Changing Bitmaps Using Java���������������������������������������������������� 330 Installing Bitmap Objects into Your Low-Bit Ambient Mode��������������������������������������������������������������� 332 Refining Interactive Mode: Set Tick Marks Color to Black������������������������������������������������������������������ 334 Testing Interactive and Ambient Modes in the Square AVD���������������������������������������������������������������� 335 Android Wear Burn-In Mode: Bitmap and Java Code��������������������������������������������������� 338 Creating Burn-In Mode Bitmaps: GIMP Brightness-Contrast�������������������������������������������������������������� 338 Burn-In Protection in Java: if(enableBurnInAmbientMode)���������������������������������������������������������������� 340 Testing the Burn-In Protect Mode Bitmap and Java Code������������������������������������������������������������������ 342 Summary���������������������������������������������������������������������������������������������������������������������� 344 ■■Chapter 13: Watch Face Configuration Companion Activity: Google Mobile Services������������������������������������������������������������������������������������� 345 Creating a ProWatchFaceCompanionConfigActivity����������������������������������������������������� 346 The Mobile App: Adding Your Activity to AndroidManifest������������������������������������������������������������������ 346 The Java Class: Creating a WatchFace Companion Activity���������������������������������������������������������������� 348 The Wear App: Adding Companion Metadata to Manifest������������������������������������������������������������������� 352 Google Play Services: The GoogleApiClient Class��������������������������������������������������������� 353 Android’s GoogleApiClient: Using Google Mobile Services����������������������������������������������������������������� 353 Creating the Play Client: Coding Your onCreate( ) method����������������������������������������������������������������� 356 The WatchFaceCompanion Class: Configuration Constants���������������������������������������������������������������� 360 The ComponentName Class: Specify a Component���������������������������������������������������������������������������� 361 Setting Watch Face Identity: ComponentName and PeerId���������������������������������������������������������������� 362 The GoogleApiClient.Builder: Building a Google API Client����������������������������������������������������������������� 363 Building the GoogleApiClient: Using the Wearable API������������������������������������������������������������������������ 366 Starting and Stopping a Play Client: onStart( ) and onStop( )�������������������������������������������������������������� 367 Connect a Client: Creating the onConnected Method��������������������������������������������������� 368 Android Uri Class: Uniform Resource Identifier Objects��������������������������������������������������������������������� 369 Android Uri.Builder Class: Building an Android URI Object����������������������������������������������������������������� 370 Building a Uri for a Client: Finishing the onConnected( )��������������������������������������������������������������������� 371 Android’s GMS DataApi Interface: Configuring a Data API������������������������������������������������������������������ 372 Using the DataApi Class: Configuring the Wearable.API��������������������������������������������������������������������� 373 The Android PendingResult Class: Receiving the Result�������������������������������������������������������������������� 374 Contents xv Creating a Not Connected Dialog: Using AlertDialog���������������������������������������������������� 375 Android AlertDialog: Creating an Alert Dialog for Your App����������������������������������������������������������������� 377 Android AlertDialog.Builder: Building the Alert Dialog������������������������������������������������������������������������ 377 Using AlertDialog.Builder: Coding the AlertDialog System������������������������������������������������������������������ 378 Coding an onResult Method: DataItem and DataMap��������������������������������������������������� 380 Android’s DataItem Interface: A Foundation for Wear Data���������������������������������������������������������������� 381 Loading a DataItem Object: Using a getDataItem( ) Method�������������������������������������������������������������� 382 Android’s DataMapItem Class: A DataItem with a Map����������������������������������������������������������������������� 382 Using a DataMapItem Object: The fromDataItem( ) Method��������������������������������������������������������������� 383 Android Data Map������������������������������������������������������������������������������������������������������������������������������� 383 Creating a DataMap Object: Using a getDataMap( ) Method�������������������������������������������������������������� 385 Creating a Listener Service: onMessageReceived( )���������������������������������������������������� 386 The Android MessageEvent Class: Processing a Message����������������������������������������������������������������� 388 Implementing a MessageEvent Object: Extracting the Data��������������������������������������������������������������� 388 The ConnectionResult Class: Connecting to the Network������������������������������������������������������������������� 391 Implementing a ConnectionResult: Blocking a Connection���������������������������������������������������������������� 393 Summary���������������������������������������������������������������������������������������������������������������������� 394 ■■Chapter 14: Watch Face Configuration Companion Activity Utility and Wearable API���������������������������������������������������������������������������������������������� 395 The ProWatchFaceUtility Class: Managing the Data����������������������������������������������������� 396 Creating a ProWatchFaceUtility Class: Defining Constants����������������������������������������������������������������� 396 Loading DataItems into a DataMap: putConfigDataItem( )����������������������������������������������������������������� 399 Android PutDataMapRequest Class: Put in a Data Request���������������������������������������������������������������� 400 Using PutDataMapRequest to Put a Configuration DataItam�������������������������������������������������������������� 401 Android Wearable Class: Android’s Wearable APIs������������������������������������������������������� 403 Using the Wearable Class: Putting a DataApi Data Request��������������������������������������������������������������� 404 Using Android’s Node API: fetchConfigDataMap( ) Method���������������������������������������������������������������� 404 Using Wearable DataApi: DataItemResultCallback( ) Class����������������������������������������������������������������� 412 Replacing Changed Data: overwriteKeysInConfigDataMap���������������������������������������������������������������� 417 Updating a DataMap Object: onConfigDataMapFetched( )������������������������������������������������������������������ 419 xvi Contents Connect the Maps: Call the Utility from the Listener���������������������������������������������������� 422 Finishing the Configuration Companion: UI Design������������������������������������������������������ 424 Choosing Color Using the Spinner Widget: XML UI Layout������������������������������������������������������������������ 425 Setting the Spinner Widget: setUpColorPickerSelection( )������������������������������������������������������������������ 432 Setting Up a Spinner Listener: setUpColorPickerListener( )���������������������������������������������������������������� 435 Setting Up All Four Spinners: A setUpAllPickers( ) Method���������������������������������������������������������������� 439 Testing the WatchFaceCompanion Activity: Nexus 5���������������������������������������������������� 441 Summary���������������������������������������������������������������������������������������������������������������������� 449 ■■Chapter 15: Wearables Application Testing: Using Hardware Devices in Android Studio��������������������������������������������������������������������������������������������������� 451 Interfacing a Device with a Computer: USB Drivers����������������������������������������������������� 452 Installing Wear API: Linking Smartwatch with Phone��������������������������������������������������� 459 Downloading and Installing Wear API: Google Play Store������������������������������������������������������������������� 459 Setting Up the Smartwatch: Sony SmartWatch 3������������������������������������������������������������������������������� 460 Using ADB: Linking a Smartphone with the AVD����������������������������������������������������������� 461 Android Debug Bridge: Networking Hardware and Software������������������������������������������������������������� 462 Using AVD Inside Android Studio: ADB Port Forwarding��������������������������������������������������������������������� 463 Bluetooth Debugging: Linking to your Smartwatch������������������������������������������������������ 468 Smartwatch Set Up: Pair and Enable Bluetooth Debugging��������������������������������������������������������������� 468 Java Singleton: ProWatchFaceUtility( ) Constructor������������������������������������������������������ 470 Testing and Debugging: Creating Your APK Files��������������������������������������������������������� 472 The Android Studio Build System: An Overview���������������������������������������������������������������������������������� 472 Configuring Gradle Builds: Creating Different APK Types�������������������������������������������������������������������� 475 Building Your Project: Using the Gradle Build Engine������������������������������������������������������������������������� 480 Summary���������������������������������������������������������������������������������������������������������������������� 486 ■■Chapter 16: Wear API Deprecation: Updating Apps to Use New Classes or Methods�������������������������������������������������������������������������������������������������������� 487 Dealing with the Unexpected Update: Android 5.2������������������������������������������������������� 488 Dealing with Deprecation: getDrawable( ) and Time���������������������������������������������������� 490 Android’s Resources Class: Two getDrawable( ) Methods����������������������������������������������������������������� 491 The Resources.Theme Nested Class: Theme Attributes��������������������������������������������������������������������� 491 Contents xvii The ResourcesCompat Class: Backward Compatibility���������������������������������������������������������������������� 492 Dealing with Deprecated Classes: The Time Class����������������������������������������������������������������������������� 493 Upgrading Your Code: Calendar and getDrawable( )���������������������������������������������������� 496 Upgrading the Time Class Code: Using the Calendar Class���������������������������������������������������������������� 497 Upgrading timeZoneReceiver: The setTimeZone( ) Method���������������������������������������������������������������� 498 Upgrading the onDraw( ) Method: Using setTimeInMillis( )���������������������������������������������������������������� 499 Loading Your Time Variables: Using the get( ) Method����������������������������������������������������������������������� 501 Upgrade the onVisibilityChanged( ) Method: setTimeZone( )�������������������������������������������������������������� 501 Upgrading the Code: Using the getDrawable(int, Theme)������������������������������������������������������������������ 502 Solving IDE Problems Introduced by SDK Upgrades���������������������������������������������������� 504 Upgrading Gradle Files: Adding Build Definition Sections������������������������������������������������������������������ 505 Using Invalidate and Restart: Rebuilding Project Structure���������������������������������������������������������������� 509 Using Import Project��������������������������������������������������������������������������������������������������������������������������� 509 Re-creating a Project from Scratch: Copy Code and Assets��������������������������������������������������������������� 512 The Moral of the Story: Android Is More than Java or XML���������������������������������������������������������������� 519 Summary���������������������������������������������������������������������������������������������������������������������� 519 ■■Chapter 17: The Future of Android IoT APIs: Android TV, Glass, Auto, and Wear����������������������������������������������������������������������������������������������������������� 521 HD and UHD Android TV: The Opposite of Wear������������������������������������������������������������ 522 Android Auto: Android Apps for the Car Dashboard������������������������������������������������������ 525 Google Glass: Develop Apps for Smart Eyeglasses������������������������������������������������������ 529 Android Wear: Interesting API Elements to Explore������������������������������������������������������ 531 Detecting Location: GPS Data from Google Play Services������������������������������������������������������������������ 531 Voice Actions: Using Speech Recognition Technology������������������������������������������������������������������������ 535 Summary���������������������������������������������������������������������������������������������������������������������� 538 Index��������������������������������������������������������������������������������������������������������������������� 539 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 “mini-issue” 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 programming language that is used with Android (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 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 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 xix xx About the Author 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 About the Technical Reviewer Jeff Tang has successfully developed mobile, web, and enterprise apps on many platforms He became a Microsoftcertified 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 xxi 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 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 and HTML5 interactive content development platforms xxiii ... Theme provides the new Android conforming style to use for your Android apps Because the Android Wear SDK is a part of Android Studio (Android plus IntelliJ), these new themes will apply to pro. .. Defining the Wearable Material Theme: Using the Style Attribute Just as in the previous versions of Android, the material theme is defined using the Android Style attribute Examples of the various... using the Android OS without either of the APIs Let’s take a closer look at Glass development Google Glass’s Android Studio GDK: The Glass Development Kit The Google Android GDK is an add-on to the

Ngày đăng: 16/04/2019, 14:23

Mục lục

  • Contents at a Glance

  • About the Technical Reviewer

  • 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

        • Google Glass Development Kit: GDK for Android or Mirror

          • Google Glass’s Android Studio GDK: The Glass Development Kit

          • Develop Google Glass Apps Using Only the Android Environment

          • Using RESTful Services with Google Glass: The Mirror API

          • Hybrid Glass Applications: Mixing Android GDK and the Mirror API

          • True Android or Android Peripheral: Bluetooth Link

          • Wearable Apps Design: Android 5 Material Design

            • The Android Material Design Themes: Light and Dark

              • Defining the Wearable Material Theme: Using the Style Attribute

              • Defining the Wearable Material Theme Color Palette: The Item Tag

              • Customizing a Wearable Material Theme Status Bar: statusBarColor

              • Customizing a Wearable Material Theme: Individual View Themes

              • Android Material Design View Widgets: Lists and Cards

                • Android RecyclerView Class: Optimized (Recycled) List Viewing

                • Android CardView Class: The Index Card Organization Paradigm

                • Android Material Design Effects: Shadows and Animation

                  • Android Material Design 3D Effects: Automatic View Shadowing

                  • Android Material Design Animation: Touch Feedback for Your UI

                  • Android Material Design Transitions: Enhanced Activity Transitions

Tài liệu cùng người dùng

Tài liệu liên quan