Courtesy: GitHub

Android WatchFace Sample

A simple sample that demonstrates watch faces and complications for Wear 2.0.

Introduction

This sample demonstrates how to create watch faces for Android Wear.

The analog watch face covers best practices for Wear 2.0 including complications and a new configuration Activity for configuring both the watch face styles and the complications themselves. We recommend you reuse the configuration code and follow the UX patterns in your watch face's configuration activity. You can see a preview in the screenshots section of the README.

The analog watch face has three main classes:

  • AnalogComplicationWatchFaceService.java (watchface folder) - Renders the watch face and active complications.

  • AnalogComplicationConfigActivity.java (config folder) - Allows user to adjust watch face settings (arm and background colors) and watch face complications.

  • AnalogComplicationConfigData.java (models folder) - Populates the configuration screen in the previous step by returning a list of items. Each item represents something you can customize on the watch face. Items include a preview of the watch face (where you can pick your complications), marker color, background color, unread notifications, and background image complication. You can extend this and add your own.

We include several older watch faces to show best practices for Wear 1.0. If you are targeting older devices, you may want to review those watch faces (digital, opengl, and interactive).

The digital watch face includes a watch-side and phone-side configuration example.

This sample includes both a phone app and a wearable app.

Because watch face apps do not have a default Activity in their project, you will need to set your Configurations to "Do not launch Activity" for both the Wear and Application modules. If you are unsure how to do this, please review the "Run Starter project" section in the Google Watch Face Code Lab.

Pre-requisites

  • Android SDK 27
  • Android Build Tools v26.0.2
  • Android Support Repository

Screenshots

Screenshot Screenshot Screenshot Screenshot Screenshot Screenshot Screenshot Screenshot Screenshot

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-WatchFace

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.

License

Copyright 2017 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compile "com.android.support:support-v4:27.0.0"

compile "com.android.support:support-v13:27.0.0"

compile "com.android.support:cardview-v7:27.0.0"

compile "com.android.support:appcompat-v7:27.0.0"

compile 'com.google.android.support:wearable:2.1.0'

compile 'com.google.android.gms:play-services-wearable:11.6.0'

compile 'com.android.support:support-v13:27.0.0'

compileSdkVersion 27

minSdkVersion 18

targetSdkVersion 27

compile 'com.android.support:palette-v7:27.0.0'

compile 'com.android.support:wear:27.0.0'

compile 'com.google.android.gms:play-services-wearable:11.6.0'

compile 'com.android.support:support-v13:27.0.0'

compile 'com.google.android.support:wearable:2.1.0'

compileSdkVersion 27

versionCode 1

versionName "1.0"

minSdkVersion 24

targetSdkVersion 27

package com.example.android.wearable.watchface

uses-permission

  • android.permission.WAKE_LOCK

package com.example.android.wearable.watchface

uses-permission

  • android.permission.WAKE_LOCK
  • com.google.android.wearable.permission.RECEIVE_COMPLICATION_DATA

ComplicationHelperActivity

AnalogComplicationConfigActivity

DigitalWatchFaceWearableConfigActivity

ColorSelectionActivity

ComplicationHelperActivity

AnalogComplicationConfigActivity

The watch-side config activity for {@link AnalogComplicationWatchFaceService}, which allows for setting the left and right complications of watch face along with the second's marker color, background color, unread notifications toggle, and background complication image.

DigitalWatchFaceWearableConfigActivity

The watch-side config activity for {@link DigitalWatchFaceService}, which allows for setting the background color.

The layout of a color item including image and label.

The duration of the expand/shrink animation.

The ratio for the size of a circle in shrink state.

ColorSelectionActivity

Allows user to select color for something on the watch face (background, highlight,etc.) and saves it to {@link android.content.SharedPreferences} in {@link android.support.v7.widget.RecyclerView.Adapter}.

OpenGLWatchFaceService

AnalogComplicationWatchFaceService

InteractiveWatchFaceService

DigitalWatchFaceService

DigitalWatchFaceConfigListenerService

IncrementingNumberComplicationProviderService

OpenGLWatchFaceService

Sample watch face using OpenGL. The watch face is rendered using {@link Gles2ColoredTriangleList}s. The camera moves around in interactive mode and stops moving when the watch enters ambient mode.

Expected frame rate in interactive mode.

Z distance from the camera to the watchface.

How long each frame is displayed at expected frame rate.

Cycle time before the camera motion repeats.

Number of camera angles to precompute.

Projection transformation matrix. Converts from 3D to 2D.

View transformation matrices to use in interactive mode. Converts from world to camera- relative coordinates. One matrix per camera position.

The view transformation matrix to use in ambient mode

Model transformation matrices. Converts from model-relative coordinates to world coordinates. One matrix per degree of rotation.

Products of {@link #mViewMatrices} and {@link #mProjectionMatrix}. One matrix per camera position.

The product of {@link #mAmbientViewMatrix} and {@link #mProjectionMatrix}

Product of {@link #mModelMatrices}, {@link #mViewMatrices}, and {@link #mProjectionMatrix}.

Triangles for the 4 major ticks. These are grouped together to speed up rendering.

Triangles for the 8 minor ticks. These are grouped together to speed up rendering.

Triangle for the second hand.

Triangle for the minute hand.

Triangle for the hour hand.

Whether we've registered {@link #mTimeZoneReceiver}.

Creates a triangle for a hand on the watch face. * @param program program for drawing triangles @param width width of base of triangle @param length length of triangle @param color color in RGBA order, each in the range [0, 1]

Creates a triangle list for the major ticks on the watch face. * @param program program for drawing triangles

Creates a triangle list for the minor ticks on the watch face. * @param program program for drawing triangles

Destructively rotates the given coordinates in the XY plane about the origin by the given angle. * @param coords flattened 3D coordinates @param angleDegrees angle in degrees clockwise when viewed from negative infinity on the Z axis

AnalogComplicationWatchFaceService

Demonstrates two simple complications in a watch face.

Starts/stops the {@link #mUpdateTimeHandler} timer based on the state of the watch face.

Returns whether the {@link #mUpdateTimeHandler} timer should be running. The timer should only run in active mode.

InteractiveWatchFaceService

Demonstrates interactive watch face capabilities, i.e., touching the display and registering three different events: touch, touch-cancel and tap. The watch face UI will show the count of these events as they occur. See the {@code onTapCommand} below.

Whether the display supports fewer bits for each color in ambient mode. When true, we disable anti-aliasing in ambient mode.

Accepts tap events via WatchFaceStyle (setAcceptsTapEvents(true)).

Loads offsets / text size based on device type (square vs. round).

Draws background

Covers area under peek card

DigitalWatchFaceService

IMPORTANT NOTE: This watch face is optimized for Wear 1.x. If you want to see a Wear 2.0 watch face, check out AnalogComplicationWatchFaceService.java. * Sample digital watch face with blinking colons and seconds. In ambient mode, the seconds are replaced with an AM/PM indicator and the colons don't blink. On devices with low-bit ambient mode, the text is drawn without anti-aliasing in ambient mode. On devices which require burn-in protection, the hours are drawn in normal rather than bold. The time is drawn with less contrast and without seconds in mute mode.

Update rate in milliseconds for normal (not ambient and not mute) mode. We update twice a second to blink the colons.

Update rate in milliseconds for mute mode. We update every minute, like in ambient mode.

Alpha value for drawing time when in mute mode.

Alpha value for drawing time when not in mute mode.

How often {@link #mUpdateTimeHandler} ticks in milliseconds.

Handler to update the time periodically in interactive mode.

Handles time zone and locale changes.

Unregistering an unregistered receiver throws an exception. Keep track of the registration state to prevent that.

Whether the display supports fewer bits for each color in ambient mode. When true, we disable anti-aliasing in ambient mode.

Starts the {@link #mUpdateTimeHandler} timer if it should be running and isn't currently or stops it if it shouldn't be running but currently is.

Returns whether the {@link #mUpdateTimeHandler} timer should be running. The timer should only run when we're visible and in interactive mode.

Updates the color of a UI item according to the given {@code configKey}. Does nothing if {@code configKey} isn't recognized. * @return whether UI has been updated

DigitalWatchFaceConfigListenerService

A {@link WearableListenerService} listening for {@link DigitalWatchFaceService} config messages and updating the config {@link com.google.android.gms.wearable.DataItem} accordingly.

IncrementingNumberComplicationProviderService

Example Watch Face Complication data provider provides a number that can be incremented on tap.

ComplicationToggleReceiver

ComplicationToggleReceiver

Simple {@link BroadcastReceiver} subclass for asynchronously incrementing an integer for any complication id triggered via TapAction on complication. Also, provides static method to create a {@link PendingIntent} that triggers this receiver.

Returns a pending intent, suitable for use as a tap intent, that causes a complication to be toggled and updated.

Returns the key for the shared preference used to hold the current state of a given complication.