/* * 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 com.example.android.wearable.synchronizednotifications; import android.app.PendingIntent; import android.content.Intent; import android.support.v4.app.Fragment; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; import android.util.Log; import android.view.MenuItem; import com.example.android.wearable.synchronizednotifications.common.Constants; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.wearable.DataApi; import com.google.android.gms.wearable.PutDataMapRequest; import com.google.android.gms.wearable.PutDataRequest; import com.google.android.gms.wearable.Wearable; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; /** * A simple fragment that presents three buttons that would trigger three different combinations of * notifications on the handset and the watch: *
setLocalOnly(true)
. If withDismissal
is set to true
, a
* {@link android.app.PendingIntent} will be added to handle the dismissal of notification to
* be able to remove the mirrored notification on the wearable.
*/
private void buildLocalOnlyNotification(String title, String content, int notificationId,
boolean withDismissal) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this.getActivity());
builder.setContentTitle(title)
.setContentText(content)
.setLocalOnly(true)
.setSmallIcon(R.drawable.ic_launcher);
if (withDismissal) {
Intent dismissIntent = new Intent(Constants.ACTION_DISMISS);
dismissIntent.putExtra(Constants.KEY_NOTIFICATION_ID, Constants.BOTH_ID);
PendingIntent pendingIntent =
PendingIntent.getService(
this.getActivity(),
0,
dismissIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setDeleteIntent(pendingIntent);
}
NotificationManagerCompat.from(this.getActivity()).notify(notificationId, builder.build());
}
/**
* Builds a DataItem that on the wearable will be interpreted as a request to show a
* notification. The result will be a notification that only shows up on the wearable.
*/
private void buildWearableOnlyNotification(String title, String content, String path) {
if (mGoogleApiClient.isConnected()) {
PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(path);
putDataMapRequest.getDataMap().putString(Constants.KEY_CONTENT, content);
putDataMapRequest.getDataMap().putString(Constants.KEY_TITLE, title);
PutDataRequest request = putDataMapRequest.asPutDataRequest();
request.setUrgent();
Wearable.DataApi.putDataItem(mGoogleApiClient, request)
.setResultCallback(new ResultCallback