Skip to content

Most visited

Recently visited

navigation
XYZTouristAttractions / Wearable / src / com.example.android.xyztouristattractions / service /

ListenerService.java

1
/*
2
 * Copyright 2015 Google Inc. All rights reserved.
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.xyztouristattractions.service;
18
 
19
import android.app.Notification;
20
import android.app.PendingIntent;
21
import android.content.Intent;
22
import android.graphics.Bitmap;
23
import android.net.Uri;
24
import android.support.v4.app.NotificationCompat;
25
import android.support.v4.app.NotificationManagerCompat;
26
import android.util.Log;
27
 
28
import com.example.android.xyztouristattractions.R;
29
import com.example.android.xyztouristattractions.common.Constants;
30
import com.example.android.xyztouristattractions.common.Utils;
31
import com.example.android.xyztouristattractions.ui.AttractionsActivity;
32
import com.google.android.gms.common.ConnectionResult;
33
import com.google.android.gms.common.api.GoogleApiClient;
34
import com.google.android.gms.wearable.DataEvent;
35
import com.google.android.gms.wearable.DataEventBuffer;
36
import com.google.android.gms.wearable.DataMap;
37
import com.google.android.gms.wearable.DataMapItem;
38
import com.google.android.gms.wearable.MessageEvent;
39
import com.google.android.gms.wearable.Wearable;
40
import com.google.android.gms.wearable.WearableListenerService;
41
 
42
import java.util.ArrayList;
43
import java.util.concurrent.TimeUnit;
44
 
45
/**
46
 * A Wear listener service, used to receive inbound messages from
47
 * other devices.
48
 */
49
public class ListenerService extends WearableListenerService {
50
    private static final String TAG = ListenerService.class.getSimpleName();
51
 
52
    @Override
53
    public void onDataChanged(DataEventBuffer dataEvents) {
54
        Log.d(TAG, "onDataChanged: " + dataEvents);
55
 
56
        for (DataEvent event : dataEvents) {
57
            if (event.getType() == DataEvent.TYPE_CHANGED
58
                    && event.getDataItem() != null
59
                    && Constants.ATTRACTION_PATH.equals(event.getDataItem().getUri().getPath())) {
60
 
61
                DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
62
                ArrayList<DataMap> attractionsData =
63
                        dataMapItem.getDataMap().getDataMapArrayList(Constants.EXTRA_ATTRACTIONS);
64
                showNotification(dataMapItem.getUri(), attractionsData);
65
            }
66
        }
67
    }
68
 
69
    @Override
70
    public void onMessageReceived(MessageEvent messageEvent) {
71
        Log.v(TAG, "onMessageReceived: " + messageEvent);
72
 
73
        if (Constants.CLEAR_NOTIFICATIONS_PATH.equals(messageEvent.getPath())) {
74
            // Clear the local notification
75
            UtilityService.clearNotification(this);
76
        }
77
    }
78
 
79
    private void showNotification(Uri attractionsUri, ArrayList<DataMap> attractions) {
80
        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
81
                .addApi(Wearable.API)
82
                .build();
83
 
84
        ConnectionResult connectionResult = googleApiClient.blockingConnect(
85
                Constants.GOOGLE_API_CLIENT_TIMEOUT_S, TimeUnit.SECONDS);
86
 
87
        if (!connectionResult.isSuccess() || !googleApiClient.isConnected()) {
88
            Log.e(TAG, String.format(Constants.GOOGLE_API_CLIENT_ERROR_MSG,
89
                    connectionResult.getErrorCode()));
90
            return;
91
        }
92
 
93
        Intent intent = new Intent(this, AttractionsActivity.class);
94
        // Pass through the data Uri as an extra
95
        intent.putExtra(Constants.EXTRA_ATTRACTIONS_URI, attractionsUri);
96
        PendingIntent pendingIntent =
97
                PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
98
 
99
        int count = attractions.size();
100
 
101
        DataMap attraction = attractions.get(0);
102
 
103
        Bitmap bitmap = Utils.loadBitmapFromAsset(
104
                googleApiClient, attraction.getAsset(Constants.EXTRA_IMAGE));
105
 
106
        PendingIntent deletePendingIntent = PendingIntent.getService(
107
                this, 0, UtilityService.getClearRemoteNotificationsIntent(this), 0);
108
 
109
        Notification notification = new NotificationCompat.Builder(this)
110
                .setContentText(getResources().getQuantityString(
111
                        R.plurals.attractions_found, count, count))
112
                .setSmallIcon(R.mipmap.ic_launcher)
113
                .setDeleteIntent(deletePendingIntent)
114
                .addAction(new NotificationCompat.Action.Builder(R.drawable.ic_full_explore,
115
                        getString(R.string.action_explore),
116
                        pendingIntent).build())
117
                .extend(new NotificationCompat.WearableExtender()
118
                        .setBackground(bitmap)
119
                )
120
                .build();
121
 
122
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
123
        notificationManager.notify(Constants.WEAR_NOTIFICATION_ID, notification);
124
 
125
        googleApiClient.disconnect();
126
    }
127
}
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!

Follow Google Developers on WeChat

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