| 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.content.res.Resources; |
| 24 |
import android.graphics.Canvas; |
| 25 |
import android.graphics.Color; |
| 26 |
import android.graphics.Paint; |
| 27 |
import android.graphics.Rect; |
| 28 |
import android.graphics.Typeface; |
| 29 |
import android.os.Bundle; |
| 30 |
import android.os.Handler; |
| 31 |
import android.os.Message; |
| 32 |
import android.support.wearable.watchface.CanvasWatchFaceService; |
| 33 |
import android.support.wearable.watchface.WatchFaceStyle; |
| 34 |
import android.view.SurfaceHolder; |
| 35 |
import android.view.WindowInsets; |
| 36 |
<#if isInteractive>import android.widget.Toast;</#if> |
| 37 |
|
| 38 |
import java.lang.ref.WeakReference; |
| 39 |
import java.util.Calendar; |
| 40 |
import java.util.TimeZone; |
| 41 |
import java.util.concurrent.TimeUnit; |
| 42 |
|
| 43 |
/** |
| 44 |
* Digital watch face with seconds. In ambient mode, the seconds aren't displayed. On devices with |
| 45 |
* low-bit ambient mode, the text is drawn without anti-aliasing in ambient mode. |
| 46 |
*/ |
| 47 |
public class ${serviceClass} extends CanvasWatchFaceService { |
| 48 |
private static final Typeface NORMAL_TYPEFACE = |
| 49 |
Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL); |
| 50 |
|
| 51 |
/** |
| 52 |
* Update rate in milliseconds for interactive mode. We update once a second since seconds are |
| 53 |
* displayed in interactive mode. |
| 54 |
*/ |
| 55 |
private static final long INTERACTIVE_UPDATE_RATE_MS = TimeUnit.SECONDS.toMillis(1); |
| 56 |
|
| 57 |
/** |
| 58 |
* Handler message id for updating the time periodically in interactive mode. |
| 59 |
*/ |
| 60 |
private static final int MSG_UPDATE_TIME = 0; |
| 61 |
|
| 62 |
@Override |
| 63 |
public Engine onCreateEngine() { |
| 64 |
return new Engine(); |
| 65 |
} |
| 66 |
|
| 67 |
private class Engine extends CanvasWatchFaceService.Engine { |
| 68 |
final Handler mUpdateTimeHandler = new EngineHandler(this); |
| 69 |
|
| 70 |
final BroadcastReceiver mTimeZoneReceiver = new BroadcastReceiver() { |
| 71 |
@Override |
| 72 |
public void onReceive(Context context, Intent intent) { |
| 73 |
mCalendar.setTimeZone(TimeZone.getDefault()); |
| 74 |
invalidate(); |
| 75 |
} |
| 76 |
}; |
| 77 |
boolean mRegisteredTimeZoneReceiver = false; |
| 78 |
|
| 79 |
Paint mBackgroundPaint; |
| 80 |
Paint mTextPaint; |
| 81 |
|
| 82 |
boolean mAmbient; |
| 83 |
Calendar mCalendar; |
| 84 |
|
| 85 |
float mXOffset; |
| 86 |
float mYOffset; |
| 87 |
|
| 88 |
/** |
| 89 |
* Whether the display supports fewer bits for each color in ambient mode. When true, we |
| 90 |
* disable anti-aliasing in ambient mode. |
| 91 |
*/ |
| 92 |
boolean mLowBitAmbient; |
| 93 |
|
| 94 |
@Override |
| 95 |
public void onCreate(SurfaceHolder holder) { |
| 96 |
super.onCreate(holder); |
| 97 |
|
| 98 |
setWatchFaceStyle(new WatchFaceStyle.Builder(${serviceClass}.this) |
| 99 |
.setCardPeekMode(WatchFaceStyle.PEEK_MODE_VARIABLE) |
| 100 |
.setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE) |
| 101 |
.setShowSystemUiTime(false) |
| 102 |
<#if isInteractive> |
| 103 |
.setAcceptsTapEvents(true) |
| 104 |
</#if> |
| 105 |
.build()); |
| 106 |
Resources resources = ${serviceClass}.this.getResources(); |
| 107 |
mYOffset = resources.getDimension(R.dimen.digital_y_offset); |
| 108 |
|
| 109 |
mBackgroundPaint = new Paint(); |
| 110 |
mBackgroundPaint.setColor(resources.getColor(R.color.background)); |
| 111 |
|
| 112 |
mTextPaint = new Paint(); |
| 113 |
mTextPaint = createTextPaint(resources.getColor(R.color.digital_text)); |
| 114 |
|
| 115 |
mCalendar = Calendar.getInstance(); |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public void onDestroy() { |
| 120 |
mUpdateTimeHandler.removeMessages(MSG_UPDATE_TIME); |
| 121 |
super.onDestroy(); |
| 122 |
} |
| 123 |
|
| 124 |
private Paint createTextPaint(int textColor) { |
| 125 |
Paint paint = new Paint(); |
| 126 |
paint.setColor(textColor); |
| 127 |
paint.setTypeface(NORMAL_TYPEFACE); |
| 128 |
paint.setAntiAlias(true); |
| 129 |
return paint; |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public void onVisibilityChanged(boolean visible) { |
| 134 |
super.onVisibilityChanged(visible); |
| 135 |
|
| 136 |
if (visible) { |
| 137 |
registerReceiver(); |
| 138 |
|
| 139 |
// Update time zone in case it changed while we weren't visible. |
| 140 |
mCalendar.setTimeZone(TimeZone.getDefault()); |
| 141 |
invalidate(); |
| 142 |
} else { |
| 143 |
unregisterReceiver(); |
| 144 |
} |
| 145 |
|
| 146 |
// Whether the timer should be running depends on whether we're visible (as well as |
| 147 |
// whether we're in ambient mode), so we may need to start or stop the timer. |
| 148 |
updateTimer(); |
| 149 |
} |
| 150 |
|
| 151 |
private void registerReceiver() { |
| 152 |
if (mRegisteredTimeZoneReceiver) { |
| 153 |
return; |
| 154 |
} |
| 155 |
mRegisteredTimeZoneReceiver = true; |
| 156 |
IntentFilter filter = new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED); |
| 157 |
${serviceClass}.this.registerReceiver(mTimeZoneReceiver, filter); |
| 158 |
} |
| 159 |
|
| 160 |
private void unregisterReceiver() { |
| 161 |
if (!mRegisteredTimeZoneReceiver) { |
| 162 |
return; |
| 163 |
} |
| 164 |
mRegisteredTimeZoneReceiver = false; |
| 165 |
${serviceClass}.this.unregisterReceiver(mTimeZoneReceiver); |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public void onApplyWindowInsets(WindowInsets insets) { |
| 170 |
super.onApplyWindowInsets(insets); |
| 171 |
|
| 172 |
// Load resources that have alternate values for round watches. |
| 173 |
Resources resources = ${serviceClass}.this.getResources(); |
| 174 |
boolean isRound = insets.isRound(); |
| 175 |
mXOffset = resources.getDimension(isRound |
| 176 |
? R.dimen.digital_x_offset_round : R.dimen.digital_x_offset); |
| 177 |
float textSize = resources.getDimension(isRound |
| 178 |
? R.dimen.digital_text_size_round : R.dimen.digital_text_size); |
| 179 |
|
| 180 |
mTextPaint.setTextSize(textSize); |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public void onPropertiesChanged(Bundle properties) { |
| 185 |
super.onPropertiesChanged(properties); |
| 186 |
mLowBitAmbient = properties.getBoolean(PROPERTY_LOW_BIT_AMBIENT, false); |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public void onTimeTick() { |
| 191 |
super.onTimeTick(); |
| 192 |
invalidate(); |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
public void onAmbientModeChanged(boolean inAmbientMode) { |
| 197 |
super.onAmbientModeChanged(inAmbientMode); |
| 198 |
if (mAmbient != inAmbientMode) { |
| 199 |
mAmbient = inAmbientMode; |
| 200 |
if (mLowBitAmbient) { |
| 201 |
mTextPaint.setAntiAlias(!inAmbientMode); |
| 202 |
} |
| 203 |
invalidate(); |
| 204 |
} |
| 205 |
|
| 206 |
// Whether the timer should be running depends on whether we're visible (as well as |
| 207 |
// whether we're in ambient mode), so we may need to start or stop the timer. |
| 208 |
updateTimer(); |
| 209 |
} |
| 210 |
|
| 211 |
<#if isInteractive> |
| 212 |
/** |
| 213 |
* Captures tap event (and tap type) and toggles the background color if the user finishes |
| 214 |
* a tap. |
| 215 |
*/ |
| 216 |
@Override |
| 217 |
public void onTapCommand(int tapType, int x, int y, long eventTime) { |
| 218 |
switch (tapType) { |
| 219 |
case TAP_TYPE_TOUCH: |
| 220 |
// The user has started touching the screen. |
| 221 |
break; |
| 222 |
case TAP_TYPE_TOUCH_CANCEL: |
| 223 |
// The user has started a different gesture or otherwise cancelled the tap. |
| 224 |
break; |
| 225 |
case TAP_TYPE_TAP: |
| 226 |
// The user has completed the tap gesture. |
| 227 |
// TODO: Add code to handle the tap gesture. |
| 228 |
Toast.makeText(getApplicationContext(), R.string.message, Toast.LENGTH_SHORT) |
| 229 |
.show(); |
| 230 |
break; |
| 231 |
} |
| 232 |
invalidate(); |
| 233 |
} |
| 234 |
|
| 235 |
</#if> |
| 236 |
@Override |
| 237 |
public void onDraw(Canvas canvas, Rect bounds) { |
| 238 |
// Draw the background. |
| 239 |
if (isInAmbientMode()) { |
| 240 |
canvas.drawColor(Color.BLACK); |
| 241 |
} else { |
| 242 |
canvas.drawRect(0, 0, bounds.width(), bounds.height(), mBackgroundPaint); |
| 243 |
} |
| 244 |
|
| 245 |
// Draw H:MM in ambient mode or H:MM:SS in interactive mode. |
| 246 |
long now = System.currentTimeMillis(); |
| 247 |
mCalendar.setTimeInMillis(now); |
| 248 |
|
| 249 |
String text = mAmbient |
| 250 |
? String.format("%d:%02d", mCalendar.get(Calendar.HOUR), |
| 251 |
mCalendar.get(Calendar.MINUTE)) |
| 252 |
: String.format("%d:%02d:%02d", mCalendar.get(Calendar.HOUR), |
| 253 |
mCalendar.get(Calendar.MINUTE), mCalendar.get(Calendar.SECOND)); |
| 254 |
canvas.drawText(text, mXOffset, mYOffset, mTextPaint); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Starts the {@link #mUpdateTimeHandler} timer if it should be running and isn't currently |
| 259 |
* or stops it if it shouldn't be running but currently is. |
| 260 |
*/ |
| 261 |
private void updateTimer() { |
| 262 |
mUpdateTimeHandler.removeMessages(MSG_UPDATE_TIME); |
| 263 |
if (shouldTimerBeRunning()) { |
| 264 |
mUpdateTimeHandler.sendEmptyMessage(MSG_UPDATE_TIME); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Returns whether the {@link #mUpdateTimeHandler} timer should be running. The timer should |
| 270 |
* only run when we're visible and in interactive mode. |
| 271 |
*/ |
| 272 |
private boolean shouldTimerBeRunning() { |
| 273 |
return isVisible() && !isInAmbientMode(); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Handle updating the time periodically in interactive mode. |
| 278 |
*/ |
| 279 |
private void handleUpdateTimeMessage() { |
| 280 |
invalidate(); |
| 281 |
if (shouldTimerBeRunning()) { |
| 282 |
long timeMs = System.currentTimeMillis(); |
| 283 |
long delayMs = INTERACTIVE_UPDATE_RATE_MS |
| 284 |
- (timeMs % INTERACTIVE_UPDATE_RATE_MS); |
| 285 |
mUpdateTimeHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIME, delayMs); |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
private static class EngineHandler extends Handler { |
| 291 |
private final WeakReference<${serviceClass}.Engine> mWeakReference; |
| 292 |
|
| 293 |
public EngineHandler(${serviceClass}.Engine reference) { |
| 294 |
mWeakReference = new WeakReference<>(reference); |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public void handleMessage(Message msg) { |
| 299 |
${serviceClass}.Engine engine = mWeakReference.get(); |
| 300 |
if (engine != null) { |
| 301 |
switch (msg.what) { |
| 302 |
case MSG_UPDATE_TIME: |
| 303 |
engine.handleUpdateTimeMessage(); |
| 304 |
break; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
} |