aboutsummaryrefslogtreecommitdiff
path: root/wearable/wear/XYZTouristAttractions/Wearable/src/main/java/com/example/android/xyztouristattractions/service/UtilityService.java
blob: 8a4fea56aa098fee65882baeb0def8a857fc9b70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright 2015 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.xyztouristattractions.service;

import android.app.IntentService;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.example.android.xyztouristattractions.R;
import com.example.android.xyztouristattractions.common.Constants;
import com.example.android.xyztouristattractions.common.Utils;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.CapabilityApi;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.Wearable;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * A utility IntentService, used for a variety of asynchronous background
 * operations that do not necessarily need to be tied to a UI.
 */
public class UtilityService extends IntentService {

    private static final String TAG = UtilityService.class.getSimpleName();

    private static final String ACTION_CLEAR_NOTIFICATION = "clear_notification";
    private static final String ACTION_CLEAR_REMOTE_NOTIFICATIONS = "clear_remote_notifications";
    private static final String ACTION_START_DEVICE_ACTIVITY = "start_device_activity";
    private static final String EXTRA_START_PATH = "start_path";
    private static final String EXTRA_START_ACTIVITY_INFO = "start_activity_info";
    private static final long GET_CAPABILITY_TIMEOUT_S = 10;

    public static void clearNotification(Context context) {
        Intent intent = new Intent(context, UtilityService.class);
        intent.setAction(UtilityService.ACTION_CLEAR_NOTIFICATION);
        context.startService(intent);
    }

    public static void clearRemoteNotifications(Context context) {
        context.startService(getClearRemoteNotificationsIntent(context));
    }

    public static Intent getClearRemoteNotificationsIntent(Context context) {
        Intent intent = new Intent(context, UtilityService.class);
        intent.setAction(UtilityService.ACTION_CLEAR_REMOTE_NOTIFICATIONS);
        return intent;
    }

    /**
     * Trigger a message that asks the master device to start an activity.
     *
     * @param context the context
     * @param path the path that will be sent via the wearable message API
     * @param name the tourist attraction name
     * @param city the tourist attraction city
     */
    public static void startDeviceActivity(Context context, String path, String name, String city) {
        Intent intent = new Intent(context, UtilityService.class);
        intent.setAction(UtilityService.ACTION_START_DEVICE_ACTIVITY);
        String extraInfo;
        if (Constants.START_ATTRACTION_PATH.equals(path)) {
            extraInfo = name;
        } else {
            extraInfo = name + ", " + city;
        }
        intent.putExtra(EXTRA_START_ACTIVITY_INFO, extraInfo);
        intent.putExtra(EXTRA_START_PATH, path);
        context.startService(intent);
    }

    public UtilityService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String action = intent != null ? intent.getAction() : null;
        if (ACTION_CLEAR_NOTIFICATION.equals(action)) {
            clearNotificationInternal();
        } else if (ACTION_CLEAR_REMOTE_NOTIFICATIONS.equals(action)) {
            clearRemoteNotificationsInternal();
        } else if (ACTION_START_DEVICE_ACTIVITY.equals(action)) {
            startDeviceActivityInternal(intent.getStringExtra(EXTRA_START_PATH),
                    intent.getStringExtra(EXTRA_START_ACTIVITY_INFO));
        }
    }

    /**
     * Clear the local notifications
     */
    private void clearNotificationInternal() {
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancel(Constants.WEAR_NOTIFICATION_ID);
    }

    /**
     * Trigger a message to ask other devices to clear their notifications
     */
    private void clearRemoteNotificationsInternal() {
        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .build();

        ConnectionResult connectionResult = googleApiClient.blockingConnect(
                Constants.GOOGLE_API_CLIENT_TIMEOUT_S, TimeUnit.SECONDS);

        if (connectionResult.isSuccess() && googleApiClient.isConnected()) {
            Iterator<String> itr = Utils.getNodes(googleApiClient).iterator();
            while (itr.hasNext()) {
                // Loop through all connected nodes
                Wearable.MessageApi.sendMessage(
                        googleApiClient, itr.next(), Constants.CLEAR_NOTIFICATIONS_PATH, null);
            }
        }

        googleApiClient.disconnect();
    }

    /**
     * Sends the actual message to ask other devices that are capable of showing "details" to start
     * the appropriate activity
     *
     * @param path the path to pass to the wearable message API
     * @param extraInfo extra info that varies based on the path being sent
     */
    private void startDeviceActivityInternal(String path, String extraInfo) {
        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .build();

        ConnectionResult connectionResult = googleApiClient.blockingConnect(
                Constants.GOOGLE_API_CLIENT_TIMEOUT_S, TimeUnit.SECONDS);

        if (connectionResult.isSuccess() && googleApiClient.isConnected()) {
            CapabilityApi.GetCapabilityResult result = Wearable.CapabilityApi.getCapability(
                    googleApiClient,
                    getApplicationContext().getString(R.string.show_detail_capability_name),
                    CapabilityApi.FILTER_REACHABLE)
                    .await(GET_CAPABILITY_TIMEOUT_S, TimeUnit.SECONDS);
            if (result.getStatus().isSuccess()) {
                Set<Node> nodes = result.getCapability().getNodes();
                for (Node node : nodes) {
                    Wearable.MessageApi.sendMessage(
                            googleApiClient, node.getId(), path, extraInfo.getBytes());
                }
            } else {
                Log.e(TAG, "startDeviceActivityInternal() Failed to get capabilities, status: "
                        + result.getStatus().getStatusMessage());
            }

            googleApiClient.disconnect();
        }
    }

}