├── .gitignore
├── 1-base
├── build.gradle
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── android
│ │ └── example
│ │ └── watchface
│ │ └── MyWatchFaceService.java
│ └── res
│ ├── drawable-nodpi
│ └── preview_analog.png
│ ├── values
│ ├── ids.xml
│ └── strings.xml
│ └── xml
│ └── watch_face.xml
├── 2-background
├── build.gradle
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── android
│ │ └── example
│ │ └── watchface
│ │ └── MyWatchFaceService.java
│ └── res
│ ├── drawable-nodpi
│ ├── custom_background.jpg
│ └── preview_analog.png
│ ├── values
│ ├── ids.xml
│ └── strings.xml
│ └── xml
│ └── watch_face.xml
├── 3-hands
├── build.gradle
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── android
│ │ └── example
│ │ └── watchface
│ │ └── MyWatchFaceService.java
│ └── res
│ ├── drawable-nodpi
│ ├── custom_background.jpg
│ └── preview_analog.png
│ ├── values
│ ├── ids.xml
│ └── strings.xml
│ └── xml
│ └── watch_face.xml
├── 4-ambient
├── build.gradle
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── android
│ │ └── example
│ │ └── watchface
│ │ └── MyWatchFaceService.java
│ └── res
│ ├── drawable-nodpi
│ ├── custom_background.jpg
│ └── preview_analog.png
│ ├── values
│ ├── ids.xml
│ └── strings.xml
│ └── xml
│ └── watch_face.xml
├── 5-palette
├── build.gradle
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── android
│ │ └── example
│ │ └── watchface
│ │ └── MyWatchFaceService.java
│ └── res
│ ├── drawable-nodpi
│ ├── custom_background.jpg
│ ├── custom_background2.jpg
│ └── preview_analog.png
│ ├── values
│ ├── ids.xml
│ └── strings.xml
│ └── xml
│ └── watch_face.xml
├── CONTRIB.md
├── LICENSE
├── NOTICE
├── README.md
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | /local.properties
3 | *.iml
4 | /.idea
5 | .DS_Store
6 | **/build
7 |
--------------------------------------------------------------------------------
/1-base/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | apply plugin: 'com.android.application'
18 |
19 |
20 | android {
21 | compileSdkVersion 21
22 | buildToolsVersion "21.1.2"
23 |
24 | defaultConfig {
25 | applicationId "com.android.example.watchface"
26 | minSdkVersion 21
27 | targetSdkVersion 21
28 | versionCode 1
29 | versionName "1.0"
30 | }
31 | buildTypes {
32 | release {
33 | minifyEnabled false
34 | proguardFiles getDefaultProguardFile('proguard-android.txt')
35 | }
36 | }
37 | }
38 |
39 | dependencies {
40 | compile 'com.google.android.support:wearable:1.1.0'
41 | compile 'com.google.android.gms:play-services-wearable:6.5.87'
42 | }
43 |
--------------------------------------------------------------------------------
/1-base/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
33 |
37 |
40 |
43 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/1-base/src/main/java/com/android/example/watchface/MyWatchFaceService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.android.example.watchface;
18 |
19 | import android.content.BroadcastReceiver;
20 | import android.content.Context;
21 | import android.content.Intent;
22 | import android.content.IntentFilter;
23 | import android.graphics.Canvas;
24 | import android.graphics.Color;
25 | import android.graphics.Paint;
26 | import android.graphics.Rect;
27 | import android.os.Handler;
28 | import android.os.Message;
29 | import android.support.wearable.watchface.CanvasWatchFaceService;
30 | import android.support.wearable.watchface.WatchFaceStyle;
31 | import android.text.format.Time;
32 | import android.view.SurfaceHolder;
33 |
34 | import java.util.TimeZone;
35 | import java.util.concurrent.TimeUnit;
36 |
37 | /**
38 | * Analog watch face with a ticking second hand. In ambient mode, the second hand isn't shown. On
39 | * devices with low-bit ambient mode, the hands are drawn without anti-aliasing in ambient mode.
40 | */
41 | public class MyWatchFaceService extends CanvasWatchFaceService {
42 |
43 | /**
44 | * Update rate in milliseconds for interactive mode. We update once a second to advance the
45 | * second hand.
46 | */
47 | private static final long INTERACTIVE_UPDATE_RATE_MS = TimeUnit.SECONDS.toMillis(1);
48 |
49 | @Override
50 | public Engine onCreateEngine() {
51 | return new Engine();
52 | }
53 |
54 | private class Engine extends CanvasWatchFaceService.Engine {
55 |
56 | /* Handler to update the time once a second in interactive mode. */
57 | private final Handler mUpdateTimeHandler = new Handler() {
58 | @Override
59 | public void handleMessage(Message message) {
60 | if (R.id.message_update == message.what) {
61 | invalidate();
62 | if (shouldTimerBeRunning()) {
63 | long timeMs = System.currentTimeMillis();
64 | long delayMs = INTERACTIVE_UPDATE_RATE_MS
65 | - (timeMs % INTERACTIVE_UPDATE_RATE_MS);
66 | mUpdateTimeHandler.sendEmptyMessageDelayed(R.id.message_update, delayMs);
67 | }
68 | }
69 | }
70 | };
71 |
72 | private final BroadcastReceiver mTimeZoneReceiver = new BroadcastReceiver() {
73 | @Override
74 | public void onReceive(Context context, Intent intent) {
75 | mTime.clear(intent.getStringExtra("time-zone"));
76 | mTime.setToNow();
77 | }
78 | };
79 |
80 | private boolean mRegisteredTimeZoneReceiver = false;
81 |
82 | private static final float STROKE_WIDTH = 3f;
83 |
84 | private Time mTime;
85 |
86 | private Paint mBackgroundPaint;
87 | private Paint mHandPaint;
88 |
89 | private boolean mAmbient;
90 |
91 | private float mHourHandLength;
92 | private float mMinuteHandLength;
93 | private float mSecondHandLength;
94 |
95 | private int mWidth;
96 | private int mHeight;
97 | private float mCenterX;
98 | private float mCenterY;
99 |
100 | @Override
101 | public void onCreate(SurfaceHolder holder) {
102 | super.onCreate(holder);
103 |
104 | setWatchFaceStyle(new WatchFaceStyle.Builder(MyWatchFaceService.this)
105 | .setCardPeekMode(WatchFaceStyle.PEEK_MODE_SHORT)
106 | .setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
107 | .setShowSystemUiTime(false)
108 | .build());
109 |
110 | mBackgroundPaint = new Paint();
111 | mBackgroundPaint.setColor(Color.BLACK);
112 |
113 | mHandPaint = new Paint();
114 | mHandPaint.setColor(Color.WHITE);
115 | mHandPaint.setStrokeWidth(STROKE_WIDTH);
116 | mHandPaint.setAntiAlias(true);
117 | mHandPaint.setStrokeCap(Paint.Cap.ROUND);
118 |
119 | mTime = new Time();
120 | }
121 |
122 | @Override
123 | public void onDestroy() {
124 | mUpdateTimeHandler.removeMessages(R.id.message_update);
125 | super.onDestroy();
126 | }
127 |
128 | @Override
129 | public void onTimeTick() {
130 | super.onTimeTick();
131 | invalidate();
132 | }
133 |
134 | @Override
135 | public void onAmbientModeChanged(boolean inAmbientMode) {
136 | super.onAmbientModeChanged(inAmbientMode);
137 | if (mAmbient != inAmbientMode) {
138 | mAmbient = inAmbientMode;
139 | invalidate();
140 | }
141 |
142 | /*
143 | * Whether the timer should be running depends on whether we're visible (as well as
144 | * whether we're in ambient mode), so we may need to start or stop the timer.
145 | */
146 | updateTimer();
147 | }
148 |
149 | @Override
150 | public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
151 | super.onSurfaceChanged(holder, format, width, height);
152 | mWidth = width;
153 | mHeight = height;
154 | /*
155 | * Find the coordinates of the center point on the screen.
156 | * Ignore the window insets so that, on round watches
157 | * with a "chin", the watch face is centered on the entire screen,
158 | * not just the usable portion.
159 | */
160 | mCenterX = mWidth / 2f;
161 | mCenterY = mHeight / 2f;
162 | /*
163 | * Calculate the lengths of the watch hands and store them in member variables.
164 | */
165 | mHourHandLength = mCenterX - 80;
166 | mMinuteHandLength = mCenterX - 40;
167 | mSecondHandLength = mCenterX - 20;
168 | }
169 |
170 | @Override
171 | public void onDraw(Canvas canvas, Rect bounds) {
172 | mTime.setToNow();
173 |
174 | // Draw the background.
175 | canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
176 |
177 | /*
178 | * These calculations reflect the rotation in degrees per unit of
179 | * time, e.g. 360 / 60 = 6 and 360 / 12 = 30
180 | */
181 | final float secondsRotation = mTime.second * 6f;
182 | final float minutesRotation = mTime.minute * 6f;
183 | // account for the offset of the hour hand due to minutes of the hour.
184 | final float hourHandOffset = mTime.minute / 2f;
185 | final float hoursRotation = (mTime.hour * 30) + hourHandOffset;
186 |
187 | // save the canvas state before we begin to rotate it
188 | canvas.save();
189 |
190 | canvas.rotate(hoursRotation, mCenterX, mCenterY);
191 | canvas.drawLine(mCenterX, mCenterY, mCenterX, mCenterY - mHourHandLength, mHandPaint);
192 |
193 | canvas.rotate(minutesRotation - hoursRotation, mCenterX, mCenterY);
194 | canvas.drawLine(mCenterX, mCenterY, mCenterX, mCenterY - mMinuteHandLength, mHandPaint);
195 |
196 | if (!mAmbient) {
197 | canvas.rotate(secondsRotation - minutesRotation, mCenterX, mCenterY);
198 | canvas.drawLine(mCenterX, mCenterY, mCenterX, mCenterY - mSecondHandLength,
199 | mHandPaint);
200 | }
201 | // restore the canvas' original orientation.
202 | canvas.restore();
203 | }
204 |
205 | @Override
206 | public void onVisibilityChanged(boolean visible) {
207 | super.onVisibilityChanged(visible);
208 |
209 | if (visible) {
210 | registerReceiver();
211 |
212 | // Update time zone in case it changed while we weren't visible.
213 | mTime.clear(TimeZone.getDefault().getID());
214 | mTime.setToNow();
215 | } else {
216 | unregisterReceiver();
217 | }
218 |
219 | /*
220 | * Whether the timer should be running depends on whether we're visible
221 | * (as well as whether we're in ambient mode),
222 | * so we may need to start or stop the timer.
223 | */
224 | updateTimer();
225 | }
226 |
227 | private void registerReceiver() {
228 | if (mRegisteredTimeZoneReceiver) {
229 | return;
230 | }
231 | mRegisteredTimeZoneReceiver = true;
232 | IntentFilter filter = new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED);
233 | MyWatchFaceService.this.registerReceiver(mTimeZoneReceiver, filter);
234 | }
235 |
236 | private void unregisterReceiver() {
237 | if (!mRegisteredTimeZoneReceiver) {
238 | return;
239 | }
240 | mRegisteredTimeZoneReceiver = false;
241 | MyWatchFaceService.this.unregisterReceiver(mTimeZoneReceiver);
242 | }
243 |
244 | private void updateTimer() {
245 | mUpdateTimeHandler.removeMessages(R.id.message_update);
246 | if (shouldTimerBeRunning()) {
247 | mUpdateTimeHandler.sendEmptyMessage(R.id.message_update);
248 | }
249 | }
250 |
251 | /**
252 | * Returns whether the {@link #mUpdateTimeHandler} timer should be running. The timer
253 | * should only run when we're visible and in interactive mode.
254 | */
255 | private boolean shouldTimerBeRunning() {
256 | return isVisible() && !isInAmbientMode();
257 | }
258 | }
259 | }
260 |
--------------------------------------------------------------------------------
/1-base/src/main/res/drawable-nodpi/preview_analog.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-codelab-watchface/5fc095fbb95a7c3ed5b17dfc905f9ed384b4b266/1-base/src/main/res/drawable-nodpi/preview_analog.png
--------------------------------------------------------------------------------
/1-base/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/1-base/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 | Codelab Watchface
19 | Analog Codelab Watchface
20 |
21 |
--------------------------------------------------------------------------------
/1-base/src/main/res/xml/watch_face.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/2-background/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | apply plugin: 'com.android.application'
18 |
19 |
20 | android {
21 | compileSdkVersion 21
22 | buildToolsVersion "21.1.2"
23 |
24 | defaultConfig {
25 | applicationId "com.android.example.watchface"
26 | minSdkVersion 21
27 | targetSdkVersion 21
28 | versionCode 1
29 | versionName "1.0"
30 | }
31 | buildTypes {
32 | release {
33 | minifyEnabled false
34 | proguardFiles getDefaultProguardFile('proguard-android.txt')
35 | }
36 | }
37 | }
38 |
39 | dependencies {
40 | compile 'com.google.android.support:wearable:1.1.0'
41 | compile 'com.google.android.gms:play-services-wearable:6.5.87'
42 | }
43 |
--------------------------------------------------------------------------------
/2-background/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
33 |
37 |
40 |
43 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/2-background/src/main/java/com/android/example/watchface/MyWatchFaceService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.android.example.watchface;
18 |
19 | import android.content.BroadcastReceiver;
20 | import android.content.Context;
21 | import android.content.Intent;
22 | import android.content.IntentFilter;
23 | import android.graphics.Bitmap;
24 | import android.graphics.BitmapFactory;
25 | import android.graphics.Canvas;
26 | import android.graphics.Color;
27 | import android.graphics.Paint;
28 | import android.graphics.Rect;
29 | import android.os.Handler;
30 | import android.os.Message;
31 | import android.support.wearable.watchface.CanvasWatchFaceService;
32 | import android.support.wearable.watchface.WatchFaceStyle;
33 | import android.text.format.Time;
34 | import android.view.SurfaceHolder;
35 |
36 | import java.util.TimeZone;
37 | import java.util.concurrent.TimeUnit;
38 |
39 | /**
40 | * Analog watch face with a ticking second hand. In ambient mode, the second hand isn't shown. On
41 | * devices with low-bit ambient mode, the hands are drawn without anti-aliasing in ambient mode.
42 | */
43 | public class MyWatchFaceService extends CanvasWatchFaceService {
44 |
45 | /**
46 | * Update rate in milliseconds for interactive mode. We update once a second to advance the
47 | * second hand.
48 | */
49 | private static final long INTERACTIVE_UPDATE_RATE_MS = TimeUnit.SECONDS.toMillis(1);
50 |
51 | @Override
52 | public Engine onCreateEngine() {
53 | return new Engine();
54 | }
55 |
56 | private class Engine extends CanvasWatchFaceService.Engine {
57 |
58 | /* Handler to update the time once a second in interactive mode. */
59 | private final Handler mUpdateTimeHandler = new Handler() {
60 | @Override
61 | public void handleMessage(Message message) {
62 | if (R.id.message_update == message.what) {
63 | invalidate();
64 | if (shouldTimerBeRunning()) {
65 | long timeMs = System.currentTimeMillis();
66 | long delayMs = INTERACTIVE_UPDATE_RATE_MS
67 | - (timeMs % INTERACTIVE_UPDATE_RATE_MS);
68 | mUpdateTimeHandler.sendEmptyMessageDelayed(R.id.message_update, delayMs);
69 | }
70 | }
71 | }
72 | };
73 |
74 | private final BroadcastReceiver mTimeZoneReceiver = new BroadcastReceiver() {
75 | @Override
76 | public void onReceive(Context context, Intent intent) {
77 | mTime.clear(intent.getStringExtra("time-zone"));
78 | mTime.setToNow();
79 | }
80 | };
81 |
82 | private boolean mRegisteredTimeZoneReceiver = false;
83 |
84 | private static final float STROKE_WIDTH = 3f;
85 |
86 | private Time mTime;
87 |
88 | private Paint mBackgroundPaint;
89 | private Paint mHandPaint;
90 |
91 | private boolean mAmbient;
92 |
93 | private Bitmap mBackgroundBitmap;
94 |
95 | private float mHourHandLength;
96 | private float mMinuteHandLength;
97 | private float mSecondHandLength;
98 |
99 | private int mWidth;
100 | private int mHeight;
101 | private float mCenterX;
102 | private float mCenterY;
103 | private float mScale = 1;
104 |
105 | @Override
106 | public void onCreate(SurfaceHolder holder) {
107 | super.onCreate(holder);
108 |
109 | setWatchFaceStyle(new WatchFaceStyle.Builder(MyWatchFaceService.this)
110 | .setCardPeekMode(WatchFaceStyle.PEEK_MODE_SHORT)
111 | .setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
112 | .setShowSystemUiTime(false)
113 | .build());
114 |
115 | mBackgroundPaint = new Paint();
116 | mBackgroundPaint.setColor(Color.BLACK);
117 |
118 | final int backgroundResId = R.drawable.custom_background;
119 |
120 | mBackgroundBitmap = BitmapFactory.decodeResource(getResources(), backgroundResId);
121 |
122 | mHandPaint = new Paint();
123 | mHandPaint.setColor(Color.WHITE);
124 | mHandPaint.setStrokeWidth(STROKE_WIDTH);
125 | mHandPaint.setAntiAlias(true);
126 | mHandPaint.setStrokeCap(Paint.Cap.ROUND);
127 |
128 | mTime = new Time();
129 | }
130 |
131 | @Override
132 | public void onDestroy() {
133 | mUpdateTimeHandler.removeMessages(R.id.message_update);
134 | super.onDestroy();
135 | }
136 |
137 | @Override
138 | public void onTimeTick() {
139 | super.onTimeTick();
140 | invalidate();
141 | }
142 |
143 | @Override
144 | public void onAmbientModeChanged(boolean inAmbientMode) {
145 | super.onAmbientModeChanged(inAmbientMode);
146 | if (mAmbient != inAmbientMode) {
147 | mAmbient = inAmbientMode;
148 | invalidate();
149 | }
150 |
151 | /*
152 | * Whether the timer should be running depends on whether we're visible (as well as
153 | * whether we're in ambient mode), so we may need to start or stop the timer.
154 | */
155 | updateTimer();
156 | }
157 |
158 | @Override
159 | public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
160 | super.onSurfaceChanged(holder, format, width, height);
161 | mWidth = width;
162 | mHeight = height;
163 | /*
164 | * Find the coordinates of the center point on the screen.
165 | * Ignore the window insets so that, on round watches
166 | * with a "chin", the watch face is centered on the entire screen,
167 | * not just the usable portion.
168 | */
169 | mCenterX = mWidth / 2f;
170 | mCenterY = mHeight / 2f;
171 | mScale = ((float) width) / (float) mBackgroundBitmap.getWidth();
172 | /*
173 | * Calculate the lengths of the watch hands and store them in member variables.
174 | */
175 | mHourHandLength = mCenterX - 80;
176 | mMinuteHandLength = mCenterX - 40;
177 | mSecondHandLength = mCenterX - 20;
178 |
179 | mBackgroundBitmap = Bitmap.createScaledBitmap(mBackgroundBitmap,
180 | (int) (mBackgroundBitmap.getWidth() * mScale),
181 | (int) (mBackgroundBitmap.getHeight() * mScale), true);
182 | }
183 |
184 | @Override
185 | public void onDraw(Canvas canvas, Rect bounds) {
186 | mTime.setToNow();
187 |
188 | // Draw the background.
189 | canvas.drawBitmap(mBackgroundBitmap, 0, 0, mBackgroundPaint);
190 |
191 | /*
192 | * These calculations reflect the rotation in degrees per unit of
193 | * time, e.g. 360 / 60 = 6 and 360 / 12 = 30
194 | */
195 | final float secondsRotation = mTime.second * 6f;
196 | final float minutesRotation = mTime.minute * 6f;
197 | // account for the offset of the hour hand due to minutes of the hour.
198 | final float hourHandOffset = mTime.minute / 2f;
199 | final float hoursRotation = (mTime.hour * 30) + hourHandOffset;
200 |
201 | // save the canvas state before we begin to rotate it
202 | canvas.save();
203 |
204 | canvas.rotate(hoursRotation, mCenterX, mCenterY);
205 | canvas.drawLine(mCenterX, mCenterY, mCenterX, mCenterY - mHourHandLength, mHandPaint);
206 |
207 | canvas.rotate(minutesRotation - hoursRotation, mCenterX, mCenterY);
208 | canvas.drawLine(mCenterX, mCenterY, mCenterX, mCenterY - mMinuteHandLength, mHandPaint);
209 |
210 | if (!mAmbient) {
211 | canvas.rotate(secondsRotation - minutesRotation, mCenterX, mCenterY);
212 | canvas.drawLine(mCenterX, mCenterY, mCenterX, mCenterY - mSecondHandLength,
213 | mHandPaint);
214 | }
215 | // restore the canvas' original orientation.
216 | canvas.restore();
217 | }
218 |
219 | @Override
220 | public void onVisibilityChanged(boolean visible) {
221 | super.onVisibilityChanged(visible);
222 |
223 | if (visible) {
224 | registerReceiver();
225 |
226 | // Update time zone in case it changed while we weren't visible.
227 | mTime.clear(TimeZone.getDefault().getID());
228 | mTime.setToNow();
229 | } else {
230 | unregisterReceiver();
231 | }
232 |
233 | /*
234 | * Whether the timer should be running depends on whether we're visible
235 | * (as well as whether we're in ambient mode),
236 | * so we may need to start or stop the timer.
237 | */
238 | updateTimer();
239 | }
240 |
241 | private void registerReceiver() {
242 | if (mRegisteredTimeZoneReceiver) {
243 | return;
244 | }
245 | mRegisteredTimeZoneReceiver = true;
246 | IntentFilter filter = new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED);
247 | MyWatchFaceService.this.registerReceiver(mTimeZoneReceiver, filter);
248 | }
249 |
250 | private void unregisterReceiver() {
251 | if (!mRegisteredTimeZoneReceiver) {
252 | return;
253 | }
254 | mRegisteredTimeZoneReceiver = false;
255 | MyWatchFaceService.this.unregisterReceiver(mTimeZoneReceiver);
256 | }
257 |
258 | private void updateTimer() {
259 | mUpdateTimeHandler.removeMessages(R.id.message_update);
260 | if (shouldTimerBeRunning()) {
261 | mUpdateTimeHandler.sendEmptyMessage(R.id.message_update);
262 | }
263 | }
264 |
265 | /**
266 | * Returns whether the {@link #mUpdateTimeHandler} timer should be running. The timer
267 | * should only run when we're visible and in interactive mode.
268 | */
269 | private boolean shouldTimerBeRunning() {
270 | return isVisible() && !isInAmbientMode();
271 | }
272 | }
273 | }
274 |
--------------------------------------------------------------------------------
/2-background/src/main/res/drawable-nodpi/custom_background.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-codelab-watchface/5fc095fbb95a7c3ed5b17dfc905f9ed384b4b266/2-background/src/main/res/drawable-nodpi/custom_background.jpg
--------------------------------------------------------------------------------
/2-background/src/main/res/drawable-nodpi/preview_analog.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-codelab-watchface/5fc095fbb95a7c3ed5b17dfc905f9ed384b4b266/2-background/src/main/res/drawable-nodpi/preview_analog.png
--------------------------------------------------------------------------------
/2-background/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/2-background/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 | Codelab Watchface
19 | Analog Codelab Watchface
20 |
21 |
--------------------------------------------------------------------------------
/2-background/src/main/res/xml/watch_face.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/3-hands/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | apply plugin: 'com.android.application'
18 |
19 |
20 | android {
21 | compileSdkVersion 21
22 | buildToolsVersion "21.1.2"
23 |
24 | defaultConfig {
25 | applicationId "com.android.example.watchface"
26 | minSdkVersion 21
27 | targetSdkVersion 21
28 | versionCode 1
29 | versionName "1.0"
30 | }
31 | buildTypes {
32 | release {
33 | minifyEnabled false
34 | proguardFiles getDefaultProguardFile('proguard-android.txt')
35 | }
36 | }
37 | }
38 |
39 | dependencies {
40 | compile 'com.google.android.support:wearable:1.1.0'
41 | compile 'com.google.android.gms:play-services-wearable:6.5.87'
42 | }
43 |
--------------------------------------------------------------------------------
/3-hands/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
33 |
37 |
40 |
43 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/3-hands/src/main/java/com/android/example/watchface/MyWatchFaceService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.android.example.watchface;
18 |
19 | import android.content.BroadcastReceiver;
20 | import android.content.Context;
21 | import android.content.Intent;
22 | import android.content.IntentFilter;
23 | import android.graphics.Bitmap;
24 | import android.graphics.BitmapFactory;
25 | import android.graphics.Canvas;
26 | import android.graphics.Color;
27 | import android.graphics.Paint;
28 | import android.graphics.Rect;
29 | import android.os.Handler;
30 | import android.os.Message;
31 | import android.support.wearable.watchface.CanvasWatchFaceService;
32 | import android.support.wearable.watchface.WatchFaceStyle;
33 | import android.text.format.Time;
34 | import android.view.SurfaceHolder;
35 |
36 | import java.util.TimeZone;
37 | import java.util.concurrent.TimeUnit;
38 |
39 | /**
40 | * Analog watch face with a ticking second hand. In ambient mode, the second hand isn't shown. On
41 | * devices with low-bit ambient mode, the hands are drawn without anti-aliasing in ambient mode.
42 | */
43 | public class MyWatchFaceService extends CanvasWatchFaceService {
44 |
45 | /**
46 | * Update rate in milliseconds for interactive mode. We update once a second to advance the
47 | * second hand.
48 | */
49 | private static final long INTERACTIVE_UPDATE_RATE_MS = TimeUnit.SECONDS.toMillis(1);
50 |
51 | @Override
52 | public Engine onCreateEngine() {
53 | return new Engine();
54 | }
55 |
56 | private class Engine extends CanvasWatchFaceService.Engine {
57 |
58 | /* Handler to update the time once a second in interactive mode. */
59 | private final Handler mUpdateTimeHandler = new Handler() {
60 | @Override
61 | public void handleMessage(Message message) {
62 | if (R.id.message_update == message.what) {
63 | invalidate();
64 | if (shouldTimerBeRunning()) {
65 | long timeMs = System.currentTimeMillis();
66 | long delayMs = INTERACTIVE_UPDATE_RATE_MS
67 | - (timeMs % INTERACTIVE_UPDATE_RATE_MS);
68 | mUpdateTimeHandler.sendEmptyMessageDelayed(R.id.message_update, delayMs);
69 | }
70 | }
71 | }
72 | };
73 |
74 | private final BroadcastReceiver mTimeZoneReceiver = new BroadcastReceiver() {
75 | @Override
76 | public void onReceive(Context context, Intent intent) {
77 | mTime.clear(intent.getStringExtra("time-zone"));
78 | mTime.setToNow();
79 | }
80 | };
81 |
82 | private boolean mRegisteredTimeZoneReceiver = false;
83 |
84 | // Feel free to change these values and see what happens to the watch face.
85 | private static final float HAND_END_CAP_RADIUS = 4f;
86 | private static final float STROKE_WIDTH = 4f;
87 | private static final int SHADOW_RADIUS = 6;
88 |
89 | private Time mTime;
90 |
91 | private Paint mBackgroundPaint;
92 | private Paint mHandPaint;
93 |
94 | private boolean mAmbient;
95 |
96 | private Bitmap mBackgroundBitmap;
97 |
98 | private float mHourHandLength;
99 | private float mMinuteHandLength;
100 | private float mSecondHandLength;
101 |
102 | private int mWidth;
103 | private int mHeight;
104 | private float mCenterX;
105 | private float mCenterY;
106 | private float mScale = 1;
107 |
108 | @Override
109 | public void onCreate(SurfaceHolder holder) {
110 | super.onCreate(holder);
111 |
112 | setWatchFaceStyle(new WatchFaceStyle.Builder(MyWatchFaceService.this)
113 | .setCardPeekMode(WatchFaceStyle.PEEK_MODE_SHORT)
114 | .setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
115 | .setShowSystemUiTime(false)
116 | .build());
117 |
118 | mBackgroundPaint = new Paint();
119 | mBackgroundPaint.setColor(Color.BLACK);
120 |
121 | final int backgroundResId = R.drawable.custom_background;
122 |
123 | mBackgroundBitmap = BitmapFactory.decodeResource(getResources(), backgroundResId);
124 | mHandPaint = new Paint();
125 | mHandPaint.setColor(Color.WHITE);
126 | mHandPaint.setStrokeWidth(STROKE_WIDTH);
127 | mHandPaint.setAntiAlias(true);
128 | mHandPaint.setStrokeCap(Paint.Cap.ROUND);
129 | mHandPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, Color.BLACK);
130 | mHandPaint.setStyle(Paint.Style.STROKE);
131 |
132 | mTime = new Time();
133 | }
134 |
135 | @Override
136 | public void onDestroy() {
137 | mUpdateTimeHandler.removeMessages(R.id.message_update);
138 | super.onDestroy();
139 | }
140 |
141 | @Override
142 | public void onTimeTick() {
143 | super.onTimeTick();
144 | invalidate();
145 | }
146 |
147 | @Override
148 | public void onAmbientModeChanged(boolean inAmbientMode) {
149 | super.onAmbientModeChanged(inAmbientMode);
150 | if (mAmbient != inAmbientMode) {
151 | mAmbient = inAmbientMode;
152 | invalidate();
153 | }
154 |
155 | /*
156 | * Whether the timer should be running depends on whether we're visible (as well as
157 | * whether we're in ambient mode), so we may need to start or stop the timer.
158 | */
159 | updateTimer();
160 | }
161 |
162 | @Override
163 | public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
164 | super.onSurfaceChanged(holder, format, width, height);
165 | mWidth = width;
166 | mHeight = height;
167 | /*
168 | * Find the coordinates of the center point on the screen.
169 | * Ignore the window insets so that, on round watches
170 | * with a "chin", the watch face is centered on the entire screen,
171 | * not just the usable portion.
172 | */
173 | mCenterX = mWidth / 2f;
174 | mCenterY = mHeight / 2f;
175 | mScale = ((float) width) / (float) mBackgroundBitmap.getWidth();
176 | /*
177 | * Calculate the lengths of the watch hands and store them in member variables.
178 | */
179 | mHourHandLength = mCenterX * 0.5f;
180 | mMinuteHandLength = mCenterX * 0.7f;
181 | mSecondHandLength = mCenterX * 0.9f;
182 |
183 | mBackgroundBitmap = Bitmap.createScaledBitmap(mBackgroundBitmap,
184 | (int) (mBackgroundBitmap.getWidth() * mScale),
185 | (int) (mBackgroundBitmap.getHeight() * mScale), true);
186 | }
187 |
188 | @Override
189 | public void onDraw(Canvas canvas, Rect bounds) {
190 | mTime.setToNow();
191 |
192 | // Draw the background.
193 | canvas.drawBitmap(mBackgroundBitmap, 0, 0, mBackgroundPaint);
194 |
195 | /*
196 | * These calculations reflect the rotation in degrees per unit of
197 | * time, e.g. 360 / 60 = 6 and 360 / 12 = 30
198 | */
199 | final float secondsRotation = mTime.second * 6f;
200 | final float minutesRotation = mTime.minute * 6f;
201 | // account for the offset of the hour hand due to minutes of the hour.
202 | final float hourHandOffset = mTime.minute / 2f;
203 | final float hoursRotation = (mTime.hour * 30) + hourHandOffset;
204 |
205 | // save the canvas state before we begin to rotate it
206 | canvas.save();
207 |
208 | canvas.rotate(hoursRotation, mCenterX, mCenterY);
209 | drawHand(canvas, mHourHandLength);
210 |
211 | canvas.rotate(minutesRotation - hoursRotation, mCenterX, mCenterY);
212 | drawHand(canvas, mMinuteHandLength);
213 |
214 | canvas.rotate(secondsRotation - minutesRotation, mCenterX, mCenterY);
215 | canvas.drawLine(mCenterX, mCenterY - HAND_END_CAP_RADIUS, mCenterX,
216 | mCenterY - mSecondHandLength, mHandPaint);
217 | canvas.drawCircle(mCenterX, mCenterY, HAND_END_CAP_RADIUS, mHandPaint);
218 | // restore the canvas' original orientation.
219 | canvas.restore();
220 | }
221 |
222 | private void drawHand(Canvas canvas, float handLength) {
223 | canvas.drawRoundRect(mCenterX - HAND_END_CAP_RADIUS, mCenterY - handLength,
224 | mCenterX + HAND_END_CAP_RADIUS, mCenterY + HAND_END_CAP_RADIUS,
225 | HAND_END_CAP_RADIUS, HAND_END_CAP_RADIUS, mHandPaint);
226 | }
227 |
228 | @Override
229 | public void onVisibilityChanged(boolean visible) {
230 | super.onVisibilityChanged(visible);
231 |
232 | if (visible) {
233 | registerReceiver();
234 |
235 | // Update time zone in case it changed while we weren't visible.
236 | mTime.clear(TimeZone.getDefault().getID());
237 | mTime.setToNow();
238 | } else {
239 | unregisterReceiver();
240 | }
241 |
242 | /*
243 | * Whether the timer should be running depends on whether we're visible
244 | * (as well as whether we're in ambient mode),
245 | * so we may need to start or stop the timer.
246 | */
247 | updateTimer();
248 | }
249 |
250 | private void registerReceiver() {
251 | if (mRegisteredTimeZoneReceiver) {
252 | return;
253 | }
254 | mRegisteredTimeZoneReceiver = true;
255 | IntentFilter filter = new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED);
256 | MyWatchFaceService.this.registerReceiver(mTimeZoneReceiver, filter);
257 | }
258 |
259 | private void unregisterReceiver() {
260 | if (!mRegisteredTimeZoneReceiver) {
261 | return;
262 | }
263 | mRegisteredTimeZoneReceiver = false;
264 | MyWatchFaceService.this.unregisterReceiver(mTimeZoneReceiver);
265 | }
266 |
267 | private void updateTimer() {
268 | mUpdateTimeHandler.removeMessages(R.id.message_update);
269 | if (shouldTimerBeRunning()) {
270 | mUpdateTimeHandler.sendEmptyMessage(R.id.message_update);
271 | }
272 | }
273 |
274 | /**
275 | * Returns whether the {@link #mUpdateTimeHandler} timer should be running. The timer
276 | * should only run when we're visible and in interactive mode.
277 | */
278 | private boolean shouldTimerBeRunning() {
279 | return isVisible() && !isInAmbientMode();
280 | }
281 | }
282 | }
283 |
--------------------------------------------------------------------------------
/3-hands/src/main/res/drawable-nodpi/custom_background.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-codelab-watchface/5fc095fbb95a7c3ed5b17dfc905f9ed384b4b266/3-hands/src/main/res/drawable-nodpi/custom_background.jpg
--------------------------------------------------------------------------------
/3-hands/src/main/res/drawable-nodpi/preview_analog.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-codelab-watchface/5fc095fbb95a7c3ed5b17dfc905f9ed384b4b266/3-hands/src/main/res/drawable-nodpi/preview_analog.png
--------------------------------------------------------------------------------
/3-hands/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/3-hands/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 | Codelab Watchface
19 | Analog Codelab Watchface
20 |
21 |
--------------------------------------------------------------------------------
/3-hands/src/main/res/xml/watch_face.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/4-ambient/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | apply plugin: 'com.android.application'
18 |
19 |
20 | android {
21 | compileSdkVersion 21
22 | buildToolsVersion "21.1.2"
23 |
24 | defaultConfig {
25 | applicationId "com.android.example.watchface"
26 | minSdkVersion 21
27 | targetSdkVersion 21
28 | versionCode 1
29 | versionName "1.0"
30 | }
31 | buildTypes {
32 | release {
33 | minifyEnabled false
34 | proguardFiles getDefaultProguardFile('proguard-android.txt')
35 | }
36 | }
37 | }
38 |
39 | dependencies {
40 | compile 'com.google.android.support:wearable:1.1.0'
41 | compile 'com.google.android.gms:play-services-wearable:6.5.87'
42 | }
43 |
--------------------------------------------------------------------------------
/4-ambient/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
33 |
37 |
40 |
43 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/4-ambient/src/main/java/com/android/example/watchface/MyWatchFaceService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.android.example.watchface;
18 |
19 | import android.content.BroadcastReceiver;
20 | import android.content.Context;
21 | import android.content.Intent;
22 | import android.content.IntentFilter;
23 | import android.graphics.Bitmap;
24 | import android.graphics.BitmapFactory;
25 | import android.graphics.Canvas;
26 | import android.graphics.Color;
27 | import android.graphics.ColorMatrix;
28 | import android.graphics.ColorMatrixColorFilter;
29 | import android.graphics.Paint;
30 | import android.graphics.Rect;
31 | import android.os.Bundle;
32 | import android.os.Handler;
33 | import android.os.Message;
34 | import android.support.wearable.watchface.CanvasWatchFaceService;
35 | import android.support.wearable.watchface.WatchFaceStyle;
36 | import android.text.format.Time;
37 | import android.view.SurfaceHolder;
38 |
39 | import java.util.TimeZone;
40 | import java.util.concurrent.TimeUnit;
41 |
42 | /**
43 | * Analog watch face with a ticking second hand. In ambient mode, the second hand isn't shown. On
44 | * devices with low-bit ambient mode, the hands are drawn without anti-aliasing in ambient mode.
45 | */
46 | public class MyWatchFaceService extends CanvasWatchFaceService {
47 |
48 | /**
49 | * Update rate in milliseconds for interactive mode. We update once a second to advance the
50 | * second hand.
51 | */
52 | private static final long INTERACTIVE_UPDATE_RATE_MS = TimeUnit.SECONDS.toMillis(1);
53 |
54 | @Override
55 | public Engine onCreateEngine() {
56 | return new Engine();
57 | }
58 |
59 | private class Engine extends CanvasWatchFaceService.Engine {
60 |
61 | /* Handler to update the time once a second in interactive mode. */
62 | private final Handler mUpdateTimeHandler = new Handler() {
63 | @Override
64 | public void handleMessage(Message message) {
65 | if (R.id.message_update == message.what) {
66 | invalidate();
67 | if (shouldTimerBeRunning()) {
68 | long timeMs = System.currentTimeMillis();
69 | long delayMs = INTERACTIVE_UPDATE_RATE_MS
70 | - (timeMs % INTERACTIVE_UPDATE_RATE_MS);
71 | mUpdateTimeHandler.sendEmptyMessageDelayed(R.id.message_update, delayMs);
72 | }
73 | }
74 | }
75 | };
76 |
77 | private final BroadcastReceiver mTimeZoneReceiver = new BroadcastReceiver() {
78 | @Override
79 | public void onReceive(Context context, Intent intent) {
80 | mTime.clear(intent.getStringExtra("time-zone"));
81 | mTime.setToNow();
82 | }
83 | };
84 |
85 | private boolean mRegisteredTimeZoneReceiver = false;
86 |
87 | // Feel free to change these values and see what happens to the watch face.
88 | private static final float HAND_END_CAP_RADIUS = 4f;
89 | private static final float STROKE_WIDTH = 4f;
90 | private static final int SHADOW_RADIUS = 6;
91 |
92 | private Time mTime;
93 |
94 | private Paint mBackgroundPaint;
95 | private Paint mHandPaint;
96 |
97 | private boolean mAmbient;
98 |
99 | private Bitmap mBackgroundBitmap;
100 | private Bitmap mGrayBackgroundBitmap;
101 |
102 | private float mHourHandLength;
103 | private float mMinuteHandLength;
104 | private float mSecondHandLength;
105 |
106 | /**
107 | * Whether the display supports fewer bits for each color in ambient mode.
108 | * When true, we disable anti-aliasing in ambient mode.
109 | */
110 | private boolean mLowBitAmbient;
111 | /**
112 | * Whether the display supports burn in protection in ambient mode.
113 | * When true, remove the background in ambient mode.
114 | */
115 | private boolean mBurnInProtection;
116 |
117 | private int mWidth;
118 | private int mHeight;
119 | private float mCenterX;
120 | private float mCenterY;
121 | private float mScale = 1;
122 | private Rect mCardBounds = new Rect();
123 |
124 | @Override
125 | public void onCreate(SurfaceHolder holder) {
126 | super.onCreate(holder);
127 |
128 | setWatchFaceStyle(new WatchFaceStyle.Builder(MyWatchFaceService.this)
129 | .setCardPeekMode(WatchFaceStyle.PEEK_MODE_SHORT)
130 | .setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
131 | .setShowSystemUiTime(false)
132 | .build());
133 |
134 | mBackgroundPaint = new Paint();
135 | mBackgroundPaint.setColor(Color.BLACK);
136 |
137 | final int backgroundResId = R.drawable.custom_background;
138 |
139 | mBackgroundBitmap = BitmapFactory.decodeResource(getResources(), backgroundResId);
140 | mHandPaint = new Paint();
141 | mHandPaint.setColor(Color.WHITE);
142 | mHandPaint.setStrokeWidth(STROKE_WIDTH);
143 | mHandPaint.setAntiAlias(true);
144 | mHandPaint.setStrokeCap(Paint.Cap.ROUND);
145 | mHandPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, Color.BLACK);
146 | mHandPaint.setStyle(Paint.Style.STROKE);
147 |
148 | mTime = new Time();
149 | }
150 |
151 | @Override
152 | public void onDestroy() {
153 | mUpdateTimeHandler.removeMessages(R.id.message_update);
154 | super.onDestroy();
155 | }
156 |
157 | @Override
158 | public void onPropertiesChanged(Bundle properties) {
159 | super.onPropertiesChanged(properties);
160 | mLowBitAmbient = properties.getBoolean(PROPERTY_LOW_BIT_AMBIENT, false);
161 | mBurnInProtection = properties.getBoolean(PROPERTY_BURN_IN_PROTECTION, false);
162 | }
163 |
164 | @Override
165 | public void onTimeTick() {
166 | super.onTimeTick();
167 | invalidate();
168 | }
169 |
170 | @Override
171 | public void onAmbientModeChanged(boolean inAmbientMode) {
172 | super.onAmbientModeChanged(inAmbientMode);
173 | if (mAmbient != inAmbientMode) {
174 | mAmbient = inAmbientMode;
175 | if (mLowBitAmbient || mBurnInProtection) {
176 | mHandPaint.setAntiAlias(!inAmbientMode);
177 | }
178 | invalidate();
179 | }
180 |
181 | /*
182 | * Whether the timer should be running depends on whether we're visible (as well as
183 | * whether we're in ambient mode), so we may need to start or stop the timer.
184 | */
185 | updateTimer();
186 | }
187 |
188 | @Override
189 | public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
190 | super.onSurfaceChanged(holder, format, width, height);
191 | mWidth = width;
192 | mHeight = height;
193 | /*
194 | * Find the coordinates of the center point on the screen.
195 | * Ignore the window insets so that, on round watches
196 | * with a "chin", the watch face is centered on the entire screen,
197 | * not just the usable portion.
198 | */
199 | mCenterX = mWidth / 2f;
200 | mCenterY = mHeight / 2f;
201 | mScale = ((float) width) / (float) mBackgroundBitmap.getWidth();
202 | /*
203 | * Calculate the lengths of the watch hands and store them in member variables.
204 | */
205 | mHourHandLength = mCenterX * 0.5f;
206 | mMinuteHandLength = mCenterX * 0.7f;
207 | mSecondHandLength = mCenterX * 0.9f;
208 |
209 | mBackgroundBitmap = Bitmap.createScaledBitmap(mBackgroundBitmap,
210 | (int) (mBackgroundBitmap.getWidth() * mScale),
211 | (int) (mBackgroundBitmap.getHeight() * mScale), true);
212 |
213 | if (!mBurnInProtection || !mLowBitAmbient) {
214 | initGrayBackgroundBitmap();
215 | }
216 | }
217 |
218 | private void initGrayBackgroundBitmap() {
219 | mGrayBackgroundBitmap = Bitmap.createBitmap(mBackgroundBitmap.getWidth(),
220 | mBackgroundBitmap.getHeight(), Bitmap.Config.ARGB_8888);
221 | Canvas canvas = new Canvas(mGrayBackgroundBitmap);
222 | Paint grayPaint = new Paint();
223 | ColorMatrix colorMatrix = new ColorMatrix();
224 | colorMatrix.setSaturation(0);
225 | ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
226 | grayPaint.setColorFilter(filter);
227 | canvas.drawBitmap(mBackgroundBitmap, 0, 0, grayPaint);
228 | }
229 |
230 | @Override
231 | public void onDraw(Canvas canvas, Rect bounds) {
232 | mTime.setToNow();
233 |
234 | if (mAmbient && (mLowBitAmbient || mBurnInProtection)) {
235 | canvas.drawColor(Color.BLACK);
236 | } else if (mAmbient) {
237 | canvas.drawBitmap(mGrayBackgroundBitmap, 0, 0, mBackgroundPaint);
238 | } else {
239 | canvas.drawBitmap(mBackgroundBitmap, 0, 0, mBackgroundPaint);
240 | }
241 |
242 | /*
243 | * These calculations reflect the rotation in degrees per unit of
244 | * time, e.g. 360 / 60 = 6 and 360 / 12 = 30
245 | */
246 | final float secondsRotation = mTime.second * 6f;
247 | final float minutesRotation = mTime.minute * 6f;
248 | // account for the offset of the hour hand due to minutes of the hour.
249 | final float hourHandOffset = mTime.minute / 2f;
250 | final float hoursRotation = (mTime.hour * 30) + hourHandOffset;
251 |
252 | // save the canvas state before we begin to rotate it
253 | canvas.save();
254 |
255 | canvas.rotate(hoursRotation, mCenterX, mCenterY);
256 | drawHand(canvas, mHourHandLength);
257 |
258 | canvas.rotate(minutesRotation - hoursRotation, mCenterX, mCenterY);
259 | drawHand(canvas, mMinuteHandLength);
260 |
261 | /*
262 | * Make sure the "seconds" hand is drawn only when we are in interactive mode.
263 | * Otherwise we only update the watch face once a minute.
264 | */
265 | if (!mAmbient) {
266 | canvas.rotate(secondsRotation - minutesRotation, mCenterX, mCenterY);
267 | canvas.drawLine(mCenterX, mCenterY - HAND_END_CAP_RADIUS, mCenterX,
268 | mCenterY - mSecondHandLength, mHandPaint);
269 | }
270 | canvas.drawCircle(mCenterX, mCenterY, HAND_END_CAP_RADIUS, mHandPaint);
271 | // restore the canvas' original orientation.
272 | canvas.restore();
273 |
274 | if (mAmbient) {
275 | canvas.drawRect(mCardBounds, mBackgroundPaint);
276 | }
277 | }
278 |
279 | private void drawHand(Canvas canvas, float handLength) {
280 | canvas.drawRoundRect(mCenterX - HAND_END_CAP_RADIUS, mCenterY - handLength,
281 | mCenterX + HAND_END_CAP_RADIUS, mCenterY + HAND_END_CAP_RADIUS,
282 | HAND_END_CAP_RADIUS, HAND_END_CAP_RADIUS, mHandPaint);
283 | }
284 |
285 | @Override
286 | public void onVisibilityChanged(boolean visible) {
287 | super.onVisibilityChanged(visible);
288 |
289 | if (visible) {
290 | registerReceiver();
291 |
292 | // Update time zone in case it changed while we weren't visible.
293 | mTime.clear(TimeZone.getDefault().getID());
294 | mTime.setToNow();
295 | } else {
296 | unregisterReceiver();
297 | }
298 |
299 | /*
300 | * Whether the timer should be running depends on whether we're visible
301 | * (as well as whether we're in ambient mode),
302 | * so we may need to start or stop the timer.
303 | */
304 | updateTimer();
305 | }
306 |
307 | @Override
308 | public void onPeekCardPositionUpdate(Rect rect) {
309 | super.onPeekCardPositionUpdate(rect);
310 | mCardBounds.set(rect);
311 | }
312 |
313 | private void registerReceiver() {
314 | if (mRegisteredTimeZoneReceiver) {
315 | return;
316 | }
317 | mRegisteredTimeZoneReceiver = true;
318 | IntentFilter filter = new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED);
319 | MyWatchFaceService.this.registerReceiver(mTimeZoneReceiver, filter);
320 | }
321 |
322 | private void unregisterReceiver() {
323 | if (!mRegisteredTimeZoneReceiver) {
324 | return;
325 | }
326 | mRegisteredTimeZoneReceiver = false;
327 | MyWatchFaceService.this.unregisterReceiver(mTimeZoneReceiver);
328 | }
329 |
330 | private void updateTimer() {
331 | mUpdateTimeHandler.removeMessages(R.id.message_update);
332 | if (shouldTimerBeRunning()) {
333 | mUpdateTimeHandler.sendEmptyMessage(R.id.message_update);
334 | }
335 | }
336 |
337 | /**
338 | * Returns whether the {@link #mUpdateTimeHandler} timer should be running. The timer
339 | * should only run when we're visible and in interactive mode.
340 | */
341 | private boolean shouldTimerBeRunning() {
342 | return isVisible() && !isInAmbientMode();
343 | }
344 | }
345 | }
346 |
--------------------------------------------------------------------------------
/4-ambient/src/main/res/drawable-nodpi/custom_background.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-codelab-watchface/5fc095fbb95a7c3ed5b17dfc905f9ed384b4b266/4-ambient/src/main/res/drawable-nodpi/custom_background.jpg
--------------------------------------------------------------------------------
/4-ambient/src/main/res/drawable-nodpi/preview_analog.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-codelab-watchface/5fc095fbb95a7c3ed5b17dfc905f9ed384b4b266/4-ambient/src/main/res/drawable-nodpi/preview_analog.png
--------------------------------------------------------------------------------
/4-ambient/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/4-ambient/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 | Codelab Watchface
19 | Analog Codelab Watchface
20 |
21 |
--------------------------------------------------------------------------------
/4-ambient/src/main/res/xml/watch_face.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/5-palette/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | apply plugin: 'com.android.application'
18 |
19 |
20 | android {
21 | compileSdkVersion 21
22 | buildToolsVersion "21.1.2"
23 |
24 | defaultConfig {
25 | applicationId "com.android.example.watchface"
26 | minSdkVersion 21
27 | targetSdkVersion 21
28 | versionCode 1
29 | versionName "1.0"
30 | }
31 | buildTypes {
32 | release {
33 | minifyEnabled false
34 | proguardFiles getDefaultProguardFile('proguard-android.txt')
35 | }
36 | }
37 | }
38 |
39 | dependencies {
40 | compile 'com.android.support:palette-v7:21.0.0'
41 | compile 'com.google.android.support:wearable:1.1.0'
42 | compile 'com.google.android.gms:play-services-wearable:6.5.87'
43 | }
44 |
--------------------------------------------------------------------------------
/5-palette/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
33 |
37 |
40 |
43 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/5-palette/src/main/java/com/android/example/watchface/MyWatchFaceService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.android.example.watchface;
18 |
19 | import android.content.BroadcastReceiver;
20 | import android.content.Context;
21 | import android.content.Intent;
22 | import android.content.IntentFilter;
23 | import android.graphics.Bitmap;
24 | import android.graphics.BitmapFactory;
25 | import android.graphics.Canvas;
26 | import android.graphics.Color;
27 | import android.graphics.ColorMatrix;
28 | import android.graphics.ColorMatrixColorFilter;
29 | import android.graphics.Paint;
30 | import android.graphics.Rect;
31 | import android.os.Bundle;
32 | import android.os.Handler;
33 | import android.os.Message;
34 | import android.support.v7.graphics.Palette;
35 | import android.support.wearable.watchface.CanvasWatchFaceService;
36 | import android.support.wearable.watchface.WatchFaceStyle;
37 | import android.text.format.Time;
38 | import android.view.SurfaceHolder;
39 |
40 | import java.util.TimeZone;
41 | import java.util.concurrent.TimeUnit;
42 |
43 | /**
44 | * Analog watch face with a ticking second hand. In ambient mode, the second hand isn't shown. On
45 | * devices with low-bit ambient mode, the hands are drawn without anti-aliasing in ambient mode.
46 | */
47 | public class MyWatchFaceService extends CanvasWatchFaceService {
48 |
49 | /**
50 | * Update rate in milliseconds for interactive mode. We update once a second to advance the
51 | * second hand.
52 | */
53 | private static final long INTERACTIVE_UPDATE_RATE_MS = TimeUnit.SECONDS.toMillis(1);
54 |
55 | @Override
56 | public Engine onCreateEngine() {
57 | return new Engine();
58 | }
59 |
60 | private class Engine extends CanvasWatchFaceService.Engine {
61 |
62 | /* Handler to update the time once a second in interactive mode. */
63 | private final Handler mUpdateTimeHandler = new Handler() {
64 | @Override
65 | public void handleMessage(Message message) {
66 | if (R.id.message_update == message.what) {
67 | invalidate();
68 | if (shouldTimerBeRunning()) {
69 | long timeMs = System.currentTimeMillis();
70 | long delayMs = INTERACTIVE_UPDATE_RATE_MS
71 | - (timeMs % INTERACTIVE_UPDATE_RATE_MS);
72 | mUpdateTimeHandler.sendEmptyMessageDelayed(R.id.message_update, delayMs);
73 | }
74 | }
75 | }
76 | };
77 |
78 | private final BroadcastReceiver mTimeZoneReceiver = new BroadcastReceiver() {
79 | @Override
80 | public void onReceive(Context context, Intent intent) {
81 | mTime.clear(intent.getStringExtra("time-zone"));
82 | mTime.setToNow();
83 | }
84 | };
85 |
86 | private boolean mRegisteredTimeZoneReceiver = false;
87 |
88 | // Feel free to change these values and see what happens to the watch face.
89 | private static final float HAND_END_CAP_RADIUS = 4f;
90 | private static final float STROKE_WIDTH = 4f;
91 | private static final int SHADOW_RADIUS = 6;
92 |
93 | private Time mTime;
94 |
95 | private Paint mBackgroundPaint;
96 | private Paint mHandPaint;
97 |
98 | private boolean mAmbient;
99 |
100 | private Bitmap mBackgroundBitmap;
101 | private Bitmap mGrayBackgroundBitmap;
102 | private int mWatchHandColor;
103 | private int mWatchHandShadowColor;
104 |
105 | private float mHourHandLength;
106 | private float mMinuteHandLength;
107 | private float mSecondHandLength;
108 |
109 | /**
110 | * Whether the display supports fewer bits for each color in ambient mode.
111 | * When true, we disable anti-aliasing in ambient mode.
112 | */
113 | private boolean mLowBitAmbient;
114 | /**
115 | * Whether the display supports burn in protection in ambient mode.
116 | * When true, remove the background in ambient mode.
117 | */
118 | private boolean mBurnInProtection;
119 |
120 | private int mWidth;
121 | private int mHeight;
122 | private float mCenterX;
123 | private float mCenterY;
124 | private float mScale = 1;
125 | private Rect mCardBounds = new Rect();
126 |
127 | @Override
128 | public void onCreate(SurfaceHolder holder) {
129 | super.onCreate(holder);
130 |
131 | setWatchFaceStyle(new WatchFaceStyle.Builder(MyWatchFaceService.this)
132 | .setCardPeekMode(WatchFaceStyle.PEEK_MODE_SHORT)
133 | .setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
134 | .setShowSystemUiTime(false)
135 | .build());
136 |
137 | mBackgroundPaint = new Paint();
138 | mBackgroundPaint.setColor(Color.BLACK);
139 |
140 | /*
141 | * Toggle the backgroundResIds to see
142 | * the change of colors due to palette doing its magic.
143 | */
144 | final int backgroundResId = R.drawable.custom_background;
145 | //final int backgroundResId = R.drawable.custom_background2;
146 |
147 | mBackgroundBitmap = BitmapFactory.decodeResource(getResources(), backgroundResId);
148 | mHandPaint = new Paint();
149 | mHandPaint.setColor(Color.WHITE);
150 | mHandPaint.setStrokeWidth(STROKE_WIDTH);
151 | mHandPaint.setAntiAlias(true);
152 | mHandPaint.setStrokeCap(Paint.Cap.ROUND);
153 | mHandPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, Color.BLACK);
154 | mHandPaint.setStyle(Paint.Style.STROKE);
155 |
156 | Palette.generateAsync(mBackgroundBitmap, new Palette.PaletteAsyncListener() {
157 | @Override
158 | public void onGenerated(Palette palette) {
159 | /*
160 | * Sometimes, palette is unable to generate a color palette
161 | * so we need to check that we have one.
162 | */
163 | if (palette != null) {
164 | mWatchHandColor = palette.getVibrantColor(Color.WHITE);
165 | mWatchHandShadowColor = palette.getDarkMutedColor(Color.BLACK);
166 | setWatchHandColor();
167 | }
168 | }
169 | });
170 |
171 | mTime = new Time();
172 | }
173 |
174 | private void setWatchHandColor() {
175 | if (mAmbient) {
176 | mHandPaint.setColor(Color.WHITE);
177 | mHandPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, Color.BLACK);
178 | } else {
179 | mHandPaint.setColor(mWatchHandColor);
180 | mHandPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor);
181 | }
182 | }
183 |
184 | @Override
185 | public void onDestroy() {
186 | mUpdateTimeHandler.removeMessages(R.id.message_update);
187 | super.onDestroy();
188 | }
189 |
190 | @Override
191 | public void onPropertiesChanged(Bundle properties) {
192 | super.onPropertiesChanged(properties);
193 | mLowBitAmbient = properties.getBoolean(PROPERTY_LOW_BIT_AMBIENT, false);
194 | mBurnInProtection = properties.getBoolean(PROPERTY_BURN_IN_PROTECTION, false);
195 | }
196 |
197 | @Override
198 | public void onTimeTick() {
199 | super.onTimeTick();
200 | invalidate();
201 | }
202 |
203 | @Override
204 | public void onAmbientModeChanged(boolean inAmbientMode) {
205 | super.onAmbientModeChanged(inAmbientMode);
206 | if (mAmbient != inAmbientMode) {
207 | mAmbient = inAmbientMode;
208 | if (mLowBitAmbient || mBurnInProtection) {
209 | mHandPaint.setAntiAlias(!inAmbientMode);
210 | }
211 | setWatchHandColor();
212 | invalidate();
213 | }
214 |
215 | /*
216 | * Whether the timer should be running depends on whether we're visible (as well as
217 | * whether we're in ambient mode), so we may need to start or stop the timer.
218 | */
219 | updateTimer();
220 | }
221 |
222 | @Override
223 | public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
224 | super.onSurfaceChanged(holder, format, width, height);
225 | mWidth = width;
226 | mHeight = height;
227 | /*
228 | * Find the coordinates of the center point on the screen.
229 | * Ignore the window insets so that, on round watches
230 | * with a "chin", the watch face is centered on the entire screen,
231 | * not just the usable portion.
232 | */
233 | mCenterX = mWidth / 2f;
234 | mCenterY = mHeight / 2f;
235 | mScale = ((float) width) / (float) mBackgroundBitmap.getWidth();
236 | /*
237 | * Calculate the lengths of the watch hands and store them in member variables.
238 | */
239 | mHourHandLength = mCenterX * 0.5f;
240 | mMinuteHandLength = mCenterX * 0.7f;
241 | mSecondHandLength = mCenterX * 0.9f;
242 |
243 | mBackgroundBitmap = Bitmap.createScaledBitmap(mBackgroundBitmap,
244 | (int) (mBackgroundBitmap.getWidth() * mScale),
245 | (int) (mBackgroundBitmap.getHeight() * mScale), true);
246 |
247 | if (!mBurnInProtection || !mLowBitAmbient) {
248 | initGrayBackgroundBitmap();
249 | }
250 | }
251 |
252 | private void initGrayBackgroundBitmap() {
253 | mGrayBackgroundBitmap = Bitmap.createBitmap(mBackgroundBitmap.getWidth(),
254 | mBackgroundBitmap.getHeight(), Bitmap.Config.ARGB_8888);
255 | Canvas canvas = new Canvas(mGrayBackgroundBitmap);
256 | Paint grayPaint = new Paint();
257 | ColorMatrix colorMatrix = new ColorMatrix();
258 | colorMatrix.setSaturation(0);
259 | ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
260 | grayPaint.setColorFilter(filter);
261 | canvas.drawBitmap(mBackgroundBitmap, 0, 0, grayPaint);
262 | }
263 |
264 | @Override
265 | public void onDraw(Canvas canvas, Rect bounds) {
266 | mTime.setToNow();
267 |
268 | if (mAmbient && (mLowBitAmbient || mBurnInProtection)) {
269 | canvas.drawColor(Color.BLACK);
270 | } else if (mAmbient) {
271 | canvas.drawBitmap(mGrayBackgroundBitmap, 0, 0, mBackgroundPaint);
272 | } else {
273 | canvas.drawBitmap(mBackgroundBitmap, 0, 0, mBackgroundPaint);
274 | }
275 |
276 | /*
277 | * These calculations reflect the rotation in degrees per unit of
278 | * time, e.g. 360 / 60 = 6 and 360 / 12 = 30
279 | */
280 | final float secondsRotation = mTime.second * 6f;
281 | final float minutesRotation = mTime.minute * 6f;
282 | // account for the offset of the hour hand due to minutes of the hour.
283 | final float hourHandOffset = mTime.minute / 2f;
284 | final float hoursRotation = (mTime.hour * 30) + hourHandOffset;
285 |
286 | // save the canvas state before we begin to rotate it
287 | canvas.save();
288 |
289 | canvas.rotate(hoursRotation, mCenterX, mCenterY);
290 | drawHand(canvas, mHourHandLength);
291 |
292 | canvas.rotate(minutesRotation - hoursRotation, mCenterX, mCenterY);
293 | drawHand(canvas, mMinuteHandLength);
294 |
295 | /*
296 | * Make sure the "seconds" hand is drawn only when we are in interactive mode.
297 | * Otherwise we only update the watch face once a minute.
298 | */
299 | if (!mAmbient) {
300 | canvas.rotate(secondsRotation - minutesRotation, mCenterX, mCenterY);
301 | canvas.drawLine(mCenterX, mCenterY - HAND_END_CAP_RADIUS, mCenterX,
302 | mCenterY - mSecondHandLength, mHandPaint);
303 | }
304 | canvas.drawCircle(mCenterX, mCenterY, HAND_END_CAP_RADIUS, mHandPaint);
305 | // restore the canvas' original orientation.
306 | canvas.restore();
307 |
308 | if (mAmbient) {
309 | canvas.drawRect(mCardBounds, mBackgroundPaint);
310 | }
311 | }
312 |
313 | private void drawHand(Canvas canvas, float handLength) {
314 | canvas.drawRoundRect(mCenterX - HAND_END_CAP_RADIUS, mCenterY - handLength,
315 | mCenterX + HAND_END_CAP_RADIUS, mCenterY + HAND_END_CAP_RADIUS,
316 | HAND_END_CAP_RADIUS, HAND_END_CAP_RADIUS, mHandPaint);
317 | }
318 |
319 | @Override
320 | public void onVisibilityChanged(boolean visible) {
321 | super.onVisibilityChanged(visible);
322 |
323 | if (visible) {
324 | registerReceiver();
325 |
326 | // Update time zone in case it changed while we weren't visible.
327 | mTime.clear(TimeZone.getDefault().getID());
328 | mTime.setToNow();
329 | } else {
330 | unregisterReceiver();
331 | }
332 |
333 | /*
334 | * Whether the timer should be running depends on whether we're visible
335 | * (as well as whether we're in ambient mode),
336 | * so we may need to start or stop the timer.
337 | */
338 | updateTimer();
339 | }
340 |
341 | @Override
342 | public void onPeekCardPositionUpdate(Rect rect) {
343 | super.onPeekCardPositionUpdate(rect);
344 | mCardBounds.set(rect);
345 | }
346 |
347 | private void registerReceiver() {
348 | if (mRegisteredTimeZoneReceiver) {
349 | return;
350 | }
351 | mRegisteredTimeZoneReceiver = true;
352 | IntentFilter filter = new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED);
353 | MyWatchFaceService.this.registerReceiver(mTimeZoneReceiver, filter);
354 | }
355 |
356 | private void unregisterReceiver() {
357 | if (!mRegisteredTimeZoneReceiver) {
358 | return;
359 | }
360 | mRegisteredTimeZoneReceiver = false;
361 | MyWatchFaceService.this.unregisterReceiver(mTimeZoneReceiver);
362 | }
363 |
364 | private void updateTimer() {
365 | mUpdateTimeHandler.removeMessages(R.id.message_update);
366 | if (shouldTimerBeRunning()) {
367 | mUpdateTimeHandler.sendEmptyMessage(R.id.message_update);
368 | }
369 | }
370 |
371 | /**
372 | * Returns whether the {@link #mUpdateTimeHandler} timer should be running. The timer
373 | * should only run when we're visible and in interactive mode.
374 | */
375 | private boolean shouldTimerBeRunning() {
376 | return isVisible() && !isInAmbientMode();
377 | }
378 | }
379 | }
380 |
--------------------------------------------------------------------------------
/5-palette/src/main/res/drawable-nodpi/custom_background.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-codelab-watchface/5fc095fbb95a7c3ed5b17dfc905f9ed384b4b266/5-palette/src/main/res/drawable-nodpi/custom_background.jpg
--------------------------------------------------------------------------------
/5-palette/src/main/res/drawable-nodpi/custom_background2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-codelab-watchface/5fc095fbb95a7c3ed5b17dfc905f9ed384b4b266/5-palette/src/main/res/drawable-nodpi/custom_background2.jpg
--------------------------------------------------------------------------------
/5-palette/src/main/res/drawable-nodpi/preview_analog.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-codelab-watchface/5fc095fbb95a7c3ed5b17dfc905f9ed384b4b266/5-palette/src/main/res/drawable-nodpi/preview_analog.png
--------------------------------------------------------------------------------
/5-palette/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/5-palette/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 | Codelab Watchface
19 | Analog Codelab Watchface
20 |
21 |
--------------------------------------------------------------------------------
/5-palette/src/main/res/xml/watch_face.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/CONTRIB.md:
--------------------------------------------------------------------------------
1 | # How to become a contributor and submit your own code
2 |
3 | ## Contributor License Agreements
4 |
5 | We'd love to accept your sample apps and patches! Before we can take them, we
6 | have to jump a couple of legal hurdles.
7 |
8 | Please fill out either the individual or corporate Contributor License Agreement (CLA).
9 |
10 | * If you are an individual writing original source code and you're sure you
11 | own the intellectual property, then you'll need to sign an [individual CLA]
12 | (https://developers.google.com/open-source/cla/individual).
13 | * If you work for a company that wants to allow you to contribute your work,
14 | then you'll need to sign a [corporate CLA]
15 | (https://developers.google.com/open-source/cla/corporate).
16 |
17 | Follow either of the two links above to access the appropriate CLA and
18 | instructions for how to sign and return it. Once we receive it, we'll be able to
19 | accept your pull requests.
20 |
21 | ## Contributing A Patch
22 |
23 | 1. Submit an issue describing your proposed change to the repo in question.
24 | 1. The repo owner will respond to your issue promptly.
25 | 1. If your proposed change is accepted, and you haven't already done so, sign a
26 | Contributor License Agreement (see details above).
27 | 1. Fork the desired repo, develop and test your code changes.
28 | 1. Ensure that your code adheres to the existing style in the sample to which
29 | you are contributing. Refer to the
30 | [Android Code Style Guide]
31 | (https://source.android.com/source/code-style.html) for the
32 | recommended coding standards for this organization.
33 | 1. Ensure that your code has an appropriate set of unit tests which all pass.
34 | 1. Submit a pull request.
35 |
36 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright 2015 The Android Open Source Project
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/NOTICE:
--------------------------------------------------------------------------------
1 | This sample uses the following software:
2 |
3 | Copyright 2015 The Android Open Source Project
4 |
5 | Licensed under the Apache License, Version 2.0 (the "License");
6 | you may not use this file except in compliance with the License.
7 | You may obtain a copy of the License at
8 |
9 | http://www.apache.org/licenses/LICENSE-2.0
10 |
11 | Unless required by applicable law or agreed to in writing, software
12 | distributed under the License is distributed on an "AS IS" BASIS,
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | See the License for the specific language governing permissions and
15 | limitations under the License.
16 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Android Wear watch face codelab
2 | ===============================
3 |
4 | This repo has been migrated to [github.com/googlecodelabs/watchface][1]. Please check that repo for future updates. Thank you!
5 |
6 | [1]: https://github.com/googlecodelabs/watchface
7 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
18 |
19 | buildscript {
20 | repositories {
21 | jcenter()
22 | }
23 | dependencies {
24 | classpath 'com.android.tools.build:gradle:1.1.0'
25 |
26 | // NOTE: Do not place your application dependencies here; they belong
27 | // in the individual module build.gradle files
28 | }
29 | }
30 |
31 | allprojects {
32 | repositories {
33 | jcenter()
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright (C) 2015 The Android Open Source Project
3 | #
4 | # Licensed under the Apache License, Version 2.0 (the "License");
5 | # you may not use this file except in compliance with the License.
6 | # You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | #
16 |
17 | # Project-wide Gradle settings.
18 |
19 | # IDE (e.g. Android Studio) users:
20 | # Gradle settings configured through the IDE *will override*
21 | # any settings specified in this file.
22 |
23 | # For more details on how to configure your build environment visit
24 | # http://www.gradle.org/docs/current/userguide/build_environment.html
25 |
26 | # Specifies the JVM arguments used for the daemon process.
27 | # The setting is particularly useful for tweaking memory settings.
28 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
29 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
30 |
31 | # When configured, Gradle will run in incubating parallel mode.
32 | # This option should only be used with decoupled projects. More details, visit
33 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
34 | # org.gradle.parallel=true
35 | org.gradle.daemon=true
36 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlearchive/android-codelab-watchface/5fc095fbb95a7c3ed5b17dfc905f9ed384b4b266/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Apr 10 15:27:10 PDT 2013
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2015 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | include '1-base', '2-background', '3-hands', '4-ambient', '5-palette'
--------------------------------------------------------------------------------