Skip to content

Most visited

Recently visited

navigation
WatchFace / Wearable / src / com.example.android.wearable.watchface /

DigitalWatchFaceConfigListenerService.java

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.os.Bundle;
20
import android.util.Log;
21
 
22
import com.google.android.gms.common.ConnectionResult;
23
import com.google.android.gms.common.api.GoogleApiClient;
24
import com.google.android.gms.wearable.DataMap;
25
import com.google.android.gms.wearable.MessageEvent;
26
import com.google.android.gms.wearable.Wearable;
27
import com.google.android.gms.wearable.WearableListenerService;
28
 
29
import java.util.concurrent.TimeUnit;
30
 
31
/**
32
 * A {@link WearableListenerService} listening for {@link DigitalWatchFaceService} config messages
33
 * and updating the config {@link com.google.android.gms.wearable.DataItem} accordingly.
34
 */
35
public class DigitalWatchFaceConfigListenerService extends WearableListenerService
36
        implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
37
    private static final String TAG = "DigitalListenerService";
38
 
39
    private GoogleApiClient mGoogleApiClient;
40
 
41
    @Override // WearableListenerService
42
    public void onMessageReceived(MessageEvent messageEvent) {
43
 
44
        if (Log.isLoggable(TAG, Log.DEBUG)) {
45
            Log.d(TAG, "onMessageReceived: " + messageEvent);
46
        }
47
 
48
        if (!messageEvent.getPath().equals(DigitalWatchFaceUtil.PATH_WITH_FEATURE)) {
49
            return;
50
        }
51
        byte[] rawData = messageEvent.getData();
52
        // It's allowed that the message carries only some of the keys used in the config DataItem
53
        // and skips the ones that we don't want to change.
54
        DataMap configKeysToOverwrite = DataMap.fromByteArray(rawData);
55
        if (Log.isLoggable(TAG, Log.DEBUG)) {
56
            Log.d(TAG, "Received watch face config message: " + configKeysToOverwrite);
57
        }
58
 
59
        if (mGoogleApiClient == null) {
60
            mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this)
61
                    .addOnConnectionFailedListener(this).addApi(Wearable.API).build();
62
        }
63
        if (!mGoogleApiClient.isConnected()) {
64
            ConnectionResult connectionResult =
65
                    mGoogleApiClient.blockingConnect(30, TimeUnit.SECONDS);
66
 
67
            if (!connectionResult.isSuccess()) {
68
                Log.e(TAG, "Failed to connect to GoogleApiClient.");
69
                return;
70
            }
71
        }
72
 
73
        DigitalWatchFaceUtil.overwriteKeysInConfigDataMap(mGoogleApiClient, configKeysToOverwrite);
74
    }
75
 
76
    @Override // GoogleApiClient.ConnectionCallbacks
77
    public void onConnected(Bundle connectionHint) {
78
        if (Log.isLoggable(TAG, Log.DEBUG)) {
79
            Log.d(TAG, "onConnected: " + connectionHint);
80
        }
81
    }
82
 
83
    @Override  // GoogleApiClient.ConnectionCallbacks
84
    public void onConnectionSuspended(int cause) {
85
        if (Log.isLoggable(TAG, Log.DEBUG)) {
86
            Log.d(TAG, "onConnectionSuspended: " + cause);
87
        }
88
    }
89
 
90
    @Override  // GoogleApiClient.OnConnectionFailedListener
91
    public void onConnectionFailed(ConnectionResult result) {
92
        if (Log.isLoggable(TAG, Log.DEBUG)) {
93
            Log.d(TAG, "onConnectionFailed: " + result);
94
        }
95
    }
96
}
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.