| 1 |
/* |
| 2 |
* Copyright (C) 2014 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 ${packageName}; |
| 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.WatchFaceService; |
| 37 |
import android.support.wearable.watchface.WatchFaceStyle; |
| 38 |
import android.view.SurfaceHolder; |
| 39 |
<#if isInteractive>import android.widget.Toast;</#if> |
| 40 |
|
| 41 |
import java.lang.ref.WeakReference; |
| 42 |
import java.util.Calendar; |
| 43 |
import java.util.TimeZone; |
| 44 |
import java.util.concurrent.TimeUnit; |
| 45 |
|
| 46 |
/** |
| 47 |
* Analog watch face with a ticking second hand. In ambient mode, the second hand isn't |
| 48 |
* shown. On devices with low-bit ambient mode, the hands are drawn without anti-aliasing in ambient |
| 49 |
* mode. The watch face is drawn with less contrast in mute mode. |
| 50 |
*/ |
| 51 |
public class ${serviceClass} extends CanvasWatchFaceService { |
| 52 |
|
| 53 |
/* |
| 54 |
* Update rate in milliseconds for interactive mode. We update once a second to advance the |
| 55 |
* second hand. |
| 56 |
*/ |
| 57 |
private static final long INTERACTIVE_UPDATE_RATE_MS = TimeUnit.SECONDS.toMillis(1); |
| 58 |
|
| 59 |
/** |
| 60 |
* Handler message id for updating the time periodically in interactive mode. |
| 61 |
*/ |
| 62 |
private static final int MSG_UPDATE_TIME = 0; |
| 63 |
|
| 64 |
@Override |
| 65 |
public Engine onCreateEngine() { |
| 66 |
return new Engine(); |
| 67 |
} |
| 68 |
|
| 69 |
private class Engine extends CanvasWatchFaceService.Engine { |
| 70 |
private static final float HOUR_STROKE_WIDTH = 5f; |
| 71 |
private static final float MINUTE_STROKE_WIDTH = 3f; |
| 72 |
private static final float SECOND_TICK_STROKE_WIDTH = 2f; |
| 73 |
|
| 74 |
private static final float CENTER_GAP_AND_CIRCLE_RADIUS = 4f; |
| 75 |
|
| 76 |
private static final int SHADOW_RADIUS = 6; |
| 77 |
|
| 78 |
private Calendar mCalendar; |
| 79 |
private boolean mRegisteredTimeZoneReceiver = false; |
| 80 |
private boolean mMuteMode; |
| 81 |
|
| 82 |
private float mCenterX; |
| 83 |
private float mCenterY; |
| 84 |
|
| 85 |
private float mSecondHandLength; |
| 86 |
private float sMinuteHandLength; |
| 87 |
private float sHourHandLength; |
| 88 |
|
| 89 |
/* Colors for all hands (hour, minute, seconds, ticks) based on photo loaded. */ |
| 90 |
private int mWatchHandColor; |
| 91 |
private int mWatchHandHighlightColor; |
| 92 |
private int mWatchHandShadowColor; |
| 93 |
|
| 94 |
private Paint mHourPaint; |
| 95 |
private Paint mMinutePaint; |
| 96 |
private Paint mSecondPaint; |
| 97 |
private Paint mTickAndCirclePaint; |
| 98 |
|
| 99 |
private Paint mBackgroundPaint; |
| 100 |
private Bitmap mBackgroundBitmap; |
| 101 |
private Bitmap mGrayBackgroundBitmap; |
| 102 |
|
| 103 |
private boolean mAmbient; |
| 104 |
private boolean mLowBitAmbient; |
| 105 |
private boolean mBurnInProtection; |
| 106 |
|
| 107 |
private final Rect mPeekCardBounds = new Rect(); |
| 108 |
|
| 109 |
private final BroadcastReceiver mTimeZoneReceiver = new BroadcastReceiver() { |
| 110 |
@Override |
| 111 |
public void onReceive(Context context, Intent intent) { |
| 112 |
mCalendar.setTimeZone(TimeZone.getDefault()); |
| 113 |
invalidate(); |
| 114 |
} |
| 115 |
}; |
| 116 |
|
| 117 |
/* Handler to update the time once a second in interactive mode. */ |
| 118 |
private final Handler mUpdateTimeHandler = new EngineHandler(this); |
| 119 |
|
| 120 |
@Override |
| 121 |
public void onCreate(SurfaceHolder holder) { |
| 122 |
super.onCreate(holder); |
| 123 |
|
| 124 |
setWatchFaceStyle(new WatchFaceStyle.Builder(${serviceClass}.this) |
| 125 |
.setCardPeekMode(WatchFaceStyle.PEEK_MODE_SHORT) |
| 126 |
.setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE) |
| 127 |
.setShowSystemUiTime(false) |
| 128 |
<#if isInteractive> |
| 129 |
.setAcceptsTapEvents(true) |
| 130 |
</#if> |
| 131 |
.build()); |
| 132 |
|
| 133 |
mBackgroundPaint = new Paint(); |
| 134 |
mBackgroundPaint.setColor(Color.BLACK); |
| 135 |
mBackgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg); |
| 136 |
|
| 137 |
/* Set defaults for colors */ |
| 138 |
mWatchHandColor = Color.WHITE; |
| 139 |
mWatchHandHighlightColor = Color.RED; |
| 140 |
mWatchHandShadowColor = Color.BLACK; |
| 141 |
|
| 142 |
mHourPaint = new Paint(); |
| 143 |
mHourPaint.setColor(mWatchHandColor); |
| 144 |
mHourPaint.setStrokeWidth(HOUR_STROKE_WIDTH); |
| 145 |
mHourPaint.setAntiAlias(true); |
| 146 |
mHourPaint.setStrokeCap(Paint.Cap.ROUND); |
| 147 |
mHourPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor); |
| 148 |
|
| 149 |
mMinutePaint = new Paint(); |
| 150 |
mMinutePaint.setColor(mWatchHandColor); |
| 151 |
mMinutePaint.setStrokeWidth(MINUTE_STROKE_WIDTH); |
| 152 |
mMinutePaint.setAntiAlias(true); |
| 153 |
mMinutePaint.setStrokeCap(Paint.Cap.ROUND); |
| 154 |
mMinutePaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor); |
| 155 |
|
| 156 |
mSecondPaint = new Paint(); |
| 157 |
mSecondPaint.setColor(mWatchHandHighlightColor); |
| 158 |
mSecondPaint.setStrokeWidth(SECOND_TICK_STROKE_WIDTH); |
| 159 |
mSecondPaint.setAntiAlias(true); |
| 160 |
mSecondPaint.setStrokeCap(Paint.Cap.ROUND); |
| 161 |
mSecondPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor); |
| 162 |
|
| 163 |
mTickAndCirclePaint = new Paint(); |
| 164 |
mTickAndCirclePaint.setColor(mWatchHandColor); |
| 165 |
mTickAndCirclePaint.setStrokeWidth(SECOND_TICK_STROKE_WIDTH); |
| 166 |
mTickAndCirclePaint.setAntiAlias(true); |
| 167 |
mTickAndCirclePaint.setStyle(Paint.Style.STROKE); |
| 168 |
mTickAndCirclePaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor); |
| 169 |
|
| 170 |
/* Extract colors from background image to improve watchface style. */ |
| 171 |
Palette.from(mBackgroundBitmap).generate(new Palette.PaletteAsyncListener() { |
| 172 |
@Override |
| 173 |
public void onGenerated(Palette palette) { |
| 174 |
if (palette != null) { |
| 175 |
mWatchHandHighlightColor = palette.getVibrantColor(Color.RED); |
| 176 |
mWatchHandColor = palette.getLightVibrantColor(Color.WHITE); |
| 177 |
mWatchHandShadowColor = palette.getDarkMutedColor(Color.BLACK); |
| 178 |
updateWatchHandStyle(); |
| 179 |
} |
| 180 |
} |
| 181 |
}); |
| 182 |
|
| 183 |
mCalendar = Calendar.getInstance(); |
| 184 |
} |
| 185 |
|
| 186 |
@Override |
| 187 |
public void onDestroy() { |
| 188 |
mUpdateTimeHandler.removeMessages(MSG_UPDATE_TIME); |
| 189 |
super.onDestroy(); |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
public void onPropertiesChanged(Bundle properties) { |
| 194 |
super.onPropertiesChanged(properties); |
| 195 |
mLowBitAmbient = properties.getBoolean(PROPERTY_LOW_BIT_AMBIENT, false); |
| 196 |
mBurnInProtection = properties.getBoolean(PROPERTY_BURN_IN_PROTECTION, false); |
| 197 |
} |
| 198 |
|
| 199 |
@Override |
| 200 |
public void onTimeTick() { |
| 201 |
super.onTimeTick(); |
| 202 |
invalidate(); |
| 203 |
} |
| 204 |
|
| 205 |
@Override |
| 206 |
public void onAmbientModeChanged(boolean inAmbientMode) { |
| 207 |
super.onAmbientModeChanged(inAmbientMode); |
| 208 |
mAmbient = inAmbientMode; |
| 209 |
|
| 210 |
updateWatchHandStyle(); |
| 211 |
|
| 212 |
/* Check and trigger whether or not timer should be running (only in active mode). */ |
| 213 |
updateTimer(); |
| 214 |
} |
| 215 |
|
| 216 |
private void updateWatchHandStyle(){ |
| 217 |
if (mAmbient){ |
| 218 |
mHourPaint.setColor(Color.WHITE); |
| 219 |
mMinutePaint.setColor(Color.WHITE); |
| 220 |
mSecondPaint.setColor(Color.WHITE); |
| 221 |
mTickAndCirclePaint.setColor(Color.WHITE); |
| 222 |
|
| 223 |
mHourPaint.setAntiAlias(false); |
| 224 |
mMinutePaint.setAntiAlias(false); |
| 225 |
mSecondPaint.setAntiAlias(false); |
| 226 |
mTickAndCirclePaint.setAntiAlias(false); |
| 227 |
|
| 228 |
mHourPaint.clearShadowLayer(); |
| 229 |
mMinutePaint.clearShadowLayer(); |
| 230 |
mSecondPaint.clearShadowLayer(); |
| 231 |
mTickAndCirclePaint.clearShadowLayer(); |
| 232 |
|
| 233 |
} else { |
| 234 |
mHourPaint.setColor(mWatchHandColor); |
| 235 |
mMinutePaint.setColor(mWatchHandColor); |
| 236 |
mSecondPaint.setColor(mWatchHandHighlightColor); |
| 237 |
mTickAndCirclePaint.setColor(mWatchHandColor); |
| 238 |
|
| 239 |
mHourPaint.setAntiAlias(true); |
| 240 |
mMinutePaint.setAntiAlias(true); |
| 241 |
mSecondPaint.setAntiAlias(true); |
| 242 |
mTickAndCirclePaint.setAntiAlias(true); |
| 243 |
|
| 244 |
mHourPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor); |
| 245 |
mMinutePaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor); |
| 246 |
mSecondPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor); |
| 247 |
mTickAndCirclePaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
@Override |
| 252 |
public void onInterruptionFilterChanged(int interruptionFilter) { |
| 253 |
super.onInterruptionFilterChanged(interruptionFilter); |
| 254 |
boolean inMuteMode = (interruptionFilter == WatchFaceService.INTERRUPTION_FILTER_NONE); |
| 255 |
|
| 256 |
/* Dim display in mute mode. */ |
| 257 |
if (mMuteMode != inMuteMode) { |
| 258 |
mMuteMode = inMuteMode; |
| 259 |
mHourPaint.setAlpha(inMuteMode ? 100 : 255); |
| 260 |
mMinutePaint.setAlpha(inMuteMode ? 100 : 255); |
| 261 |
mSecondPaint.setAlpha(inMuteMode ? 80 : 255); |
| 262 |
invalidate(); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
@Override |
| 267 |
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { |
| 268 |
super.onSurfaceChanged(holder, format, width, height); |
| 269 |
|
| 270 |
/* |
| 271 |
* Find the coordinates of the center point on the screen, and ignore the window |
| 272 |
* insets, so that, on round watches with a "chin", the watch face is centered on the |
| 273 |
* entire screen, not just the usable portion. |
| 274 |
*/ |
| 275 |
mCenterX = width / 2f; |
| 276 |
mCenterY = height / 2f; |
| 277 |
|
| 278 |
/* |
| 279 |
* Calculate lengths of different hands based on watch screen size. |
| 280 |
*/ |
| 281 |
mSecondHandLength = (float) (mCenterX * 0.875); |
| 282 |
sMinuteHandLength = (float) (mCenterX * 0.75); |
| 283 |
sHourHandLength = (float) (mCenterX * 0.5); |
| 284 |
|
| 285 |
|
| 286 |
/* Scale loaded background image (more efficient) if surface dimensions change. */ |
| 287 |
float scale = ((float) width) / (float) mBackgroundBitmap.getWidth(); |
| 288 |
|
| 289 |
mBackgroundBitmap = Bitmap.createScaledBitmap(mBackgroundBitmap, |
| 290 |
(int) (mBackgroundBitmap.getWidth() * scale), |
| 291 |
(int) (mBackgroundBitmap.getHeight() * scale), true); |
| 292 |
|
| 293 |
/* |
| 294 |
* Create a gray version of the image only if it will look nice on the device in |
| 295 |
* ambient mode. That means we don't want devices that support burn-in |
| 296 |
* protection (slight movements in pixels, not great for images going all the way to |
| 297 |
* edges) and low ambient mode (degrades image quality). |
| 298 |
* |
| 299 |
* Also, if your watch face will know about all images ahead of time (users aren't |
| 300 |
* selecting their own photos for the watch face), it will be more |
| 301 |
* efficient to create a black/white version (png, etc.) and load that when you need it. |
| 302 |
*/ |
| 303 |
if (!mBurnInProtection && !mLowBitAmbient) { |
| 304 |
initGrayBackgroundBitmap(); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
private void initGrayBackgroundBitmap() { |
| 309 |
mGrayBackgroundBitmap = Bitmap.createBitmap( |
| 310 |
mBackgroundBitmap.getWidth(), |
| 311 |
mBackgroundBitmap.getHeight(), |
| 312 |
Bitmap.Config.ARGB_8888); |
| 313 |
Canvas canvas = new Canvas(mGrayBackgroundBitmap); |
| 314 |
Paint grayPaint = new Paint(); |
| 315 |
ColorMatrix colorMatrix = new ColorMatrix(); |
| 316 |
colorMatrix.setSaturation(0); |
| 317 |
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix); |
| 318 |
grayPaint.setColorFilter(filter); |
| 319 |
canvas.drawBitmap(mBackgroundBitmap, 0, 0, grayPaint); |
| 320 |
} |
| 321 |
<#if isInteractive> |
| 322 |
/** |
| 323 |
* Captures tap event (and tap type). The {@link WatchFaceService#TAP_TYPE_TAP} case can be |
| 324 |
* used for implementing specific logic to handle the gesture. |
| 325 |
*/ |
| 326 |
@Override |
| 327 |
public void onTapCommand(int tapType, int x, int y, long eventTime) { |
| 328 |
switch (tapType) { |
| 329 |
case TAP_TYPE_TOUCH: |
| 330 |
// The user has started touching the screen. |
| 331 |
break; |
| 332 |
case TAP_TYPE_TOUCH_CANCEL: |
| 333 |
// The user has started a different gesture or otherwise cancelled the tap. |
| 334 |
break; |
| 335 |
case TAP_TYPE_TAP: |
| 336 |
// The user has completed the tap gesture. |
| 337 |
// TODO: Add code to handle the tap gesture. |
| 338 |
Toast.makeText(getApplicationContext(), R.string.message, Toast.LENGTH_SHORT) |
| 339 |
.show(); |
| 340 |
break; |
| 341 |
} |
| 342 |
invalidate(); |
| 343 |
} |
| 344 |
</#if> |
| 345 |
@Override |
| 346 |
public void onDraw(Canvas canvas, Rect bounds) { |
| 347 |
long now = System.currentTimeMillis(); |
| 348 |
mCalendar.setTimeInMillis(now); |
| 349 |
|
| 350 |
if (mAmbient && (mLowBitAmbient || mBurnInProtection)) { |
| 351 |
canvas.drawColor(Color.BLACK); |
| 352 |
} else if (mAmbient) { |
| 353 |
canvas.drawBitmap(mGrayBackgroundBitmap, 0, 0, mBackgroundPaint); |
| 354 |
} else { |
| 355 |
canvas.drawBitmap(mBackgroundBitmap, 0, 0, mBackgroundPaint); |
| 356 |
} |
| 357 |
|
| 358 |
/* |
| 359 |
* Draw ticks. Usually you will want to bake this directly into the photo, but in |
| 360 |
* cases where you want to allow users to select their own photos, this dynamically |
| 361 |
* creates them on top of the photo. |
| 362 |
*/ |
| 363 |
float innerTickRadius = mCenterX - 10; |
| 364 |
float outerTickRadius = mCenterX; |
| 365 |
for (int tickIndex = 0; tickIndex < 12; tickIndex++) { |
| 366 |
float tickRot = (float) (tickIndex * Math.PI * 2 / 12); |
| 367 |
float innerX = (float) Math.sin(tickRot) * innerTickRadius; |
| 368 |
float innerY = (float) -Math.cos(tickRot) * innerTickRadius; |
| 369 |
float outerX = (float) Math.sin(tickRot) * outerTickRadius; |
| 370 |
float outerY = (float) -Math.cos(tickRot) * outerTickRadius; |
| 371 |
canvas.drawLine(mCenterX + innerX, mCenterY + innerY, |
| 372 |
mCenterX + outerX, mCenterY + outerY, mTickAndCirclePaint); |
| 373 |
} |
| 374 |
|
| 375 |
/* |
| 376 |
* These calculations reflect the rotation in degrees per unit of time, e.g., |
| 377 |
* 360 / 60 = 6 and 360 / 12 = 30. |
| 378 |
*/ |
| 379 |
final float seconds = |
| 380 |
(mCalendar.get(Calendar.SECOND) + mCalendar.get(Calendar.MILLISECOND) / 1000f); |
| 381 |
final float secondsRotation = seconds * 6f; |
| 382 |
|
| 383 |
final float minutesRotation = mCalendar.get(Calendar.MINUTE) * 6f; |
| 384 |
|
| 385 |
final float hourHandOffset = mCalendar.get(Calendar.MINUTE) / 2f; |
| 386 |
final float hoursRotation = (mCalendar.get(Calendar.HOUR) * 30) + hourHandOffset; |
| 387 |
|
| 388 |
/* |
| 389 |
* Save the canvas state before we can begin to rotate it. |
| 390 |
*/ |
| 391 |
canvas.save(); |
| 392 |
|
| 393 |
canvas.rotate(hoursRotation, mCenterX, mCenterY); |
| 394 |
canvas.drawLine( |
| 395 |
mCenterX, |
| 396 |
mCenterY - CENTER_GAP_AND_CIRCLE_RADIUS, |
| 397 |
mCenterX, |
| 398 |
mCenterY - sHourHandLength, |
| 399 |
mHourPaint); |
| 400 |
|
| 401 |
canvas.rotate(minutesRotation - hoursRotation, mCenterX, mCenterY); |
| 402 |
canvas.drawLine( |
| 403 |
mCenterX, |
| 404 |
mCenterY - CENTER_GAP_AND_CIRCLE_RADIUS, |
| 405 |
mCenterX, |
| 406 |
mCenterY - sMinuteHandLength, |
| 407 |
mMinutePaint); |
| 408 |
|
| 409 |
/* |
| 410 |
* Ensure the "seconds" hand is drawn only when we are in interactive mode. |
| 411 |
* Otherwise, we only update the watch face once a minute. |
| 412 |
*/ |
| 413 |
if (!mAmbient) { |
| 414 |
canvas.rotate(secondsRotation - minutesRotation, mCenterX, mCenterY); |
| 415 |
canvas.drawLine( |
| 416 |
mCenterX, |
| 417 |
mCenterY - CENTER_GAP_AND_CIRCLE_RADIUS, |
| 418 |
mCenterX, |
| 419 |
mCenterY - mSecondHandLength, |
| 420 |
mSecondPaint); |
| 421 |
|
| 422 |
} |
| 423 |
canvas.drawCircle( |
| 424 |
mCenterX, |
| 425 |
mCenterY, |
| 426 |
CENTER_GAP_AND_CIRCLE_RADIUS, |
| 427 |
mTickAndCirclePaint); |
| 428 |
|
| 429 |
/* Restore the canvas' original orientation. */ |
| 430 |
canvas.restore(); |
| 431 |
|
| 432 |
/* Draw rectangle behind peek card in ambient mode to improve readability. */ |
| 433 |
if (mAmbient) { |
| 434 |
canvas.drawRect(mPeekCardBounds, mBackgroundPaint); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
@Override |
| 439 |
public void onVisibilityChanged(boolean visible) { |
| 440 |
super.onVisibilityChanged(visible); |
| 441 |
|
| 442 |
if (visible) { |
| 443 |
registerReceiver(); |
| 444 |
/* Update time zone in case it changed while we weren't visible. */ |
| 445 |
mCalendar.setTimeZone(TimeZone.getDefault()); |
| 446 |
invalidate(); |
| 447 |
} else { |
| 448 |
unregisterReceiver(); |
| 449 |
} |
| 450 |
|
| 451 |
/* Check and trigger whether or not timer should be running (only in active mode). */ |
| 452 |
updateTimer(); |
| 453 |
} |
| 454 |
|
| 455 |
@Override |
| 456 |
public void onPeekCardPositionUpdate(Rect rect) { |
| 457 |
super.onPeekCardPositionUpdate(rect); |
| 458 |
mPeekCardBounds.set(rect); |
| 459 |
} |
| 460 |
|
| 461 |
private void registerReceiver() { |
| 462 |
if (mRegisteredTimeZoneReceiver) { |
| 463 |
return; |
| 464 |
} |
| 465 |
mRegisteredTimeZoneReceiver = true; |
| 466 |
IntentFilter filter = new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED); |
| 467 |
${serviceClass}.this.registerReceiver(mTimeZoneReceiver, filter); |
| 468 |
} |
| 469 |
|
| 470 |
private void unregisterReceiver() { |
| 471 |
if (!mRegisteredTimeZoneReceiver) { |
| 472 |
return; |
| 473 |
} |
| 474 |
mRegisteredTimeZoneReceiver = false; |
| 475 |
${serviceClass}.this.unregisterReceiver(mTimeZoneReceiver); |
| 476 |
} |
| 477 |
|
| 478 |
/** |
| 479 |
* Starts/stops the {@link #mUpdateTimeHandler} timer based on the state of the watch face. |
| 480 |
*/ |
| 481 |
private void updateTimer() { |
| 482 |
mUpdateTimeHandler.removeMessages(MSG_UPDATE_TIME); |
| 483 |
if (shouldTimerBeRunning()) { |
| 484 |
mUpdateTimeHandler.sendEmptyMessage(MSG_UPDATE_TIME); |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* Returns whether the {@link #mUpdateTimeHandler} timer should be running. The timer |
| 490 |
* should only run in active mode. |
| 491 |
*/ |
| 492 |
private boolean shouldTimerBeRunning() { |
| 493 |
return isVisible() && !mAmbient; |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* Handle updating the time periodically in interactive mode. |
| 498 |
*/ |
| 499 |
private void handleUpdateTimeMessage() { |
| 500 |
invalidate(); |
| 501 |
if (shouldTimerBeRunning()) { |
| 502 |
long timeMs = System.currentTimeMillis(); |
| 503 |
long delayMs = INTERACTIVE_UPDATE_RATE_MS |
| 504 |
- (timeMs % INTERACTIVE_UPDATE_RATE_MS); |
| 505 |
mUpdateTimeHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIME, delayMs); |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
private static class EngineHandler extends Handler { |
| 511 |
private final WeakReference<${serviceClass}.Engine> mWeakReference; |
| 512 |
|
| 513 |
public EngineHandler(${serviceClass}.Engine reference) { |
| 514 |
mWeakReference = new WeakReference<>(reference); |
| 515 |
} |
| 516 |
|
| 517 |
@Override |
| 518 |
public void handleMessage(Message msg) { |
| 519 |
${serviceClass}.Engine engine = mWeakReference.get(); |
| 520 |
if (engine != null) { |
| 521 |
switch (msg.what) { |
| 522 |
case MSG_UPDATE_TIME: |
| 523 |
engine.handleUpdateTimeMessage(); |
| 524 |
break; |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
} |