zhenma.myapplication.DataLayerListenerService.java Source code

Java tutorial

Introduction

Here is the source code for zhenma.myapplication.DataLayerListenerService.java

Source

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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 zhenma.myapplication;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;

import java.util.concurrent.TimeUnit;

/**
 * Listens to DataItems and Messages from the local node.
 */
public class DataLayerListenerService extends WearableListenerService {

    private static final String TAG = "DataLayerListener";

    private static final String START_ACTIVITY_PATH = "/start-activity";
    private static final String DATA_ITEM_RECEIVED_PATH = "/data-item-received";
    public static final String COUNT_PATH = "/count";
    public static final String IMAGE_PATH = "/image";
    public static final String IMAGE_KEY = "photo";
    GoogleApiClient mGoogleApiClient;

    @Override
    public void onCreate() {
        super.onCreate();
        mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).build();
        mGoogleApiClient.connect();
    }

    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        //LOGD(TAG, "onDataChanged: " + dataEvents);
        if (!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting()) {
            ConnectionResult connectionResult = mGoogleApiClient.blockingConnect(30, TimeUnit.SECONDS);
            if (!connectionResult.isSuccess()) {
                Log.e(TAG, "DataLayerListenerService failed to connect to GoogleApiClient, " + "error code: "
                        + connectionResult.getErrorCode());
                return;
            }
        }

        // Loop through the events and send a message back to the node that created the data item.
        /*for (DataEvent event : dataEvents) {
        Uri uri = event.getDataItem().getUri();
        String path = uri.getPath();
        if (COUNT_PATH.equals(path)) {
            // Get the node id of the node that created the data item from the host portion of
            // the uri.
            String nodeId = uri.getHost();
            // Set the data of the message to be the bytes of the Uri.
            byte[] payload = uri.toString().getBytes();
            
            // Send the rpc
            Wearable.MessageApi.sendMessage(mGoogleApiClient, nodeId, DATA_ITEM_RECEIVED_PATH,
                    payload);
        }
        }*/
    }

    public void btnShowNotificationClick(String friend_id, String name, boolean if_friend) {
        int notificationId = 101;

        // Create an intent for the reply action
        Intent actionIntent = new Intent(this, MainActivity.class);
        if (if_friend) {
            actionIntent.putExtra("methodName", "noAction");
        } else {
            actionIntent.putExtra("methodName", "sendAcceptMessage");
        }
        actionIntent.putExtra("friend_id", friend_id);
        actionIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent actionPendingIntent = PendingIntent.getActivity(this, 0, actionIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        // Create the action
        NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_checked,
                getString(if_friend ? R.string.oldFriendRemind : R.string.acceptButt), actionPendingIntent).build();

        //Building notification layout
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(if_friend ? R.drawable.yellow_handshack : R.drawable.red_handshack)
                .setContentTitle(if_friend ? "Old Friend: " + name : "New Friend Request: " + name)
                .setDefaults(Notification.DEFAULT_ALL).setContentText("Swipe left!")
                .extend(new NotificationCompat.WearableExtender().addAction(action));

        // instance of the NotificationManager service
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

        // Build the notification and notify it using notification manager.
        notificationManager.notify(notificationId, notificationBuilder.build());
        Log.d("Notification", "Notify Sent!");
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        LOGD(TAG, "onMessageReceived: " + messageEvent);

        String event = messageEvent.getPath();

        String[] message = event.split("--");

        if (message[0].equals("config/stop")) {
            btnShowNotificationClick(message[1], "", false);
        } else if (message[0].equals("OLD_FRIEND")) {
            btnShowNotificationClick(message[1], message[2], true);
        }

        // Check to see if the message is to start an activity
        /*if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {
        //Intent startIntent = new Intent(this, MainActivity.class);
        //startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //startActivity(startIntent);
        btnShowNotificationClick();
        }*/
    }

    @Override
    public void onPeerConnected(Node peer) {
        LOGD(TAG, "onPeerConnected: " + peer);
    }

    @Override
    public void onPeerDisconnected(Node peer) {
        LOGD(TAG, "onPeerDisconnected: " + peer);
    }

    public static void LOGD(final String tag, String message) {
        if (Log.isLoggable(tag, Log.DEBUG)) {
            Log.d(tag, message);
        }
    }

}