• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.example.android.wearable.watchface;
18 
19 import android.graphics.Canvas;
20 import android.graphics.Color;
21 import android.graphics.Paint;
22 import android.graphics.Rect;
23 import android.support.wearable.watchface.CanvasWatchFaceService;
24 import android.support.wearable.watchface.WatchFaceStyle;
25 import android.util.Log;
26 import android.view.SurfaceHolder;
27 
28 /**
29  * Proof of concept sample watch face that demonstrates how a watch face can detect where the peek
30  * card is. This watch face draws a border around the area where the peeking card is.
31  */
32 public class CardBoundsWatchFaceService extends CanvasWatchFaceService {
33 
34     private static final String TAG = "CardBoundsWatchFace";
35 
36     @Override
onCreateEngine()37     public Engine onCreateEngine() {
38         return new Engine();
39     }
40 
41     private class Engine extends CanvasWatchFaceService.Engine {
42 
43         static final int BORDER_WIDTH_PX = 5;
44 
45         final Rect mCardBounds = new Rect();
46         final Paint mPaint = new Paint();
47 
48         @Override
onCreate(SurfaceHolder holder)49         public void onCreate(SurfaceHolder holder) {
50             if (Log.isLoggable(TAG, Log.DEBUG)) {
51                 Log.d(TAG, "onCreate");
52             }
53             super.onCreate(holder);
54             setWatchFaceStyle(new WatchFaceStyle.Builder(CardBoundsWatchFaceService.this)
55                     .setCardPeekMode(WatchFaceStyle.PEEK_MODE_VARIABLE)
56                     .setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
57                     .setShowSystemUiTime(true)
58                     .setPeekOpacityMode(WatchFaceStyle.PEEK_OPACITY_MODE_TRANSLUCENT)
59                     .build());
60         }
61 
62         @Override
onAmbientModeChanged(boolean inAmbientMode)63         public void onAmbientModeChanged(boolean inAmbientMode) {
64             if (Log.isLoggable(TAG, Log.DEBUG)) {
65                 Log.d(TAG, "onAmbientModeChanged: " + inAmbientMode);
66             }
67             super.onAmbientModeChanged(inAmbientMode);
68             invalidate();
69         }
70 
71         @Override
onPeekCardPositionUpdate(Rect bounds)72         public void onPeekCardPositionUpdate(Rect bounds) {
73             super.onPeekCardPositionUpdate(bounds);
74             if (Log.isLoggable(TAG, Log.DEBUG)) {
75                 Log.d(TAG, "onPeekCardPositionUpdate: " + bounds);
76             }
77             super.onPeekCardPositionUpdate(bounds);
78             if (!bounds.equals(mCardBounds)) {
79                 mCardBounds.set(bounds);
80                 invalidate();
81             }
82         }
83 
84         @Override
onDraw(Canvas canvas, Rect bounds)85         public void onDraw(Canvas canvas, Rect bounds) {
86             // Clear screen.
87             canvas.drawColor(isInAmbientMode() ? Color.BLACK : Color.BLUE);
88 
89             // Draw border around card in interactive mode.
90             if (!isInAmbientMode()) {
91                 mPaint.setColor(Color.MAGENTA);
92                 canvas.drawRect(mCardBounds.left - BORDER_WIDTH_PX,
93                         mCardBounds.top - BORDER_WIDTH_PX,
94                         mCardBounds.right + BORDER_WIDTH_PX,
95                         mCardBounds.bottom + BORDER_WIDTH_PX, mPaint);
96             }
97 
98             // Fill area under card.
99             mPaint.setColor(isInAmbientMode() ? Color.RED : Color.GREEN);
100             canvas.drawRect(mCardBounds, mPaint);
101         }
102     }
103 }
104