Skip to content

Most visited

Recently visited

navigation
SynchronizedNotifications / Application / src / com.example.android.wearable.synchronizednotifications /

DismissListener.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.synchronizednotifications;
18
 
19
import static com.google.android.gms.wearable.PutDataRequest.WEAR_URI_SCHEME;
20
 
21
import android.content.Intent;
22
import android.net.Uri;
23
import android.os.Bundle;
24
import android.support.v4.app.NotificationManagerCompat;
25
import android.util.Log;
26
 
27
import com.example.android.wearable.synchronizednotifications.common.Constants;
28
import com.google.android.gms.common.ConnectionResult;
29
import com.google.android.gms.common.api.GoogleApiClient;
30
import com.google.android.gms.common.api.ResultCallback;
31
import com.google.android.gms.wearable.DataApi;
32
import com.google.android.gms.wearable.DataEvent;
33
import com.google.android.gms.wearable.DataEventBuffer;
34
import com.google.android.gms.wearable.Wearable;
35
import com.google.android.gms.wearable.WearableListenerService;
36
 
37
/**
38
 * A {@link com.google.android.gms.wearable.WearableListenerService} that is invoked when certain
39
 * notifications are dismissed from either the phone or watch.
40
 */
41
public class DismissListener extends WearableListenerService
42
        implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
43
        ResultCallback<DataApi.DeleteDataItemsResult> {
44
 
45
    private static final String TAG = "DismissListener";
46
    private GoogleApiClient mGoogleApiClient;
47
 
48
    @Override
49
    public void onCreate() {
50
        super.onCreate();
51
        mGoogleApiClient = new GoogleApiClient.Builder(this)
52
                .addApi(Wearable.API)
53
                .addConnectionCallbacks(this)
54
                .addOnConnectionFailedListener(this)
55
                .build();
56
    }
57
 
58
    @Override
59
    public void onDataChanged(DataEventBuffer dataEvents) {
60
        for (DataEvent dataEvent : dataEvents) {
61
            if (dataEvent.getType() == DataEvent.TYPE_DELETED) {
62
                if (Constants.BOTH_PATH.equals(dataEvent.getDataItem().getUri().getPath())) {
63
                    // notification on the phone should be dismissed
64
                    NotificationManagerCompat.from(this).cancel(Constants.BOTH_ID);
65
                }
66
            }
67
        }
68
    }
69
 
70
    @Override
71
    public int onStartCommand(Intent intent, int flags, int startId) {
72
        if (null != intent) {
73
            String action = intent.getAction();
74
            if (Constants.ACTION_DISMISS.equals(action)) {
75
                // We need to dismiss the wearable notification. We delete the DataItem that
76
                // created the notification to inform the wearable.
77
                int notificationId = intent.getIntExtra(Constants.KEY_NOTIFICATION_ID, -1);
78
                if (notificationId == Constants.BOTH_ID) {
79
                    dismissWearableNotification(notificationId);
80
                }
81
            }
82
        }
83
        return super.onStartCommand(intent, flags, startId);
84
    }
85
 
86
    /**
87
     * Removes the DataItem that was used to create a notification on the watch. By deleting the
88
     * data item, a {@link com.google.android.gms.wearable.WearableListenerService} on the watch
89
     * will be notified and the notification on the watch will be removed. To
90
     * access the Wearable DataApi, we first need to ensure the GoogleApiClient is ready,
91
     * which will then run the onConnected callback were the data removal is
92
     * defined.
93
     *
94
     * @param id The ID of the notification that should be removed
95
     */
96
    private void dismissWearableNotification(final int id) {
97
        mGoogleApiClient.connect();
98
    }
99
 
100
    @Override // ConnectionCallbacks
101
    public void onConnected(Bundle bundle) {
102
        final Uri dataItemUri =
103
                new Uri.Builder().scheme(WEAR_URI_SCHEME).path(Constants.BOTH_PATH).build();
104
        if (Log.isLoggable(TAG, Log.DEBUG)) {
105
            Log.d(TAG, "Deleting Uri: " + dataItemUri.toString());
106
        }
107
        Wearable.DataApi.deleteDataItems(
108
                mGoogleApiClient, dataItemUri).setResultCallback(this);
109
    }
110
 
111
    @Override // ConnectionCallbacks
112
    public void onConnectionSuspended(int i) {
113
    }
114
 
115
    @Override // OnConnectionFailedListener
116
    public void onConnectionFailed(ConnectionResult connectionResult) {
117
        Log.e(TAG, "Failed to connect to the Google API client");
118
    }
119
 
120
    @Override // ResultCallback<DataApi.DeleteDataItemsResult>
121
    public void onResult(DataApi.DeleteDataItemsResult deleteDataItemsResult) {
122
        if (!deleteDataItemsResult.getStatus().isSuccess()) {
123
            Log.e(TAG, "dismissWearableNotification(): failed to delete DataItem");
124
        }
125
        mGoogleApiClient.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.